f57ce284174c1f6ad37cb4486d9a6a4cf08f071c
[ira/wip.git] / lib / replace / wafsamba.py
1 # a waf tool to add autoconf-like macros to the configure section
2 # and for SAMBA_ macros for building libraries, binaries etc
3
4 import Build
5 from Configure import conf
6
7
8 ####################################################
9 # some autoconf like helpers, to make the transition
10 # to waf a bit easier for those used to autoconf
11 # m4 files
12 @conf
13 def DEFUN(conf, d, v):
14     conf.define(d, v, quote=False)
15     conf.env.append_value('CCDEFINES', d + '=' + str(v))
16
17 @conf
18 def CHECK_HEADERS(conf, list):
19     for hdr in list.split():
20         if conf.check(header_name=hdr):
21             conf.env.hlist.append(hdr)
22
23 @conf
24 def CHECK_TYPES(conf, list):
25     for t in list.split():
26         conf.check(type_name=t, header_name=conf.env.hlist)
27
28 @conf
29 def CHECK_TYPE_IN(conf, t, hdr):
30     if conf.check(header_name=hdr):
31         conf.check(type_name=t, header_name=hdr)
32
33 @conf
34 def CHECK_TYPE(conf, t, alternate):
35     if not conf.check(type_name=t, header_name=conf.env.hlist):
36         conf.DEFUN(t, alternate)
37
38 @conf
39 def CHECK_FUNCS(conf, list):
40     for f in list.split():
41         conf.check(function_name=f, header_name=conf.env.hlist)
42
43 @conf
44 def CHECK_FUNCS_IN(conf, list, library):
45     if conf.check(lib=library, uselib_store=library):
46         for f in list.split():
47             conf.check(function_name=f, lib=library, header_name=conf.env.hlist)
48
49 #################################################
50 # write out config.h in the right directory
51 @conf
52 def SAMBA_CONFIG_H(conf):
53     import os
54     if os.path.normpath(conf.curdir) == os.path.normpath(conf.srcdir):
55         conf.write_config_header('config.h')
56
57
58 ##############################################################
59 # setup a configurable path
60 @conf
61 def CONFIG_PATH(conf, name, default):
62     if not name in conf.env:
63         conf.env[name] = conf.env['PREFIX'] + default
64     conf.define(name, conf.env[name], quote=True)
65
66
67 ################################################################
68 # magic rpath handling
69 #
70 # we want a different rpath when installing and when building
71 # Note that this should really check if rpath is available on this platform
72 # and it should also honor an --enable-rpath option
73 def set_rpath(bld):
74     import Options
75     if Options.is_install:
76         bld.env['RPATH'] = ['-Wl,-rpath=' + bld.env.PREFIX + '/lib']
77     else:
78         bld.env.append_value('RPATH', '-Wl,-rpath=build/default')
79 Build.BuildContext.set_rpath = set_rpath
80
81
82 ################################################################
83 # create a list of files by pre-pending each with a subdir name
84 def SUBDIR(bld, subdir, list):
85     ret = ''
86     for l in list.split():
87         ret = ret + subdir + '/' + l + ' '
88     return ret
89 Build.BuildContext.SUBDIR = SUBDIR
90
91
92 ################################################################
93 # this will contain the set of includes needed per Samba library
94 Build.BuildContext.SAMBA_LIBRARY_INCLUDES = {}
95
96 ################################################################
97 # this will contain the library dependencies of each Samba library
98 Build.BuildContext.SAMBA_LIBRARY_DEPS = {}
99
100 #################################################################
101 # return a include list for a set of library dependencies
102 def SAMBA_LIBRARY_INCLUDE_LIST(bld, libdeps):
103     ret = bld.curdir + ' '
104     for l in libdeps.split():
105         if l in bld.SAMBA_LIBRARY_INCLUDES:
106             ret = ret + bld.SAMBA_LIBRARY_INCLUDES[l] + ' '
107     return ret
108 Build.BuildContext.SAMBA_LIBRARY_INCLUDE_LIST = SAMBA_LIBRARY_INCLUDE_LIST
109
110 #################################################################
111 # return a library list for a set of library dependencies
112 def SAMBA_LIBRARY_LIB_LIST(bld, libdeps):
113     ret = ' '
114     for l in libdeps.split():
115         if l in bld.SAMBA_LIBRARY_DEPS:
116             ret = ret + l + ' ' + bld.SAMBA_LIBRARY_LIB_LIST(bld.SAMBA_LIBRARY_DEPS[l])
117     return ret
118 Build.BuildContext.SAMBA_LIBRARY_LIB_LIST = SAMBA_LIBRARY_LIB_LIST
119
120
121 #################################################################
122 # define a Samba library
123 def SAMBA_LIBRARY(bld, libname, source_list,
124                   libdeps='', include_list='.', vnum=None):
125     ilist = bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + bld.SUBDIR(bld.curdir, include_list)
126     bld(
127         features = 'cc cshlib',
128         source = source_list,
129         target=libname,
130         includes='. ' + ilist,
131         vnum=vnum)
132     bld.SAMBA_LIBRARY_INCLUDES[libname] = ilist
133     bld.SAMBA_LIBRARY_DEPS[libname] = libdeps
134 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
135
136 #################################################################
137 # define a Samba binary
138 def SAMBA_BINARY(bld, binname, source_list, libdeps='', include_list=''):
139     bld(
140         features = 'cc cprogram',
141         source = source_list,
142         target = binname,
143         uselib_local = bld.SAMBA_LIBRARY_LIB_LIST(libdeps),
144         includes = '. ' + bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + include_list)
145 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
146
147 ############################################################
148 # this overrides the 'waf -v' debug output to be in a nice
149 # unix like format instead of a python list.
150 # Thanks to ita on #waf for this
151 def exec_command(self, cmd, **kw):
152     import Utils, Logs
153     _cmd = cmd
154     if isinstance(cmd, list):
155         _cmd = ' '.join(cmd)
156     Logs.debug('runner: %s' % _cmd)
157     if self.log:
158         self.log.write('%s\n' % cmd)
159         kw['log'] = self.log
160     try:
161         if not kw.get('cwd', None):
162             kw['cwd'] = self.cwd
163     except AttributeError:
164         self.cwd = kw['cwd'] = self.bldnode.abspath()
165     return Utils.exec_command(cmd, **kw)
166 Build.BuildContext.exec_command = exec_command
167
168
169 ######################################################
170 # this is used as a decorator to make functions only
171 # run once. Based on the idea from
172 # http://stackoverflow.com/questions/815110/is-there-a-decorator-to-simply-cache-function-return-values
173 runonce_ret = {}
174 def runonce(function):
175     def wrapper(*args):
176         if args in runonce_ret:
177             return runonce_ret[args]
178         else:
179             ret = function(*args)
180             runonce_ret[args] = ret
181             return ret
182     return wrapper