| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright 2016-2017 The Meson development team |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
import sysconfig |
| 7 |
import typing as T |
| 8 |
|
| 9 |
from .. import mesonlib |
| 10 |
from . import ExtensionModule, ModuleInfo |
| 11 |
from ..build import ( |
| 12 |
BuildTarget, CustomTarget, CustomTargetIndex, ExtractedObjects, |
| 13 |
GeneratedList, SharedModule, StructuredSources |
| 14 |
) |
| 15 |
from ..interpreter.type_checking import SHARED_MOD_KWS |
| 16 |
from ..interpreterbase import typed_kwargs, typed_pos_args, noPosargs, noKwargs |
| 17 |
from ..programs import ExternalProgram |
| 18 |
|
| 19 |
if T.TYPE_CHECKING: |
| 20 |
from . import ModuleState |
| 21 |
from ..interpreter.interpreter import BuildTargetSource, Interpreter |
| 22 |
from ..interpreter.kwargs import SharedModule as SharedModuleKW |
| 23 |
from ..interpreterbase import TYPE_var, TYPE_kwargs |
| 24 |
from ..interpreter.type_checking import SourcesVarargsType |
| 25 |
|
| 26 |
|
| 27 |
_MOD_KWARGS = [k for k in SHARED_MOD_KWS if k.name not in {'name_prefix', 'name_suffix'}] |
| 28 |
|
| 29 |
|
| 30 |
class Python3Module(ExtensionModule): |
| 31 |
|
| 32 |
INFO = ModuleInfo('python3', '0.38.0', deprecated='0.48.0') |
| 33 |
|
| 34 |
def __init__(self, interp: Interpreter): |
| 35 |
super().__init__(interp) |
| 36 |
self.methods.update({ |
| 37 |
'extension_module': self.extension_module, |
| 38 |
'find_python': self.find_python, |
| 39 |
'language_version': self.language_version, |
| 40 |
'sysconfig_path': self.sysconfig_path, |
| 41 |
}) |
| 42 |
|
| 43 |
@typed_pos_args('python3.extension_module', str, varargs=(str, mesonlib.File, CustomTarget, CustomTargetIndex, GeneratedList, StructuredSources, ExtractedObjects, BuildTarget)) |
| 44 |
@typed_kwargs('python3.extension_module', *_MOD_KWARGS) |
| 45 |
def extension_module(self, state: ModuleState, args: T.Tuple[str, T.List[BuildTargetSource]], kwargs: SharedModuleKW) -> SharedModule: |
| 46 |
host_system = state.environment.machines.host.system |
| 47 |
if host_system == 'darwin': |
| 48 |
# Default suffix is 'dylib' but Python does not use it for extensions. |
| 49 |
suffix = 'so' |
| 50 |
elif host_system == 'windows': |
| 51 |
# On Windows the extension is pyd for some unexplainable reason. |
| 52 |
suffix = 'pyd' |
| 53 |
else: |
| 54 |
suffix = None |
| 55 |
kwargs['name_prefix'] = '' |
| 56 |
kwargs['name_suffix'] = suffix |
| 57 |
m = self.interpreter.build_target( |
| 58 |
state.current_node, T.cast('T.Tuple[str, SourcesVarargsType]', args), kwargs, SharedModule) |
| 59 |
assert isinstance(m, SharedModule), 'for mypy' |
| 60 |
return m |
| 61 |
|
| 62 |
@noPosargs |
| 63 |
@noKwargs |
| 64 |
def find_python(self, state: ModuleState, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> ExternalProgram: |
| 65 |
command = state.environment.lookup_binary_entry(mesonlib.MachineChoice.HOST, 'python3') |
| 66 |
if command is not None: |
| 67 |
py3 = ExternalProgram.from_entry('python3', command) |
| 68 |
else: |
| 69 |
py3 = ExternalProgram('python3', mesonlib.python_command, silent=True) |
| 70 |
return py3 |
| 71 |
|
| 72 |
@noPosargs |
| 73 |
@noKwargs |
| 74 |
def language_version(self, state: ModuleState, args: T.List[TYPE_var], kwargs: TYPE_kwargs) -> str: |
| 75 |
return sysconfig.get_python_version() |
| 76 |
|
| 77 |
@noKwargs |
| 78 |
@typed_pos_args('python3.sysconfig_path', str) |
| 79 |
def sysconfig_path(self, state: ModuleState, args: T.Tuple[str], kwargs: TYPE_kwargs) -> str: |
| 80 |
path_name = args[0] |
| 81 |
valid_names = sysconfig.get_path_names() |
| 82 |
if path_name not in valid_names: |
| 83 |
raise mesonlib.MesonException(f'{path_name} is not a valid path name {valid_names}.') |
| 84 |
|
| 85 |
# Get a relative path without a prefix, e.g. lib/python3.6/site-packages |
| 86 |
return sysconfig.get_path(path_name, vars={'base': '', 'platbase': '', 'installed_base': ''})[1:] |
| 87 |
|
| 88 |
|
| 89 |
def initialize(interp: Interpreter) -> Python3Module: |
| 90 |
return Python3Module(interp) |