wafsamba: Allow newer or the same python module versions to be installed,
[Samba.git] / buildtools / wafsamba / samba_bundled.py
blob4455fe40fc5c19faae40350d7b1c21a40e527721
1 # functions to support bundled libraries
3 from Configure import conf
4 import sys, Logs
5 from samba_utils import *
7 def PRIVATE_NAME(bld, name, private_extension, private_library):
8 '''possibly rename a library to include a bundled extension'''
9 if bld.env.DISABLE_SHARED or not private_extension:
10 return name
11 if name in bld.env.PRIVATE_EXTENSION_EXCEPTION and not private_library:
12 return name
13 if private_library and bld.EXPAND_VARIABLES(bld.env.LIBDIR) != bld.EXPAND_VARIABLES(bld.env.PRIVATELIBDIR):
14 # Private libraries already have their own namespace in another way
15 return name
16 extension = getattr(bld.env, 'PRIVATE_EXTENSION', '')
17 if extension:
18 return name + '-' + extension
19 return name
22 def target_in_list(target, lst, default):
23 for l in lst:
24 if target == l:
25 return True
26 if '!' + target == l:
27 return False
28 if l == 'ALL':
29 return True
30 if l == 'NONE':
31 return False
32 return default
35 def BUILTIN_LIBRARY(bld, name):
36 '''return True if a library should be builtin
37 instead of being built as a shared lib'''
38 if bld.env.DISABLE_SHARED:
39 return True
40 return target_in_list(name, bld.env.BUILTIN_LIBRARIES, False)
41 Build.BuildContext.BUILTIN_LIBRARY = BUILTIN_LIBRARY
44 def BUILTIN_DEFAULT(opt, builtins):
45 '''set a comma separated default list of builtin libraries for this package'''
46 if 'BUILTIN_LIBRARIES_DEFAULT' in Options.options:
47 return
48 Options.options['BUILTIN_LIBRARIES_DEFAULT'] = builtins
49 Options.Handler.BUILTIN_DEFAULT = BUILTIN_DEFAULT
52 def PRIVATE_EXTENSION_DEFAULT(opt, extension, noextension=''):
53 '''set a default private library extension'''
54 if 'PRIVATE_EXTENSION_DEFAULT' in Options.options:
55 return
56 Options.options['PRIVATE_EXTENSION_DEFAULT'] = extension
57 Options.options['PRIVATE_EXTENSION_EXCEPTION'] = noextension
58 Options.Handler.PRIVATE_EXTENSION_DEFAULT = PRIVATE_EXTENSION_DEFAULT
61 def minimum_library_version(conf, libname, default):
62 '''allow override of mininum system library version'''
64 minlist = Options.options.MINIMUM_LIBRARY_VERSION
65 if not minlist:
66 return default
68 for m in minlist.split(','):
69 a = m.split(':')
70 if len(a) != 2:
71 Logs.error("Bad syntax for --minimum-library-version of %s" % m)
72 sys.exit(1)
73 if a[0] == libname:
74 return a[1]
75 return default
78 @conf
79 def LIB_MAY_BE_BUNDLED(conf, libname):
80 return ('NONE' not in conf.env.BUNDLED_LIBS and
81 '!%s' % libname not in conf.env.BUNDLED_LIBS)
84 @conf
85 def LIB_MUST_BE_BUNDLED(conf, libname):
86 return ('ALL' in conf.env.BUNDLED_LIBS or
87 libname in conf.env.BUNDLED_LIBS)
90 @runonce
91 @conf
92 def CHECK_BUNDLED_SYSTEM(conf, libname, minversion='0.0.0',
93 checkfunctions=None, headers=None,
94 onlyif=None, implied_deps=None,
95 require_headers=True):
96 '''check if a library is available as a system library.
97 this first tries via pkg-config, then if that fails
98 tries by testing for a specified function in the specified lib
99 '''
100 if conf.LIB_MUST_BE_BUNDLED(libname):
101 return False
102 found = 'FOUND_SYSTEMLIB_%s' % libname
103 if found in conf.env:
104 return conf.env[found]
106 def check_functions_headers():
107 '''helper function for CHECK_BUNDLED_SYSTEM'''
108 if checkfunctions is None:
109 return True
110 if require_headers and headers and not conf.CHECK_HEADERS(headers, lib=libname):
111 return False
112 return conf.CHECK_FUNCS_IN(checkfunctions, libname, headers=headers,
113 empty_decl=False, set_target=False)
115 # see if the library should only use a system version if another dependent
116 # system version is found. That prevents possible use of mixed library
117 # versions
118 if onlyif:
119 for syslib in TO_LIST(onlyif):
120 f = 'FOUND_SYSTEMLIB_%s' % syslib
121 if not f in conf.env:
122 if not conf.LIB_MAY_BE_BUNDLED(libname):
123 Logs.error('ERROR: Use of system library %s depends on missing system library %s' % (libname, syslib))
124 sys.exit(1)
125 conf.env[found] = False
126 return False
128 minversion = minimum_library_version(conf, libname, minversion)
130 # try pkgconfig first
131 if (conf.check_cfg(package=libname,
132 args='"%s >= %s" --cflags --libs' % (libname, minversion),
133 msg='Checking for system %s >= %s' % (libname, minversion)) and
134 check_functions_headers()):
135 conf.SET_TARGET_TYPE(libname, 'SYSLIB')
136 conf.env[found] = True
137 if implied_deps:
138 conf.SET_SYSLIB_DEPS(libname, implied_deps)
139 return True
140 if checkfunctions is not None:
141 if check_functions_headers():
142 conf.env[found] = True
143 if implied_deps:
144 conf.SET_SYSLIB_DEPS(libname, implied_deps)
145 conf.SET_TARGET_TYPE(libname, 'SYSLIB')
146 return True
147 conf.env[found] = False
148 if not conf.LIB_MAY_BE_BUNDLED(libname):
149 Logs.error('ERROR: System library %s of version %s not found, and bundling disabled' % (libname, minversion))
150 sys.exit(1)
151 return False
154 @runonce
155 @conf
156 def CHECK_BUNDLED_SYSTEM_PYTHON(conf, libname, modulename, minversion='0.0.0'):
157 '''check if a python module is available on the system and
158 has the specified minimum version.
160 if conf.LIB_MUST_BE_BUNDLED(libname):
161 return False
163 # see if the library should only use a system version if another dependent
164 # system version is found. That prevents possible use of mixed library
165 # versions
166 minversion = minimum_library_version(conf, libname, minversion)
168 try:
169 m = __import__(modulename)
170 except ImportError:
171 found = False
172 else:
173 try:
174 version = m.__version__
175 except AttributeError:
176 found = False
177 else:
178 found = tuple(version.split(".")) >= tuple(minversion.split("."))
179 if not found and not conf.LIB_MAY_BE_BUNDLED(libname):
180 Logs.error('ERROR: Python module %s of version %s not found, and bundling disabled' % (libname, minversion))
181 sys.exit(1)
182 return found
185 def NONSHARED_BINARY(bld, name):
186 '''return True if a binary should be built without non-system shared libs'''
187 if bld.env.DISABLE_SHARED:
188 return True
189 return target_in_list(name, bld.env.NONSHARED_BINARIES, False)
190 Build.BuildContext.NONSHARED_BINARY = NONSHARED_BINARY