| 1 |
# -*- mode: cmake -*- |
| 2 |
# @HEADER |
| 3 |
# ***************************************************************************** |
| 4 |
# TriBITS: Tribal Build, Integrate, and Test System |
| 5 |
# |
| 6 |
# Copyright 2013-2016 NTESS and the TriBITS contributors. |
| 7 |
# SPDX-License-Identifier: BSD-3-Clause |
| 8 |
# ***************************************************************************** |
| 9 |
# @HEADER |
| 10 |
# |
| 11 |
# Based on version from MSTK which is from Amanzi open source code - |
| 12 |
# https://software.lanl.gov/ascem/trac) |
| 13 |
# |
| 14 |
# add_package_dependency(<PACKNAME> DEPENDS_ON <req_pack>) |
| 15 |
# |
| 16 |
|
| 17 |
# CMake module |
| 18 |
include(CMakeParseArguments) |
| 19 |
|
| 20 |
# MSTK modules |
| 21 |
include(ParseLibraryList) |
| 22 |
|
| 23 |
function(add_package_dependency) |
| 24 |
|
| 25 |
# Macro: _print_usage |
| 26 |
macro(_print_usage) |
| 27 |
message("\nadd_package_dependency(<target_package> DEPENDS_ON <req_package>)\n" |
| 28 |
" Add req_package to target_package dependencies.\n") |
| 29 |
endmacro() |
| 30 |
|
| 31 |
# Parse the arguments |
| 32 |
set(_options "") |
| 33 |
set(_oneValue "DEPENDS_ON") |
| 34 |
set(_multiValue "") |
| 35 |
cmake_parse_arguments(ADD_PACK "${_options}" "${_oneValue}" "${_multiValue}" ${ARGN}) |
| 36 |
|
| 37 |
# Define the target package name |
| 38 |
list(GET ADD_PACK_UNPARSED_ARGUMENTS 0 target_package) |
| 39 |
if ( NOT target_package ) |
| 40 |
_print_usage() |
| 41 |
message(FATAL_ERROR "Must define a target_package") |
| 42 |
endif() |
| 43 |
|
| 44 |
# Define the required package |
| 45 |
set(req_package "") |
| 46 |
if(ADD_PACK_DEPENDS_ON) |
| 47 |
set(req_package ${ADD_PACK_DEPENDS_ON}) |
| 48 |
else() |
| 49 |
_print_usage() |
| 50 |
message(FATAL_ERROR "Must define a required package") |
| 51 |
endif() |
| 52 |
|
| 53 |
# Find required package |
| 54 |
message(STATUS "${target_package} depends on ${req_package}") |
| 55 |
message(STATUS "Updating ${target_package}_LIBRARIES and ${target_package}_INCLUDE_DIRS") |
| 56 |
find_package(${req_package} REQUIRED) |
| 57 |
if( ${req_package}_LIBRARIES AND ${req_package}_INCLUDE_DIRS ) |
| 58 |
|
| 59 |
# Add the include paths |
| 60 |
set(_save_inc_list ${${target_package}_INCLUDE_DIRS}) |
| 61 |
list(APPEND _save_inc_list ${${req_package}_INCLUDE_DIRS}) |
| 62 |
set(${target_package}_INCLUDE_DIRS ${_save_inc_list} PARENT_SCOPE) |
| 63 |
list(REMOVE_DUPLICATES ${target_package}_INCLUDE_DIRS) |
| 64 |
|
| 65 |
# Add the libraries....this can be tricky because some packages |
| 66 |
# *cough* HDF5 *cough* return a list with keywords debug, optimized |
| 67 |
# general in the list. These keywords are flags that are used when |
| 68 |
# CMAKE_BUILD_TYPE is set. Need to construct the LIBRARIES carefully |
| 69 |
# if these are present. |
| 70 |
parse_library_list(${${target_package}_LIBRARIES} |
| 71 |
FOUND target_libs_split |
| 72 |
DEBUG target_debug_libs |
| 73 |
OPT target_opt_libs |
| 74 |
GENERAL target_gen_libs) |
| 75 |
|
| 76 |
parse_library_list(${${req_package}_LIBRARIES} |
| 77 |
FOUND req_libs_split |
| 78 |
DEBUG req_debug_libs |
| 79 |
OPT req_opt_libs |
| 80 |
GENERAL req_gen_libs) |
| 81 |
|
| 82 |
# _save_lib_list tmp storage |
| 83 |
set(_save_lib_list "") |
| 84 |
if ( ${target_libs_split} OR ${req_libs_split} ) |
| 85 |
|
| 86 |
# Define the parsed lists if the original list did not contain keywords |
| 87 |
if ( NOT ${target_libs_split} ) |
| 88 |
set(target_debug_libs ${${target_package}_LIBRARIES}) |
| 89 |
set(target_opt_libs ${${target_package}_LIBRARIES}) |
| 90 |
set(target_general_libs ${${target_package}_LIBRARIES}) |
| 91 |
endif() |
| 92 |
|
| 93 |
if ( NOT ${req_libs_split} ) |
| 94 |
set(req_debug_libs ${${req_package}_LIBRARIES}) |
| 95 |
set(req_opt_libs ${${req_package}_LIBRARIES}) |
| 96 |
set(req_general_libs ${${req_package}_LIBRARIES}) |
| 97 |
endif() |
| 98 |
|
| 99 |
# Define each type and store in tmp lists removing duplicates |
| 100 |
set(_save_debug_list "") |
| 101 |
set(_save_opt_list "") |
| 102 |
set(_save_gen_list "") |
| 103 |
if ( target_debug_libs OR req_debug_libs ) |
| 104 |
set(_save_debug_list "${target_debug_libs}" "${req_debug_libs}") |
| 105 |
list(REMOVE_DUPLICATES _save_debug_list) |
| 106 |
endif() |
| 107 |
if ( target_opt_libs OR req_opt_libs ) |
| 108 |
set(_save_opt_list "${target_opt_libs}" "${req_opt_libs}") |
| 109 |
list(REMOVE_DUPLICATES _save_opt_list) |
| 110 |
endif() |
| 111 |
if ( target_gen_libs OR req_gen_libs ) |
| 112 |
set(_save_gen_list "${target_gen_libs}" "${req_gen_libs}") |
| 113 |
list(REMOVE_DUPLICATES _save_gen_list) |
| 114 |
endif() |
| 115 |
|
| 116 |
# Now build the _save_lib_list with the keywords |
| 117 |
if(_save_debug_list) |
| 118 |
list(APPEND _save_lib_list "debug") |
| 119 |
list(APPEND _save_lib_list "${_save_debug_list}") |
| 120 |
endif() |
| 121 |
|
| 122 |
if(_save_opt_list) |
| 123 |
list(APPEND _save_lib_list "optimized") |
| 124 |
list(APPEND _save_lib_list "${_save_opt_list}") |
| 125 |
endif() |
| 126 |
|
| 127 |
if(_save_gen_list) |
| 128 |
list(APPEND _save_lib_list "general") |
| 129 |
list(APPEND _save_lib_list "${_save_gen_list}") |
| 130 |
endif() |
| 131 |
|
| 132 |
else() |
| 133 |
|
| 134 |
# Neither list has keywords |
| 135 |
set(_save_lib_list "${${target_package}_LIBRARIES}" "${${req_package}_LIBRARIES}") |
| 136 |
list(REMOVE_DUPLICATES _save_lib_list) |
| 137 |
|
| 138 |
endif() |
| 139 |
|
| 140 |
|
| 141 |
set(${target_package}_LIBRARIES ${_save_lib_list} PARENT_SCOPE) |
| 142 |
|
| 143 |
endif() |
| 144 |
|
| 145 |
endfunction() |