@@ -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+
259278def 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