| 1 |
# @HEADER |
| 2 |
# ***************************************************************************** |
| 3 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 4 |
# |
| 5 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 6 |
# SPDX-License-Identifier: BSD-3-Clause |
| 7 |
# ***************************************************************************** |
| 8 |
# @HEADER |
| 9 |
|
| 10 |
|
| 11 |
################################################################################ |
| 12 |
# |
| 13 |
# TribitPackageDependencies.cmake |
| 14 |
# |
| 15 |
# Contains macros and functions related to defining internal package and |
| 16 |
# external package/TPL dependencies with a little bit of logic. |
| 17 |
# |
| 18 |
################################################################################ |
| 19 |
|
| 20 |
|
| 21 |
include_guard() |
| 22 |
|
| 23 |
include("${CMAKE_CURRENT_LIST_DIR}/../common/TribitsCMakePolicies.cmake" |
| 24 |
NO_POLICY_SCOPE) |
| 25 |
|
| 26 |
include(TribitsParseArgumentsHelpers) |
| 27 |
include(MessageWrapper) |
| 28 |
|
| 29 |
|
| 30 |
# @MACRO: tribits_extpkg_define_dependencies() |
| 31 |
# |
| 32 |
# Macro called from inside of a `FindTPL<tplName>Dependencies.cmake`_ file to |
| 33 |
# define the direct upstream dependencies an external package/TPL. |
| 34 |
# |
| 35 |
# Usage:: |
| 36 |
# |
| 37 |
# tribits_extpkg_define_dependencies( <tplName> |
| 38 |
# DEPENDENCIES <upstreamTpl_0> <upstreamTpl_1>:<vis_1> ... ) |
| 39 |
# |
| 40 |
# The listed upstream dependencies ``<upstreamTpl_i>`` are other external |
| 41 |
# packages/TPLs listed before this external packages/TPL ``<tplName>`` in a |
| 42 |
# `<repoDir>/TPLsList.cmake`_ file. Each upstream dependency can include a |
| 43 |
# visibility specification ``<vis_i>`` that can be added to the dependency |
| 44 |
# using a colon ``:`` with ``<upstreamTpl_1>:<vis_1>`` where ``<vis_i>`` can |
| 45 |
# take the allowed values: |
| 46 |
# |
| 47 |
# * ``PUBLIC``: The include directories along with the libraries will be |
| 48 |
# propagated downstream to clients of ``<tplName>``. |
| 49 |
# |
| 50 |
# * ``PRIVATE``: Only the libraries will be propagated downstream to clients |
| 51 |
# of ``<tplName>``. |
| 52 |
# |
| 53 |
# If ``<vis_i>`` is not specified, then the default is ``PRIVATE``. (If a |
| 54 |
# package needs the include directories from some external package/TPL, then |
| 55 |
# it should list that external package/TPL as a direct dependency and not |
| 56 |
# expect to get include directories from indirect dependencies.) |
| 57 |
# |
| 58 |
macro(tribits_extpkg_define_dependencies |
| 59 |
tplName |
| 60 |
) |
| 61 |
|
| 62 |
# Parse arguments |
| 63 |
cmake_parse_arguments( |
| 64 |
PARSE "" "" # prefix, options, one_value_keywords |
| 65 |
"DEPENDENCIES" # multi_value_keywords |
| 66 |
${ARGN} |
| 67 |
) |
| 68 |
tribits_check_for_unparsed_arguments(PARSE) |
| 69 |
tribits_assert_parse_arg_one_or_more_values(PARSE DEPENDENCIES) |
| 70 |
|
| 71 |
set(${tplName}_LIB_DEFINED_DEPENDENCIES ${PARSE_DEPENDENCIES} CACHE STRING |
| 72 |
"List of all potential dependencies for external package/TPL '${tplName}'") |
| 73 |
mark_as_advanced(${tplName}_LIB_DEFINED_DEPENDENCIES) |
| 74 |
|
| 75 |
endmacro() |
| 76 |
# |
| 77 |
# NOTE: Above, we use a cache var for ${tplName}_LIB_DEFINED_DEPENDENCIES to allow |
| 78 |
# the user to override what dependencies a TPL can depend on. Since this does |
| 79 |
# not depend on what actual TPLs are enabled, it is okay to set this as a |
| 80 |
# cache var. As with any genetic change to a CMakeLists.txt file, you always |
| 81 |
# need to blow a way the cache and configure from scratch to get a correct |
| 82 |
# configuration. (Only some types of changes to CMakeLists.txt files allow |
| 83 |
# for correct reconfiguration.) |
| 84 |
|
| 85 |
|
| 86 |
# @MACRO: tribits_extpkg_setup_enabled_dependencies() |
| 87 |
# |
| 88 |
# Usage:: |
| 89 |
# |
| 90 |
# tribits_extpkg_setup_enabled_dependencies(<externalPkgName>) |
| 91 |
# |
| 92 |
# Macro that sets up the list of enabled external package/TPL dependencies |
| 93 |
# |
| 94 |
# Takes the list ``<externalPkgName>_LIB_DEFINED_DEPENDENCIES`` and sets the |
| 95 |
# default entries of the non-cache var |
| 96 |
# ``<externalPkgName>_LIB_ENABLED_DEPENDENCIES``. However, if |
| 97 |
# ``${<externalPkgName>_LIB_ENABLED_DEPENDENCIES`` is non-empty when this |
| 98 |
# macro is called, then it will not be changed. That allows the user to |
| 99 |
# override the list of enabled TPL dependencies in the cache. This also sets |
| 100 |
# the non-cache vars ``<externalPkgName>_ENABLE_<upstsreamPkgName>=ON`` for |
| 101 |
# each enabled package listed in |
| 102 |
# ``<externalPkgName>_LIB_ENABLED_DEPENDENCIES`` and to ``OFF`` for each |
| 103 |
# ``<upstsreamPkgName>`` listed in |
| 104 |
# ``<externalPkgName>_LIB_DEFINED_DEPENDENCIES`` but not in |
| 105 |
# ``<externalPkgName>_LIB_ENABLED_DEPENDENCIES``. |
| 106 |
# |
| 107 |
macro(tribits_extpkg_setup_enabled_dependencies externalPkgName) |
| 108 |
|
| 109 |
set(libEnabledDependencies "") |
| 110 |
|
| 111 |
if (TPL_ENABLE_${externalPkgName}) |
| 112 |
foreach(upstreamPkgEntry IN LISTS ${externalPkgName}_LIB_DEFINED_DEPENDENCIES) |
| 113 |
tribits_extpkg_get_dep_name_and_vis(${upstreamPkgEntry} |
| 114 |
upstreamPkgName upstreamPkgVis) |
| 115 |
if (TPL_ENABLE_${upstreamPkgName}) |
| 116 |
list(APPEND libEnabledDependencies ${upstreamPkgEntry}) |
| 117 |
else() |
| 118 |
set(${externalPkgName}_ENABLE_${upstreamPkgName} OFF) |
| 119 |
endif() |
| 120 |
endforeach() |
| 121 |
endif() |
| 122 |
|
| 123 |
if ("${${externalPkgName}_LIB_ENABLED_DEPENDENCIES}" STREQUAL "") |
| 124 |
# Only set of not already set as a cache var, for example, by the user |
| 125 |
set(${externalPkgName}_LIB_ENABLED_DEPENDENCIES ${libEnabledDependencies}) |
| 126 |
endif() |
| 127 |
|
| 128 |
foreach(upstreamPkgEntry IN LISTS ${externalPkgName}_LIB_ENABLED_DEPENDENCIES) |
| 129 |
tribits_extpkg_get_dep_name_and_vis( ${upstreamPkgEntry} |
| 130 |
upstreamPkgName upstreamPkgVis) |
| 131 |
set(${externalPkgName}_ENABLE_${upstreamPkgName} ON) |
| 132 |
endforeach() |
| 133 |
|
| 134 |
endmacro() |
| 135 |
# |
| 136 |
# NOTE: Above we set ${externalPkgName}_LIB_ENABLED_DEPENDENCIES as a regular |
| 137 |
# project-level variable, not as a cache var. That is because we want it to |
| 138 |
# update when the set of enabled TPLs changes without having to reconfigure |
| 139 |
# from scratch. However, we use the if statement to allow the user to |
| 140 |
# override the default logic on the dependencies for an enabled TPL. |
| 141 |
|
| 142 |
|
| 143 |
# @FUNCTION: tribits_extpkg_get_dep_name_and_vis() |
| 144 |
# |
| 145 |
# Extract ``<PkgName>`` and ``<Vis>`` from ``<PkgName>[:<Vis>]`` input with |
| 146 |
# default ``<Vis>`` of ``PRIVATE``. |
| 147 |
# |
| 148 |
# Usage:: |
| 149 |
# |
| 150 |
# tribits_extpkg_get_dep_name_and_vis( |
| 151 |
# <upstreamTplDepEntry> <upstreamTplDepNameOut> <upstreamTplDepVisOut>) |
| 152 |
# |
| 153 |
function(tribits_extpkg_get_dep_name_and_vis |
| 154 |
upstreamTplDepEntry upstreamTplDepNameOut upstreamTplDepVisOut |
| 155 |
) |
| 156 |
# Split on ':' to get <PkgName>[:<Vis>] |
| 157 |
string(REPLACE ":" ";" upstreamTplAndVisList "${upstreamTplDepEntry}") |
| 158 |
list(LENGTH upstreamTplAndVisList upstreamTplAndVisListLen) |
| 159 |
# Validate one or two entries only |
| 160 |
if (upstreamTplAndVisListLen GREATER 2) |
| 161 |
math(EXPR numColons "${upstreamTplAndVisListLen}-1") |
| 162 |
message_wrapper(FATAL_ERROR |
| 163 |
"ERROR: '${upstreamTplDepEntry}' has ${numColons} ':' but only 1 is allowed!") |
| 164 |
endif() |
| 165 |
# Set <PkgName> |
| 166 |
list(GET upstreamTplAndVisList 0 upstreamTplDepName) |
| 167 |
# Set <Vis> |
| 168 |
if (upstreamTplAndVisListLen EQUAL 2) |
| 169 |
list(GET upstreamTplAndVisList 1 upstreamTplDepVis) |
| 170 |
else() |
| 171 |
set(upstreamTplDepVis PRIVATE) |
| 172 |
endif() |
| 173 |
# Assert valid <Vis> |
| 174 |
set(validVisValues "PUBLIC" "PRIVATE") |
| 175 |
if (NOT upstreamTplDepVis IN_LIST validVisValues) |
| 176 |
message_wrapper(FATAL_ERROR |
| 177 |
"ERROR: '${upstreamTplDepEntry}' has invalid visibility '${upstreamTplDepVis}'." |
| 178 |
" Only 'PUBLIC' or 'PRIVATE' allowed!") |
| 179 |
return() # Only executed in unit-test mode! |
| 180 |
endif() |
| 181 |
# Set outputs |
| 182 |
set(${upstreamTplDepNameOut} ${upstreamTplDepName} PARENT_SCOPE) |
| 183 |
set(${upstreamTplDepVisOut} ${upstreamTplDepVis} PARENT_SCOPE) |
| 184 |
endfunction() |