build: Set default minimum python version to 3.4.0
[sfrench/samba-autobuild/.git] / buildtools / wafsamba / samba_python.py
1 # waf build tool for building IDL files with pidl
2
3 import os, sys
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=(3,4,0)):
9
10     if not mandatory:
11         version=(2,6,0)
12
13     # enable tool to build python extensions
14     if conf.env.HAVE_PYTHON_H:
15         conf.check_python_version(version)
16         return
17
18     interpreters = []
19
20     conf.find_program('python3', var='PYTHON', mandatory=mandatory)
21     conf.load('python')
22     path_python = conf.find_program('python3')
23
24     conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
25     conf.check_python_version(version)
26
27     interpreters.append(conf.env['PYTHON'])
28     conf.env.python_interpreters = interpreters
29
30
31 @conf
32 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
33     if conf.env.disable_python:
34         if mandatory:
35             raise Errors.WafError("Cannot check for python headers when "
36                                  "--disable-python specified")
37
38         conf.msg("python headers", "Check disabled due to --disable-python")
39         # we don't want PYTHONDIR in config.h, as otherwise changing
40         # --prefix causes a complete rebuild
41         conf.env.DEFINES = [x for x in conf.env.DEFINES
42             if not x.startswith('PYTHONDIR=')
43             and not x.startswith('PYTHONARCHDIR=')]
44
45         return
46
47     if conf.env["python_headers_checked"] == []:
48         _check_python_headers(conf, mandatory)
49         conf.env["python_headers_checked"] = "yes"
50
51     else:
52         conf.msg("python headers", "using cache")
53
54     # we don't want PYTHONDIR in config.h, as otherwise changing
55     # --prefix causes a complete rebuild
56     conf.env.DEFINES = [x for x in conf.env.DEFINES
57         if not x.startswith('PYTHONDIR=')
58         and not x.startswith('PYTHONARCHDIR=')]
59
60 def _check_python_headers(conf, mandatory):
61     try:
62         conf.errors.ConfigurationError
63         conf.check_python_headers()
64     except conf.errors.ConfigurationError:
65         if mandatory:
66              raise
67
68     if conf.env['PYTHON_VERSION'] > '3':
69         abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
70         conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
71     else:
72         conf.env['PYTHON_SO_ABI_FLAG'] = ''
73     conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
74         conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
75
76     for lib in conf.env['LINKFLAGS_PYEMBED']:
77         if lib.startswith('-L'):
78             conf.env.append_unique('LIBPATH_PYEMBED', lib[2:]) # strip '-L'
79             conf.env['LINKFLAGS_PYEMBED'].remove(lib)
80
81     # same as in waf 1.5, keep only '-fno-strict-aliasing'
82     # and ignore defines such as NDEBUG _FORTIFY_SOURCE=2
83     conf.env.DEFINES_PYEXT = []
84     conf.env.CFLAGS_PYEXT = ['-fno-strict-aliasing']
85
86     return
87
88 def PYTHON_BUILD_IS_ENABLED(self):
89     return self.CONFIG_SET('HAVE_PYTHON_H')
90
91 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
92
93
94 def SAMBA_PYTHON(bld, name,
95                  source='',
96                  deps='',
97                  public_deps='',
98                  realname=None,
99                  cflags='',
100                  cflags_end=None,
101                  includes='',
102                  init_function_sentinel=None,
103                  local_include=True,
104                  vars=None,
105                  install=True,
106                  enabled=True):
107     '''build a python extension for Samba'''
108
109     # force-disable when we can't build python modules, so
110     # every single call doesn't need to pass this in.
111     if not bld.PYTHON_BUILD_IS_ENABLED():
112         enabled = False
113
114     # when we support static python modules we'll need to gather
115     # the list from all the SAMBA_PYTHON() targets
116     if init_function_sentinel is not None:
117         cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
118
119     # From https://docs.python.org/2/c-api/arg.html:
120     # Starting with Python 2.5 the type of the length argument to
121     # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
122     # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
123     # including Python.h. If the macro is defined, length is a Py_ssize_t
124     # rather than an int.
125
126     # Because <Python.h> if often included before includes.h/config.h
127     # This must be in the -D compiler options
128     cflags += ' -DPY_SSIZE_T_CLEAN=1'
129
130     source = bld.EXPAND_VARIABLES(source, vars=vars)
131
132     if realname is not None:
133         link_name = 'python/%s' % realname
134     else:
135         link_name = None
136
137     bld.SAMBA_LIBRARY(name,
138                       source=source,
139                       deps=deps,
140                       public_deps=public_deps,
141                       includes=includes,
142                       cflags=cflags,
143                       cflags_end=cflags_end,
144                       local_include=local_include,
145                       vars=vars,
146                       realname=realname,
147                       link_name=link_name,
148                       pyext=True,
149                       target_type='PYTHON',
150                       install_path='${PYTHONARCHDIR}',
151                       allow_undefined_symbols=True,
152                       install=install,
153                       enabled=enabled)
154
155 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
156
157
158 def pyembed_libname(bld, name):
159     if bld.env['PYTHON_SO_ABI_FLAG']:
160         return name + bld.env['PYTHON_SO_ABI_FLAG']
161     else:
162         return name
163
164 Build.BuildContext.pyembed_libname = pyembed_libname
165
166