Debian Codegraph @

unittests/optiontests.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2024 Meson project contributors
3
4 from mesonbuild.options import *
5 from mesonbuild.envconfig import MachineInfo
6
7 import os
8 import unittest
9
10
11 def make_machine(system: str) -> MachineInfo:
12 return MachineInfo(system, 'x86_64', 'x86_64', 'little', None, None)
13
14
15 def num_options(store: OptionStore) -> int:
16 return len(store.options)
17
18
19 class OptionTests(unittest.TestCase):
20
21 def test_basic(self):
22 optstore = OptionStore(False)
23 name = 'someoption'
24 default_value = 'somevalue'
25 new_value = 'new_value'
26 vo = UserStringOption(name, 'An option of some sort', default_value)
27 optstore.add_system_option(name, vo)
28 self.assertEqual(optstore.get_value_for(name), default_value)
29 optstore.set_option(OptionKey.from_string(name), new_value)
30 self.assertEqual(optstore.get_value_for(name), new_value)
31
32 def test_toplevel_project(self):
33 optstore = OptionStore(False)
34 name = 'someoption'
35 default_value = 'somevalue'
36 new_value = 'new_value'
37 k = OptionKey(name)
38 vo = UserStringOption(k.name, 'An option of some sort', default_value)
39 optstore.add_system_option(k.name, vo)
40 self.assertEqual(optstore.get_value_for(k), default_value)
41 optstore.initialize_from_top_level_project_call({OptionKey('someoption'): new_value}, {}, {})
42 self.assertEqual(optstore.get_value_for(k), new_value)
43
44 def test_machine_vs_project(self):
45 optstore = OptionStore(False)
46 name = 'backend'
47 default_value = 'ninja'
48 proj_value = 'xcode'
49 mfile_value = 'vs2010'
50 k = OptionKey(name)
51 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
52 optstore.add_system_option('prefix', prefix)
53 vo = UserStringOption(k.name, 'You know what this is', default_value)
54 optstore.add_system_option(k.name, vo)
55 self.assertEqual(optstore.get_value_for(k), default_value)
56 optstore.initialize_from_top_level_project_call({OptionKey(name): proj_value}, {},
57 {OptionKey(name): mfile_value})
58 self.assertEqual(optstore.get_value_for(k), mfile_value)
59
60 def test_subproject_system_option(self):
61 """Test that subproject system options get their default value from the global
62 option (e.g. "sub:b_lto" can be initialized from "b_lto")."""
63 optstore = OptionStore(False)
64 name = 'b_lto'
65 default_value = 'false'
66 new_value = 'true'
67 k = OptionKey(name)
68 subk = k.evolve(subproject='sub')
69 optstore.initialize_from_top_level_project_call({}, {}, {OptionKey(name): new_value})
70 vo = UserStringOption(k.name, 'An option of some sort', default_value)
71 optstore.add_system_option(subk, vo)
72 self.assertEqual(optstore.get_value_for(subk), new_value)
73
74 def test_parsing(self):
75 with self.subTest('subproject'):
76 s1 = OptionKey.from_string('sub:optname')
77 s1_expected = OptionKey('optname', 'sub', MachineChoice.HOST)
78 self.assertEqual(s1, s1_expected)
79 self.assertEqual(str(s1), 'sub:optname')
80
81 with self.subTest('plain name'):
82 s2 = OptionKey.from_string('optname')
83 s2_expected = OptionKey('optname', None, MachineChoice.HOST)
84 self.assertEqual(s2, s2_expected)
85 self.assertEqual(str(s2), 'optname')
86
87 with self.subTest('root project'):
88 s3 = OptionKey.from_string(':optname')
89 s3_expected = OptionKey('optname', '', MachineChoice.HOST)
90 self.assertEqual(s3, s3_expected)
91 self.assertEqual(str(s3), ':optname')
92
93 def test_subproject_for_system(self):
94 optstore = OptionStore(False)
95 name = 'someoption'
96 default_value = 'somevalue'
97 vo = UserStringOption(name, 'An option of some sort', default_value)
98 optstore.add_system_option(name, vo)
99 self.assertEqual(optstore.get_value_for(name, 'somesubproject'), default_value)
100
101 def test_reset(self):
102 optstore = OptionStore(False)
103 name = 'someoption'
104 original_value = 'original'
105 reset_value = 'reset'
106 vo = UserStringOption(name, 'An option set twice', original_value)
107 optstore.add_system_option(name, vo)
108 self.assertEqual(optstore.get_value_for(name), original_value)
109 self.assertEqual(num_options(optstore), 1)
110 vo2 = UserStringOption(name, 'An option set twice', reset_value)
111 optstore.add_system_option(name, vo2)
112 self.assertEqual(optstore.get_value_for(name), original_value)
113 self.assertEqual(num_options(optstore), 1)
114
115 def test_project_nonyielding(self):
116 optstore = OptionStore(False)
117 name = 'someoption'
118 top_value = 'top'
119 sub_value = 'sub'
120 vo = UserStringOption(name, 'A top level option', top_value, False)
121 optstore.add_project_option(OptionKey(name, ''), vo)
122 self.assertEqual(optstore.get_value_for(name, ''), top_value, False)
123 self.assertEqual(num_options(optstore), 1)
124 vo2 = UserStringOption(name, 'A subproject option', sub_value)
125 optstore.add_project_option(OptionKey(name, 'sub'), vo2)
126 self.assertEqual(optstore.get_value_for(name, ''), top_value)
127 self.assertEqual(optstore.get_value_for(name, 'sub'), sub_value)
128 self.assertEqual(num_options(optstore), 2)
129
130 def test_toplevel_project_yielding(self):
131 optstore = OptionStore(False)
132 name = 'someoption'
133 top_value = 'top'
134 vo = UserStringOption(name, 'A top level option', top_value, True)
135 optstore.add_project_option(OptionKey(name, ''), vo)
136 self.assertEqual(optstore.get_value_for(name, ''), top_value)
137
138 def test_project_yielding(self):
139 optstore = OptionStore(False)
140 name = 'someoption'
141 top_value = 'top'
142 sub_value = 'sub'
143 vo = UserStringOption(name, 'A top level option', top_value)
144 optstore.add_project_option(OptionKey(name, ''), vo)
145 self.assertEqual(optstore.get_value_for(name, ''), top_value)
146 self.assertEqual(num_options(optstore), 1)
147 vo2 = UserStringOption(name, 'A subproject option', sub_value, True)
148 optstore.add_project_option(OptionKey(name, 'sub'), vo2)
149 self.assertEqual(optstore.get_value_for(name, ''), top_value)
150 self.assertEqual(optstore.get_value_for(name, 'sub'), top_value)
151 self.assertEqual(num_options(optstore), 2)
152
153 def test_project_yielding_not_defined_in_top_project(self):
154 optstore = OptionStore(False)
155 top_name = 'a_name'
156 top_value = 'top'
157 sub_name = 'different_name'
158 sub_value = 'sub'
159 vo = UserStringOption(top_name, 'A top level option', top_value)
160 optstore.add_project_option(OptionKey(top_name, ''), vo)
161 self.assertEqual(optstore.get_value_for(top_name, ''), top_value)
162 self.assertEqual(num_options(optstore), 1)
163 vo2 = UserStringOption(sub_name, 'A subproject option', sub_value, True)
164 optstore.add_project_option(OptionKey(sub_name, 'sub'), vo2)
165 self.assertEqual(optstore.get_value_for(top_name, ''), top_value)
166 self.assertEqual(optstore.get_value_for(sub_name, 'sub'), sub_value)
167 self.assertEqual(num_options(optstore), 2)
168
169 def test_project_yielding_initialize(self):
170 optstore = OptionStore(False)
171 name = 'someoption'
172 top_value = 'top'
173 sub_value = 'sub'
174 subp = 'subp'
175 cmd_line = { OptionKey(name): top_value, OptionKey(name, subp): sub_value }
176
177 vo = UserStringOption(name, 'A top level option', 'default1')
178 optstore.add_project_option(OptionKey(name, ''), vo)
179 optstore.initialize_from_top_level_project_call({}, cmd_line, {})
180 self.assertEqual(optstore.get_value_for(name, ''), top_value)
181 self.assertEqual(num_options(optstore), 1)
182
183 vo2 = UserStringOption(name, 'A subproject option', 'default2', True)
184 optstore.add_project_option(OptionKey(name, 'subp'), vo2)
185 self.assertEqual(optstore.get_value_for(name, ''), top_value)
186 self.assertEqual(optstore.get_value_for(name, subp), top_value)
187 self.assertEqual(num_options(optstore), 2)
188
189 optstore.initialize_from_subproject_call(subp, {}, {}, cmd_line, {})
190 self.assertEqual(optstore.get_value_for(name, ''), top_value)
191 self.assertEqual(optstore.get_value_for(name, subp), sub_value)
192
193 def test_augments(self):
194 optstore = OptionStore(False)
195 name = 'cpp_std'
196 sub_name = 'sub'
197 sub2_name = 'sub2'
198 top_value = 'c++11'
199 aug_value = 'c++23'
200
201 co = UserComboOption(name,
202 'C++ language standard to use',
203 top_value,
204 choices=['c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23'])
205 optstore.add_system_option(name, co)
206 self.assertEqual(optstore.get_value_for(name), top_value)
207 self.assertEqual(optstore.get_value_for(name, sub_name), top_value)
208 self.assertEqual(optstore.get_value_for(name, sub2_name), top_value)
209
210 # First augment a subproject
211 with self.subTest('set subproject override'):
212 optstore.set_from_configure_command({OptionKey.from_string(f'{sub_name}:{name}'): aug_value})
213 self.assertEqual(optstore.get_value_for(name), top_value)
214 self.assertEqual(optstore.get_value_for(name, sub_name), aug_value)
215 self.assertEqual(optstore.get_value_for(name, sub2_name), top_value)
216
217 with self.subTest('unset subproject override'):
218 optstore.set_from_configure_command({OptionKey.from_string(f'{sub_name}:{name}'): None})
219 self.assertEqual(optstore.get_value_for(name), top_value)
220 self.assertEqual(optstore.get_value_for(name, sub_name), top_value)
221 self.assertEqual(optstore.get_value_for(name, sub2_name), top_value)
222
223 # And now augment the top level option
224 optstore.set_from_configure_command({OptionKey.from_string(f':{name}'): aug_value})
225 self.assertEqual(optstore.get_value_for(name, None), top_value)
226 self.assertEqual(optstore.get_value_for(name, ''), aug_value)
227 self.assertEqual(optstore.get_value_for(name, sub_name), top_value)
228 self.assertEqual(optstore.get_value_for(name, sub2_name), top_value)
229
230 optstore.set_from_configure_command({OptionKey.from_string(f':{name}'): None})
231 self.assertEqual(optstore.get_value_for(name), top_value)
232 self.assertEqual(optstore.get_value_for(name, sub_name), top_value)
233 self.assertEqual(optstore.get_value_for(name, sub2_name), top_value)
234
235 def test_augment_set_sub(self):
236 optstore = OptionStore(False)
237 name = 'cpp_std'
238 sub_name = 'sub'
239 sub2_name = 'sub2'
240 top_value = 'c++11'
241 aug_value = 'c++23'
242 set_value = 'c++20'
243
244 co = UserComboOption(name,
245 'C++ language standard to use',
246 top_value,
247 choices=['c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23'],
248 )
249 optstore.add_system_option(name, co)
250 optstore.set_from_configure_command({OptionKey.from_string(f'{sub_name}:{name}'): aug_value})
251 optstore.set_from_configure_command({OptionKey.from_string(f'{sub_name}:{name}'): set_value})
252 self.assertEqual(optstore.get_value_for(name), top_value)
253 self.assertEqual(optstore.get_value_for(name, sub_name), set_value)
254
255 def test_build_to_host(self):
256 key = OptionKey('cpp_std')
257 def_value = 'c++98'
258 opt_value = 'c++17'
259 optstore = OptionStore(False)
260 co = UserComboOption(key.name,
261 'C++ language standard to use',
262 def_value,
263 choices=['c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23'],
264 )
265 optstore.add_compiler_option('cpp', key, co)
266
267 cmd_line = {key: opt_value}
268 optstore.initialize_from_top_level_project_call({}, cmd_line, {})
269 self.assertEqual(optstore.get_option_and_value_for(key.as_build())[1], opt_value)
270 self.assertEqual(optstore.get_value_for(key.as_build()), opt_value)
271
272 def test_build_to_host_subproject(self):
273 key = OptionKey('cpp_std')
274 def_value = 'c++98'
275 opt_value = 'c++17'
276 subp = 'subp'
277 optstore = OptionStore(False)
278 co = UserComboOption(key.name,
279 'C++ language standard to use',
280 def_value,
281 choices=['c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23'],
282 )
283 optstore.add_compiler_option('cpp', key, co)
284
285 spcall = {key: opt_value}
286 optstore.initialize_from_top_level_project_call({}, {}, {})
287 optstore.initialize_from_subproject_call(subp, spcall, {}, {}, {})
288 self.assertEqual(optstore.get_option_and_value_for(key.evolve(subproject=subp,
289 machine=MachineChoice.BUILD))[1], opt_value)
290 self.assertEqual(optstore.get_value_for(key.evolve(subproject=subp,
291 machine=MachineChoice.BUILD)), opt_value)
292
293 def test_build_to_host_cross(self):
294 key = OptionKey('cpp_std')
295 def_value = 'c++98'
296 opt_value = 'c++17'
297 optstore = OptionStore(True)
298 for k in [key, key.as_build()]:
299 co = UserComboOption(key.name,
300 'C++ language standard to use',
301 def_value,
302 choices=['c++98', 'c++11', 'c++14', 'c++17', 'c++20', 'c++23'],
303 )
304 optstore.add_compiler_option('cpp', k, co)
305
306 cmd_line = {key: opt_value}
307 optstore.initialize_from_top_level_project_call({}, cmd_line, {})
308 print(optstore.options)
309
310 self.assertEqual(optstore.get_option_and_value_for(key)[1], opt_value)
311 self.assertEqual(optstore.get_option_and_value_for(key.as_build())[1], def_value)
312 self.assertEqual(optstore.get_value_for(key), opt_value)
313 self.assertEqual(optstore.get_value_for(key.as_build()), def_value)
314
315 def test_b_nonexistent(self):
316 optstore = OptionStore(False)
317 self.assertTrue(optstore.accept_as_pending_option(OptionKey('b_ndebug')))
318 self.assertFalse(optstore.accept_as_pending_option(OptionKey('b_whatever')))
319
320 def test_backend_option_pending(self):
321 optstore = OptionStore(False)
322 # backend options are known after the first invocation
323 self.assertTrue(optstore.accept_as_pending_option(OptionKey('backend_whatever'), True))
324 self.assertFalse(optstore.accept_as_pending_option(OptionKey('backend_whatever'), False))
325
326 def test_reconfigure_b_nonexistent(self):
327 optstore = OptionStore(False)
328 optstore.set_from_configure_command({OptionKey('b_ndebug'): True})
329
330 def test_unconfigure_nonexistent(self):
331 optstore = OptionStore(False)
332 with self.assertRaises(MesonException):
333 optstore.set_from_configure_command({OptionKey('nonexistent'): None})
334
335 def test_subproject_proj_opt_with_same_name(self):
336 name = 'tests'
337 subp = 'subp'
338
339 optstore = OptionStore(False)
340 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
341 optstore.add_system_option('prefix', prefix)
342 o = UserBooleanOption(name, 'Tests', False)
343 optstore.add_project_option(OptionKey(name, subproject=''), o)
344 o = UserBooleanOption(name, 'Tests', True)
345 optstore.add_project_option(OptionKey(name, subproject=subp), o)
346
347 cmd_line = {OptionKey(name): True}
348 spcall = {OptionKey(name): False}
349
350 optstore.initialize_from_top_level_project_call({}, cmd_line, {})
351 optstore.initialize_from_subproject_call(subp, spcall, {}, cmd_line, {})
352 self.assertEqual(optstore.get_value_for(name, ''), True)
353 self.assertEqual(optstore.get_value_for(name, subp), False)
354
355 def test_subproject_cmdline_override_global(self):
356 name = 'optimization'
357 subp = 'subp'
358 new_value = '0'
359
360 optstore = OptionStore(False)
361 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
362 optstore.add_system_option('prefix', prefix)
363 o = UserComboOption(name, 'Optimization level', '0', choices=['plain', '0', 'g', '1', '2', '3', 's'])
364 optstore.add_system_option(name, o)
365
366 toplevel_proj_default = {OptionKey(name): 's'}
367 subp_proj_default = {OptionKey(name): '3'}
368 cmd_line = {OptionKey(name): new_value}
369
370 optstore.initialize_from_top_level_project_call(toplevel_proj_default, cmd_line, {})
371 optstore.initialize_from_subproject_call(subp, {}, subp_proj_default, cmd_line, {})
372 self.assertEqual(optstore.get_value_for(name, subp), new_value)
373 self.assertEqual(optstore.get_value_for(name), new_value)
374
375 def test_subproject_parent_override_subp(self):
376 name = 'optimization'
377 subp = 'subp'
378 default_value = 's'
379 subp_value = '0'
380
381 optstore = OptionStore(False)
382 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
383 optstore.add_system_option('prefix', prefix)
384 o = UserComboOption(name, 'Optimization level', '0', choices=['plain', '0', 'g', '1', '2', '3', 's'])
385 optstore.add_system_option(name, o)
386
387 toplevel_proj_default = {OptionKey(name, subproject=subp): subp_value, OptionKey(name): default_value}
388 subp_proj_default = {OptionKey(name): '3'}
389
390 optstore.initialize_from_top_level_project_call(toplevel_proj_default, {}, {})
391 optstore.initialize_from_subproject_call(subp, {}, subp_proj_default, {}, {})
392 self.assertEqual(optstore.get_value_for(name, subp), subp_value)
393 self.assertEqual(optstore.get_value_for(name), default_value)
394
395 def test_subproject_cmdline_override_global_and_augment(self):
396 name = 'optimization'
397 subp = 'subp'
398 global_value = 's'
399 new_value = '0'
400
401 optstore = OptionStore(False)
402 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
403 optstore.add_system_option('prefix', prefix)
404 o = UserComboOption(name, 'Optimization level', '0', choices=['plain', '0', 'g', '1', '2', '3', 's'])
405 optstore.add_system_option(name, o)
406
407 toplevel_proj_default = {OptionKey(name): '1'}
408 subp_proj_default = {OptionKey(name): '3'}
409 cmd_line = {OptionKey(name): global_value, OptionKey(name, subproject=subp): new_value}
410
411 optstore.initialize_from_top_level_project_call(toplevel_proj_default, cmd_line, {})
412 optstore.initialize_from_subproject_call(subp, {}, subp_proj_default, cmd_line, {})
413 self.assertEqual(optstore.get_value_for(name, subp), new_value)
414 self.assertEqual(optstore.get_value_for(name), global_value)
415
416 def test_subproject_cmdline_override_toplevel(self):
417 name = 'default_library'
418 subp = 'subp'
419 toplevel_value = 'both'
420 subp_value = 'static'
421
422 optstore = OptionStore(False)
423 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
424 optstore.add_system_option('prefix', prefix)
425 o = UserComboOption(name, 'Kind of library', 'both', choices=['shared', 'static', 'both'])
426 optstore.add_system_option(name, o)
427
428 toplevel_proj_default = {OptionKey(name): 'shared'}
429 subp_proj_default = {OptionKey(name): subp_value}
430 cmd_line = {OptionKey(name, subproject=''): toplevel_value}
431
432 optstore.initialize_from_top_level_project_call(toplevel_proj_default, cmd_line, {})
433 optstore.initialize_from_subproject_call(subp, {}, subp_proj_default, cmd_line, {})
434 self.assertEqual(optstore.get_value_for(name, subp), subp_value)
435 self.assertEqual(optstore.get_value_for(name, ''), toplevel_value)
436
437 def test_subproject_buildtype(self):
438 subp = 'subp'
439 main1 = {OptionKey('buildtype'): 'release'}
440 main2 = {OptionKey('optimization'): '3', OptionKey('debug'): 'false'}
441 sub1 = {OptionKey('buildtype'): 'debug'}
442 sub2 = {OptionKey('optimization'): '0', OptionKey('debug'): 'true'}
443
444 for mainopt, subopt in ((main1, sub1),
445 (main2, sub2),
446 ({**main1, **main2}, {**sub1, **sub2})):
447 optstore = OptionStore(False)
448 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
449 optstore.add_system_option('prefix', prefix)
450 o = UserComboOption('buildtype', 'Build type to use', 'debug', choices=['plain', 'debug', 'debugoptimized', 'release', 'minsize', 'custom'])
451 optstore.add_system_option(o.name, o)
452 o = UserComboOption('optimization', 'Optimization level', '0', choices=['plain', '0', 'g', '1', '2', '3', 's'])
453 optstore.add_system_option(o.name, o)
454 o = UserBooleanOption('debug', 'Enable debug symbols and other information', True)
455 optstore.add_system_option(o.name, o)
456
457 optstore.initialize_from_top_level_project_call(mainopt, {}, {})
458 optstore.initialize_from_subproject_call(subp, {}, subopt, {}, {})
459 self.assertEqual(optstore.get_value_for('buildtype', subp), 'debug')
460 self.assertEqual(optstore.get_value_for('optimization', subp), '0')
461 self.assertEqual(optstore.get_value_for('debug', subp), True)
462
463 def test_deprecated_nonstring_value(self):
464 # TODO: add a lot more deprecated option tests
465 optstore = OptionStore(False)
466 name = 'deprecated'
467 do = UserStringOption(name, 'An option with some deprecation', '0',
468 deprecated={'true': '1'})
469 optstore.add_system_option(name, do)
470 optstore.set_option(OptionKey(name), True)
471 value = optstore.get_value_for(name)
472 self.assertEqual(value, '1')
473
474 def test_pending_augment_validation(self):
475 name = 'b_lto'
476 subproject = 'mysubproject'
477
478 optstore = OptionStore(False)
479 prefix = UserStringOption('prefix', 'This is needed by OptionStore', '/usr')
480 optstore.add_system_option('prefix', prefix)
481
482 optstore.initialize_from_top_level_project_call({}, {}, {})
483 optstore.initialize_from_subproject_call(subproject, {}, {OptionKey(name): 'true'}, {}, {})
484
485 bo = UserBooleanOption(name, 'LTO', False)
486 key = OptionKey(name, subproject=subproject)
487 optstore.add_system_option(key, bo)
488 stored_value = optstore.get_value_for(key)
489 self.assertIsInstance(stored_value, bool)
490 self.assertTrue(stored_value)
491
492 def test_yielding_boolean_option_with_falsy_parent(self):
493 """Test that yielding is correctly initialized when parent option value is False."""
494 optstore = OptionStore(False)
495 name = 'someoption'
496 subproject_name = 'sub'
497 parent_option = UserBooleanOption(name, 'A parent boolean option', False, yielding=True)
498 optstore.add_project_option(OptionKey(name, ''), parent_option)
499
500 child_option = UserBooleanOption(name, 'A child boolean option', True, yielding=True)
501 child_key = OptionKey(name, subproject_name)
502 optstore.add_project_option(child_key, child_option)
503 self.assertTrue(optstore.options[child_key].yielding)
504
505 def test_machine_canonicalization_cross(self):
506 """Test that BUILD machine options are handled correctly in cross compilation."""
507 optstore = OptionStore(True)
508
509 # Test that BUILD machine per-machine option is NOT canonicalized to HOST
510 host_pkg_config = OptionKey('pkg_config_path', machine=MachineChoice.HOST)
511 build_pkg_config = OptionKey('pkg_config_path', machine=MachineChoice.BUILD)
512 host_option_obj = UserStringArrayOption('pkg_config_path', 'Host pkg-config paths', ['/mingw/lib64/pkgconfig'])
513 build_option_obj = UserStringArrayOption('pkg_config_path', 'Build pkg-config paths', ['/usr/lib64/pkgconfig'])
514 optstore.add_system_option(host_pkg_config, host_option_obj)
515 optstore.add_system_option(build_pkg_config, build_option_obj)
516 option, value = optstore.get_option_and_value_for(build_pkg_config)
517 self.assertEqual(value, ['/usr/lib64/pkgconfig'])
518
519 # Test that non-per-machine BUILD option IS canonicalized to HOST
520 build_opt = OptionKey('optimization', machine=MachineChoice.BUILD)
521 host_opt = OptionKey('optimization', machine=MachineChoice.HOST)
522 common_option_obj = UserComboOption('optimization', 'Optimization level', '0',
523 choices=['plain', '0', 'g', '1', '2', '3', 's'])
524 optstore.add_system_option(host_opt, common_option_obj)
525 self.assertEqual(optstore.get_value_for(build_opt), '0')
526
527 def test_machine_canonicalization_native(self):
528 """Test that BUILD machine options are canonicalized to HOST when not cross compiling."""
529 optstore = OptionStore(False)
530
531 host_pkg_config = OptionKey('pkg_config_path', machine=MachineChoice.HOST)
532 build_pkg_config = OptionKey('pkg_config_path', machine=MachineChoice.BUILD)
533 host_option_obj = UserStringArrayOption('pkg_config_path', 'Host pkg-config paths', ['/mingw/lib64/pkgconfig'])
534 build_option_obj = UserStringArrayOption('pkg_config_path', 'Build pkg-config paths', ['/usr/lib64/pkgconfig'])
535
536 # Add per-machine option for HOST only (BUILD will be canonicalized)
537 optstore.add_system_option(host_pkg_config, host_option_obj)
538 option, value = optstore.get_option_and_value_for(build_pkg_config)
539 self.assertEqual(value, ['/mingw/lib64/pkgconfig'])
540
541 # Try again adding build option too, for completeness
542 optstore.add_system_option(build_pkg_config, build_option_obj)
543 option, value = optstore.get_option_and_value_for(build_pkg_config)
544 self.assertEqual(value, ['/mingw/lib64/pkgconfig'])
545
546 def test_sanitize_prefix_windows_host(self):
547 """Test that Windows paths are accepted when host is Windows."""
548 optstore = OptionStore(True) # cross-compile
549 optstore.set_host_machine(make_machine('windows'))
550 result = optstore.sanitize_prefix('C:\\Windows')
551 self.assertEqual(result, 'C:\\Windows')
552 result = optstore.sanitize_prefix('\\\\server\\share')
553 self.assertEqual(result, '\\\\server\\share')
554 # Forward slashes should also be accepted on Windows
555 result = optstore.sanitize_prefix('C:/Windows')
556 self.assertEqual(result, 'C:/Windows')
557 result = optstore.sanitize_prefix('//server/share')
558 self.assertEqual(result, '//server/share')
559
560 def test_sanitize_prefix_posix_host(self):
561 """Test that POSIX paths are accepted when host is POSIX."""
562 optstore = OptionStore(True) # cross-compile
563 optstore.set_host_machine(make_machine('linux'))
564 result = optstore.sanitize_prefix('/usr/local')
565 self.assertEqual(result, '/usr/local')
566 # Windows path should be rejected
567 with self.assertRaises(MesonException):
568 optstore.sanitize_prefix('\\myprog')
569 with self.assertRaises(MesonException):
570 optstore.sanitize_prefix('C:\\Windows')
571 with self.assertRaises(MesonException):
572 optstore.sanitize_prefix('C:/Windows')
573 with self.assertRaises(MesonException):
574 optstore.sanitize_prefix('\\\\server\\share')
575 # This one is not parsed as UNC
576 result = optstore.sanitize_prefix('//server/share')
577 self.assertEqual(result, '//server/share')
578
579 def test_sanitize_prefix_cygwin_host(self):
580 """Test that Cygwin uses POSIX-style paths."""
581 optstore = OptionStore(True)
582 optstore.set_host_machine(make_machine('cygwin'))
583 result = optstore.sanitize_prefix('/usr/local')
584 self.assertEqual(result, '/usr/local')
585 result = optstore.sanitize_prefix('/cygdrive/c/Windows')
586 self.assertEqual(result, '/cygdrive/c/Windows')
587
588 def test_sanitize_dir_option_cross_to_windows(self):
589 """Test directory option sanitization when cross-compiling to Windows."""
590 optstore = OptionStore(True)
591 optstore.set_host_machine(make_machine('windows'))
592 optstore.init_builtins()
593 # Set libdir to absolute path inside prefix, should be relativized
594 optstore.set_option(OptionKey('prefix'), 'C:\\Program Files\\MyProg')
595 optstore.set_option(OptionKey('libdir'), 'C:\\Program Files\\MyProg\\lib')
596 self.assertEqual(optstore.get_value_for('libdir'), 'lib')
597
598 def test_sanitize_dir_option_cross_to_linux(self):
599 """Test directory option sanitization when cross-compiling to Linux."""
600 optstore = OptionStore(True)
601 optstore.set_host_machine(make_machine('linux'))
602 optstore.init_builtins()
603 # Set libdir to absolute path inside prefix, should be relativized
604 optstore.set_option(OptionKey('prefix'), '/opt/myapp')
605 optstore.set_option(OptionKey('libdir'), '/opt/myapp/lib64')
606 self.assertEqual(optstore.get_value_for('libdir'), 'lib64')
607
608 def test_sanitize_prefix_native_path(self):
609 """Test that native paths are accepted without set_host_machine()."""
610 optstore = OptionStore(False)
611 native_path = os.sep + 'myprog'
612 result = optstore.sanitize_prefix(native_path)
613 self.assertEqual(result, native_path)
614
615 def test_is_host_absolute(self):
616 """Test _is_host_absolute with various host configurations."""
617 # POSIX host
618 optstore = OptionStore(True)
619 optstore.set_host_machine(make_machine('linux'))
620 self.assertTrue(optstore._is_host_absolute('/usr'))
621 self.assertTrue(optstore._is_host_absolute('/usr/local'))
622 self.assertFalse(optstore._is_host_absolute('relative'))
623 self.assertFalse(optstore._is_host_absolute('C:\\Windows'))
624 self.assertFalse(optstore._is_host_absolute('C:/Windows'))
625
626 # Windows host - accepts both full absolute and root-relative
627 optstore = OptionStore(True)
628 optstore.set_host_machine(make_machine('windows'))
629 self.assertTrue(optstore._is_host_absolute('C:\\Windows'))
630 self.assertTrue(optstore._is_host_absolute('C:/Windows'))
631 self.assertTrue(optstore._is_host_absolute('//server/share'))
632 self.assertTrue(optstore._is_host_absolute('\\\\server\\share'))
633 # Root-relative paths accepted for backwards compat
634 self.assertTrue(optstore._is_host_absolute('/usr'))
635 self.assertTrue(optstore._is_host_absolute('\\myprog'))
636 self.assertFalse(optstore._is_host_absolute('relative'))
637
638 # No host set - uses build machine semantics
639 optstore = OptionStore(False)
640 self.assertTrue(optstore._is_host_absolute(os.sep + 'myprog'))
641 self.assertTrue(optstore._is_host_absolute('/myprog'))