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