47bef40a3dc39960dff6179d3e2142ea0916f16a
[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, Build, shutil, Utils
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):
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     return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
42                            define=define,
43                            local_include=False,
44                            headers=headers,
45                            msg='Checking C prototype for %s' % function)
46
47
48 @conf
49 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
50     '''check that a named charset is able to be used with iconv_open() for conversion
51     to a target charset
52     '''
53     msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
54     if define is None:
55         define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
56     return conf.CHECK_CODE('''
57                            iconv_t cd = iconv_open("%s", "%s");
58                            if (cd == 0 || cd == (iconv_t)-1) return -1;
59                            ''' % (charset, outcharset),
60                            define=define,
61                            execute=True,
62                            msg=msg,
63                            lib='iconv',
64                            headers=headers)
65
66
67
68 # this one is quite complex, and should probably be broken up
69 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
70 # functions that make writing complex compound tests like this much easier
71 @conf
72 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
73     '''see if the platform supports building libraries'''
74
75     if msg is None:
76         if rpath:
77             msg = "rpath library support"
78         else:
79             msg = "building library support"
80
81     k = 0
82     while k < 10000:
83         dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
84         try:
85             shutil.rmtree(dir)
86         except OSError:
87             pass
88         try:
89             os.stat(dir)
90         except:
91             break
92         k += 1
93
94     try:
95         os.makedirs(dir)
96     except:
97         conf.fatal('cannot create a configuration test folder %r' % dir)
98
99     try:
100         os.stat(dir)
101     except:
102         conf.fatal('cannot use the configuration test folder %r' % dir)
103
104     bdir = os.path.join(dir, 'testbuild')
105     if not os.path.exists(bdir):
106         os.makedirs(bdir)
107
108     env = conf.env
109
110     subdir = os.path.join(dir, "libdir")
111
112     os.makedirs(subdir)
113
114     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
115     dest.write('int lib_func(void) { return 42; }\n')
116     dest.close()
117
118     dest = open(os.path.join(dir, 'main.c'), 'w')
119     dest.write('int main(void) {return !(lib_func() == 42);}\n')
120     dest.close()
121
122     bld = Build.BuildContext()
123     bld.log = conf.log
124     bld.all_envs.update(conf.all_envs)
125     bld.all_envs['default'] = env
126     bld.lst_variants = bld.all_envs.keys()
127     bld.load_dirs(dir, bdir)
128
129     bld.rescan(bld.srcnode)
130
131     bld(features='cc cshlib',
132         source='libdir/lib1.c',
133         target='libdir/lib1',
134         name='lib1')
135
136     o = bld(features='cc cprogram',
137             source='main.c',
138             target='prog1',
139             uselib_local='lib1')
140
141     if rpath:
142         o.rpath=os.path.join(bdir, 'default/libdir')
143
144     # compile the program
145     try:
146         bld.compile()
147     except:
148         conf.check_message(msg, '', False)
149         return False
150
151     # path for execution
152     lastprog = o.link_task.outputs[0].abspath(env)
153
154     if not rpath:
155         if 'LD_LIBRARY_PATH' in os.environ:
156             old_ld_library_path = os.environ['LD_LIBRARY_PATH']
157         else:
158             old_ld_library_path = None
159         ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
160
161     # we need to run the program, try to get its result
162     args = []
163     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
164     (out, err) = proc.communicate()
165     w = conf.log.write
166     w(str(out))
167     w('\n')
168     w(str(err))
169     w('\nreturncode %r\n' % proc.returncode)
170     ret = (proc.returncode == 0)
171
172     if not rpath:
173         os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
174
175     conf.check_message(msg, '', ret)
176     return ret