aboutsummaryrefslogtreecommitdiff
path: root/test cases/common/14 configure file/meson.build
blob: f40dc524fe07b318e9faaae55b0e3eedf048ad8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
project('configure file test', 'c')

conf = configuration_data()

conf.set('var', 'mystring')
conf.set('other', 'string 2')
conf.set('second', ' bonus')
conf.set('BE_TRUE', true)

assert(conf.get('var') == 'mystring', 'Get function is not working.')
assert(conf.get('var', 'default') == 'mystring', 'Get function is not working.')
assert(conf.get('notthere', 'default') == 'default', 'Default value getting is not working.')

cfile = configure_file(input : 'config.h.in',
  output : 'config.h',
  configuration : conf)

e = executable('inctest', 'prog.c',
# Note that you should NOT do this. Don't add generated headers here
# This tests that we do the right thing even if people add in conf files
# to their sources.
  cfile)
test('inctest', e)

# Test if we can also pass files() as input
configure_file(input : files('config.h.in'),
  output : 'config2.h',
  configuration : conf)

# Now generate a header file with an external script.
genprog = import('python3').find_python()
scriptfile = '@0@/generator.py'.format(meson.current_source_dir())
ifile = '@0@/dummy.dat'.format(meson.current_source_dir())
ofile = '@0@/config2.h'.format(meson.current_build_dir())

check_file = find_program('check_file.py')
# Configure in source root with command and absolute paths
outf = configure_file(input : 'dummy.dat',
  output : 'config2.h',
  command : [genprog, scriptfile,  ifile, ofile],
  install_dir : 'share/appdir')
ret = run_command(check_file, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Same again as before, but an input file should not be required in
# this case where we use a command/script to generate the output file.
genscript2b = '@0@/generator-without-input-file.py'.format(meson.current_source_dir())
ofile2b = '@0@/config2b.h'.format(meson.current_build_dir())
outf = configure_file(
  output : 'config2b.h',
  command : [genprog, genscript2b, ofile2b],
  install_dir : 'share/appdir')
ret = run_command(check_file, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

genscript2deps = '@0@/generator-deps.py'.format(meson.current_source_dir())
ofile2deps = '@0@/config2deps.h'.format(meson.current_build_dir())
outf = configure_file(
  output : 'config2deps.h',
  depfile : 'depfile.d',
  command : [genprog, genscript2deps, ofile2deps, '@DEPFILE@'])
ret = run_command(check_file, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

found_script = find_program('generator.py')
# More configure_file tests in here
subdir('subdir')

test('inctest2', executable('prog2', 'prog2.c'))

# Generate a conf file without an input file.

dump = configuration_data()
dump.set_quoted('SHOULD_BE_STRING', 'string', description : 'A string')
dump.set_quoted('SHOULD_BE_STRING2', 'A "B" C')
dump.set_quoted('SHOULD_BE_STRING3', 'A "" C')
dump.set_quoted('SHOULD_BE_STRING4', 'A " C')
dump.set('SHOULD_BE_RETURN', 'return')
dump.set('SHOULD_BE_DEFINED', true)
dump.set('SHOULD_BE_UNDEFINED', false)
dump.set('SHOULD_BE_ONE', 1)
dump.set('SHOULD_BE_ZERO', 0, description : 'Absolutely zero')
dump.set('SHOULD_BE_QUOTED_ONE', '"1"')

dump.set_quoted('INTEGER_AS_STRING', '12')
if dump.get_unquoted('INTEGER_AS_STRING').to_int() == 12
  dump.set('SHOULD_BE_UNQUOTED_STRING', dump.get_unquoted('SHOULD_BE_STRING'))
endif

configure_file(output : 'config3.h',
  configuration : dump)

test('Configless.', executable('dumpprog', 'dumpprog.c'))


# Config file generation in a loop with @BASENAME@ substitution
dump = configuration_data()
dump.set('ZERO', 0)
config_templates = files(['config4a.h.in', 'config4b.h.in'])
foreach config_template : config_templates
  configure_file(input : config_template, output : '@BASENAME@', configuration : dump)
endforeach

test('Substituted', executable('prog4', 'prog4.c'))

# Test `capture` keyword

basename_py = find_program('basename.py')
file_contains_py = find_program('file_contains.py')
test_string = 'hello world'
test_input_file = join_paths(meson.current_build_dir(), test_string)
run_command(find_program('touch.py'), test_input_file)
configs = [
    # no input
    configure_file(command: [ basename_py, test_string ], capture: true, output: 'capture test 1'),
    # with input
    configure_file(input: test_input_file, command: [ basename_py, '@INPUT@' ], capture: true, output: 'capture test 2'),
]
foreach c : configs
    test('@0@'.format(c), file_contains_py, args: [ c, test_string ])
endforeach

# Test variable is substituted only once
conf5 = configuration_data()
conf5.set('var', '@var2@')
conf5.set('var2', 'error')
configure_file(
  input : 'config5.h.in',
  output : '@BASENAME@',
  configuration : conf5)
test('test5', executable('prog5', 'prog5.c'))

# Test escaping
conf6 = configuration_data()
conf6.set('var1', 'foo')
conf6.set('var2', 'bar')
configure_file(
  input : 'config6.h.in',
  output : '@BASENAME@',
  configuration : conf6)
test('test6', executable('prog6', 'prog6.c'))

# test empty install dir string
cfile = configure_file(input : 'config.h.in',
  output : 'do_not_get_installed.h',
  install_dir : '',
  configuration : conf)

# test install_dir : false (deprecated)
cfile = configure_file(input : 'config.h.in',
  output : 'do_not_get_installed_please.h',
  install_dir : false,
  configuration : conf)

# test intsall_dir with install: false
cfile = configure_file(input : 'config.h.in',
  output : 'do_not_get_installed_in_install_dir.h',
  install : false,
  install_dir : 'share/appdir',
  configuration : conf)

# Test escaping with cmake format
conf7 = configuration_data()
conf7.set('var1', 'foo')
conf7.set('var2', 'bar')
configure_file(
  input : 'config7.h.in',
  output : '@BASENAME@',
  format : 'cmake',
  configuration : conf7)
test('test7', executable('prog7', 'prog7.c'))

# Test copying of an empty configuration data object
inf = 'invalid-utf8.bin.in'
outf = configure_file(input : inf,
  output : 'invalid-utf8.bin',
  copy: true)
ret = run_command(check_file, inf, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif
# Now the same, but using a File object as an argument.
inf2 = files('invalid-utf8.bin.in')[0]
ret = run_command(check_file, inf2, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Test copy of a binary file
outf = configure_file(input : inf,
  output : 'somebinary.bin',
  copy : true)
ret = run_command(check_file, inf, outf)
if ret.returncode() != 0
  error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
endif

# Test non isolatin1 encoded input file which fails to decode with utf-8
conf8 = configuration_data()
conf8.set('var', 'foo')
configure_file(
  input : 'config8.h.in',
  output : '@BASENAME@',
  encoding : 'koi8-r',
  configuration : conf8)

# Test that passing an empty configuration_data() object to a file with
# #mesondefine substitutions does not print the warning.
configure_file(
  input: 'nosubst-nocopy1.txt.in',
  output: 'nosubst-nocopy1.txt',
  configuration : configuration_data())

# test that passing an empty configuration_data() object to a file with
# @foo@ substitutions does not print the warning.
configure_file(
  input: 'nosubst-nocopy2.txt.in',
  output: 'nosubst-nocopy2.txt',
  configuration : configuration_data())

# test that passing a configured file object to test() works, and that passing
# an empty configuration_data() object to a file that leads to no substitutions
# prints a warning (see unit tests)
test_file = configure_file(
  input: 'test.py.in',
  output: 'test.py',
  configuration: configuration_data())

# Test that overwriting an existing file creates a warning.
configure_file(
  input: 'test.py.in',
  output: 'double_output.txt',
  configuration: conf)
configure_file(
  input: 'test.py.in',
  output: 'double_output.txt',
  configuration: conf)

# Test that the same file name in a different subdir will not create a warning
configure_file(
  input: 'test.py.in',
  output: 'no_write_conflict.txt',
  configuration: conf)

# Test that @BASENAME@ is substituted before checking and does not create a warning.
configure_file(
  input: 'differentafterbasename1.in',
  output: '@BASENAME@',
  configuration: conf
)
configure_file(
  input: 'differentafterbasename2.in',
  output: '@BASENAME@',
  configuration: conf
)

# Test that @BASENAME@ is substituted before checking and does create a warning on conflict.
configure_file(
  input: 'sameafterbasename.in',
  output: '@BASENAME@',
  configuration: conf
)
configure_file(
  input: 'sameafterbasename.in2',
  output: '@BASENAME@',
  configuration: conf
)

test('configure-file', test_file)

cdata = configuration_data()
cdata.set('invalid_value', ['array'])

# Dictionaries

cdata = configuration_data({
  'A_STRING' : '"foo"',
  'A_INT' : 42,
  'A_DEFINED' : true,
  'A_UNDEFINED' : false,
})

configure_file(output : 'config9a.h',
  configuration : cdata,
)

configure_file(output : 'config9b.h',
  configuration : {
    'B_STRING' : '"foo"',
    'B_INT' : 42,
    'B_DEFINED' : true,
    'B_UNDEFINED' : false,
  }
)

test('test9', executable('prog9', 'prog9.c'))

check_inputs = find_program('check_inputs.py')
configure_file(output : 'check_inputs.txt',
  input : ['prog.c', files('prog2.c', 'prog4.c')],
  command : [check_inputs, '@OUTPUT@', '@INPUT0@', '@INPUT@', files('prog5.c')]
)