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