ec98ba0d7eea550aa95aee37aa8d9f8283b1db43
[obnox/samba/samba-obnox.git] / buildtools / wafsamba / samba_conftests.py
1 # a set of config tests that use the samba_autoconf functions
2 # to test for commonly needed configuration options
3
4 import os, shutil, re
5 import Build, Configure, Utils
6 from Configure import conf
7 from samba_utils import *
8
9
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     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
18 Options.Handler.add_option = add_option
19
20 @conf
21 def 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
74 def 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
84 def 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
128 def 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
144 def 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
161 def 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
188 def 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 = '''
195 int foo(int v) {
196     return v * 2;
197 }
198 '''
199     return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
200
201 @conf
202 def 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     dest = open(os.path.join(subdir, 'liblc1.c'), 'w')
219     dest.write('#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
220     dest.close()
221
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)
228
229     bld.rescan(bld.srcnode)
230
231     bld(features='cc cshlib',
232         source='liblctest/liblc1.c',
233         ldflags=conf.env['EXTRA_LDFLAGS'],
234         target='liblc',
235         name='liblc')
236
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
244
245
246 @conf
247 def CHECK_SHLIB_W_PYTHON(conf, msg):
248     '''check if we need -undefined dynamic_lookup'''
249
250     dir = find_config_dir(conf)
251
252     env = conf.env
253
254     snip = '''
255 #include <Python.h>
256 #include <crt_externs.h>
257 #define environ (*_NSGetEnviron())
258
259 static PyObject *ldb_module = NULL;
260 int foo(int v) {
261     extern char **environ;
262     environ[0] = 1;
263     ldb_module = PyImport_ImportModule("ldb");
264     return v * 2;
265 }'''
266     return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
267
268 # this one is quite complex, and should probably be broken up
269 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
270 # functions that make writing complex compound tests like this much easier
271 @conf
272 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
273     '''see if the platform supports building libraries'''
274
275     if msg is None:
276         if rpath:
277             msg = "rpath library support"
278         else:
279             msg = "building library support"
280
281     dir = find_config_dir(conf)
282
283     bdir = os.path.join(dir, 'testbuild')
284     if not os.path.exists(bdir):
285         os.makedirs(bdir)
286
287     env = conf.env
288
289     subdir = os.path.join(dir, "libdir")
290
291     os.makedirs(subdir)
292
293     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
294     dest.write('int lib_func(void) { return 42; }\n')
295     dest.close()
296
297     dest = open(os.path.join(dir, 'main.c'), 'w')
298     dest.write('int main(void) {return !(lib_func() == 42);}\n')
299     dest.close()
300
301     bld = Build.BuildContext()
302     bld.log = conf.log
303     bld.all_envs.update(conf.all_envs)
304     bld.all_envs['default'] = env
305     bld.lst_variants = bld.all_envs.keys()
306     bld.load_dirs(dir, bdir)
307
308     bld.rescan(bld.srcnode)
309
310     ldflags = []
311     if version_script:
312         ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
313         dest = open(os.path.join(dir,'vscript'), 'w')
314         dest.write('TEST_1.0A2 { global: *; };\n')
315         dest.close()
316
317     bld(features='cc cshlib',
318         source='libdir/lib1.c',
319         target='libdir/lib1',
320         ldflags=ldflags,
321         name='lib1')
322
323     o = bld(features='cc cprogram',
324             source='main.c',
325             target='prog1',
326             uselib_local='lib1')
327
328     if rpath:
329         o.rpath=os.path.join(bdir, 'default/libdir')
330
331     # compile the program
332     try:
333         bld.compile()
334     except:
335         conf.check_message(msg, '', False)
336         return False
337
338     # path for execution
339     lastprog = o.link_task.outputs[0].abspath(env)
340
341     if not rpath:
342         if 'LD_LIBRARY_PATH' in os.environ:
343             old_ld_library_path = os.environ['LD_LIBRARY_PATH']
344         else:
345             old_ld_library_path = None
346         ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
347
348     # we need to run the program, try to get its result
349     args = conf.SAMBA_CROSS_ARGS(msg=msg)
350     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
351     (out, err) = proc.communicate()
352     w = conf.log.write
353     w(str(out))
354     w('\n')
355     w(str(err))
356     w('\nreturncode %r\n' % proc.returncode)
357     ret = (proc.returncode == 0)
358
359     if not rpath:
360         os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
361
362     conf.check_message(msg, '', ret)
363     return ret
364
365
366
367 @conf
368 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
369     '''work out what extension perl uses for manpages'''
370
371     if msg is None:
372         if section:
373             msg = "perl man%s extension" % section
374         else:
375             msg = "perl manpage generation"
376
377     conf.check_message_1(msg)
378
379     dir = find_config_dir(conf)
380
381     bdir = os.path.join(dir, 'testbuild')
382     if not os.path.exists(bdir):
383         os.makedirs(bdir)
384
385     dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
386     dest.write("""
387 use ExtUtils::MakeMaker;
388 WriteMakefile(
389     'NAME'    => 'WafTest',
390     'EXE_FILES' => [ 'WafTest' ]
391 );
392 """)
393     dest.close()
394     back = os.path.abspath('.')
395     os.chdir(bdir)
396     proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
397                              stdout=Utils.pproc.PIPE,
398                              stderr=Utils.pproc.PIPE)
399     (out, err) = proc.communicate()
400     os.chdir(back)
401
402     ret = (proc.returncode == 0)
403     if not ret:
404         conf.check_message_2('not found', color='YELLOW')
405         return
406
407     if section:
408         f = open(os.path.join(bdir,'Makefile'), 'r')
409         man = f.read()
410         f.close()
411         m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
412         if not m:
413             conf.check_message_2('not found', color='YELLOW')
414             return
415         ext = m.group(1)
416         conf.check_message_2(ext)
417         return ext
418
419     conf.check_message_2('ok')
420     return True
421
422
423 @conf
424 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
425     '''run a command and return result'''
426     if msg is None:
427         msg = 'Checking %s' % ' '.join(cmd)
428     conf.COMPOUND_START(msg)
429     cmd = cmd[:]
430     if on_target:
431         cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
432     try:
433         ret = Utils.cmd_output(cmd)
434     except:
435         conf.COMPOUND_END(False)
436         return False
437     if boolean:
438         conf.COMPOUND_END('ok')
439         if define:
440             conf.DEFINE(define, '1')
441     else:
442         ret = ret.strip()
443         conf.COMPOUND_END(ret)
444         if define:
445             conf.DEFINE(define, ret, quote=True)
446     return ret
447
448
449 @conf
450 def CHECK_UNAME(conf):
451     '''setup SYSTEM_UNAME_* defines'''
452     ret = True
453     for v in "sysname machine release version".split():
454         if not conf.CHECK_CODE('''
455                                struct utsname n;
456                                if (uname(&n) == -1) return -1;
457                                printf("%%s", n.%s);
458                                ''' % v,
459                                define='SYSTEM_UNAME_%s' % v.upper(),
460                                execute=True,
461                                define_ret=True,
462                                quote=True,
463                                headers='sys/utsname.h',
464                                local_include=False,
465                                msg="Checking uname %s type" % v):
466             ret = False
467     return ret
468
469 @conf
470 def CHECK_INLINE(conf):
471     '''check for the right value for inline'''
472     conf.COMPOUND_START('Checking for inline')
473     for i in ['inline', '__inline__', '__inline']:
474         ret = conf.CHECK_CODE('''
475         typedef int foo_t;
476         static %s foo_t static_foo () {return 0; }
477         %s foo_t foo () {return 0; }''' % (i, i),
478                               define='INLINE_MACRO',
479                               addmain=False,
480                               link=False)
481         if ret:
482             if i != 'inline':
483                 conf.DEFINE('inline', i, quote=False)
484             break
485     if not ret:
486         conf.COMPOUND_END(ret)
487     else:
488         conf.COMPOUND_END(i)
489     return ret
490
491 @conf
492 def CHECK_XSLTPROC_MANPAGES(conf):
493     '''check if xsltproc can run with the given stylesheets'''
494
495
496     if not conf.CONFIG_SET('XSLTPROC'):
497         conf.find_program('xsltproc', var='XSLTPROC')
498     if not conf.CONFIG_SET('XSLTPROC'):
499         return False
500
501     s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
502     conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
503                              msg='Checking for stylesheet %s' % s,
504                              define='XSLTPROC_MANPAGES', on_target=False,
505                              boolean=True)
506     if not conf.CONFIG_SET('XSLTPROC_MANPAGES'):
507         print "A local copy of the docbook.xsl wasn't found on your system" \
508               " consider installing package like docbook-xsl"