cmake: get add_library() names

2019-06-14 12:09发布

问题:

Is there a way to get the names of all add_library() calls? E.g.

add_library(lib1
  some.cpp
)

add_library(lib2
  some.cpp
)

add_library(lib3
  some.cpp
)

# And then somehow get in a variable MY_LIBRARIES_NAMES = lib1 lib2 lib3 

Is there any cmake call or variable that does that? (I am interested in cmake version 2.8 if it matters)

Thanks!

回答1:

In newer versions of CMake (>= version 3.7) that would be the BUILDSYSTEM_TARGETS directory property:

get_directory_property(MY_LIBRARIES_NAMES BUILDSYSTEM_TARGETS)

For older versions of CMake you could overwrite the add_library() call to collect a list of targets:

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

...

get_property(_allTargets GLOBAL PROPERTY GlobalTargetList)
message(STATUS "GlobalTargetList: ${_allTargets}")

References

  • Making all projects in CMake Visual Studio depend on one project
  • CMake: How do I change properties on subdirectory project targets?


标签: cmake