ldb: Update ldb.set_opaque() to accept only supported types
[Samba.git] / buildtools / wafsamba / samba_conftests.py
blob38ce20d968f0b35857cce1a80ea25d57dcc90253
1 # a set of config tests that use the samba_autoconf functions
2 # to test for commonly needed configuration options
4 import os, shutil, re
5 from waflib import Build, Configure, Utils, Options, Logs, Errors
6 from waflib.Configure import conf
7 from samba_utils import TO_LIST, ADD_LD_LIBRARY_PATH, get_string
10 def add_option(self, *k, **kw):
11 '''syntax help: provide the "match" attribute to opt.add_option() so that folders can be added to specific config tests'''
12 Options.OptionsContext.parser = self
13 match = kw.get('match', [])
14 if match:
15 del kw['match']
16 opt = self.parser.add_option(*k, **kw)
17 opt.match = match
18 return opt
19 Options.OptionsContext.add_option = add_option
21 @conf
22 def check(self, *k, **kw):
23 '''Override the waf defaults to inject --with-directory options'''
25 if not 'env' in kw:
26 kw['env'] = self.env.derive()
28 # match the configuration test with specific options, for example:
29 # --with-libiconv -> Options.options.iconv_open -> "Checking for library iconv"
30 additional_dirs = []
31 if 'msg' in kw:
32 msg = kw['msg']
33 for x in Options.OptionsContext.parser.parser.option_list:
34 if getattr(x, 'match', None) and msg in x.match:
35 d = getattr(Options.options, x.dest, '')
36 if d:
37 additional_dirs.append(d)
39 # we add the additional dirs twice: once for the test data, and again if the compilation test succeeds below
40 def add_options_dir(dirs, env):
41 for x in dirs:
42 if not x in env.CPPPATH:
43 env.CPPPATH = [os.path.join(x, 'include')] + env.CPPPATH
44 if not x in env.LIBPATH:
45 env.LIBPATH = [os.path.join(x, 'lib')] + env.LIBPATH
47 add_options_dir(additional_dirs, kw['env'])
49 self.validate_c(kw)
50 self.start_msg(kw['msg'])
51 ret = None
52 try:
53 ret = self.run_c_code(*k, **kw)
54 except Configure.ConfigurationError as e:
55 self.end_msg(kw['errmsg'], 'YELLOW')
56 if 'mandatory' in kw and kw['mandatory']:
57 if Logs.verbose > 1:
58 raise
59 else:
60 self.fatal('the configuration failed (see %r)' % self.log.name)
61 else:
62 kw['success'] = ret
63 self.end_msg(self.ret_msg(kw['okmsg'], kw))
65 # success! keep the CPPPATH/LIBPATH
66 add_options_dir(additional_dirs, self.env)
68 self.post_check(*k, **kw)
69 if not kw.get('execute', False):
70 return ret == 0
71 return ret
74 @conf
75 def CHECK_ICONV(conf, define='HAVE_NATIVE_ICONV'):
76 '''check if the iconv library is installed
77 optionally pass a define'''
78 if conf.CHECK_FUNCS_IN('iconv_open', 'iconv', checklibc=True, headers='iconv.h'):
79 conf.DEFINE(define, 1)
80 return True
81 return False
84 @conf
85 def CHECK_LARGEFILE(conf, define='HAVE_LARGEFILE'):
86 '''see what we need for largefile support'''
87 getconf_cflags = conf.CHECK_COMMAND(['getconf', 'LFS_CFLAGS'])
88 if getconf_cflags is not False:
89 if (conf.CHECK_CODE('if (sizeof(off_t) < 8) return 1',
90 define='WORKING_GETCONF_LFS_CFLAGS',
91 execute=True,
92 cflags=getconf_cflags,
93 msg='Checking getconf large file support flags work')):
94 conf.ADD_CFLAGS(getconf_cflags)
95 getconf_cflags_list=TO_LIST(getconf_cflags)
96 for flag in getconf_cflags_list:
97 if flag[:2] == "-D":
98 flag_split = flag[2:].split('=')
99 if len(flag_split) == 1:
100 conf.DEFINE(flag_split[0], '1')
101 else:
102 conf.DEFINE(flag_split[0], flag_split[1])
104 if conf.CHECK_CODE('if (sizeof(off_t) < 8) return 1',
105 define,
106 execute=True,
107 msg='Checking for large file support without additional flags'):
108 return True
110 if conf.CHECK_CODE('if (sizeof(off_t) < 8) return 1',
111 define,
112 execute=True,
113 cflags='-D_FILE_OFFSET_BITS=64',
114 msg='Checking for -D_FILE_OFFSET_BITS=64'):
115 conf.DEFINE('_FILE_OFFSET_BITS', 64)
116 return True
118 if conf.CHECK_CODE('if (sizeof(off_t) < 8) return 1',
119 define,
120 execute=True,
121 cflags='-D_LARGE_FILES',
122 msg='Checking for -D_LARGE_FILES'):
123 conf.DEFINE('_LARGE_FILES', 1)
124 return True
125 return False
128 @conf
129 def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None, msg=None, lib=None):
130 '''verify that a C prototype matches the one on the current system'''
131 if not conf.CHECK_DECLS(function, headers=headers):
132 return False
133 if not msg:
134 msg = 'Checking C prototype for %s' % function
135 return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
136 define=define,
137 local_include=False,
138 headers=headers,
139 link=False,
140 execute=False,
141 msg=msg,
142 lib=lib)
145 @conf
146 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
147 '''check that a named charset is able to be used with iconv_open() for conversion
148 to a target charset
150 msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
151 if define is None:
152 define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
153 return conf.CHECK_CODE('''
154 iconv_t cd = iconv_open("%s", "%s");
155 if (cd == 0 || cd == (iconv_t)-1) return -1;
156 ''' % (charset, outcharset),
157 define=define,
158 execute=True,
159 msg=msg,
160 lib='iconv',
161 headers=headers)
163 def find_config_dir(conf):
164 '''find a directory to run tests in'''
165 k = 0
166 while k < 10000:
167 dir = os.path.join(conf.bldnode.abspath(), '.conf_check_%d' % k)
168 try:
169 shutil.rmtree(dir)
170 except OSError:
171 pass
172 try:
173 os.stat(dir)
174 except:
175 break
176 k += 1
178 try:
179 os.makedirs(dir)
180 except:
181 conf.fatal('cannot create a configuration test folder %r' % dir)
183 try:
184 os.stat(dir)
185 except:
186 conf.fatal('cannot use the configuration test folder %r' % dir)
187 return dir
189 @conf
190 def CHECK_SHLIB_INTRASINC_NAME_FLAGS(conf, msg):
192 check if the waf default flags for setting the name of lib
193 are ok
196 snip = '''
197 int foo(int v) {
198 return v * 2;
201 return conf.check(features='c cshlib',vnum="1",fragment=snip,msg=msg, mandatory=False)
203 @conf
204 def CHECK_NEED_LC(conf, msg):
205 '''check if we need -lc'''
207 dir = find_config_dir(conf)
209 env = conf.env
211 bdir = os.path.join(dir, 'testbuild2')
212 if not os.path.exists(bdir):
213 os.makedirs(bdir)
216 subdir = os.path.join(dir, "liblctest")
218 os.makedirs(subdir)
220 Utils.writef(os.path.join(subdir, 'liblc1.c'), '#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
222 bld = Build.BuildContext()
223 bld.log = conf.log
224 bld.all_envs.update(conf.all_envs)
225 bld.all_envs['default'] = env
226 bld.lst_variants = bld.all_envs.keys()
227 bld.load_dirs(dir, bdir)
229 bld.rescan(bld.srcnode)
231 bld(features='c cshlib',
232 source='liblctest/liblc1.c',
233 ldflags=conf.env['EXTRA_LDFLAGS'],
234 target='liblc',
235 name='liblc')
237 try:
238 bld.compile()
239 conf.check_message(msg, '', True)
240 return True
241 except:
242 conf.check_message(msg, '', False)
243 return False
246 @conf
247 def CHECK_SHLIB_W_PYTHON(conf, msg):
248 '''check if we need -undefined dynamic_lookup'''
250 dir = find_config_dir(conf)
251 snip = '''
252 #include <Python.h>
253 #include <crt_externs.h>
254 #define environ (*_NSGetEnviron())
256 static PyObject *ldb_module = NULL;
257 int foo(int v) {
258 extern char **environ;
259 environ[0] = 1;
260 ldb_module = PyImport_ImportModule("ldb");
261 return v * 2;
264 return conf.check(features='c cshlib',uselib='PYEMBED',fragment=snip,msg=msg, mandatory=False)
266 # this one is quite complex, and should probably be broken up
267 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
268 # functions that make writing complex compound tests like this much easier
269 @conf
270 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
271 '''see if the platform supports building libraries'''
273 if msg is None:
274 if rpath:
275 msg = "rpath library support"
276 else:
277 msg = "building library support"
279 dir = find_config_dir(conf)
281 bdir = os.path.join(dir, 'testbuild')
282 if not os.path.exists(bdir):
283 os.makedirs(bdir)
285 env = conf.env
287 subdir = os.path.join(dir, "libdir")
289 os.makedirs(subdir)
291 Utils.writef(os.path.join(subdir, 'lib1.c'), 'int lib_func(void) { return 42; }\n')
292 Utils.writef(os.path.join(dir, 'main.c'),
293 'int lib_func(void);\n'
294 'int main(void) {return !(lib_func() == 42);}\n')
296 bld = Build.BuildContext()
297 bld.log = conf.log
298 bld.all_envs.update(conf.all_envs)
299 bld.all_envs['default'] = env
300 bld.lst_variants = bld.all_envs.keys()
301 bld.load_dirs(dir, bdir)
303 bld.rescan(bld.srcnode)
305 ldflags = []
306 if version_script:
307 ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
308 Utils.writef(os.path.join(dir,'vscript'), 'TEST_1.0A2 { global: *; };\n')
310 bld(features='c cshlib',
311 source='libdir/lib1.c',
312 target='libdir/lib1',
313 ldflags=ldflags,
314 name='lib1')
316 o = bld(features='c cprogram',
317 source='main.c',
318 target='prog1',
319 uselib_local='lib1')
321 if rpath:
322 o.rpath=os.path.join(bdir, 'default/libdir')
324 # compile the program
325 try:
326 bld.compile()
327 except:
328 conf.check_message(msg, '', False)
329 return False
331 # path for execution
332 lastprog = o.link_task.outputs[0].abspath(env)
334 if not rpath:
335 if 'LD_LIBRARY_PATH' in os.environ:
336 old_ld_library_path = os.environ['LD_LIBRARY_PATH']
337 else:
338 old_ld_library_path = None
339 ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
341 # we need to run the program, try to get its result
342 args = conf.SAMBA_CROSS_ARGS(msg=msg)
343 proc = Utils.subprocess.Popen([lastprog] + args,
344 stdout=Utils.subprocess.PIPE, stderr=Utils.subprocess.PIPE)
345 (out, err) = proc.communicate()
346 w = conf.log.write
347 w(str(out))
348 w('\n')
349 w(str(err))
350 w('\nreturncode %r\n' % proc.returncode)
351 ret = (proc.returncode == 0)
353 if not rpath:
354 os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
356 conf.check_message(msg, '', ret)
357 return ret
361 @conf
362 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
363 '''work out what extension perl uses for manpages'''
365 if msg is None:
366 if section:
367 msg = "perl man%s extension" % section
368 else:
369 msg = "perl manpage generation"
371 conf.start_msg(msg)
373 dir = find_config_dir(conf)
375 bdir = os.path.join(dir, 'testbuild')
376 if not os.path.exists(bdir):
377 os.makedirs(bdir)
379 Utils.writef(os.path.join(bdir, 'Makefile.PL'), """
380 use ExtUtils::MakeMaker;
381 WriteMakefile(
382 'NAME' => 'WafTest',
383 'EXE_FILES' => [ 'WafTest' ]
385 """)
386 back = os.path.abspath('.')
387 os.chdir(bdir)
388 proc = Utils.subprocess.Popen(['perl', 'Makefile.PL'],
389 stdout=Utils.subprocess.PIPE,
390 stderr=Utils.subprocess.PIPE)
391 (out, err) = proc.communicate()
392 os.chdir(back)
394 ret = (proc.returncode == 0)
395 if not ret:
396 conf.end_msg('not found', color='YELLOW')
397 return
399 if section:
400 man = Utils.readf(os.path.join(bdir,'Makefile'))
401 m = re.search(r'MAN%sEXT\s+=\s+(\w+)' % section, man)
402 if not m:
403 conf.end_msg('not found', color='YELLOW')
404 return
405 ext = m.group(1)
406 conf.end_msg(ext)
407 return ext
409 conf.end_msg('ok')
410 return True
413 @conf
414 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
415 '''run a command and return result'''
416 if msg is None:
417 msg = 'Checking %s' % ' '.join(cmd)
418 conf.COMPOUND_START(msg)
419 cmd = cmd[:]
420 if on_target:
421 cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
422 try:
423 ret = get_string(Utils.cmd_output(cmd))
424 except:
425 conf.COMPOUND_END(False)
426 return False
427 if boolean:
428 conf.COMPOUND_END('ok')
429 if define:
430 conf.DEFINE(define, '1')
431 else:
432 ret = ret.strip()
433 conf.COMPOUND_END(ret)
434 if define:
435 conf.DEFINE(define, ret, quote=True)
436 return ret
439 @conf
440 def CHECK_UNAME(conf):
441 '''setup SYSTEM_UNAME_* defines'''
442 ret = True
443 for v in "sysname machine release version".split():
444 if not conf.CHECK_CODE('''
445 int printf(const char *format, ...);
446 struct utsname n;
447 if (uname(&n) == -1) return -1;
448 printf("%%s", n.%s);
449 ''' % v,
450 define='SYSTEM_UNAME_%s' % v.upper(),
451 execute=True,
452 define_ret=True,
453 quote=True,
454 headers='sys/utsname.h',
455 local_include=False,
456 msg="Checking uname %s type" % v):
457 ret = False
458 return ret
460 @conf
461 def CHECK_INLINE(conf):
462 '''check for the right value for inline'''
463 conf.COMPOUND_START('Checking for inline')
464 for i in ['inline', '__inline__', '__inline']:
465 ret = conf.CHECK_CODE('''
466 typedef int foo_t;
467 static %s foo_t static_foo () {return 0; }
468 %s foo_t foo () {return 0; }\n''' % (i, i),
469 define='INLINE_MACRO',
470 addmain=False,
471 link=False)
472 if ret:
473 if i != 'inline':
474 conf.DEFINE('inline', i, quote=False)
475 break
476 if not ret:
477 conf.COMPOUND_END(ret)
478 else:
479 conf.COMPOUND_END(i)
480 return ret
482 @conf
483 def CHECK_XSLTPROC_MANPAGES(conf):
484 '''check if xsltproc can run with the given stylesheets'''
487 if not conf.CONFIG_SET('XSLTPROC'):
488 conf.find_program('xsltproc', var='XSLTPROC')
489 if not conf.CONFIG_SET('XSLTPROC'):
490 return False
492 s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
493 conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.get_flat('XSLTPROC'), s),
494 msg='Checking for stylesheet %s' % s,
495 define='XSLTPROC_MANPAGES', on_target=False,
496 boolean=True)
497 if not conf.CONFIG_SET('XSLTPROC_MANPAGES'):
498 print("A local copy of the docbook.xsl wasn't found on your system" \
499 " consider installing package like docbook-xsl")
502 # Determine the standard libpath for the used compiler,
503 # so we can later use that to filter out these standard
504 # library paths when some tools like cups-config or
505 # python-config report standard lib paths with their
506 # ldflags (-L...)
508 @conf
509 def CHECK_STANDARD_LIBPATH(conf):
510 # at least gcc and clang support this:
511 try:
512 cmd = conf.env.CC + ['-print-search-dirs']
513 out = get_string(Utils.cmd_output(cmd)).split('\n')
514 except ValueError:
515 # option not supported by compiler - use a standard list of directories
516 dirlist = [ '/usr/lib', '/usr/lib64' ]
517 except:
518 raise Errors.WafError('Unexpected error running "%s"' % (cmd))
519 else:
520 dirlist = []
521 for line in out:
522 line = line.strip()
523 if line.startswith("libraries: ="):
524 dirliststr = line[len("libraries: ="):]
525 dirlist = [ os.path.normpath(x) for x in dirliststr.split(':') ]
526 break
528 conf.env.STANDARD_LIBPATH = dirlist