meson.build 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. project('get define', 'c', 'cpp')
  2. host_system = host_machine.system()
  3. foreach lang : ['c', 'cpp']
  4. cc = meson.get_compiler(lang)
  5. if host_system == 'linux'
  6. d = cc.get_define('__linux__')
  7. assert(d == '1', '__linux__ value is @0@ instead of 1'.format(d))
  8. elif host_system == 'darwin'
  9. d = cc.get_define('__APPLE__')
  10. assert(d == '1', '__APPLE__ value is @0@ instead of 1'.format(d))
  11. elif host_system == 'windows'
  12. d = cc.get_define('_WIN32')
  13. assert(d == '1', '_WIN32 value is @0@ instead of 1'.format(d))
  14. elif host_system == 'cygwin'
  15. d = cc.get_define('__CYGWIN__')
  16. assert(d == '1', '__CYGWIN__ value is @0@ instead of 1'.format(d))
  17. elif host_system == 'haiku'
  18. d = cc.get_define('__HAIKU__')
  19. assert(d == '1', '__HAIKU__ value is @0@ instead of 1'.format(d))
  20. elif host_system == 'freebsd'
  21. # the __FreeBSD__ define will be equal to the major version of the release
  22. # (ex, in FreeBSD 11.x, __FreeBSD__ == 11). To make the test robust when
  23. # being run on various versions of FreeBSD, just test that the define is
  24. # set.
  25. d = cc.get_define('__FreeBSD__')
  26. assert(d != '', '__FreeBSD__ value is unset')
  27. elif host_system == 'dragonfly'
  28. d = cc.get_define('__DragonFly__')
  29. assert(d == '1', '__DragonFly__ value is @0@ instead of 1'.format(d))
  30. elif host_system == 'netbsd'
  31. d = cc.get_define('__NetBSD__')
  32. assert(d == '1', '__NetBSD__ value is @0@ instead of 1'.format(d))
  33. elif host_system == 'gnu'
  34. d = cc.get_define('__GNU__')
  35. assert(d == '1', '__GNU__ value is @0@ instead of 1'.format(d))
  36. else
  37. error('Please report a bug and help us improve support for this platform')
  38. endif
  39. if cc.find_library('z', required : false).found()
  40. # When a C file containing #include <foo.h> is pre-processed and foo.h is
  41. # found in the compiler's default search path, GCC inserts an extra comment
  42. # between the delimiter and the define which causes a parsing error.
  43. # https://github.com/mesonbuild/meson/issues/1726
  44. if host_machine.system() == 'netbsd'
  45. # NetBSD's zlib doesn't is version 1.2.3 and doesn't have a
  46. # ZLIB_VER_MAJOR, but it does have a ZLIB_VERSION (which is a string), so
  47. # check the first non-quote character of that.
  48. ver = cc.get_define('ZLIB_VERSION', prefix : '#include <zlib.h>')[1]
  49. assert(ver == '1', 'ZLIB_VERSION (major) value is "@0@" instead of "1"'.format(ver))
  50. else
  51. ver = cc.get_define('ZLIB_VER_MAJOR', prefix : '#include <zlib.h>')
  52. assert(ver == '1', 'ZLIB_VER_MAJOR value is "@0@" instead of "1"'.format(ver))
  53. endif
  54. endif
  55. # Check that an undefined value is empty.
  56. have = cc.get_define('MESON_FAIL_VALUE')
  57. assert(have == '', 'MESON_FAIL_VALUE value is "@0@" instead of ""'.format(have))
  58. # This is used in the test_preprocessor_checks_CPPFLAGS() unit test.
  59. have = cc.get_define('MESON_TEST_DEFINE_VALUE')
  60. expect = get_option('MESON_TEST_DEFINE_VALUE')
  61. assert(have == expect, 'MESON_TEST_DEFINE_VALUE value is "@0@" instead of "@1@"'.format(have, expect))
  62. run_1665_test = false
  63. if meson.is_cross_build()
  64. lang_arg = meson.get_cross_property(lang + '_args', '')
  65. if lang_arg == '-DMESON_TEST_ISSUE_1665=1'
  66. run_1665_test = true
  67. endif
  68. endif
  69. if run_1665_test
  70. have = cc.get_define('MESON_TEST_ISSUE_1665')
  71. assert(have == '1', 'MESON_TEST_ISSUE_1665 value is "@0@" instead of "1"'.format(have))
  72. endif
  73. have = cc.get_define('TEST_VERSION_STR',
  74. prefix : '#include <concat.h>', include_directories: include_directories('.'))
  75. assert(have == '"6.0.0"', 'TEST_VERSION_STR value is "@0@" instead of ""6.0.0""'.format(have))
  76. concat_examples = {
  77. 'TEST_CONCAT_1': '"abcdef"',
  78. 'TEST_CONCAT_2': '1',
  79. 'TEST_CONCAT_3': '1 2 3',
  80. 'TEST_CONCAT_4': '"ab" 1 "cd"',
  81. 'TEST_CONCAT_5': '"ab\"cd"',
  82. 'TEST_CONCAT_6': '"ab\" \"cd"',
  83. }
  84. foreach def,expected : concat_examples
  85. have = cc.get_define(def,
  86. prefix : '#include <concat.h>', include_directories: include_directories('.'))
  87. assert(have == expected, '@0@ value is "@1@" instead of "@2@"'.format(def, have, expected))
  88. endforeach
  89. endforeach