source: vendor/current/buildtools/wafsamba/samba_conftests.py

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

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