45946d524bf12b959ea0500f816d4b834ef3e1de
[samba.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     return target_in_list(name, bld.env.BUILTIN_LIBRARIES, False)
34 Build.BuildContext.BUILTIN_LIBRARY = BUILTIN_LIBRARY
35
36
37 def BUILTIN_DEFAULT(opt, builtins):
38     '''set a comma separated default list of builtin libraries for this package'''
39     if 'BUILTIN_LIBRARIES_DEFAULT' in Options.options:
40         return
41     Options.options['BUILTIN_LIBRARIES_DEFAULT'] = builtins
42 Options.Handler.BUILTIN_DEFAULT = BUILTIN_DEFAULT
43
44
45 def PRIVATE_EXTENSION_DEFAULT(opt, extension, noextension=''):
46     '''set a default private library extension'''
47     if 'PRIVATE_EXTENSION_DEFAULT' in Options.options:
48         return
49     Options.options['PRIVATE_EXTENSION_DEFAULT'] = extension
50     Options.options['PRIVATE_EXTENSION_EXCEPTION'] = noextension
51 Options.Handler.PRIVATE_EXTENSION_DEFAULT = PRIVATE_EXTENSION_DEFAULT
52
53
54 def minimum_library_version(conf, libname, default):
55     '''allow override of mininum system library version'''
56
57     minlist = Options.options.MINIMUM_LIBRARY_VERSION
58     if not minlist:
59         return default
60
61     for m in minlist.split(','):
62         a = m.split(':')
63         if len(a) != 2:
64             Logs.error("Bad syntax for --minimum-library-version of %s" % m)
65             sys.exit(1)
66         if a[0] == libname:
67             return a[1]
68     return default
69
70
71 @conf
72 def LIB_MAY_BE_BUNDLED(conf, libname):
73     if libname in conf.env.BUNDLED_LIBS:
74         return True
75     if '!%s' % libname in conf.env.BUNDLED_LIBS:
76         return False
77     if 'NONE' in conf.env.BUNDLED_LIBS:
78         return False
79     return True
80
81 @conf
82 def LIB_MUST_BE_BUNDLED(conf, libname):
83     if libname in conf.env.BUNDLED_LIBS:
84         return True
85     if '!%s' % libname in conf.env.BUNDLED_LIBS:
86         return False
87     if 'ALL' in conf.env.BUNDLED_LIBS:
88         return True
89     return False
90
91 @conf
92 def LIB_MUST_BE_PRIVATE(conf, libname):
93     return ('ALL' in conf.env.PRIVATE_LIBS or
94             libname in conf.env.PRIVATE_LIBS)
95
96 @conf
97 def CHECK_PREREQUISITES(conf, prereqs):
98     missing = []
99     for syslib in TO_LIST(prereqs):
100         f = 'FOUND_SYSTEMLIB_%s' % syslib
101         if not f in conf.env:
102             missing.append(syslib)
103     return missing
104
105
106 @runonce
107 @conf
108 def CHECK_BUNDLED_SYSTEM_PKG(conf, libname, minversion='0.0.0',
109         onlyif=None, implied_deps=None, pkg=None):
110     '''check if a library is available as a system library.
111
112     This only tries using pkg-config
113     '''
114     return conf.CHECK_BUNDLED_SYSTEM(libname,
115                                      minversion=minversion,
116                                      onlyif=onlyif,
117                                      implied_deps=implied_deps,
118                                      pkg=pkg)
119
120 @runonce
121 @conf
122 def CHECK_BUNDLED_SYSTEM(conf, libname, minversion='0.0.0',
123                          checkfunctions=None, headers=None, checkcode=None,
124                          onlyif=None, implied_deps=None,
125                          require_headers=True, pkg=None, set_target=True):
126     '''check if a library is available as a system library.
127     this first tries via pkg-config, then if that fails
128     tries by testing for a specified function in the specified lib
129     '''
130     if conf.LIB_MUST_BE_BUNDLED(libname):
131         return False
132     found = 'FOUND_SYSTEMLIB_%s' % libname
133     if found in conf.env:
134         return conf.env[found]
135
136     def check_functions_headers_code():
137         '''helper function for CHECK_BUNDLED_SYSTEM'''
138         if require_headers and headers and not conf.CHECK_HEADERS(headers, lib=libname):
139             return False
140         if checkfunctions is not None:
141             ok = conf.CHECK_FUNCS_IN(checkfunctions, libname, headers=headers,
142                                      empty_decl=False, set_target=False)
143             if not ok:
144                 return False
145         if checkcode is not None:
146             define='CHECK_BUNDLED_SYSTEM_%s' % libname.upper()
147             ok = conf.CHECK_CODE(checkcode, lib=libname,
148                                  headers=headers, local_include=False,
149                                  msg=msg, define=define)
150             conf.CONFIG_RESET(define)
151             if not ok:
152                 return False
153         return True
154
155
156     # see if the library should only use a system version if another dependent
157     # system version is found. That prevents possible use of mixed library
158     # versions
159     if onlyif:
160         missing = conf.CHECK_PREREQUISITES(onlyif)
161         if missing:
162             if not conf.LIB_MAY_BE_BUNDLED(libname):
163                 Logs.error('ERROR: Use of system library %s depends on missing system library/libraries %r' % (libname, missing))
164                 sys.exit(1)
165             conf.env[found] = False
166             return False
167
168     minversion = minimum_library_version(conf, libname, minversion)
169
170     msg = 'Checking for system %s' % libname
171     if minversion != '0.0.0':
172         msg += ' >= %s' % minversion
173
174     uselib_store=libname.upper()
175     if pkg is None:
176         pkg = libname
177
178     # try pkgconfig first
179     if (conf.check_cfg(package=pkg,
180                       args='"%s >= %s" --cflags --libs' % (pkg, minversion),
181                       msg=msg, uselib_store=uselib_store) and
182         check_functions_headers_code()):
183         if set_target:
184             conf.SET_TARGET_TYPE(libname, 'SYSLIB')
185         conf.env[found] = True
186         if implied_deps:
187             conf.SET_SYSLIB_DEPS(libname, implied_deps)
188         return True
189     if checkfunctions is not None:
190         if check_functions_headers_code():
191             conf.env[found] = True
192             if implied_deps:
193                 conf.SET_SYSLIB_DEPS(libname, implied_deps)
194             if set_target:
195                 conf.SET_TARGET_TYPE(libname, 'SYSLIB')
196             return True
197     conf.env[found] = False
198     if not conf.LIB_MAY_BE_BUNDLED(libname):
199         Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
200         sys.exit(1)
201     return False
202
203
204 def tuplize_version(version):
205     return tuple([int(x) for x in version.split(".")])
206
207 @runonce
208 @conf
209 def CHECK_BUNDLED_SYSTEM_PYTHON(conf, libname, modulename, minversion='0.0.0'):
210     '''check if a python module is available on the system and
211     has the specified minimum version.
212     '''
213     if conf.LIB_MUST_BE_BUNDLED(libname):
214         return False
215
216     # see if the library should only use a system version if another dependent
217     # system version is found. That prevents possible use of mixed library
218     # versions
219     minversion = minimum_library_version(conf, libname, minversion)
220
221     try:
222         m = __import__(modulename)
223     except ImportError:
224         found = False
225     else:
226         try:
227             version = m.__version__
228         except AttributeError:
229             found = False
230         else:
231             found = tuplize_version(version) >= tuplize_version(minversion)
232     if not found and not conf.LIB_MAY_BE_BUNDLED(libname):
233         Logs.error('ERROR: Python module %s of version %s not found, and bundling disabled' % (libname, minversion))
234         sys.exit(1)
235     return found
236
237
238 def NONSHARED_BINARY(bld, name):
239     '''return True if a binary should be built without non-system shared libs'''
240     return target_in_list(name, bld.env.NONSHARED_BINARIES, False)
241 Build.BuildContext.NONSHARED_BINARY = NONSHARED_BINARY
242
243