Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -411,17 +411,14 @@ if(PYBIND11_INSTALL)
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()
# 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}")
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cmake_build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions tests/test_cmake_build/pkgconfig_prefix.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# 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()