1 # waf build tool for building IDL files with pidl
4 import Build
, Logs
, Utils
, Configure
5 from 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
.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')
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
)
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
)
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 Utils
.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 del(conf
.env
.defines
['PYTHONDIR'])
52 del(conf
.env
.defines
['PYTHONARCHDIR'])
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')
61 _check_python_headers(conf
, mandatory
)
62 conf
.env
["python_headers_checked"] = "yes"
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']))
70 conf
.msg("python headers", "using cache")
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'])
77 def _check_python_headers(conf
, mandatory
):
79 Configure
.ConfigurationError
80 conf
.check_python_headers(mandatory
=mandatory
)
81 except Configure
.ConfigurationError
:
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
% ''
89 conf
.env
['PYTHON_SO_ABI_FLAG'] = ''
90 conf
.env
['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
91 conf
.env
['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
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
)
100 def PYTHON_BUILD_IS_ENABLED(self
):
101 return self
.CONFIG_SET('HAVE_PYTHON_H')
103 Build
.BuildContext
.PYTHON_BUILD_IS_ENABLED
= PYTHON_BUILD_IS_ENABLED
106 def SAMBA_PYTHON(bld
, name
,
113 init_function_sentinel
=None,
118 '''build a python extension for Samba'''
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():
125 if bld
.env
['IS_EXTRA_PYTHON']:
126 name
= 'extra-' + name
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
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.
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'
144 source
= bld
.EXPAND_VARIABLES(source
, vars=vars)
146 if realname
is not None:
147 link_name
= 'python_modules/%s' % realname
151 bld
.SAMBA_LIBRARY(name
,
154 public_deps
=public_deps
,
157 local_include
=local_include
,
162 target_type
='PYTHON',
163 install_path
='${PYTHONARCHDIR}',
164 allow_undefined_symbols
=True,
168 Build
.BuildContext
.SAMBA_PYTHON
= SAMBA_PYTHON
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']
177 Build
.BuildContext
.pyembed_libname
= pyembed_libname
180 def gen_python_environments(bld
, extra_env_vars
=()):
181 """Generate all Python environments
183 To be used in a for loop. Normally, the loop body will be executed once.
185 When --extra-python is used, the body will additionaly be executed
186 with the extra-python environment active.
190 if bld
.env
['EXTRA_PYTHON']:
191 copied
= ('GLOBAL_DEPENDENCIES', 'TARGET_TYPE') + tuple(extra_env_vars
)
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']
197 bld
.all_envs
['default'] = default_env
199 Build
.BuildContext
.gen_python_environments
= gen_python_environments