Description
When cetmodules is included as a subproject (e.g., via FetchContent), the cet_cmake_env() macro may fail to set the correct CMAKE_LIBRARY_OUTPUT_DIRECTORY in the parent project.
Root Cause
In Modules/CetCMakeEnv.cmake, include(GNUInstallDirs) is called at the top level of the file. When cetmodules is added via add_subdirectory() (which FetchContent_MakeAvailable does), this include is executed in the cetmodules scope.
Because GNUInstallDirs defines variables like CMAKE_INSTALL_LIBDIR in the current scope, these variables are not available to the parent project. When the parent project later calls cet_cmake_env(), the project_variable(LIBRARY_DIR ...) call uses an undefined CMAKE_INSTALL_LIBDIR, resulting in phlex_LIBRARY_DIR (or equivalent) being an empty string. Consequently, CMAKE_LIBRARY_OUTPUT_DIRECTORY is set to ${PROJECT_BINARY_DIR} instead of the expected ${PROJECT_BINARY_DIR}/lib.
This issue is masked if the parent project explicitly calls include(CetCMakeEnv), as that forces GNUInstallDirs to be processed in the parent's scope. However, relying on this is fragile and counter-intuitive since cet_cmake_env() is designed to bootstrap the environment.
Proposed Solution
Move the include(GNUInstallDirs) call from the top level of Modules/CetCMakeEnv.cmake into the cet_cmake_env() macro. This ensures that the necessary installation directory variables are defined in the scope of the project that is actually initializing its environment.
Suggested Change:
--- a/Modules/CetCMakeEnv.cmake
+++ b/Modules/CetCMakeEnv.cmake
@@ -55,8 +55,6 @@
endif()
- # See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html.
- include(GNUInstallDirs)
##################
define_property(TARGET PROPERTY CET_EXEC_LOCATION
@@ -109: macro(cet_cmake_env)
+ # See https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html.
+ include(GNUInstallDirs)
+
# project() must have been called first.
Description
When cetmodules is included as a subproject (e.g., via FetchContent), the cet_cmake_env() macro may fail to set the correct CMAKE_LIBRARY_OUTPUT_DIRECTORY in the parent project.
Root Cause
In Modules/CetCMakeEnv.cmake, include(GNUInstallDirs) is called at the top level of the file. When cetmodules is added via add_subdirectory() (which FetchContent_MakeAvailable does), this include is executed in the cetmodules scope.
Because GNUInstallDirs defines variables like CMAKE_INSTALL_LIBDIR in the current scope, these variables are not available to the parent project. When the parent project later calls cet_cmake_env(), the project_variable(LIBRARY_DIR ...) call uses an undefined CMAKE_INSTALL_LIBDIR, resulting in phlex_LIBRARY_DIR (or equivalent) being an empty string. Consequently, CMAKE_LIBRARY_OUTPUT_DIRECTORY is set to ${PROJECT_BINARY_DIR} instead of the expected ${PROJECT_BINARY_DIR}/lib.
This issue is masked if the parent project explicitly calls include(CetCMakeEnv), as that forces GNUInstallDirs to be processed in the parent's scope. However, relying on this is fragile and counter-intuitive since cet_cmake_env() is designed to bootstrap the environment.
Proposed Solution
Move the include(GNUInstallDirs) call from the top level of Modules/CetCMakeEnv.cmake into the cet_cmake_env() macro. This ensures that the necessary installation directory variables are defined in the scope of the project that is actually initializing its environment.
Suggested Change: