What is the name of CMake's default build target?

后端 未结 2 1003
醉酒成梦
醉酒成梦 2020-12-21 10:33

I have a custom target, and I want it to depend on the default target (the one that is built with make).

add_custom_target(foo ....)
add_depend         


        
相关标签:
2条回答
  • 2020-12-21 10:59

    The default build target does not exist as a CMake target at CMake configure time. It is only exists in the generated build system. Therefore it is not possible to have the default target depend on a custom target.

    0 讨论(0)
  • 2020-12-21 11:07

    I think a possible solution depends strongly on the use case. E.g. if this is for executing a test after the system has been build you would use CTest instead of calling make directly.

    To your CMakeLists.txt you would add:

     add_test(NAME foo COMMAND ...)
    

    and then use CTest for building and executing:

     ctest --build-and-test ...
    

    More generally speaking and not considering the question of why you would like to do it - I think the best thing would be to just name and rely on concrete target dependencies instead of just taking ALL targets - I just wanted to add two possibilities to do what you wanted to do.

    One would be to determine/track the list of all targets used as discussed here. This would look e.g. for library targets like this (getting your own/private GlobalTargetList):

    macro(add_library _target)
        _add_library(${_target} ${ARGN})
        set_property(GLOBAL APPEND PROPERTY GlobalTargetList ${_target})
    endmacro()
    

    and use it at the end of your main CMakeLists.txt with

    get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
    add_dependencies(foo ${_allTargets})
    

    Edit: Global BUILDSYSTEM_TARGETS property was released with CMake 3.7

    The second - less favorable - approach does require that the foo target is not part of the ALL build (otherwise you end-up in an endless loop):

    add_custom_target(foo)
    set_target_properties(foo PROPERTIES EXCLUDE_FROM_ALL 1) 
    
    add_custom_command(
        TARGET foo
        PRE_BUILD
        COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target ALL_BUILD --config $<CONFIGURATION>
    )
    
    0 讨论(0)
提交回复
热议问题