xref: /aosp_15_r20/external/mesa3d/meson_to_hermetic/meson_common.py (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1import os
2import warnings
3import meson_impl as impl
4
5_gArrayOptions = []
6_gFeatureOptions = []
7_gBooleanOptions = []
8_gComboOptions = []
9_gSimpleOptions = []
10
11
12def noop():
13    return
14
15
16def message(str):
17    print(str)
18
19
20def error(message):
21    exit(message)
22
23
24def warning(message):
25    warnings.warn(message)
26
27
28def set_relative_dir(dir):
29    impl.set_relative_dir(dir)
30
31
32def files(*filenames):
33    file_list = []
34    for file in filenames:
35        file_list.append(impl.File(os.path.join(impl.get_relative_dir(), file)))
36    return file_list
37
38
39def declare_dependency(
40    compile_args=[],
41    d_import_dirs=[],
42    d_module_versions='',
43    dependencies=[],
44    extra_files=[],
45    include_directories=[],
46    link_args=[],
47    link_whole=[],
48    link_with=[],
49    objects=[],
50    sources=[],
51    variables=[],
52    version='',
53):
54    link_with = impl.get_linear_list([link_with])
55    link_whole = impl.get_linear_list([link_whole])
56
57    return impl.Dependency(
58        'declared',
59        version,
60        found=True,
61        compile_args=compile_args,
62        include_directories=include_directories,
63        dependencies=dependencies,
64        sources=sources,
65        link_with=link_with,
66        link_whole=link_whole,
67    )
68
69
70def find_program(name: str, required=False, native=False, disabler=False, version=''):
71    if type(required) is impl.FeatureOption:
72        required = required.state == impl.EnableState.ENABLED
73    if type(required) is not bool:
74        exit('Unhandled required type: ' + str(type(required)))
75
76    maybe_filename = impl.get_relative_dir(name)
77
78    # may be a script in the current directory
79    if os.path.isfile(maybe_filename):
80        return impl.Program(maybe_filename, found=True)
81
82    # These are required for building turnip though not tagged as such
83    if name == 'bison' or name == 'flex' or name == 'gzip':
84        return impl.Program(name, found=True)
85
86    if (
87        name == 'byacc'
88        or name == 'glslangValidator'
89        or name == 'install_megadrivers.py'
90        or name == 'nm'
91        or name == 'python'
92        or name == 'symbols-check.py'
93        or name == 'sphinx-build'
94    ):
95        return impl.Program(name, found=required)
96
97    exit('Unhandled program check: ' + name)
98
99
100def add_project_arguments(args, language=[], native=False):
101    impl.add_project_arguments(args, language, native)
102
103
104def add_project_link_arguments(args, language=[], native=False):
105    return
106
107
108# Used by meson_options.txt to define an option
109def option(
110    name: str,
111    type: str,
112    min: int = 0,
113    max: int = 0,
114    value='',
115    choices=[],
116    description='',
117    deprecated=None,
118):
119    if type == 'array':
120        global _gArrayOptions
121        _gArrayOptions.append(impl.ArrayOption(name, value))
122        return
123    if type == 'feature':
124        global _gFeatureOptions
125        if value == '' or value == 'auto':
126            state = impl.EnableState.AUTO
127        elif value == 'disabled':
128            state = impl.EnableState.DISABLED
129        elif value == 'enabled':
130            state = impl.EnableState.ENABLED
131        else:
132            exit('Unhandled feature option value')
133        _gFeatureOptions.append(impl.FeatureOption(name, state))
134        return
135    if type == 'boolean':
136        global _gBooleanOptions
137        if isinstance(value, str):
138            flag = True if value.lower() == 'true' else False
139            _gBooleanOptions.append(impl.BooleanOption(name, flag))
140        else:
141            _gBooleanOptions.append(impl.BooleanOption(name, value))
142        return
143    if type == 'combo':
144        global _gComboOptions
145        _gComboOptions.append(impl.ComboOption(name, value))
146        return
147    if type == 'string' or type == 'integer':
148        global _gSimpleOptions
149        _gSimpleOptions.append(impl.SimpleOption(name, value))
150        return
151
152
153def set_option(name, value: str):
154    print('set_option: %s=%s' % (name, value))
155    for option in _gArrayOptions:
156        if option.name == name:
157            option.set(value)
158
159    for option in _gFeatureOptions:
160        if option.name == name:
161            option.set(value)
162
163    for option in _gBooleanOptions:
164        if option.name == name:
165            option.set(value)
166
167    for option in _gComboOptions:
168        if option.name == name:
169            option.set(value)
170
171    for option in _gSimpleOptions:
172        if option.name == name:
173            option.set(value)
174
175    for option in impl.get_project_options():
176        if option.name == name:
177            option.set(value)
178
179
180def get_option(name):
181    for option in _gArrayOptions:
182        if option.name == name:
183            return option.strings
184
185    for option in _gFeatureOptions:
186        if option.name == name:
187            return option
188
189    for option in _gBooleanOptions:
190        if option.name == name:
191            return option.value
192
193    for option in _gComboOptions:
194        if option.name == name:
195            return option.value
196
197    for option in _gSimpleOptions:
198        if option.name == name:
199            return option.value
200
201    for option in impl.get_project_options():
202        if option.name == name:
203            return option.value
204
205    # built-in options
206    if name == 'layout':
207        return 'mirror'
208    if name == 'prefix':
209        return 'prefix'
210    if name == 'libdir':
211        return 'libdir'
212    if name == 'datadir':
213        return 'datadir'
214    if name == 'sysconfdir':
215        return 'sysconfdir'
216    if name == 'includedir':
217        return 'includedir'
218    if name == 'c_args':
219        return ''
220    if name == 'cpp_rtti':
221        return False
222    if name == 'debug':
223        return True
224    if name == 'b_sanitize':
225        return False
226    if name == 'backend':
227        return 'custom'
228
229    exit('Unhandled option: ' + name)
230
231
232def project(name, language_list, version, license, meson_version, default_options=[]):
233    impl.project(name, language_list, version, license, meson_version, default_options)
234
235
236def run_command(program, *commands, check=False):
237    return program.run_command(commands)
238
239
240def environment():
241    return impl.Environment()
242
243
244def join_paths(*paths):
245    joined_path = ''
246    for path in paths:
247        joined_path = os.path.join(joined_path, path)
248    return joined_path
249
250
251def executable(
252    target_name,
253    *source,
254    c_args=[],
255    cpp_args=[],
256    c_pch='',
257    build_by_default=False,
258    build_rpath='',
259    d_debug=[],
260    d_import_dirs=[],
261    d_module_versions=[],
262    d_unittest=False,
263    dependencies=[],
264    export_dynamic=False,
265    extra_files='',
266    gnu_symbol_visibility='',
267    gui_app=False,
268    implib=False,
269    implicit_include_directories=False,
270    include_directories=[],
271    install=False,
272    install_dir='',
273    install_mode=[],
274    install_rpath='',
275    install_tag='',
276    link_args=[],
277    link_depends='',
278    link_language='',
279    link_whole=[],
280    link_with=[],
281    name_prefix='',
282    name_suffix='',
283    native=False,
284    objects=[],
285    override_options=[],
286    pie=False,
287    rust_crate_type='',
288    rust_dependency_map={},
289    sources='',
290    vala_args=[],
291    vs_module_defs='',
292    win_subsystem='',
293):
294    return impl.Executable(target_name)
295
296
297def test(
298    name,
299    executable,
300    args=[],
301    depends=[],
302    env=[],
303    is_parallel=False,
304    priority=0,
305    protocol='',
306    should_fail=False,
307    suite='',
308    timeout=0,
309    verbose=False,
310    workdir='',
311):
312    return
313
314
315def summary(entry, bool_yn=False, list_sep='', section=''):
316    return
317
318
319def install_headers(*headers, subdir=''):
320    return
321
322
323def install_data(
324    *files,
325    follow_symlinks=False,
326    install_dir='',
327    install_mode=[],
328    install_tag='',
329    preserve_path=False,
330    rename=[],
331    sources=[],
332):
333    return
334
335
336def subdir(dir_=''):
337    return
338