summaryrefslogtreecommitdiff
path: root/buildtools/wafsamba/samba_autoconf.py
blob: 84f6a2d10f3ebe475de1a7a6d54fe119a2839d9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# a waf tool to add autoconf-like macros to the configure section

import Build, os, Logs, sys, Configure, Options
import string, Task, Utils, optparse
from Configure import conf
from Logs import debug
from TaskGen import extension
from samba_utils import *

####################################################
# some autoconf like helpers, to make the transition
# to waf a bit easier for those used to autoconf
# m4 files

@runonce
@conf
def DEFINE(conf, d, v):
    '''define a config option'''
    conf.define(d, v, quote=False)
    conf.env.append_value('CCDEFINES', d + '=' + str(v))

@runonce
def CHECK_HEADER(conf, h, add_headers=True):
    '''check for a header'''
    if conf.check(header_name=h) and add_headers:
        conf.env.hlist.append(h)
        return True
    return False


@conf
def CHECK_HEADERS(conf, list, add_headers=True):
    '''check for a list of headers'''
    ret = True
    for hdr in to_list(list):
        if not CHECK_HEADER(conf, hdr, add_headers):
            ret = False
    return ret


@conf
def CHECK_TYPES(conf, list):
    '''check for a list of types'''
    ret = True
    for t in to_list(list):
        if not conf.check(type_name=t, header_name=conf.env.hlist):
            ret = False
    return ret


@conf
def CHECK_TYPE_IN(conf, t, hdr):
    '''check for a type in a specific header'''
    if conf.check(header_name=hdr):
        conf.check(type_name=t, header_name=hdr)
        return True
    return False


@conf
def CHECK_TYPE(conf, t, alternate=None, headers=None, define=None):
    '''check for a type with an alternate'''
    if headers is None:
        headers = conf.env.hlist
    if define is not None:
        ret = conf.check(type_name=t, header_name=headers, define_name=define)
    else:
        ret = conf.check(type_name=t, header_name=headers)
    if not ret and alternate is not None:
        conf.DEFINE(t, alternate)
    return ret


@conf
def CHECK_VARIABLE(conf, v, define=None, always=False, headers=None):
    '''check for a variable declaration (or define)'''
    hdrs=''
    if headers is not None:
        hlist = to_list(headers)
    else:
        hlist = conf.env.hlist
    for h in hlist:
        hdrs += '#include <%s>\n' % h
    if define is None:
        define = 'HAVE_%s' % v.upper()
    if conf.check(fragment=
                  '''
                  %s
                  int main(void) {
                    #ifndef %s
                    void *_x; _x=(void *)&%s;
                    #endif
                    return 0;
                  }
                  ''' % (hdrs, v, v),
                  execute=0,
                  msg="Checking for variable %s" % v):
        conf.DEFINE(define, 1)
        return True
    elif always:
        conf.DEFINE(define, 0)
        return False

@conf
def CHECK_DECLS(conf, vars, reverse=False, headers=None):
    '''check a list of variable declarations, using the HAVE_DECL_xxx form
       of define

       When reverse==True then use HAVE_xxx_DECL instead of HAVE_DECL_xxx
       '''
    ret = True
    for v in to_list(vars):
        if not reverse:
            define='HAVE_DECL_%s' % v.upper()
        else:
            define='HAVE_%s_DECL' % v.upper()
        if not CHECK_VARIABLE(conf, v, define=define, headers=headers):
            ret = False
    return ret


@runonce
def CHECK_FUNC(conf, f, checklink=False):
    '''check for a function'''
    if checklink:
        return CHECK_CODE(conf, '%s()' % f, execute=False, define='HAVE_%s' % f.upper())
    return conf.check(function_name=f, header_name=conf.env.hlist)


@conf
def CHECK_FUNCS(conf, list, checklink=False):
    '''check for a list of functions'''
    ret = True
    for f in to_list(list):
        if not CHECK_FUNC(conf, f, checklink):
            ret = False
    return ret


@conf
def CHECK_SIZEOF(conf, vars, headers=None, define=None):
    '''check the size of a type'''
    hdrs=''
    if headers is not None:
        hlist = to_list(headers)
    else:
        hlist = conf.env.hlist
    for h in hlist:
        hdrs += '#include <%s>\n' % h
    for v in to_list(vars):
        if define is None:
            define_name = 'SIZEOF_%s' % string.replace(v.upper(), ' ', '_')
        else:
            define_name = define
        conf.check(fragment=
                   '''
                  %s
                  int main(void) {
                    printf("%%u\\n", (unsigned)sizeof(%s));
                    return 0;
                  }
                  ''' % (hdrs, v),
                   execute=1,
                   define_ret=True,
                   define_name=define_name,
                   quote=False,
                   msg="Checking size of %s" % v)


@conf
def CHECK_CODE(conf, code, define,
               always=False, execute=False, addmain=True,
               headers=None, msg=None):
    '''check if some code compiles and/or runs'''
    hdrs=''
    if headers is not None:
        hlist = to_list(headers)
    else:
        hlist = conf.env.hlist
    for h in hlist:
        hdrs += '#include <%s>\n' % h

    if execute:
        execute = 1
    else:
        execute = 0

    if addmain:
        fragment='#include "__confdefs.h"\n%s\n int main(void) { %s; return 0; }' % (hdrs, code)
    else:
        fragment='#include "__confdefs.h"\n%s\n%s' % (hdrs, code)

    conf.write_config_header('__confdefs.h', top=True)

    if msg is None:
        msg="Checking for %s" % define

    if conf.check(fragment=fragment,
                  execute=execute,
                  ccflags='-I%s' % conf.curdir,
                  includes='# . ../default',
                  msg=msg):
        conf.DEFINE(define, 1)
        return True
    elif always:
        conf.DEFINE(define, 0)
        return False


@conf
def CHECK_STRUCTURE_MEMBER(conf, structname, member,
                           always=False, define=None, headers=None):
    '''check for a structure member'''
    hdrs=''
    if headers is not None:
        hlist = to_list(headers)
    else:
        hlist = conf.env.hlist
    for h in hlist:
        hdrs += '#include <%s>\n' % h
    if define is None:
        define = 'HAVE_%s' % member.upper()
    if conf.check(fragment=
                  '''
                  %s
                  int main(void) {
                    %s s;
                    void *_x; _x=(void *)&s.%s;
                    return 0;
                  }
                  ''' % (hdrs, structname, member),
                  execute=0,
                  msg="Checking for member %s in %s" % (member, structname)):
        conf.DEFINE(define, 1)
        return True
    elif always:
        conf.DEFINE(define, 0)
        return False


#################################################
# return True if a configuration option was found
@conf
def CONFIG_SET(conf, option):
    return (option in conf.env) and (conf.env[option] != ())
Build.BuildContext.CONFIG_SET = CONFIG_SET


###########################################################
# check that the functions in 'list' are available in 'library'
# if they are, then make that library available as a dependency
#
# if the library is not available and mandatory==True, then
# raise an error.
#
# If the library is not available and mandatory==False, then
# add the library to the list of dependencies to remove from
# build rules
#
# optionally check for the functions first in libc
@conf
def CHECK_FUNCS_IN(conf, list, library, mandatory=False, checklibc=False):
    # first see if the functions are in libc
    if checklibc:
        remaining = []
        for f in to_list(list):
            if not CHECK_FUNC(conf, f):
                remaining.append(f)
    else:
        remaining = to_list(list)

    if remaining == []:
        LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
        return True

    if not conf.check(lib=library, uselib_store=library):
        conf.ASSERT(not mandatory,
                    "Mandatory library '%s' not found for functions '%s'" % (library, list))
        # if it isn't a mandatory library, then remove it from dependency lists
        LOCAL_CACHE_SET(conf, 'EMPTY_TARGETS', library.upper(), True)
        return False

    conf.define('HAVE_LIB%s' % string.replace(library.upper(),'-','_'), 1)

    ret = True
    for f in remaining:
        if not conf.check(function_name=f, lib=library, header_name=conf.env.hlist):
            ret = False
    conf.env['LIB_' + library.upper()] = library
    LOCAL_CACHE_SET(conf, 'TARGET_TYPE', library, 'SYSLIB')
    return ret


#################################################
# write out config.h in the right directory
@conf
def SAMBA_CONFIG_H(conf, path=None):
    if os.path.normpath(conf.curdir) != os.path.normpath(os.environ.get('PWD')):
        return
    if path is None:
        conf.write_config_header('config.h', top=True)
    else:
        conf.write_config_header(path)


##############################################################
# setup a configurable path
@conf
def CONFIG_PATH(conf, name, default):
    if not name in conf.env:
        conf.env[name] = conf.env['PREFIX'] + default
    conf.define(name, conf.env[name], quote=True)

##############################################################
# add some CFLAGS to the command line
@conf
def ADD_CFLAGS(conf, flags):
    if not 'EXTRA_CFLAGS' in conf.env:
        conf.env['EXTRA_CFLAGS'] = []
    conf.env['EXTRA_CFLAGS'].extend(to_list(flags))

##############################################################
# add some extra include directories to all builds
@conf
def ADD_EXTRA_INCLUDES(conf, includes):
    if not 'EXTRA_INCLUDES' in conf.env:
        conf.env['EXTRA_INCLUDES'] = []
    conf.env['EXTRA_INCLUDES'].extend(to_list(includes))


##############################################################
# work out the current flags. local flags are added first
def CURRENT_CFLAGS(bld, cflags):
    if not 'EXTRA_CFLAGS' in bld.env:
        list = []
    else:
        list = bld.env['EXTRA_CFLAGS'];
    ret = to_list(cflags)
    ret.extend(list)
    return ret