From e1c81f5c2da2b7a6d81e1f5022ccc37431b60d08 Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 1 Sep 2026 23:24:19 -0400 Subject: [PATCH 1/2] 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 65f794a49d..48c8eabcf2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -377,17 +377,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 011329844b999e5b635c7d7ec74dac736126150e Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Tue, 1 Sep 2026 23:25:07 -0400 Subject: [PATCH 2/2] fix(cmake): keep CMP0190 cross-compiling default across reconfigure Fixes item 27 of #6159. --- CMakeLists.txt | 17 ++++++++++++----- tools/pybind11Common.cmake | 7 +++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 48c8eabcf2..534c234129 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -102,12 +102,19 @@ option(PYBIND11_SIMPLE_GIL_MANAGEMENT set(PYBIND11_INTERNALS_VERSION "" CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.") -# Record whether the project set this before option() supplies its OFF default, so -# pybind11Common.cmake can apply its CMP0190-aware default without clobbering an explicit value. -if(NOT DEFINED PYBIND11_USE_CROSSCOMPILING) - set(_PYBIND11_USE_CROSSCOMPILING_DEFAULTED ON) +# CMP0190 prohibits calling FindPython with both Interpreter and Development components +# when cross-compiling, unless CMAKE_CROSSCOMPILING_EMULATOR is defined. Compute the default +# here, before option() caches it, so that a reconfigure keeps it (pybind11Common.cmake applies +# the same default for find_package() consumers, where the option does not exist). +set(_pybind11_use_crosscompiling_default OFF) +if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1" AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR) + cmake_policy(GET CMP0190 _pybind11_cmp0190) + if(_pybind11_cmp0190 STREQUAL "NEW") + set(_pybind11_use_crosscompiling_default ON) + endif() endif() -option(PYBIND11_USE_CROSSCOMPILING "Respect CMAKE_CROSSCOMPILING" OFF) +option(PYBIND11_USE_CROSSCOMPILING "Respect CMAKE_CROSSCOMPILING" + ${_pybind11_use_crosscompiling_default}) cmake_dependent_option( USE_PYTHON_INCLUDE_DIR diff --git a/tools/pybind11Common.cmake b/tools/pybind11Common.cmake index d75fb67520..194f4c0f67 100644 --- a/tools/pybind11Common.cmake +++ b/tools/pybind11Common.cmake @@ -45,12 +45,11 @@ set(pybind11_INCLUDE_DIRS # when cross-compiling, unless the CMAKE_CROSSCOMPILING_EMULATOR variable is defined. # Default PYBIND11_USE_CROSSCOMPILING to ON in that case, but never override a value the # project set explicitly (e.g. Emscripten/Pyodide defines an emulator yet still wants it ON). -# PYBIND11_USE_CROSSCOMPILING is undefined for find_package() consumers, but our own -# CMakeLists.txt always defines it via option(); _PYBIND11_USE_CROSSCOMPILING_DEFAULTED tells -# us whether that came from the project or from the option() default. +# This applies to find_package() consumers only; the pybind11 project itself computes the same +# default for its PYBIND11_USE_CROSSCOMPILING option, which is always defined. if(CMAKE_VERSION VERSION_GREATER_EQUAL "4.1" AND NOT DEFINED CMAKE_CROSSCOMPILING_EMULATOR - AND (NOT DEFINED PYBIND11_USE_CROSSCOMPILING OR _PYBIND11_USE_CROSSCOMPILING_DEFAULTED)) + AND NOT DEFINED PYBIND11_USE_CROSSCOMPILING) cmake_policy(GET CMP0190 _pybind11_cmp0190) if(_pybind11_cmp0190 STREQUAL "NEW") set(PYBIND11_USE_CROSSCOMPILING "ON")