build: uname on opensolaris returns 1 for success
[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):
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                            link=False,
46                            execute=False,
47                            msg='Checking C prototype for %s' % function)
48
49
50 @conf
51 def CHECK_CHARSET_EXISTS(conf, charset, outcharset='UCS-2LE', headers=None, define=None):
52     '''check that a named charset is able to be used with iconv_open() for conversion
53     to a target charset
54     '''
55     msg = 'Checking if can we convert from %s to %s' % (charset, outcharset)
56     if define is None:
57         define = 'HAVE_CHARSET_%s' % charset.upper().replace('-','_')
58     return conf.CHECK_CODE('''
59                            iconv_t cd = iconv_open("%s", "%s");
60                            if (cd == 0 || cd == (iconv_t)-1) return -1;
61                            ''' % (charset, outcharset),
62                            define=define,
63                            execute=True,
64                            msg=msg,
65                            lib='iconv',
66                            headers=headers)
67
68 def find_config_dir(conf):
69     '''find a directory to run tests in'''
70     k = 0
71     while k < 10000:
72         dir = os.path.join(conf.blddir, '.conf_check_%d' % k)
73         try:
74             shutil.rmtree(dir)
75         except OSError:
76             pass
77         try:
78             os.stat(dir)
79         except:
80             break
81         k += 1
82
83     try:
84         os.makedirs(dir)
85     except:
86         conf.fatal('cannot create a configuration test folder %r' % dir)
87
88     try:
89         os.stat(dir)
90     except:
91         conf.fatal('cannot use the configuration test folder %r' % dir)
92     return dir
93
94
95 # this one is quite complex, and should probably be broken up
96 # into several parts. I'd quite like to create a set of CHECK_COMPOUND()
97 # functions that make writing complex compound tests like this much easier
98 @conf
99 def CHECK_LIBRARY_SUPPORT(conf, rpath=False, msg=None):
100     '''see if the platform supports building libraries'''
101
102     if msg is None:
103         if rpath:
104             msg = "rpath library support"
105         else:
106             msg = "building library support"
107
108     dir = find_config_dir(conf)
109
110     bdir = os.path.join(dir, 'testbuild')
111     if not os.path.exists(bdir):
112         os.makedirs(bdir)
113
114     env = conf.env
115
116     subdir = os.path.join(dir, "libdir")
117
118     os.makedirs(subdir)
119
120     dest = open(os.path.join(subdir, 'lib1.c'), 'w')
121     dest.write('int lib_func(void) { return 42; }\n')
122     dest.close()
123
124     dest = open(os.path.join(dir, 'main.c'), 'w')
125     dest.write('int main(void) {return !(lib_func() == 42);}\n')
126     dest.close()
127
128     bld = Build.BuildContext()
129     bld.log = conf.log
130     bld.all_envs.update(conf.all_envs)
131     bld.all_envs['default'] = env
132     bld.lst_variants = bld.all_envs.keys()
133     bld.load_dirs(dir, bdir)
134
135     bld.rescan(bld.srcnode)
136
137     bld(features='cc cshlib',
138         source='libdir/lib1.c',
139         target='libdir/lib1',
140         name='lib1')
141
142     o = bld(features='cc cprogram',
143             source='main.c',
144             target='prog1',
145             uselib_local='lib1')
146
147     if rpath:
148         o.rpath=os.path.join(bdir, 'default/libdir')
149
150     # compile the program
151     try:
152         bld.compile()
153     except:
154         conf.check_message(msg, '', False)
155         return False
156
157     # path for execution
158     lastprog = o.link_task.outputs[0].abspath(env)
159
160     if not rpath:
161         if 'LD_LIBRARY_PATH' in os.environ:
162             old_ld_library_path = os.environ['LD_LIBRARY_PATH']
163         else:
164             old_ld_library_path = None
165         ADD_LD_LIBRARY_PATH(os.path.join(bdir, 'default/libdir'))
166
167     # we need to run the program, try to get its result
168     args = conf.SAMBA_CROSS_ARGS(msg=msg)
169     proc = Utils.pproc.Popen([lastprog] + args, stdout=Utils.pproc.PIPE, stderr=Utils.pproc.PIPE)
170     (out, err) = proc.communicate()
171     w = conf.log.write
172     w(str(out))
173     w('\n')
174     w(str(err))
175     w('\nreturncode %r\n' % proc.returncode)
176     ret = (proc.returncode == 0)
177
178     if not rpath:
179         os.environ['LD_LIBRARY_PATH'] = old_ld_library_path or ''
180
181     conf.check_message(msg, '', ret)
182     return ret
183
184
185
186 @conf
187 def CHECK_PERL_MANPAGE(conf, msg=None, section=None):
188     '''work out what extension perl uses for manpages'''
189
190     if msg is None:
191         if section:
192             msg = "perl man%s extension" % section
193         else:
194             msg = "perl manpage generation"
195
196     conf.check_message_1(msg)
197
198     dir = find_config_dir(conf)
199
200     bdir = os.path.join(dir, 'testbuild')
201     if not os.path.exists(bdir):
202         os.makedirs(bdir)
203
204     dest = open(os.path.join(bdir, 'Makefile.PL'), 'w')
205     dest.write("""
206 use ExtUtils::MakeMaker;
207 WriteMakefile(
208     'NAME'      => 'WafTest',
209     'EXE_FILES' => [ 'WafTest' ]
210 );
211 """)
212     dest.close()
213     back = os.path.abspath('.')
214     os.chdir(bdir)
215     proc = Utils.pproc.Popen(['perl', 'Makefile.PL'],
216                              stdout=Utils.pproc.PIPE,
217                              stderr=Utils.pproc.PIPE)
218     (out, err) = proc.communicate()
219     os.chdir(back)
220
221     ret = (proc.returncode == 0)
222     if not ret:
223         conf.check_message_2('not found', color='YELLOW')
224         return
225
226     if section:
227         f = open(os.path.join(bdir,'Makefile'), 'r')
228         man = f.read()
229         f.close()
230         m = re.search('MAN%sEXT\s+=\s+(\w+)' % section, man)
231         if not m:
232             conf.check_message_2('not found', color='YELLOW')
233             return
234         ext = m.group(1)
235         conf.check_message_2(ext)
236         return ext
237
238     conf.check_message_2('ok')
239     return True
240
241
242 @conf
243 def CHECK_COMMAND(conf, cmd, msg=None, define=None, on_target=True):
244     '''run a command and return result'''
245     if msg is None:
246         msg = 'Checking %s' % ' '.join(cmd)
247     conf.COMPOUND_START(msg)
248     cmd = cmd[:]
249     if on_target:
250         cmd.extend(conf.SAMBA_CROSS_ARGS(msg=msg))
251     try:
252         ret = Utils.cmd_output(cmd)
253     except:
254         conf.COMPOUND_END(False)
255         return False
256     ret = ret.strip()
257     conf.COMPOUND_END(ret)
258     if define:
259         conf.DEFINE(define, ret, quote=True)
260     return ret
261
262
263 @conf
264 def CHECK_UNAME(conf):
265     '''setup SYSTEM_UNAME_* defines'''
266     ret = True
267     for v in "sysname machine release version".split():
268         if not conf.CHECK_CODE('''
269                                struct utsname n;
270                                if (uname(&n) == -1) return -1;
271                                printf("%%s", n.%s);
272                                ''' % v,
273                                define='SYSTEM_UNAME_%s' % v.upper(),
274                                execute=True,
275                                define_ret=True,
276                                quote=True,
277                                headers='sys/utsname.h',
278                                local_include=False,
279                                msg="Checking uname %s type" % v):
280             ret = False
281     return ret