build: lib needs to take a list when more than 1
[Samba/gebeck_regimport.git] / buildtools / wafsamba / samba_autoconf.py
blob0b2686003344f6921984c7100637bb13c4fedd77
1 # a waf tool to add autoconf-like macros to the configure section
3 import Build, os, Options
4 import string
5 from Configure import conf
6 from samba_utils import *
8 ####################################################
9 # some autoconf like helpers, to make the transition
10 # to waf a bit easier for those used to autoconf
11 # m4 files
13 @runonce
14 @conf
15 def DEFINE(conf, d, v, add_to_cflags=False):
16 '''define a config option'''
17 conf.define(d, v, quote=False)
18 if add_to_cflags:
19 conf.env.append_value('CCDEFINES', d + '=' + str(v))
21 @runonce
22 def CHECK_HEADER(conf, h, add_headers=True):
23 '''check for a header'''
24 if conf.check(header_name=h) and add_headers:
25 conf.env.hlist.append(h)
26 return True
27 return False
30 @conf
31 def CHECK_HEADERS(conf, list, add_headers=True):
32 '''check for a list of headers'''
33 ret = True
34 for hdr in TO_LIST(list):
35 if not CHECK_HEADER(conf, hdr, add_headers):
36 ret = False
37 return ret
40 @conf
41 def CHECK_TYPES(conf, list):
42 '''check for a list of types'''
43 ret = True
44 lst = TO_LIST(list)
45 for t in TO_LIST(list):
46 if not conf.check(type_name=t, header_name=conf.env.hlist):
47 ret = False
48 return ret
51 @conf
52 def CHECK_TYPE_IN(conf, t, hdr, define=None):
53 '''check for a type in a specific header'''
54 if conf.check(header_name=hdr):
55 if define is None:
56 ret = conf.check(type_name=t, header_name=hdr)
57 else:
58 ret = conf.check(type_name=t, header_name=hdr, define_name=define)
59 return ret
60 return False
63 @conf
64 def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
65 '''check for a type with an alternate'''
66 if headers is None:
67 headers = conf.env.hlist
68 if define is not None:
69 ret = conf.check(type_name=t, header_name=headers, define_name=define)
70 else:
71 ret = conf.check(type_name=t, header_name=headers)
72 if not ret and alternate is not None:
73 conf.DEFINE(t, alternate)
74 return ret
77 @conf
78 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
79 '''check for a variable declaration (or define)'''
80 hdrs=''
81 if headers is not None:
82 hlist = TO_LIST(headers)
83 else:
84 hlist = conf.env.hlist
85 for h in hlist:
86 hdrs += '#include <%s>\n' % h
87 if define is None:
88 define = 'HAVE_%s' % v.upper()
89 if conf.check(fragment=
90 '''
92 int main(void) {
93 #ifndef %s
94 void *_x; _x=(void *)&%s;
95 #endif
96 return 0;
98 ''' % (hdrs, v, v),
99 execute=0,
100 msg="Checking for variable %s" % v):
101 conf.DEFINE(define, 1)
102 return True
103 elif always:
104 conf.DEFINE(define, 0)
105 return False
107 @conf
108 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
109 '''check a list of variable declarations, using the HAVE_DECL_xxx form
110 of define
112 When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
114 ret = True
115 for v in TO_LIST(vars):
116 if not reverse:
117 define='HAVE_DECL_%s' % v.upper()
118 else:
119 define='HAVE_%s_DECL' % v.upper()
120 if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
121 ret = False
122 return ret
125 @runonce
126 def CHECK_FUNC(conf, f, checklink=False):
127 '''check for a function'''
128 if checklink:
129 return CHECK_CODE(conf, '%s()' % f, execute=False, define='HAVE_%s' % f.upper())
130 return conf.check(function_name=f, header_name=conf.env.hlist)
133 @conf
134 def CHECK_FUNCS(conf, list, checklink=False):
135 '''check for a list of functions'''
136 ret = True
137 for f in TO_LIST(list):
138 if not CHECK_FUNC(conf, f, checklink):
139 ret = False
140 return ret
143 @conf
144 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
145 '''check the size of a type'''
146 hdrs=''
147 if headers is not None:
148 hlist = TO_LIST(headers)
149 else:
150 hlist = conf.env.hlist
151 for h in hlist:
152 hdrs += '#include <%s>\n' % h
153 for v in TO_LIST(vars):
154 if define is None:
155 define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
156 else:
157 define_name = define
158 conf.check(fragment=
161 int main(void) {
162 printf("%%u\\n", (unsigned)sizeof(%s));
163 return 0;
165 ''' % (hdrs, v),
166 execute=1,
167 define_ret=True,
168 define_name=define_name,
169 quote=False,
170 msg="Checking size of %s" % v)
173 @conf
174 def CHECK_CODE(conf, code, define,
175 always=False, execute=False, addmain=True, mandatory=False,
176 headers=None, msg=None, cflags='', includes='# .',
177 local_include=True):
178 '''check if some code compiles and/or runs'''
179 hdrs=''
180 if headers is not None:
181 hlist = TO_LIST(headers)
182 else:
183 hlist = conf.env.hlist
184 for h in hlist:
185 hdrs += '#include <%s>\n' % h
187 if execute:
188 execute = 1
189 else:
190 execute = 0
192 if addmain:
193 fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
194 else:
195 fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)
197 conf.write_config_header('__confdefs.h', top=True)
199 if msg is None:
200 msg="Checking for %s" % define
202 # include the directory containing __confdefs.h
203 cflags += ' -I../../default'
205 if local_include:
206 cflags += ' -I%s' % conf.curdir
208 if conf.check(fragment=fragment,
209 execute=execute,
210 define_name = define,
211 mandatory = mandatory,
212 ccflags=TO_LIST(cflags),
213 includes=includes,
214 msg=msg):
215 conf.DEFINE(define, 1)
216 return True
217 if always:
218 conf.DEFINE(define, 0)
219 return False
223 @conf
224 def CHECK_STRUCTURE_MEMBER(conf, structname, member,
225 always=False, define=None, headers=None):
226 '''check for a structure member'''
227 hdrs=''
228 if headers is not None:
229 hlist = TO_LIST(headers)
230 else:
231 hlist = conf.env.hlist
232 for h in hlist:
233 hdrs += '#include <%s>\n' % h
234 if define is None:
235 define = 'HAVE_%s' % member.upper()
236 if conf.check(fragment=
239 int main(void) {
240 %s s;
241 void *_x; _x=(void *)&s.%s;
242 return 0;
244 ''' % (hdrs, structname, member),
245 execute=0,
246 msg="Checking for member %s in %s" % (member, structname)):
247 conf.DEFINE(define, 1)
248 return True
249 elif always:
250 conf.DEFINE(define, 0)
251 return False
254 @conf
255 def CHECK_CFLAGS(conf, cflags, variable):
256 '''check if the given cflags are accepted by the compiler'''
257 if conf.check(fragment='int main(void) { return 0; }',
258 execute=0,
259 ccflags=cflags,
260 msg="Checking compiler accepts %s" % cflags):
261 conf.env[variable] = cflags
262 return True
263 return False
266 #################################################
267 # return True if a configuration option was found
268 @conf
269 def CONFIG_SET(conf, option):
270 return (option in conf.env) and (conf.env[option] != ())
271 Build.BuildContext.CONFIG_SET = CONFIG_SET
274 ###########################################################
275 # check that the functions in 'list' are available in 'library'
276 # if they are, then make that library available as a dependency
278 # if the library is not available and mandatory==True, then
279 # raise an error.
281 # If the library is not available and mandatory==False, then
282 # add the library to the list of dependencies to remove from
283 # build rules
285 # optionally check for the functions first in libc
286 @conf
287 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
288 # first see if the functions are in libc
289 if checklibc:
290 remaining = []
291 for f in TO_LIST(list):
292 if not CHECK_FUNC(conf, f):
293 remaining.append(f)
294 else:
295 remaining = TO_LIST(list)
297 if remaining == []:
298 for lib in TO_LIST(library):
299 if GET_TARGET_TYPE(conf, lib) != 'SYSLIB':
300 SET_TARGET_TYPE(conf, lib, 'EMPTY')
301 return True
303 ret = True
304 for lib in TO_LIST(library):
305 if not conf.check(lib=lib, uselib_store=lib):
306 conf.ASSERT(not mandatory,
307 "Mandatory library '%s' not found for functions '%s'" % (library, list))
308 # if it isn't a mandatory library, then remove it from dependency lists
309 SET_TARGET_TYPE(conf, library, 'EMPTY')
310 ret = False
311 else:
312 conf.define('HAVE_LIB%s' % string.replace(lib.upper(),'-','_'), 1)
313 conf.env['LIB_' + lib.upper()] = lib
314 LOCAL_CACHE_SET(conf, 'TARGET_TYPE', lib, 'SYSLIB')
316 if not ret:
317 return ret
319 ret = True
320 for f in remaining:
321 if not conf.check(function_name=f, lib=TO_LIST(library), header_name=conf.env.hlist):
322 ret = False
323 return ret
326 #################################################
327 # write out config.h in the right directory
328 @conf
329 def SAMBA_CONFIG_H(conf, path=None):
330 # we don't want to produce a config.h in places like lib/replace
331 # when we are building projects that depend on lib/replace
332 if os.path.realpath(conf.curdir) != os.path.realpath(Options.launch_dir):
333 return
334 if path is None:
335 conf.write_config_header('config.h', top=True)
336 else:
337 conf.write_config_header(path)
340 ##############################################################
341 # setup a configurable path
342 @conf
343 def CONFIG_PATH(conf, name, default):
344 if not name in conf.env:
345 if default[0] == '/':
346 conf.env[name] = default
347 else:
348 conf.env[name] = conf.env['PREFIX'] + default
349 conf.define(name, conf.env[name], quote=True)
351 ##############################################################
352 # add some CFLAGS to the command line
353 @conf
354 def ADD_CFLAGS(conf, flags):
355 if not 'EXTRA_CFLAGS' in conf.env:
356 conf.env['EXTRA_CFLAGS'] = []
357 conf.env['EXTRA_CFLAGS'].extend(TO_LIST(flags))
359 ##############################################################
360 # add some extra include directories to all builds
361 @conf
362 def ADD_EXTRA_INCLUDES(conf, includes):
363 if not 'EXTRA_INCLUDES' in conf.env:
364 conf.env['EXTRA_INCLUDES'] = []
365 conf.env['EXTRA_INCLUDES'].extend(TO_LIST(includes))
368 ##############################################################
369 # work out the current flags. local flags are added first
370 def CURRENT_CFLAGS(bld, target, cflags):
371 if not 'EXTRA_CFLAGS' in bld.env:
372 list = []
373 else:
374 list = bld.env['EXTRA_CFLAGS'];
375 ret = TO_LIST(cflags)
376 ret.extend(list)
377 return ret