Skip to content

Commit ded714f

Browse files
itamaroclaude
andcommitted
relocatable.py test: check that pkg-config gives paths inside the moved tree
`developer.pkg-config` queries `--cflags` and `--libs` for the installed python-*.pc files, with the moved tree's pkgconfig directory on PKG_CONFIG_PATH as Meson does. It fails on CPython main, where the files have an absolute prefix; the failure also says whether `--define-prefix` would have given the right paths. It is skipped without pkg-config. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
1 parent 1278647 commit ded714f

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Either way, `gather` runs CPython's top-level `make install ENSUREPIP=no` into a
6464
- Every stdlib extension module that imports in the build directory still imports, from inside the moved tree
6565
- Those paths are still right when Python is run through a symlink
6666
- A venv created from it works
67-
- `build-details.json` and `python-config` give paths inside the moved tree
67+
- `build-details.json`, `python-config` and `pkg-config` (when installed) give paths inside the moved tree
6868
- A C extension builds against it and imports
6969
- Native files don't link against libraries outside the moved tree
7070

‎relocatability.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,25 @@ def build_details_problems(
256256
return problems
257257

258258

259+
def pkg_config_problems(cflags: str, libs: str, root: pathlib.Path) -> list[str]:
260+
"""Return what's wrong with pkg-config's `--cflags` and `--libs` output.
261+
262+
The `-I` and `-L` directories must be inside *root*, and Python.h must
263+
be in one of the `-I` directories. Whether libpython is installed isn't
264+
checked: the files name it even in a build that doesn't install one.
265+
"""
266+
includes = [flag[2:] for flag in shlex.split(cflags) if flag.startswith("-I")]
267+
lib_dirs = [flag[2:] for flag in shlex.split(libs) if flag.startswith("-L")]
268+
problems = [
269+
f"{directory} is outside the relocated tree"
270+
for directory in (*includes, *lib_dirs)
271+
if not path_is_within(directory, root)
272+
]
273+
if not any(pathlib.Path(d, "Python.h").is_file() for d in includes):
274+
problems.append("no include directory contains Python.h")
275+
return problems
276+
277+
259278
def find_build_interpreter(build_dir: pathlib.Path) -> pathlib.Path:
260279
"""Return the interpreter built in *build_dir*.
261280
@@ -360,6 +379,7 @@ def run(self) -> list[CheckResult]:
360379
self._check("metadata.prefix-leaks", self._check_prefix_leaks)
361380
self._check("metadata.build-details", self._check_build_details)
362381
self._check("developer.python-config", self._check_python_config)
382+
self._check("developer.pkg-config", self._check_pkg_config)
363383
for module in STDLIB_PROBES:
364384
self._check(
365385
f"stdlib.{module}",
@@ -670,6 +690,43 @@ def _check_python_config(self) -> str:
670690
raise CheckFailed("python-config reports stale paths", failures)
671691
return "python-config reports the relocated prefix"
672692

693+
def _check_pkg_config(self) -> str:
694+
tool = shutil.which("pkg-config") or shutil.which("pkgconf")
695+
if tool is None:
696+
raise CheckSkipped("pkg-config is not installed")
697+
pc_dir = self.root / "lib" / "pkgconfig"
698+
modules = sorted(
699+
path.stem for path in pc_dir.glob("python-*.pc") if not path.is_symlink()
700+
)
701+
if not modules:
702+
raise CheckFailed("no installed pkg-config file found", [os.fspath(pc_dir)])
703+
# Like Meson: add the installation's own directory to the search path.
704+
env = {
705+
name: value
706+
for name, value in self.env.items()
707+
if not name.startswith("PKG_CONFIG_")
708+
}
709+
env["PKG_CONFIG_PATH"] = os.fspath(pc_dir)
710+
711+
def problems(module: str, *options: str) -> list[str]:
712+
outputs = []
713+
for query in ("--cflags", "--libs"):
714+
completed = self._run([tool, *options, query, module], env=env)
715+
self._require_success(completed, f"pkg-config {query} {module} failed")
716+
outputs.append(completed.stdout.strip())
717+
found = pkg_config_problems(*outputs, self.root)
718+
return [f"{module}: {problem}" for problem in found]
719+
720+
failures = [failure for module in modules for failure in problems(module)]
721+
if failures:
722+
# Tells whether a consumer can work around a stale prefix today.
723+
works = not any(problems(module, "--define-prefix") for module in modules)
724+
failures.append(
725+
f"with --define-prefix: {'no problems' if works else 'still fails'}"
726+
)
727+
raise CheckFailed("pkg-config reports stale paths", failures)
728+
return f"pkg-config resolves {', '.join(modules)} inside the relocated tree"
729+
673730
def _check_stdlib(self, module: str) -> str:
674731
if self.broken_modules is not None and module in self.broken_modules:
675732
raise CheckSkipped(

0 commit comments

Comments
 (0)