Debian Codegraph @

mesonbuild/compilers/asm.py

raw
1 from __future__ import annotations
2
3 import os
4 import typing as T
5
6 from ..mesonlib import EnvironmentException, get_meson_command
7 from ..options import OptionKey
8 from .compilers import Compiler
9 from ..linkers import RSPFileSyntax
10 from ..linkers.linkers import VisualStudioLikeLinkerMixin
11 from .mixins.metrowerks import MetrowerksCompiler, mwasmarm_instruction_set_args, mwasmeppc_instruction_set_args
12 from .mixins.ti import TICompiler
13
14 if T.TYPE_CHECKING:
15 from ..environment import Environment
16 from ..linkers.linkers import DynamicLinker
17 from ..mesonlib import MachineChoice
18
19 nasm_optimization_args: T.Dict[str, T.List[str]] = {
20 'plain': [],
21 '0': ['-O0'],
22 'g': ['-O0'],
23 '1': ['-O1'],
24 '2': ['-Ox'],
25 '3': ['-Ox'],
26 's': ['-Ox'],
27 }
28
29
30 class ASMCompiler(Compiler):
31
32 """Shared base class for all ASM Compilers (Assemblers)"""
33
34 _SUPPORTED_ARCHES: T.Set[str] = set()
35
36 def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str,
37 for_machine: MachineChoice, env: Environment,
38 linker: T.Optional[DynamicLinker] = None,
39 full_version: T.Optional[str] = None):
40 info = env.machines[for_machine]
41 if self._SUPPORTED_ARCHES and info.cpu_family not in self._SUPPORTED_ARCHES:
42 raise EnvironmentException(f'ASM Compiler {self.id} does not support building for {info.cpu_family} CPU family.')
43 super().__init__(ccache, exelist, version, for_machine, env, linker, full_version)
44
45 def sanity_check(self, work_dir: str) -> None:
46 return None
47
48 def _sanity_check_source_code(self) -> str:
49 # TODO: Stub implementation to be replaced in future patch
50 return ''
51
52
53 class NasmCompiler(ASMCompiler):
54 language = 'nasm'
55 id = 'nasm'
56
57 # https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features
58 crt_args: T.Dict[str, T.List[str]] = {
59 'none': [],
60 'md': ['/DEFAULTLIB:ucrt.lib', '/DEFAULTLIB:vcruntime.lib', '/DEFAULTLIB:msvcrt.lib'],
61 'mdd': ['/DEFAULTLIB:ucrtd.lib', '/DEFAULTLIB:vcruntimed.lib', '/DEFAULTLIB:msvcrtd.lib'],
62 'mt': ['/DEFAULTLIB:libucrt.lib', '/DEFAULTLIB:libvcruntime.lib', '/DEFAULTLIB:libcmt.lib'],
63 'mtd': ['/DEFAULTLIB:libucrtd.lib', '/DEFAULTLIB:libvcruntimed.lib', '/DEFAULTLIB:libcmtd.lib'],
64 }
65
66 _SUPPORTED_ARCHES = {'x86', 'x86_64'}
67
68 def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str,
69 for_machine: 'MachineChoice', env: Environment,
70 linker: T.Optional['DynamicLinker'] = None,
71 full_version: T.Optional[str] = None):
72 super().__init__(ccache, exelist, version, for_machine, env, linker, full_version)
73 if isinstance(self.linker, VisualStudioLikeLinkerMixin):
74 self.base_options.add(OptionKey('b_vscrt'))
75
76 def needs_static_linker(self) -> bool:
77 return True
78
79 def get_always_args(self) -> T.List[str]:
80 if self.info.is_64_bit:
81 if self.info.cpu == 'x32':
82 cpu = 'x32'
83 else:
84 cpu = '64'
85 else:
86 cpu = '32'
87 if self.info.is_windows() or self.info.is_cygwin():
88 plat = 'win'
89 define = f'WIN{cpu}'
90 elif self.info.is_darwin():
91 plat = 'macho'
92 define = 'MACHO'
93 elif self.info.is_os2():
94 cpu = ''
95 if self.environment.coredata.optstore.get_value_for(OptionKey('os2_emxomf')):
96 plat = 'obj2'
97 define = 'OBJ2'
98 else:
99 plat = 'aout'
100 define = 'AOUT'
101 else:
102 plat = 'elf'
103 define = 'ELF'
104 args = ['-f', f'{plat}{cpu}', f'-D{define}']
105 if self.info.is_64_bit:
106 args.append('-D__x86_64__')
107 return args
108
109 def get_werror_args(self) -> T.List[str]:
110 return ['-Werror']
111
112 def get_output_args(self, outputname: str) -> T.List[str]:
113 return ['-o', outputname]
114
115 def unix_args_to_native(self, args: T.List[str]) -> T.List[str]:
116 outargs: T.List[str] = []
117 for arg in args:
118 if arg in {'-mms-bitfields', '-pthread'}:
119 continue
120 outargs.append(arg)
121 return outargs
122
123 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
124 return nasm_optimization_args[optimization_level]
125
126 def get_debug_args(self, is_debug: bool) -> T.List[str]:
127 if is_debug:
128 return ['-g']
129 return []
130
131 def get_depfile_suffix(self) -> str:
132 return 'd'
133
134 def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
135 return ['-MD', outfile, '-MQ', outtarget]
136
137 def get_pic_args(self) -> T.List[str]:
138 return []
139
140 def get_include_args(self, path: str, is_system: bool) -> T.List[str]:
141 if not path:
142 path = '.'
143 return ['-I' + path]
144
145 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
146 build_dir: str) -> T.List[str]:
147 for idx, i in enumerate(parameter_list):
148 if i[:2] == '-I':
149 parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:]))
150 return parameter_list
151
152 def get_crt_compile_args(self, crt_val: str) -> T.List[str]:
153 return []
154
155 # Linking ASM-only objects into an executable or DLL
156 # require this, otherwise it'll fail to find
157 # _WinMain or _DllMainCRTStartup.
158 def get_crt_link_args(self, crt_val: str) -> T.List[str]:
159 if not isinstance(self.linker, VisualStudioLikeLinkerMixin):
160 return []
161 return self.crt_args[self.get_crt_val(crt_val)]
162
163 def rsp_file_syntax(self) -> RSPFileSyntax:
164 return RSPFileSyntax.NASM
165
166 class YasmCompiler(NasmCompiler):
167 id = 'yasm'
168
169 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
170 # Yasm is incompatible with Nasm optimization flags.
171 return []
172
173 def get_exelist(self, ccache: bool = True) -> T.List[str]:
174 # Wrap yasm executable with an internal script that will write depfile.
175 exelist = super().get_exelist(ccache)
176 return get_meson_command() + ['--internal', 'yasm'] + exelist
177
178 def get_debug_args(self, is_debug: bool) -> T.List[str]:
179 if is_debug:
180 if isinstance(self.linker, VisualStudioLikeLinkerMixin):
181 return ['-g', 'cv8']
182 elif self.info.is_darwin():
183 return ['-g', 'null']
184 else:
185 return ['-g', 'dwarf2']
186 return []
187
188 def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
189 return ['--depfile', outfile]
190
191 # https://learn.microsoft.com/en-us/cpp/assembler/masm/ml-and-ml64-command-line-reference
192 class MasmCompiler(ASMCompiler):
193 language = 'masm'
194 id = 'ml'
195
196 _SUPPORTED_ARCHES = {'x86', 'x86_64'}
197
198 def get_compile_only_args(self) -> T.List[str]:
199 return ['/c']
200
201 @staticmethod
202 def get_argument_syntax() -> str:
203 return 'msvc'
204
205 def needs_static_linker(self) -> bool:
206 return True
207
208 def get_always_args(self) -> T.List[str]:
209 return ['/nologo']
210
211 def get_werror_args(self) -> T.List[str]:
212 return ['/WX']
213
214 def get_output_args(self, outputname: str) -> T.List[str]:
215 return ['/Fo', outputname]
216
217 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
218 return []
219
220 def get_debug_args(self, is_debug: bool) -> T.List[str]:
221 if is_debug:
222 return ['/Zi']
223 return []
224
225 def get_pic_args(self) -> T.List[str]:
226 return []
227
228 def get_include_args(self, path: str, is_system: bool) -> T.List[str]:
229 if not path:
230 path = '.'
231 return ['-I' + path]
232
233 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
234 build_dir: str) -> T.List[str]:
235 for idx, i in enumerate(parameter_list):
236 if i[:2] == '-I' or i[:2] == '/I':
237 parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:]))
238 return parameter_list
239
240 def get_crt_compile_args(self, crt_val: str) -> T.List[str]:
241 return []
242
243 def depfile_for_object(self, objfile: str) -> T.Optional[str]:
244 return None
245
246
247 # https://learn.microsoft.com/en-us/cpp/assembler/arm/arm-assembler-command-line-reference
248 class MasmARMCompiler(ASMCompiler):
249 language = 'masm'
250 id = 'armasm'
251 _SUPPORTED_ARCHES = {'arm', 'aarch64'}
252
253 def needs_static_linker(self) -> bool:
254 return True
255
256 def get_always_args(self) -> T.List[str]:
257 return ['-nologo']
258
259 def get_werror_args(self) -> T.List[str]:
260 return []
261
262 def get_output_args(self, outputname: str) -> T.List[str]:
263 return ['-o', outputname]
264
265 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
266 return []
267
268 def get_debug_args(self, is_debug: bool) -> T.List[str]:
269 if is_debug:
270 return ['-g']
271 return []
272
273 def get_pic_args(self) -> T.List[str]:
274 return []
275
276 def get_include_args(self, path: str, is_system: bool) -> T.List[str]:
277 if not path:
278 path = '.'
279 return ['-i' + path]
280
281 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
282 build_dir: str) -> T.List[str]:
283 for idx, i in enumerate(parameter_list):
284 if i[:2] == '-I':
285 parameter_list[idx] = i[:2] + os.path.normpath(os.path.join(build_dir, i[2:]))
286 return parameter_list
287
288 def get_crt_compile_args(self, crt_val: str) -> T.List[str]:
289 return []
290
291 def get_depfile_format(self) -> str:
292 return 'msvc'
293
294 def depfile_for_object(self, objfile: str) -> T.Optional[str]:
295 return None
296
297
298 # https://downloads.ti.com/docs/esd/SPRUI04/
299 class TILinearAsmCompiler(TICompiler, ASMCompiler):
300 language = 'linearasm'
301 _SUPPORTED_ARCHES = {'c6000'}
302
303 def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str,
304 for_machine: MachineChoice, env: Environment,
305 linker: T.Optional[DynamicLinker] = None,
306 full_version: T.Optional[str] = None):
307 ASMCompiler.__init__(self, ccache, exelist, version, for_machine, env, linker, full_version)
308 TICompiler.__init__(self)
309
310 def needs_static_linker(self) -> bool:
311 return True
312
313 def get_always_args(self) -> T.List[str]:
314 return []
315
316 def get_crt_compile_args(self, crt_val: str) -> T.List[str]:
317 return []
318
319 def get_depfile_suffix(self) -> str:
320 return 'd'
321
322
323 class MetrowerksAsmCompiler(MetrowerksCompiler, ASMCompiler):
324 language = 'nasm'
325
326 def __init__(self, ccache: T.List[str], exelist: T.List[str], version: str,
327 for_machine: 'MachineChoice', env: Environment,
328 linker: T.Optional['DynamicLinker'] = None,
329 full_version: T.Optional[str] = None):
330 ASMCompiler.__init__(self, ccache, exelist, version, for_machine, env, linker, full_version)
331 MetrowerksCompiler.__init__(self)
332
333 self.warn_args: T.Dict[str, T.List[str]] = {
334 '0': [],
335 '1': [],
336 '2': [],
337 '3': [],
338 'everything': []}
339 self.can_compile_suffixes.add('s')
340
341 def get_crt_compile_args(self, crt_val: str) -> T.List[str]:
342 return []
343
344 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
345 return []
346
347 def get_pic_args(self) -> T.List[str]:
348 return []
349
350 def needs_static_linker(self) -> bool:
351 return True
352
353
354 class MetrowerksAsmCompilerARM(MetrowerksAsmCompiler):
355 id = 'mwasmarm'
356 _SUPPORTED_ARCHES = {'arm'}
357
358 def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[str]]:
359 return mwasmarm_instruction_set_args.get(instruction_set, None)
360
361
362 class MetrowerksAsmCompilerEmbeddedPowerPC(MetrowerksAsmCompiler):
363 id = 'mwasmeppc'
364 _SUPPORTED_ARCHES = {'ppc'}
365
366 def get_instruction_set_args(self, instruction_set: str) -> T.Optional[T.List[str]]:
367 return mwasmeppc_instruction_set_args.get(instruction_set, None)