Skip to content

Commit 35503f0

Browse files
committed
Prepend sysroot to Python build-details paths when cross-compiling
Since paths contained in the `build-details.json` file are relative to the sysroot, prepend it to them to compile and link against the correct Python executable.
1 parent e716a1f commit 35503f0

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

mesonbuild/dependencies/python.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,9 @@ def sanity(self) -> bool:
247247

248248
class _PythonDependencyBase(_Base):
249249

250-
def __init__(self, python_holder: 'BasicPythonExternalProgram', embed: bool):
250+
def __init__(self, python_holder: 'BasicPythonExternalProgram', embed: bool,
251+
for_machine: 'MachineChoice'):
252+
self.for_machine = for_machine
251253
self.embed = embed
252254
self.build_config = python_holder.build_config
253255

@@ -295,6 +297,8 @@ def find_libpy(self, environment: 'Environment') -> None:
295297
path = self.build_config['libpython'].get('dynamic')
296298
if not path:
297299
raise DependencyException('Python does not provide a dynamic libpython library')
300+
sysroot = environment.properties[self.for_machine].get_sys_root() or ''
301+
path = sysroot + path
298302
if not os.path.isfile(path):
299303
raise DependencyException('Python dynamic library does not exist or is not a file')
300304
self.link_args = [path]
@@ -347,7 +351,8 @@ def get_windows_link_args(self, limited_api: bool) -> T.Optional[T.List[str]]:
347351
key = 'dynamic-stableabi'
348352
else:
349353
key = 'dynamic'
350-
return [self.build_config['libpython'][key]]
354+
sysroot = environment.properties[for_machine].get_sys_root() or ''
355+
return [sysroot + self.build_config['libpython'][key]]
351356

352357
if self.platform.startswith('win'):
353358
vernum = self.variables.get('py_version_nodot')
@@ -447,7 +452,8 @@ def find_libpy_windows(self, env: 'Environment', limited_api: bool = False) -> N
447452
class PythonPkgConfigDependency(PkgConfigDependency, _PythonDependencyBase):
448453

449454
def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any],
450-
installation: 'BasicPythonExternalProgram', embed: bool):
455+
installation: 'BasicPythonExternalProgram', embed: bool,
456+
for_machine: 'MachineChoice'):
451457
pkg_embed = '-embed' if embed and mesonlib.version_compare(installation.info['version'], '>=3.8') else ''
452458
pkg_name = f'python-{installation.version}{pkg_embed}'
453459

@@ -464,11 +470,15 @@ def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any],
464470
mlog.debug(f'Skipping pkgconfig lookup, {pkg_libdir_origin} is unset')
465471
self.is_found = False
466472
return
473+
474+
sysroot = environment.properties[for_machine].get_sys_root() or ''
475+
pkg_libdir = sysroot + pkg_libdir
476+
467477
mlog.debug(f'Searching for {pkg_libdir!r} via pkgconfig lookup in {pkg_libdir_origin}')
468478
pkgconfig_paths = [pkg_libdir] if pkg_libdir else []
469479

470480
PkgConfigDependency.__init__(self, pkg_name, environment, kwargs, extra_paths=pkgconfig_paths)
471-
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False))
481+
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False), for_machine)
472482

473483
if pkg_libdir and not self.is_found:
474484
mlog.debug(f'{pkg_name!r} could not be found in {pkg_libdir_origin}, '
@@ -492,17 +502,19 @@ def __init__(self, environment: 'Environment', kwargs: T.Dict[str, T.Any],
492502
class PythonFrameworkDependency(ExtraFrameworkDependency, _PythonDependencyBase):
493503

494504
def __init__(self, name: str, environment: 'Environment',
495-
kwargs: T.Dict[str, T.Any], installation: 'BasicPythonExternalProgram'):
505+
kwargs: T.Dict[str, T.Any], installation: 'BasicPythonExternalProgram',
506+
for_machine: 'MachineChoice'):
496507
ExtraFrameworkDependency.__init__(self, name, environment, kwargs)
497-
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False))
508+
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False), for_machine)
498509

499510

500511
class PythonSystemDependency(SystemDependency, _PythonDependencyBase):
501512

502513
def __init__(self, name: str, environment: 'Environment',
503-
kwargs: T.Dict[str, T.Any], installation: 'BasicPythonExternalProgram'):
514+
kwargs: T.Dict[str, T.Any], installation: 'BasicPythonExternalProgram',
515+
for_machine: 'MachineChoice'):
504516
SystemDependency.__init__(self, name, environment, kwargs)
505-
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False))
517+
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False), for_machine)
506518

507519
# For most platforms, match pkg-config behavior. iOS is a special case;
508520
# check for that first, so that check takes priority over
@@ -522,7 +534,8 @@ def __init__(self, name: str, environment: 'Environment',
522534

523535
# compile args
524536
if self.build_config:
525-
inc_paths = mesonlib.OrderedSet([self.build_config['c_api']['headers']])
537+
sysroot = environment.properties[for_machine].get_sys_root() or ''
538+
inc_paths = mesonlib.OrderedSet([sysroot + self.build_config['c_api']['headers']])
526539
else:
527540
inc_paths = mesonlib.OrderedSet([
528541
self.variables.get('INCLUDEPY'),
@@ -559,20 +572,20 @@ def python_factory(env: 'Environment', for_machine: 'MachineChoice',
559572

560573
if DependencyMethods.PKGCONFIG in methods:
561574
if from_installation:
562-
candidates.append(functools.partial(PythonPkgConfigDependency, env, kwargs, installation, embed))
575+
candidates.append(functools.partial(PythonPkgConfigDependency, env, kwargs, installation, embed, for_machine))
563576
else:
564577
candidates.append(functools.partial(PkgConfigDependency, 'python3', env, kwargs))
565578

566579
if DependencyMethods.SYSTEM in methods:
567-
candidates.append(functools.partial(PythonSystemDependency, 'python', env, kwargs, installation))
580+
candidates.append(functools.partial(PythonSystemDependency, 'python', env, kwargs, installation, for_machine))
568581

569582
if DependencyMethods.EXTRAFRAMEWORK in methods:
570583
nkwargs = kwargs.copy()
571584
if mesonlib.version_compare(installation.version, '>= 3'):
572585
# There is a python in /System/Library/Frameworks, but that's python 2.x,
573586
# Python 3 will always be in /Library
574587
nkwargs['paths'] = ['/Library/Frameworks']
575-
candidates.append(functools.partial(PythonFrameworkDependency, 'Python', env, nkwargs, installation))
588+
candidates.append(functools.partial(PythonFrameworkDependency, 'Python', env, nkwargs, installation, for_machine))
576589

577590
return candidates
578591

0 commit comments

Comments
 (0)