build: check size of types
[abartlet/samba.git/.git] / buildtools / wafsamba / samba_autoconf.py
1 # a waf tool to add autoconf-like macros to the configure section
2
3 import Build, os, Logs, sys, Configure, Options
4 import string, Task, Utils, optparse
5 from Configure import conf
6 from Logs import debug
7 from TaskGen import extension
8 from samba_utils import *
9
10 ####################################################
11 # some autoconf like helpers, to make the transition
12 # to waf a bit easier for those used to autoconf
13 # m4 files
14 @runonce
15 @conf
16 def DEFINE(conf, d, v):
17     conf.define(d, v, quote=False)
18     conf.env.append_value('CCDEFINES', d + '=' + str(v))
19
20 @runonce
21 def CHECK_HEADER(conf, h, add_headers=True):
22     if conf.check(header_name=h) and add_headers:
23         conf.env.hlist.append(h)
24         return True
25     return False
26
27 @conf
28 def CHECK_HEADERS(conf, list, add_headers=True):
29     ret = True
30     for hdr in to_list(list):
31         if not CHECK_HEADER(conf, hdr, add_headers):
32             ret = False
33     return ret
34
35 @conf
36 def CHECK_TYPES(conf, list):
37     ret = True
38     for t in to_list(list):
39         if not conf.check(type_name=t, header_name=conf.env.hlist):
40             ret = False
41     return ret
42
43 @conf
44 def CHECK_TYPE_IN(conf, t, hdr):
45     if conf.check(header_name=hdr):
46         conf.check(type_name=t, header_name=hdr)
47         return True
48     return False
49
50 @conf
51 def CHECK_TYPE(conf, t, alternate):
52     if not conf.check(type_name=t, header_name=conf.env.hlist):
53         conf.DEFINE(t, alternate)
54         return True
55     return False
56
57 @conf
58 def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
59     hdrs=''
60     if headers is not None:
61         hlist = to_list(headers)
62     else:
63         hlist = conf.env.hlist
64     for h in hlist:
65         hdrs += '#include <%s>\n' % h
66     if define is None:
67         define = 'HAVE_%s' % v.upper()
68     if conf.check(fragment=
69                   '''
70                   %s
71                   int main(void) {
72                     #ifndef %s
73                     void *_x; _x=(void *)&%s;
74                     #endif
75                     return 0;
76                   }
77                   ''' % (hdrs, v, v),
78                   execute=0,
79                   msg="Checking for variable %s" % v):
80         conf.DEFINE(define, 1)
81         return True
82     elif always:
83         conf.DEFINE(define, 0)
84         return False
85
86 @conf
87 def CHECK_DECLS(conf, vars, reverse=False, headers=None):
88     '''check a list of variable declarations, using the HAVE_DECL_xxx form
89        of define
90
91        When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
92        '''
93     ret = True
94     for v in to_list(vars):
95         if not reverse:
96             define='HAVE_DECL_%s' % v.upper()
97         else:
98             define='HAVE_%s_DECL' % v.upper()
99         if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
100             ret = False
101     return ret
102
103
104 @runonce
105 def CHECK_FUNC(conf, f):
106     return conf.check(function_name=f, header_name=conf.env.hlist)
107
108
109 @conf
110 def CHECK_FUNCS(conf, list):
111     ret = True
112     for f in to_list(list):
113         if not CHECK_FUNC(conf, f):
114             ret = False
115     return ret
116
117 @conf
118 def CHECK_SIZEOF(conf, vars, headers=None, define=None):
119     hdrs=''
120     if headers is not None:
121         hlist = to_list(headers)
122     else:
123         hlist = conf.env.hlist
124     for h in hlist:
125         hdrs += '#include <%s>\n' % h
126     for v in to_list(vars):
127         if define is None:
128             define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
129         else:
130             define_name = define
131         conf.check(fragment=
132                    '''
133                   %s
134                   int main(void) {
135                     printf("%%u\\n", (unsigned)sizeof(%s));
136                     return 0;
137                   }
138                   ''' % (hdrs, v),
139                    execute=1,
140                    define_ret=True,
141                    define_name=define_name,
142                    quote=False,
143                    msg="Checking size of %s" % v)
144
145
146 #################################################
147 # return True if a configuration option was found
148 @conf
149 def CONFIG_SET(conf, option):
150     return (option in conf.env) and (conf.env[option] != ())
151 Build.BuildContext.CONFIG_SET = CONFIG_SET
152
153
154 ###########################################################
155 # check that the functions in 'list' are available in 'library'
156 # if they are, then make that library available as a dependency
157 #
158 # if the library is not available and mandatory==True, then
159 # raise an error.
160 #
161 # If the library is not available and mandatory==False, then
162 # add the library to the list of dependencies to remove from
163 # build rules
164 #
165 # optionally check for the functions first in libc
166 @conf
167 def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
168     # first see if the functions are in libc
169     if checklibc:
170         remaining = []
171         for f in to_list(list):
172             if not CHECK_FUNC(conf, f):
173                 remaining.append(f)
174     else:
175         remaining = to_list(list)
176
177     if remaining == []:
178         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
179         return True
180
181     if not conf.check(lib=library, uselib_store=library):
182         conf.ASSERT(not mandatory,
183                     "Mandatory library '%s' not found for functions '%s'" % (library, list))
184         # if it isn't a mandatory library, then remove it from dependency lists
185         LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
186         return False
187
188     ret = True
189     for f in remaining:
190         if not conf.check(function_name=f, lib=library, header_name=conf.env.hlist):
191             ret = False
192     conf.env['LIB_' + library.upper()] = library
193     LOCAL_CACHE_SET(conf, 'TARGET_TYPE', library, 'SYSLIB')
194     return ret
195
196
197 #################################################
198 # write out config.h in the right directory
199 @conf
200 def SAMBA_CONFIG_H(conf, path=None):
201     if os.path.normpath(conf.curdir) != os.path.normpath(os.environ.get('PWD')):
202         return
203     if path is None:
204         conf.write_config_header('config.h', top=True)
205     else:
206         conf.write_config_header(path)
207
208
209 ##############################################################
210 # setup a configurable path
211 @conf
212 def CONFIG_PATH(conf, name, default):
213     if not name in conf.env:
214         conf.env[name] = conf.env['PREFIX'] + default
215     conf.define(name, conf.env[name], quote=True)
216
217 ##############################################################
218 # add some CFLAGS to the command line
219 @conf
220 def ADD_CFLAGS(conf, flags):
221     if not 'EXTRA_CFLAGS' in conf.env:
222         conf.env['EXTRA_CFLAGS'] = []
223     conf.env['EXTRA_CFLAGS'].extend(to_list(flags))
224
225 ##############################################################
226 # add some extra include directories to all builds
227 @conf
228 def ADD_EXTRA_INCLUDES(conf, includes):
229     if not 'EXTRA_INCLUDES' in conf.env:
230         conf.env['EXTRA_INCLUDES'] = []
231     conf.env['EXTRA_INCLUDES'].extend(to_list(includes))
232
233
234 ##############################################################
235 # work out the current flags. local flags are added first
236 def CURRENT_CFLAGS(bld, cflags):
237     if not 'EXTRA_CFLAGS' in bld.env:
238         list = []
239     else:
240         list = bld.env['EXTRA_CFLAGS'];
241     ret = to_list(cflags)
242     ret.extend(list)
243     return ret