def sort_libraries(libraries):
    """Sort libraries according to their necessary include order"""
    for i in range(len(libraries)):
        for j in range(i, len(libraries)):
            if libraries[i].depends_on(libraries[j]) or libraries[i].has_addon_for(libraries[j]):
                tmp = libraries[i]
                libraries[i] = libraries[j]
                libraries[j] = tmp
    return libraries