Custom target to do all the work with a simple command:

Custom target to do all the work with a simple command:

add_custom_target(run_all COMMAND ${CMAKE_COMMAND} -E echo \">>>> Running all variants\" WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

set(common_flags "-DGENERATE_ASSEMBLY=${GENERATE_ASSEMBLY} -DCPU_MODEL=\"${CPU_MODEL}\"")

if(WIN32)
    list(APPEND VARIANTS "-G \"Visual Studio 14\" -DCMAKE_BUILD_TYPE=Release")
    list(APPEND VARIANTS "-G \"Visual Studio 14\" -DCMAKE_BUILD_TYPE=Debug")
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Release")
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_BUILD_TYPE=Debug")
elseif(CYGWIN)
    # Nothing
elseif(UNIX)
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_BUILD_TYPE=Release")
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=$(which g++) -DCMAKE_BUILD_TYPE=Debug")
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_BUILD_TYPE=Release")
    list(APPEND VARIANTS "-G \"Unix Makefiles\" -DCMAKE_CXX_COMPILER=$(which clang++) -DCMAKE_BUILD_TYPE=Debug")
endif()
list(LENGTH VARIANTS variants_count)
math(EXPR end "${variants_count} - 1")

foreach(i RANGE ${end})
    list(GET VARIANTS ${i} variant)
    string(REGEX REPLACE " " "-" variant_label "${variant}")

    add_custom_command(TARGET run_all
                       COMMAND ${CMAKE_COMMAND} -E echo "Setting up '${variant}'"
                       COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/${i}
                       COMMAND ${CMAKE_COMMAND} -E sleep 1
                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR})

    add_custom_command(TARGET run_all
                       COMMAND ${CMAKE_COMMAND} -E echo "Building variant: '${variant}'"
                       COMMAND ${CMAKE_COMMAND} ../.. ${variant} ${common_flags}
                       COMMAND ${CMAKE_COMMAND} --build .
                       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/${i})

    foreach(target ${TARGETS})
        add_custom_command(TARGET run_all
                           COMMAND ${CMAKE_COMMAND} -E echo "Running target ${target}"
                           COMMAND ${CMAKE_COMMAND} --build . --target run_${target}
                           WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/${i})
    endforeach()
endforeach()

No comments:

Post a Comment