From 840951e3185409c0a8f3a9853974c01b1239180f Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 1 Sep 2026 23:24:19 -0400 Subject: [PATCH 1/3] fix(cmake): compute pybind11.pc prefix relative to CMAKE_INSTALL_DATAROOTDIR Fixes item 7 of #6159. --- CMakeLists.txt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index df0eb99965..eef02cf685 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -411,17 +411,14 @@ if(PYBIND11_INSTALL) if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}") set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") else() + # Go up from the pkgconfig directory, then once more for each component of + # CMAKE_INSTALL_DATAROOTDIR, to get back to CMAKE_INSTALL_PREFIX. set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}") - if(CMAKE_VERSION VERSION_LESS 3.20) - set(prefix_for_pc_file "\${pcfiledir}/..") - while(pc_datarootdir) - get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY) - string(APPEND prefix_for_pc_file "/..") - endwhile() - else() - cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR - OUTPUT_VARIABLE prefix_for_pc_file) - endif() + set(prefix_for_pc_file "\${pcfiledir}/..") + while(pc_datarootdir) + get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY) + string(APPEND prefix_for_pc_file "/..") + endwhile() endif() endif() join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") From f4adf6c16b3d4fe0e5a40ea2363bc7d2c80984c3 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sat, 21 Nov 2026 13:29:56 -0500 Subject: [PATCH 2/3] fix(cmake): normalize CMAKE_INSTALL_DATAROOTDIR for pybind11.pc prefix Values such as "." or "./share" gave one ".." too many. Compute the relative path from the normalized pkgconfig install directory instead. Assisted-by: ClaudeCode:claude-fable-5-2 --- CMakeLists.txt | 16 +++---- tests/test_cmake_build/CMakeLists.txt | 11 +++++ tests/test_cmake_build/pkgconfig_prefix.cmake | 45 +++++++++++++++++++ 3 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 tests/test_cmake_build/pkgconfig_prefix.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index eef02cf685..2d5a669644 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -411,14 +411,14 @@ if(PYBIND11_INSTALL) if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}") set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") else() - # Go up from the pkgconfig directory, then once more for each component of - # CMAKE_INSTALL_DATAROOTDIR, to get back to CMAKE_INSTALL_PREFIX. - set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}") - set(prefix_for_pc_file "\${pcfiledir}/..") - while(pc_datarootdir) - get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY) - string(APPEND prefix_for_pc_file "/..") - endwhile() + # Relative path from the pkgconfig install directory back to the prefix. + # The base directory is a stand-in for the prefix; ABSOLUTE normalizes + # values such as "." or "./share". + get_filename_component(pc_install_dir "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig" ABSOLUTE + BASE_DIR "${CMAKE_CURRENT_BINARY_DIR}") + file(RELATIVE_PATH prefix_for_pc_file "${pc_install_dir}" "${CMAKE_CURRENT_BINARY_DIR}") + string(REGEX REPLACE "/+$" "" prefix_for_pc_file "${prefix_for_pc_file}") + set(prefix_for_pc_file "\${pcfiledir}/${prefix_for_pc_file}") endif() endif() join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") diff --git a/tests/test_cmake_build/CMakeLists.txt b/tests/test_cmake_build/CMakeLists.txt index 21d92f8cf7..c205f4f51c 100644 --- a/tests/test_cmake_build/CMakeLists.txt +++ b/tests/test_cmake_build/CMakeLists.txt @@ -104,6 +104,17 @@ if(PYBIND11_INSTALL) OR PYBIND11_STABLE_ABI)) pybind11_add_build_test(installed_embed INSTALL) endif() + + add_custom_target( + test_build_pkgconfig_prefix + ${CMAKE_COMMAND} + "-DSOURCE_DIR=${pybind11_SOURCE_DIR}" + "-DWORK_DIR=${CMAKE_CURRENT_BINARY_DIR}/pkgconfig_prefix" + "-DGENERATOR=${CMAKE_GENERATOR}" + "-DCXX_COMPILER=${CMAKE_CXX_COMPILER}" + -P + "${CMAKE_CURRENT_SOURCE_DIR}/pkgconfig_prefix.cmake") + add_dependencies(test_cmake_build test_build_pkgconfig_prefix) endif() add_dependencies(check test_cmake_build) diff --git a/tests/test_cmake_build/pkgconfig_prefix.cmake b/tests/test_cmake_build/pkgconfig_prefix.cmake new file mode 100644 index 0000000000..5999ecc0ca --- /dev/null +++ b/tests/test_cmake_build/pkgconfig_prefix.cmake @@ -0,0 +1,45 @@ +# Check that prefix= in the installed pybind11.pc resolves to the install prefix. +# Usage: cmake -DSOURCE_DIR=... -DWORK_DIR=... -DGENERATOR=... -DCXX_COMPILER=... +# -P pkgconfig_prefix.cmake + +foreach(datarootdir IN ITEMS "share" "." "./share" "lib/foo/share") + string(MAKE_C_IDENTIFIER "${datarootdir}" name) + set(build_dir "${WORK_DIR}/build_${name}") + set(install_dir "${WORK_DIR}/install_${name}") + file(REMOVE_RECURSE "${build_dir}" "${install_dir}") + + execute_process( + COMMAND + "${CMAKE_COMMAND}" -S "${SOURCE_DIR}" -B "${build_dir}" -G "${GENERATOR}" + "-DCMAKE_CXX_COMPILER=${CXX_COMPILER}" -DPYBIND11_TEST=OFF -DPYBIND11_NOPYTHON=ON + "-DCMAKE_INSTALL_PREFIX=${install_dir}" + "-DCMAKE_INSTALL_DATAROOTDIR=${datarootdir}" + RESULT_VARIABLE result + OUTPUT_QUIET) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Configure failed for CMAKE_INSTALL_DATAROOTDIR=${datarootdir}") + endif() + + execute_process( + COMMAND "${CMAKE_COMMAND}" -P "${build_dir}/cmake_install.cmake" + RESULT_VARIABLE result + OUTPUT_QUIET) + if(NOT result EQUAL 0) + message(FATAL_ERROR "Install failed for CMAKE_INSTALL_DATAROOTDIR=${datarootdir}") + endif() + + set(pcfiledir "${install_dir}/${datarootdir}/pkgconfig") + file(STRINGS "${pcfiledir}/pybind11.pc" prefix_line REGEX "^prefix=") + string(REPLACE "prefix=" "" pc_prefix "${prefix_line}") + string(REPLACE "\${pcfiledir}" "${pcfiledir}" pc_prefix "${pc_prefix}") + get_filename_component(pc_prefix "${pc_prefix}" ABSOLUTE) + get_filename_component(expected "${install_dir}" ABSOLUTE) + + if(NOT pc_prefix STREQUAL expected) + message( + FATAL_ERROR + "CMAKE_INSTALL_DATAROOTDIR=${datarootdir}: ${prefix_line} resolves to " + "${pc_prefix}, expected ${expected}") + endif() + message(STATUS "CMAKE_INSTALL_DATAROOTDIR=${datarootdir}: ${prefix_line} OK") +endforeach() From 316f2219d151d7c85b351531373f7672733d2f97 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 21 Nov 2026 18:30:24 +0000 Subject: [PATCH 3/3] style: pre-commit fixes --- tests/test_cmake_build/pkgconfig_prefix.cmake | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/test_cmake_build/pkgconfig_prefix.cmake b/tests/test_cmake_build/pkgconfig_prefix.cmake index 5999ecc0ca..303ab9a911 100644 --- a/tests/test_cmake_build/pkgconfig_prefix.cmake +++ b/tests/test_cmake_build/pkgconfig_prefix.cmake @@ -12,8 +12,7 @@ foreach(datarootdir IN ITEMS "share" "." "./share" "lib/foo/share") COMMAND "${CMAKE_COMMAND}" -S "${SOURCE_DIR}" -B "${build_dir}" -G "${GENERATOR}" "-DCMAKE_CXX_COMPILER=${CXX_COMPILER}" -DPYBIND11_TEST=OFF -DPYBIND11_NOPYTHON=ON - "-DCMAKE_INSTALL_PREFIX=${install_dir}" - "-DCMAKE_INSTALL_DATAROOTDIR=${datarootdir}" + "-DCMAKE_INSTALL_PREFIX=${install_dir}" "-DCMAKE_INSTALL_DATAROOTDIR=${datarootdir}" RESULT_VARIABLE result OUTPUT_QUIET) if(NOT result EQUAL 0) @@ -36,10 +35,8 @@ foreach(datarootdir IN ITEMS "share" "." "./share" "lib/foo/share") get_filename_component(expected "${install_dir}" ABSOLUTE) if(NOT pc_prefix STREQUAL expected) - message( - FATAL_ERROR - "CMAKE_INSTALL_DATAROOTDIR=${datarootdir}: ${prefix_line} resolves to " - "${pc_prefix}, expected ${expected}") + message(FATAL_ERROR "CMAKE_INSTALL_DATAROOTDIR=${datarootdir}: ${prefix_line} resolves to " + "${pc_prefix}, expected ${expected}") endif() message(STATUS "CMAKE_INSTALL_DATAROOTDIR=${datarootdir}: ${prefix_line} OK") endforeach()