Improve the VFS Makefile so that it is easier for use out of tree but still works...
[Samba/gebeck_regimport.git] / buildtools / wafsamba / samba_conftests.py
blob1838b57c9635f068e4b53338d99c5a8dc853636f
1 # a set of config tests that use the samba_autoconf functions
2 # to test for commonly needed configuration options
4 import os, Build, shutil, Utils, re
5 from Configure import conf
6 from samba_utils import *
8 @conf
9 def CHECK_ICONV(conf, define='HAVE_NATIVE_ICONV'):
10 '''check if the iconv library is installed
11 optionally pass a define'''
12 if conf.CHECK_FUNCS_IN('iconv_open', 'iconv', checklibc=True, headers='iconv.h'):
13 conf.DEFINE(define, 1)
14 return True
15 return False
18 @conf
19 def CHECK_LARGEFILE(conf, define='HAVE_LARGEFILE'):
20 '''see what we need for largefile support'''
21 getconf_cflags = conf.CHECK_COMMAND(['getconf', 'LFS_CFLAGS']);
22 if getconf_cflags is not False:
23 if (conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
24 define='WORKING_GETCONF_LFS_CFLAGS',
25 execute=True,
26 cflags=getconf_cflags,
27 msg='Checking getconf large file support flags work')):
28 conf.ADD_CFLAGS(getconf_cflags)
29 getconf_cflags_list=TO_LIST(getconf_cflags)
30 for flag in getconf_cflags_list:
31 if flag[:2] == "-D":
32 flag_split = flag[2:].split('=')
33 if len(flag_split) == 1:
34 conf.DEFINE(flag_split[0], '1')
35 else:
36 conf.DEFINE(flag_split[0], flag_split[1])
38 if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
39 define,
40 execute=True,
41 msg='Checking for large file support without additional flags'):
42 return True
44 if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
45 define,
46 execute=True,
47 cflags='-D_FILE_OFFSET_BITS=64',
48 msg='Checking for -D_FILE_OFFSET_BITS=64'):
49 conf.DEFINE('_FILE_OFFSET_BITS', 64)
50 return True
51 return False
54 @conf
55 def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None, msg=None):
56 '''verify that a C prototype matches the one on the current system'''
57 if not conf.CHECK_DECLS(function, headers=headers):
58 return False
59 if not msg:
60 msg = 'Checking C prototype for %s' % function
61 return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
62 define=define,
63 local_include=False,
64 headers=headers,
65 link=False,
66 execute=False,
67 msg=msg)
70 @conf
71 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
72 '''check that a named charset is able to be used with iconv_open() for conversion
73 to a target charset
74 '''
75 msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
76 if define is None:
77 define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
78 return conf.CHECK_CODE('''
79 iconv_t cd = iconv_open("%s", "%s");
80 if (cd == 0 || cd == (iconv_t)-1) return -1;
81 ''' % (charset, outcharset),
82 define=define,
83 execute=True,
84 msg=msg,
85 lib='iconv',
86 headers=headers)
88 def find_config_dir(conf):
89 '''find a directory to run tests in'''
90 k = 0
91 while k < 10000:
92 dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
93 try:
94 shutil.rmtree(dir)
95 except OSError:
96 pass
97 try:
98 os.stat(dir)
99 except:
100 break
101 k += 1
103 try:
104 os.makedirs(dir)
105 except:
106 conf.fatal('cannot create a configuration test folder %r' % dir)
108 try:
109 os.stat(dir)
110 except:
111 conf.fatal('cannot use the configuration test folder %r' % dir)
112 return dir
114 @conf
115 def CHECK_SHLIB_INTRASINC_NAME_FLAGS(conf, msg):
117 check if the waf default flags for setting the name of lib
118 are ok
121 snip = '''
122 int foo(int v) {
123 return v * 2;
126 return conf.check(features='cc cshlib',vnum="1",fragment=snip,msg=msg)
128 @conf
129 def CHECK_NEED_LC(conf, msg):
130 '''check if we need -lc'''
132 dir = find_config_dir(conf)
134 env = conf.env
136 bdir = os.path.join(dir, 'testbuild2')
137 if not os.path.exists(bdir):
138 os.makedirs(bdir)
141 subdir = os.path.join(dir, "liblctest")
143 os.makedirs(subdir)
145 dest = open(os.path.join(subdir, 'liblc1.c'), 'w')
146 dest.write('#include <stdio.h>\nint lib_func(void) { FILE *f = fopen("foo", "r");}\n')
147 dest.close()
149 bld = Build.BuildContext()
150 bld.log = conf.log
151 bld.all_envs.update(conf.all_envs)
152 bld.all_envs['default'] = env
153 bld.lst_variants = bld.all_envs.keys()
154 bld.load_dirs(dir, bdir)
156 bld.rescan(bld.srcnode)
158 bld(features='cc cshlib',
159 source='liblctest/liblc1.c',
160 ldflags=conf.env['EXTRA_LDFLAGS'],
161 target='liblc',
162 name='liblc')
164 try:
165 bld.compile()
166 conf.check_message(msg, '', True)
167 return True
168 except:
169 conf.check_message(msg, '', False)
170 return False
173 @conf
174 def CHECK_SHLIB_W_PYTHON(conf, msg):
175 '''check if we need -undefined dynamic_lookup'''
177 dir = find_config_dir(conf)
179 env = conf.env
181 snip = '''
182 #include <Python.h>
183 #include <crt_externs.h>
184 #define environ (*_NSGetEnviron())
186 static PyObject *ldb_module = NULL;
187 int foo(int v) {
188 extern char **environ;
189 environ[0] = 1;
190 ldb_module = PyImport_ImportModule("ldb");
191 return v * 2;
192 }'''
193 return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
195 # this one is quite complex, and should probably be broken up
196 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
197 # functions that make writing complex compound tests like this much easier
198 @conf
199 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
200 '''see if the platform supports building libraries'''
202 if msg is None:
203 if rpath:
204 msg = "rpath library support"
205 else:
206 msg = "building library support"
208 dir = find_config_dir(conf)
210 bdir = os.path.join(dir, 'testbuild')
211 if not os.path.exists(bdir):
212 os.makedirs(bdir)
214 env = conf.env
216 subdir = os.path.join(dir, "libdir")
218 os.makedirs(subdir)
220 dest = open(os.path.join(subdir, 'lib1.c'), 'w')
221 dest.write('int lib_func(void) { return 42; }\n')
222 dest.close()
224 dest = open(os.path.join(dir, 'main.c'), 'w')
225 dest.write('int main(void) {return !(lib_func() == 42);}\n')
226 dest.close()
228 bld = Build.BuildContext()
229 bld.log = conf.log
230 bld.all_envs.update(conf.all_envs)
231 bld.all_envs['default'] = env
232 bld.lst_variants = bld.all_envs.keys()
233 bld.load_dirs(dir, bdir)
235 bld.rescan(bld.srcnode)
237 ldflags = []
238 if version_script:
239 ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
240 dest = open(os.path.join(dir,'vscript'), 'w')
241 dest.write('TEST_1.0A2 { global: *; };\n')
242 dest.close()
244 bld(features='cc cshlib',
245 source='libdir/lib1.c',
246 target='libdir/lib1',
247 ldflags=ldflags,
248 name='lib1')
250 o = bld(features='cc cprogram',
251 source='main.c',
252 target='prog1',
253 uselib_local='lib1')
255 if rpath:
256 o.rpath=os.path.join(bdir, 'default/libdir')
258 # compile the program
259 try:
260 bld.compile()
261 except:
262 conf.check_message(msg, '', False)
263 return False
265 # path for execution
266 lastprog = o.link_task.outputs[0].abspath(env)
268 if not rpath:
269 if 'LD_LIBRARY_PATH' in os.environ:
270 old_ld_library_path = os.environ['LD_LIBRARY_PATH']
271 else:
272 old_ld_library_path = None
273 ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
275 # we need to run the program, try to get its result
276 args = conf.SAMBA_CROSS_ARGS(msg=msg)
277 proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
278 (out, err) = proc.communicate()
279 w = conf.log.write
280 w(str(out))
281 w('\n')
282 w(str(err))
283 w('\nreturncode %r\n' % proc.returncode)
284 ret = (proc.returncode == 0)
286 if not rpath:
287 os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
289 conf.check_message(msg, '', ret)
290 return ret
294 @conf
295 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
296 '''work out what extension perl uses for manpages'''
298 if msg is None:
299 if section:
300 msg = "perl man%s extension" % section
301 else:
302 msg = "perl manpage generation"
304 conf.check_message_1(msg)
306 dir = find_config_dir(conf)
308 bdir = os.path.join(dir, 'testbuild')
309 if not os.path.exists(bdir):
310 os.makedirs(bdir)
312 dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
313 dest.write("""
314 use ExtUtils::MakeMaker;
315 WriteMakefile(
316 'NAME' => 'WafTest',
317 'EXE_FILES' => [ 'WafTest' ]
319 """)
320 dest.close()
321 back = os.path.abspath('.')
322 os.chdir(bdir)
323 proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
324 stdout=Utils.pproc.PIPE,
325 stderr=Utils.pproc.PIPE)
326 (out, err) = proc.communicate()
327 os.chdir(back)
329 ret = (proc.returncode == 0)
330 if not ret:
331 conf.check_message_2('not found', color='YELLOW')
332 return
334 if section:
335 f = open(os.path.join(bdir,'Makefile'), 'r')
336 man = f.read()
337 f.close()
338 m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
339 if not m:
340 conf.check_message_2('not found', color='YELLOW')
341 return
342 ext = m.group(1)
343 conf.check_message_2(ext)
344 return ext
346 conf.check_message_2('ok')
347 return True
350 @conf
351 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
352 '''run a command and return result'''
353 if msg is None:
354 msg = 'Checking %s' % ' '.join(cmd)
355 conf.COMPOUND_START(msg)
356 cmd = cmd[:]
357 if on_target:
358 cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
359 try:
360 ret = Utils.cmd_output(cmd)
361 except:
362 conf.COMPOUND_END(False)
363 return False
364 if boolean:
365 conf.COMPOUND_END('ok')
366 if define:
367 conf.DEFINE(define, '1')
368 else:
369 ret = ret.strip()
370 conf.COMPOUND_END(ret)
371 if define:
372 conf.DEFINE(define, ret, quote=True)
373 return ret
376 @conf
377 def CHECK_UNAME(conf):
378 '''setup SYSTEM_UNAME_* defines'''
379 ret = True
380 for v in "sysname machine release version".split():
381 if not conf.CHECK_CODE('''
382 struct utsname n;
383 if (uname(&n) == -1) return -1;
384 printf("%%s", n.%s);
385 ''' % v,
386 define='SYSTEM_UNAME_%s' % v.upper(),
387 execute=True,
388 define_ret=True,
389 quote=True,
390 headers='sys/utsname.h',
391 local_include=False,
392 msg="Checking uname %s type" % v):
393 ret = False
394 return ret
396 @conf
397 def CHECK_INLINE(conf):
398 '''check for the right value for inline'''
399 conf.COMPOUND_START('Checking for inline')
400 for i in ['inline', '__inline__', '__inline']:
401 ret = conf.CHECK_CODE('''
402 typedef int foo_t;
403 static %s foo_t static_foo () {return 0; }
404 %s foo_t foo () {return 0; }''' % (i, i),
405 define='INLINE_MACRO',
406 addmain=False,
407 link=False)
408 if ret:
409 if i != 'inline':
410 conf.DEFINE('inline', i, quote=False)
411 break
412 if not ret:
413 conf.COMPOUND_END(ret)
414 else:
415 conf.COMPOUND_END(i)
416 return ret
418 @conf
419 def CHECK_XSLTPROC_MANPAGES(conf):
420 '''check if xsltproc can run with the given stylesheets'''
423 if not conf.CONFIG_SET('XSLTPROC'):
424 conf.find_program('xsltproc', var='XSLTPROC')
425 if not conf.CONFIG_SET('XSLTPROC'):
426 return False
428 s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
429 conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
430 msg='Checking for stylesheet %s' % s,
431 define='XSLTPROC_MANPAGES', on_target=False,
432 boolean=True)