Milerius/shiva

View on GitHub
cmake/directory.cmake

Summary

Maintainability
Test Coverage

##! Macro to get all the subdirectories of the current directory
macro(SUBDIRLIST result curdir)
    file(GLOB children RELATIVE ${curdir} ${curdir}/*)
    set(dirlist "")
    foreach (child ${children})
        if (IS_DIRECTORY ${curdir}/${child})
            list(APPEND dirlist ${child})
        endif ()
    endforeach ()
    set(${result} ${dirlist})
endmacro()

function(group_target_sources target)
    get_target_property(sources ${target} INTERFACE_SOURCES)
    foreach (file ${sources})
        get_filename_component(path "${file}" ABSOLUTE)
        get_filename_component(path "${path}" PATH)
        file(RELATIVE_PATH path ${PROJECT_SOURCE_DIR} "${path}")
        string(REGEX REPLACE "/" "\\\\" win_path "${path}")
        source_group("${win_path}" REGULAR_EXPRESSION "${path}/[^/\\]+\\..*")
    endforeach ()
endfunction()

function(get_libraries OUTPUT TARGET)
    get_target_property(libs ${TARGET} INTERFACE_LINK_LIBRARIES)
    list(APPEND VISITED_TARGETS ${TARGET})
    set(LIB_FILES "")
    message("libs -> ${libs}")
    foreach (current_lib ${libs})
        if (TARGET ${current_lib})
            list(FIND VISITED_TARGETS ${current_lib} VISITED)
            if (${VISITED} EQUAL -1)
                string(FIND "${current_lib}" "shiva::" pos1)
                if (${pos1} GREATER -1)
                    get_libraries(LINK_LIB_FILES ${current_lib})
                    list(APPEND LIB_FILES ${current_lib} ${LINK_LIB_FILES})
                endif ()
            endif ()
        endif ()
    endforeach (current_lib ${libs})
    set(VISITED_TARGETS ${VISITED_TARGETS} PARENT_SCOPE)
    set(${OUTPUT} ${LIB_FILES} PARENT_SCOPE)
endfunction()

macro(magic_source_group TARGET)
    if (MSVC AND USE_PROJECT_IN_AN_IDE)
        get_libraries(__result__ ${TARGET})
        foreach (__current_lib__ ${__result__})
            group_target_sources(${__current_lib__})
        endforeach ()
    endif ()
endmacro()