s3-waf: fixed tests for charsets
[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 import os, Build, shutil, Utils
4 from Configure import conf
5
6 @conf
7 def CHECK_ICONV(conf, define='HAVE_NATIVE_ICONV'):
8     '''check if the iconv library is installed
9        optionally pass a define'''
10     if conf.CHECK_FUNCS_IN('iconv_open', 'iconv', checklibc=True, headers='iconv.h'):
11         conf.DEFINE(define, 1)
12         return True
13     return False
14
15
16 @conf
17 def CHECK_LARGEFILE(conf, define='HAVE_LARGEFILE'):
18     '''see what we need for largefile support'''
19     if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
20                        define,
21                        execute=True,
22                        msg='Checking for large file support'):
23         return True
24     if conf.CHECK_CODE('return !(sizeof(off_t) >= 8)',
25                        define,
26                        execute=True,
27                        cflags='-D_FILE_OFFSET_BITS=64',
28                        msg='Checking for -D_FILE_OFFSET_BITS=64'):
29         conf.DEFINE('_FILE_OFFSET_BITS', 64)
30         return True
31     return False
32
33
34 @conf
35 def CHECK_C_PROTOTYPE(conf, function, prototype, define, headers=None):
36     '''verify that a C prototype matches the one on the current system'''
37     if not conf.CHECK_DECLS(function, headers=headers):
38         return False
39     return conf.CHECK_CODE('%s; void *_x = (void *)%s' % (prototype, function),
40                            define=define,
41                            local_include=False,
42                            headers=headers,
43                            msg='Checking C prototype for %s' % function)
44
45
46 @conf
47 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
48     '''check that a named charset is able to be used with iconv_open() for conversion
49     to a target charset
50     '''
51     msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
52     if define is None:
53         define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
54     return conf.CHECK_CODE('''
55                            iconv_t cd = iconv_open("%s", "%s");
56                            if (cd == 0 || cd == (iconv_t)-1) return -1;
57                            ''' % (charset, outcharset),
58                            define=define,
59                            execute=True,
60                            msg=msg,
61                            lib='iconv',
62                            headers=headers)
63
64
65
66 # this one is quite complex, and should probably be broken up
67 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
68 # functions that make writing complex compound tests like this much easier
69 @conf
70 def CHECK_RPATH_SUPPORT(conf):
71     '''see if the platform supports rpath for libraries'''
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
95     bdir = os.path.join(dir, 'testbuild')
96     if not os.path.exists(bdir):
97         os.makedirs(bdir)
98
99     env = conf.env
100
101     subdir = os.path.join(dir, "libdir")
102
103     os.makedirs(subdir)
104
105     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
106     dest.write('int lib_func(void) { return 42; }\n')
107     dest.close()
108
109     dest = open(os.path.join(dir, 'main.c'), 'w')
110     dest.write('int main(void) {return !(lib_func() == 42);}\n')
111     dest.close()
112
113     back = os.path.abspath('.')
114
115     bld = Build.BuildContext()
116     bld.log = conf.log
117     bld.all_envs.update(conf.all_envs)
118     bld.all_envs['default'] = env
119     bld.lst_variants = bld.all_envs.keys()
120     bld.load_dirs(dir, bdir)
121
122     os.chdir(dir)
123
124     bld.rescan(bld.srcnode)
125
126     bld(features='cc cshlib',
127         source='libdir/lib1.c',
128         target='libdir/lib1',
129         name='lib1')
130
131     o = bld(features='cc cprogram',
132             source='main.c',
133             target='prog1',
134             uselib_local='lib1',
135             rpath=os.path.join(bdir, 'default/libdir'))
136
137     # compile the program
138     try:
139         bld.compile()
140     except:
141         conf.check_message('rpath support', '', False)
142         return False
143
144     # chdir before returning
145     os.chdir(back)
146
147     # path for execution
148     lastprog = o.link_task.outputs[0].abspath(env)
149
150     # we need to run the program, try to get its result
151     args = []
152     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
153     (out, err) = proc.communicate()
154     w = conf.log.write
155     w(str(out))
156     w('\n')
157     w(str(err))
158     w('\nreturncode %r\n' % proc.returncode)
159     ret = (proc.returncode == 0)
160
161     conf.check_message('rpath support', '', ret)
162     return ret