How can I build multiple targets using cmake --build

前端 未结 2 1865
梦谈多话
梦谈多话 2020-12-10 16:16

I have a CMake build with a bunch of different targets A, B, C, etc. An external application is tasked with building, and currently do

相关标签:
2条回答
  • 2020-12-10 16:48

    Maybe not the "nicest" way, but definitely a solution would be to introduce a custom top-level target and make the needed targets depend on it. For example:

    cmake_minimum_required(VERSION 3.9) # can be lower
    
    project(demo LANGUAGES C)
    
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/a.c"
        [[
        #include <stdio.h>
        int main(void) { printf("a\n"); return 0; }
        ]])
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/b.c"
        [[
        #include <stdio.h>
        int main(void) { printf("b\n"); return 0; }
        ]])
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/c.c"
        [[
        #include <stdio.h>
        int main(void) { printf("c\n"); return 0; }
        ]])
    
    add_executable(A "${CMAKE_CURRENT_BINARY_DIR}/a.c")
    add_executable(B "${CMAKE_CURRENT_BINARY_DIR}/b.c")
    add_executable(C "${CMAKE_CURRENT_BINARY_DIR}/c.c")
    
    set(DEMO_ENABLE_TARGETS "" CACHE
        STRING "Targets to be built in demo simultaneously (default: none)")
    
    if(NOT "${DEMO_ENABLE_TARGETS}" STREQUAL "")
        add_custom_target(enabled_targets)
        foreach(target IN LISTS DEMO_ENABLE_TARGETS)
            add_dependencies(enabled_targets ${target})
        endforeach()
    endif()
    

    Then invoke

    $ cmake -H. -Bbuild -DDEMO_ENABLE_TARGETS="B;C"
    $ cmake --build build --target enabled_targets
    

    and only B and C will be built.

    Note that you have to specify DEMO_ENABLE_TARGETS's contents as a list, otherwise it'll break.

    0 讨论(0)
  • 2020-12-10 16:55

    CMake version 3.15 added support for this feature. Simply list all targets on the command line as follows:

    cmake --build . --target Library1 Library2
    
    0 讨论(0)
提交回复
热议问题