wafsamba: Allow to specify VERSION file path
[samba.git] / buildtools / wafsamba / samba_python.py
1 # waf build tool for building IDL files with pidl
2
3 import os
4 import Build, Logs, Utils, Configure
5 from Configure import conf
6
7 @conf
8 def SAMBA_CHECK_PYTHON(conf, mandatory=True, version=(2,4,2)):
9     # enable tool to build python extensions
10     if conf.env.HAVE_PYTHON_H:
11         conf.check_python_version(version)
12         return
13
14     interpreters = []
15
16     if conf.env['EXTRA_PYTHON']:
17         conf.all_envs['extrapython'] = conf.env.copy()
18         conf.setenv('extrapython')
19         conf.env['PYTHON'] = conf.env['EXTRA_PYTHON']
20         conf.env['IS_EXTRA_PYTHON'] = 'yes'
21         conf.find_program('python', var='PYTHON', mandatory=True)
22         conf.check_tool('python')
23         try:
24             conf.check_python_version((3, 3, 0))
25         except Exception:
26             Logs.warn('extra-python needs to be Python 3.3 or later')
27             raise
28         interpreters.append(conf.env['PYTHON'])
29         conf.setenv('default')
30
31     conf.find_program('python', var='PYTHON', mandatory=mandatory)
32     conf.check_tool('python')
33     path_python = conf.find_program('python')
34     conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
35     conf.check_python_version(version)
36
37     interpreters.append(conf.env['PYTHON'])
38     conf.env.python_interpreters = interpreters
39
40
41 @conf
42 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
43     if conf.env.disable_python:
44         if mandatory:
45             raise Utils.WafError("Cannot check for python headers when "
46                                  "--disable-python specified")
47
48         conf.msg("python headers", "Check disabled due to --disable-python")
49         # we don't want PYTHONDIR in config.h, as otherwise changing
50         # --prefix causes a complete rebuild
51         del(conf.env.defines['PYTHONDIR'])
52         del(conf.env.defines['PYTHONARCHDIR'])
53         return
54
55     if conf.env["python_headers_checked"] == []:
56         if conf.env['EXTRA_PYTHON']:
57             conf.setenv('extrapython')
58             _check_python_headers(conf, mandatory=True)
59             conf.setenv('default')
60
61         _check_python_headers(conf, mandatory)
62         conf.env["python_headers_checked"] = "yes"
63
64         if conf.env['EXTRA_PYTHON']:
65             extraversion = conf.all_envs['extrapython']['PYTHON_VERSION']
66             if extraversion == conf.env['PYTHON_VERSION']:
67                 raise Utils.WafError("extrapython %s is same as main python %s" % (
68                     extraversion, conf.env['PYTHON_VERSION']))
69     else:
70         conf.msg("python headers", "using cache")
71
72     # we don't want PYTHONDIR in config.h, as otherwise changing
73     # --prefix causes a complete rebuild
74     del(conf.env.defines['PYTHONDIR'])
75     del(conf.env.defines['PYTHONARCHDIR'])
76
77 def _check_python_headers(conf, mandatory):
78     try:
79         Configure.ConfigurationError
80         conf.check_python_headers(mandatory=mandatory)
81     except Configure.ConfigurationError:
82         if mandatory:
83              raise
84
85     if conf.env['PYTHON_VERSION'] > '3':
86         abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
87         conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
88     else:
89         conf.env['PYTHON_SO_ABI_FLAG'] = ''
90     conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
91         conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
92
93     for lib in conf.env['LINKFLAGS_PYEMBED']:
94         if lib.startswith('-L'):
95             conf.env.append_unique('LIBPATH_PYEMBED', lib[2:]) # strip '-L'
96             conf.env['LINKFLAGS_PYEMBED'].remove(lib)
97
98     return
99
100 def PYTHON_BUILD_IS_ENABLED(self):
101     return self.CONFIG_SET('HAVE_PYTHON_H')
102
103 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
104
105
106 def SAMBA_PYTHON(bld, name,
107                  source='',
108                  deps='',
109                  public_deps='',
110                  realname=None,
111                  cflags='',
112                  includes='',
113                  init_function_sentinel=None,
114                  local_include=True,
115                  vars=None,
116                  install=True,
117                  enabled=True):
118     '''build a python extension for Samba'''
119
120     # force-disable when we can't build python modules, so
121     # every single call doesn't need to pass this in.
122     if not bld.PYTHON_BUILD_IS_ENABLED():
123         enabled = False
124
125     if bld.env['IS_EXTRA_PYTHON']:
126         name = 'extra-' + name
127
128     # when we support static python modules we'll need to gather
129     # the list from all the SAMBA_PYTHON() targets
130     if init_function_sentinel is not None:
131         cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
132
133     # From https://docs.python.org/2/c-api/arg.html:
134     # Starting with Python 2.5 the type of the length argument to
135     # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
136     # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
137     # including Python.h. If the macro is defined, length is a Py_ssize_t
138     # rather than an int.
139
140     # Because <Python.h> if often included before includes.h/config.h
141     # This must be in the -D compiler options
142     cflags += ' -DPY_SSIZE_T_CLEAN=1'
143
144     source = bld.EXPAND_VARIABLES(source, vars=vars)
145
146     if realname is not None:
147         link_name = 'python_modules/%s' % realname
148     else:
149         link_name = None
150
151     bld.SAMBA_LIBRARY(name,
152                       source=source,
153                       deps=deps,
154                       public_deps=public_deps,
155                       includes=includes,
156                       cflags=cflags,
157                       local_include=local_include,
158                       vars=vars,
159                       realname=realname,
160                       link_name=link_name,
161                       pyext=True,
162                       target_type='PYTHON',
163                       install_path='${PYTHONARCHDIR}',
164                       allow_undefined_symbols=True,
165                       install=install,
166                       enabled=enabled)
167
168 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
169
170
171 def pyembed_libname(bld, name, extrapython=False):
172     if bld.env['PYTHON_SO_ABI_FLAG']:
173         return name + bld.env['PYTHON_SO_ABI_FLAG']
174     else:
175         return name
176
177 Build.BuildContext.pyembed_libname = pyembed_libname
178
179
180 def gen_python_environments(bld, extra_env_vars=()):
181     """Generate all Python environments
182
183     To be used in a for loop. Normally, the loop body will be executed once.
184
185     When --extra-python is used, the body will additionaly be executed
186     with the extra-python environment active.
187     """
188     yield
189
190     if bld.env['EXTRA_PYTHON']:
191         copied = ('GLOBAL_DEPENDENCIES', 'TARGET_TYPE') + tuple(extra_env_vars)
192         for name in copied:
193             bld.all_envs['extrapython'][name] = bld.all_envs['default'][name]
194         default_env = bld.all_envs['default']
195         bld.all_envs['default'] = bld.all_envs['extrapython']
196         yield
197         bld.all_envs['default'] = default_env
198
199 Build.BuildContext.gen_python_environments = gen_python_environments