Skip to content

Commit

Permalink
Make TriBITS package with raw CMake work (#582)
Browse files Browse the repository at this point in the history
Allow TriBITS package TribitsExampleProject/Package1 to use 100% raw CMake and
update TriBITS to allow that.

This raw CMake build mode for Package1 uses no TriBITS macros, functions, or
other functionality at all.

The only changes to TriBITS-proper needed to allow this were:

* Skip check for not calling tribits_package_postprocess() if the
  `<Package>::all_libs` target is already defined.

* Build up the list of package libraries target for the `<Project>_libs`
  target from `<Package>::all_libs` instead of `<Package>_libs` (since the
  latter does not exist for a raw CMake package).

However, this also required some changes to the
TribitsExampleProject2/Package1 raw build system:

* Use `${CMAKE_PROJECT_NAME}_INSTALL_LIB_DIR}` as the library install location
  instead of `${CMAKE_INSTALL_LIBDIR}` when building as a TriBITS package.

This seems to pass all of the TriBITS tests.

NOTE: This commit does **not** yet contain the changes to TriBITS to allow
calling project() in the package's CMakeLists.txt files and still use TriBITS
macros in the package's CMakeLists.txt files.  Those changes will come later
as we add an example that calls some TriBITS functions.
  • Loading branch information
bartlettroscoe committed Aug 3, 2023
1 parent 6b2b69f commit 9015a58
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 26 deletions.
45 changes: 31 additions & 14 deletions test/core/ExamplesUnitTests/TribitsExampleApp2_Tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,29 @@ endif()


function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase
sharedOrStatic fullOrComponents
sharedOrStatic package1TribitsOrRawCMake fullOrComponents
)

if (sharedOrStatic STREQUAL "SHARED")
set(buildSharedLibsArg -DBUILD_SHARED_LIBS=ON)
elseif (sharedOrStatic STREQUAL "STATIC")
set(buildSharedLibsArg -DBUILD_SHARED_LIBS=OFF)
else()
message(FATAL_ERROR "Invalid value of buildSharedLibsArg='${buildSharedLibsArg}'!")
message(FATAL_ERROR "Invalid value of sharedOrStatic='${sharedOrStatic}'!")
endif()
set(tribitsExProj2Suffix "${sharedOrStatic}")

if ( (package1TribitsOrRawCMake STREQUAL "") OR
(package1TribitsOrRawCMake STREQUAL "PACKAGE1_USE_TRIBITS")
)
set(package1UseRawCMakeArgs "")
elseif (package1TribitsOrRawCMake STREQUAL "PACKAGE1_USE_RAW_CMAKE")
set(package1UseRawCMakeArgs "-D Package1_USE_RAW_CMAKE=TRUE")
string(APPEND tribitsExProj2Suffix "_${package1TribitsOrRawCMake}")
else()
message(FATAL_ERROR "package1UseRawCMakeArgs='${package1UseRawCMakeArgs}' Invalid!")
endif()


set(fullDepsRegex "Full Deps: Package3{Package2{Package1{tpl1}, Tpl3{Tpl2a{tpl1}, Tpl2b{no deps}}}, Package1{tpl1}, Tpl4{Tpl3{Tpl2a{tpl1}, Tpl2b{no deps}}, Tpl2a{tpl1}, Tpl2b{no deps}}, Tpl2a{tpl1}, Tpl2b{no deps}}")

Expand All @@ -113,7 +126,7 @@ function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase
set(fullDepsRegex "Full Deps: Package3{Package1{tpl1}, Tpl2a{tpl1}, Tpl2b{no deps}}")
endif()

set(testSuffix ${sharedOrStatic}_${fullOrComponents})
set(testSuffix ${tribitsExProj2Suffix}_${fullOrComponents})
set(testBaseName
${CMAKE_CURRENT_FUNCTION}_${tribitsExProj2TestNameBaseBase}_${testSuffix})
set(testName ${PACKAGE_NAME}_${testBaseName})
Expand All @@ -129,7 +142,7 @@ function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase
MESSAGE "Configure TribitsExampleApp2 locally against already installed TribitsExProject2"
WORKING_DIRECTORY app_build
CMND ${CMAKE_COMMAND} ARGS
-DCMAKE_PREFIX_PATH=${${tribitsExProj2TestNameBaseBase}_${sharedOrStatic}_INSTALL_DIR}
-DCMAKE_PREFIX_PATH=${${tribitsExProj2TestNameBaseBase}_${tribitsExProj2Suffix}_INSTALL_DIR}
${tribitsExProjUseComponentsArg}
${${PROJECT_NAME}_TRIBITS_DIR}/examples/TribitsExampleApp2
PASS_REGULAR_EXPRESSION_ALL
Expand Down Expand Up @@ -169,20 +182,24 @@ function(TribitsExampleApp2 tribitsExProj2TestNameBaseBase

if (${testNameBase}_NAME)
set_tests_properties(${${testNameBase}_NAME}
PROPERTIES DEPENDS ${${tribitsExProj2TestNameBaseBase}_${sharedOrStatic}_NAME} )
PROPERTIES DEPENDS ${${tribitsExProj2TestNameBaseBase}_${tribitsExProj2Suffix}_NAME}
)
endif()

endfunction()


TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts STATIC FULL)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts STATIC "" FULL)
# NOTE: We don't need to test the permutation SHARED FULL as well. That does
# not really test anything new given that shared is tested with COMPONENTS.
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts STATIC COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts SHARED COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls STATIC COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls SHARED COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_explicit_tpl_vars STATIC COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_explicit_tpl_vars SHARED COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package SHARED COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package STATIC COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts STATIC "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts SHARED "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls STATIC "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_tpl_parts_no_optional_packages_tpls SHARED "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_explicit_tpl_vars STATIC "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_explicit_tpl_vars SHARED "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package SHARED "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package STATIC "" COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package SHARED PACKAGE1_USE_RAW_CMAKE COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package STATIC PACKAGE1_USE_RAW_CMAKE COMPONENTS)
TribitsExampleApp2(TribitsExampleProject2_find_package STATIC PACKAGE1_USE_RAW_CMAKE FULL)
9 changes: 6 additions & 3 deletions tribits/core/package_arch/TribitsGlobalMacros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2072,17 +2072,20 @@ macro(tribits_configure_enabled_packages)
tribits_trace_file_processing(PACKAGE ADD_SUBDIR
"${TRIBITS_PACKAGE_CMAKELIST_FILE}")
if (NOT ${TRIBITS_PACKAGE}_SOURCE_DIR STREQUAL ${PROJECT_NAME}_SOURCE_DIR)
add_subdirectory(${${TRIBITS_PACKAGE}_SOURCE_DIR} ${${TRIBITS_PACKAGE}_BINARY_DIR})
add_subdirectory(${${TRIBITS_PACKAGE}_SOURCE_DIR}
${${TRIBITS_PACKAGE}_BINARY_DIR})
else()
include("${TRIBITS_PACKAGE_CMAKELIST_FILE}")
endif()
if (NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS)
if ((NOT ${PACKAGE_NAME}_TRIBITS_PACKAGE_POSTPROCESS) AND
(NOT TARGET ${PACKAGE_NAME}::all_libs)
)
tribits_report_invalid_tribits_usage(
"ERROR: Forgot to call tribits_package_postprocess() in"
" ${TRIBITS_PACKAGE_CMAKELIST_FILE}")
endif()

list(APPEND ENABLED_PACKAGE_LIBS_TARGETS ${TRIBITS_PACKAGE}_libs)
list(APPEND ENABLED_PACKAGE_LIBS_TARGETS ${TRIBITS_PACKAGE}::all_libs)
list(APPEND ${PROJECT_NAME}_LIBRARIES ${${TRIBITS_PACKAGE}_LIBRARIES})

tribits_package_config_code_stop_timer(PROCESS_THIS_PACKAGE_TIME_START_SECONDS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ endmacro()

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

# Being processed as a TriBITS package
message("Configuring package Package1 as full TriBITS package")
tribits_package(Package1)
add_subdirectory(src)
tribits_add_test_directories(test)
Expand All @@ -32,19 +32,37 @@ else()
add_subdirectory(test)
endif()

# Generate the all_libs target
# 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
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 and install the Package1Config.cmake file
# 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
if (COMMAND tribits_package)
set(pkgConfigInstallDir
"${${CMAKE_PROJECT_NAME}_INSTALL_LIB_DIR}/cmake/${PACKAGE_NAME}")
else()
set(pkgConfigInstallDir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}")
endif()
install(EXPORT ${PROJECT_NAME}
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
DESTINATION "${pkgConfigInstallDir}"
NAMESPACE ${PROJECT_NAME}::
FILE ${PROJECT_NAME}ConfigTargets.cmake )
configure_file(
Expand All @@ -54,8 +72,14 @@ else()
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/CMakeFiles/Package1Config.install.cmake"
RENAME "Package1Config.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} )
DESTINATION "${pkgConfigInstallDir}" )

endif()


# NOTE: Above, the variable CMAKE_INSTALL_LIBDIR set by
# include(GNUInstallDirs) is different that what is used by TriBITS by default
# in at least some cases. That is very irritating and this is something that
# needs to be fixed (see TriBITSPub/TriBITS#411). But note that the location
# of the libraries does not have to be the same as the installed
# `<Package>Config.cmake`.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ target_link_libraries(Package1_package1
set_target_properties(Package1_package1 PROPERTIES
EXPORT_NAME package1)
add_library(Package1::package1 ALIAS Package1_package1)
install(
TARGETS Package1_package1
install(TARGETS Package1_package1
EXPORT ${PROJECT_NAME}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} )
install(
Expand Down

0 comments on commit 9015a58

Please sign in to comment.