Add include/lib folders from the commandline
[nivanova/samba-autobuild/.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     return False
117
118
119 @conf
120 def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None, msg=None):
121     '''verify that a C prototype matches the one on the current system'''
122     if not conf.CHECK_DECLS(function, headers=headers):
123         return False
124     if not msg:
125         msg = 'Checking C prototype for %s' % function
126     return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
127                            define=define,
128                            local_include=False,
129                            headers=headers,
130                            link=False,
131                            execute=False,
132                            msg=msg)
133
134
135 @conf
136 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
137     '''check that a named charset is able to be used with iconv_open() for conversion
138     to a target charset
139     '''
140     msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
141     if define is None:
142         define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
143     return conf.CHECK_CODE('''
144                            iconv_t cd = iconv_open("%s", "%s");
145                            if (cd == 0 || cd == (iconv_t)-1) return -1;
146                            ''' % (charset, outcharset),
147                            define=define,
148                            execute=True,
149                            msg=msg,
150                            lib='iconv',
151                            headers=headers)
152
153 def find_config_dir(conf):
154     '''find a directory to run tests in'''
155     k = 0
156     while k < 10000:
157         dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
158         try:
159             shutil.rmtree(dir)
160         except OSError:
161             pass
162         try:
163             os.stat(dir)
164         except:
165             break
166         k += 1
167
168     try:
169         os.makedirs(dir)
170     except:
171         conf.fatal('cannot create a configuration test folder %r' % dir)
172
173     try:
174         os.stat(dir)
175     except:
176         conf.fatal('cannot use the configuration test folder %r' % dir)
177     return dir
178
179 @conf
180 def CHECK_SHLIB_INTRASINC_NAME_FLAGS(conf, msg):
181     '''
182         check if the waf default flags for setting the name of lib
183         are ok
184     '''
185
186     snip = '''
187 int foo(int v) {
188     return v * 2;
189 }
190 '''
191     return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
192
193 @conf
194 def CHECK_NEED_LC(conf, msg):
195     '''check if we need -lc'''
196
197     dir = find_config_dir(conf)
198
199     env = conf.env
200
201     bdir = os.path.join(dir, 'testbuild2')
202     if not os.path.exists(bdir):
203         os.makedirs(bdir)
204
205
206     subdir = os.path.join(dir, "liblctest")
207
208     os.makedirs(subdir)
209
210     dest = open(os.path.join(subdir, 'liblc1.c'), 'w')
211     dest.write('#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
212     dest.close()
213
214     bld = Build.BuildContext()
215     bld.log = conf.log
216     bld.all_envs.update(conf.all_envs)
217     bld.all_envs['default'] = env
218     bld.lst_variants = bld.all_envs.keys()
219     bld.load_dirs(dir, bdir)
220
221     bld.rescan(bld.srcnode)
222
223     bld(features='cc cshlib',
224         source='liblctest/liblc1.c',
225         ldflags=conf.env['EXTRA_LDFLAGS'],
226         target='liblc',
227         name='liblc')
228
229     try:
230         bld.compile()
231         conf.check_message(msg, '', True)
232         return True
233     except:
234         conf.check_message(msg, '', False)
235         return False
236
237
238 @conf
239 def CHECK_SHLIB_W_PYTHON(conf, msg):
240     '''check if we need -undefined dynamic_lookup'''
241
242     dir = find_config_dir(conf)
243
244     env = conf.env
245
246     snip = '''
247 #include <Python.h>
248 #include <crt_externs.h>
249 #define environ (*_NSGetEnviron())
250
251 static PyObject *ldb_module = NULL;
252 int foo(int v) {
253     extern char **environ;
254     environ[0] = 1;
255     ldb_module = PyImport_ImportModule("ldb");
256     return v * 2;
257 }'''
258     return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
259
260 # this one is quite complex, and should probably be broken up
261 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
262 # functions that make writing complex compound tests like this much easier
263 @conf
264 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
265     '''see if the platform supports building libraries'''
266
267     if msg is None:
268         if rpath:
269             msg = "rpath library support"
270         else:
271             msg = "building library support"
272
273     dir = find_config_dir(conf)
274
275     bdir = os.path.join(dir, 'testbuild')
276     if not os.path.exists(bdir):
277         os.makedirs(bdir)
278
279     env = conf.env
280
281     subdir = os.path.join(dir, "libdir")
282
283     os.makedirs(subdir)
284
285     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
286     dest.write('int lib_func(void) { return 42; }\n')
287     dest.close()
288
289     dest = open(os.path.join(dir, 'main.c'), 'w')
290     dest.write('int main(void) {return !(lib_func() == 42);}\n')
291     dest.close()
292
293     bld = Build.BuildContext()
294     bld.log = conf.log
295     bld.all_envs.update(conf.all_envs)
296     bld.all_envs['default'] = env
297     bld.lst_variants = bld.all_envs.keys()
298     bld.load_dirs(dir, bdir)
299
300     bld.rescan(bld.srcnode)
301
302     ldflags = []
303     if version_script:
304         ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
305         dest = open(os.path.join(dir,'vscript'), 'w')
306         dest.write('TEST_1.0A2 { global: *; };\n')
307         dest.close()
308
309     bld(features='cc cshlib',
310         source='libdir/lib1.c',
311         target='libdir/lib1',
312         ldflags=ldflags,
313         name='lib1')
314
315     o = bld(features='cc cprogram',
316             source='main.c',
317             target='prog1',
318             uselib_local='lib1')
319
320     if rpath:
321         o.rpath=os.path.join(bdir, 'default/libdir')
322
323     # compile the program
324     try:
325         bld.compile()
326     except:
327         conf.check_message(msg, '', False)
328         return False
329
330     # path for execution
331     lastprog = o.link_task.outputs[0].abspath(env)
332
333     if not rpath:
334         if 'LD_LIBRARY_PATH' in os.environ:
335             old_ld_library_path = os.environ['LD_LIBRARY_PATH']
336         else:
337             old_ld_library_path = None
338         ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
339
340     # we need to run the program, try to get its result
341     args = conf.SAMBA_CROSS_ARGS(msg=msg)
342     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
343     (out, err) = proc.communicate()
344     w = conf.log.write
345     w(str(out))
346     w('\n')
347     w(str(err))
348     w('\nreturncode %r\n' % proc.returncode)
349     ret = (proc.returncode == 0)
350
351     if not rpath:
352         os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
353
354     conf.check_message(msg, '', ret)
355     return ret
356
357
358
359 @conf
360 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
361     '''work out what extension perl uses for manpages'''
362
363     if msg is None:
364         if section:
365             msg = "perl man%s extension" % section
366         else:
367             msg = "perl manpage generation"
368
369     conf.check_message_1(msg)
370
371     dir = find_config_dir(conf)
372
373     bdir = os.path.join(dir, 'testbuild')
374     if not os.path.exists(bdir):
375         os.makedirs(bdir)
376
377     dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
378     dest.write("""
379 use ExtUtils::MakeMaker;
380 WriteMakefile(
381     'NAME'    => 'WafTest',
382     'EXE_FILES' => [ 'WafTest' ]
383 );
384 """)
385     dest.close()
386     back = os.path.abspath('.')
387     os.chdir(bdir)
388     proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
389                              stdout=Utils.pproc.PIPE,
390                              stderr=Utils.pproc.PIPE)
391     (out, err) = proc.communicate()
392     os.chdir(back)
393
394     ret = (proc.returncode == 0)
395     if not ret:
396         conf.check_message_2('not found', color='YELLOW')
397         return
398
399     if section:
400         f = open(os.path.join(bdir,'Makefile'), 'r')
401         man = f.read()
402         f.close()
403         m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
404         if not m:
405             conf.check_message_2('not found', color='YELLOW')
406             return
407         ext = m.group(1)
408         conf.check_message_2(ext)
409         return ext
410
411     conf.check_message_2('ok')
412     return True
413
414
415 @conf
416 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
417     '''run a command and return result'''
418     if msg is None:
419         msg = 'Checking %s' % ' '.join(cmd)
420     conf.COMPOUND_START(msg)
421     cmd = cmd[:]
422     if on_target:
423         cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
424     try:
425         ret = Utils.cmd_output(cmd)
426     except:
427         conf.COMPOUND_END(False)
428         return False
429     if boolean:
430         conf.COMPOUND_END('ok')
431         if define:
432             conf.DEFINE(define, '1')
433     else:
434         ret = ret.strip()
435         conf.COMPOUND_END(ret)
436         if define:
437             conf.DEFINE(define, ret, quote=True)
438     return ret
439
440
441 @conf
442 def CHECK_UNAME(conf):
443     '''setup SYSTEM_UNAME_* defines'''
444     ret = True
445     for v in "sysname machine release version".split():
446         if not conf.CHECK_CODE('''
447                                struct utsname n;
448                                if (uname(&n) == -1) return -1;
449                                printf("%%s", n.%s);
450                                ''' % v,
451                                define='SYSTEM_UNAME_%s' % v.upper(),
452                                execute=True,
453                                define_ret=True,
454                                quote=True,
455                                headers='sys/utsname.h',
456                                local_include=False,
457                                msg="Checking uname %s type" % v):
458             ret = False
459     return ret
460
461 @conf
462 def CHECK_INLINE(conf):
463     '''check for the right value for inline'''
464     conf.COMPOUND_START('Checking for inline')
465     for i in ['inline', '__inline__', '__inline']:
466         ret = conf.CHECK_CODE('''
467         typedef int foo_t;
468         static %s foo_t static_foo () {return 0; }
469         %s foo_t foo () {return 0; }''' % (i, i),
470                               define='INLINE_MACRO',
471                               addmain=False,
472                               link=False)
473         if ret:
474             if i != 'inline':
475                 conf.DEFINE('inline', i, quote=False)
476             break
477     if not ret:
478         conf.COMPOUND_END(ret)
479     else:
480         conf.COMPOUND_END(i)
481     return ret
482
483 @conf
484 def CHECK_XSLTPROC_MANPAGES(conf):
485     '''check if xsltproc can run with the given stylesheets'''
486
487
488     if not conf.CONFIG_SET('XSLTPROC'):
489         conf.find_program('xsltproc', var='XSLTPROC')
490     if not conf.CONFIG_SET('XSLTPROC'):
491         return False
492
493     s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
494     conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
495                              msg='Checking for stylesheet %s' % s,
496                              define='XSLTPROC_MANPAGES', on_target=False,
497                              boolean=True)