| 1 |
# SPDX-License-Identifier: Apache-2.0 |
| 2 |
# Copyright © 2021-2025 Intel Corporation |
| 3 |
|
| 4 |
"""Abstraction for Cython language compilers.""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
import os |
| 8 |
import typing as T |
| 9 |
|
| 10 |
from .. import options |
| 11 |
from .. import mlog |
| 12 |
from ..mesonlib import version_compare, EnvironmentException |
| 13 |
from .compilers import CompileCheckMode, Compiler |
| 14 |
|
| 15 |
if T.TYPE_CHECKING: |
| 16 |
from ..options import MutableKeyedOptionDictType |
| 17 |
from ..build import BuildTarget |
| 18 |
|
| 19 |
|
| 20 |
class CythonCompiler(Compiler): |
| 21 |
|
| 22 |
"""Cython Compiler.""" |
| 23 |
|
| 24 |
language = 'cython' |
| 25 |
id = 'cython' |
| 26 |
|
| 27 |
def needs_static_linker(self) -> bool: |
| 28 |
# We transpile into C, so we don't need any linker |
| 29 |
return False |
| 30 |
|
| 31 |
def get_always_args(self) -> T.List[str]: |
| 32 |
return ['--fast-fail'] |
| 33 |
|
| 34 |
def get_werror_args(self) -> T.List[str]: |
| 35 |
return ['-Werror'] |
| 36 |
|
| 37 |
def get_output_args(self, outputname: str) -> T.List[str]: |
| 38 |
return ['-o', outputname] |
| 39 |
|
| 40 |
def get_optimization_args(self, optimization_level: str) -> T.List[str]: |
| 41 |
# Cython doesn't have optimization levels itself, the underlying |
| 42 |
# compiler might though |
| 43 |
return [] |
| 44 |
|
| 45 |
def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]: |
| 46 |
if version_compare(self.version, '>=0.29.33'): |
| 47 |
return ['-M'] |
| 48 |
return [] |
| 49 |
|
| 50 |
def get_depfile_suffix(self) -> str: |
| 51 |
return 'dep' |
| 52 |
|
| 53 |
def get_pic_args(self) -> T.List[str]: |
| 54 |
# We can lie here, it's fine |
| 55 |
return [] |
| 56 |
|
| 57 |
def _sanity_check_filenames(self) -> T.Tuple[str, T.Optional[str], str]: |
| 58 |
sourcename, _, binname = super()._sanity_check_filenames() |
| 59 |
|
| 60 |
lang = self.get_compileropt_value('language', None) |
| 61 |
assert isinstance(lang, str) |
| 62 |
|
| 63 |
# This is almost certainly not good enough |
| 64 |
ext = 'dll' if self.environment.machines[self.for_machine].is_windows() else 'so' |
| 65 |
|
| 66 |
return (sourcename, f'{os.path.splitext(sourcename)[0]}.{lang}', |
| 67 |
f'{os.path.splitext(binname)[0]}.{ext}') |
| 68 |
|
| 69 |
def _transpiled_sanity_check_compile_args( |
| 70 |
self, compiler: Compiler, sourcename: str, binname: str |
| 71 |
) -> T.Tuple[T.List[str], T.List[str]]: |
| 72 |
version = self.get_compileropt_value('version', None) |
| 73 |
assert isinstance(version, str) |
| 74 |
|
| 75 |
from ..dependencies import find_external_dependency |
| 76 |
with mlog.no_logging(): |
| 77 |
dep = find_external_dependency( |
| 78 |
f'python{version}', self.environment, {'required': False, 'native': self.for_machine}) |
| 79 |
if not dep.found(): |
| 80 |
raise EnvironmentException( |
| 81 |
'Cython requires python3 dependency for link testing, but it could not be found') |
| 82 |
|
| 83 |
args, largs = super()._transpiled_sanity_check_compile_args(compiler, sourcename, binname) |
| 84 |
args.extend(compiler.get_pic_args()) |
| 85 |
args.extend(dep.get_all_compile_args()) |
| 86 |
|
| 87 |
largs.extend(dep.get_all_link_args()) |
| 88 |
largs.extend(compiler.get_std_shared_lib_link_args()) |
| 89 |
largs.extend(compiler.get_allow_undefined_link_args()) |
| 90 |
return args, largs |
| 91 |
|
| 92 |
def _sanity_check_mode(self) -> CompileCheckMode: |
| 93 |
# the sanity check only transpiles, the generated source is compiled |
| 94 |
# and linked by _transpiled_sanity_check_compile_args() |
| 95 |
return CompileCheckMode.COMPILE |
| 96 |
|
| 97 |
def _sanity_check_compile_args(self, sourcename: str, binname: str |
| 98 |
) -> T.Tuple[T.List[str], T.List[str]]: |
| 99 |
args, largs = super()._sanity_check_compile_args(sourcename, binname) |
| 100 |
args.extend(self.get_option_compile_args(None)) |
| 101 |
return args, largs |
| 102 |
|
| 103 |
def _sanity_check_source_code(self) -> str: |
| 104 |
return 'def func():\n print("Hello world")' |
| 105 |
|
| 106 |
def _run_sanity_check(self, cmdlist: T.List[str], work_dir: str) -> None: |
| 107 |
# XXX: this is a punt |
| 108 |
# This means we transpile the Cython .pyx file into C or C++, and we |
| 109 |
# link it, but we don't actually attempt to run it. |
| 110 |
return |
| 111 |
|
| 112 |
def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], |
| 113 |
build_dir: str) -> T.List[str]: |
| 114 |
new: T.List[str] = [] |
| 115 |
for i in parameter_list: |
| 116 |
new.append(i) |
| 117 |
|
| 118 |
return new |
| 119 |
|
| 120 |
def get_options(self) -> 'MutableKeyedOptionDictType': |
| 121 |
opts = super().get_options() |
| 122 |
|
| 123 |
key = self.form_compileropt_key('version') |
| 124 |
opts[key] = options.UserComboOption( |
| 125 |
self.make_option_name(key), |
| 126 |
'Python version to target', |
| 127 |
'3', |
| 128 |
choices=['2', '3']) |
| 129 |
|
| 130 |
key = self.form_compileropt_key('language') |
| 131 |
opts[key] = options.UserComboOption( |
| 132 |
self.make_option_name(key), |
| 133 |
'Output C or C++ files', |
| 134 |
'c', |
| 135 |
choices=['c', 'cpp']) |
| 136 |
|
| 137 |
return opts |
| 138 |
|
| 139 |
def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]: |
| 140 |
args: T.List[str] = [] |
| 141 |
version = self.get_compileropt_value('version', target, subproject) |
| 142 |
assert isinstance(version, str) |
| 143 |
args.append(f'-{version}') |
| 144 |
|
| 145 |
lang = self.get_compileropt_value('language', target, subproject) |
| 146 |
assert isinstance(lang, str) |
| 147 |
if lang == 'cpp': |
| 148 |
args.append('--cplus') |
| 149 |
return args |