Skip to content

Commit

Permalink
Break out clean CMakeLists.[raw|tribits].cmake files (TriBITSPub#582)
Browse files Browse the repository at this point in the history
This makes it easier to compare the TriBITS build system for a package with a
raw CMake build system that is TriBITS-compliant.
  • Loading branch information
bartlettroscoe committed Sep 6, 2023
1 parent fcd82bb commit 950e528
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 83 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
cmake_minimum_required(VERSION 3.23.0 FATAL_ERROR)

if (COMMAND tribits_package)
message("Configuring raw CMake package Package1")
else()
message("Configuring raw CMake project Package1")
endif()

# Standard project-level stuff
project(Package1 LANGUAGES C CXX)
include(GNUInstallDirs)
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()
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)

# 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 )

# 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}" )
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
message("Configuring package Package1 as full TriBITS package")
tribits_package(Package1)
add_subdirectory(src)
tribits_add_test_directories(test)
tribits_package_postprocess()
Original file line number Diff line number Diff line change
@@ -1,77 +1,15 @@
cmake_minimum_required(VERSION 3.23.0 FATAL_ERROR)

macro(include_raw_cmake_build)
# 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)
include("${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.raw.cmake"
NO_POLICY_SCOPE)
return()
endif()
endmacro()

if ((COMMAND tribits_package) AND (NOT Package1_USE_RAW_CMAKE))

message("Configuring package Package1 as full TriBITS package")
tribits_package(Package1)
add_subdirectory(src)
tribits_add_test_directories(test)
tribits_package_postprocess()

else()

if (COMMAND tribits_package)
message("Configuring raw CMake package Package1")
else()
message("Configuring raw CMake project Package1")
endif()
project(Package1 LANGUAGES C CXX)
include(GNUInstallDirs)
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()
add_subdirectory(test)
include("${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.tribits.cmake"
NO_POLICY_SCOPE)
endif()
endmacro()

# 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)

# Generate Package1Config.cmake file for the build tree
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 )

# Generate and install the Package1Config.cmake file for the install tree
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}" )

endif()
# Pull in the base CMakeLists.txt file variant
include_cmakelists_file()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
tribits_include_directories(${CMAKE_CURRENT_SOURCE_DIR})
tribits_add_library(package1 HEADERS Package1.hpp SOURCES Package1.cpp)
tribits_add_executable(package1-prg NOEXEPREFIX NOEXESUFFIX
SOURCES Package1_Prg.cpp INSTALLABLE )
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
include_raw_cmake_build()

tribits_include_directories(${CMAKE_CURRENT_SOURCE_DIR})

tribits_add_library(package1 HEADERS Package1.hpp SOURCES Package1.cpp)

tribits_add_executable(package1-prg NOEXEPREFIX NOEXESUFFIX
SOURCES Package1_Prg.cpp INSTALLABLE )
include_cmakelists_file()
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ set_tests_properties(Package1_Prg-advanced
PROPERTIES PASS_REGULAR_EXPRESSION "something_extra")

# NOTE: With raw CMake/CTest, it is not possible to require the matches of
# multiple regexes. Also, it is not possible to require a non-zero return
# code in addition to requiring a regex match the output. These more advanced
# features of tribits_add_advanced_test() would need to be provided by writing
# a wrapper script (e.g. using a Python script, a cmake -P script, etc.)
# multiple regexes (i.e. not the require the match of *both* "Package1 Deps:
# tpl1" and "something_extra"). Also, it is not possible to require a
# non-zero return code in addition to requiring a regex match the output.
# These more advanced features of tribits_add_advanced_test() would need to be
# provided by writing a wrapper script (e.g. using a Python script, a cmake -P
# script, etc.). Also, these tests don't support other features like: b)
# allow tests to be disabled for a variety of reasons like number of MPI
# processes required, incompatible system, disable cache variables -D
# <fullTestName>_DISABLE=ON, etc.; b) printing which tests got added or did
# not get added and why when <Package>_TRACE_ADD_TEST=ON, etc.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ tribits_add_test(package1-prg NOEXEPREFIX NOEXESUFFIX
PASS_REGULAR_EXPRESSION "Package1 Deps: tpl1" )

tribits_add_advanced_test(Prg-advanced
OVERALL_WORKING_DIRECTORY TEST_NAME
OVERALL_NUM_MPI_PROCS 1
TEST_0
EXEC package1-prg DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../src"
NOEXEPREFIX NOEXESUFFIX
Expand Down

0 comments on commit 950e528

Please sign in to comment.