waf: added configure test for -Wl,--version-script
[metze/samba/wip.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_SHLIB_W_PYTHON(conf, msg):
112     '''check if we need -undefined dynamic_lookup'''
113
114     dir = find_config_dir(conf)
115
116     env = conf.env
117
118     snip = '''
119 #include <Python.h>
120 #include <crt_externs.h>
121 #define environ (*_NSGetEnviron())
122
123 static PyObject *ldb_module = NULL;
124 int foo(int v) {
125     extern char **environ;
126     environ[0] = 1;
127     ldb_module = PyImport_ImportModule("ldb");
128     return v * 2;
129 }'''
130     return conf.check(features='cc cshlib',uselib='PYEMBED',fragment=snip,msg=msg)
131
132 # this one is quite complex, and should probably be broken up
133 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
134 # functions that make writing complex compound tests like this much easier
135 @conf
136 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, version_script=False, msg=None):
137     '''see if the platform supports building libraries'''
138
139     if msg is None:
140         if rpath:
141             msg = "rpath library support"
142         else:
143             msg = "building library support"
144
145     dir = find_config_dir(conf)
146
147     bdir = os.path.join(dir, 'testbuild')
148     if not os.path.exists(bdir):
149         os.makedirs(bdir)
150
151     env = conf.env
152
153     subdir = os.path.join(dir, "libdir")
154
155     os.makedirs(subdir)
156
157     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
158     dest.write('int lib_func(void) { return 42; }\n')
159     dest.close()
160
161     dest = open(os.path.join(dir, 'main.c'), 'w')
162     dest.write('int main(void) {return !(lib_func() == 42);}\n')
163     dest.close()
164
165     bld = Build.BuildContext()
166     bld.log = conf.log
167     bld.all_envs.update(conf.all_envs)
168     bld.all_envs['default'] = env
169     bld.lst_variants = bld.all_envs.keys()
170     bld.load_dirs(dir, bdir)
171
172     bld.rescan(bld.srcnode)
173
174     ldflags = []
175     if version_script:
176         ldflags.append("-Wl,--version-script=%s/vscript" % bld.path.abspath())
177         dest = open(os.path.join(dir,'vscript'), 'w')
178         dest.write('TEST_1.0A2 { global: *; };\n')
179         dest.close()
180
181     bld(features='cc cshlib',
182         source='libdir/lib1.c',
183         target='libdir/lib1',
184         ldflags=ldflags,
185         name='lib1')
186
187     o = bld(features='cc cprogram',
188             source='main.c',
189             target='prog1',
190             uselib_local='lib1')
191
192     if rpath:
193         o.rpath=os.path.join(bdir, 'default/libdir')
194
195     # compile the program
196     try:
197         bld.compile()
198     except:
199         conf.check_message(msg, '', False)
200         return False
201
202     # path for execution
203     lastprog = o.link_task.outputs[0].abspath(env)
204
205     if not rpath:
206         if 'LD_LIBRARY_PATH' in os.environ:
207             old_ld_library_path = os.environ['LD_LIBRARY_PATH']
208         else:
209             old_ld_library_path = None
210         ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
211
212     # we need to run the program, try to get its result
213     args = conf.SAMBA_CROSS_ARGS(msg=msg)
214     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
215     (out, err) = proc.communicate()
216     w = conf.log.write
217     w(str(out))
218     w('\n')
219     w(str(err))
220     w('\nreturncode %r\n' % proc.returncode)
221     ret = (proc.returncode == 0)
222
223     if not rpath:
224         os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
225
226     conf.check_message(msg, '', ret)
227     return ret
228
229
230
231 @conf
232 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
233     '''work out what extension perl uses for manpages'''
234
235     if msg is None:
236         if section:
237             msg = "perl man%s extension" % section
238         else:
239             msg = "perl manpage generation"
240
241     conf.check_message_1(msg)
242
243     dir = find_config_dir(conf)
244
245     bdir = os.path.join(dir, 'testbuild')
246     if not os.path.exists(bdir):
247         os.makedirs(bdir)
248
249     dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
250     dest.write("""
251 use ExtUtils::MakeMaker;
252 WriteMakefile(
253     'NAME'      => 'WafTest',
254     'EXE_FILES' => [ 'WafTest' ]
255 );
256 """)
257     dest.close()
258     back = os.path.abspath('.')
259     os.chdir(bdir)
260     proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
261                              stdout=Utils.pproc.PIPE,
262                              stderr=Utils.pproc.PIPE)
263     (out, err) = proc.communicate()
264     os.chdir(back)
265
266     ret = (proc.returncode == 0)
267     if not ret:
268         conf.check_message_2('not found', color='YELLOW')
269         return
270
271     if section:
272         f = open(os.path.join(bdir,'Makefile'), 'r')
273         man = f.read()
274         f.close()
275         m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
276         if not m:
277             conf.check_message_2('not found', color='YELLOW')
278             return
279         ext = m.group(1)
280         conf.check_message_2(ext)
281         return ext
282
283     conf.check_message_2('ok')
284     return True
285
286
287 @conf
288 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True, boolean=False):
289     '''run a command and return result'''
290     if msg is None:
291         msg = 'Checking %s' % ' '.join(cmd)
292     conf.COMPOUND_START(msg)
293     cmd = cmd[:]
294     if on_target:
295         cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
296     try:
297         ret = Utils.cmd_output(cmd)
298     except:
299         conf.COMPOUND_END(False)
300         return False
301     if boolean:
302         conf.COMPOUND_END('ok')
303         if define:
304             conf.DEFINE(define, '1')
305     else:
306         ret = ret.strip()
307         conf.COMPOUND_END(ret)
308         if define:
309             conf.DEFINE(define, ret, quote=True)
310     return ret
311
312
313 @conf
314 def CHECK_UNAME(conf):
315     '''setup SYSTEM_UNAME_* defines'''
316     ret = True
317     for v in "sysname machine release version".split():
318         if not conf.CHECK_CODE('''
319                                struct utsname n;
320                                if (uname(&n) == -1) return -1;
321                                printf("%%s", n.%s);
322                                ''' % v,
323                                define='SYSTEM_UNAME_%s' % v.upper(),
324                                execute=True,
325                                define_ret=True,
326                                quote=True,
327                                headers='sys/utsname.h',
328                                local_include=False,
329                                msg="Checking uname %s type" % v):
330             ret = False
331     return ret
332
333 @conf
334 def CHECK_INLINE(conf):
335     '''check for the right value for inline'''
336     conf.COMPOUND_START('Checking for inline')
337     for i in ['inline', '__inline__', '__inline']:
338         ret = conf.CHECK_CODE('''
339         typedef int foo_t;
340         static %s foo_t static_foo () {return 0; }
341         %s foo_t foo () {return 0; }''' % (i, i),
342                               define='INLINE_MACRO',
343                               addmain=False,
344                               link=False)
345         if ret:
346             if i != 'inline':
347                 conf.DEFINE('inline', i, quote=False)
348             break
349     if not ret:
350         conf.COMPOUND_END(ret)
351     else:
352         conf.COMPOUND_END(i)
353     return ret
354
355 @conf
356 def CHECK_XSLTPROC_MANPAGES(conf):
357     '''check if xsltproc can run with the given stylesheets'''
358
359
360     if not conf.CONFIG_SET('XSLTPROC'):
361         conf.find_program('xsltproc', var='XSLTPROC')
362     if not conf.CONFIG_SET('XSLTPROC'):
363         return False
364
365     s='http://docbook.sourceforge.net/release/xsl/current/manpages/docbook.xsl'
366     conf.CHECK_COMMAND('%s --nonet %s 2> /dev/null' % (conf.env.XSLTPROC, s),
367                              msg='Checking for stylesheet %s' % s,
368                              define='XSLTPROC_MANPAGES', on_target=False,
369                              boolean=True)