wafsamba: Return missing libs rather than last syslib.
[kai/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     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
85 @conf
86 def CHECK_PREREQUISITES(conf, prereqs):
87     missing = []
88     for syslib in TO_LIST(prereqs):
89         f = 'FOUND_SYSTEMLIB_%s' % syslib
90         if not f in conf.env:
91             missing.append(syslib)
92     return missing
93
94
95 @runonce
96 @conf
97 def CHECK_BUNDLED_SYSTEM_PKG(conf, libname, minversion='0.0.0',
98         onlyif=None, implied_deps=None, pkg=None):
99     '''check if a library is available as a system library.
100
101     This only tries using pkg-config
102     '''
103     if conf.LIB_MUST_BE_BUNDLED(libname):
104         return False
105     found = 'FOUND_SYSTEMLIB_%s' % libname
106     if found in conf.env:
107         return conf.env[found]
108
109     # see if the library should only use a system version if another dependent
110     # system version is found. That prevents possible use of mixed library
111     # versions
112     if onlyif:
113         missing = conf.CHECK_PREREQUISITES(onlyif)
114         if missing:
115             if not conf.LIB_MAY_BE_BUNDLED(libname):
116                 Logs.error('ERROR: Use of system library %s depends on missing system library/libraries %r' % (libname, missing))
117                 sys.exit(1)
118             conf.env[found] = False
119             return False
120
121     minversion = minimum_library_version(conf, libname, minversion)
122
123     msg = 'Checking for system %s' % libname
124     if minversion != '0.0.0':
125         msg += ' >= %s' % minversion
126
127     if pkg is None:
128         pkg = libname
129
130     if conf.check_cfg(package=pkg,
131                       args='"%s >= %s" --cflags --libs' % (pkg, minversion),
132                       msg=msg, uselib_store=libname.upper()):
133         conf.SET_TARGET_TYPE(libname, 'SYSLIB')
134         conf.env[found] = True
135         if implied_deps:
136             conf.SET_SYSLIB_DEPS(libname, implied_deps)
137         return True
138     conf.env[found] = False
139     if not conf.LIB_MAY_BE_BUNDLED(libname):
140         Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
141         sys.exit(1)
142     return False
143
144
145 @runonce
146 @conf
147 def CHECK_BUNDLED_SYSTEM(conf, libname, minversion='0.0.0',
148                          checkfunctions=None, headers=None,
149                          onlyif=None, implied_deps=None,
150                          require_headers=True):
151     '''check if a library is available as a system library.
152     this first tries via pkg-config, then if that fails
153     tries by testing for a specified function in the specified lib
154     '''
155     if conf.LIB_MUST_BE_BUNDLED(libname):
156         return False
157     found = 'FOUND_SYSTEMLIB_%s' % libname
158     if found in conf.env:
159         return conf.env[found]
160
161     def check_functions_headers():
162         '''helper function for CHECK_BUNDLED_SYSTEM'''
163         if checkfunctions is None:
164             return True
165         if require_headers and headers and not conf.CHECK_HEADERS(headers, lib=libname):
166             return False
167         return conf.CHECK_FUNCS_IN(checkfunctions, libname, headers=headers,
168                                    empty_decl=False, set_target=False)
169
170     # see if the library should only use a system version if another dependent
171     # system version is found. That prevents possible use of mixed library
172     # versions
173     if onlyif:
174         missing = conf.CHECK_PREREQUISITES(onlyif)
175         if missing:
176             if not conf.LIB_MAY_BE_BUNDLED(libname):
177                 Logs.error('ERROR: Use of system library %s depends on missing system library/libraries %r' % (libname, missing))
178                 sys.exit(1)
179             conf.env[found] = False
180             return False
181
182     minversion = minimum_library_version(conf, libname, minversion)
183
184     msg = 'Checking for system %s' % libname
185     if minversion != '0.0.0':
186         msg += ' >= %s' % minversion
187
188     # try pkgconfig first
189     if (conf.check_cfg(package=libname,
190                       args='"%s >= %s" --cflags --libs' % (libname, minversion),
191                       msg=msg) and
192         check_functions_headers()):
193         conf.SET_TARGET_TYPE(libname, 'SYSLIB')
194         conf.env[found] = True
195         if implied_deps:
196             conf.SET_SYSLIB_DEPS(libname, implied_deps)
197         return True
198     if checkfunctions is not None:
199         if check_functions_headers():
200             conf.env[found] = True
201             if implied_deps:
202                 conf.SET_SYSLIB_DEPS(libname, implied_deps)
203             conf.SET_TARGET_TYPE(libname, 'SYSLIB')
204             return True
205     conf.env[found] = False
206     if not conf.LIB_MAY_BE_BUNDLED(libname):
207         Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
208         sys.exit(1)
209     return False
210
211
212 @runonce
213 @conf
214 def CHECK_BUNDLED_SYSTEM_PYTHON(conf, libname, modulename, minversion='0.0.0'):
215     '''check if a python module is available on the system and
216     has the specified minimum version.
217     '''
218     if conf.LIB_MUST_BE_BUNDLED(libname):
219         return False
220
221     # see if the library should only use a system version if another dependent
222     # system version is found. That prevents possible use of mixed library
223     # versions
224     minversion = minimum_library_version(conf, libname, minversion)
225
226     try:
227         m = __import__(modulename)
228     except ImportError:
229         found = False
230     else:
231         try:
232             version = m.__version__
233         except AttributeError:
234             found = False
235         else:
236             found = tuple(version.split(".")) >= tuple(minversion.split("."))
237     if not found and not conf.LIB_MAY_BE_BUNDLED(libname):
238         Logs.error('ERROR: Python module %s of version %s not found, and bundling disabled' % (libname, minversion))
239         sys.exit(1)
240     return found
241
242
243 def NONSHARED_BINARY(bld, name):
244     '''return True if a binary should be built without non-system shared libs'''
245     if bld.env.DISABLE_SHARED:
246         return True
247     return target_in_list(name, bld.env.NONSHARED_BINARIES, False)
248 Build.BuildContext.NONSHARED_BINARY = NONSHARED_BINARY
249
250