Skip to content

Expose effective GNUInstallDirs values to clients after cpp_library_setup() #35

Closed

Description

@sean-parent

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_DATADIR and CMAKE_INSTALL_DATAROOTDIR overrides 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

Activity

  1. sean-parent commented on Nov 18, 2026

    @sean-parent
    MemberAuthor

    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.

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