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,6,0)):
9 # enable tool to build python extensions
10 if conf.env.HAVE_PYTHON_H:
11 conf.check_python_version(version)
16 conf.find_program('python3', var='PYTHON', mandatory=mandatory)
18 path_python = conf.find_program('python3')
20 conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
21 conf.check_python_version(version)
23 interpreters.append(conf.env['PYTHON'])
24 conf.env.python_interpreters = interpreters
28 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
29 if conf.env.disable_python:
31 raise Errors.WafError("Cannot check for python headers when "
32 "--disable-python specified")
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=')]
43 if conf.env["python_headers_checked"] == []:
44 _check_python_headers(conf, mandatory)
45 conf.env["python_headers_checked"] = "yes"
48 conf.msg("python headers", "using cache")
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=')]
56 def _check_python_headers(conf, mandatory):
58 conf.errors.ConfigurationError
59 conf.check_python_headers()
60 except conf.errors.ConfigurationError:
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 % ''
68 conf.env['PYTHON_SO_ABI_FLAG'] = ''
69 conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
70 conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
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)
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']
84 def PYTHON_BUILD_IS_ENABLED(self):
85 return self.CONFIG_SET('HAVE_PYTHON_H')
87 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
90 def SAMBA_PYTHON(bld, name,
98 init_function_sentinel=None,
103 '''build a python extension for Samba'''
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():
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
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.
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'
126 source = bld.EXPAND_VARIABLES(source, vars=vars)
128 if realname is not None:
129 link_name = 'python/%s' % realname
133 bld.SAMBA_LIBRARY(name,
136 public_deps=public_deps,
139 cflags_end=cflags_end,
140 local_include=local_include,
145 target_type='PYTHON',
146 install_path='${PYTHONARCHDIR}',
147 allow_undefined_symbols=True,
151 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
154 def pyembed_libname(bld, name):
155 if bld.env['PYTHON_SO_ABI_FLAG']:
156 return name + bld.env['PYTHON_SO_ABI_FLAG']
160 Build.BuildContext.pyembed_libname = pyembed_libname