Repository navigation
Description
Problem
A goal of cpp-library is to minimize client CMake infrastructure. However, clients adding their own install rules after cpp_library_setup() still need a directory-scope include(GNUInstallDirs) to obtain effective install-directory defaults.
In v5.8.1, the call chain is:
cpp_library_setup() → _cpp_library_setup_core() → _cpp_library_setup_install()
The innermost function includes GNUInstallDirs when installation is enabled. Its own install rules work, but effective normal-variable defaults do not survive the function boundaries to the client.
Verified reproduction
With cpp-library v5.8.1 and CMake 4.4.5, removing the directory-scope include(GNUInstallDirs) from stlab/cel-std and configuring with CEL_INSTALL=ON produces an empty caller-visible CMAKE_INSTALL_DATADIR. This rule:
cpp_library_setup(
# Library-specific arguments omitted.
)
if(CEL_INSTALL)
install(FILES LICENSE licenses/CPM-MIT.txt
DESTINATION "${CMAKE_INSTALL_DATADIR}/licenses/cel-std")
endif()then generates an absolute destination /licenses/cel-std, rather than ${CMAKE_INSTALL_PREFIX}/share/licenses/cel-std.
Restoring the directory-scope include restores the correct generated destination. No installation to the erroneous absolute path was performed.
A minimal scope reproduction, run with cmake -P, is:
cmake_minimum_required(VERSION 3.24)
function(initialize_install_dirs)
include(GNUInstallDirs)
message(STATUS "inside: ${CMAKE_INSTALL_DATADIR}")
endfunction()
initialize_install_dirs()
message(STATUS "outside: ${CMAKE_INSTALL_DATADIR}")
include(GNUInstallDirs)
message(STATUS "after local include: ${CMAKE_INSTALL_DATADIR}")On the tested version, this prints share, an empty value, and share, respectively. Script mode also emits an expected architecture warning because no project language is enabled; the actual library configure reproduction enables CXX and does not depend on that warning.
The tested GNUInstallDirs implementation deliberately stores an empty cache entry for fallback directories and supplies the effective fallback as a normal variable. Relying on the cache alone therefore does not preserve the effective value. Older CMake versions were not tested.
Suggested fix
Initialize GNU install directories in the public cpp_library_setup() function after project() and expose the supported effective CMAKE_INSTALL_<dir> and CMAKE_INSTALL_FULL_<dir> values to its caller using PARENT_SCOPE, before the consumer-mode early return. An equivalent implementation that preserves those semantics is also suitable.
Publishing from only the innermost function is insufficient because there are multiple intervening function scopes. The public function already publishes project-version values with PARENT_SCOPE.
Preserve GNUInstallDirs cache behavior, user overrides, and prefix handling. Do not hard-code directory defaults or force calculated defaults into the cache.
Regression coverage
- A client adds a license install rule after setup without including GNUInstallDirs itself.
- Default generated destination remains prefix-relative
share/licenses/<package>. - Explicit
CMAKE_INSTALL_DATADIRandCMAKE_INSTALL_DATAROOTDIRoverrides are respected. - Relevant relative and full directory values are available in the client scope.
- Source-consumer and installed-package configurations continue to work.
- Installation-disabled configurations gain no install rules.
- Reconfiguration after a prefix change follows GNUInstallDirs behavior.
References
- https://github.com/stlab/cpp-library/blob/v5.8.1/cmake/cpp-library-install.cmake#L459-L475
- https://github.com/stlab/cpp-library/blob/v5.8.1/cmake/cpp-library-setup.cmake#L127-L136
- https://github.com/stlab/cpp-library/blob/v5.8.1/cpp-library.cmake#L266-L301
- https://cmake.org/cmake/help/latest/module/GNUInstallDirs.html
Activity
sean-parent commentedon Nov 18, 2026
Verified that this was fixed before the current branch by commit 36d5e2a, merged in #40 and already present on main. The public cpp_library_setup() includes GNUInstallDirs and publishes effective relative/full install-directory values to its caller before the subproject early return, without forcing the cache. Re-ran cmake -P tests/setup/test_install_dirs.cmake: all six cases passed (defaults, cache/normal-variable overrides, dataroot, disabled installation, and subproject scope). Closing as already completed; the upcoming PR is not the fixing change.