third_party: Update waf to version 2.0.17
[vlendec/samba-autobuild/.git] / third_party / waf / waflib / Tools / python.py
1 #!/usr/bin/env python
2 # encoding: utf-8
3 # Thomas Nagy, 2007-2015 (ita)
4 # Gustavo Carneiro (gjc), 2007
5
6 """
7 Support for Python, detect the headers and libraries and provide
8 *use* variables to link C/C++ programs against them::
9
10         def options(opt):
11                 opt.load('compiler_c python')
12         def configure(conf):
13                 conf.load('compiler_c python')
14                 conf.check_python_version((2,4,2))
15                 conf.check_python_headers()
16         def build(bld):
17                 bld.program(features='pyembed', source='a.c', target='myprog')
18                 bld.shlib(features='pyext', source='b.c', target='mylib')
19 """
20
21 import os, sys
22 from waflib import Errors, Logs, Node, Options, Task, Utils
23 from waflib.TaskGen import extension, before_method, after_method, feature
24 from waflib.Configure import conf
25
26 FRAG = '''
27 #include <Python.h>
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31         void Py_Initialize(void);
32         void Py_Finalize(void);
33 #ifdef __cplusplus
34 }
35 #endif
36 int main(int argc, char **argv)
37 {
38    (void)argc; (void)argv;
39    Py_Initialize();
40    Py_Finalize();
41    return 0;
42 }
43 '''
44 """
45 Piece of C/C++ code used in :py:func:`waflib.Tools.python.check_python_headers`
46 """
47
48 INST = '''
49 import sys, py_compile
50 py_compile.compile(sys.argv[1], sys.argv[2], sys.argv[3], True)
51 '''
52 """
53 Piece of Python code used in :py:class:`waflib.Tools.python.pyo` and :py:class:`waflib.Tools.python.pyc` for byte-compiling python files
54 """
55
56 DISTUTILS_IMP = ['from distutils.sysconfig import get_config_var, get_python_lib']
57
58 @before_method('process_source')
59 @feature('py')
60 def feature_py(self):
61         """
62         Create tasks to byte-compile .py files and install them, if requested
63         """
64         self.install_path = getattr(self, 'install_path', '${PYTHONDIR}')
65         install_from = getattr(self, 'install_from', None)
66         if install_from and not isinstance(install_from, Node.Node):
67                 install_from = self.path.find_dir(install_from)
68         self.install_from = install_from
69
70         ver = self.env.PYTHON_VERSION
71         if not ver:
72                 self.bld.fatal('Installing python files requires PYTHON_VERSION, try conf.check_python_version')
73
74         if int(ver.replace('.', '')) > 31:
75                 self.install_32 = True
76
77 @extension('.py')
78 def process_py(self, node):
79         """
80         Add signature of .py file, so it will be byte-compiled when necessary
81         """
82         assert(hasattr(self, 'install_path')), 'add features="py"'
83
84         # where to install the python file
85         if self.install_path:
86                 if self.install_from:
87                         self.add_install_files(install_to=self.install_path, install_from=node, cwd=self.install_from, relative_trick=True)
88                 else:
89                         self.add_install_files(install_to=self.install_path, install_from=node, relative_trick=True)
90
91         lst = []
92         if self.env.PYC:
93                 lst.append('pyc')
94         if self.env.PYO:
95                 lst.append('pyo')
96
97         if self.install_path:
98                 if self.install_from:
99                         pyd = Utils.subst_vars("%s/%s" % (self.install_path, node.path_from(self.install_from)), self.env)
100                 else:
101                         pyd = Utils.subst_vars("%s/%s" % (self.install_path, node.path_from(self.path)), self.env)
102         else:
103                 pyd = node.abspath()
104
105         for ext in lst:
106                 if self.env.PYTAG and not self.env.NOPYCACHE:
107                         # __pycache__ installation for python 3.2 - PEP 3147
108                         name = node.name[:-3]
109                         pyobj = node.parent.get_bld().make_node('__pycache__').make_node("%s.%s.%s" % (name, self.env.PYTAG, ext))
110                         pyobj.parent.mkdir()
111                 else:
112                         pyobj = node.change_ext(".%s" % ext)
113
114                 tsk = self.create_task(ext, node, pyobj)
115                 tsk.pyd = pyd
116
117                 if self.install_path:
118                         self.add_install_files(install_to=os.path.dirname(pyd), install_from=pyobj, cwd=node.parent.get_bld(), relative_trick=True)
119
120 class pyc(Task.Task):
121         """
122         Byte-compiling python files
123         """
124         color = 'PINK'
125         def __str__(self):
126                 node = self.outputs[0]
127                 return node.path_from(node.ctx.launch_node())
128         def run(self):
129                 cmd = [Utils.subst_vars('${PYTHON}', self.env), '-c', INST, self.inputs[0].abspath(), self.outputs[0].abspath(), self.pyd]
130                 ret = self.generator.bld.exec_command(cmd)
131                 return ret
132
133 class pyo(Task.Task):
134         """
135         Byte-compiling python files
136         """
137         color = 'PINK'
138         def __str__(self):
139                 node = self.outputs[0]
140                 return node.path_from(node.ctx.launch_node())
141         def run(self):
142                 cmd = [Utils.subst_vars('${PYTHON}', self.env), Utils.subst_vars('${PYFLAGS_OPT}', self.env), '-c', INST, self.inputs[0].abspath(), self.outputs[0].abspath(), self.pyd]
143                 ret = self.generator.bld.exec_command(cmd)
144                 return ret
145
146 @feature('pyext')
147 @before_method('propagate_uselib_vars', 'apply_link')
148 @after_method('apply_bundle')
149 def init_pyext(self):
150         """
151         Change the values of *cshlib_PATTERN* and *cxxshlib_PATTERN* to remove the
152         *lib* prefix from library names.
153         """
154         self.uselib = self.to_list(getattr(self, 'uselib', []))
155         if not 'PYEXT' in self.uselib:
156                 self.uselib.append('PYEXT')
157         # override shlib_PATTERN set by the osx module
158         self.env.cshlib_PATTERN = self.env.cxxshlib_PATTERN = self.env.macbundle_PATTERN = self.env.pyext_PATTERN
159         self.env.fcshlib_PATTERN = self.env.dshlib_PATTERN = self.env.pyext_PATTERN
160
161         try:
162                 if not self.install_path:
163                         return
164         except AttributeError:
165                 self.install_path = '${PYTHONARCHDIR}'
166
167 @feature('pyext')
168 @before_method('apply_link', 'apply_bundle')
169 def set_bundle(self):
170         """Mac-specific pyext extension that enables bundles from c_osx.py"""
171         if Utils.unversioned_sys_platform() == 'darwin':
172                 self.mac_bundle = True
173
174 @before_method('propagate_uselib_vars')
175 @feature('pyembed')
176 def init_pyembed(self):
177         """
178         Add the PYEMBED variable.
179         """
180         self.uselib = self.to_list(getattr(self, 'uselib', []))
181         if not 'PYEMBED' in self.uselib:
182                 self.uselib.append('PYEMBED')
183
184 @conf
185 def get_python_variables(self, variables, imports=None):
186         """
187         Spawn a new python process to dump configuration variables
188
189         :param variables: variables to print
190         :type variables: list of string
191         :param imports: one import by element
192         :type imports: list of string
193         :return: the variable values
194         :rtype: list of string
195         """
196         if not imports:
197                 try:
198                         imports = self.python_imports
199                 except AttributeError:
200                         imports = DISTUTILS_IMP
201
202         program = list(imports) # copy
203         program.append('')
204         for v in variables:
205                 program.append("print(repr(%s))" % v)
206         os_env = dict(os.environ)
207         try:
208                 del os_env['MACOSX_DEPLOYMENT_TARGET'] # see comments in the OSX tool
209         except KeyError:
210                 pass
211
212         try:
213                 out = self.cmd_and_log(self.env.PYTHON + ['-c', '\n'.join(program)], env=os_env)
214         except Errors.WafError:
215                 self.fatal('The distutils module is unusable: install "python-devel"?')
216         self.to_log(out)
217         return_values = []
218         for s in out.splitlines():
219                 s = s.strip()
220                 if not s:
221                         continue
222                 if s == 'None':
223                         return_values.append(None)
224                 elif (s[0] == "'" and s[-1] == "'") or (s[0] == '"' and s[-1] == '"'):
225                         return_values.append(eval(s))
226                 elif s[0].isdigit():
227                         return_values.append(int(s))
228                 else: break
229         return return_values
230
231 @conf
232 def test_pyembed(self, mode, msg='Testing pyembed configuration'):
233         self.check(header_name='Python.h', define_name='HAVE_PYEMBED', msg=msg,
234                 fragment=FRAG, errmsg='Could not build a python embedded interpreter',
235                 features='%s %sprogram pyembed' % (mode, mode))
236
237 @conf
238 def test_pyext(self, mode, msg='Testing pyext configuration'):
239         self.check(header_name='Python.h', define_name='HAVE_PYEXT', msg=msg,
240                 fragment=FRAG, errmsg='Could not build python extensions',
241                 features='%s %sshlib pyext' % (mode, mode))
242
243 @conf
244 def python_cross_compile(self, features='pyembed pyext'):
245         """
246         For cross-compilation purposes, it is possible to bypass the normal detection and set the flags that you want:
247         PYTHON_VERSION='3.4' PYTAG='cpython34' pyext_PATTERN="%s.so" PYTHON_LDFLAGS='-lpthread -ldl' waf configure
248
249         The following variables are used:
250         PYTHON_VERSION    required
251         PYTAG             required
252         PYTHON_LDFLAGS    required
253         pyext_PATTERN     required
254         PYTHON_PYEXT_LDFLAGS
255         PYTHON_PYEMBED_LDFLAGS
256         """
257         features = Utils.to_list(features)
258         if not ('PYTHON_LDFLAGS' in self.environ or 'PYTHON_PYEXT_LDFLAGS' in self.environ or 'PYTHON_PYEMBED_LDFLAGS' in self.environ):
259                 return False
260
261         for x in 'PYTHON_VERSION PYTAG pyext_PATTERN'.split():
262                 if not x in self.environ:
263                         self.fatal('Please set %s in the os environment' % x)
264                 else:
265                         self.env[x] = self.environ[x]
266
267         xx = self.env.CXX_NAME and 'cxx' or 'c'
268         if 'pyext' in features:
269                 flags = self.environ.get('PYTHON_PYEXT_LDFLAGS', self.environ.get('PYTHON_LDFLAGS'))
270                 if flags is None:
271                         self.fatal('No flags provided through PYTHON_PYEXT_LDFLAGS as required')
272                 else:
273                         self.parse_flags(flags, 'PYEXT')
274                 self.test_pyext(xx)
275         if 'pyembed' in features:
276                 flags = self.environ.get('PYTHON_PYEMBED_LDFLAGS', self.environ.get('PYTHON_LDFLAGS'))
277                 if flags is None:
278                         self.fatal('No flags provided through PYTHON_PYEMBED_LDFLAGS as required')
279                 else:
280                         self.parse_flags(flags, 'PYEMBED')
281                 self.test_pyembed(xx)
282         return True
283
284 @conf
285 def check_python_headers(conf, features='pyembed pyext'):
286         """
287         Check for headers and libraries necessary to extend or embed python by using the module *distutils*.
288         On success the environment variables xxx_PYEXT and xxx_PYEMBED are added:
289
290         * PYEXT: for compiling python extensions
291         * PYEMBED: for embedding a python interpreter
292         """
293         features = Utils.to_list(features)
294         assert ('pyembed' in features) or ('pyext' in features), "check_python_headers features must include 'pyembed' and/or 'pyext'"
295         env = conf.env
296         if not env.CC_NAME and not env.CXX_NAME:
297                 conf.fatal('load a compiler first (gcc, g++, ..)')
298
299         # bypass all the code below for cross-compilation
300         if conf.python_cross_compile(features):
301                 return
302
303         if not env.PYTHON_VERSION:
304                 conf.check_python_version()
305
306         pybin = env.PYTHON
307         if not pybin:
308                 conf.fatal('Could not find the python executable')
309
310         # so we actually do all this for compatibility reasons and for obtaining pyext_PATTERN below
311         v = 'prefix SO LDFLAGS LIBDIR LIBPL INCLUDEPY Py_ENABLE_SHARED MACOSX_DEPLOYMENT_TARGET LDSHARED CFLAGS LDVERSION'.split()
312         try:
313                 lst = conf.get_python_variables(["get_config_var('%s') or ''" % x for x in v])
314         except RuntimeError:
315                 conf.fatal("Python development headers not found (-v for details).")
316
317         vals = ['%s = %r' % (x, y) for (x, y) in zip(v, lst)]
318         conf.to_log("Configuration returned from %r:\n%s\n" % (pybin, '\n'.join(vals)))
319
320         dct = dict(zip(v, lst))
321         x = 'MACOSX_DEPLOYMENT_TARGET'
322         if dct[x]:
323                 env[x] = conf.environ[x] = dct[x]
324         env.pyext_PATTERN = '%s' + dct['SO'] # not a mistake
325
326
327         # Try to get pythonX.Y-config
328         num = '.'.join(env.PYTHON_VERSION.split('.')[:2])
329         conf.find_program([''.join(pybin) + '-config', 'python%s-config' % num, 'python-config-%s' % num, 'python%sm-config' % num], var='PYTHON_CONFIG', msg="python-config", mandatory=False)
330
331         if env.PYTHON_CONFIG:
332                 # check python-config output only once
333                 if conf.env.HAVE_PYTHON_H:
334                         return
335
336                 # python2.6-config requires 3 runs
337                 all_flags = [['--cflags', '--libs', '--ldflags']]
338                 if sys.hexversion < 0x2070000:
339                         all_flags = [[k] for k in all_flags[0]]
340
341                 xx = env.CXX_NAME and 'cxx' or 'c'
342
343                 if 'pyembed' in features:
344                         for flags in all_flags:
345                                 # Python 3.8 has different flags for pyembed, needs --embed
346                                 embedflags = flags + ['--embed']
347                                 try:
348                                         conf.check_cfg(msg='Asking python-config for pyembed %r flags' % ' '.join(embedflags), path=env.PYTHON_CONFIG, package='', uselib_store='PYEMBED', args=embedflags)
349                                 except conf.errors.ConfigurationError:
350                                         # However Python < 3.8 doesn't accept --embed, so we need a fallback
351                                         conf.check_cfg(msg='Asking python-config for pyembed %r flags' % ' '.join(flags), path=env.PYTHON_CONFIG, package='', uselib_store='PYEMBED', args=flags)
352
353                         try:
354                                 conf.test_pyembed(xx)
355                         except conf.errors.ConfigurationError:
356                                 # python bug 7352
357                                 if dct['Py_ENABLE_SHARED'] and dct['LIBDIR']:
358                                         env.append_unique('LIBPATH_PYEMBED', [dct['LIBDIR']])
359                                         conf.test_pyembed(xx)
360                                 else:
361                                         raise
362
363                 if 'pyext' in features:
364                         for flags in all_flags:
365                                 conf.check_cfg(msg='Asking python-config for pyext %r flags' % ' '.join(flags), path=env.PYTHON_CONFIG, package='', uselib_store='PYEXT', args=flags)
366
367                         try:
368                                 conf.test_pyext(xx)
369                         except conf.errors.ConfigurationError:
370                                 # python bug 7352
371                                 if dct['Py_ENABLE_SHARED'] and dct['LIBDIR']:
372                                         env.append_unique('LIBPATH_PYEXT', [dct['LIBDIR']])
373                                         conf.test_pyext(xx)
374                                 else:
375                                         raise
376
377                 conf.define('HAVE_PYTHON_H', 1)
378                 return
379
380         # No python-config, do something else on windows systems
381         all_flags = dct['LDFLAGS'] + ' ' + dct['CFLAGS']
382         conf.parse_flags(all_flags, 'PYEMBED')
383
384         all_flags = dct['LDFLAGS'] + ' ' + dct['LDSHARED'] + ' ' + dct['CFLAGS']
385         conf.parse_flags(all_flags, 'PYEXT')
386
387         result = None
388         if not dct["LDVERSION"]:
389                 dct["LDVERSION"] = env.PYTHON_VERSION
390
391         # further simplification will be complicated
392         for name in ('python' + dct['LDVERSION'], 'python' + env.PYTHON_VERSION + 'm', 'python' + env.PYTHON_VERSION.replace('.', '')):
393
394                 # LIBPATH_PYEMBED is already set; see if it works.
395                 if not result and env.LIBPATH_PYEMBED:
396                         path = env.LIBPATH_PYEMBED
397                         conf.to_log("\n\n# Trying default LIBPATH_PYEMBED: %r\n" % path)
398                         result = conf.check(lib=name, uselib='PYEMBED', libpath=path, mandatory=False, msg='Checking for library %s in LIBPATH_PYEMBED' % name)
399
400                 if not result and dct['LIBDIR']:
401                         path = [dct['LIBDIR']]
402                         conf.to_log("\n\n# try again with -L$python_LIBDIR: %r\n" % path)
403                         result = conf.check(lib=name, uselib='PYEMBED', libpath=path, mandatory=False, msg='Checking for library %s in LIBDIR' % name)
404
405                 if not result and dct['LIBPL']:
406                         path = [dct['LIBPL']]
407                         conf.to_log("\n\n# try again with -L$python_LIBPL (some systems don't install the python library in $prefix/lib)\n")
408                         result = conf.check(lib=name, uselib='PYEMBED', libpath=path, mandatory=False, msg='Checking for library %s in python_LIBPL' % name)
409
410                 if not result:
411                         path = [os.path.join(dct['prefix'], "libs")]
412                         conf.to_log("\n\n# try again with -L$prefix/libs, and pythonXY name rather than pythonX.Y (win32)\n")
413                         result = conf.check(lib=name, uselib='PYEMBED', libpath=path, mandatory=False, msg='Checking for library %s in $prefix/libs' % name)
414
415                 if result:
416                         break # do not forget to set LIBPATH_PYEMBED
417
418         if result:
419                 env.LIBPATH_PYEMBED = path
420                 env.append_value('LIB_PYEMBED', [name])
421         else:
422                 conf.to_log("\n\n### LIB NOT FOUND\n")
423
424         # under certain conditions, python extensions must link to
425         # python libraries, not just python embedding programs.
426         if Utils.is_win32 or dct['Py_ENABLE_SHARED']:
427                 env.LIBPATH_PYEXT = env.LIBPATH_PYEMBED
428                 env.LIB_PYEXT = env.LIB_PYEMBED
429
430         conf.to_log("Include path for Python extensions (found via distutils module): %r\n" % (dct['INCLUDEPY'],))
431         env.INCLUDES_PYEXT = [dct['INCLUDEPY']]
432         env.INCLUDES_PYEMBED = [dct['INCLUDEPY']]
433
434         # Code using the Python API needs to be compiled with -fno-strict-aliasing
435         if env.CC_NAME == 'gcc':
436                 env.append_value('CFLAGS_PYEMBED', ['-fno-strict-aliasing'])
437                 env.append_value('CFLAGS_PYEXT', ['-fno-strict-aliasing'])
438         if env.CXX_NAME == 'gcc':
439                 env.append_value('CXXFLAGS_PYEMBED', ['-fno-strict-aliasing'])
440                 env.append_value('CXXFLAGS_PYEXT', ['-fno-strict-aliasing'])
441
442         if env.CC_NAME == "msvc":
443                 from distutils.msvccompiler import MSVCCompiler
444                 dist_compiler = MSVCCompiler()
445                 dist_compiler.initialize()
446                 env.append_value('CFLAGS_PYEXT', dist_compiler.compile_options)
447                 env.append_value('CXXFLAGS_PYEXT', dist_compiler.compile_options)
448                 env.append_value('LINKFLAGS_PYEXT', dist_compiler.ldflags_shared)
449
450         # See if it compiles
451         conf.check(header_name='Python.h', define_name='HAVE_PYTHON_H', uselib='PYEMBED', fragment=FRAG, errmsg='Distutils not installed? Broken python installation? Get python-config now!')
452
453 @conf
454 def check_python_version(conf, minver=None):
455         """
456         Check if the python interpreter is found matching a given minimum version.
457         minver should be a tuple, eg. to check for python >= 2.4.2 pass (2,4,2) as minver.
458
459         If successful, PYTHON_VERSION is defined as 'MAJOR.MINOR' (eg. '2.4')
460         of the actual python version found, and PYTHONDIR and PYTHONARCHDIR
461         are defined, pointing to the site-packages directories appropriate for
462         this python version, where modules/packages/extensions should be
463         installed.
464
465         :param minver: minimum version
466         :type minver: tuple of int
467         """
468         assert minver is None or isinstance(minver, tuple)
469         pybin = conf.env.PYTHON
470         if not pybin:
471                 conf.fatal('could not find the python executable')
472
473         # Get python version string
474         cmd = pybin + ['-c', 'import sys\nfor x in sys.version_info: print(str(x))']
475         Logs.debug('python: Running python command %r', cmd)
476         lines = conf.cmd_and_log(cmd).split()
477         assert len(lines) == 5, "found %r lines, expected 5: %r" % (len(lines), lines)
478         pyver_tuple = (int(lines[0]), int(lines[1]), int(lines[2]), lines[3], int(lines[4]))
479
480         # Compare python version with the minimum required
481         result = (minver is None) or (pyver_tuple >= minver)
482
483         if result:
484                 # define useful environment variables
485                 pyver = '.'.join([str(x) for x in pyver_tuple[:2]])
486                 conf.env.PYTHON_VERSION = pyver
487
488                 if 'PYTHONDIR' in conf.env:
489                         # Check if --pythondir was specified
490                         pydir = conf.env.PYTHONDIR
491                 elif 'PYTHONDIR' in conf.environ:
492                         # Check environment for PYTHONDIR
493                         pydir = conf.environ['PYTHONDIR']
494                 else:
495                         # Finally, try to guess
496                         if Utils.is_win32:
497                                 (python_LIBDEST, pydir) = conf.get_python_variables(
498                                           ["get_config_var('LIBDEST') or ''",
499                                            "get_python_lib(standard_lib=0) or ''"])
500                         else:
501                                 python_LIBDEST = None
502                                 (pydir,) = conf.get_python_variables( ["get_python_lib(standard_lib=0, prefix=%r) or ''" % conf.env.PREFIX])
503                         if python_LIBDEST is None:
504                                 if conf.env.LIBDIR:
505                                         python_LIBDEST = os.path.join(conf.env.LIBDIR, 'python' + pyver)
506                                 else:
507                                         python_LIBDEST = os.path.join(conf.env.PREFIX, 'lib', 'python' + pyver)
508
509                 if 'PYTHONARCHDIR' in conf.env:
510                         # Check if --pythonarchdir was specified
511                         pyarchdir = conf.env.PYTHONARCHDIR
512                 elif 'PYTHONARCHDIR' in conf.environ:
513                         # Check environment for PYTHONDIR
514                         pyarchdir = conf.environ['PYTHONARCHDIR']
515                 else:
516                         # Finally, try to guess
517                         (pyarchdir, ) = conf.get_python_variables( ["get_python_lib(plat_specific=1, standard_lib=0, prefix=%r) or ''" % conf.env.PREFIX])
518                         if not pyarchdir:
519                                 pyarchdir = pydir
520
521                 if hasattr(conf, 'define'): # conf.define is added by the C tool, so may not exist
522                         conf.define('PYTHONDIR', pydir)
523                         conf.define('PYTHONARCHDIR', pyarchdir)
524
525                 conf.env.PYTHONDIR = pydir
526                 conf.env.PYTHONARCHDIR = pyarchdir
527
528         # Feedback
529         pyver_full = '.'.join(map(str, pyver_tuple[:3]))
530         if minver is None:
531                 conf.msg('Checking for python version', pyver_full)
532         else:
533                 minver_str = '.'.join(map(str, minver))
534                 conf.msg('Checking for python version >= %s' % (minver_str,), pyver_full, color=result and 'GREEN' or 'YELLOW')
535
536         if not result:
537                 conf.fatal('The python version is too old, expecting %r' % (minver,))
538
539 PYTHON_MODULE_TEMPLATE = '''
540 import %s as current_module
541 version = getattr(current_module, '__version__', None)
542 if version is not None:
543         print(str(version))
544 else:
545         print('unknown version')
546 '''
547
548 @conf
549 def check_python_module(conf, module_name, condition=''):
550         """
551         Check if the selected python interpreter can import the given python module::
552
553                 def configure(conf):
554                         conf.check_python_module('pygccxml')
555                         conf.check_python_module('re', condition="ver > num(2, 0, 4) and ver <= num(3, 0, 0)")
556
557         :param module_name: module
558         :type module_name: string
559         """
560         msg = "Checking for python module %r" % module_name
561         if condition:
562                 msg = '%s (%s)' % (msg, condition)
563         conf.start_msg(msg)
564         try:
565                 ret = conf.cmd_and_log(conf.env.PYTHON + ['-c', PYTHON_MODULE_TEMPLATE % module_name])
566         except Errors.WafError:
567                 conf.end_msg(False)
568                 conf.fatal('Could not find the python module %r' % module_name)
569
570         ret = ret.strip()
571         if condition:
572                 conf.end_msg(ret)
573                 if ret == 'unknown version':
574                         conf.fatal('Could not check the %s version' % module_name)
575
576                 from distutils.version import LooseVersion
577                 def num(*k):
578                         if isinstance(k[0], int):
579                                 return LooseVersion('.'.join([str(x) for x in k]))
580                         else:
581                                 return LooseVersion(k[0])
582                 d = {'num': num, 'ver': LooseVersion(ret)}
583                 ev = eval(condition, {}, d)
584                 if not ev:
585                         conf.fatal('The %s version does not satisfy the requirements' % module_name)
586         else:
587                 if ret == 'unknown version':
588                         conf.end_msg(True)
589                 else:
590                         conf.end_msg(ret)
591
592 def configure(conf):
593         """
594         Detect the python interpreter
595         """
596         v = conf.env
597         if getattr(Options.options, 'pythondir', None):
598                 v.PYTHONDIR = Options.options.pythondir
599         if getattr(Options.options, 'pythonarchdir', None):
600                 v.PYTHONARCHDIR = Options.options.pythonarchdir
601         if getattr(Options.options, 'nopycache', None):
602                 v.NOPYCACHE=Options.options.nopycache
603
604         if not v.PYTHON:
605                 v.PYTHON = [getattr(Options.options, 'python', None) or sys.executable]
606         v.PYTHON = Utils.to_list(v.PYTHON)
607         conf.find_program('python', var='PYTHON')
608
609         v.PYFLAGS = ''
610         v.PYFLAGS_OPT = '-O'
611
612         v.PYC = getattr(Options.options, 'pyc', 1)
613         v.PYO = getattr(Options.options, 'pyo', 1)
614
615         try:
616                 v.PYTAG = conf.cmd_and_log(conf.env.PYTHON + ['-c', "import imp;print(imp.get_tag())"]).strip()
617         except Errors.WafError:
618                 pass
619
620 def options(opt):
621         """
622         Add python-specific options
623         """
624         pyopt=opt.add_option_group("Python Options")
625         pyopt.add_option('--nopyc', dest = 'pyc', action='store_false', default=1,
626                                          help = 'Do not install bytecode compiled .pyc files (configuration) [Default:install]')
627         pyopt.add_option('--nopyo', dest='pyo', action='store_false', default=1,
628                                          help='Do not install optimised compiled .pyo files (configuration) [Default:install]')
629         pyopt.add_option('--nopycache',dest='nopycache', action='store_true',
630                                          help='Do not use __pycache__ directory to install objects [Default:auto]')
631         pyopt.add_option('--python', dest="python",
632                                          help='python binary to be used [Default: %s]' % sys.executable)
633         pyopt.add_option('--pythondir', dest='pythondir',
634                                          help='Installation path for python modules (py, platform-independent .py and .pyc files)')
635         pyopt.add_option('--pythonarchdir', dest='pythonarchdir',
636                                          help='Installation path for python extension (pyext, platform-dependent .so or .dylib files)')
637