Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix pkg-config paths for absolute install dirs#2198

Merged
LebedevRI merged 5 commits into
google:mainfrom
Haihan-Jiang:codex/benchmark-pkgconfig-absolute-dirs
May 25, 2026
Merged

Fix pkg-config paths for absolute install dirs#2198
LebedevRI merged 5 commits into
google:mainfrom
Haihan-Jiang:codex/benchmark-pkgconfig-absolute-dirs

Conversation

@Haihan-Jiang

@Haihan-Jiang Haihan-Jiang commented May 24, 2026

Copy link
Copy Markdown
Contributor

Fixes #1396.

CMAKE_INSTALL_FULL_LIBDIR and CMAKE_INSTALL_FULL_INCLUDEDIR always expand to absolute paths when configuring pkg-config files. That makes normal relative installs less relocatable: libdir no longer follows prefix/exec_prefix.

This PR builds pkg-config paths from CMAKE_INSTALL_LIBDIR and CMAKE_INSTALL_INCLUDEDIR instead:

  • relative library dirs become ${exec_prefix}/...
  • relative include dirs become ${prefix}/...
  • absolute dirs stay absolute for layouts such as Nix store paths

benchmark_main.pc gets the same prefix/exec_prefix handling. The test checks the generated .pc files in both relative and absolute install-dir configurations.

Validation:

  • git diff --check
  • ctest --test-dir /tmp/benchmark-pkgconfig-rel-review -R pkg_config_path_test --output-on-failure
  • ctest --test-dir /tmp/benchmark-pkgconfig-abs-review -R pkg_config_path_test --output-on-failure
  • cmake --build /tmp/benchmark-pkgconfig-rel-review --target benchmark benchmark_main -j2

@Haihan-Jiang Haihan-Jiang marked this pull request as ready for review May 24, 2026 18:01

@LebedevRI LebedevRI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description seems AI-generated. It's extremely verbose,
but does not actually explain what specific problem this is fixing,
just explains the diff itself.

Comment thread src/CMakeLists.txt Outdated
set(namespace "${PROJECT_NAME}::")

include(CMakePackageConfigHelpers)
include(GoogleBenchmarkPkgConfig)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name is too generic, i'd just place fn inline here

Comment thread cmake/benchmark.pc.in
Comment thread src/CMakeLists.txt Outdated
endif()
endif()

if(IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Err, inline the file, not the function.

@LebedevRI

Copy link
Copy Markdown
Collaborator

CMAKE_INSTALL_FULL_LIBDIR and CMAKE_INSTALL_FULL_INCLUDEDIR always expand to absolute paths when configuring pkg-config files. That makes normal relative installs less relocatable: libdir no longer follows prefix/exec_prefix.

It is still not clear to me what the problem is.
Does cmake canonicalize CMAKE_INSTALL_FULL_*DIR?
Are prefix/exec_prefix variables somehow magic in pkg-config?

Comment thread src/CMakeLists.txt Outdated
Comment on lines 145 to 146
BENCHMARK_PKG_CONFIG_INCLUDEDIR "${CMAKE_INSTALL_INCLUDEDIR}" "prefix")
configure_file("${PROJECT_SOURCE_DIR}/cmake/benchmark.pc.in" "${pkg_config}" @ONLY)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate with newline please

@LebedevRI LebedevRI left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Diff is fine, but
It is still not clear to me what the problem is.
Does cmake canonicalize CMAKE_INSTALL_FULL_*DIR?
Are prefix/exec_prefix variables somehow magic in pkg-config?

@Haihan-Jiang

Copy link
Copy Markdown
Contributor Author

Thanks, the problem is specifically relocatability of the generated .pc file for the common relative GNUInstallDirs case.

CMAKE_INSTALL_FULL_LIBDIR and CMAKE_INSTALL_FULL_INCLUDEDIR are the absolute variants. When CMAKE_INSTALL_LIBDIR=lib and CMAKE_INSTALL_PREFIX=/old/prefix, CMake expands the full form to /old/prefix/lib. If that absolute value is written into the .pc file, changing prefix later does not affect libdir.

prefix and exec_prefix are not magic by themselves, but they are normal pkg-config variables and downstream users can override them, for example with pkg-config --define-variable=prefix=/new/prefix .... That only works if libdir and includedir are expressed in terms of those variables, e.g. libdir=${exec_prefix}/lib and includedir=${prefix}/include.

So the intended fix is:

  • relative CMake install dirs stay relocatable in the .pc file;
  • absolute install dirs, such as Nix/store-style layouts, stay absolute.

Comment thread src/CMakeLists.txt Outdated
endif()
endif()

function(benchmark_make_pkgconfig_path output_var install_dir path_prefix)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please name this _cmake_path and add a fixme for cmake 3.20:
https://cmake.org/cmake/help/latest/command/cmake_path.html#append

Comment thread test/CMakeLists.txt Outdated
compile_output_test(scoped_pause_test)
benchmark_add_test(NAME scoped_pause_test COMMAND scoped_pause_test)

benchmark_add_test(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please drop the test stuff

@Haihan-Jiang

Copy link
Copy Markdown
Contributor Author

Addressed the remaining concrete review items and pushed 41dbf87:

  • renamed the helper to _cmake_path and added the CMake 3.20 cmake_path(APPEND) FIXME
  • separated the configure calls with a blank line
  • dropped the pkg-config test harness as requested

Local validation:

  • git diff --check
  • cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_TESTING=ON -DBENCHMARK_DOWNLOAD_DEPENDENCIES=ON -DCMAKE_CXX_FLAGS="-isystem /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1"
  • checked generated build/benchmark.pc and build/benchmark_main.pc
  • cmake --build build --target benchmark -- -j4

@LebedevRI

Copy link
Copy Markdown
Collaborator

@Haihan-Jiang thank you, i'll merge this one.

@LebedevRI LebedevRI merged commit 926a850 into google:main May 25, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] pkg-config file is broken when CMAKE_INSTALL_{INCLUDE,LIB}DIR is absolute

2 participants