Skip to content

Commit

Permalink
Merge pull request #493 from bartlettroscoe/299-find-tpl-module-find-…
Browse files Browse the repository at this point in the history
…package

Add, use, and document new function tribits_external_package_create_imported_all_libs_target_and_config_file() (#299)
  • Loading branch information
bartlettroscoe committed Jul 12, 2022
2 parents 974f565 + c81102e commit f36aad6
Show file tree
Hide file tree
Showing 14 changed files with 913 additions and 423 deletions.
13 changes: 3 additions & 10 deletions test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,8 @@ function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase
if (fullOrComponents STREQUAL "FULL")
set(tribitsExProjUseComponentsArg "")
elseif (fullOrComponents STREQUAL "COMPONENTS")
if (tribitsExProj2TestNameBaseBase STREQUAL "TribitsExampleProject2_find_package")
# ToDo: Remove this special case once the test
# TribitsExampleProject2_find_package enables all the packages!
set(tribitsExProjUseComponentsArg
-DTribitsExApp2_USE_COMPONENTS="Package1")
set(fullDepsRegex "Full Deps: Package1{tpl1}")
else()
set(tribitsExProjUseComponentsArg
-DTribitsExApp2_USE_COMPONENTS="Package1,Package2,Package3")
endif()
set(tribitsExProjUseComponentsArg
-DTribitsExApp2_USE_COMPONENTS="Package1,Package2,Package3")
else()
message(FATAL_ERROR "Invalid value of fullOrComponents='${fullOrComponents}'!")
endif()
Expand Down Expand Up @@ -141,6 +133,7 @@ function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase
${tribitsExProjUseComponentsArg}
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleApp2
PASS_REGULAR_EXPRESSION_ALL
"TribitsExProj2_PACKAGE_LIST = Package3[;]Package2[;]Package1"
"-- Configuring done"
"-- Generating done"
"-- Build files have been written to: .*/${testName}/app_build"
Expand Down
77 changes: 54 additions & 23 deletions test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ endmacro()
########################################################################


function(TribitsExampleProject2_find_tpl_parts sharedOrStatic findingTplsMethod)
function(TribitsExampleProject2_find_tpl_parts sharedOrStatic findingTplsMethod)

TribitsExampleProject2_test_setup_header()

Expand All @@ -104,6 +104,13 @@ function(TribitsExampleProject2_find_tpl_parts sharedOrStatic findingTplsMethod)
"${tplInstallBaseDir}/install_tpl4<semicolon>${tplInstallBaseDir}/install_tpl3<semicolon>${tplInstallBaseDir}/install_tpl2<semicolon>${tplInstallBaseDir}/install_tpl1"
)

set(allTplsNoPrefindArgs
"-DTpl1_ALLOW_PACKAGE_PREFIND=OFF"
"-DTpl2_ALLOW_PACKAGE_PREFIND=OFF"
"-DTpl3_ALLOW_PACKAGE_PREFIND=OFF"
"-DTpl4_ALLOW_PACKAGE_PREFIND=OFF"
)

if (findingTplsMethod STREQUAL "TPL_LIBRARY_AND_INCLUDE_DIRS")
set(tplLibAndIncDirsArgs
"-DTpl1_INCLUDE_DIRS=${tplInstallBaseDir}/install_tpl1/include"
Expand All @@ -126,13 +133,13 @@ function(TribitsExampleProject2_find_tpl_parts sharedOrStatic findingTplsMethod)
elseif (findingTplsMethod STREQUAL "CMAKE_PREFIX_PATH_CACHE")
set(testNameSuffix "_CMAKE_PREFIX_PATH_CACHE")
set(cmakePrefixPathCacheArg "-DCMAKE_PREFIX_PATH=${cmakePrefixPath}")
set(tplLibAndIncDirsArgs "-DTpl1_ALLOW_PACKAGE_PREFIND=OFF")
set(tplLibAndIncDirsArgs "${allTplsNoPrefindArgs}")
set(searchingTplLibAndINcDirsRegexes "")
elseif (findingTplsMethod STREQUAL "CMAKE_PREFIX_PATH_ENV")
set(testNameSuffix "_CMAKE_PREFIX_PATH_ENV")
string(REPLACE "<semicolon>" ":" cmakePrefixPathEnv "${cmakePrefixPath}")
set(cmakePrefixPathEnvArg ENVIRONMENT CMAKE_PREFIX_PATH=${cmakePrefixPathEnv})
set(tplLibAndIncDirsArgs "-DTpl1_ALLOW_PACKAGE_PREFIND=OFF")
set(tplLibAndIncDirsArgs "${allTplsNoPrefindArgs}")
set(searchingTplLibAndINcDirsRegexes "")
else()
message(FATAL_ERROR
Expand Down Expand Up @@ -282,7 +289,7 @@ TribitsExampleProject2_find_tpl_parts(SHARED CMAKE_PREFIX_PATH_ENV)
########################################################################


function(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls sharedOrStatic)
function(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls sharedOrStatic)

TribitsExampleProject2_test_setup_header()

Expand Down Expand Up @@ -312,6 +319,9 @@ function(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls sharedO
-DCMAKE_BUILD_TYPE=DEBUG
"-DCMAKE_PREFIX_PATH=${cmakePrefixPath}"
-DTpl1_ALLOW_PACKAGE_PREFIND=OFF
-DTpl2_ALLOW_PACKAGE_PREFIND=OFF
-DTpl3_ALLOW_PACKAGE_PREFIND=OFF
-DTpl4_ALLOW_PACKAGE_PREFIND=OFF
-DTribitsExProj2_ENABLE_ALL_OPTIONAL_PACKAGES=OFF
-DPackage3_ENABLE_Package2=OFF
-DTribitsExProj2_ENABLE_TESTS=ON
Expand Down Expand Up @@ -395,7 +405,7 @@ TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls(SHARED)
########################################################################


function(TribitsExampleProject2_explicit_tpl_vars sharedOrStatic)
function(TribitsExampleProject2_explicit_tpl_vars sharedOrStatic)

TribitsExampleProject2_test_setup_header()

Expand Down Expand Up @@ -504,69 +514,87 @@ TribitsExampleProject2_explicit_tpl_vars(SHARED)
########################################################################


function(TribitsExampleProject2_find_package_test sharedOrStatic)
function(TribitsExampleProject2_find_package sharedOrStatic)

TribitsExampleProject2_test_setup_header()

set(testNameBase TribitsExampleProject2_find_package_${sharedOrStatic})
set(testNameBase ${CMAKE_CURRENT_FUNCTION}_${sharedOrStatic})
set(testName ${PACKAGE_NAME}_${testNameBase})
set(testDir "${CMAKE_CURRENT_BINARY_DIR}/${testName}")

set(tplInstallBaseDir "${TribitsExampleProject2_Tpls_install_${sharedOrStatic}_DIR}")

tribits_add_advanced_test( ${testNameBase}
OVERALL_WORKING_DIRECTORY TEST_NAME
OVERALL_NUM_MPI_PROCS 1

ENVIRONMENT
"CMAKE_PREFIX_PATH=${TribitsExampleProject2_Tpls_install_${sharedOrStatic}_DIR}/install_tpl1"
LIST_SEPARATOR "<semicolon>"

TEST_0
MESSAGE "Configure TribitsExampleProject2 against pre-installed Tpl1"
MESSAGE "Configure TribitsExampleProject2 against pre-installed TPLs"
CMND ${CMAKE_COMMAND}
ARGS
${TribitsExampleProject2_COMMON_CONFIG_ARGS}
-DCMAKE_BUILD_TYPE=DEBUG
-DTPL_ENABLE_Tpl3=ON
-DTPL_ENABLE_Tpl4=ON
-DTribitsExProj2_ENABLE_ALL_PACKAGES=ON
-DTribitsExProj2_ENABLE_TESTS=ON
-DCMAKE_INSTALL_PREFIX=install
-DTribitsExProj2_ENABLE_Package1=ON
-D CMAKE_PREFIX_PATH="${tplInstallBaseDir}/install_tpl1<semicolon>${tplInstallBaseDir}/install_tpl2<semicolon>${tplInstallBaseDir}/install_tpl3<semicolon>${tplInstallBaseDir}/install_tpl4"
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleProject2
ALWAYS_FAIL_ON_NONZERO_RETURN
PASS_REGULAR_EXPRESSION_ALL
"Using find_package[(]Tpl1 [.][.][.][)] [.][.][.]"
"Found Tpl1_DIR='.*TribitsExampleProject2_Tpls_install_${sharedOrStatic}/install_tpl1/lib/cmake/Tpl1'"
"TPL_Tpl1_LIBRARIES='Tpl1::all_libs'"
"TPL_Tpl1_INCLUDE_DIRS=''"
"-- Using find_package[(]Tpl1 [.][.][.][)] [.][.][.]"
"-- Found Tpl1_DIR='.*TribitsExampleProject2_Tpls_install_${sharedOrStatic}/install_tpl1/lib/cmake/Tpl1'"
"-- Generating Tpl1::all_libs and Tpl1Config.cmake"
"-- Found Tpl2_DIR='.*TribitsExampleProject2_Tpls_install_${sharedOrStatic}/install_tpl2/lib/cmake/Tpl2'"
"-- Generating Tpl2::all_libs and Tpl2Config.cmake"
"-- Found Tpl3_DIR='.*TribitsExampleProject2_Tpls_install_${sharedOrStatic}/install_tpl3/lib/cmake/Tpl3'"
"-- Generating Tpl3::all_libs and Tpl3Config.cmake"
"-- Found Tpl4_DIR='.*TribitsExampleProject2_Tpls_install_${sharedOrStatic}/install_tpl4/lib/cmake/Tpl4'"
"-- Generating Tpl4::all_libs and Tpl4Config.cmake"
"-- Configuring done"
"-- Generating done"

TEST_1
MESSAGE "Build Package1 and tests"
MESSAGE "Build Packages and tests"
CMND make
ALWAYS_FAIL_ON_NONZERO_RETURN
PASS_REGULAR_EXPRESSION_ALL
"package1-prg"
"package2-prg"
"package3-prg"

TEST_2
MESSAGE "Run tests for Package1"
MESSAGE "Run tests"
CMND ${CMAKE_CTEST_COMMAND} ARGS -VV
ALWAYS_FAIL_ON_NONZERO_RETURN
PASS_REGULAR_EXPRESSION_ALL
"Test.*Package1_Prg.*Passed"
"100% tests passed, 0 tests failed"
"Test.*Package2_Prg.*Passed"
"Test.*Package3_Prg.*Passed"
"100% tests passed, 0 tests failed out of 3"

TEST_3
MESSAGE "Install Package1"
MESSAGE "Install"
CMND make ARGS install
PASS_REGULAR_EXPRESSION_ALL
"Tpl1Config.cmake"
"Tpl1ConfigVersion.cmake"
"Tpl2Config.cmake"
"Tpl2ConfigVersion.cmake"
"Tpl3Config.cmake"
"Tpl3ConfigVersion.cmake"
"Tpl4Config.cmake"
"Tpl4ConfigVersion.cmake"
ALWAYS_FAIL_ON_NONZERO_RETURN

${ENV_PATH_HACK_FOR_TPL1_${sharedOrStatic}_ARG}

ADDED_TEST_NAME_OUT ${testNameBase}_NAME
)
# NOTE: The above test ensures that find_package() works with manual
# building of the target .
# building of the target.

if (${testNameBase}_NAME)
set(${testNameBase}_NAME ${${testNameBase}_NAME} PARENT_SCOPE)
Expand All @@ -578,8 +606,8 @@ function(TribitsExampleProject2_find_package_test sharedOrStatic)
endfunction()


TribitsExampleProject2_find_package_test(STATIC)
TribitsExampleProject2_find_package_test(SHARED)
TribitsExampleProject2_find_package(STATIC)
TribitsExampleProject2_find_package(SHARED)


########################################################################
Expand Down Expand Up @@ -680,6 +708,9 @@ tribits_add_advanced_test( ${testNameBase}
# find_package(Tpl1) will not find Tpl1Config.cmake just because
# CMAKE_INSTALL_PREFIX is in the search path.
#
# This test also sets Tpl1_EXTRACT_INFO_AFTER_FIND_PACKAGE=ON so we can test
# that path through FindTPLTpl1.cmake.
#
# NOTE: Updated versions of TriBITS will not find TriBITS-generated files
# like Tpl1Config.cmake because they are placed under a different subdir
# <installDir>/external_pacakges/.
Expand Down
132 changes: 132 additions & 0 deletions tribits/core/package_arch/TribitsExternalPackageFindTplHelpers.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# @HEADER
# ************************************************************************
#
# TriBITS: Tribal Build, Integrate, and Test System
# Copyright 2013 Sandia Corporation
#
# Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
# the U.S. Government retains certain rights in this software.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the Corporation nor the names of the
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ************************************************************************
# @HEADER


# @FUNCTION: tribits_external_package_create_imported_all_libs_target_and_config_file()
#
# Call from a ``FindTPL<tplName>.cmake`` module that calls inner
# ``find_package(<externalPkg>)`` for external package that uses modern CMake
# IMPORTED targets.
#
# Usage::
#
# tribits_external_package_create_imported_all_libs_target_and_config_file(
# <tplName>
# INNER_FIND_PACKAGE_NAME <externalPkg>
# IMPORTED_TARGETS_FOR_ALL_LIBS <importedTarget0> <importedTarget1> ... )
#
# This function is called in a TriBITS ``FindTPL<tplName>.cmake`` wrapper
# module after it calls ``find_package(<externalPkg>)`` and then creates the
# IMPORTED target ``<tplName>::all_libs`` from the list of IMPORTED targets
# ``<importedTarget0> <importedTarget1> ...`` which are defined from the call
# ``find_package(<externalPkg>)``. This function also takes care of
# generating the correct ``<tplName>Config.cmake`` file under the directory::
#
# ${${PROJECT_NAME}_BINARY_DIR}/${${PROJECT_NAME}_BUILD_DIR_EXTERNAL_PKGS_DIR}
#
# The generated ``<tplName>Config.cmake`` file calls
# ``find_dependency(<externalPkg>)`` (with no other argument) and then, again,
# defines the correct imported library dependency.
#
# For more details, see `Creating FindTPL*.cmake using find_package() with
# IMPORTED targets`_.
#
function(tribits_external_package_create_imported_all_libs_target_and_config_file
tplName
)

# Parse arguments
cmake_parse_arguments(
PARSE_ARGV 1
PARSE "" "" # prefix, options, one_value_keywords
"INNER_FIND_PACKAGE_NAME;IMPORTED_TARGETS_FOR_ALL_LIBS" #multi_value_keywords
)
tribits_check_for_unparsed_arguments(PARSE)
tribits_assert_parse_arg_one_value(PARSE INNER_FIND_PACKAGE_NAME)
tribits_assert_parse_arg_one_or_more_values(PARSE IMPORTED_TARGETS_FOR_ALL_LIBS)

# Create imported target <tplName>::all_libs
add_library(${tplName}::all_libs INTERFACE IMPORTED GLOBAL)
foreach (importedTarget IN LISTS PARSE_IMPORTED_TARGETS_FOR_ALL_LIBS)
target_link_libraries(${tplName}::all_libs INTERFACE ${importedTarget})
endforeach()

# Create <tplName>Config.cmake file
tribits_external_package_create_package_config_file_with_imported_targets(
${tplName}
INNER_FIND_PACKAGE_NAME ${PARSE_INNER_FIND_PACKAGE_NAME}
IMPORTED_TARGETS_FOR_ALL_LIBS ${PARSE_IMPORTED_TARGETS_FOR_ALL_LIBS} )

endfunction()


function(tribits_external_package_create_package_config_file_with_imported_targets
tplName
)

# Parse arguments
cmake_parse_arguments(
PARSE_ARGV 1
PARSE "" "" # prefix, options, one_value_keywords
"INNER_FIND_PACKAGE_NAME;IMPORTED_TARGETS_FOR_ALL_LIBS" #multi_value_keywords
)
tribits_check_for_unparsed_arguments(PARSE)
tribits_assert_parse_arg_one_value(PARSE INNER_FIND_PACKAGE_NAME)
tribits_assert_parse_arg_one_or_more_values(PARSE IMPORTED_TARGETS_FOR_ALL_LIBS)
set(externalPkg ${PARSE_INNER_FIND_PACKAGE_NAME})

# Create <tplName>Config.cmake file
set(configFileStr "")
string(APPEND configFileStr
"include(CMakeFindDependencyMacro)\n"
"set(${externalPkg}_DIR \"${${externalPkg}_DIR}\")\n"
"find_dependency(${externalPkg})\n"
"add_library(${tplName}::all_libs INTERFACE IMPORTED GLOBAL)\n"
)
foreach (importedTarget IN LISTS PARSE_IMPORTED_TARGETS_FOR_ALL_LIBS)
string(APPEND configFileStr
"target_link_libraries(${tplName}::all_libs INTERFACE ${importedTarget})\n")
endforeach()
set(buildDirExternalPkgsDir
"${${PROJECT_NAME}_BINARY_DIR}/${${PROJECT_NAME}_BUILD_DIR_EXTERNAL_PKGS_DIR}")
set(tplConfigFile
"${buildDirExternalPkgsDir}/${tplName}/${tplName}Config.cmake")
file(WRITE "${tplConfigFile}" "${configFileStr}")

endfunction()
Loading

0 comments on commit f36aad6

Please sign in to comment.