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=(3,4,0)):
13 # enable tool to build python extensions
14 if conf.env.HAVE_PYTHON_H:
15 conf.check_python_version(version)
20 conf.find_program('python3', var='PYTHON', mandatory=mandatory)
22 path_python = conf.find_program('python3')
24 conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
25 conf.check_python_version(version)
27 interpreters.append(conf.env['PYTHON'])
28 conf.env.python_interpreters = interpreters
32 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
33 if conf.env.disable_python:
35 raise Errors.WafError("Cannot check for python headers when "
36 "--disable-python specified")
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=')]
47 if conf.env["python_headers_checked"] == []:
48 _check_python_headers(conf, mandatory)
49 conf.env["python_headers_checked"] = "yes"
52 conf.msg("python headers", "using cache")
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=')]
60 def _check_python_headers(conf, mandatory):
62 conf.errors.ConfigurationError
63 conf.check_python_headers()
64 except conf.errors.ConfigurationError:
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 % ''
72 conf.env['PYTHON_SO_ABI_FLAG'] = ''
73 conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
74 conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
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)
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']
88 def PYTHON_BUILD_IS_ENABLED(self):
89 return self.CONFIG_SET('HAVE_PYTHON_H')
91 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
94 def SAMBA_PYTHON(bld, name,
102 init_function_sentinel=None,
107 '''build a python extension for Samba'''
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():
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
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.
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'
130 source = bld.EXPAND_VARIABLES(source, vars=vars)
132 if realname is not None:
133 link_name = 'python/%s' % realname
137 bld.SAMBA_LIBRARY(name,
140 public_deps=public_deps,
143 cflags_end=cflags_end,
144 local_include=local_include,
149 target_type='PYTHON',
150 install_path='${PYTHONARCHDIR}',
151 allow_undefined_symbols=True,
155 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
158 def pyembed_libname(bld, name):
159 if bld.env['PYTHON_SO_ABI_FLAG']:
160 return name + bld.env['PYTHON_SO_ABI_FLAG']
164 Build.BuildContext.pyembed_libname = pyembed_libname