Don't use sysconf(_SC_NGROUPS_MAX) on macOS for getgroups()
[samba.git] / buildtools / wafsamba / wscript
1 #!/usr/bin/env python
2
3 # this is a base set of waf rules that everything else pulls in first
4
5 import os, sys
6 from waflib import Configure, Logs, Options, Utils, Context, Errors
7 import wafsamba
8 from samba_utils import symlink
9 from optparse import SUPPRESS_HELP
10
11 # this forces configure to be re-run if any of the configure
12 # sections of the build scripts change. We have to check
13 # for this in sys.argv as options have not yet been parsed when
14 # we need to set this. This is off by default until some issues
15 # are resolved related to WAFCACHE. It will need a lot of testing
16 # before it is enabled by default.
17 if '--enable-auto-reconfigure' in sys.argv:
18     Configure.autoconfig = 'clobber'
19
20 def default_value(option, default=''):
21     if option in Options.options.__dict__:
22         return Options.options.__dict__[option]
23     return default
24
25 def options(opt):
26     opt.load('compiler_cc')
27
28     opt.load('gnu_dirs')
29
30     gr = opt.option_group('library handling options')
31
32     gr.add_option('--bundled-libraries',
33                    help=("comma separated list of bundled libraries. May include !LIBNAME to disable bundling a library. Can be 'NONE' or 'ALL' [auto]"),
34                    action="store", dest='BUNDLED_LIBS', default='')
35
36     gr.add_option('--private-libraries',
37                    help=("comma separated list of normally public libraries to build instead as private libraries. May include !LIBNAME to disable making a library private. Can be 'NONE' or 'ALL' [auto]"),
38                    action="store", dest='PRIVATE_LIBS', default='')
39
40     extension_default = default_value('PRIVATE_EXTENSION_DEFAULT')
41     gr.add_option('--private-library-extension',
42                    help=("name extension for private libraries [%s]" % extension_default),
43                    action="store", dest='PRIVATE_EXTENSION', default=extension_default)
44
45     extension_exception = default_value('PRIVATE_EXTENSION_EXCEPTION')
46     gr.add_option('--private-extension-exception',
47                    help=("comma separated list of libraries to not apply extension to [%s]" % extension_exception),
48                    action="store", dest='PRIVATE_EXTENSION_EXCEPTION', default=extension_exception)
49
50     builtin_default = default_value('BUILTIN_LIBRARIES_DEFAULT')
51     gr.add_option('--builtin-libraries',
52                    help=("command separated list of libraries to build directly into binaries [%s]" % builtin_default),
53                    action="store", dest='BUILTIN_LIBRARIES', default=builtin_default)
54
55     gr.add_option('--minimum-library-version',
56                    help=("list of minimum system library versions (LIBNAME1:version,LIBNAME2:version)"),
57                    action="store", dest='MINIMUM_LIBRARY_VERSION', default='')
58
59     gr.add_option('--disable-rpath',
60                    help=("Disable use of rpath for build binaries"),
61                    action="store_true", dest='disable_rpath_build', default=False)
62     gr.add_option('--disable-rpath-install',
63                    help=("Disable use of rpath for library path in installed files"),
64                    action="store_true", dest='disable_rpath_install', default=False)
65     gr.add_option('--disable-rpath-private-install',
66                    help=("Disable use of rpath for private library path in installed files"),
67                    action="store_true", dest='disable_rpath_private_install', default=False)
68     gr.add_option('--nonshared-binary',
69                    help=("Disable use of shared libs for the listed binaries"),
70                    action="store", dest='NONSHARED_BINARIES', default='')
71     gr.add_option('--disable-symbol-versions',
72                    help=("Disable use of the --version-script linker option"),
73                    action="store_true", dest='disable_symbol_versions', default=False)
74
75     opt.add_option('--with-modulesdir',
76                    help=("modules directory [PREFIX/modules]"),
77                    action="store", dest='MODULESDIR', default='${PREFIX}/modules')
78
79     opt.add_option('--with-privatelibdir',
80                    help=("private library directory [PREFIX/lib/%s]" % Context.g_module.APPNAME),
81                    action="store", dest='PRIVATELIBDIR', default=None)
82
83     opt.add_option('--with-libiconv',
84                    help='additional directory to search for libiconv',
85                    action='store', dest='iconv_open', default='/usr/local',
86                    match = ['Checking for library iconv', 'Checking for iconv_open', 'Checking for header iconv.h'])
87     opt.add_option('--without-gettext',
88                    help=("Disable use of gettext"),
89                    action="store_true", dest='disable_gettext', default=False)
90
91     gr = opt.option_group('developer options')
92
93     gr.add_option('-C',
94                    help='enable configure caching',
95                    action='store_true', dest='enable_configure_cache')
96     gr.add_option('--enable-auto-reconfigure',
97                    help='enable automatic reconfigure on build',
98                    action='store_true', dest='enable_auto_reconfigure')
99     gr.add_option('--enable-debug',
100                    help=("Turn on debugging symbols"),
101                    action="store_true", dest='debug', default=False)
102     gr.add_option('--enable-developer',
103                    help=("Turn on developer warnings and debugging"),
104                    action="store_true", dest='developer', default=False)
105     gr.add_option('--pidl-developer',
106                    help=("annotate PIDL-generated code for developers"),
107                    action="store_true", dest='pidl_developer', default=False)
108     gr.add_option('--disable-warnings-as-errors',
109                    help=("Do not treat all warnings as errors (disable -Werror)"),
110                    action="store_true", dest='disable_warnings_as_errors', default=False)
111     opt.add_option('--enable-coverage',
112                    help=("enable options necessary for code coverage "
113                          "reporting on selftest (default=no)"),
114                    action="store_true", dest='enable_coverage', default=False)
115     gr.add_option('--fatal-errors',
116                    help=("Stop compilation on first error (enable -Wfatal-errors)"),
117                    action="store_true", dest='fatal_errors', default=False)
118     gr.add_option('--enable-gccdeps',
119                    help=("Enable use of gcc -MD dependency module"),
120                    action="store_true", dest='enable_gccdeps', default=True)
121     gr.add_option('--pedantic',
122                    help=("Enable even more compiler warnings"),
123                    action='store_true', dest='pedantic', default=False)
124     gr.add_option('--git-local-changes',
125                    help=("mark version with + if local git changes"),
126                    action='store_true', dest='GIT_LOCAL_CHANGES', default=False)
127     gr.add_option('--address-sanitizer',
128                    help=("Enable address sanitizer compile and linker flags"),
129                    action="store_true", dest='address_sanitizer', default=False)
130     gr.add_option('--undefined-sanitizer',
131         help=("Enable undefined behaviour sanitizer compile and linker flags"),
132         action="store_true",
133         dest='undefined_sanitizer',
134         default=False)
135     gr.add_option('--enable-libfuzzer',
136                   help=("Build fuzzing binaries (use ADDITIONAL_CFLAGS to specify compiler options for libFuzzer or use CC=honggfuzz/hfuzz-cc)"),
137                   action="store_true", dest='enable_libfuzzer', default=False)
138     gr.add_option('--enable-afl-fuzzer',
139                   help=("Build fuzzing binaries AFL-style (typically use with CC=afl-gcc)"),
140                   action="store_true", dest='enable_afl_fuzzer', default=False)
141
142     # Fuzz targets may need additional LDFLAGS that we can't use on
143     # internal binaries like asn1_compile
144
145     gr.add_option('--fuzz-target-ldflags',
146                   help=("Linker flags to be used when building fuzz targets"),
147                   action="store", dest='FUZZ_TARGET_LDFLAGS', default='')
148
149     gr.add_option('--abi-check',
150                    help=("Check ABI signatures for libraries"),
151                    action='store_true', dest='ABI_CHECK', default=False)
152     gr.add_option('--abi-check-disable',
153                    help=("Disable ABI checking (used with --enable-developer)"),
154                    action='store_true', dest='ABI_CHECK_DISABLE', default=False)
155     gr.add_option('--abi-update',
156                    help=("Update ABI signature files for libraries"),
157                    action='store_true', dest='ABI_UPDATE', default=False)
158
159     gr.add_option('--show-deps',
160                    help=("Show dependency tree for the given target"),
161                    dest='SHOWDEPS', default='')
162
163     gr.add_option('--symbol-check',
164                   help=("check symbols in object files against project rules"),
165                   action='store_true', dest='SYMBOLCHECK', default=False)
166
167     gr.add_option('--dup-symbol-check',
168                   help=("check for duplicate symbols in object files and system libs (must be configured with --enable-developer)"),
169                   action='store_true', dest='DUP_SYMBOLCHECK', default=False)
170
171     gr.add_option('--why-needed',
172                   help=("TARGET:DEPENDENCY check why TARGET needs DEPENDENCY"),
173                   action='store', type='str', dest='WHYNEEDED', default=None)
174
175     gr.add_option('--show-duplicates',
176                   help=("Show objects which are included in multiple binaries or libraries"),
177                   action='store_true', dest='SHOW_DUPLICATES', default=False)
178
179     gr = opt.add_option_group('cross compilation options')
180
181     gr.add_option('--cross-compile',
182                    help=("configure for cross-compilation"),
183                    action='store_true', dest='CROSS_COMPILE', default=False)
184     gr.add_option('--cross-execute',
185                    help=("command prefix to use for cross-execution in configure"),
186                    action='store', dest='CROSS_EXECUTE', default='')
187     gr.add_option('--cross-answers',
188                    help=("answers to cross-compilation configuration (auto modified)"),
189                    action='store', dest='CROSS_ANSWERS', default='')
190     gr.add_option('--hostcc',
191                    help=("set host compiler when cross compiling"),
192                    action='store', dest='HOSTCC', default=False)
193
194     # we use SUPPRESS_HELP for these, as they are ignored, and are there only
195     # to allow existing RPM spec files to work
196     opt.add_option('--build',
197                    help=SUPPRESS_HELP,
198                    action='store', dest='AUTOCONF_BUILD', default='')
199     opt.add_option('--host',
200                    help=SUPPRESS_HELP,
201                    action='store', dest='AUTOCONF_HOST', default='')
202     opt.add_option('--target',
203                    help=SUPPRESS_HELP,
204                    action='store', dest='AUTOCONF_TARGET', default='')
205     opt.add_option('--program-prefix',
206                    help=SUPPRESS_HELP,
207                    action='store', dest='AUTOCONF_PROGRAM_PREFIX', default='')
208     opt.add_option('--disable-dependency-tracking',
209                    help=SUPPRESS_HELP,
210                    action='store_true', dest='AUTOCONF_DISABLE_DEPENDENCY_TRACKING', default=False)
211     opt.add_option('--disable-silent-rules',
212                    help=SUPPRESS_HELP,
213                    action='store_true', dest='AUTOCONF_DISABLE_SILENT_RULES', default=False)
214
215     gr = opt.option_group('dist options')
216     gr.add_option('--sign-release',
217                    help='sign the release tarball created by waf dist',
218                    action='store_true', dest='SIGN_RELEASE')
219     gr.add_option('--tag',
220                    help='tag release in git at the same time',
221                    type='string', action='store', dest='TAG_RELEASE')
222
223     opt.add_option('--disable-python',
224                     help='do not generate python modules',
225                     action='store_true', dest='disable_python', default=False)
226
227
228 @Utils.run_once
229 def configure(conf):
230     conf.env.hlist = []
231     conf.env.srcdir = conf.srcnode.abspath()
232
233     conf.define('SRCDIR', conf.env['srcdir'])
234
235     conf.SETUP_CONFIGURE_CACHE(Options.options.enable_configure_cache)
236
237     # load our local waf extensions
238     conf.load('gnu_dirs')
239     conf.load('wafsamba')
240
241     conf.CHECK_CC_ENV()
242
243     conf.load('compiler_c')
244
245     conf.CHECK_STANDARD_LIBPATH()
246
247     # we need git for 'waf dist'
248     conf.find_program('git', var='GIT')
249
250     # older gcc versions (< 4.4) does not work with gccdeps, so we have to see if the .d file is generated
251     if Options.options.enable_gccdeps:
252         # stale file removal - the configuration may pick up the old .pyc file
253         p = os.path.join(conf.env.srcdir, 'buildtools/wafsamba/gccdeps.pyc')
254         if os.path.exists(p):
255             os.remove(p)
256         conf.load('gccdeps')
257
258     # make the install paths available in environment
259     conf.env.LIBDIR = Options.options.LIBDIR or '${PREFIX}/lib'
260     conf.env.BINDIR = Options.options.BINDIR or '${PREFIX}/bin'
261     conf.env.SBINDIR = Options.options.SBINDIR or '${PREFIX}/sbin'
262     conf.env.MODULESDIR = Options.options.MODULESDIR
263     conf.env.PRIVATELIBDIR = Options.options.PRIVATELIBDIR
264     conf.env.BUNDLED_LIBS = Options.options.BUNDLED_LIBS.split(',')
265     conf.env.SYSTEM_LIBS = ()
266     conf.env.PRIVATE_LIBS = Options.options.PRIVATE_LIBS.split(',')
267     conf.env.BUILTIN_LIBRARIES = Options.options.BUILTIN_LIBRARIES.split(',')
268     conf.env.NONSHARED_BINARIES = Options.options.NONSHARED_BINARIES.split(',')
269
270     conf.env.PRIVATE_EXTENSION = Options.options.PRIVATE_EXTENSION
271     conf.env.PRIVATE_EXTENSION_EXCEPTION = Options.options.PRIVATE_EXTENSION_EXCEPTION.split(',')
272
273     conf.env.CROSS_COMPILE = Options.options.CROSS_COMPILE
274     conf.env.CROSS_EXECUTE = Options.options.CROSS_EXECUTE
275     conf.env.CROSS_ANSWERS = Options.options.CROSS_ANSWERS
276     conf.env.HOSTCC        = Options.options.HOSTCC
277
278     conf.env.AUTOCONF_BUILD = Options.options.AUTOCONF_BUILD
279     conf.env.AUTOCONF_HOST  = Options.options.AUTOCONF_HOST
280     conf.env.AUTOCONF_PROGRAM_PREFIX = Options.options.AUTOCONF_PROGRAM_PREFIX
281
282     conf.env.disable_python = Options.options.disable_python
283
284     if (conf.env.AUTOCONF_HOST and
285         conf.env.AUTOCONF_BUILD and
286         conf.env.AUTOCONF_BUILD != conf.env.AUTOCONF_HOST):
287         Logs.error('ERROR: Mismatch between --build and --host. Please use --cross-compile instead')
288         sys.exit(1)
289     if conf.env.AUTOCONF_PROGRAM_PREFIX:
290         Logs.error('ERROR: --program-prefix not supported')
291         sys.exit(1)
292
293     # enable ABI checking for developers
294     conf.env.ABI_CHECK = Options.options.ABI_CHECK or Options.options.developer
295     if Options.options.ABI_CHECK_DISABLE:
296         conf.env.ABI_CHECK = False
297     try:
298         conf.find_program('gdb', mandatory=True)
299     except:
300         conf.env.ABI_CHECK = False
301
302     conf.env.enable_coverage = Options.options.enable_coverage
303     if conf.env.enable_coverage:
304         conf.ADD_LDFLAGS('-lgcov', testflags=True)
305         conf.ADD_CFLAGS('--coverage', testflags=True)
306         # disable abi check for coverage, otherwise ld will fail
307         conf.env.ABI_CHECK = False
308
309     conf.env.GIT_LOCAL_CHANGES = Options.options.GIT_LOCAL_CHANGES
310
311     conf.CHECK_UNAME()
312
313     # see if we can compile and run a simple C program
314     conf.CHECK_CODE('printf("hello world")',
315                     define='HAVE_SIMPLE_C_PROG',
316                     mandatory=True,
317                     execute=True,
318                     headers='stdio.h',
319                     msg='Checking simple C program')
320
321     # Try to find the right extra flags for -Werror behaviour
322     for f in ["-Werror",       # GCC
323               "-errwarn=%all", # Sun Studio
324               "-qhalt=w",     # IBM xlc
325               "-w2",           # Tru64
326              ]:
327         if conf.CHECK_CFLAGS([f]):
328             if not 'WERROR_CFLAGS' in conf.env:
329                 conf.env['WERROR_CFLAGS'] = []
330             conf.env['WERROR_CFLAGS'].extend([f])
331             break
332
333     # check which compiler/linker flags are needed for rpath support
334     if conf.CHECK_LDFLAGS(['-Wl,-rpath,.']):
335         conf.env['RPATH_ST'] = '-Wl,-rpath,%s'
336     elif conf.CHECK_LDFLAGS(['-Wl,-R,.']):
337         conf.env['RPATH_ST'] = '-Wl,-R,%s'
338
339     # check for rpath
340     if conf.CHECK_LIBRARY_SUPPORT(rpath=True):
341         support_rpath = True
342         conf.env.RPATH_ON_BUILD   = not Options.options.disable_rpath_build
343         conf.env.RPATH_ON_INSTALL = (conf.env.RPATH_ON_BUILD and
344                                      not Options.options.disable_rpath_install)
345         if not conf.env.PRIVATELIBDIR:
346             conf.env.PRIVATELIBDIR = '%s/%s' % (conf.env.LIBDIR, Context.g_module.APPNAME)
347         conf.env.RPATH_ON_INSTALL_PRIVATE = (
348             not Options.options.disable_rpath_private_install)
349     else:
350         support_rpath = False
351         conf.env.RPATH_ON_INSTALL = False
352         conf.env.RPATH_ON_BUILD   = False
353         conf.env.RPATH_ON_INSTALL_PRIVATE = False
354         if not conf.env.PRIVATELIBDIR:
355             # rpath is not possible so there is no sense in having a
356             # private library directory by default.
357             # the user can of course always override it.
358             conf.env.PRIVATELIBDIR = conf.env.LIBDIR
359
360     if (not Options.options.disable_symbol_versions and
361         conf.CHECK_LIBRARY_SUPPORT(rpath=support_rpath,
362                                    version_script=True,
363                                    msg='-Wl,--version-script support')):
364         conf.env.HAVE_LD_VERSION_SCRIPT = True
365     else:
366         conf.env.HAVE_LD_VERSION_SCRIPT = False
367
368     if conf.CHECK_CFLAGS(['-fvisibility=hidden']):
369         conf.env.VISIBILITY_CFLAGS = '-fvisibility=hidden'
370         conf.CHECK_CODE('''int main(void) { return 0; }
371                            __attribute__((visibility("default"))) void vis_foo2(void) {}\n''',
372                         cflags=conf.env.VISIBILITY_CFLAGS,
373                         strict=True,
374                         define='HAVE_VISIBILITY_ATTR', addmain=False)
375
376     # check HAVE_CONSTRUCTOR_ATTRIBUTE
377     conf.CHECK_CODE('''
378             void test_constructor_attribute(void) __attribute__ ((constructor));
379
380             void test_constructor_attribute(void)
381             {
382                 return;
383             }
384
385             int main(void) {
386                 return 0;
387             }
388             ''',
389             'HAVE_CONSTRUCTOR_ATTRIBUTE',
390             addmain=False,
391             strict=True,
392             msg='Checking for library constructor support')
393
394     # check HAVE_PRAGMA_INIT alternatively
395     if not conf.env.HAVE_CONSTRUCTOR_ATTRIBUTE:
396             conf.CHECK_CODE('''
397                 #pragma init (test_init)
398
399                 void test_init(void)
400                 {
401                     return;
402                 }
403
404                 int main(void) {
405                     return 0;
406                 }
407                 ''',
408                 'HAVE_PRAGMA_INIT',
409                 addmain=False,
410                 strict=True,
411                 msg='Checking for pragma init support')
412
413     # check HAVE_DESTRUCTOR_ATTRIBUTE
414     conf.CHECK_CODE('''
415             void test_destructor_attribute(void) __attribute__ ((destructor));
416
417             void test_destructor_attribute(void)
418             {
419                 return;
420             }
421
422             int main(void) {
423                 return 0;
424             }
425             ''',
426             'HAVE_DESTRUCTOR_ATTRIBUTE',
427             addmain=False,
428             strict=True,
429             msg='Checking for library destructor support')
430
431     # check HAVE_PRAGMA_FINI alternatively
432     if not conf.env.HAVE_DESTRUCTOR_ATTRIBUTE:
433             conf.CHECK_CODE('''
434                 #pragma fini (test_fini)
435
436                 void test_fini(void)
437                 {
438                     return;
439                 }
440
441                 int main(void) {
442                     return 0;
443                 }
444                 ''',
445                 'HAVE_PRAGMA_FINI',
446                 addmain=False,
447                 strict=True,
448                 msg='Checking for pragma fini support')
449
450     conf.CHECK_CODE('''
451             void test_attribute(void) __attribute__ (());
452
453             void test_attribute(void)
454             {
455                 return;
456             }
457
458             int main(void) {
459                 return 0;
460             }
461             ''',
462             'HAVE___ATTRIBUTE__',
463             addmain=False,
464             strict=True,
465             msg='Checking for __attribute__')
466
467     # Solaris by defauls uses draft versions of some functions unless you set _POSIX_PTHREAD_SEMANTICS
468     if sys.platform.startswith('sunos'):
469         conf.DEFINE('_POSIX_PTHREAD_SEMANTICS', 1)
470
471     if sys.platform.startswith('aix'):
472         conf.DEFINE('_ALL_SOURCE', 1, add_to_cflags=True)
473         # Might not be needed if ALL_SOURCE is defined
474         # conf.DEFINE('_XOPEN_SOURCE', 600, add_to_cflags=True)
475
476     # we should use the PIC options in waf instead
477     # Some compilo didn't support -fPIC but just print a warning
478     if conf.env['COMPILER_CC'] == "suncc":
479         conf.ADD_CFLAGS('-KPIC', testflags=True)
480         # we really want define here as we need to have this
481         # define even during the tests otherwise detection of
482         # boolean is broken
483         conf.DEFINE('_STDC_C99', 1, add_to_cflags=True)
484         conf.DEFINE('_XPG6', 1, add_to_cflags=True)
485     else:
486         conf.ADD_CFLAGS('-fPIC', testflags=True)
487
488     # On Solaris 8 with suncc (at least) the flags for the linker to define the name of the
489     # library are not always working (if the command line is very very long and with a lot
490     # files)
491
492     if conf.env['COMPILER_CC'] == "suncc":
493         save = conf.env['SONAME_ST']
494         conf.env['SONAME_ST'] = '-Wl,-h,%s'
495         if not conf.CHECK_SHLIB_INTRASINC_NAME_FLAGS("Checking if flags %s are ok" % conf.env['SONAME_ST']):
496             conf.env['SONAME_ST'] = save
497
498     conf.CHECK_INLINE()
499
500     # check for pkgconfig
501     conf.CHECK_CFG(atleast_pkgconfig_version='0.0.0')
502
503     conf.DEFINE('_GNU_SOURCE', 1, add_to_cflags=True)
504     conf.DEFINE('_XOPEN_SOURCE_EXTENDED', 1, add_to_cflags=True)
505
506     #
507     # Needs to be defined before std*.h and string*.h are included
508     # As Python.h already brings string.h we need it in CFLAGS.
509     # See memset_s() details here:
510     # https://en.cppreference.com/w/c/string/byte/memset
511     #
512     if conf.CHECK_CFLAGS(['-D__STDC_WANT_LIB_EXT1__=1']):
513         conf.ADD_CFLAGS('-D__STDC_WANT_LIB_EXT1__=1')
514
515     # on Tru64 certain features are only available with _OSF_SOURCE set to 1
516     # and _XOPEN_SOURCE set to 600
517     if conf.env['SYSTEM_UNAME_SYSNAME'] == 'OSF1':
518         conf.DEFINE('_OSF_SOURCE', 1, add_to_cflags=True)
519         conf.DEFINE('_XOPEN_SOURCE', 600, add_to_cflags=True)
520
521     # SCM_RIGHTS is only avail if _XOPEN_SOURCE iŃ• defined on IRIX
522     if conf.env['SYSTEM_UNAME_SYSNAME'] == 'IRIX':
523         conf.DEFINE('_XOPEN_SOURCE', 600, add_to_cflags=True)
524         conf.DEFINE('_BSD_TYPES', 1, add_to_cflags=True)
525
526     # Try to find the right extra flags for C99 initialisers
527     for f in ["", "-AC99", "-qlanglvl=extc99", "-qlanglvl=stdc99", "-c99"]:
528         if conf.CHECK_CFLAGS([f], '''
529 struct foo {int x;char y;};
530 struct foo bar = { .y = 'X', .x = 1 };
531 '''):
532             if f != "":
533                 conf.ADD_CFLAGS(f)
534             break
535
536     # get the base headers we'll use for the rest of the tests
537     conf.CHECK_HEADERS('stdio.h sys/types.h sys/stat.h stdlib.h stddef.h memory.h string.h',
538                        add_headers=True)
539     conf.CHECK_HEADERS('strings.h inttypes.h stdint.h unistd.h minix/config.h', add_headers=True)
540     conf.CHECK_HEADERS('ctype.h', add_headers=True)
541
542     if sys.platform == 'darwin':
543         conf.DEFINE('_DARWIN_C_SOURCE', 1, add_to_cflags=True)
544         conf.DEFINE('_DARWIN_UNLIMITED_GETGROUPS', 1, add_to_cflags=True)
545     else:
546         conf.CHECK_HEADERS('standards.h', add_headers=True)
547
548     conf.CHECK_HEADERS('stdbool.h stdint.h stdarg.h vararg.h', add_headers=True)
549     conf.CHECK_HEADERS('limits.h assert.h')
550
551     # see if we need special largefile flags
552     if not conf.CHECK_LARGEFILE():
553         raise Errors.WafError('Samba requires large file support support, but not available on this platform: sizeof(off_t) < 8')
554
555     if conf.env.HAVE_STDDEF_H and conf.env.HAVE_STDLIB_H:
556         conf.DEFINE('STDC_HEADERS', 1)
557
558     conf.CHECK_HEADERS('sys/time.h time.h', together=True)
559
560     if conf.env.HAVE_SYS_TIME_H and conf.env.HAVE_TIME_H:
561         conf.DEFINE('TIME_WITH_SYS_TIME', 1)
562
563     # cope with different extensions for libraries
564     (root, ext) = os.path.splitext(conf.env.cshlib_PATTERN)
565     if ext[0] == '.':
566         conf.define('SHLIBEXT', ext[1:], quote=True)
567     else:
568         conf.define('SHLIBEXT', "so", quote=True)
569
570     # First try a header check for cross-compile friendlyness
571     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
572                         #define B __BYTE_ORDER
573                         #elif defined(BYTE_ORDER)
574                         #define B BYTE_ORDER
575                         #endif
576
577                         #ifdef __LITTLE_ENDIAN
578                         #define LITTLE __LITTLE_ENDIAN
579                         #elif defined(LITTLE_ENDIAN)
580                         #define LITTLE LITTLE_ENDIAN
581                         #endif
582
583                         #if !defined(LITTLE) || !defined(B) || LITTLE != B
584                         #error Not little endian.
585                         #endif
586                         int main(void) { return 0; }\n""",
587                             addmain=False,
588                             headers="endian.h sys/endian.h",
589                             define="HAVE_LITTLE_ENDIAN")
590     conf.CHECK_CODE(code = """#ifdef __BYTE_ORDER
591                         #define B __BYTE_ORDER
592                         #elif defined(BYTE_ORDER)
593                         #define B BYTE_ORDER
594                         #endif
595
596                         #ifdef __BIG_ENDIAN
597                         #define BIG __BIG_ENDIAN
598                         #elif defined(BIG_ENDIAN)
599                         #define BIG BIG_ENDIAN
600                         #endif
601
602                         #if !defined(BIG) || !defined(B) || BIG != B
603                         #error Not big endian.
604                         #endif
605                         int main(void) { return 0; }\n""",
606                             addmain=False,
607                             headers="endian.h sys/endian.h",
608                             define="HAVE_BIG_ENDIAN")
609
610     if not conf.CONFIG_SET("HAVE_BIG_ENDIAN") and not conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
611         # That didn't work!  Do runtime test.
612         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
613             u.i = 0x01020304;
614             return u.c[0] == 0x04 && u.c[1] == 0x03 && u.c[2] == 0x02 && u.c[3] == 0x01 ? 0 : 1;""",
615                           addmain=True, execute=True,
616                           define='HAVE_LITTLE_ENDIAN',
617                           msg="Checking for HAVE_LITTLE_ENDIAN - runtime")
618         conf.CHECK_CODE("""union { int i; char c[sizeof(int)]; } u;
619             u.i = 0x01020304;
620             return u.c[0] == 0x01 && u.c[1] == 0x02 && u.c[2] == 0x03 && u.c[3] == 0x04 ? 0 : 1;""",
621                           addmain=True, execute=True,
622                           define='HAVE_BIG_ENDIAN',
623                           msg="Checking for HAVE_BIG_ENDIAN - runtime")
624
625     # Extra sanity check.
626     if conf.CONFIG_SET("HAVE_BIG_ENDIAN") == conf.CONFIG_SET("HAVE_LITTLE_ENDIAN"):
627         Logs.error("Failed endian determination.  The PDP-11 is back?")
628         sys.exit(1)
629     else:
630         if conf.CONFIG_SET("HAVE_BIG_ENDIAN"):
631             conf.DEFINE('WORDS_BIGENDIAN', 1)
632
633     # check if signal() takes a void function
634     if conf.CHECK_CODE('return *(signal (0, 0)) (0) == 1',
635                        define='RETSIGTYPE_INT',
636                        execute=False,
637                        headers='signal.h',
638                        msg='Checking if signal handlers return int'):
639         conf.DEFINE('RETSIGTYPE', 'int')
640     else:
641         conf.DEFINE('RETSIGTYPE', 'void')
642
643     conf.CHECK_VARIABLE('__FUNCTION__', define='HAVE_FUNCTION_MACRO')
644
645     conf.CHECK_CODE('va_list ap1,ap2; va_copy(ap1,ap2)',
646                     define="HAVE_VA_COPY",
647                     msg="Checking for va_copy")
648
649     conf.CHECK_CODE('''
650                     #define eprintf(...) fprintf(stderr, __VA_ARGS__)
651                     eprintf("bla", "bar")
652                     ''', define='HAVE__VA_ARGS__MACRO')
653
654     conf.env.enable_fuzzing = False
655
656     conf.env.enable_libfuzzer = Options.options.enable_libfuzzer
657     conf.env.enable_afl_fuzzer = Options.options.enable_afl_fuzzer
658     if conf.env.enable_libfuzzer or conf.env.enable_afl_fuzzer:
659         conf.env.enable_fuzzing = True
660         conf.DEFINE('FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION', 1)
661         conf.env.FUZZ_TARGET_LDFLAGS = Options.options.FUZZ_TARGET_LDFLAGS
662
663     # Create a symlink of the compile db for clangd
664     symlink(os.path.join(conf.bldnode.abspath(), 'default/compile_commands.json'),
665             os.path.join(conf.srcnode.abspath(), 'compile_commands.json'),
666             force=True)
667
668     conf.SAMBA_BUILD_ENV()
669
670
671 def build(bld):
672     # give a more useful message if the source directory has moved
673     curdir = bld.path.abspath()
674     srcdir = bld.srcnode.abspath()
675     relpath = os.path.relpath(curdir, srcdir)
676     if relpath.find('../') != -1:
677         Logs.error('bld.path %s is not a child of %s' % (curdir, srcdir))
678         raise Errors.WafError('''The top source directory has moved. Please run distclean and reconfigure''')
679
680     bld.SETUP_BUILD_GROUPS()
681     bld.ENFORCE_GROUP_ORDERING()
682     bld.CHECK_PROJECT_RULES()