Skip to content

Commit 638b9bd

Browse files
committed
Added DEPENDENCY_LINK_TARGETS parameter to dependency() CMake function.
Allows projects to define a list of CMake targets that when consuming the declared dependency the underlying project should link to. A project named e.g. myproject can link to targets of dependencies by using the CMake cache variable MYPROJECT_DEPENDENCIES_LINK_TARGETS, for example in a call to the target_link_libraries() function. Signed-off-by: kilo52 <[email protected]>
1 parent aca8e57 commit 638b9bd

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

share/c/cmake/DependencyUtil.cmake

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ include(FetchContent)
134134
# to configure the dependency. This argument can optionally be used to
135135
# specify a file which deviates from the convention.
136136
#
137+
# [multiValueArgs]
138+
#
139+
# DEPENDENCY_LINK_TARGETS:
140+
# A list of target names that should be registered as link targets when
141+
# consuming the dependency. This argument is optional. When specified,
142+
# the provided names represent CMake targets that are made available to
143+
# the underlying project by the dependency. These target names are then
144+
# appended to the global variable ${PROJECT}_DEPENDENCIES_LINK_TARGETS
145+
# where ${PROJECT} is the name in all caps of the underlying project as
146+
# given to the CMake project() function. A dependency with "TEST" scope
147+
# is never added to that list.
148+
#
137149
# [options]
138150
#
139151
# DEPENDENCY_QUIET:
@@ -182,7 +194,9 @@ function(dependency)
182194
DEPENDENCY_FILE_HASH
183195
DEPENDENCY_CONFIG_FILE
184196
)
185-
set(multiValueArgs "")
197+
set(multiValueArgs
198+
DEPENDENCY_LINK_TARGETS
199+
)
186200

187201
cmake_parse_arguments(
188202
DEP_ARGS
@@ -231,6 +245,7 @@ function(dependency)
231245
endif()
232246

233247
set(DEP_ALLOWED_SCOPES ANY RELEASE TEST DEBUG)
248+
set(DEP_TRACKED_SCOPES ANY RELEASE DEBUG)
234249

235250
if(NOT "${DEPENDENCY_SCOPE}" IN_LIST DEP_ALLOWED_SCOPES)
236251
message(
@@ -434,4 +449,27 @@ function(dependency)
434449

435450
FetchContent_MakeAvailable(${DEP_ARGS_DEPENDENCY_NAME})
436451

452+
# Check tracked identifiers
453+
if(DEPENDENCY_SCOPE IN_LIST DEP_TRACKED_SCOPES)
454+
# Append dependency link targets to a tracked global cached list
455+
if(DEP_ARGS_DEPENDENCY_LINK_TARGETS)
456+
set(PROJECT_DEPENDENCIES_LINK_TARGETS
457+
${PROJECT_NAME_UPPER}_DEPENDENCIES_LINK_TARGETS
458+
)
459+
set(_dep_link_list "${${PROJECT_DEPENDENCIES_LINK_TARGETS}}")
460+
foreach(_link_item IN ITEMS ${DEP_ARGS_DEPENDENCY_LINK_TARGETS})
461+
list(FIND _dep_link_list "${_link_item}" _dep_idx)
462+
if(_dep_idx EQUAL -1)
463+
list(APPEND _dep_link_list "${_link_item}")
464+
endif()
465+
endforeach()
466+
set(${PROJECT_DEPENDENCIES_LINK_TARGETS}
467+
${_dep_link_list}
468+
CACHE STRING
469+
"List of link targets of all dependencies"
470+
FORCE
471+
)
472+
endif()
473+
endif()
474+
437475
endfunction()

share/cpp/cmake/DependencyUtil.cmake

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ include(FetchContent)
134134
# to configure the dependency. This argument can optionally be used to
135135
# specify a file which deviates from the convention.
136136
#
137+
# [multiValueArgs]
138+
#
139+
# DEPENDENCY_LINK_TARGETS:
140+
# A list of target names that should be registered as link targets when
141+
# consuming the dependency. This argument is optional. When specified,
142+
# the provided names represent CMake targets that are made available to
143+
# the underlying project by the dependency. These target names are then
144+
# appended to the global variable ${PROJECT}_DEPENDENCIES_LINK_TARGETS
145+
# where ${PROJECT} is the name in all caps of the underlying project as
146+
# given to the CMake project() function. A dependency with "TEST" scope
147+
# is never added to that list.
148+
#
137149
# [options]
138150
#
139151
# DEPENDENCY_QUIET:
@@ -182,7 +194,9 @@ function(dependency)
182194
DEPENDENCY_FILE_HASH
183195
DEPENDENCY_CONFIG_FILE
184196
)
185-
set(multiValueArgs "")
197+
set(multiValueArgs
198+
DEPENDENCY_LINK_TARGETS
199+
)
186200

187201
cmake_parse_arguments(
188202
DEP_ARGS
@@ -231,6 +245,7 @@ function(dependency)
231245
endif()
232246

233247
set(DEP_ALLOWED_SCOPES ANY RELEASE TEST DEBUG)
248+
set(DEP_TRACKED_SCOPES ANY RELEASE DEBUG)
234249

235250
if(NOT "${DEPENDENCY_SCOPE}" IN_LIST DEP_ALLOWED_SCOPES)
236251
message(
@@ -434,4 +449,27 @@ function(dependency)
434449

435450
FetchContent_MakeAvailable(${DEP_ARGS_DEPENDENCY_NAME})
436451

452+
# Check tracked identifiers
453+
if(DEPENDENCY_SCOPE IN_LIST DEP_TRACKED_SCOPES)
454+
# Append dependency link targets to a tracked global cached list
455+
if(DEP_ARGS_DEPENDENCY_LINK_TARGETS)
456+
set(PROJECT_DEPENDENCIES_LINK_TARGETS
457+
${PROJECT_NAME_UPPER}_DEPENDENCIES_LINK_TARGETS
458+
)
459+
set(_dep_link_list "${${PROJECT_DEPENDENCIES_LINK_TARGETS}}")
460+
foreach(_link_item IN ITEMS ${DEP_ARGS_DEPENDENCY_LINK_TARGETS})
461+
list(FIND _dep_link_list "${_link_item}" _dep_idx)
462+
if(_dep_idx EQUAL -1)
463+
list(APPEND _dep_link_list "${_link_item}")
464+
endif()
465+
endforeach()
466+
set(${PROJECT_DEPENDENCIES_LINK_TARGETS}
467+
${_dep_link_list}
468+
CACHE STRING
469+
"List of link targets of all dependencies"
470+
FORCE
471+
)
472+
endif()
473+
endif()
474+
437475
endfunction()

0 commit comments

Comments
 (0)