| 1 |
# SPDX-License-Identifier: Apache-2. |
| 2 |
# Copyright 2022 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import functools, json, operator, os, textwrap |
| 7 |
from pathlib import Path |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from .. import mesonlib, mlog |
| 11 |
from .base import process_method_kw, DependencyCandidate, DependencyException, DependencyMethods, ExternalDependency, SystemDependency |
| 12 |
from .configtool import ConfigToolDependency |
| 13 |
from .detect import packages |
| 14 |
from .factory import DependencyFactory |
| 15 |
from .framework import ExtraFrameworkDependency |
| 16 |
from .pkgconfig import PkgConfigDependency |
| 17 |
from ..envconfig import detect_cpu_family |
| 18 |
from ..mesonlib import MachineChoice, path_is_in_root |
| 19 |
from ..programs import ExternalProgram |
| 20 |
from ..options import OptionKey |
| 21 |
from ..scripts import destdir_join |
| 22 |
|
| 23 |
if T.TYPE_CHECKING: |
| 24 |
from typing_extensions import Final, TypedDict |
| 25 |
|
| 26 |
from .factory import DependencyGenerator |
| 27 |
from ..environment import Environment |
| 28 |
from .base import DependencyObjectKWs |
| 29 |
|
| 30 |
class PythonIntrospectionDict(TypedDict): |
| 31 |
|
| 32 |
install_paths: T.Dict[str, str] |
| 33 |
is_pypy: bool |
| 34 |
is_venv: bool |
| 35 |
is_freethreaded: bool |
| 36 |
link_libpython: bool |
| 37 |
sysconfig_paths: T.Dict[str, str] |
| 38 |
paths: T.Dict[str, str] |
| 39 |
platform: str |
| 40 |
suffix: str |
| 41 |
limited_api_suffix: str |
| 42 |
variables: T.Dict[str, str] |
| 43 |
version: str |
| 44 |
|
| 45 |
_Base = ExternalDependency |
| 46 |
else: |
| 47 |
_Base = object |
| 48 |
|
| 49 |
|
| 50 |
class Pybind11ConfigToolDependency(ConfigToolDependency): |
| 51 |
|
| 52 |
tools = ['pybind11-config'] |
| 53 |
|
| 54 |
# any version of the tool is valid, since this is header-only |
| 55 |
allow_default_for_cross = True |
| 56 |
|
| 57 |
# pybind11 in 2.10.4 added --version, sanity-check another flag unique to it |
| 58 |
# in the meantime |
| 59 |
skip_version = '--pkgconfigdir' |
| 60 |
|
| 61 |
def __init__(self, name: str, environment: Environment, kwargs: DependencyObjectKWs): |
| 62 |
super().__init__(name, environment, kwargs) |
| 63 |
if not self.is_found: |
| 64 |
return |
| 65 |
self.compile_args = self.get_config_value(['--includes'], 'compile_args') |
| 66 |
|
| 67 |
|
| 68 |
class NumPyConfigToolDependency(ConfigToolDependency): |
| 69 |
|
| 70 |
tools = ['numpy-config'] |
| 71 |
|
| 72 |
def __init__(self, name: str, environment: Environment, kwargs: DependencyObjectKWs): |
| 73 |
super().__init__(name, environment, kwargs) |
| 74 |
if not self.is_found: |
| 75 |
return |
| 76 |
self.compile_args = self.get_config_value(['--cflags'], 'compile_args') |
| 77 |
|
| 78 |
|
| 79 |
class PythonBuildConfig: |
| 80 |
"""PEP 739 build-details.json config file.""" |
| 81 |
|
| 82 |
IMPLEMENTED_VERSION: Final[str] = '1.0' |
| 83 |
"""Schema version currently implemented.""" |
| 84 |
_PATH_KEYS = ( |
| 85 |
'base_interpreter', |
| 86 |
'libpython.dynamic', |
| 87 |
'libpython.dynamic_stableabi', |
| 88 |
'libpython.static', |
| 89 |
'c_api.headers', |
| 90 |
'c_api.pkgconfig_path', |
| 91 |
) |
| 92 |
"""Path keys — may be relative, need to be expanded.""" |
| 93 |
|
| 94 |
def __init__(self, path: str) -> None: |
| 95 |
self._path = Path(path) |
| 96 |
|
| 97 |
try: |
| 98 |
self._data = json.loads(self._path.read_text(encoding='utf8')) |
| 99 |
except OSError as e: |
| 100 |
raise DependencyException(f'Failed to read python.build_config: {e}') from e |
| 101 |
|
| 102 |
self._validate_data() |
| 103 |
self._expand_paths() |
| 104 |
|
| 105 |
def __getitem__(self, key: str) -> T.Any: |
| 106 |
return functools.reduce(operator.getitem, key.split('.'), self._data) |
| 107 |
|
| 108 |
def __contains__(self, key: str) -> bool: |
| 109 |
try: |
| 110 |
self[key] |
| 111 |
except KeyError: |
| 112 |
return False |
| 113 |
else: |
| 114 |
return True |
| 115 |
|
| 116 |
def get(self, key: str, default: T.Any = None) -> T.Any: |
| 117 |
try: |
| 118 |
return self[key] |
| 119 |
except KeyError: |
| 120 |
return default |
| 121 |
|
| 122 |
def _validate_data(self) -> None: |
| 123 |
schema_version = self._data['schema_version'] |
| 124 |
if mesonlib.version_compare(schema_version, '< 1.0'): |
| 125 |
raise DependencyException(f'Invalid schema_version in python.build_config: {schema_version}') |
| 126 |
if mesonlib.version_compare(schema_version, '>= 2.0'): |
| 127 |
raise DependencyException( |
| 128 |
f'Unsupported schema_version {schema_version!r} in python.build_config, ' |
| 129 |
f'but we only implement support for {self.IMPLEMENTED_VERSION!r}' |
| 130 |
) |
| 131 |
# Schema version that we currently understand |
| 132 |
if mesonlib.version_compare(schema_version, f'> {self.IMPLEMENTED_VERSION}'): |
| 133 |
mlog.log( |
| 134 |
f'python.build_config has schema_version {schema_version!r}, ' |
| 135 |
f'but we only implement support for {self.IMPLEMENTED_VERSION!r}, ' |
| 136 |
'new functionality might be missing' |
| 137 |
) |
| 138 |
|
| 139 |
def _expand_paths(self) -> None: |
| 140 |
"""Expand relative path (they're relative to base_prefix).""" |
| 141 |
for key in self._PATH_KEYS: |
| 142 |
if key not in self: |
| 143 |
continue |
| 144 |
parent, _, child = key.rpartition('.') |
| 145 |
container = self[parent] if parent else self._data |
| 146 |
path = Path(container[child]) |
| 147 |
if not path.is_absolute(): |
| 148 |
container[child] = os.fspath(self.base_prefix / path) |
| 149 |
|
| 150 |
@property |
| 151 |
def config_path(self) -> Path: |
| 152 |
return self._path |
| 153 |
|
| 154 |
@mesonlib.lazy_property |
| 155 |
def base_prefix(self) -> Path: |
| 156 |
path = Path(self._data['base_prefix']) |
| 157 |
if path.is_absolute(): |
| 158 |
return path |
| 159 |
# Non-absolute paths are relative to the build config directory |
| 160 |
return self.config_path.parent / path |
| 161 |
|
| 162 |
|
| 163 |
class BasicPythonExternalProgram(ExternalProgram): |
| 164 |
def __init__(self, name: str, command: T.Optional[T.List[str]] = None, |
| 165 |
ext_prog: T.Optional[ExternalProgram] = None, |
| 166 |
build_config_path: T.Optional[str] = None): |
| 167 |
if ext_prog is None: |
| 168 |
super().__init__(name, command=command, silent=True) |
| 169 |
else: |
| 170 |
self.name = name |
| 171 |
self.command = ext_prog.command |
| 172 |
self.path = ext_prog.path |
| 173 |
self.cached_version = None |
| 174 |
self.version_arg = '--version' |
| 175 |
|
| 176 |
self.build_config = PythonBuildConfig(build_config_path) if build_config_path else None |
| 177 |
|
| 178 |
# We want strong key values, so we always populate this with bogus data. |
| 179 |
# Otherwise to make the type checkers happy we'd have to do .get() for |
| 180 |
# everycall, even though we know that the introspection data will be |
| 181 |
# complete |
| 182 |
self.info: 'PythonIntrospectionDict' = { |
| 183 |
'install_paths': {}, |
| 184 |
'is_pypy': False, |
| 185 |
'is_venv': False, |
| 186 |
'is_freethreaded': False, |
| 187 |
'link_libpython': False, |
| 188 |
'sysconfig_paths': {}, |
| 189 |
'paths': {}, |
| 190 |
'platform': 'sentinel', |
| 191 |
'suffix': 'sentinel', |
| 192 |
'limited_api_suffix': 'sentinel', |
| 193 |
'variables': {}, |
| 194 |
'version': '0.0', |
| 195 |
} |
| 196 |
self.pure: bool = True |
| 197 |
|
| 198 |
@property |
| 199 |
def version(self) -> str: |
| 200 |
if self.build_config: |
| 201 |
value = self.build_config['language']['version'] |
| 202 |
else: |
| 203 |
value = self.info['variables'].get('LDVERSION') or self.info['version'] |
| 204 |
assert isinstance(value, str) |
| 205 |
return value |
| 206 |
|
| 207 |
def _check_version(self, version: str) -> bool: |
| 208 |
if self.name == 'python2': |
| 209 |
return mesonlib.version_compare(version, '< 3.0') |
| 210 |
elif self.name == 'python3': |
| 211 |
return mesonlib.version_compare(version, '>= 3.0') |
| 212 |
return True |
| 213 |
|
| 214 |
def sanity(self) -> bool: |
| 215 |
# Sanity check, we expect to have something that at least quacks in tune |
| 216 |
|
| 217 |
if self.build_config: |
| 218 |
if not self.build_config['libpython']: |
| 219 |
mlog.debug('This Python installation does not provide a libpython') |
| 220 |
return False |
| 221 |
if not self.build_config['c_api']: |
| 222 |
mlog.debug('This Python installation does support the C API') |
| 223 |
return False |
| 224 |
|
| 225 |
import importlib.resources |
| 226 |
|
| 227 |
with importlib.resources.path('mesonbuild.scripts', 'python_info.py') as f: |
| 228 |
cmd = self.get_command() + [str(f)] |
| 229 |
env = os.environ.copy() |
| 230 |
env['SETUPTOOLS_USE_DISTUTILS'] = 'stdlib' |
| 231 |
p, stdout, stderr = mesonlib.Popen_safe(cmd, env=env) |
| 232 |
|
| 233 |
try: |
| 234 |
info = json.loads(stdout) |
| 235 |
except json.JSONDecodeError: |
| 236 |
info = None |
| 237 |
mlog.debug('Could not introspect Python (%s): exit code %d' % (str(p.args), p.returncode)) |
| 238 |
mlog.debug('Program stdout:\n') |
| 239 |
mlog.debug(stdout) |
| 240 |
mlog.debug('Program stderr:\n') |
| 241 |
mlog.debug(stderr) |
| 242 |
|
| 243 |
if info is not None and self._check_version(info['version']): |
| 244 |
self.info = T.cast('PythonIntrospectionDict', info) |
| 245 |
return True |
| 246 |
else: |
| 247 |
return False |
| 248 |
|
| 249 |
|
| 250 |
class _PythonDependencyBase(_Base): |
| 251 |
|
| 252 |
for_machine: MachineChoice |
| 253 |
|
| 254 |
def __init__(self, python_holder: 'BasicPythonExternalProgram', embed: bool): |
| 255 |
self.embed = embed |
| 256 |
self.build_config = python_holder.build_config |
| 257 |
|
| 258 |
if self.build_config: |
| 259 |
self.version = self.build_config['language']['version'] |
| 260 |
self.platform = self.build_config['platform'] |
| 261 |
self.is_freethreaded = 't' in self.build_config['abi']['flags'] |
| 262 |
self.link_libpython = self.build_config['libpython']['link_extensions'] |
| 263 |
# TODO: figure out how to deal with frameworks |
| 264 |
# see the logic at the bottom of PythonPkgConfigDependency.__init__() |
| 265 |
if self.env.machines.host.is_darwin(): |
| 266 |
raise DependencyException('--python.build-config is not supported on Darwin') |
| 267 |
else: |
| 268 |
self.version = python_holder.info['version'] |
| 269 |
self.platform = python_holder.info['platform'] |
| 270 |
self.is_freethreaded = python_holder.info['is_freethreaded'] |
| 271 |
self.link_libpython = python_holder.info['link_libpython'] |
| 272 |
# This data shouldn't be needed when build_config is set |
| 273 |
self.is_pypy = python_holder.info['is_pypy'] |
| 274 |
self.variables = python_holder.info['variables'] |
| 275 |
|
| 276 |
self.paths = python_holder.info['paths'] |
| 277 |
|
| 278 |
# The "-embed" version of python.pc / python-config was introduced in 3.8, |
| 279 |
# and distutils extension linking was changed to be considered a non embed |
| 280 |
# usage. Before then, this dependency always uses the embed=True handling |
| 281 |
# because that is the only one that exists. |
| 282 |
# |
| 283 |
# On macOS and some Linux distros (Debian) distutils doesn't link extensions |
| 284 |
# against libpython, even on 3.7 and below. We call into distutils and |
| 285 |
# mirror its behavior. See https://github.com/mesonbuild/meson/issues/4117 |
| 286 |
if not self.link_libpython: |
| 287 |
self.link_libpython = embed |
| 288 |
|
| 289 |
self.info: T.Optional[T.Dict[str, str]] = None |
| 290 |
if mesonlib.version_compare(self.version, '>= 3.0'): |
| 291 |
self.major_version = 3 |
| 292 |
else: |
| 293 |
self.major_version = 2 |
| 294 |
|
| 295 |
# pyconfig.h is shared between regular and free-threaded builds in the |
| 296 |
# Windows installer from python.org, and hence does not define |
| 297 |
# Py_GIL_DISABLED correctly. So do it here: |
| 298 |
if mesonlib.is_windows() and self.is_freethreaded: |
| 299 |
self.compile_args += ['-DPy_GIL_DISABLED'] |
| 300 |
|
| 301 |
def find_libpy(self, environment: 'Environment') -> None: |
| 302 |
if self.build_config: |
| 303 |
path = self.build_config['libpython'].get('dynamic') |
| 304 |
if not path: |
| 305 |
raise DependencyException('Python does not provide a dynamic libpython library') |
| 306 |
sysroot = environment.properties[self.for_machine].get_sys_root() |
| 307 |
if sysroot and not path_is_in_root(Path(path), Path(sysroot)): |
| 308 |
path = destdir_join(sysroot, path) |
| 309 |
if not os.path.isfile(path): |
| 310 |
raise DependencyException('Python dynamic library does not exist or is not a file') |
| 311 |
self.link_args = [path] |
| 312 |
self.is_found = True |
| 313 |
return |
| 314 |
|
| 315 |
if self.is_pypy: |
| 316 |
if self.major_version == 3: |
| 317 |
libname = 'pypy3-c' |
| 318 |
else: |
| 319 |
libname = 'pypy-c' |
| 320 |
libdir = os.path.join(self.variables.get('base'), 'bin') |
| 321 |
libdirs = [libdir] |
| 322 |
else: |
| 323 |
libname = f'python{self.version}' |
| 324 |
if 'DEBUG_EXT' in self.variables: |
| 325 |
libname += self.variables['DEBUG_EXT'] |
| 326 |
if 'ABIFLAGS' in self.variables: |
| 327 |
libname += self.variables['ABIFLAGS'] |
| 328 |
libdirs = [] |
| 329 |
|
| 330 |
largs = self.clib_compiler.find_library(libname, libdirs) |
| 331 |
if largs is not None: |
| 332 |
self.link_args = largs |
| 333 |
self.is_found = True |
| 334 |
|
| 335 |
def get_windows_python_arch(self) -> str: |
| 336 |
if self.platform.startswith('mingw'): |
| 337 |
if 'x86_64' in self.platform: |
| 338 |
return 'x86_64' |
| 339 |
elif 'i686' in self.platform: |
| 340 |
return 'x86' |
| 341 |
elif 'aarch64' in self.platform: |
| 342 |
return 'aarch64' |
| 343 |
else: |
| 344 |
raise DependencyException(f'MinGW Python built with unknown platform {self.platform!r}, please file a bug') |
| 345 |
elif self.platform == 'win32': |
| 346 |
return 'x86' |
| 347 |
elif self.platform in {'win64', 'win-amd64'}: |
| 348 |
return 'x86_64' |
| 349 |
elif self.platform in {'win-arm64'}: |
| 350 |
return 'aarch64' |
| 351 |
raise DependencyException('Unknown Windows Python platform {self.platform!r}') |
| 352 |
|
| 353 |
def get_windows_link_args(self, limited_api: bool, environment: 'Environment') -> T.Optional[T.List[str]]: |
| 354 |
if self.build_config: |
| 355 |
if self.static: |
| 356 |
key = 'static' |
| 357 |
elif limited_api: |
| 358 |
key = 'dynamic-stableabi' |
| 359 |
else: |
| 360 |
key = 'dynamic' |
| 361 |
sysroot = environment.properties[self.for_machine].get_sys_root() |
| 362 |
path = self.build_config['libpython'][key] |
| 363 |
if sysroot and not path_is_in_root(Path(path), Path(sysroot)): |
| 364 |
path = destdir_join(sysroot, path) |
| 365 |
return [path] |
| 366 |
|
| 367 |
if self.platform.startswith('win'): |
| 368 |
vernum = self.variables.get('py_version_nodot') |
| 369 |
verdot = self.variables.get('py_version_short') |
| 370 |
imp_lower = self.variables.get('implementation_lower', 'python') |
| 371 |
if self.static: |
| 372 |
libpath = Path('libs') / f'libpython{vernum}.a' |
| 373 |
else: |
| 374 |
if limited_api: |
| 375 |
vernum = vernum[0] |
| 376 |
comp = self.get_compiler() |
| 377 |
if comp.id == "gcc": |
| 378 |
if imp_lower == 'pypy' and verdot == '3.8': |
| 379 |
# The naming changed between 3.8 and 3.9 |
| 380 |
libpath = Path('libpypy3-c.dll') |
| 381 |
elif imp_lower == 'pypy': |
| 382 |
libpath = Path(f'libpypy{verdot}-c.dll') |
| 383 |
else: |
| 384 |
if self.is_freethreaded: |
| 385 |
libpath = Path(f'python{vernum}t.dll') |
| 386 |
else: |
| 387 |
libpath = Path(f'python{vernum}.dll') |
| 388 |
else: |
| 389 |
if self.is_freethreaded: |
| 390 |
libpath = Path('libs') / f'python{vernum}t.lib' |
| 391 |
else: |
| 392 |
libpath = Path('libs') / f'python{vernum}.lib' |
| 393 |
# For a debug build, pyconfig.h may force linking with |
| 394 |
# pythonX_d.lib (see meson#10776). This cannot be avoided |
| 395 |
# and won't work unless we also have a debug build of |
| 396 |
# Python itself (except with pybind11, which has an ugly |
| 397 |
# hack to work around this) - so emit a warning to explain |
| 398 |
# the cause of the expected link error. |
| 399 |
buildtype = self.env.coredata.optstore.get_value_for(OptionKey('buildtype')) |
| 400 |
assert isinstance(buildtype, str) |
| 401 |
debug = self.env.coredata.optstore.get_value_for(OptionKey('debug')) |
| 402 |
# `debugoptimized` buildtype may not set debug=True currently, see gh-11645 |
| 403 |
is_debug_build = debug or buildtype == 'debug' |
| 404 |
vscrt_debug = False |
| 405 |
if OptionKey('b_vscrt') in self.env.coredata.optstore: |
| 406 |
vscrt = self.env.coredata.optstore.get_value_for('b_vscrt') |
| 407 |
if vscrt in {'mdd', 'mtd', 'from_buildtype', 'static_from_buildtype'}: |
| 408 |
vscrt_debug = True |
| 409 |
if is_debug_build and vscrt_debug and not self.variables.get('Py_DEBUG'): |
| 410 |
mlog.warning(textwrap.dedent('''\ |
| 411 |
Using a debug build type with MSVC or an MSVC-compatible compiler |
| 412 |
when the Python interpreter is not also a debug build will almost |
| 413 |
certainly result in a failed build. Prefer using a release build |
| 414 |
type or a debug Python interpreter. |
| 415 |
''')) |
| 416 |
# base_prefix to allow for virtualenvs. |
| 417 |
lib = Path(self.variables.get('base_prefix')) / libpath |
| 418 |
elif self.platform.startswith('mingw'): |
| 419 |
if self.static: |
| 420 |
if limited_api: |
| 421 |
libname = self.variables.get('ABI3DLLLIBRARY') |
| 422 |
else: |
| 423 |
libname = self.variables.get('LIBRARY') |
| 424 |
else: |
| 425 |
if limited_api: |
| 426 |
libname = self.variables.get('ABI3LDLIBRARY') |
| 427 |
else: |
| 428 |
libname = self.variables.get('LDLIBRARY') |
| 429 |
lib = Path(self.variables.get('LIBDIR')) / libname |
| 430 |
else: |
| 431 |
raise mesonlib.MesonBugException( |
| 432 |
'On a Windows path, but the OS doesn\'t appear to be Windows or MinGW.') |
| 433 |
if not lib.exists(): |
| 434 |
mlog.log('Could not find Python3 library {!r}'.format(str(lib))) |
| 435 |
return None |
| 436 |
return [str(lib)] |
| 437 |
|
| 438 |
def find_libpy_windows(self, env: 'Environment', limited_api: bool = False) -> None: |
| 439 |
''' |
| 440 |
Find python3 libraries on Windows and also verify that the arch matches |
| 441 |
what we are building for. |
| 442 |
''' |
| 443 |
try: |
| 444 |
pyarch = self.get_windows_python_arch() |
| 445 |
except DependencyException as e: |
| 446 |
mlog.log(str(e)) |
| 447 |
self.is_found = False |
| 448 |
return |
| 449 |
arch = detect_cpu_family(env.coredata.compilers.host) |
| 450 |
if arch != pyarch: |
| 451 |
mlog.log('Need', mlog.bold(self.name), f'for {arch}, but found {pyarch}') |
| 452 |
self.is_found = False |
| 453 |
return |
| 454 |
# This can fail if the library is not found |
| 455 |
largs = self.get_windows_link_args(limited_api, env) |
| 456 |
if largs is None: |
| 457 |
self.is_found = False |
| 458 |
return |
| 459 |
self.link_args = largs |
| 460 |
self.is_found = True |
| 461 |
|
| 462 |
|
| 463 |
class PythonPkgConfigDependency(PkgConfigDependency, _PythonDependencyBase): |
| 464 |
|
| 465 |
# name is needed for polymorphism |
| 466 |
def __init__(self, name: str, environment: Environment, kwargs: DependencyObjectKWs, |
| 467 |
installation: 'BasicPythonExternalProgram'): |
| 468 |
embed = kwargs.get('embed', False) |
| 469 |
pkg_embed = '-embed' if embed and mesonlib.version_compare(installation.info['version'], '>=3.8') else '' |
| 470 |
pkg_name = f'python-{installation.version}{pkg_embed}' |
| 471 |
|
| 472 |
if installation.build_config: |
| 473 |
pkg_libdir = installation.build_config.get('c_api.pkgconfig_path') |
| 474 |
pkg_libdir_origin = 'c_api.pkgconfig_path from the Python build config' |
| 475 |
else: |
| 476 |
pkg_libdir = installation.info['variables'].get('LIBPC') |
| 477 |
pkg_libdir_origin = 'LIBPC' |
| 478 |
if pkg_libdir is None: |
| 479 |
# we do not fall back to system directories, since this could lead |
| 480 |
# to using pkg-config of another Python installation, for example |
| 481 |
# we could end up using CPython .pc file for PyPy |
| 482 |
mlog.debug(f'Skipping pkgconfig lookup, {pkg_libdir_origin} is unset') |
| 483 |
self.is_found = False |
| 484 |
return |
| 485 |
|
| 486 |
for_machine = kwargs['native'] |
| 487 |
sysroot = environment.properties[for_machine].get_sys_root() |
| 488 |
if sysroot and not path_is_in_root(Path(pkg_libdir), Path(sysroot)): |
| 489 |
pkg_libdir = destdir_join(sysroot, pkg_libdir) |
| 490 |
|
| 491 |
mlog.debug(f'Searching for {pkg_libdir!r} via pkgconfig lookup in {pkg_libdir_origin}') |
| 492 |
pkgconfig_paths = [pkg_libdir] if pkg_libdir else [] |
| 493 |
|
| 494 |
PkgConfigDependency.__init__(self, pkg_name, environment, kwargs, extra_paths=pkgconfig_paths) |
| 495 |
_PythonDependencyBase.__init__(self, installation, embed) |
| 496 |
|
| 497 |
if pkg_libdir and not self.is_found: |
| 498 |
mlog.debug(f'{pkg_name!r} could not be found in {pkg_libdir_origin}, ' |
| 499 |
'this is likely due to a relocated python installation') |
| 500 |
return |
| 501 |
|
| 502 |
# pkg-config files are usually accurate starting with python 3.8 |
| 503 |
if not self.link_libpython and mesonlib.version_compare(self.version, '< 3.8'): |
| 504 |
self.link_args = [] |
| 505 |
|
| 506 |
# But not Apple, because it's a framework |
| 507 |
if self.env.machines.host.is_darwin() and 'PYTHONFRAMEWORKPREFIX' in self.variables: |
| 508 |
framework_prefix = self.variables['PYTHONFRAMEWORKPREFIX'] |
| 509 |
# Add rpath, will be de-duplicated if necessary |
| 510 |
if framework_prefix.startswith('/Applications/Xcode.app/'): |
| 511 |
self.link_args += ['-Wl,-rpath,' + framework_prefix] |
| 512 |
if self.raw_link_args is not None: |
| 513 |
# When None, self.link_args is used |
| 514 |
self.raw_link_args += ['-Wl,-rpath,' + framework_prefix] |
| 515 |
|
| 516 |
|
| 517 |
class PythonFrameworkDependency(ExtraFrameworkDependency, _PythonDependencyBase): |
| 518 |
|
| 519 |
def __init__(self, name: str, environment: 'Environment', |
| 520 |
kwargs: DependencyObjectKWs, installation: 'BasicPythonExternalProgram'): |
| 521 |
ExtraFrameworkDependency.__init__(self, name, environment, kwargs) |
| 522 |
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False)) |
| 523 |
|
| 524 |
|
| 525 |
class PythonSystemDependency(SystemDependency, _PythonDependencyBase): |
| 526 |
|
| 527 |
def __init__(self, name: str, environment: 'Environment', |
| 528 |
kwargs: DependencyObjectKWs, installation: BasicPythonExternalProgram): |
| 529 |
SystemDependency.__init__(self, name, environment, kwargs) |
| 530 |
_PythonDependencyBase.__init__(self, installation, kwargs.get('embed', False)) |
| 531 |
|
| 532 |
# For most platforms, match pkg-config behavior. iOS is a special case; |
| 533 |
# check for that first, so that check takes priority over |
| 534 |
# `link_libpython` (which *shouldn't* be set, but just in case) |
| 535 |
if self.platform.startswith('ios-'): |
| 536 |
# iOS doesn't use link_libpython - it links with the *framework*. |
| 537 |
self.link_args = ['-framework', 'Python', '-F', self.variables.get('base_prefix')] |
| 538 |
self.is_found = True |
| 539 |
elif self.link_libpython: |
| 540 |
# link args |
| 541 |
if mesonlib.is_windows(): |
| 542 |
self.find_libpy_windows(environment, limited_api=False) |
| 543 |
else: |
| 544 |
self.find_libpy(environment) |
| 545 |
else: |
| 546 |
self.is_found = True |
| 547 |
|
| 548 |
# compile args |
| 549 |
if self.build_config: |
| 550 |
sysroot = environment.properties[self.for_machine].get_sys_root() |
| 551 |
path = self.build_config['c_api']['headers'] |
| 552 |
if sysroot and not path_is_in_root(Path(path), Path(sysroot)): |
| 553 |
path = destdir_join(sysroot, path) |
| 554 |
inc_paths = mesonlib.OrderedSet([path]) |
| 555 |
else: |
| 556 |
inc_paths = mesonlib.OrderedSet([ |
| 557 |
self.variables.get('INCLUDEPY'), |
| 558 |
self.paths.get('include'), |
| 559 |
self.paths.get('platinclude')]) |
| 560 |
|
| 561 |
self.compile_args += ['-I' + path for path in inc_paths if path] |
| 562 |
|
| 563 |
# https://sourceforge.net/p/mingw-w64/mailman/message/30504611/ |
| 564 |
# https://github.com/python/cpython/pull/100137 |
| 565 |
if mesonlib.is_windows() and self.get_windows_python_arch().endswith('64') and mesonlib.version_compare(self.version, '<3.12'): |
| 566 |
self.compile_args += ['-DMS_WIN64='] |
| 567 |
|
| 568 |
if not self.clib_compiler.has_header('Python.h', '', extra_args=self.compile_args)[0]: |
| 569 |
self.is_found = False |
| 570 |
|
| 571 |
def python_factory(env: Environment, kwargs: DependencyObjectKWs, |
| 572 |
installation: T.Optional['BasicPythonExternalProgram'] = None) -> T.List['DependencyGenerator']: |
| 573 |
# We can't use the factory_methods decorator here, as we need to pass the |
| 574 |
# extra installation argument |
| 575 |
methods = process_method_kw({DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM}, kwargs) |
| 576 |
candidates: T.List['DependencyGenerator'] = [] |
| 577 |
from_installation = installation is not None |
| 578 |
# When not invoked through the python module, default installation. |
| 579 |
if installation is None: |
| 580 |
installation = BasicPythonExternalProgram('python3', mesonlib.python_command) |
| 581 |
installation.sanity() |
| 582 |
|
| 583 |
if DependencyMethods.PKGCONFIG in methods: |
| 584 |
if from_installation: |
| 585 |
candidates.append(DependencyCandidate( |
| 586 |
functools.partial(PythonPkgConfigDependency, installation=installation), |
| 587 |
'python3', PythonPkgConfigDependency.type_name, arguments=(env, kwargs))) |
| 588 |
else: |
| 589 |
candidates.append(DependencyCandidate.from_dependency( |
| 590 |
'python3', PkgConfigDependency, (env, kwargs))) |
| 591 |
|
| 592 |
if DependencyMethods.SYSTEM in methods: |
| 593 |
# This is a unique log-tried. |
| 594 |
candidates.append(DependencyCandidate( |
| 595 |
functools.partial(PythonSystemDependency, installation=installation), |
| 596 |
'python', 'sysconfig', arguments=(env, kwargs))) |
| 597 |
|
| 598 |
if DependencyMethods.EXTRAFRAMEWORK in methods: |
| 599 |
nkwargs = kwargs.copy() |
| 600 |
if mesonlib.version_compare(installation.version, '>= 3'): |
| 601 |
# There is a python in /System/Library/Frameworks, but that's python 2.x, |
| 602 |
# Python 3 will always be in /Library |
| 603 |
nkwargs['paths'] = ['/Library/Frameworks'] |
| 604 |
candidates.append(DependencyCandidate( |
| 605 |
functools.partial(PythonFrameworkDependency, installation=installation), |
| 606 |
'python', PythonPkgConfigDependency.type_name, arguments=(env, nkwargs))) |
| 607 |
|
| 608 |
return candidates |
| 609 |
|
| 610 |
packages['python3'] = python_factory |
| 611 |
|
| 612 |
packages['pybind11'] = pybind11_factory = DependencyFactory( |
| 613 |
'pybind11', |
| 614 |
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.CMAKE], |
| 615 |
configtool=Pybind11ConfigToolDependency, |
| 616 |
) |
| 617 |
|
| 618 |
packages['numpy'] = numpy_factory = DependencyFactory( |
| 619 |
'numpy', |
| 620 |
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL], |
| 621 |
configtool=NumPyConfigToolDependency, |
| 622 |
) |