Debian Codegraph @

mesonbuild/compilers/cython.py

raw
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 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_compile_args(self, sourcename: str, binname: str
93 ) -> T.Tuple[T.List[str], T.List[str]]:
94 args, largs = super()._sanity_check_compile_args(sourcename, binname)
95 args.extend(self.get_option_compile_args(None))
96 return args, largs
97
98 def _sanity_check_source_code(self) -> str:
99 return 'def func():\n print("Hello world")'
100
101 def _run_sanity_check(self, cmdlist: T.List[str], work_dir: str) -> None:
102 # XXX: this is a punt
103 # This means we transpile the Cython .pyx file into C or C++, and we
104 # link it, but we don't actually attempt to run it.
105 return
106
107 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
108 build_dir: str) -> T.List[str]:
109 new: T.List[str] = []
110 for i in parameter_list:
111 new.append(i)
112
113 return new
114
115 def get_options(self) -> 'MutableKeyedOptionDictType':
116 opts = super().get_options()
117
118 key = self.form_compileropt_key('version')
119 opts[key] = options.UserComboOption(
120 self.make_option_name(key),
121 'Python version to target',
122 '3',
123 choices=['2', '3'])
124
125 key = self.form_compileropt_key('language')
126 opts[key] = options.UserComboOption(
127 self.make_option_name(key),
128 'Output C or C++ files',
129 'c',
130 choices=['c', 'cpp'])
131
132 return opts
133
134 def get_option_compile_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]:
135 args: T.List[str] = []
136 version = self.get_compileropt_value('version', target, subproject)
137 assert isinstance(version, str)
138 args.append(f'-{version}')
139
140 lang = self.get_compileropt_value('language', target, subproject)
141 assert isinstance(lang, str)
142 if lang == 'cpp':
143 args.append('--cplus')
144 return args