meson.build 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. configure_file(input : 'dummy.dat',
  31. output : 'config2.h',
  32. command : [genprog, scriptfile, ifile, ofile],
  33. install_dir : 'share/appdir')
  34. run_command(check_file, join_paths(meson.current_build_dir(), 'config2.h'))
  35. # Same again as before, but an input file should not be required in
  36. # this case where we use a command/script to generate the output file.
  37. genscript2b = '@0@/generator-without-input-file.py'.format(meson.current_source_dir())
  38. ofile2b = '@0@/config2b.h'.format(meson.current_build_dir())
  39. configure_file(
  40. output : 'config2b.h',
  41. command : [genprog, genscript2b, ofile2b],
  42. install_dir : 'share/appdir')
  43. run_command(check_file, join_paths(meson.current_build_dir(), 'config2b.h'))
  44. found_script = find_program('generator.py')
  45. # More configure_file tests in here
  46. subdir('subdir')
  47. test('inctest2', executable('prog2', 'prog2.c'))
  48. # Generate a conf file without an input file.
  49. dump = configuration_data()
  50. dump.set_quoted('SHOULD_BE_STRING', 'string', description : 'A string')
  51. dump.set_quoted('SHOULD_BE_STRING2', 'A "B" C')
  52. dump.set_quoted('SHOULD_BE_STRING3', 'A "" C')
  53. dump.set_quoted('SHOULD_BE_STRING4', 'A " C')
  54. dump.set('SHOULD_BE_RETURN', 'return')
  55. dump.set('SHOULD_BE_DEFINED', true)
  56. dump.set('SHOULD_BE_UNDEFINED', false)
  57. dump.set('SHOULD_BE_ONE', 1)
  58. dump.set('SHOULD_BE_ZERO', 0, description : 'Absolutely zero')
  59. dump.set('SHOULD_BE_QUOTED_ONE', '"1"')
  60. configure_file(output : 'config3.h',
  61. configuration : dump)
  62. test('Configless.', executable('dumpprog', 'dumpprog.c'))
  63. # Config file generation in a loop with @BASENAME@ substitution
  64. dump = configuration_data()
  65. dump.set('ZERO', 0)
  66. config_templates = files(['config4a.h.in', 'config4b.h.in'])
  67. foreach config_template : config_templates
  68. configure_file(input : config_template, output : '@BASENAME@',
  69. configuration : dump)
  70. endforeach
  71. test('Substituted', executable('prog4', 'prog4.c'))
  72. # Test `capture` keyword
  73. basename_py = find_program('basename.py')
  74. file_contains_py = find_program('file_contains.py')
  75. test_string = 'hello world'
  76. test_input_file = join_paths(meson.current_build_dir(), test_string)
  77. run_command(find_program('touch.py'), test_input_file)
  78. configs = [
  79. # no input
  80. configure_file(command: [ basename_py, test_string ], capture: true, output: 'capture test 1'),
  81. # with input
  82. configure_file(input: test_input_file, command: [ basename_py, '@INPUT@' ], capture: true, output: 'capture test 2'),
  83. ]
  84. foreach c : configs
  85. test('@0@'.format(c), file_contains_py, args: [ c, test_string ])
  86. endforeach