ccan: check for all the used config.h defines
[sfrench/samba-autobuild/.git] / lib / ccan / wscript
1 #!/usr/bin/env python
2
3 import Logs, sys, Options
4
5 def configure(conf):
6     conf.DEFINE('HAVE_CCAN', 1)
7     conf.CHECK_HEADERS('err.h')
8     conf.CHECK_HEADERS('byteswap.h')
9     conf.CHECK_FUNCS('bswap_64', link=False, headers="byteswap.h")
10
11     # FIXME: if they don't have -Werror, these will all fail.  But they
12     # probably will anyway...
13     conf.CHECK_CODE('int __attribute__((cold)) func(int x) { return x; }',
14                     addmain=False, link=False, cflags="-Werror",
15                     define='HAVE_ATTRIBUTE_COLD')
16     conf.CHECK_CODE('int __attribute__((const)) func(int x) { return x; }',
17                     addmain=False, link=False, cflags="-Werror",
18                     define='HAVE_ATTRIBUTE_CONST')
19     conf.CHECK_CODE('void __attribute__((noreturn)) func(int x) { exit(x); }',
20                     addmain=False, link=False, cflags="-Werror",
21                     define='HAVE_ATTRIBUTE_NORETURN')
22     conf.CHECK_CODE('void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }',
23                     addmain=False, link=False, cflags="-Werror",
24                     define='HAVE_ATTRIBUTE_PRINTF')
25     conf.CHECK_CODE('int __attribute__((unused)) func(int x) { return x; }',
26                     addmain=False, link=False, cflags="-Werror",
27                     define='HAVE_ATTRIBUTE_UNUSED')
28     conf.CHECK_CODE('int __attribute__((used)) func(int x) { return x; }',
29                     addmain=False, link=False, cflags="-Werror",
30                     define='HAVE_ATTRIBUTE_USED')
31     # We try to use headers for a compile-time test.
32     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
33                         #define B __BYTE_ORDER
34                         #elif defined(BYTE_ORDER)
35                         #define B BYTE_ORDER
36                         #endif
37
38                         #ifdef __LITTLE_ENDIAN
39                         #define LITTLE __LITTLE_ENDIAN
40                         #elif defined(LITTLE_ENDIAN)
41                         #define LITTLE LITTLE_ENDIAN
42                         #endif
43
44                         #if !defined(LITTLE) || !defined(B) || LITTLE != B
45                         #error Not little endian.
46                         #endif""",
47                            headers="endian.h sys/endian.h",
48                            define="HAVE_LITTLE_ENDIAN")
49     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
50                         #define B __BYTE_ORDER
51                         #elif defined(BYTE_ORDER)
52                         #define B BYTE_ORDER
53                         #endif
54
55                         #ifdef __BIG_ENDIAN
56                         #define BIG __BIG_ENDIAN
57                         #elif defined(BIG_ENDIAN)
58                         #define BIG BIG_ENDIAN
59                         #endif
60
61                         #if !defined(BIG) || !defined(B) || BIG != B
62                         #error Not big endian.
63                         #endif""",
64                            headers="endian.h sys/endian.h",
65                            define="HAVE_BIG_ENDIAN")
66
67     if not conf.CONFIG_SET("HAVE_BIG_ENDIAN") and not conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
68         # That didn't work!  Do runtime test.
69         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
70           u.i = 0x01020304;
71           return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;""",
72                         addmain=True, execute=True,
73                         define='HAVE_LITTLE_ENDIAN',
74                         msg="Checking for HAVE_LITTLE_ENDIAN - runtime")
75         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
76           u.i = 0x01020304;
77           return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;""",
78                         addmain=True, execute=True,
79                         define='HAVE_BIG_ENDIAN',
80                         msg="Checking for HAVE_BIG_ENDIAN - runtime")
81
82     # Extra sanity check.
83     if conf.CONFIG_SET("HAVE_BIG_ENDIAN") == conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
84         Logs.error("Failed endian determination.  The PDP-11 is back?")
85         sys.exit(1)
86
87     conf.CHECK_CODE('return __builtin_choose_expr(1, 0, "garbage");',
88                     link=True,
89                     define='HAVE_BUILTIN_CHOOSE_EXPR')
90     conf.CHECK_CODE('return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;',
91                     link=True,
92                     define='HAVE_BUILTIN_CLZ')
93     conf.CHECK_CODE('return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;',
94                     link=True,
95                     define='HAVE_BUILTIN_CLZL')
96     conf.CHECK_CODE('return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;',
97                     link=True,
98                     define='HAVE_BUILTIN_CLZLL')
99     conf.CHECK_CODE('return __builtin_constant_p(1) ? 0 : 1;',
100                     link=True,
101                     define='HAVE_BUILTIN_CONSTANT_P')
102     conf.CHECK_CODE('return __builtin_expect(main != 0, 1) ? 0 : 1;',
103                     link=True,
104                     define='HAVE_BUILTIN_EXPECT')
105     conf.CHECK_CODE('return __builtin_popcountl(255L) == 8 ? 0 : 1;',
106                     link=True,
107                     define='HAVE_BUILTIN_POPCOUNTL')
108     conf.CHECK_CODE('return __builtin_types_compatible_p(char *, int) ? 1 : 0;',
109                     link=True,
110                     define='HAVE_BUILTIN_TYPES_COMPATIBLE_P')
111     conf.CHECK_CODE('int *foo = (int[]) { 1, 2, 3, 4 }; return foo[0] ? 0 : 1;',
112                     define='HAVE_COMPOUND_LITERALS')
113     conf.CHECK_CODE('struct foo { unsigned int x; int arr[]; };',
114                     addmain=False, link=False,
115                     define='HAVE_FLEXIBLE_ARRAY_MEMBER')
116     conf.CHECK_CODE("""#include <ctype.h>
117           int main(void) { return isblank(' ') ? 0 : 1; }""",
118                     link=True, addmain=False, add_headers=False,
119                     define='HAVE_ISBLANK')
120     conf.CHECK_CODE('int x = 1; __typeof__(x) i; i = x; return i == x ? 0 : 1;',
121                     link=True,
122                     define='HAVE_TYPEOF')
123     conf.CHECK_CODE('int __attribute__((warn_unused_result)) func(int x) { return x; }',
124                     addmain=False, link=False, cflags="-Werror",
125                     define='HAVE_WARN_UNUSED_RESULT')
126
127     # backtrace could be in libexecinfo or in libc
128     conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo', checklibc=True, headers='execinfo.h')
129
130     # Only check for FILE_OFFSET_BITS=64 if off_t is normally small:
131     # use raw routines because wrappers include previous _GNU_SOURCE
132     # or _FILE_OFFSET_BITS defines.
133     conf.check(fragment="""#include <sys/types.h>
134                int main(void) { return !(sizeof(off_t) < 8); }""",
135                execute=True, msg='Checking for small off_t',
136                define_name='SMALL_OFF_T')
137     # Unreliable return value above, hence use define.
138     if conf.CONFIG_SET('SMALL_OFF_T'):
139         conf.check(fragment="""#include <sys/types.h>
140                    int main(void) { return !(sizeof(off_t) >= 8); }""",
141                    execute=True, msg='Checking for -D_FILE_OFFSET_BITS=64',
142                    ccflags='-D_FILE_OFFSET_BITS=64',
143                    define_name='HAVE_FILE_OFFSET_BITS')
144
145 def ccan_module(bld, name, deps=''):
146     bld.SAMBA_SUBSYSTEM('ccan-%s' % name,
147                         source=bld.path.ant_glob('%s/*.c' % name),
148                         deps=deps)
149     bld.env.CCAN_MODS += 'ccan-%s ' % name
150
151 def build(bld):
152     bld.env.CCAN_MODS = ""
153
154     # These have actual C files.
155     ccan_module(bld, 'hash', 'ccan-build_assert')
156     ccan_module(bld, 'ilog', 'ccan-compiler');
157     ccan_module(bld, 'read_write_all')
158     ccan_module(bld, 'str', 'ccan-build_assert')
159     ccan_module(bld, 'tally', 'ccan-build_assert ccan-likely')
160
161     # These are headers only.
162     ccan_module(bld, 'array_size', 'ccan-build_assert')
163     ccan_module(bld, 'asearch','ccan-typesafe_cb ccan-array_size')
164     ccan_module(bld, 'build_assert')
165     ccan_module(bld, 'cast', 'ccan-build_assert')
166     ccan_module(bld, 'check_type', 'ccan-build_assert')
167     ccan_module(bld, 'compiler')
168     ccan_module(bld, 'endian')
169     ccan_module(bld, 'likely', 'ccan-str')
170     ccan_module(bld, 'typesafe_cb')
171     ccan_module(bld, 'err', 'ccan-compiler')
172
173     # Failtest pulls in a lot of stuff, and it's only for unit tests.
174     if bld.env.DEVELOPER_MODE:
175         ccan_module(bld, 'container_of', 'ccan-check_type')
176         ccan_module(bld, 'htable', 'ccan-compiler')
177         ccan_module(bld, 'list', 'ccan-container_of')
178         ccan_module(bld, 'time')
179         ccan_module(bld, 'tcon')
180         ccan_module(bld, 'tlist', 'ccan-list ccan-tcon')
181         ccan_module(bld, 'failtest',
182                     '''
183                     ccan-err ccan-hash ccan-htable ccan-list
184                     ccan-read_write_all ccan-str ccan-time execinfo
185                     ''')
186
187     # This is the complete CCAN collection as one group.
188     bld.SAMBA_LIBRARY('ccan',
189                       source='',
190                       deps=bld.env.CCAN_MODS,
191                       private_library=True,
192                       grouping_library=True)