| 1 |
project('has function', 'c', 'cpp') |
| 2 |
|
| 3 |
host_system = host_machine.system() |
| 4 |
|
| 5 |
# This is used in the `test_compiler_check_flags_order` unit test |
| 6 |
unit_test_args = '-I/tmp' |
| 7 |
defines_has_builtin = '''#ifndef __has_builtin |
| 8 |
#error "no __has_builtin" |
| 9 |
#endif |
| 10 |
''' |
| 11 |
compilers = [meson.get_compiler('c'), meson.get_compiler('cpp')] |
| 12 |
|
| 13 |
foreach cc : compilers |
| 14 |
if not cc.has_function('printf', prefix : '#include<stdio.h>', |
| 15 |
args : unit_test_args) |
| 16 |
error('"printf" function not found (should always exist).') |
| 17 |
endif |
| 18 |
|
| 19 |
# Should also be able to detect it without specifying the header |
| 20 |
# We check for a different function here to make sure the result is |
| 21 |
# not taken from a cache (ie. the check above) |
| 22 |
# On MSVC fprintf is defined as an inline function in the header, so it cannot |
| 23 |
# be found without the include. |
| 24 |
if not ['msvc', 'intel-cl'].contains(cc.get_id()) |
| 25 |
assert(cc.has_function('fprintf', args : unit_test_args), |
| 26 |
'"fprintf" function not found without include (on !msvc).') |
| 27 |
else |
| 28 |
assert(cc.has_function('fprintf', prefix : '#include <stdio.h>', |
| 29 |
args : unit_test_args), |
| 30 |
'"fprintf" function not found with include (on msvc).') |
| 31 |
# Compiler intrinsics |
| 32 |
assert(cc.has_function('strcmp'), |
| 33 |
'strcmp intrinsic should have been found on MSVC') |
| 34 |
assert(cc.has_function('strcmp', prefix : '#include <string.h>'), |
| 35 |
'strcmp intrinsic should have been found with #include on MSVC') |
| 36 |
endif |
| 37 |
|
| 38 |
if cc.has_function('hfkerhisadf', prefix : '#include<stdio.h>', |
| 39 |
args : unit_test_args) |
| 40 |
error('Found nonexistent function "hfkerhisadf".') |
| 41 |
endif |
| 42 |
|
| 43 |
if cc.has_function('hfkerhisadf', args : unit_test_args) |
| 44 |
error('Found nonexistent function "hfkerhisadf".') |
| 45 |
endif |
| 46 |
|
| 47 |
# With glibc (before 2.32, see below) on Linux, lchmod is a stub that will |
| 48 |
# always return an error, we want to detect that and declare that the |
| 49 |
# function is not available. |
| 50 |
# We can't check for the C library used here of course, but the main |
| 51 |
# alternative Linux C library (musl) doesn't use glibc's stub mechanism; |
| 52 |
# also, it has implemented lchmod since 2013, so it should be safe to check |
| 53 |
# that lchmod is available on Linux when not using glibc. |
| 54 |
if host_system == 'linux' or host_system == 'darwin' |
| 55 |
assert (cc.has_function('poll', prefix : '#include <poll.h>', |
| 56 |
args : unit_test_args), |
| 57 |
'couldn\'t detect "poll" when defined by a header') |
| 58 |
lchmod_prefix = '#include <sys/stat.h>\n#include <unistd.h>' |
| 59 |
has_lchmod = cc.has_function('lchmod', prefix : lchmod_prefix, args : unit_test_args) |
| 60 |
|
| 61 |
if host_system == 'linux' |
| 62 |
# __GLIBC__ macro can be retrieved by including almost any C library header |
| 63 |
glibc_major = cc.get_define('__GLIBC__', prefix: '#include <unistd.h>', args: unit_test_args) |
| 64 |
# __GLIBC__ will only be set for glibc |
| 65 |
if glibc_major != '' |
| 66 |
glibc_minor = cc.get_define('__GLIBC_MINOR__', prefix: '#include <unistd.h>', args: unit_test_args) |
| 67 |
glibc_vers = '@0@.@1@'.format(glibc_major, glibc_minor) |
| 68 |
message('GLIBC version:', glibc_vers) |
| 69 |
|
| 70 |
# lchmod was implemented in glibc 2.32 (https://sourceware.org/pipermail/libc-announce/2020/000029.html) |
| 71 |
if glibc_vers.version_compare('<2.32') |
| 72 |
assert (not has_lchmod, '"lchmod" check should have failed') |
| 73 |
else |
| 74 |
assert (has_lchmod, '"lchmod" check should have succeeded') |
| 75 |
endif |
| 76 |
else |
| 77 |
# Other C libraries for Linux should have lchmod |
| 78 |
assert (has_lchmod, '"lchmod" check should have succeeded') |
| 79 |
endif |
| 80 |
else |
| 81 |
# macOS and *BSD have lchmod |
| 82 |
assert (has_lchmod, '"lchmod" check should have succeeded') |
| 83 |
endif |
| 84 |
# Check that built-ins are found properly both with and without headers |
| 85 |
assert(cc.has_function('alloca', args : unit_test_args), |
| 86 |
'built-in alloca must be found on ' + host_system) |
| 87 |
assert(cc.has_function('alloca', prefix : '#include <alloca.h>', |
| 88 |
args : unit_test_args), |
| 89 |
'built-in alloca must be found with #include') |
| 90 |
if not cc.compiles(defines_has_builtin, args : unit_test_args) |
| 91 |
assert(not cc.has_function('alloca', |
| 92 |
prefix : '#include <alloca.h>\n#undef alloca', |
| 93 |
args : unit_test_args), |
| 94 |
'built-in alloca must not be found with #include and #undef') |
| 95 |
endif |
| 96 |
endif |
| 97 |
|
| 98 |
# For some functions one needs to define _GNU_SOURCE before including the |
| 99 |
# right headers to get them picked up. Make sure we can detect these functions |
| 100 |
# as well without any prefix |
| 101 |
if cc.has_header_symbol('sys/stat.h', 'statx', |
| 102 |
prefix : '#define _GNU_SOURCE', |
| 103 |
args : unit_test_args) |
| 104 |
assert (cc.has_function('statx', args : unit_test_args), |
| 105 |
'Failed to detect function "statx" (should always exist).') |
| 106 |
endif |
| 107 |
|
| 108 |
# We should be able to find GCC and Clang __builtin functions |
| 109 |
if ['gcc', 'clang'].contains(cc.get_id()) |
| 110 |
# __builtin_constant_p is documented to exist at least as far back as |
| 111 |
# GCC 2.95.3 |
| 112 |
assert(cc.has_function('__builtin_constant_p', args : unit_test_args), |
| 113 |
'__builtin_constant_p must be found under gcc and clang') |
| 114 |
endif |
| 115 |
endforeach |