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