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