pybind11.pc: use pcfiledir for relative destinations (#4830)

* pybind11.pc: use pcfiledir for relative destinations

If the datarootdir is absolute, just use the absolute path directly.
However, if it is relative, we can compute the prefix from the location
of the `.pc` file itself. This allows the install to be relocatable.

* chore: use 3.20's cmake_path if available

* style: pre-commit fixes

* Update CMakeLists.txt

---------

Co-authored-by: Henry Schreiner <HenrySchreinerIII@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Ben Boeckel 2023-11-16 05:55:14 +00:00 committed by GitHub
parent 9591cfb0b8
commit dc9b39596d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -289,7 +289,21 @@ if(PYBIND11_INSTALL)
# pkg-config support # pkg-config support
if(NOT prefix_for_pc_file) if(NOT prefix_for_pc_file)
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
else()
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()
endif()
endif() endif()
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in" configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"