build: added target directory options
[nivanova/samba-autobuild/.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 # this will contain the set of includes needed per Samba library
93 Build.BuildContext.SAMBA_LIBRARY_INCLUDES = {}
94
95 ################################################################
96 # this will contain the library dependencies of each Samba library
97 Build.BuildContext.SAMBA_LIBRARY_DEPS = {}
98
99 #################################################################
100 # return a include list for a set of library dependencies
101 def SAMBA_LIBRARY_INCLUDE_LIST(bld, libdeps):
102     ret = bld.curdir + ' '
103     for l in libdeps.split():
104         if l in bld.SAMBA_LIBRARY_INCLUDES:
105             ret = ret + bld.SAMBA_LIBRARY_INCLUDES[l] + ' '
106     return ret
107 Build.BuildContext.SAMBA_LIBRARY_INCLUDE_LIST = SAMBA_LIBRARY_INCLUDE_LIST
108
109
110 #################################################################
111 # define a Samba library
112 def SAMBA_LIBRARY(bld, libname, source_list,
113                   libdeps='', include_list='.', vnum=None):
114     ilist = bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + bld.SUBDIR(bld.curdir, include_list)
115     bld(
116         features = 'cc cshlib',
117         source = source_list,
118         target=libname,
119         includes='. ' + ilist,
120         vnum=vnum)
121     bld.SAMBA_LIBRARY_INCLUDES[libname] = ilist
122 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
123
124 #################################################################
125 # define a Samba binary
126 def SAMBA_BINARY(bld, binname, source_list, libdeps='', include_list=''):
127     bld(
128         features = 'cc cprogram',
129         source = source_list,
130         target = binname,
131         uselib_local = libdeps,
132         includes = '. ' + bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + include_list)
133 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
134
135 ############################################################
136 # this overrides the 'waf -v' debug output to be in a nice
137 # unix like format instead of a python list.
138 # Thanks to ita on #waf for this
139 def exec_command(self, cmd, **kw):
140     import Utils, Logs
141     _cmd = cmd
142     if isinstance(cmd, list):
143         _cmd = ' '.join(cmd)
144     Logs.debug('runner: %s' % _cmd)
145     if self.log:
146         self.log.write('%s\n' % cmd)
147         kw['log'] = self.log
148     try:
149         if not kw.get('cwd', None):
150             kw['cwd'] = self.cwd
151     except AttributeError:
152         self.cwd = kw['cwd'] = self.bldnode.abspath()
153     return Utils.exec_command(cmd, **kw)
154 Build.BuildContext.exec_command = exec_command
155
156
157 ######################################################
158 # this is used as a decorator to make functions only
159 # run once. Based on the idea from
160 # http://stackoverflow.com/questions/815110/is-there-a-decorator-to-simply-cache-function-return-values
161 runonce_ret = {}
162 def runonce(function):
163     def wrapper(*args):
164         if args in runonce_ret:
165             return runonce_ret[args]
166         else:
167             ret = function(*args)
168             runonce_ret[args] = ret
169             return ret
170     return wrapper