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, version=(3,6,0)):
10 if conf.env.enable_fuzzing:
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',
21 mandatory=not conf.env.disable_python)
23 path_python = conf.find_program('python3')
25 conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
26 conf.check_python_version(version)
28 interpreters.append(conf.env['PYTHON'])
29 conf.env.python_interpreters = interpreters
33 def SAMBA_CHECK_PYTHON_HEADERS(conf):
34 if conf.env.disable_python:
36 conf.msg("python headers", "Check disabled due to --disable-python")
37 # we don't want PYTHONDIR in config.h, as otherwise changing
38 # --prefix causes a complete rebuild
39 conf.env.DEFINES = [x for x in conf.env.DEFINES
40 if not x.startswith('PYTHONDIR=')
41 and not x.startswith('PYTHONARCHDIR=')]
45 if conf.env["python_headers_checked"] == []:
46 _check_python_headers(conf)
47 conf.env["python_headers_checked"] = "yes"
50 conf.msg("python headers", "using cache")
52 # we don't want PYTHONDIR in config.h, as otherwise changing
53 # --prefix causes a complete rebuild
54 conf.env.DEFINES = [x for x in conf.env.DEFINES
55 if not x.startswith('PYTHONDIR=')
56 and not x.startswith('PYTHONARCHDIR=')]
58 def _check_python_headers(conf):
59 conf.check_python_headers()
61 abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
62 conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
63 conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
64 conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
66 for lib in conf.env['LINKFLAGS_PYEMBED']:
67 if lib.startswith('-L'):
68 conf.env.append_unique('LIBPATH_PYEMBED', lib[2:]) # strip '-L'
69 conf.env['LINKFLAGS_PYEMBED'].remove(lib)
71 # same as in waf 1.5, keep only '-fno-strict-aliasing'
72 # and ignore defines such as NDEBUG _FORTIFY_SOURCE=2
73 conf.env.DEFINES_PYEXT = []
74 conf.env.CFLAGS_PYEXT = ['-fno-strict-aliasing']
78 def PYTHON_BUILD_IS_ENABLED(self):
79 return self.CONFIG_SET('HAVE_PYTHON_H')
81 Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
84 def SAMBA_PYTHON(bld, name,
92 init_function_sentinel=None,
97 '''build a python extension for Samba'''
99 # force-disable when we can't build python modules, so
100 # every single call doesn't need to pass this in.
101 if not bld.PYTHON_BUILD_IS_ENABLED():
104 # Save time, no need to build python bindings when fuzzing
105 if bld.env.enable_fuzzing:
108 # when we support static python modules we'll need to gather
109 # the list from all the SAMBA_PYTHON() targets
110 if init_function_sentinel is not None:
111 cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
113 # From https://docs.python.org/2/c-api/arg.html:
114 # Starting with Python 2.5 the type of the length argument to
115 # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
116 # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
117 # including Python.h. If the macro is defined, length is a Py_ssize_t
118 # rather than an int.
120 # Because <Python.h> if often included before includes.h/config.h
121 # This must be in the -D compiler options
122 cflags += ' -DPY_SSIZE_T_CLEAN=1'
124 source = bld.EXPAND_VARIABLES(source, vars=vars)
126 if realname is not None:
127 link_name = 'python/%s' % realname
131 bld.SAMBA_LIBRARY(name,
134 public_deps=public_deps,
137 cflags_end=cflags_end,
138 local_include=local_include,
143 target_type='PYTHON',
144 install_path='${PYTHONARCHDIR}',
145 allow_undefined_symbols=True,
149 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
152 def pyembed_libname(bld, name):
153 if bld.env['PYTHON_SO_ABI_FLAG']:
154 return name + bld.env['PYTHON_SO_ABI_FLAG']
158 Build.BuildContext.pyembed_libname = pyembed_libname