Debian Codegraph @

mesonbuild/compilers/vala.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2012-2017 The Meson development team
3
4 from __future__ import annotations
5
6 import os.path
7 import typing as T
8
9 from .. import mlog
10 from .. import mesonlib
11 from ..mesonlib import version_compare, LibType
12 from ..options import OptionKey
13 from .compilers import CompileCheckMode, Compiler
14
15 if T.TYPE_CHECKING:
16 from ..arglist import CompilerArgs
17 from ..environment import Environment
18 from ..mesonlib import MachineChoice
19 from ..dependencies import Dependency
20 from ..build import BuildTarget
21
22 class ValaCompiler(Compiler):
23
24 language = 'vala'
25 id = 'valac'
26
27 def __init__(self, exelist: T.List[str], version: str, for_machine: MachineChoice,
28 environment: Environment):
29 super().__init__([], exelist, version, for_machine, environment)
30 self.version = version
31 self.base_options = {OptionKey('b_colorout')}
32 self.force_link = False
33 self._has_color_support = version_compare(self.version, '>=0.37.1')
34 self._has_posix_profile = version_compare(self.version, '>= 0.44')
35
36 def needs_static_linker(self) -> bool:
37 return False # Because compiles into C.
38
39 def get_optimization_args(self, optimization_level: str) -> T.List[str]:
40 return []
41
42 def get_dependency_gen_args(self, outtarget: str, outfile: str) -> T.List[str]:
43 if version_compare(self.version, '>=0.47.2'):
44 return ['--depfile', outfile]
45 return []
46
47 def get_depfile_suffix(self) -> str:
48 return 'depfile'
49
50 def get_debug_args(self, is_debug: bool) -> T.List[str]:
51 return ['--debug'] if is_debug else []
52
53 def get_output_args(self, outputname: str) -> T.List[str]:
54 return [] # Because compiles into C.
55
56 def get_compile_only_args(self) -> T.List[str]:
57 return [] # Because compiles into C.
58
59 def get_compiler_args_for_mode(self, mode: CompileCheckMode) -> T.List[str]:
60 args: T.List[str] = []
61 if mode is CompileCheckMode.LINK and self.force_link:
62 return args
63 args += self.get_always_args()
64 if mode is CompileCheckMode.COMPILE:
65 args += self.get_compile_only_args()
66 elif mode is CompileCheckMode.PREPROCESS:
67 args += self.get_preprocess_only_args()
68 return args
69
70 def get_preprocess_only_args(self) -> T.List[str]:
71 return []
72
73 def get_pic_args(self) -> T.List[str]:
74 return []
75
76 def get_pie_args(self) -> T.List[str]:
77 return []
78
79 def get_pie_link_args(self) -> T.List[str]:
80 return []
81
82 def get_always_args(self) -> T.List[str]:
83 return ['-C']
84
85 def get_warn_args(self, level: str) -> T.List[str]:
86 return []
87
88 def get_werror_args(self) -> T.List[str]:
89 return ['--fatal-warnings']
90
91 def get_colorout_args(self, colortype: str) -> T.List[str]:
92 if self._has_color_support:
93 return ['--color=' + colortype]
94 return []
95
96 def compute_parameters_with_absolute_paths(self, parameter_list: T.List[str],
97 build_dir: str) -> T.List[str]:
98 for idx, i in enumerate(parameter_list):
99 if i[:9] == '--girdir=':
100 parameter_list[idx] = i[:9] + os.path.normpath(os.path.join(build_dir, i[9:]))
101 if i[:10] == '--vapidir=':
102 parameter_list[idx] = i[:10] + os.path.normpath(os.path.join(build_dir, i[10:]))
103 if i[:13] == '--includedir=':
104 parameter_list[idx] = i[:13] + os.path.normpath(os.path.join(build_dir, i[13:]))
105 if i[:14] == '--metadatadir=':
106 parameter_list[idx] = i[:14] + os.path.normpath(os.path.join(build_dir, i[14:]))
107
108 return parameter_list
109
110 def _sanity_check_source_code(self) -> str:
111 return 'public static int main() { return 0; }'
112
113 def _sanity_check_mode(self) -> CompileCheckMode:
114 # the sanity check only transpiles, the generated source is compiled
115 # and linked by _transpiled_sanity_check_compile_args()
116 return CompileCheckMode.COMPILE
117
118 def _sanity_check_compile_args(self, sourcename: str, binname: str
119 ) -> T.Tuple[T.List[str], T.List[str]]:
120 args, largs = super()._sanity_check_compile_args(sourcename, binname)
121 if self._has_posix_profile:
122 # This removes the glib requirement. Posix and libc are equivalent,
123 # but posix is available in older versions of valac
124 args.append('--profile=posix')
125 return args, largs
126
127 def _transpiled_sanity_check_compile_args(
128 self, compiler: Compiler, sourcename: str, binname: str
129 ) -> T.Tuple[T.List[str], T.List[str]]:
130 args, largs = super()._transpiled_sanity_check_compile_args(compiler, sourcename, binname)
131 if self._has_posix_profile:
132 return args, largs
133
134 # If valac is too old for the posix profile then we need to find goobject-2.0 for linking.
135 from ..dependencies import find_external_dependency
136 with mlog.no_logging():
137 dep = find_external_dependency('gobject-2.0', self.environment,
138 {'required': False, 'native': self.for_machine})
139 if not dep.found():
140 raise mesonlib.EnvironmentException(
141 'Valac < 0.44 requires gobject-2.0 for link testing, bit it could not be found.')
142
143 args.extend(dep.get_all_compile_args())
144 largs.extend(dep.get_all_link_args())
145 return args, largs
146
147 def _sanity_check_filenames(self) -> T.Tuple[str, T.Optional[str], str]:
148 sourcename, _, binname = super()._sanity_check_filenames()
149 return sourcename, f'{os.path.splitext(sourcename)[0]}.c', binname
150
151 def find_library(self, libname: str, extra_dirs: T.List[str], libtype: LibType = LibType.PREFER_SHARED,
152 lib_prefix_warning: bool = True, ignore_system_dirs: bool = False,
153 skip_link_check: bool = False) -> T.Optional[T.List[str]]:
154 if extra_dirs and isinstance(extra_dirs, str):
155 extra_dirs = [extra_dirs]
156 # Valac always looks in the default vapi dir, so only search there if
157 # no extra dirs are specified.
158 if not extra_dirs:
159 code = 'class MesonFindLibrary : Object { }'
160 args: T.List[str] = self.get_external_compile_args()
161 vapi_args = ['--pkg', libname]
162 args += vapi_args
163 with self.cached_compile(code, extra_args=args, mode=CompileCheckMode.COMPILE) as p:
164 if p.returncode == 0:
165 return vapi_args
166 # Not found? Try to find the vapi file itself.
167 for d in extra_dirs:
168 vapi = os.path.join(d, libname + '.vapi')
169 if os.path.isfile(vapi):
170 return [vapi]
171 mlog.debug(f'Searched {extra_dirs!r} and {libname!r} wasn\'t found')
172 return None
173
174 def thread_flags(self) -> T.List[str]:
175 return []
176
177 def thread_link_flags(self) -> T.List[str]:
178 return []
179
180 def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]:
181 return []
182
183 def build_wrapper_args(self,
184 extra_args: T.Union[None, CompilerArgs, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]],
185 dependencies: T.Optional[T.List['Dependency']],
186 mode: CompileCheckMode = CompileCheckMode.COMPILE) -> CompilerArgs:
187 if callable(extra_args):
188 extra_args = extra_args(mode)
189 if extra_args is None:
190 extra_args = []
191 if dependencies is None:
192 dependencies = []
193
194 # Collect compiler arguments
195 args = self.compiler_args(self.get_compiler_check_args(mode))
196 for d in dependencies:
197 # Add compile flags needed by dependencies
198 if mode is CompileCheckMode.LINK and self.force_link:
199 # As we are passing the parameter to valac we don't need the dependent libraries.
200 a = d.get_compile_args()
201 if a:
202 p = a[0]
203 n = p[max(p.rfind('/'), p.rfind('\\'))+1:]
204 if not n == d.get_name():
205 args += ['--pkg=' + d.get_name()] # This is used by gio-2.0 among others.
206 else:
207 args += ['--pkg=' + n]
208 else:
209 args += ['--Xcc=-l' + d.get_name()] # This is used by the maths library(-lm) among others.
210 else:
211 args += d.get_compile_args()
212 if mode is CompileCheckMode.LINK:
213 # Add link flags needed to find dependencies
214 if not self.force_link: # There are no need for link dependencies when linking with valac.
215 args += d.get_link_args()
216
217 if mode is CompileCheckMode.COMPILE:
218 # Add DFLAGS from the env
219 args += self.get_external_compile_args()
220 elif mode is CompileCheckMode.LINK:
221 # Add LDFLAGS from the env
222 args += self.get_external_link_args()
223 # extra_args must override all other arguments, so we add them last
224 args += extra_args
225 return args
226
227 def links(self, code: 'mesonlib.FileOrString', *,
228 compiler: T.Optional['Compiler'] = None,
229 extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None,
230 dependencies: T.Optional[T.List['Dependency']] = None,
231 disable_cache: bool = False) -> T.Tuple[bool, bool]:
232 self.force_link = True
233 if compiler:
234 with compiler._build_wrapper(code, dependencies=dependencies, want_output=True) as r:
235 objfile = mesonlib.File.from_absolute_file(r.output_name)
236 result = self.compiles(objfile, extra_args=extra_args,
237 dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=True)
238 self.force_link = False
239 return result
240 result = self.compiles(code, extra_args=extra_args,
241 dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=disable_cache)
242 self.force_link = False
243 return result