Debian Codegraph @

test cases/common/14 configure file/meson.build

raw
1 project('configure file test', 'c', meson_version: '>=0.63.0')
2
3 fs = import('fs')
4
5 conf = configuration_data()
6
7 conf.set('var', 'mystring')
8 conf.set('other', 'string 2')
9 conf.set('second', ' bonus')
10 conf.set('BE_TRUE', true)
11
12 assert(conf.get('var') == 'mystring', 'Get function is not working.')
13 assert(conf.get('var', 'default') == 'mystring', 'Get function is not working.')
14 assert(conf.get('notthere', 'default') == 'default', 'Default value getting is not working.')
15 assert(conf.keys() == ['BE_TRUE', 'other', 'second', 'var'], 'Keys function is not working')
16
17 cfile = configure_file(input : 'config.h.in',
18 output : 'config.h',
19 configuration : conf)
20
21 e = executable('inctest', 'prog.c',
22 # Note that you should NOT do this. Don't add generated headers here
23 # This tests that we do the right thing even if people add in conf files
24 # to their sources.
25 cfile)
26 test('inctest', e)
27
28 # Test if we can also pass files() as input
29 configure_file(input : files('config.h.in'),
30 output : 'config2.h',
31 configuration : conf)
32
33 # Test if build_subdir works
34 configure_file(input : files('config.h.in'),
35 output : 'config2.h',
36 build_subdir : 'config-subdir',
37 install_dir : 'share/appdir/config-subdir',
38 configuration : conf)
39
40 # Now generate a header file with an external script.
41 genprog = import('python3').find_python()
42 scriptfile = '@0@/generator.py'.format(meson.current_source_dir())
43 ifile = '@0@/dummy.dat'.format(meson.current_source_dir())
44 ofile = '@0@/config2.h'.format(meson.current_build_dir())
45
46 check_file = find_program('check_file.py')
47 # Configure in source root with command and absolute paths
48 outf = configure_file(input : 'dummy.dat',
49 output : 'config2.h',
50 command : [genprog, scriptfile, ifile, ofile],
51 install_dir : 'share/appdir')
52 ret = run_command(check_file, outf, check: false)
53 if ret.returncode() != 0
54 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
55 endif
56
57 # Same again as before, but an input file should not be required in
58 # this case where we use a command/script to generate the output file.
59 genscript2b = '@0@/generator-without-input-file.py'.format(meson.current_source_dir())
60 ofile2b = '@0@/config2b.h'.format(meson.current_build_dir())
61 outf = configure_file(
62 output : 'config2b.h',
63 command : [genprog, genscript2b, ofile2b],
64 install_dir : 'share/appdir')
65 ret = run_command(check_file, outf, check: false)
66 if ret.returncode() != 0
67 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
68 endif
69
70 genscript2deps = '@0@/generator-deps.py'.format(meson.current_source_dir())
71 ofile2deps = '@0@/config2deps.h'.format(meson.current_build_dir())
72 outf = configure_file(
73 output : 'config2deps.h',
74 depfile : 'depfile.d',
75 command : [genprog, genscript2deps, ofile2deps, '@DEPFILE@'])
76 ret = run_command(check_file, outf, check: false)
77 if ret.returncode() != 0
78 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
79 endif
80
81 found_script = find_program('generator.py')
82 # More configure_file tests in here
83 subdir('subdir')
84
85 test('inctest2', executable('prog2', 'prog2.c'))
86
87 # Generate a conf file without an input file.
88
89 dump = configuration_data()
90 dump.set_quoted('SHOULD_BE_STRING', 'string', description : 'A string')
91 dump.set_quoted('SHOULD_BE_STRING2', 'A "B" C')
92 dump.set_quoted('SHOULD_BE_STRING3', 'A "" C')
93 dump.set_quoted('SHOULD_BE_STRING4', 'A " C')
94 dump.set('SHOULD_BE_RETURN', 'return')
95 dump.set('SHOULD_BE_DEFINED', true)
96 dump.set('SHOULD_BE_UNDEFINED', false)
97 dump.set('SHOULD_BE_ONE', 1)
98 dump.set('SHOULD_BE_ZERO', 0, description : 'Absolutely zero')
99 dump.set('SHOULD_BE_QUOTED_ONE', '"1"')
100
101 dump.set_quoted('INTEGER_AS_STRING', '12')
102 if dump.get_unquoted('INTEGER_AS_STRING').to_int() == 12
103 dump.set('SHOULD_BE_UNQUOTED_STRING', dump.get_unquoted('SHOULD_BE_STRING'))
104 endif
105
106 configure_file(output : 'config3.h',
107 configuration : dump)
108
109 test('Configless.', executable('dumpprog', 'dumpprog.c'))
110
111
112 # Config file generation in a loop with @BASENAME@ substitution
113 dump = configuration_data()
114 dump.set('ZERO', 0)
115 config_templates = files(['config4a.h.in', 'config4b.h.in'])
116 foreach config_template : config_templates
117 configure_file(input : config_template, output : '@BASENAME@', configuration : dump)
118 endforeach
119
120 test('Substituted', executable('prog4', 'prog4.c'))
121
122 # Test `capture` keyword
123
124 basename_py = find_program('basename.py')
125 file_contains_py = find_program('file_contains.py')
126 test_string = 'hello world'
127 test_input_file = join_paths(meson.current_build_dir(), test_string)
128 run_command(find_program('touch.py'), test_input_file, check: true)
129 configs = [
130 # no input
131 configure_file(command: [ basename_py, test_string ], capture: true, output: 'capture test 1'),
132 # with input
133 configure_file(input: test_input_file, command: [ basename_py, '@INPUT@' ], capture: true, output: 'capture test 2'),
134 ]
135 foreach c : configs
136 test('@0@'.format(c), file_contains_py, args: [ c, test_string ])
137 endforeach
138
139 # Test variable is substituted only once
140 conf5 = configuration_data()
141 conf5.set('var', '@var2@')
142 conf5.set('var2', 'error')
143 configure_file(
144 input : 'config5.h.in',
145 output : '@BASENAME@',
146 configuration : conf5)
147 test('test5', executable('prog5', 'prog5.c'))
148
149 # Test escaping
150 conf6 = configuration_data()
151 conf6.set('var1', 'foo')
152 conf6.set('var2', 'bar')
153 conf6.set('var3', 'baz')
154 conf6.set('var4', 'qux')
155 configure_file(
156 input : 'config6.h.in',
157 output : '@BASENAME@',
158 configuration : conf6)
159 test('test6', executable('prog6', 'prog6.c'))
160
161 # test empty install dir string
162 cfile = configure_file(input : 'config.h.in',
163 output : 'do_not_get_installed.h',
164 install_dir : '',
165 configuration : conf)
166
167 # test install_dir : false (deprecated)
168 cfile = configure_file(input : 'config.h.in',
169 output : 'do_not_get_installed_please.h',
170 install_dir : false,
171 configuration : conf)
172
173 # test install_dir with install: false
174 cfile = configure_file(input : 'config.h.in',
175 output : 'do_not_get_installed_in_install_dir.h',
176 install : false,
177 install_dir : 'share/appdir',
178 configuration : conf)
179
180 # Test escaping with cmake format
181 conf7 = configuration_data()
182 conf7.set('var1', 'foo')
183 conf7.set('var2', 'bar')
184 configure_file(
185 input : 'config7.h.in',
186 output : '@BASENAME@',
187 format : 'cmake',
188 configuration : conf7)
189 test('test7', executable('prog7', 'prog7.c'))
190
191 # Test copying of an empty configuration data object
192 inf = 'invalid-utf8.bin.in'
193 outf = configure_file(input : inf,
194 output : 'invalid-utf8.bin',
195 copy: true)
196 ret = run_command(check_file, inf, outf, check: false)
197 if ret.returncode() != 0
198 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
199 endif
200 # Now the same, but using a File object as an argument.
201 inf2 = files('invalid-utf8.bin.in')[0]
202 outf = configure_file(input : inf2,
203 output : 'invalid-utf8.bin',
204 copy: true)
205 ret = run_command(check_file, inf2, outf, check: false)
206 if ret.returncode() != 0
207 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
208 endif
209
210 # Test copy of a binary file
211 outf = configure_file(input : inf,
212 output : 'somebinary.bin',
213 copy : true)
214 ret = run_command(check_file, inf, outf, check: false)
215 if ret.returncode() != 0
216 error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
217 endif
218
219 # Test the fs replacement
220 # Test copying of an empty configuration data object
221 inf = 'invalid-utf8.bin.in'
222 outf = fs.copyfile(inf, 'invalid-utf8-1.bin',
223 install: true,
224 install_dir: get_option('datadir') / meson.project_name(),
225 install_tag: 'copyfile',
226 )
227 test('fs.copyfile string', check_file, args: [files(inf), outf])
228
229 # Test with default outname of string
230 outf = fs.copyfile(inf)
231 test('fs.copyfile default name', check_file, args: [files(inf), outf])
232
233 # Now the same, but using a File object as an argument.
234 inf2 = files('invalid-utf8.bin.in')[0]
235 outf = fs.copyfile(inf2, 'invalid-utf8-2.bin')
236 test('fs.copyfile file', check_file, args: [inf2, outf])
237
238 # Test non isolatin1 encoded input file which fails to decode with utf-8
239 conf8 = configuration_data()
240 conf8.set('var', 'foo')
241 configure_file(
242 input : 'config8.h.in',
243 output : '@BASENAME@',
244 encoding : 'koi8-r',
245 configuration : conf8)
246
247 # Test that passing an empty configuration_data() object to a file with
248 # #mesondefine substitutions does not print the warning.
249 configure_file(
250 input: 'nosubst-nocopy1.txt.in',
251 output: 'nosubst-nocopy1.txt',
252 configuration : configuration_data())
253
254 # test that passing an empty configuration_data() object to a file with
255 # @foo@ substitutions does not print the warning.
256 configure_file(
257 input: 'nosubst-nocopy2.txt.in',
258 output: 'nosubst-nocopy2.txt',
259 configuration : configuration_data())
260
261 # test that passing a configured file object to test() works, and that passing
262 # an empty configuration_data() object to a file that leads to no substitutions
263 # prints a warning (see unit tests)
264 test_file = configure_file(
265 input: 'test.py.in',
266 output: 'test.py',
267 configuration: configuration_data())
268
269 # Test that overwriting an existing file creates a warning.
270 configure_file(
271 input: 'test.py.in',
272 output: 'double_output.txt',
273 configuration: conf)
274 configure_file(
275 input: 'test.py.in',
276 output: 'double_output.txt',
277 configuration: conf)
278
279 # Test that the same file name in a different subdir will not create a warning
280 configure_file(
281 input: 'test.py.in',
282 output: 'no_write_conflict.txt',
283 configuration: conf)
284
285 # Test that @BASENAME@ is substituted before checking and does not create a warning.
286 configure_file(
287 input: 'differentafterbasename1.in',
288 output: '@BASENAME@',
289 configuration: conf
290 )
291 configure_file(
292 input: 'differentafterbasename2.in',
293 output: '@BASENAME@',
294 configuration: conf
295 )
296
297 # Test that @BASENAME@ is substituted before checking and does create a warning on conflict.
298 configure_file(
299 input: 'sameafterbasename.in',
300 output: '@BASENAME@',
301 configuration: conf
302 )
303 configure_file(
304 input: 'sameafterbasename.in2',
305 output: '@BASENAME@',
306 configuration: conf
307 )
308
309 test('configure-file', test_file)
310
311 # Dictionaries
312
313 cdata = configuration_data({
314 'A_STRING' : '"foo"',
315 'A_INT' : 42,
316 'A_DEFINED' : true,
317 'A_UNDEFINED' : false,
318 })
319
320 # merging a confdata into another one should not mark the first as immutable
321 cdata_test = configuration_data()
322 cdata_test.merge_from(cdata)
323 cdata.set_quoted('A_PATH', '/random/path')
324
325 configure_file(output : 'config9a.h',
326 configuration : cdata,
327 )
328
329
330 configure_file(output : 'config9b.h',
331 configuration : {
332 'B_STRING' : '"foo"',
333 'B_INT' : 42,
334 'B_DEFINED' : true,
335 'B_UNDEFINED' : false,
336 }
337 )
338
339 test('test9', executable('prog9', 'prog9.c'))
340
341 # Test @ and curly braces at the same time with cmake format
342 conf10 = configuration_data()
343 conf10.set('var', 'foo')
344 configure_file(
345 input : 'config10.h.in',
346 output : '@BASENAME@',
347 format : 'cmake',
348 configuration : conf10)
349 test('test10', executable('prog10', 'prog10.c'))
350
351 check_inputs = find_program('check_inputs.py')
352 configure_file(output : 'check_inputs.txt',
353 input : ['prog.c', files('prog2.c', 'prog4.c')],
354 command : [check_inputs, '@OUTPUT@', '@INPUT0@', '@INPUT@', files('prog5.c')]
355 )