PackageProject.cmake
PackageProject.cmake copied to clipboard
Automatically detect target dependencies
Great idea!
with this code appended to test/CMakeLists.txt
include(FeatureSummary)
feature_summary(
WHAT ALL
FILENAME ${CMAKE_CURRENT_SOURCE_DIR}/FeatureSummary.log)
I will get his file contents:
The following REQUIRED packages have been found:
* dependency (required version >= 1.2)
* namespaced_dependency (required version >= 4.5.6)
* transitive_dependency (required version >= 7.8.9)
This version seems better to parse and get only the list of required packages found:
include(FeatureSummary)
feature_summary(
WHAT REQUIRED_PACKAGES_FOUND
QUIET_ON_EMPTY
DESCRIPTION ""
FILENAME ${CMAKE_CURRENT_SOURCE_DIR}/RequiredPackagesFound.log)
@TheLartians This seems a good starting point:
set(current_targets main)
get_property(
_depends
TARGET ${current_targets}
PROPERTY INTERFACE_LINK_LIBRARIES
)
message(STATUS "${current_targets}: ${_depends}\n")
set(_property "PACKAGES_FOUND")
set(_currentFeatureText "")
get_property(_EnabledFeatures GLOBAL PROPERTY ${_property})
if(_EnabledFeatures)
list(REMOVE_DUPLICATES _EnabledFeatures)
endif()
message(STATUS "${_property}: ${_EnabledFeatures}")
set(_currentFeature PACKAGES_FOUND)
get_property(
_importedTargets
DIRECTORY ${CMAKE_CURRENT_SORUCE_DIR}
PROPERTY IMPORTED_TARGETS
)
foreach(_target ${_importedTargets})
list(FIND _depends ${_target} _indexInList)
if(NOT "${_indexInList}" STREQUAL "-1")
# NOTE: we need the unalias target name! CK
string(REGEX REPLACE "([^:]+::)" "" _targetName ${_target})
get_property(_version GLOBAL PROPERTY _CMAKE_${_targetName}_REQUIRED_VERSION)
message(STATUS "IMPORTED_TARGETS: ${_targetName} ${_version}")
endif()
endforeach()
# _CMAKE_dependency_REQUIRED_VERSION: >= 1.2
# _CMAKE_namespaced_dependency_REQUIRED_VERSION: >= 4.5.6
# _CMAKE_transitive_dependency_REQUIRED_VERSION: >= 7.8.9
# -- main: dependency;ns::namespaced_dependency;transitive_dependency::transitive_dependency
#
# -- PACKAGES_FOUND: dependency;namespaced_dependency;fmt;cxxopts;transitive_dependency
# -- IMPORTED_TARGETS: dependency >= 1.2
# -- IMPORTED_TARGETS: namespaced_dependency >= 4.5.6
# -- IMPORTED_TARGETS: transitive_dependency >= 7.8.9