From 18bec0d3e31884dc493a03741b2ec9fef17047ee Mon Sep 17 00:00:00 2001 From: Xavier Claessens Date: Wed, 13 Apr 2022 09:35:52 -0400 Subject: pkgconfig: Use EnvironmentVariables to build PKG_CONFIG_* env The new get_env() method that returns an EnvironmentVariables object will be needed in next commit that will pass it to CustomTarget. This has the side effect to use the proper os specific path separator instead of hardcoding `:`. It is the obvious right thing to do here, but has caused issues in the past. Hopefully issues have been fixed in the meantime. If not, better deal with fallouts than keep doing the wrong thing forever. --- mesonbuild/build.py | 2 +- mesonbuild/dependencies/pkgconfig.py | 47 ++++++++++++++----------- mesonbuild/environment.py | 2 +- mesonbuild/modules/unstable_external_project.py | 5 ++- unittests/linuxliketests.py | 7 ++-- 5 files changed, 36 insertions(+), 27 deletions(-) diff --git a/mesonbuild/build.py b/mesonbuild/build.py index cb295bb..3e7911f 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -550,7 +550,7 @@ class EnvironmentVariables(HoldableObject): curr = env.get(name) return separator.join(values if curr is None else values + [curr]) - def get_env(self, full_env: T.Dict[str, str]) -> T.Dict[str, str]: + def get_env(self, full_env: T.MutableMapping[str, str]) -> T.Dict[str, str]: env = full_env.copy() for method, name, values, separator in self.envvars: env[name] = method(env, name, values, separator) diff --git a/mesonbuild/dependencies/pkgconfig.py b/mesonbuild/dependencies/pkgconfig.py index 55a364c..e3f6e5c 100644 --- a/mesonbuild/dependencies/pkgconfig.py +++ b/mesonbuild/dependencies/pkgconfig.py @@ -13,6 +13,8 @@ # limitations under the License. from __future__ import annotations +from pathlib import Path + from .base import ExternalDependency, DependencyException, sort_libpaths, DependencyTypeName from ..mesonlib import OptionKey, OrderedSet, PerMachine, Popen_safe from ..programs import find_external_program, ExternalProgram @@ -27,6 +29,7 @@ if T.TYPE_CHECKING: from ..environment import Environment from ..mesonlib import MachineChoice from .._typing import ImmutableListProtocol + from ..build import EnvironmentVariables class PkgConfigDependency(ExternalDependency): # The class's copy of the pkg-config path. Avoids having to search for it @@ -122,36 +125,40 @@ class PkgConfigDependency(ExternalDependency): return rc, out, err @staticmethod - def setup_env(env: T.MutableMapping[str, str], environment: 'Environment', for_machine: MachineChoice, - extra_path: T.Optional[str] = None) -> None: - extra_paths: T.List[str] = environment.coredata.options[OptionKey('pkg_config_path', machine=for_machine)].value[:] - if extra_path and extra_path not in extra_paths: - extra_paths.append(extra_path) + def get_env(environment: 'Environment', for_machine: MachineChoice, + uninstalled: bool = False) -> 'EnvironmentVariables': + from ..build import EnvironmentVariables + env = EnvironmentVariables() + key = OptionKey('pkg_config_path', machine=for_machine) + extra_paths: T.List[str] = environment.coredata.options[key].value[:] + if uninstalled: + uninstalled_path = Path(environment.get_build_dir(), 'meson-uninstalled').as_posix() + if uninstalled_path not in extra_paths: + extra_paths.append(uninstalled_path) + env.set('PKG_CONFIG_PATH', extra_paths) sysroot = environment.properties[for_machine].get_sys_root() if sysroot: - env['PKG_CONFIG_SYSROOT_DIR'] = sysroot - new_pkg_config_path = ':'.join([p for p in extra_paths]) - env['PKG_CONFIG_PATH'] = new_pkg_config_path - + env.set('PKG_CONFIG_SYSROOT_DIR', [sysroot]) pkg_config_libdir_prop = environment.properties[for_machine].get_pkg_config_libdir() if pkg_config_libdir_prop: - new_pkg_config_libdir = ':'.join([p for p in pkg_config_libdir_prop]) - env['PKG_CONFIG_LIBDIR'] = new_pkg_config_libdir + env.set('PKG_CONFIG_LIBDIR', pkg_config_libdir_prop) + return env + + @staticmethod + def setup_env(env: T.MutableMapping[str, str], environment: 'Environment', for_machine: MachineChoice, + uninstalled: bool = False) -> T.Dict[str, str]: + envvars = PkgConfigDependency.get_env(environment, for_machine, uninstalled) + env = envvars.get_env(env) # Dump all PKG_CONFIG environment variables for key, value in env.items(): if key.startswith('PKG_'): mlog.debug(f'env[{key}]: {value}') + return env - def _call_pkgbin(self, args: T.List[str], env: T.Optional[T.Dict[str, str]] = None) -> T.Tuple[int, str, str]: - # Always copy the environment since we're going to modify it - # with pkg-config variables - if env is None: - env = os.environ.copy() - else: - env = env.copy() - + def _call_pkgbin(self, args: T.List[str], env: T.Optional[T.MutableMapping[str, str]] = None) -> T.Tuple[int, str, str]: assert isinstance(self.pkgbin, ExternalProgram) - PkgConfigDependency.setup_env(env, self.env, self.for_machine) + env = env or os.environ + env = PkgConfigDependency.setup_env(env, self.env, self.for_machine) fenv = frozenset(env.items()) targs = tuple(args) diff --git a/mesonbuild/environment.py b/mesonbuild/environment.py index 5e8575a..32141c9 100644 --- a/mesonbuild/environment.py +++ b/mesonbuild/environment.py @@ -654,7 +654,7 @@ class Environment: _p_env = re.split(r':|;', p_env) p_list = list(mesonlib.OrderedSet(_p_env)) elif keyname == 'pkg_config_path': - p_list = list(mesonlib.OrderedSet(p_env.split(':'))) + p_list = list(mesonlib.OrderedSet(p_env.split(os.pathsep))) else: p_list = split_args(p_env) p_list = [e for e in p_list if e] # filter out any empty elements diff --git a/mesonbuild/modules/unstable_external_project.py b/mesonbuild/modules/unstable_external_project.py index d2c5b22..33a44d9 100644 --- a/mesonbuild/modules/unstable_external_project.py +++ b/mesonbuild/modules/unstable_external_project.py @@ -155,9 +155,8 @@ class ExternalProject(NewExtensionModule): self.run_env['LDFLAGS'] = self._quote_and_join(link_args) self.run_env = self.user_env.get_env(self.run_env) - - PkgConfigDependency.setup_env(self.run_env, self.env, MachineChoice.HOST, - Path(self.env.get_build_dir(), 'meson-uninstalled').as_posix()) + self.run_env = PkgConfigDependency.setup_env(self.run_env, self.env, MachineChoice.HOST, + uninstalled=True) self.build_dir.mkdir(parents=True, exist_ok=True) self._run('configure', configure_cmd, workdir) diff --git a/unittests/linuxliketests.py b/unittests/linuxliketests.py index 2177cdd..ed6c959 100644 --- a/unittests/linuxliketests.py +++ b/unittests/linuxliketests.py @@ -1152,9 +1152,12 @@ class LinuxlikeTests(BasePlatformTests): env = get_fake_env(testdir, self.builddir, self.prefix) env.coredata.set_options({OptionKey('pkg_config_path'): pkg_dir}, subproject='') - PkgConfigDependency.setup_env({}, env, MachineChoice.HOST, pkg_dir) + # Regression test: This used to modify the value of `pkg_config_path` + # option, adding the meson-uninstalled directory to it. + PkgConfigDependency.setup_env({}, env, MachineChoice.HOST, uninstalled=True) + pkg_config_path = env.coredata.options[OptionKey('pkg_config_path')].value - self.assertEqual(len(pkg_config_path), 1) + self.assertEqual(pkg_config_path, [pkg_dir]) @skipIfNoPkgconfig def test_pkgconfig_internal_libraries(self): -- cgit v1.1