meson.build 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. project('configure file test', 'c')
  2. conf = configuration_data()
  3. conf.set('var', 'mystring')
  4. conf.set('other', 'string 2')
  5. conf.set('second', ' bonus')
  6. conf.set('BE_TRUE', true)
  7. assert(conf.get('var') == 'mystring', 'Get function is not working.')
  8. assert(conf.get('var', 'default') == 'mystring', 'Get function is not working.')
  9. assert(conf.get('notthere', 'default') == 'default', 'Default value getting is not working.')
  10. cfile = configure_file(input : 'config.h.in',
  11. output : 'config.h',
  12. configuration : conf)
  13. e = executable('inctest', 'prog.c',
  14. # Note that you should NOT do this. Don't add generated headers here
  15. # This tests that we do the right thing even if people add in conf files
  16. # to their sources.
  17. cfile)
  18. test('inctest', e)
  19. # Test if we can also pass files() as input
  20. configure_file(input : files('config.h.in'),
  21. output : 'config2.h',
  22. configuration : conf)
  23. # Now generate a header file with an external script.
  24. genprog = import('python3').find_python()
  25. scriptfile = '@0@/generator.py'.format(meson.current_source_dir())
  26. ifile = '@0@/dummy.dat'.format(meson.current_source_dir())
  27. ofile = '@0@/config2.h'.format(meson.current_build_dir())
  28. check_file = find_program('check_file.py')
  29. # Configure in source root with command and absolute paths
  30. outf = configure_file(input : 'dummy.dat',
  31. output : 'config2.h',
  32. command : [genprog, scriptfile, ifile, ofile],
  33. install_dir : 'share/appdir')
  34. ret = run_command(check_file, outf)
  35. if ret.returncode() != 0
  36. error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
  37. endif
  38. # Same again as before, but an input file should not be required in
  39. # this case where we use a command/script to generate the output file.
  40. genscript2b = '@0@/generator-without-input-file.py'.format(meson.current_source_dir())
  41. ofile2b = '@0@/config2b.h'.format(meson.current_build_dir())
  42. outf = configure_file(
  43. output : 'config2b.h',
  44. command : [genprog, genscript2b, ofile2b],
  45. install_dir : 'share/appdir')
  46. ret = run_command(check_file, outf)
  47. if ret.returncode() != 0
  48. error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
  49. endif
  50. found_script = find_program('generator.py')
  51. # More configure_file tests in here
  52. subdir('subdir')
  53. test('inctest2', executable('prog2', 'prog2.c'))
  54. # Generate a conf file without an input file.
  55. dump = configuration_data()
  56. dump.set_quoted('SHOULD_BE_STRING', 'string', description : 'A string')
  57. dump.set_quoted('SHOULD_BE_STRING2', 'A "B" C')
  58. dump.set_quoted('SHOULD_BE_STRING3', 'A "" C')
  59. dump.set_quoted('SHOULD_BE_STRING4', 'A " C')
  60. dump.set('SHOULD_BE_RETURN', 'return')
  61. dump.set('SHOULD_BE_DEFINED', true)
  62. dump.set('SHOULD_BE_UNDEFINED', false)
  63. dump.set('SHOULD_BE_ONE', 1)
  64. dump.set('SHOULD_BE_ZERO', 0, description : 'Absolutely zero')
  65. dump.set('SHOULD_BE_QUOTED_ONE', '"1"')
  66. dump.set_quoted('INTEGER_AS_STRING', '12')
  67. if dump.get_unquoted('INTEGER_AS_STRING').to_int() == 12
  68. dump.set('SHOULD_BE_UNQUOTED_STRING', dump.get_unquoted('SHOULD_BE_STRING'))
  69. endif
  70. configure_file(output : 'config3.h',
  71. configuration : dump)
  72. test('Configless.', executable('dumpprog', 'dumpprog.c'))
  73. # Config file generation in a loop with @BASENAME@ substitution
  74. dump = configuration_data()
  75. dump.set('ZERO', 0)
  76. config_templates = files(['config4a.h.in', 'config4b.h.in'])
  77. foreach config_template : config_templates
  78. configure_file(input : config_template, output : '@BASENAME@',
  79. configuration : dump)
  80. endforeach
  81. test('Substituted', executable('prog4', 'prog4.c'))
  82. # Test `capture` keyword
  83. basename_py = find_program('basename.py')
  84. file_contains_py = find_program('file_contains.py')
  85. test_string = 'hello world'
  86. test_input_file = join_paths(meson.current_build_dir(), test_string)
  87. run_command(find_program('touch.py'), test_input_file)
  88. configs = [
  89. # no input
  90. configure_file(command: [ basename_py, test_string ], capture: true, output: 'capture test 1'),
  91. # with input
  92. configure_file(input: test_input_file, command: [ basename_py, '@INPUT@' ], capture: true, output: 'capture test 2'),
  93. ]
  94. foreach c : configs
  95. test('@0@'.format(c), file_contains_py, args: [ c, test_string ])
  96. endforeach
  97. # Test variable is substituted only once
  98. conf5 = configuration_data()
  99. conf5.set('var', '@var2@')
  100. conf5.set('var2', 'error')
  101. configure_file(
  102. input : 'config5.h.in',
  103. output : '@BASENAME@',
  104. configuration : conf5
  105. )
  106. test('test5', executable('prog5', 'prog5.c'))
  107. # Test escaping
  108. conf6 = configuration_data()
  109. conf6.set('var1', 'foo')
  110. conf6.set('var2', 'bar')
  111. configure_file(
  112. input : 'config6.h.in',
  113. output : '@BASENAME@',
  114. configuration : conf6
  115. )
  116. test('test6', executable('prog6', 'prog6.c'))
  117. # test empty install dir string
  118. cfile = configure_file(input : 'config.h.in',
  119. output : 'do_not_get_installed.h',
  120. install_dir : '',
  121. configuration : conf)
  122. # Test escaping with cmake format
  123. conf7 = configuration_data()
  124. conf7.set('var1', 'foo')
  125. conf7.set('var2', 'bar')
  126. configure_file(
  127. input : 'config7.h.in',
  128. output : '@BASENAME@',
  129. format : 'cmake',
  130. configuration : conf7
  131. )
  132. test('test7', executable('prog7', 'prog7.c'))
  133. # Test empty configuration data object on invalid utf8 file
  134. inf = 'invalid-utf8.bin.in'
  135. outf = configure_file(input : inf,
  136. output : 'invalid-utf8.bin',
  137. configuration : configuration_data())
  138. ret = run_command(check_file, inf, outf)
  139. if ret.returncode() != 0
  140. error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
  141. endif
  142. # Test copy of a binary file
  143. outf = configure_file(input : inf,
  144. output : 'somebinary.bin',
  145. copy : true)
  146. ret = run_command(check_file, inf, outf)
  147. if ret.returncode() != 0
  148. error('Error running command: @0@\n@1@'.format(ret.stdout(), ret.stderr()))
  149. endif
  150. # Test non isolatin1 encoded input file which fails to decode with utf-8
  151. conf8 = configuration_data()
  152. conf8.set('var', 'foo')
  153. configure_file(
  154. input : 'config8.h.in',
  155. output : '@BASENAME@',
  156. encoding : 'koi8-r',
  157. configuration : conf8
  158. )