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