From 8f66678f99fa9e326eb2042dd99627cb549a50d8 Mon Sep 17 00:00:00 2001 From: "Roscoe A. Bartlett" Date: Fri, 8 Sep 2023 09:29:41 -0600 Subject: [PATCH] Factor longer parts out of base CMakeLists.raw.cmake to streamline (#582) This will allow me to exclude parts that I don't want to show in the developers guide. NOTE: The only reas the test file TribitsExampleProject2_Tests.cmake is updated is because I took 'a' out of 'in a raw'. --- .../TribitsExampleProject2_Tests.cmake | 8 +-- .../packages/package1/CMakeLists.raw.cmake | 50 ++----------------- .../packages/package1/CMakeLists.txt | 3 ++ .../cmake/raw/DefineAllLibsTarget.cmake | 11 ++++ .../cmake/raw/EnableTribitsTestSupport.cmake | 10 ++++ ...GeneratePackageConfigFileForBuildDir.cmake | 13 +++++ ...neratePackageConfigFileForInstallDir.cmake | 15 ++++++ .../packages/package1/test/CMakeLists.txt | 8 +-- 8 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/DefineAllLibsTarget.cmake create mode 100644 tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/EnableTribitsTestSupport.cmake create mode 100644 tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForBuildDir.cmake create mode 100644 tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForInstallDir.cmake diff --git a/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake b/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake index 652c2c5f5..4f485a1cb 100644 --- a/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake +++ b/test/core/ExamplesUnitTests/TribitsExampleProject2_Tests.cmake @@ -571,12 +571,12 @@ function(TribitsExampleProject2_find_package sharedOrStatic package1TribitsOrR "-D Package1_TRACE_ADD_TEST=TRUE" ) string(APPEND testNameSuffix "_${package1UseTribitsTestFunctions}" ) list(APPEND package1ConfiRegex - "Using TriBITS Test Functions in a raw CMake Package1 build" + "Using TriBITS Test Functions in raw CMake Package1 build" "Package1_Prg: Added test [(]BASIC, PROCESSORS=1[)]" "Package1_Prg-advanced: Added test [(]BASIC, PROCESSORS=1[)]" ) elseif (package1UseTribitsTestFunctions STREQUAL "") list(APPEND package1ConfiRegex - "Using Raw CMake add_test[(][)] in a raw CMake Package1 build" ) + "Using Raw CMake add_test[(][)] in raw CMake Package1 build" ) else() message(FATAL_ERROR "Error, package1UseTribitsTestFunctions='${package1UseTribitsTestFunctions}' is invalid!") @@ -1225,13 +1225,13 @@ function(TribitsExampleProject2_External_RawPackage1_PBP "-D Package1_TRACE_ADD_TEST=TRUE" ) string(APPEND testNameSuffix "_${package1UseTribitsTestFunctions}" ) set(package1ConfiRegex - "Using TriBITS Test Functions in a raw CMake Package1 build" + "Using TriBITS Test Functions in raw CMake Package1 build" "Package1_Prg: Added test [(]BASIC, PROCESSORS=1[)]" "Package1_Prg-advanced: Added test [(]BASIC, PROCESSORS=1[)]" ) elseif (package1UseTribitsTestFunctions STREQUAL "") set(package1UseTribitsTestFunctionsArgs "") set(package1ConfiRegex - "Using Raw CMake add_test[(][)] in a raw CMake Package1 build" ) + "Using Raw CMake add_test[(][)] in raw CMake Package1 build" ) else() message(FATAL_ERROR "Error, package1UseTribitsTestFunctions='${package1UseTribitsTestFunctions}' is invalid!") diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.raw.cmake b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.raw.cmake index 9c43d0b13..ad2a18182 100644 --- a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.raw.cmake +++ b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.raw.cmake @@ -13,51 +13,11 @@ find_package(Tpl1 CONFIG REQUIRED) add_subdirectory(src) if (Package1_ENABLE_TESTS) include(CTest) - if (Package1_USE_TRIBITS_TEST_FUNCTIONS AND Package1_TRIBITS_DIR) - set(Package1_ENABLE_TESTS ON) - include("${Package1_TRIBITS_DIR}/core/test_support/TribitsAddTest.cmake") - include("${Package1_TRIBITS_DIR}/core/test_support/TribitsAddAdvancedTest.cmake") - endif() + include("${CMAKE_CURRENT_LIST_DIR}/cmake/raw/EnableTribitsTestSupport.cmake") add_subdirectory(test) endif() -# Generate the all_libs target(s) -add_library(Package1_all_libs INTERFACE) -set_target_properties(Package1_all_libs PROPERTIES - EXPORT_NAME all_libs) -target_link_libraries(Package1_all_libs INTERFACE Package1_package1) -install(TARGETS Package1_all_libs - EXPORT ${PROJECT_NAME} - COMPONENT ${PROJECT_NAME} - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) -add_library(Package1::all_libs ALIAS Package1_all_libs) - -if (COMMAND tribits_package) - # Generate Package1Config.cmake file for the build tree (for internal - # TriBITS-compliant package) - set(packageBuildDirCMakePackagesDir - "${${CMAKE_PROJECT_NAME}_BINARY_DIR}/cmake_packages/${PROJECT_NAME}") - export(EXPORT ${PROJECT_NAME} - NAMESPACE ${PROJECT_NAME}:: - FILE "${packageBuildDirCMakePackagesDir}/${PROJECT_NAME}ConfigTargets.cmake" ) - configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/raw/Package1Config.cmake.in" - "${packageBuildDirCMakePackagesDir}/${PROJECT_NAME}/Package1Config.cmake" - @ONLY ) -endif() - -# Generate and install the Package1Config.cmake file for the install tree -# (needed for both internal and external TriBITS package) -set(pkgConfigInstallDir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") -install(EXPORT ${PROJECT_NAME} - DESTINATION "${pkgConfigInstallDir}" - NAMESPACE ${PROJECT_NAME}:: - FILE ${PROJECT_NAME}ConfigTargets.cmake ) -configure_file( - "${CMAKE_CURRENT_SOURCE_DIR}/cmake/raw/Package1Config.cmake.in" - "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Package1Config.install.cmake" - @ONLY ) -install( - FILES "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Package1Config.install.cmake" - RENAME "Package1Config.cmake" - DESTINATION "${pkgConfigInstallDir}" ) +# Stuff that TriBITS does automatically +include("${CMAKE_CURRENT_LIST_DIR}/cmake/raw/DefineAllLibsTarget.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/cmake/raw/GeneratePackageConfigFileForBuildDir.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/cmake/raw/GeneratePackageConfigFileForInstallDir.cmake") diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt index b0eaea3da..c8511aab3 100644 --- a/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt +++ b/tribits/examples/TribitsExampleProject2/packages/package1/CMakeLists.txt @@ -1,5 +1,8 @@ cmake_minimum_required(VERSION 3.23.0 FATAL_ERROR) +set(Package1_USE_RAW_CMAKE OFF CACHE BOOL + "Use raw CMake for package build, even if TriBITS could be used.") + # Macro to select the TriBITS or the raw CMake build system macro(include_cmakelists_file) if ((NOT COMMAND tribits_project) OR Package1_USE_RAW_CMAKE) diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/DefineAllLibsTarget.cmake b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/DefineAllLibsTarget.cmake new file mode 100644 index 000000000..902b1cdf1 --- /dev/null +++ b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/DefineAllLibsTarget.cmake @@ -0,0 +1,11 @@ +# Generate the all_libs target(s) +add_library(Package1_all_libs INTERFACE) +set_target_properties(Package1_all_libs + PROPERTIES EXPORT_NAME all_libs) +target_link_libraries(Package1_all_libs + INTERFACE Package1_package1) +install(TARGETS Package1_all_libs + EXPORT ${PROJECT_NAME} + COMPONENT ${PROJECT_NAME} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} ) +add_library(Package1::all_libs ALIAS Package1_all_libs) diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/EnableTribitsTestSupport.cmake b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/EnableTribitsTestSupport.cmake new file mode 100644 index 000000000..c4449e543 --- /dev/null +++ b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/EnableTribitsTestSupport.cmake @@ -0,0 +1,10 @@ +set(Package1_USE_TRIBITS_TEST_FUNCTIONS OFF CACHE BOOL + "Use TriBITS testing functions") +set(Package1_TRIBITS_DIR "" CACHE PATH + "Path to TriBITS implementation base dir (e.g. TriBITS/tribits)") +if (Package1_USE_TRIBITS_TEST_FUNCTIONS AND Package1_TRIBITS_DIR) + # Pull in and turn on TriBITS testing support + include("${Package1_TRIBITS_DIR}/core/test_support/TribitsAddTest.cmake") + include("${Package1_TRIBITS_DIR}/core/test_support/TribitsAddAdvancedTest.cmake") + set(Package1_ENABLE_TESTS ON) +endif() diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForBuildDir.cmake b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForBuildDir.cmake new file mode 100644 index 000000000..9f937dcda --- /dev/null +++ b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForBuildDir.cmake @@ -0,0 +1,13 @@ +if (COMMAND tribits_package) + # Generate Package1Config.cmake file for the build tree (for internal + # TriBITS-compliant package) + set(packageBuildDirCMakePackagesDir + "${${CMAKE_PROJECT_NAME}_BINARY_DIR}/cmake_packages/${PROJECT_NAME}") + export(EXPORT ${PROJECT_NAME} + NAMESPACE ${PROJECT_NAME}:: + FILE "${packageBuildDirCMakePackagesDir}/${PROJECT_NAME}ConfigTargets.cmake" ) + configure_file( + "${CMAKE_CURRENT_LIST_DIR}/Package1Config.cmake.in" + "${packageBuildDirCMakePackagesDir}/${PROJECT_NAME}/Package1Config.cmake" + @ONLY ) +endif() diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForInstallDir.cmake b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForInstallDir.cmake new file mode 100644 index 000000000..535685e61 --- /dev/null +++ b/tribits/examples/TribitsExampleProject2/packages/package1/cmake/raw/GeneratePackageConfigFileForInstallDir.cmake @@ -0,0 +1,15 @@ +# Generate and install the Package1Config.cmake file for the install tree +# (needed for both internal and external TriBITS package) +set(pkgConfigInstallDir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") +install(EXPORT ${PROJECT_NAME} + DESTINATION "${pkgConfigInstallDir}" + NAMESPACE ${PROJECT_NAME}:: + FILE ${PROJECT_NAME}ConfigTargets.cmake ) +configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/cmake/raw/Package1Config.cmake.in" + "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Package1Config.install.cmake" + @ONLY ) +install( + FILES "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Package1Config.install.cmake" + RENAME "Package1Config.cmake" + DESTINATION "${pkgConfigInstallDir}" ) diff --git a/tribits/examples/TribitsExampleProject2/packages/package1/test/CMakeLists.txt b/tribits/examples/TribitsExampleProject2/packages/package1/test/CMakeLists.txt index 922e735d1..11b72aec0 100644 --- a/tribits/examples/TribitsExampleProject2/packages/package1/test/CMakeLists.txt +++ b/tribits/examples/TribitsExampleProject2/packages/package1/test/CMakeLists.txt @@ -1,9 +1,9 @@ -if ((NOT COMMAND tribits_project) OR Package1_USE_RAW_CMAKE) - if (Package1_USE_TRIBITS_TEST_FUNCTIONS) - message("-- Using TriBITS Test Functions in a raw CMake Package1 build!") +if ((NOT COMMAND tribits_project) OR Package1_USE_RAW_CMAKE) + if (Package1_USE_TRIBITS_TEST_FUNCTIONS AND (COMMAND tribits_add_test)) + message("-- Using TriBITS Test Functions in raw CMake Package1 build!") include("${CMAKE_CURRENT_LIST_DIR}/CMakeLists.tribits.cmake") else() - message("-- Using Raw CMake add_test() in a raw CMake Package1 build!") + message("-- Using Raw CMake add_test() in raw CMake Package1 build!") include("${CMAKE_CURRENT_LIST_DIR}/CMakeLists.raw.cmake") endif() else()