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_compile_args(self, sourcename: str, binname: str
114 ) -> T.Tuple[T.List[str], T.List[str]]:
115 args, largs = super()._sanity_check_compile_args(sourcename, binname)
116 if self._has_posix_profile:
117 # This removes the glib requirement. Posix and libc are equivalent,
118 # but posix is available in older versions of valac
119 args.append('--profile=posix')
120 return args, largs
121
122 def _transpiled_sanity_check_compile_args(
123 self, compiler: Compiler, sourcename: str, binname: str
124 ) -> T.Tuple[T.List[str], T.List[str]]:
125 args, largs = super()._transpiled_sanity_check_compile_args(compiler, sourcename, binname)
126 if self._has_posix_profile:
127 return args, largs
128
129 # If valac is too old for the posix profile then we need to find goobject-2.0 for linking.
130 from ..dependencies import find_external_dependency
131 with mlog.no_logging():
132 dep = find_external_dependency('gobject-2.0', self.environment,
133 {'required': False, 'native': self.for_machine})
134 if not dep.found():
135 raise mesonlib.EnvironmentException(
136 'Valac < 0.44 requires gobject-2.0 for link testing, bit it could not be found.')
137
138 args.extend(dep.get_all_compile_args())
139 largs.extend(dep.get_all_link_args())
140 return args, largs
141
142 def _sanity_check_filenames(self) -> T.Tuple[str, T.Optional[str], str]:
143 sourcename, _, binname = super()._sanity_check_filenames()
144 return sourcename, f'{os.path.splitext(sourcename)[0]}.c', binname
145
146 def find_library(self, libname: str, extra_dirs: T.List[str], libtype: LibType = LibType.PREFER_SHARED,
147 lib_prefix_warning: bool = True, ignore_system_dirs: bool = False,
148 skip_link_check: bool = False) -> T.Optional[T.List[str]]:
149 if extra_dirs and isinstance(extra_dirs, str):
150 extra_dirs = [extra_dirs]
151 # Valac always looks in the default vapi dir, so only search there if
152 # no extra dirs are specified.
153 if not extra_dirs:
154 code = 'class MesonFindLibrary : Object { }'
155 args: T.List[str] = []
156 args += self.environment.coredata.get_external_args(self.for_machine, self.language)
157 vapi_args = ['--pkg', libname]
158 args += vapi_args
159 with self.cached_compile(code, extra_args=args, mode=CompileCheckMode.COMPILE) as p:
160 if p.returncode == 0:
161 return vapi_args
162 # Not found? Try to find the vapi file itself.
163 for d in extra_dirs:
164 vapi = os.path.join(d, libname + '.vapi')
165 if os.path.isfile(vapi):
166 return [vapi]
167 mlog.debug(f'Searched {extra_dirs!r} and {libname!r} wasn\'t found')
168 return None
169
170 def thread_flags(self) -> T.List[str]:
171 return []
172
173 def thread_link_flags(self) -> T.List[str]:
174 return []
175
176 def get_option_link_args(self, target: 'BuildTarget', subproject: T.Optional[str] = None) -> T.List[str]:
177 return []
178
179 def build_wrapper_args(self,
180 extra_args: T.Union[None, CompilerArgs, T.List[str], T.Callable[[CompileCheckMode], T.List[str]]],
181 dependencies: T.Optional[T.List['Dependency']],
182 mode: CompileCheckMode = CompileCheckMode.COMPILE) -> CompilerArgs:
183 if callable(extra_args):
184 extra_args = extra_args(mode)
185 if extra_args is None:
186 extra_args = []
187 if dependencies is None:
188 dependencies = []
189
190 # Collect compiler arguments
191 args = self.compiler_args(self.get_compiler_check_args(mode))
192 for d in dependencies:
193 # Add compile flags needed by dependencies
194 if mode is CompileCheckMode.LINK and self.force_link:
195 # As we are passing the parameter to valac we don't need the dependent libraries.
196 a = d.get_compile_args()
197 if a:
198 p = a[0]
199 n = p[max(p.rfind('/'), p.rfind('\\'))+1:]
200 if not n == d.get_name():
201 args += ['--pkg=' + d.get_name()] # This is used by gio-2.0 among others.
202 else:
203 args += ['--pkg=' + n]
204 else:
205 args += ['--Xcc=-l' + d.get_name()] # This is used by the maths library(-lm) among others.
206 else:
207 args += d.get_compile_args()
208 if mode is CompileCheckMode.LINK:
209 # Add link flags needed to find dependencies
210 if not self.force_link: # There are no need for link dependencies when linking with valac.
211 args += d.get_link_args()
212
213 if mode is CompileCheckMode.COMPILE:
214 # Add DFLAGS from the env
215 args += self.environment.coredata.get_external_args(self.for_machine, self.language)
216 elif mode is CompileCheckMode.LINK:
217 # Add LDFLAGS from the env
218 args += self.environment.coredata.get_external_link_args(self.for_machine, self.language)
219 # extra_args must override all other arguments, so we add them last
220 args += extra_args
221 return args
222
223 def links(self, code: 'mesonlib.FileOrString', *,
224 compiler: T.Optional['Compiler'] = None,
225 extra_args: T.Union[None, T.List[str], CompilerArgs, T.Callable[[CompileCheckMode], T.List[str]]] = None,
226 dependencies: T.Optional[T.List['Dependency']] = None,
227 disable_cache: bool = False) -> T.Tuple[bool, bool]:
228 self.force_link = True
229 if compiler:
230 with compiler._build_wrapper(code, dependencies=dependencies, want_output=True) as r:
231 objfile = mesonlib.File.from_absolute_file(r.output_name)
232 result = self.compiles(objfile, extra_args=extra_args,
233 dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=True)
234 self.force_link = False
235 return result
236 result = self.compiles(code, extra_args=extra_args,
237 dependencies=dependencies, mode=CompileCheckMode.LINK, disable_cache=disable_cache)
238 self.force_link = False
239 return result