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