Skip to content

cet_cmake_env() fails to set correct output directories when included implicitly via subproject #39

Description

@greenc-FNAL

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.

Activity

  1. greenc-FNAL commented on Jul 27, 2026

    @greenc-FNAL
    MemberAuthor

    Fixed by #40

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions