1 # waf build tool for building IDL files with pidl
4 from waflib
import Build
, Logs
, Utils
, Configure
, Errors
5 from waflib
.Configure
import 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
)
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)
24 conf
.check_python_version((3, 3, 0))
26 Logs
.warn('extra-python needs to be Python 3.3 or later')
28 interpreters
.append(conf
.env
['PYTHON'])
29 conf
.setenv('default')
31 conf
.find_program('python', var
='PYTHON', mandatory
=mandatory
)
33 path_python
= conf
.find_program('python')
34 conf
.env
.PYTHON_SPECIFIED
= (conf
.env
.PYTHON
!= path_python
)
35 conf
.check_python_version(version
)
37 interpreters
.append(conf
.env
['PYTHON'])
38 conf
.env
.python_interpreters
= interpreters
42 def SAMBA_CHECK_PYTHON_HEADERS(conf
, mandatory
=True):
43 if conf
.env
.disable_python
:
45 raise Errors
.WafError("Cannot check for python headers when "
46 "--disable-python specified")
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=')]
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')
63 _check_python_headers(conf
, mandatory
)
64 conf
.env
["python_headers_checked"] = "yes"
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']))
72 conf
.msg("python headers", "using cache")
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=')]
80 def _check_python_headers(conf
, mandatory
):
82 conf
.errors
.ConfigurationError
83 conf
.check_python_headers()
84 except conf
.errors
.ConfigurationError
:
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
% ''
92 conf
.env
['PYTHON_SO_ABI_FLAG'] = ''
93 conf
.env
['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
94 conf
.env
['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
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
)
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']
108 def PYTHON_BUILD_IS_ENABLED(self
):
109 return self
.CONFIG_SET('HAVE_PYTHON_H')
111 Build
.BuildContext
.PYTHON_BUILD_IS_ENABLED
= PYTHON_BUILD_IS_ENABLED
114 def SAMBA_PYTHON(bld
, name
,
122 init_function_sentinel
=None,
127 '''build a python extension for Samba'''
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():
134 if bld
.env
['IS_EXTRA_PYTHON']:
135 name
= 'extra-' + name
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
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.
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'
153 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
155 if realname
is not None:
156 link_name
= 'python/%s' % realname
160 bld
.SAMBA_LIBRARY(name
,
163 public_deps
=public_deps
,
166 cflags_end
=cflags_end
,
167 local_include
=local_include
,
172 target_type
='PYTHON',
173 install_path
='${PYTHONARCHDIR}',
174 allow_undefined_symbols
=True,
178 Build
.BuildContext
.SAMBA_PYTHON
= SAMBA_PYTHON
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']
187 Build
.BuildContext
.pyembed_libname
= pyembed_libname
190 def gen_python_environments(bld
, extra_env_vars
=()):
191 """Generate all Python environments
193 To be used in a for loop. Normally, the loop body will be executed once.
195 When --extra-python is used, the body will additionaly be executed
196 with the extra-python environment active.
200 if bld
.env
['EXTRA_PYTHON']:
201 copied
= ('GLOBAL_DEPENDENCIES', 'TARGET_TYPE') + tuple(extra_env_vars
)
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']
207 bld
.all_envs
['default'] = default_env
209 Build
.BuildContext
.gen_python_environments
= gen_python_environments