buildtools: Add --extra-python configure option
[samba.git] / buildtools / wafsamba / samba_python.py
1 # waf build tool for building IDL files with pidl
2
3 import Build
4 from samba_utils import *
5 from samba_autoconf import *
6
7 from Configure import conf
8
9 @conf
10 def SAMBA_CHECK_PYTHON(conf, mandatory=True, version=(2,4,2)):
11     # enable tool to build python extensions
12     if conf.env['EXTRA_PYTHON']:
13         conf.all_envs['extrapython'] = conf.env.copy()
14         conf.setenv('extrapython')
15         conf.env['PYTHON'] = conf.env['EXTRA_PYTHON']
16         conf.env['IS_EXTRA_PYTHON'] = 'yes'
17         conf.find_program('python', var='PYTHON', mandatory=True)
18         conf.check_tool('python')
19         try:
20             conf.check_python_version((3, 3, 0))
21         except Exception:
22             warn('extra-python needs to be Python 3.3 or later')
23             raise
24         conf.setenv('default')
25
26     conf.find_program('python', var='PYTHON', mandatory=mandatory)
27     conf.check_tool('python')
28     path_python = conf.find_program('python')
29     conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
30     conf.check_python_version(version)
31
32
33 @conf
34 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
35     if conf.env["python_headers_checked"] == []:
36         if conf.env['EXTRA_PYTHON']:
37             conf.setenv('extrapython')
38             _check_python_headers(conf, mandatory=True)
39             conf.setenv('default')
40
41         _check_python_headers(conf, mandatory)
42         conf.env["python_headers_checked"] = "yes"
43
44         if conf.env['EXTRA_PYTHON']:
45             extraversion = conf.all_envs['extrapython']['PYTHON_VERSION']
46             if extraversion == conf.env['PYTHON_VERSION']:
47                 raise Utils.WafError("extrapython %s is same as main python %s" % (
48                     extraversion, conf.env['PYTHON_VERSION']))
49     else:
50         conf.msg("python headers", "using cache")
51
52
53 def _check_python_headers(conf, mandatory):
54     conf.check_python_headers(mandatory=mandatory)
55
56     if conf.env['PYTHON_VERSION'] > '3':
57         abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
58         conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
59     else:
60         conf.env['PYTHON_SO_ABI_FLAG'] = ''
61
62
63 def SAMBA_PYTHON(bld, name,
64                  source='',
65                  deps='',
66                  public_deps='',
67                  realname=None,
68                  cflags='',
69                  includes='',
70                  init_function_sentinel=None,
71                  local_include=True,
72                  vars=None,
73                  install=True,
74                  enabled=True):
75     '''build a python extension for Samba'''
76
77     # when we support static python modules we'll need to gather
78     # the list from all the SAMBA_PYTHON() targets
79     if init_function_sentinel is not None:
80         cflags += '-DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
81
82     source = bld.EXPAND_VARIABLES(source, vars=vars)
83
84     if realname is not None:
85         link_name = 'python_modules/%s' % realname
86     else:
87         link_name = None
88
89     bld.SAMBA_LIBRARY(name,
90                       source=source,
91                       deps=deps,
92                       public_deps=public_deps,
93                       includes=includes,
94                       cflags=cflags,
95                       local_include=local_include,
96                       vars=vars,
97                       realname=realname,
98                       link_name=link_name,
99                       pyext=True,
100                       target_type='PYTHON',
101                       install_path='${PYTHONARCHDIR}',
102                       allow_undefined_symbols=True,
103                       allow_warnings=True,
104                       install=install,
105                       enabled=enabled)
106
107 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
108
109
110 def pyembed_libname(bld, name, extrapython=False):
111     return name + bld.env['PYTHON_SO_ABI_FLAG']
112
113 Build.BuildContext.pyembed_libname = pyembed_libname