build: find FILE_OFFSET_BITS via array
[garming/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     conf.CHECK_CODE('int __attribute__((cold)) func(int x) { return x; }',
11                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
12                     define='HAVE_ATTRIBUTE_COLD')
13     conf.CHECK_CODE('int __attribute__((const)) func(int x) { return x; }',
14                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
15                     define='HAVE_ATTRIBUTE_CONST')
16     conf.CHECK_CODE('void __attribute__((noreturn)) func(int x) { exit(x); }',
17                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
18                     define='HAVE_ATTRIBUTE_NORETURN')
19     conf.CHECK_CODE('void __attribute__((format(__printf__, 1, 2))) func(const char *fmt, ...) { }',
20                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
21                     define='HAVE_ATTRIBUTE_PRINTF')
22     conf.CHECK_CODE('int __attribute__((unused)) func(int x) { return x; }',
23                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
24                     define='HAVE_ATTRIBUTE_UNUSED')
25     conf.CHECK_CODE('int __attribute__((used)) func(int x) { return x; }',
26                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
27                     define='HAVE_ATTRIBUTE_USED')
28     # We try to use headers for a compile-time test.
29     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
30                         #define B __BYTE_ORDER
31                         #elif defined(BYTE_ORDER)
32                         #define B BYTE_ORDER
33                         #endif
34
35                         #ifdef __LITTLE_ENDIAN
36                         #define LITTLE __LITTLE_ENDIAN
37                         #elif defined(LITTLE_ENDIAN)
38                         #define LITTLE LITTLE_ENDIAN
39                         #endif
40
41                         #if !defined(LITTLE) || !defined(B) || LITTLE != B
42                         #error Not little endian.
43                         #endif""",
44                            headers="endian.h sys/endian.h",
45                            define="HAVE_LITTLE_ENDIAN")
46     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
47                         #define B __BYTE_ORDER
48                         #elif defined(BYTE_ORDER)
49                         #define B BYTE_ORDER
50                         #endif
51
52                         #ifdef __BIG_ENDIAN
53                         #define BIG __BIG_ENDIAN
54                         #elif defined(BIG_ENDIAN)
55                         #define BIG BIG_ENDIAN
56                         #endif
57
58                         #if !defined(BIG) || !defined(B) || BIG != B
59                         #error Not big endian.
60                         #endif""",
61                            headers="endian.h sys/endian.h",
62                            define="HAVE_BIG_ENDIAN")
63
64     if not conf.CONFIG_SET("HAVE_BIG_ENDIAN") and not conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
65         # That didn't work!  Do runtime test.
66         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
67           u.i = 0x01020304;
68           return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;""",
69                         addmain=True, execute=True,
70                         define='HAVE_LITTLE_ENDIAN',
71                         msg="Checking for HAVE_LITTLE_ENDIAN - runtime")
72         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
73           u.i = 0x01020304;
74           return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;""",
75                         addmain=True, execute=True,
76                         define='HAVE_BIG_ENDIAN',
77                         msg="Checking for HAVE_BIG_ENDIAN - runtime")
78
79     # Extra sanity check.
80     if conf.CONFIG_SET("HAVE_BIG_ENDIAN") == conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
81         Logs.error("Failed endian determination.  The PDP-11 is back?")
82         sys.exit(1)
83
84     conf.CHECK_CODE('return __builtin_choose_expr(1, 0, "garbage");',
85                     link=True,
86                     define='HAVE_BUILTIN_CHOOSE_EXPR')
87     conf.CHECK_CODE('return __builtin_clz(1) == (sizeof(int)*8 - 1) ? 0 : 1;',
88                     link=True,
89                     define='HAVE_BUILTIN_CLZ')
90     conf.CHECK_CODE('return __builtin_clzl(1) == (sizeof(long)*8 - 1) ? 0 : 1;',
91                     link=True,
92                     define='HAVE_BUILTIN_CLZL')
93     conf.CHECK_CODE('return __builtin_clzll(1) == (sizeof(long long)*8 - 1) ? 0 : 1;',
94                     link=True,
95                     define='HAVE_BUILTIN_CLZLL')
96     conf.CHECK_CODE('return __builtin_constant_p(1) ? 0 : 1;',
97                     link=True,
98                     define='HAVE_BUILTIN_CONSTANT_P')
99     conf.CHECK_CODE('return __builtin_expect(main != 0, 1) ? 0 : 1;',
100                     link=True,
101                     define='HAVE_BUILTIN_EXPECT')
102     conf.CHECK_CODE('return __builtin_popcountl(255L) == 8 ? 0 : 1;',
103                     link=True,
104                     define='HAVE_BUILTIN_POPCOUNTL')
105     conf.CHECK_CODE('return __builtin_types_compatible_p(char *, int) ? 1 : 0;',
106                     link=True,
107                     define='HAVE_BUILTIN_TYPES_COMPATIBLE_P')
108     conf.CHECK_CODE('int *foo = (int[]) { 1, 2, 3, 4 }; return foo[0] ? 0 : 1;',
109                     define='HAVE_COMPOUND_LITERALS')
110     conf.CHECK_CODE('struct foo { unsigned int x; int arr[]; };',
111                     addmain=False, link=False,
112                     define='HAVE_FLEXIBLE_ARRAY_MEMBER')
113     conf.CHECK_CODE("""#include <ctype.h>
114           int main(void) { return isblank(' ') ? 0 : 1; }""",
115                     link=True, addmain=False, add_headers=False,
116                     define='HAVE_ISBLANK')
117     conf.CHECK_CODE('int x = 1; __typeof__(x) i; i = x; return i == x ? 0 : 1;',
118                     link=True,
119                     define='HAVE_TYPEOF')
120     conf.CHECK_CODE('int __attribute__((warn_unused_result)) func(int x) { return x; }',
121                     addmain=False, link=False, cflags=conf.env['WERROR_CFLAGS'],
122                     define='HAVE_WARN_UNUSED_RESULT')
123
124     # backtrace could be in libexecinfo or in libc
125     conf.CHECK_FUNCS_IN('backtrace backtrace_symbols', 'execinfo', checklibc=True, headers='execinfo.h')
126
127     # Only check for FILE_OFFSET_BITS=64 if off_t is normally small:
128     # use raw routines because wrappers include previous _GNU_SOURCE
129     # or _FILE_OFFSET_BITS defines.
130     # The math for these tests is:
131     # array[-1 * !((int)(condition)) ] (condition is true) = array[0] = builds
132     # array[-1 * !((int)(condition)) ] (condition is false) = array[-1] = fails
133     conf.check(fragment="""#include <sys/types.h>
134                int main(void) { static int test_array[1 - 2 * !(((long int)(sizeof(off_t))) < 8)]; }""",
135                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) { static int test_array[1 - 2 * !(((long int)(sizeof(off_t))) >= 8)]; }""",
141                    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)