Skip to content

fix(cmake): compute pybind11.pc prefix relative to CMAKE_INSTALL_DATAROOTDIR - #6165

Open
henryiii wants to merge 3 commits into
pybind:masterfrom
henryiii:fix/cmake-pc-prefix-and-cmp0190
Open

henryiii wants to merge 3 commits into
pybind:masterfrom
henryiii:fix/cmake-pc-prefix-and-cmp0190

Conversation

@henryiii

@henryiii henryiii commented Sep 2, 2026 •

Copy link
Copy Markdown
Collaborator

🤖 AI text below 🤖

The cmake_path(RELATIVE_PATH ...) branch gave the base directory as the literal string CMAKE_INSTALL_DATAROOTDIR instead of its value, so on CMake 3.20 and later the installed pybind11.pc had an empty prefix=. Also, the .pc file needs a path relative to ${pcfiledir}, not relative to the data root.

The cmake_path branch and the while() loop are both removed. The prefix is now the relative path from the normalized pkg-config install directory to the install prefix. It uses get_filename_component(... ABSOLUTE BASE_DIR ...) and file(RELATIVE_PATH ...), which work in CMake 3.15. This also fixes CMAKE_INSTALL_DATAROOTDIR values such as . and ./share, which the loop got wrong:

CMAKE_INSTALL_DATAROOTDIR prefix=
. ${pcfiledir}/..
share, share/, ./share ${pcfiledir}/../..
lib/foo/share ${pcfiledir}/../../../..

A new test_build_pkgconfig_prefix target (part of test_cmake_build) installs with each of these values and checks that prefix= resolves to the install prefix.

The CMP0190 fix is now in a separate PR.

Suggested changelog entry:

  • Fix the prefix= value in the installed pybind11.pc, which was empty with CMake 3.20 and later, and wrong for some CMAKE_INSTALL_DATAROOTDIR values such as ..

@henryiii
henryiii marked this pull request as draft September 2, 2026 17:48
@henryiii
henryiii force-pushed the fix/cmake-pc-prefix-and-cmp0190 branch from 0113298 to 840951e Compare November 13, 2026 16:34
@henryiii henryiii changed the title fix(cmake): pybind11.pc prefix and CMP0190 default across reconfigure fix(cmake): compute pybind11.pc prefix relative to CMAKE_INSTALL_DATAROOTDIR Nov 13, 2026
@henryiii
henryiii marked this pull request as ready for review November 14, 2026 02:36
@rwgk

rwgk commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Compute the prefix from the actual pkg-config install path. I configured and installed this head with CMake 3.28 and -DCMAKE_INSTALL_DATAROOTDIR=.. CMake installs pybind11.pc at <prefix>/pkgconfig/pybind11.pc, but the generated prefix=${pcfiledir}/../.. points above the install prefix, so pkg-config --cflags pybind11 names a nonexistent include directory. The nested case lib/foo/share worked. Compute the relative path from the normalized pkg-config install directory back to the prefix and cover . alongside the nested case. Prefix computation

Generated by Codex. Ralf has yet to review these findings.

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
@henryiii

Copy link
Copy Markdown
Collaborator Author

🤖 AI text below 🤖

Confirmed. With -DCMAKE_INSTALL_DATAROOTDIR=. the file is installed at <prefix>/pkgconfig/pybind11.pc, but it had prefix=${pcfiledir}/../... ./share had the same problem (../../..).

Fixed in f4adf6c:

  • The prefix is now the relative path from the normalized pkg-config install directory to the install prefix (get_filename_component(... ABSOLUTE) and file(RELATIVE_PATH), which work with CMake 3.15).
  • Results: . gives ${pcfiledir}/.., share and ./share give ${pcfiledir}/../.., lib/foo/share gives ${pcfiledir}/../../../...
  • New target test_build_pkgconfig_prefix (part of test_cmake_build) installs with these four values and checks that prefix= resolves to the install prefix. It fails on . without the fix.

I ran the test locally with CMake 3.25 and 4.5 only. pkg-config --cflags pybind11 gives an include directory that exists for all four values.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants