| 1 |
project('get define', 'c', 'cpp') |
| 2 |
|
| 3 |
host_system = host_machine.system() |
| 4 |
|
| 5 |
system_define_map = { |
| 6 |
'linux' : ['__linux__', '1'], |
| 7 |
'darwin' : ['__APPLE__', '1'], |
| 8 |
'windows' : ['_WIN32', '1'], |
| 9 |
'cygwin' : ['__CYGWIN__', '1'], |
| 10 |
'haiku' : ['__HAIKU__', '1'], |
| 11 |
'dragonfly' : ['__DragonFly__', '1'], |
| 12 |
'netbsd' : ['__NetBSD__', '1'], |
| 13 |
'openbsd' : ['__OpenBSD__', '1'], |
| 14 |
'gnu' : ['__GNU__', '1'], |
| 15 |
'sunos' : ['__sun__', '1'], |
| 16 |
|
| 17 |
# The __FreeBSD__ define will be equal to the major version of the release |
| 18 |
# (ex, in FreeBSD 11.x, __FreeBSD__ == 11). To make the test robust when |
| 19 |
# being run on various versions of FreeBSD, just test that the define is |
| 20 |
# set. |
| 21 |
'freebsd' : ['__FreeBSD__'], |
| 22 |
} |
| 23 |
|
| 24 |
foreach lang : ['c', 'cpp'] |
| 25 |
cc = meson.get_compiler(lang) |
| 26 |
|
| 27 |
if not system_define_map.has_key(host_system) |
| 28 |
error('Please report a bug and help us improve support for this platform') |
| 29 |
endif |
| 30 |
|
| 31 |
system_define = system_define_map.get(host_system) |
| 32 |
|
| 33 |
def_name = system_define[0] |
| 34 |
def_val = cc.get_define(system_define[0]) |
| 35 |
def_exist = cc.has_define(system_define[0]) |
| 36 |
|
| 37 |
assert((def_val != '') == def_exist, |
| 38 |
'The has_define and get_define results for @0@ disagree with each other'.format(def_name)) |
| 39 |
|
| 40 |
if system_define.length() == 2 |
| 41 |
assert(def_val == system_define[1], |
| 42 |
'@0@ value is @1@ instead of @2@'.format(def_name, def_val, system_define[1])) |
| 43 |
elif system_define.length() == 1 |
| 44 |
assert(def_val != '', '@0@ value is unset'.format(def_name)) |
| 45 |
else |
| 46 |
error('Invalid number of items in system_define array, this is a bug in the test!') |
| 47 |
endif |
| 48 |
|
| 49 |
if cc.find_library('z', required : false).found() |
| 50 |
# When a C file containing #include <foo.h> is pre-processed and foo.h is |
| 51 |
# found in the compiler's default search path, GCC inserts an extra comment |
| 52 |
# between the delimiter and the define which causes a parsing error. |
| 53 |
# https://github.com/mesonbuild/meson/issues/1726 |
| 54 |
if host_machine.system() == 'netbsd' or host_machine.system() == 'openbsd' |
| 55 |
# NetBSD and OpenBSD's zlib don't have a ZLIB_VER_MAJOR, but they do have |
| 56 |
# a ZLIB_VERSION (which is a string), so check the first non-quote |
| 57 |
# character of that. |
| 58 |
ver = cc.get_define('ZLIB_VERSION', prefix : '#include <zlib.h>')[1] |
| 59 |
assert(ver == '1', 'ZLIB_VERSION (major) value is "@0@" instead of "1"'.format(ver)) |
| 60 |
else |
| 61 |
ver = cc.get_define('ZLIB_VER_MAJOR', prefix : '#include <zlib.h>') |
| 62 |
assert(ver == '1', 'ZLIB_VER_MAJOR value is "@0@" instead of "1"'.format(ver)) |
| 63 |
endif |
| 64 |
endif |
| 65 |
|
| 66 |
# Check that an undefined value is empty. |
| 67 |
have_val = cc.get_define('MESON_FAIL_VALUE') |
| 68 |
have = cc.has_define('MESON_FAIL_VALUE') |
| 69 |
assert(have_val == '', 'MESON_FAIL_VALUE value is "@0@" instead of ""'.format(have_val)) |
| 70 |
assert(not have, 'MESON_FAIL_VALUE was found even though it should not have been') |
| 71 |
|
| 72 |
# Check that an empty define is reported as existing. |
| 73 |
have_val = cc.get_define('MESON_EMPTY_VALUE', prefix: ['#define MESON_EMPTY_VALUE']) |
| 74 |
have = cc.has_define('MESON_EMPTY_VALUE', prefix: ['#define MESON_EMPTY_VALUE']) |
| 75 |
assert(have_val == '', 'MESON_EMPTY_VALUE value is "@0@" instead of ""'.format(have_val)) |
| 76 |
assert(have, 'MESON_EMPTY_VALUE was not found even though it should have been') |
| 77 |
|
| 78 |
# Check if prefix array works properly and has the expected order |
| 79 |
have = cc.get_define('MESON_FAIL_VALUE', prefix: ['#define MESON_FAIL_VALUE 1', '#undef MESON_FAIL_VALUE']) |
| 80 |
assert(have == '', 'MESON_FAIL_VALUE value is "@0@" instead of ""'.format(have)) |
| 81 |
|
| 82 |
have = cc.get_define('MESON_SUCCESS_VALUE', prefix: ['#undef MESON_SUCCESS_VALUE', '#define MESON_SUCCESS_VALUE 1']) |
| 83 |
assert(have == '1', 'MESON_SUCCESS_VALUE value is "@0@" instead of ""'.format(have)) |
| 84 |
|
| 85 |
# This is used in the test_preprocessor_checks_CPPFLAGS() unit test. |
| 86 |
have = cc.get_define('MESON_TEST_DEFINE_VALUE') |
| 87 |
expect = get_option('MESON_TEST_DEFINE_VALUE') |
| 88 |
assert(have == expect, 'MESON_TEST_DEFINE_VALUE value is "@0@" instead of "@1@"'.format(have, expect)) |
| 89 |
|
| 90 |
run_1665_test = false |
| 91 |
if meson.is_cross_build() |
| 92 |
lang_arg = meson.get_cross_property(lang + '_args', '') |
| 93 |
if lang_arg == '-DMESON_TEST_ISSUE_1665=1' |
| 94 |
run_1665_test = true |
| 95 |
endif |
| 96 |
endif |
| 97 |
|
| 98 |
if run_1665_test |
| 99 |
have = cc.get_define('MESON_TEST_ISSUE_1665') |
| 100 |
assert(have == '1', 'MESON_TEST_ISSUE_1665 value is "@0@" instead of "1"'.format(have)) |
| 101 |
endif |
| 102 |
|
| 103 |
have = cc.get_define('TEST_VERSION_STR', |
| 104 |
prefix : '#include <concat.h>', include_directories: include_directories('.')) |
| 105 |
assert(have == '"6.0.0"', 'TEST_VERSION_STR value is "@0@" instead of ""6.0.0""'.format(have)) |
| 106 |
|
| 107 |
concat_examples = { |
| 108 |
'TEST_CONCAT_1': '"abcdef"', |
| 109 |
'TEST_CONCAT_2': '1', |
| 110 |
'TEST_CONCAT_3': '1 2 3', |
| 111 |
'TEST_CONCAT_4': '"ab" 1 "cd"', |
| 112 |
'TEST_CONCAT_5': '"ab\"cd"', |
| 113 |
'TEST_CONCAT_6': '"ab\" \"cd"', |
| 114 |
} |
| 115 |
foreach def,expected : concat_examples |
| 116 |
have = cc.get_define(def, |
| 117 |
prefix : '#include <concat.h>', include_directories: include_directories('.')) |
| 118 |
assert(have == expected, '@0@ value is "@1@" instead of "@2@"'.format(def, have, expected)) |
| 119 |
endforeach |
| 120 |
endforeach |