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