s4-backupkey: Comply with [MS-BKRP] 2.2.1
[Samba.git] / buildtools / wafsamba / samba_conftests.py
blob1afc6c94fbe817d89043f2f806208995f11efcca
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 import Build, Configure, Utils
6 from Configure import conf
7 import config_c
8 from samba_utils import *
11 def add_option(self, *k, **kw):
12 '''syntax help: provide the "match" attribute to opt.add_option() so that folders can be added to specific config tests'''
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.Handler.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.copy()
28 # match the configuration test with speficic 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.Handler.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 suceeds 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.check_message_1(kw['msg'])
51 ret = None
52 try:
53 ret = self.run_c_code(*k, **kw)
54 except Configure.ConfigurationError, e:
55 self.check_message_2(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.check_message_2(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('return !(sizeof(off_t) >= 8)',
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('return !(sizeof(off_t) >= 8)',
105 define,
106 execute=True,
107 msg='Checking for large file support without additional flags'):
108 return True
110 if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
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('return !(sizeof(off_t) >= 8)',
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):
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)
144 @conf
145 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
146 '''check that a named charset is able to be used with iconv_open() for conversion
147 to a target charset
149 msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
150 if define is None:
151 define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
152 return conf.CHECK_CODE('''
153 iconv_t cd = iconv_open("%s", "%s");
154 if (cd == 0 || cd == (iconv_t)-1) return -1;
155 ''' % (charset, outcharset),
156 define=define,
157 execute=True,
158 msg=msg,
159 lib='iconv',
160 headers=headers)
162 def find_config_dir(conf):
163 '''find a directory to run tests in'''
164 k = 0
165 while k < 10000:
166 dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
167 try:
168 shutil.rmtree(dir)
169 except OSError:
170 pass
171 try:
172 os.stat(dir)
173 except:
174 break
175 k += 1
177 try:
178 os.makedirs(dir)
179 except:
180 conf.fatal('cannot create a configuration test folder %r' % dir)
182 try:
183 os.stat(dir)
184 except:
185 conf.fatal('cannot use the configuration test folder %r' % dir)
186 return dir
188 @conf
189 def CHECK_SHLIB_INTRASINC_NAME_FLAGS(conf, msg):
191 check if the waf default flags for setting the name of lib
192 are ok
195 snip = '''
196 int foo(int v) {
197 return v * 2;
200 return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
202 @conf
203 def CHECK_NEED_LC(conf, msg):
204 '''check if we need -lc'''
206 dir = find_config_dir(conf)
208 env = conf.env
210 bdir = os.path.join(dir, 'testbuild2')
211 if not os.path.exists(bdir):
212 os.makedirs(bdir)
215 subdir = os.path.join(dir, "liblctest")
217 os.makedirs(subdir)
219 dest = open(os.path.join(subdir, 'liblc1.c'), 'w')
220 dest.write('#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
221 dest.close()
223 bld = Build.BuildContext()
224 bld.log = conf.log
225 bld.all_envs.update(conf.all_envs)
226 bld.all_envs['default'] = env
227 bld.lst_variants = bld.all_envs.keys()
228 bld.load_dirs(dir, bdir)
230 bld.rescan(bld.srcnode)
232 bld(features='cc cshlib',
233 source='liblctest/liblc1.c',
234 ldflags=conf.env['EXTRA_LDFLAGS'],
235 target='liblc',
236 name='liblc')
238 try:
239 bld.compile()
240 conf.check_message(msg, '', True)
241 return True
242 except:
243 conf.check_message(msg, '', False)
244 return False
247 @conf
248 def CHECK_SHLIB_W_PYTHON(conf, msg):
249 '''check if we need -undefined dynamic_lookup'''
251 dir = find_config_dir(conf)
253 env = conf.env
255 snip = '''
256 #include <Python.h>
257 #include <crt_externs.h>
258 #define environ (*_NSGetEnviron())
260 static PyObject *ldb_module = NULL;
261 int foo(int v) {
262 extern char **environ;
263 environ[0] = 1;
264 ldb_module = PyImport_ImportModule("ldb");
265 return v * 2;
266 }'''
267 return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
269 # this one is quite complex, and should probably be broken up
270 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
271 # functions that make writing complex compound tests like this much easier
272 @conf
273 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
274 '''see if the platform supports building libraries'''
276 if msg is None:
277 if rpath:
278 msg = "rpath library support"
279 else:
280 msg = "building library support"
282 dir = find_config_dir(conf)
284 bdir = os.path.join(dir, 'testbuild')
285 if not os.path.exists(bdir):
286 os.makedirs(bdir)
288 env = conf.env
290 subdir = os.path.join(dir, "libdir")
292 os.makedirs(subdir)
294 dest = open(os.path.join(subdir, 'lib1.c'), 'w')
295 dest.write('int lib_func(void) { return 42; }\n')
296 dest.close()
298 dest = open(os.path.join(dir, 'main.c'), 'w')
299 dest.write('int main(void) {return !(lib_func() == 42);}\n')
300 dest.close()
302 bld = Build.BuildContext()
303 bld.log = conf.log
304 bld.all_envs.update(conf.all_envs)
305 bld.all_envs['default'] = env
306 bld.lst_variants = bld.all_envs.keys()
307 bld.load_dirs(dir, bdir)
309 bld.rescan(bld.srcnode)
311 ldflags = []
312 if version_script:
313 ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
314 dest = open(os.path.join(dir,'vscript'), 'w')
315 dest.write('TEST_1.0A2 { global: *; };\n')
316 dest.close()
318 bld(features='cc cshlib',
319 source='libdir/lib1.c',
320 target='libdir/lib1',
321 ldflags=ldflags,
322 name='lib1')
324 o = bld(features='cc cprogram',
325 source='main.c',
326 target='prog1',
327 uselib_local='lib1')
329 if rpath:
330 o.rpath=os.path.join(bdir, 'default/libdir')
332 # compile the program
333 try:
334 bld.compile()
335 except:
336 conf.check_message(msg, '', False)
337 return False
339 # path for execution
340 lastprog = o.link_task.outputs[0].abspath(env)
342 if not rpath:
343 if 'LD_LIBRARY_PATH' in os.environ:
344 old_ld_library_path = os.environ['LD_LIBRARY_PATH']
345 else:
346 old_ld_library_path = None
347 ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
349 # we need to run the program, try to get its result
350 args = conf.SAMBA_CROSS_ARGS(msg=msg)
351 proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
352 (out, err) = proc.communicate()
353 w = conf.log.write
354 w(str(out))
355 w('\n')
356 w(str(err))
357 w('\nreturncode %r\n' % proc.returncode)
358 ret = (proc.returncode == 0)
360 if not rpath:
361 os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
363 conf.check_message(msg, '', ret)
364 return ret
368 @conf
369 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
370 '''work out what extension perl uses for manpages'''
372 if msg is None:
373 if section:
374 msg = "perl man%s extension" % section
375 else:
376 msg = "perl manpage generation"
378 conf.check_message_1(msg)
380 dir = find_config_dir(conf)
382 bdir = os.path.join(dir, 'testbuild')
383 if not os.path.exists(bdir):
384 os.makedirs(bdir)
386 dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
387 dest.write("""
388 use ExtUtils::MakeMaker;
389 WriteMakefile(
390 'NAME' => 'WafTest',
391 'EXE_FILES' => [ 'WafTest' ]
393 """)
394 dest.close()
395 back = os.path.abspath('.')
396 os.chdir(bdir)
397 proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
398 stdout=Utils.pproc.PIPE,
399 stderr=Utils.pproc.PIPE)
400 (out, err) = proc.communicate()
401 os.chdir(back)
403 ret = (proc.returncode == 0)
404 if not ret:
405 conf.check_message_2('not found', color='YELLOW')
406 return
408 if section:
409 f = open(os.path.join(bdir,'Makefile'), 'r')
410 man = f.read()
411 f.close()
412 m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
413 if not m:
414 conf.check_message_2('not found', color='YELLOW')
415 return
416 ext = m.group(1)
417 conf.check_message_2(ext)
418 return ext
420 conf.check_message_2('ok')
421 return True
424 @conf
425 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
426 '''run a command and return result'''
427 if msg is None:
428 msg = 'Checking %s' % ' '.join(cmd)
429 conf.COMPOUND_START(msg)
430 cmd = cmd[:]
431 if on_target:
432 cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
433 try:
434 ret = Utils.cmd_output(cmd)
435 except:
436 conf.COMPOUND_END(False)
437 return False
438 if boolean:
439 conf.COMPOUND_END('ok')
440 if define:
441 conf.DEFINE(define, '1')
442 else:
443 ret = ret.strip()
444 conf.COMPOUND_END(ret)
445 if define:
446 conf.DEFINE(define, ret, quote=True)
447 return ret
450 @conf
451 def CHECK_UNAME(conf):
452 '''setup SYSTEM_UNAME_* defines'''
453 ret = True
454 for v in "sysname machine release version".split():
455 if not conf.CHECK_CODE('''
456 struct utsname n;
457 if (uname(&n) == -1) return -1;
458 printf("%%s", n.%s);
459 ''' % v,
460 define='SYSTEM_UNAME_%s' % v.upper(),
461 execute=True,
462 define_ret=True,
463 quote=True,
464 headers='sys/utsname.h',
465 local_include=False,
466 msg="Checking uname %s type" % v):
467 ret = False
468 return ret
470 @conf
471 def CHECK_INLINE(conf):
472 '''check for the right value for inline'''
473 conf.COMPOUND_START('Checking for inline')
474 for i in ['inline', '__inline__', '__inline']:
475 ret = conf.CHECK_CODE('''
476 typedef int foo_t;
477 static %s foo_t static_foo () {return 0; }
478 %s foo_t foo () {return 0; }''' % (i, i),
479 define='INLINE_MACRO',
480 addmain=False,
481 link=False)
482 if ret:
483 if i != 'inline':
484 conf.DEFINE('inline', i, quote=False)
485 break
486 if not ret:
487 conf.COMPOUND_END(ret)
488 else:
489 conf.COMPOUND_END(i)
490 return ret
492 @conf
493 def CHECK_XSLTPROC_MANPAGES(conf):
494 '''check if xsltproc can run with the given stylesheets'''
497 if not conf.CONFIG_SET('XSLTPROC'):
498 conf.find_program('xsltproc', var='XSLTPROC')
499 if not conf.CONFIG_SET('XSLTPROC'):
500 return False
502 s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
503 conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
504 msg='Checking for stylesheet %s' % s,
505 define='XSLTPROC_MANPAGES', on_target=False,
506 boolean=True)
507 if not conf.CONFIG_SET('XSLTPROC_MANPAGES'):
508 print "A local copy of the docbook.xsl wasn't found on your system" \
509 " consider installing package like docbook-xsl"
512 # Determine the standard libpath for the used compiler,
513 # so we can later use that to filter out these standard
514 # library paths when some tools like cups-config or
515 # python-config report standard lib paths with their
516 # ldflags (-L...)
518 @conf
519 def CHECK_STANDARD_LIBPATH(conf):
520 # at least gcc and clang support this:
521 try:
522 cmd = conf.env.CC + ['-print-search-dirs']
523 out = Utils.cmd_output(cmd).split('\n')
524 except ValueError:
525 # option not supported by compiler - use a standard list of directories
526 dirlist = [ '/usr/lib', '/usr/lib64' ]
527 except:
528 raise Utils.WafError('Unexpected error running "%s"' % (cmd))
529 else:
530 dirlist = []
531 for line in out:
532 line = line.strip()
533 if line.startswith("libraries: ="):
534 dirliststr = line[len("libraries: ="):]
535 dirlist = [ os.path.normpath(x) for x in dirliststr.split(':') ]
536 break
538 conf.env.STANDARD_LIBPATH = dirlist
541 waf_config_c_parse_flags = config_c.parse_flags;
542 def samba_config_c_parse_flags(line1, uselib, env):
544 # We do a special treatment of the rpath components
545 # in the linkflags line, because currently the upstream
546 # parse_flags function is incomplete with respect to
547 # treatment of the rpath. The remainder of the linkflags
548 # line is later passed to the original funcion.
550 lst1 = shlex.split(line1)
551 lst2 = []
552 while lst1:
553 x = lst1.pop(0)
556 # NOTE on special treatment of -Wl,-R and -Wl,-rpath:
558 # It is important to not put a library provided RPATH
559 # into the LINKFLAGS but in the RPATH instead, since
560 # the provided LINKFLAGS get prepended to our own internal
561 # RPATH later, and hence can potentially lead to linking
562 # in too old versions of our internal libs.
564 # We do this filtering here on our own because of some
565 # bugs in the real parse_flags() function.
567 if x == '-Wl,-rpath' or x == '-Wl,-R':
568 linkflags.remove(x)
569 x = lst1.pop(0)
570 if x.startswith('-Wl,'):
571 rpath = x[4:]
572 else:
573 rpath = x
574 elif x.startswith('-Wl,-R,'):
575 rpath = x[7:]
576 elif x.startswith('-Wl,-R'):
577 rpath = x[6:]
578 elif x.startswith('-Wl,-rpath,'):
579 rpath = x[11:]
580 else:
581 lst2.append(x)
582 continue
584 env.append_value('RPATH_' + uselib, rpath)
586 line2 = ' '.join(lst2)
587 waf_config_c_parse_flags(line2, uselib, env)
589 return
590 config_c.parse_flags = samba_config_c_parse_flags