buildtools/wafsamba: remove ENFORCE_GROUP_ORDERING
[samba.git] / buildtools / wafsamba / samba_python.py
1 # waf build tool for building IDL files with pidl
2
3 import os
4 from waflib import Build, Logs, Utils, Configure, Errors
5 from waflib.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.derive()
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.load('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.load('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 Errors.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         conf.env.DEFINES = [x for x in conf.env.DEFINES
52             if not x.startswith('PYTHONDIR=')
53             and not x.startswith('PYTHONARCHDIR=')]
54
55         return
56
57     if conf.env["python_headers_checked"] == []:
58         if conf.env['EXTRA_PYTHON']:
59             conf.setenv('extrapython')
60             _check_python_headers(conf, mandatory=True)
61             conf.setenv('default')
62
63         _check_python_headers(conf, mandatory)
64         conf.env["python_headers_checked"] = "yes"
65
66         if conf.env['EXTRA_PYTHON']:
67             extraversion = conf.all_envs['extrapython']['PYTHON_VERSION']
68             if extraversion == conf.env['PYTHON_VERSION']:
69                 raise Errors.WafError("extrapython %s is same as main python %s" % (
70                     extraversion, conf.env['PYTHON_VERSION']))
71     else:
72         conf.msg("python headers", "using cache")
73
74     # we don't want PYTHONDIR in config.h, as otherwise changing
75     # --prefix causes a complete rebuild
76     conf.env.DEFINES = [x for x in conf.env.DEFINES
77         if not x.startswith('PYTHONDIR=')
78         and not x.startswith('PYTHONARCHDIR=')]
79
80 def _check_python_headers(conf, mandatory):
81     try:
82         conf.errors.ConfigurationError
83         conf.check_python_headers()
84     except conf.errors.ConfigurationError:
85         if mandatory:
86              raise
87
88     if conf.env['PYTHON_VERSION'] > '3':
89         abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
90         conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
91     else:
92         conf.env['PYTHON_SO_ABI_FLAG'] = ''
93     conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
94         conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
95
96     for lib in conf.env['LINKFLAGS_PYEMBED']:
97         if lib.startswith('-L'):
98             conf.env.append_unique('LIBPATH_PYEMBED', lib[2:]) # strip '-L'
99             conf.env['LINKFLAGS_PYEMBED'].remove(lib)
100
101     # same as in waf 1.5, keep only '-fno-strict-aliasing'
102     # and ignore defines such as NDEBUG _FORTIFY_SOURCE=2
103     conf.env.DEFINES_PYEXT = []
104     conf.env.CFLAGS_PYEXT = ['-fno-strict-aliasing']
105
106     return
107
108 def PYTHON_BUILD_IS_ENABLED(self):
109     return self.CONFIG_SET('HAVE_PYTHON_H')
110
111 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
112
113
114 def SAMBA_PYTHON(bld, name,
115                  source='',
116                  deps='',
117                  public_deps='',
118                  realname=None,
119                  cflags='',
120                  cflags_end=None,
121                  includes='',
122                  init_function_sentinel=None,
123                  local_include=True,
124                  vars=None,
125                  install=True,
126                  enabled=True):
127     '''build a python extension for Samba'''
128
129     # force-disable when we can't build python modules, so
130     # every single call doesn't need to pass this in.
131     if not bld.PYTHON_BUILD_IS_ENABLED():
132         enabled = False
133
134     if bld.env['IS_EXTRA_PYTHON']:
135         name = 'extra-' + name
136
137     # when we support static python modules we'll need to gather
138     # the list from all the SAMBA_PYTHON() targets
139     if init_function_sentinel is not None:
140         cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
141
142     # From https://docs.python.org/2/c-api/arg.html:
143     # Starting with Python 2.5 the type of the length argument to
144     # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
145     # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
146     # including Python.h. If the macro is defined, length is a Py_ssize_t
147     # rather than an int.
148
149     # Because <Python.h> if often included before includes.h/config.h
150     # This must be in the -D compiler options
151     cflags += ' -DPY_SSIZE_T_CLEAN=1'
152
153     source = bld.EXPAND_VARIABLES(source, vars=vars)
154
155     if realname is not None:
156         link_name = 'python/%s' % realname
157     else:
158         link_name = None
159
160     bld.SAMBA_LIBRARY(name,
161                       source=source,
162                       deps=deps,
163                       public_deps=public_deps,
164                       includes=includes,
165                       cflags=cflags,
166                       cflags_end=cflags_end,
167                       local_include=local_include,
168                       vars=vars,
169                       realname=realname,
170                       link_name=link_name,
171                       pyext=True,
172                       target_type='PYTHON',
173                       install_path='${PYTHONARCHDIR}',
174                       allow_undefined_symbols=True,
175                       install=install,
176                       enabled=enabled)
177
178 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
179
180
181 def pyembed_libname(bld, name, extrapython=False):
182     if bld.env['PYTHON_SO_ABI_FLAG']:
183         return name + bld.env['PYTHON_SO_ABI_FLAG']
184     else:
185         return name
186
187 Build.BuildContext.pyembed_libname = pyembed_libname
188
189
190 def gen_python_environments(bld, extra_env_vars=()):
191     """Generate all Python environments
192
193     To be used in a for loop. Normally, the loop body will be executed once.
194
195     When --extra-python is used, the body will additionaly be executed
196     with the extra-python environment active.
197     """
198     yield
199
200     if bld.env['EXTRA_PYTHON']:
201         copied = ('GLOBAL_DEPENDENCIES', 'TARGET_TYPE') + tuple(extra_env_vars)
202         for name in copied:
203             bld.all_envs['extrapython'][name] = bld.all_envs['default'][name]
204         default_env = bld.all_envs['default']
205         bld.all_envs['default'] = bld.all_envs['extrapython']
206         yield
207         bld.all_envs['default'] = default_env
208
209 Build.BuildContext.gen_python_environments = gen_python_environments