build: neater way to find libreplace and start on tevent waf build
[samba.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 # magic rpath handling
60 #
61 # we want a different rpath when installing and when building
62 # Note that this should really check if rpath is available on this platform
63 # and it should also honor an --enable-rpath option
64 def set_rpath(bld):
65     import Options
66     if Options.is_install:
67         bld.env['RPATH'] = ['-Wl,-rpath=' + bld.env.PREFIX + '/lib']
68     else:
69         bld.env.append_value('RPATH', '-Wl,-rpath=build/default')
70 Build.BuildContext.set_rpath = set_rpath
71
72
73 ################################################################
74 # create a list of files by pre-pending each with a subdir name
75 def SUBDIR(bld, subdir, list):
76     ret = ''
77     for l in list.split():
78         ret = ret + subdir + '/' + l + ' '
79     return ret
80 Build.BuildContext.SUBDIR = SUBDIR
81
82 ################################################################
83 # this will contain the set of includes needed per Samba library
84 Build.BuildContext.SAMBA_LIBRARY_INCLUDES = {}
85
86 ################################################################
87 # this will contain the library dependencies of each Samba library
88 Build.BuildContext.SAMBA_LIBRARY_DEPS = {}
89
90 #################################################################
91 # return a include list for a set of library dependencies
92 def SAMBA_LIBRARY_INCLUDE_LIST(bld, libdeps):
93     ret = bld.curdir + ' '
94     for l in libdeps.split():
95         if l in bld.SAMBA_LIBRARY_INCLUDES:
96             ret = ret + bld.SAMBA_LIBRARY_INCLUDES[l] + ' '
97     return ret
98 Build.BuildContext.SAMBA_LIBRARY_INCLUDE_LIST = SAMBA_LIBRARY_INCLUDE_LIST
99
100
101 #################################################################
102 # define a Samba library
103 def SAMBA_LIBRARY(bld, libname, source_list,
104                   libdeps='', include_list='.', vnum=None):
105     ilist = bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + bld.SUBDIR(bld.curdir, include_list)
106     bld(
107         features = 'cc cshlib',
108         source = source_list,
109         target=libname,
110         includes='. ' + ilist,
111         vnum=vnum)
112     bld.SAMBA_LIBRARY_INCLUDES[libname] = ilist
113 Build.BuildContext.SAMBA_LIBRARY = SAMBA_LIBRARY
114
115 #################################################################
116 # define a Samba binary
117 def SAMBA_BINARY(bld, binname, source_list, libdeps='', include_list=''):
118     bld(
119         features = 'cc cprogram',
120         source = source_list,
121         target = binname,
122         uselib_local = libdeps,
123         includes = '. ' + bld.SAMBA_LIBRARY_INCLUDE_LIST(libdeps) + include_list)
124 Build.BuildContext.SAMBA_BINARY = SAMBA_BINARY
125
126 ############################################################
127 # this overrides the 'waf -v' debug output to be in a nice
128 # unix like format instead of a python list.
129 # Thanks to ita on #waf for this
130 def exec_command(self, cmd, **kw):
131     import Utils, Logs
132     _cmd = cmd
133     if isinstance(cmd, list):
134         _cmd = ' '.join(cmd)
135     Logs.debug('runner: %s' % _cmd)
136     if self.log:
137         self.log.write('%s\n' % cmd)
138         kw['log'] = self.log
139     try:
140         if not kw.get('cwd', None):
141             kw['cwd'] = self.cwd
142     except AttributeError:
143         self.cwd = kw['cwd'] = self.bldnode.abspath()
144     return Utils.exec_command(cmd, **kw)
145 Build.BuildContext.exec_command = exec_command
146
147
148 ######################################################
149 # this is used as a decorator to make functions only
150 # run once. Based on the idea from
151 # http://stackoverflow.com/questions/815110/is-there-a-decorator-to-simply-cache-function-return-values
152 runonce_ret = {}
153 def runonce(function):
154     def wrapper(*args):
155         if args in runonce_ret:
156             return runonce_ret[args]
157         else:
158             ret = function(*args)
159             runonce_ret[args] = ret
160             return ret
161     return wrapper