Debian Codegraph @

mesonbuild/compilers/mixins/compcert.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2012-2019 The Meson development team
3
4 from __future__ import annotations
5
6 """Representations specific to the CompCert C compiler family."""
7
8 import os
9 import re
10 import typing as T
11
12 if T.TYPE_CHECKING:
13 from ...envconfig import MachineInfo
14 from ...compilers.compilers import Compiler
15 else:
16 # This is a bit clever, for mypy we pretend that these mixins descend from
17 # Compiler, so we get all of the methods and attributes defined for us, but
18 # for runtime we make them descend from object (which all classes normally
19 # do). This gives up DRYer type checking, with no runtime impact
20 Compiler = object
21
22 ccomp_optimization_args: T.Dict[str, T.List[str]] = {
23 'plain': [],
24 '0': ['-O0'],
25 'g': ['-O0'],
26 '1': ['-O1'],
27 '2': ['-O2'],
28 '3': ['-O3'],
29 's': ['-Os']
30 }
31
32 ccomp_debug_args: T.Dict[bool, T.List[str]] = {
33 False: [],
34 True: ['-O0', '-g']
35 }
36
37 # As of CompCert 20.04, these arguments should be passed to the underlying gcc linker (via -WUl,<arg>)
38 # There are probably (many) more, but these are those used by picolibc
39 ccomp_args_to_wul: T.List[str] = [
40 r"^-ffreestanding$",
41 r"^-r$"
42 ]
43
44 class CompCertCompiler(Compiler):
45
46 id = 'ccomp'
47
48 def __init__(self) -> None:
49 # Assembly
50 self.can_compile_suffixes.add('s')
51 self.can_compile_suffixes.add('sx')
52 default_warn_args: T.List[str] = []
53 self.warn_args: T.Dict[str, T.List[str]] = {
54 '0': [],
55 '1': default_warn_args,
56 '2': default_warn_args + [],
57 '3': default_warn_args + [],
58 'everything': default_warn_args + []}
59
60 def get_always_args(self) -> T.List[str]:
61 return []
62
63 def get_pic_args(self) -> T.List[str]:
64 # As of now, CompCert does not support PIC
65 return []
66
67 def get_pch_suffix(self) -> str:
68 return 'pch'
69
70 def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]:
71 return []
72
73 @classmethod
74 def _unix_args_to_native(cls, args: T.List[str], info: MachineInfo) -> T.List[str]:
75 "Always returns a copy that can be independently mutated"
76 patched_args: T.List[str] = []
77 for arg in args:
78 added = 0
79 for ptrn in ccomp_args_to_wul:
80 if re.match(ptrn, arg):
81 patched_args.append('-WUl,' + arg)
82 added = 1
83 if not added:
84 patched_args.append(arg)
85 return patched_args
86
87 def thread_flags(self) -> T.List[str]:
88 return []
89
90 def get_preprocess_only_args(self) -> T.List[str]:
91 return ['-E']
92
93 def get_compile_only_args(self) -> T.List[str]:
94 return ['-c']
95
96 def get_coverage_args(self) -> T.List[str]:
97 return []
98
99 def get_no_stdinc_args(self) -> T.List[str]:
100 return ['-nostdinc']
101
102 def get_no_stdlib_link_args(self) -> T.List[str]:
103 return ['-nostdlib']
104
105 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
106 return ccomp_optimization_args[optimization_level]
107
108 def get_debug_args(self, is_debug: bool) -> T.List[str]:
109 return ccomp_debug_args[is_debug]
110
111 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str], build_dir: str) -> T.List[str]:
112 for idx, i in enumerate(parameter_list):
113 if i[:9] == '-I':
114 parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
115
116 return parameter_list