Skip to content

Commit dc9b395

Browse files
mathstufhenryiiipre-commit-ci[bot]
authored
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 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9591cfb commit dc9b395

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

CMakeLists.txt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,21 @@ if(PYBIND11_INSTALL)
289289

290290
# pkg-config support
291291
if(NOT prefix_for_pc_file)
292-
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
292+
if(IS_ABSOLUTE "${CMAKE_INSTALL_DATAROOTDIR}")
293+
set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}")
294+
else()
295+
set(pc_datarootdir "${CMAKE_INSTALL_DATAROOTDIR}")
296+
if(CMAKE_VERSION VERSION_LESS 3.20)
297+
set(prefix_for_pc_file "\${pcfiledir}/..")
298+
while(pc_datarootdir)
299+
get_filename_component(pc_datarootdir "${pc_datarootdir}" DIRECTORY)
300+
string(APPEND prefix_for_pc_file "/..")
301+
endwhile()
302+
else()
303+
cmake_path(RELATIVE_PATH CMAKE_INSTALL_PREFIX BASE_DIRECTORY CMAKE_INSTALL_DATAROOTDIR
304+
OUTPUT_VARIABLE prefix_for_pc_file)
305+
endif()
306+
endif()
293307
endif()
294308
join_paths(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}")
295309
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in"

0 commit comments

Comments
 (0)