Skip to content

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

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

henryiii wants to merge 5 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.

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

henryiii and others added 3 commits November 22, 2026 00:08
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
henryiii force-pushed the fix/cmake-pc-prefix-and-cmp0190 branch from 316f221 to 3febb1b Compare November 22, 2026 05:08
@rwgk

rwgk commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

— codex on behalf of Ralf —

Use the actual install prefix to compute the path back from pybind11.pc. I confirmed that the four regression cases pass on CMake 3.15.3 and 3.28.3. With CMAKE_INSTALL_DATAROOTDIR=../shared-data, the .pc file is installed beside the install prefix, so the path back needs the install directory’s name. Using the build directory as a stand-in gives prefix=${pcfiledir}/../../cmake-build in my repro, and pkg-config --cflags pybind11 misses the installed headers. I verified on both CMake versions that normalizing the data directory relative to CMAKE_INSTALL_PREFIX and calculating the path back to that same prefix fixes the sibling layout. Could you use the actual prefix and add ../shared-data to the existing regression cases? Prefix calculation

Optional test improvement. Add VERBATIM to test_build_pkgconfig_prefix so CMake escapes the command arguments for the build tool. I reproduced a shell syntax error with Unix Makefiles when the build path contained parentheses. Adding VERBATIM made all four cases pass. Test target

Resolve sibling data directories relative to the configured prefix. Cover this layout in the pkg-config regression and escape the arguments of its CMake test target.

Assisted-by: Codex:GPT-6

@rwgk rwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codex added a09aa6b on my behalf.

I'm approving, but leaving the final review to @henryiii.

Forward the make program, generator platform, toolset, and toolchain file
to the nested configure. Pass CMAKE_INSTALL_DATAROOTDIR as STRING. Add an
absolute data directory case, a relocated install, and an includedir check.

Assisted-by: ClaudeCode:claude-fable-5-2
@henryiii

Copy link
Copy Markdown
Collaborator Author

Fixing up the tests. Then I'll see if we can trim the tests, the new tests could take minutes on some systems, a CMake configure is expensive.

There's also an issue with sibling data directories not being relocatable, but focusing on this first.

@rwgk

rwgk commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

the new tests could take minutes on some systems, a CMake configure is expensive.

Oh, I see. I didn't consider that.

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