How do I iterate over all CMake targets programmatically?

前端 未结 3 1477
眼角桃花
眼角桃花 2021-02-13 01:18

Is there a way to get all targets of a CMake project from within the top level CMakeLists.txt, i.e. iterate over the targets programmatically?

The reason I

3条回答
  •  终归单人心
    2021-02-13 02:10

    If you want to go over a list of targets, set your CMakeLists.txt up to provide that list in the first place.

    if ( CMAKE_GENERATOR MATCHES "Xcode" )
        include(sanitize_xcode)
    endif()
    
    # A list of executables to build
    set( project_EXECUTABLES
         foo
         bar
       )
    
    # List of sources for each executable, following some naming scheme
    
    # foo
    set( EXE_foo_SOURCES
         foo/main.c
       )
    
    # bar
    set( EXE_bar_SOURCES
         bar/main.c
       )
    
    # For each executable in the list...
    foreach( exe ${project_EXECUTABLES} )
        # declare the target...
        add_executable( ${exe} ${EXE_${exe}_SOURCES} )
    
        # ...and do whatever additional configuration you need
        if ( CMAKE_GENERATOR MATCHES "Xcode" )
            sanitize_xcode( ${exe} )
        endif()
    endforeach()
    

提交回复
热议问题