s4-python: Various formatting fixes.
[obnox/samba/samba-obnox.git] / buildtools / wafsamba / samba_bundled.py
1 # functions to support bundled libraries
2
3 from Configure import conf
4 import sys, Logs
5 from samba_utils import *
6
7 def PRIVATE_NAME(bld, name, private_extension, private_library):
8     '''possibly rename a library to include a bundled extension'''
9
10     # we now use the same private name for libraries as the public name.
11     # see http://git.samba.org/?p=tridge/junkcode.git;a=tree;f=shlib for a
12     # demonstration that this is the right thing to do
13     # also see http://lists.samba.org/archive/samba-technical/2011-January/075816.html
14     return name
15
16
17 def target_in_list(target, lst, default):
18     for l in lst:
19         if target == l:
20             return True
21         if '!' + target == l:
22             return False
23         if l == 'ALL':
24             return True
25         if l == 'NONE':
26             return False
27     return default
28
29
30 def BUILTIN_LIBRARY(bld, name):
31     '''return True if a library should be builtin
32        instead of being built as a shared lib'''
33     if bld.env.DISABLE_SHARED:
34         return True
35     return target_in_list(name, bld.env.BUILTIN_LIBRARIES, False)
36 Build.BuildContext.BUILTIN_LIBRARY = BUILTIN_LIBRARY
37
38
39 def BUILTIN_DEFAULT(opt, builtins):
40     '''set a comma separated default list of builtin libraries for this package'''
41     if 'BUILTIN_LIBRARIES_DEFAULT' in Options.options:
42         return
43     Options.options['BUILTIN_LIBRARIES_DEFAULT'] = builtins
44 Options.Handler.BUILTIN_DEFAULT = BUILTIN_DEFAULT
45
46
47 def PRIVATE_EXTENSION_DEFAULT(opt, extension, noextension=''):
48     '''set a default private library extension'''
49     if 'PRIVATE_EXTENSION_DEFAULT' in Options.options:
50         return
51     Options.options['PRIVATE_EXTENSION_DEFAULT'] = extension
52     Options.options['PRIVATE_EXTENSION_EXCEPTION'] = noextension
53 Options.Handler.PRIVATE_EXTENSION_DEFAULT = PRIVATE_EXTENSION_DEFAULT
54
55
56 def minimum_library_version(conf, libname, default):
57     '''allow override of mininum system library version'''
58
59     minlist = Options.options.MINIMUM_LIBRARY_VERSION
60     if not minlist:
61         return default
62
63     for m in minlist.split(','):
64         a = m.split(':')
65         if len(a) != 2:
66             Logs.error("Bad syntax for --minimum-library-version of %s" % m)
67             sys.exit(1)
68         if a[0] == libname:
69             return a[1]
70     return default
71
72
73 @conf
74 def LIB_MAY_BE_BUNDLED(conf, libname):
75     return ('NONE' not in conf.env.BUNDLED_LIBS and
76             '!%s' % libname not in conf.env.BUNDLED_LIBS)
77
78
79 @conf
80 def LIB_MUST_BE_BUNDLED(conf, libname):
81     return ('ALL' in conf.env.BUNDLED_LIBS or
82             libname in conf.env.BUNDLED_LIBS)
83
84 @conf
85 def LIB_MUST_BE_PRIVATE(conf, libname):
86     return ('ALL' in conf.env.PRIVATE_LIBS or
87             libname in conf.env.PRIVATE_LIBS)
88
89 @conf
90 def CHECK_PREREQUISITES(conf, prereqs):
91     missing = []
92     for syslib in TO_LIST(prereqs):
93         f = 'FOUND_SYSTEMLIB_%s' % syslib
94         if not f in conf.env:
95             missing.append(syslib)
96     return missing
97
98
99 @runonce
100 @conf
101 def CHECK_BUNDLED_SYSTEM_PKG(conf, libname, minversion='0.0.0',
102         onlyif=None, implied_deps=None, pkg=None):
103     '''check if a library is available as a system library.
104
105     This only tries using pkg-config
106     '''
107     if conf.LIB_MUST_BE_BUNDLED(libname):
108         return False
109     found = 'FOUND_SYSTEMLIB_%s' % libname
110     if found in conf.env:
111         return conf.env[found]
112
113     # see if the library should only use a system version if another dependent
114     # system version is found. That prevents possible use of mixed library
115     # versions
116     if onlyif:
117         missing = conf.CHECK_PREREQUISITES(onlyif)
118         if missing:
119             if not conf.LIB_MAY_BE_BUNDLED(libname):
120                 Logs.error('ERROR: Use of system library %s depends on missing system library/libraries %r' % (libname, missing))
121                 sys.exit(1)
122             conf.env[found] = False
123             return False
124
125     minversion = minimum_library_version(conf, libname, minversion)
126
127     msg = 'Checking for system %s' % libname
128     if minversion != '0.0.0':
129         msg += ' >= %s' % minversion
130
131     if pkg is None:
132         pkg = libname
133
134     if conf.check_cfg(package=pkg,
135                       args='"%s >= %s" --cflags --libs' % (pkg, minversion),
136                       msg=msg, uselib_store=libname.upper()):
137         conf.SET_TARGET_TYPE(libname, 'SYSLIB')
138         conf.env[found] = True
139         if implied_deps:
140             conf.SET_SYSLIB_DEPS(libname, implied_deps)
141         return True
142     conf.env[found] = False
143     if not conf.LIB_MAY_BE_BUNDLED(libname):
144         Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
145         sys.exit(1)
146     return False
147
148
149 @runonce
150 @conf
151 def CHECK_BUNDLED_SYSTEM(conf, libname, minversion='0.0.0',
152                          checkfunctions=None, headers=None,
153                          onlyif=None, implied_deps=None,
154                          require_headers=True):
155     '''check if a library is available as a system library.
156     this first tries via pkg-config, then if that fails
157     tries by testing for a specified function in the specified lib
158     '''
159     if conf.LIB_MUST_BE_BUNDLED(libname):
160         return False
161     found = 'FOUND_SYSTEMLIB_%s' % libname
162     if found in conf.env:
163         return conf.env[found]
164
165     def check_functions_headers():
166         '''helper function for CHECK_BUNDLED_SYSTEM'''
167         if checkfunctions is None:
168             return True
169         if require_headers and headers and not conf.CHECK_HEADERS(headers, lib=libname):
170             return False
171         return conf.CHECK_FUNCS_IN(checkfunctions, libname, headers=headers,
172                                    empty_decl=False, set_target=False)
173
174     # see if the library should only use a system version if another dependent
175     # system version is found. That prevents possible use of mixed library
176     # versions
177     if onlyif:
178         missing = conf.CHECK_PREREQUISITES(onlyif)
179         if missing:
180             if not conf.LIB_MAY_BE_BUNDLED(libname):
181                 Logs.error('ERROR: Use of system library %s depends on missing system library/libraries %r' % (libname, missing))
182                 sys.exit(1)
183             conf.env[found] = False
184             return False
185
186     minversion = minimum_library_version(conf, libname, minversion)
187
188     msg = 'Checking for system %s' % libname
189     if minversion != '0.0.0':
190         msg += ' >= %s' % minversion
191
192     # try pkgconfig first
193     if (conf.check_cfg(package=libname,
194                       args='"%s >= %s" --cflags --libs' % (libname, minversion),
195                       msg=msg) and
196         check_functions_headers()):
197         conf.SET_TARGET_TYPE(libname, 'SYSLIB')
198         conf.env[found] = True
199         if implied_deps:
200             conf.SET_SYSLIB_DEPS(libname, implied_deps)
201         return True
202     if checkfunctions is not None:
203         if check_functions_headers():
204             conf.env[found] = True
205             if implied_deps:
206                 conf.SET_SYSLIB_DEPS(libname, implied_deps)
207             conf.SET_TARGET_TYPE(libname, 'SYSLIB')
208             return True
209     conf.env[found] = False
210     if not conf.LIB_MAY_BE_BUNDLED(libname):
211         Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
212         sys.exit(1)
213     return False
214
215
216 def tuplize_version(version):
217     return tuple([int(x) for x in version.split(".")])
218
219 @runonce
220 @conf
221 def CHECK_BUNDLED_SYSTEM_PYTHON(conf, libname, modulename, minversion='0.0.0'):
222     '''check if a python module is available on the system and
223     has the specified minimum version.
224     '''
225     if conf.LIB_MUST_BE_BUNDLED(libname):
226         return False
227
228     # see if the library should only use a system version if another dependent
229     # system version is found. That prevents possible use of mixed library
230     # versions
231     minversion = minimum_library_version(conf, libname, minversion)
232
233     try:
234         m = __import__(modulename)
235     except ImportError:
236         found = False
237     else:
238         try:
239             version = m.__version__
240         except AttributeError:
241             found = False
242         else:
243             found = tuplize_version(version) >= tuplize_version(minversion)
244     if not found and not conf.LIB_MAY_BE_BUNDLED(libname):
245         Logs.error('ERROR: Python module %s of version %s not found, and bundling disabled' % (libname, minversion))
246         sys.exit(1)
247     return found
248
249
250 def NONSHARED_BINARY(bld, name):
251     '''return True if a binary should be built without non-system shared libs'''
252     if bld.env.DISABLE_SHARED:
253         return True
254     return target_in_list(name, bld.env.NONSHARED_BINARIES, False)
255 Build.BuildContext.NONSHARED_BINARY = NONSHARED_BINARY
256
257