Skip to content

Commit

Permalink
Add cet_transitive_paths() function (#18)
Browse files Browse the repository at this point in the history
* Add cet_transitive_paths() function

This function accepts a path name and a project name (default is the
current project name).  For each transitive dependency of the
specificied project name, the value of the corresponding path name is
appended to an output variable.

* Reference cet_localize_pv in documentation.

Co-authored-by: Chris Green <[email protected]>

* Remaining changes a la Chris' suggestions

---------

Co-authored-by: Chris Green <[email protected]>
  • Loading branch information
knoepfel and greenc-FNAL authored Jan 22, 2024
1 parent 3e0ea4a commit 638ab14
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
18 changes: 4 additions & 14 deletions Modules/CetTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ include(CetMake)
include(CetPackagePath)
# May need to escape a string to avoid misinterpretation as regex
include(CetRegexEscape)
# Need to specify transitive BINARY_DIR paths for COMPILE_ONLY tests
include(CetTransitivePaths)

##################
# Programs and Modules
Expand Down Expand Up @@ -688,8 +690,8 @@ test ${test} must be defined already to be specified as a fixture for ${CET_TARG
endif()
endif()
if (CET_COMPILE_ONLY)
_transitive_binary_directories(${CETMODULES_CURRENT_PROJECT_NAME})
list(JOIN TRANSITIVE_BINARY_DIRS ":" DIRS_FOR_PREFIX_PATH)
cet_transitive_paths(BINARY_DIR IN_TREE)
list(JOIN TRANSITIVE_PATHS_WITH_BINARY_DIR ":" DIRS_FOR_PREFIX_PATH)
set(TEST_CMAKE_PREFIX_PATH "CMAKE_PREFIX_PATH=path_list_prepend:${DIRS_FOR_PREFIX_PATH}")
get_test_property(${target} ENVIRONMENT_MODIFICATION CET_TEST_ENV_TMP)
if (CET_TEST_ENV_TMP)
Expand Down Expand Up @@ -1100,15 +1102,3 @@ function(_update_defined_test_groups)
FORCE
)
endfunction()

function(_transitive_binary_directories PKG)
set(TRANSITIVE_BINARY_DIRS_TMP ${${PKG}_BINARY_DIR})
foreach(DEP IN LISTS CETMODULES_FIND_DEPS_PNAMES_PROJECT_${PKG})
if (${DEP}_IN_TREE)
_transitive_binary_directories(${DEP})
list(APPEND TRANSITIVE_BINARY_DIRS_TMP ${TRANSITIVE_BINARY_DIRS})
endif()
endforeach()
list(REMOVE_DUPLICATES TRANSITIVE_BINARY_DIRS_TMP)
set(TRANSITIVE_BINARY_DIRS ${TRANSITIVE_BINARY_DIRS_TMP} PARENT_SCOPE)
endfunction()
86 changes: 86 additions & 0 deletions Modules/CetTransitivePaths.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#[================================================================[.rst:
CetTransitivePaths
------------------
Defines the function :commmand:`cet_transitive_paths`.
#]================================================================]

include_guard()

#[================================================================[.rst:
.. command:: cet_transitive_paths
Get a list of path values for all transitive dependencies of the
given project name. For a given path name, the dependency tree of
the given project is searched according to the dependencies
specified via ``find_package``.
For example, if the path name ``"MY_PATH"`` is supplied, the
function will traverse the dependency structure and collect all
values of the path name ``"<project-or-dependency name>_MY_PATH"``.
The result can be accessed via the variable
``"TRANSITIVE_PATHS_WITH_MY_PATH"``.
.. code-block:: cmake
cet_transitive_paths(<path-name> [<project-variable localization options>] [IN_TREE] [PROJECT <project-name>])
.. seealso:: :command:`cet_localize_pv`
Options
^^^^^^^
``[PROJECT] <project-name>``
The top-level project from which the transitive dependency
traversal occurs. (default
:variable:`${CETMODULES_CURRENT_PROJECT_NAME}
<CETMODULES_CURRENT_PROJECT_NAME>`).
``[IN_TREE]``
Include paths only from projects that are included in the build tree.
Non-option arguments
^^^^^^^^^^^^^^^^^^^^
``<path-name>``
The name of the path that serves as the suffix for the top-level
project and all transitive dependencies. The path name can be a
Cetmodules project variable, in which case the options to
:command:`cet_localize_pv` may also be provided.
#]================================================================]

function(cet_transitive_paths PATH)
cmake_parse_arguments(PARSE_ARGV 1 CTP "IN_TREE" "PROJECT" "")

set(PKG ${CETMODULES_CURRENT_PROJECT_NAME})
if (CTP_PROJECT)
set(PKG ${CTP_PROJECT})
endif()

_cet_transitive_project_names(${PKG} PNAMES_RESULT)
foreach(DEP IN LISTS PNAMES_RESULT)
if (DEFINED CACHE{CETMODULES_${PATH}_PROPERTIES_PROJECT_${DEP}})
cet_localize_pv(${DEP} ${PATH} ${CTP_UNPARSED_ARGUMENTS})
endif()
list(APPEND TRANSITIVE_PATHS_TMP ${${DEP}_${PATH}})
endforeach()
set(TRANSITIVE_PATHS_WITH_${PATH} ${TRANSITIVE_PATHS_TMP} PARENT_SCOPE)
endfunction()

function(_cet_transitive_project_names PKG RESULT_LIST)
set(RESULT_LIST_TMP ${${RESULT_LIST}})
foreach(DEP IN LISTS CETMODULES_FIND_DEPS_PNAMES_PROJECT_${PKG})
if (PKG STREQUAL DEP OR
(CTP_IN_TREE AND NOT ${DEP}_IN_TREE))
continue()
endif()
if (NOT "${DEP}" IN_LIST RESULT_LIST_TMP)
_cet_transitive_project_names("${DEP}" RESULT_LIST_TMP)
endif()
endforeach()
list(PREPEND RESULT_LIST_TMP ${PKG})
set(${RESULT_LIST} ${RESULT_LIST_TMP} PARENT_SCOPE)
endfunction()
1 change: 1 addition & 0 deletions doc/reference/manual/cetmodules-commands.7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Utility
* :command:`cet_test_env_mod`
* :command:`cet_test_env_prepend`
* :command:`cet_timestamp`
* :command:`cet_transitive_paths`
* :command:`cet_version_cmp`
* :command:`cet_write_plugin_builder`
* :command:`parse_version_string`
Expand Down
1 change: 1 addition & 0 deletions doc/reference/module/CetTransitivePaths.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. cmake-module:: /../../Modules/CetTransitivePaths.cmake

0 comments on commit 638ab14

Please sign in to comment.