1# waf build tool for building IDL files with pidl
2
3import os, sys
4from waflib import Build, Logs, Utils, Configure, Errors
5from waflib.Configure import conf
6
7@conf
8def SAMBA_CHECK_PYTHON(conf, version=(3,4,0)):
9
10    if conf.env.disable_python:
11        version=(2,6,0)
12
13    # enable tool to build python extensions
14    if conf.env.HAVE_PYTHON_H:
15        conf.check_python_version(version)
16        return
17
18    interpreters = []
19
20    conf.find_program('python3', var='PYTHON',
21                      mandatory=not conf.env.disable_python)
22    conf.load('python')
23    path_python = conf.find_program('python3')
24
25    conf.env.PYTHON_SPECIFIED = (conf.env.PYTHON != path_python)
26    conf.check_python_version(version)
27
28    interpreters.append(conf.env['PYTHON'])
29    conf.env.python_interpreters = interpreters
30
31
32@conf
33def SAMBA_CHECK_PYTHON_HEADERS(conf):
34    if conf.env.disable_python:
35
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=')]
42
43        return
44
45    if conf.env["python_headers_checked"] == []:
46        _check_python_headers(conf)
47        conf.env["python_headers_checked"] = "yes"
48
49    else:
50        conf.msg("python headers", "using cache")
51
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=')]
57
58def _check_python_headers(conf):
59    conf.check_python_headers()
60
61    if conf.env['PYTHON_VERSION'] > '3':
62        abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
63        conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
64    else:
65        conf.env['PYTHON_SO_ABI_FLAG'] = ''
66    conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
67        conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
68
69    for lib in conf.env['LINKFLAGS_PYEMBED']:
70        if lib.startswith('-L'):
71            conf.env.append_unique('LIBPATH_PYEMBED', lib[2:]) # strip '-L'
72            conf.env['LINKFLAGS_PYEMBED'].remove(lib)
73
74    # same as in waf 1.5, keep only '-fno-strict-aliasing'
75    # and ignore defines such as NDEBUG _FORTIFY_SOURCE=2
76    conf.env.DEFINES_PYEXT = []
77    conf.env.CFLAGS_PYEXT = ['-fno-strict-aliasing']
78
79    return
80
81def PYTHON_BUILD_IS_ENABLED(self):
82    return self.CONFIG_SET('HAVE_PYTHON_H')
83
84Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
85
86
87def SAMBA_PYTHON(bld, name,
88                 source='',
89                 deps='',
90                 public_deps='',
91                 realname=None,
92                 cflags='',
93                 cflags_end=None,
94                 includes='',
95                 init_function_sentinel=None,
96                 local_include=True,
97                 vars=None,
98                 install=True,
99                 enabled=True):
100    '''build a python extension for Samba'''
101
102    # force-disable when we can't build python modules, so
103    # every single call doesn't need to pass this in.
104    if not bld.PYTHON_BUILD_IS_ENABLED():
105        enabled = False
106
107    # when we support static python modules we'll need to gather
108    # the list from all the SAMBA_PYTHON() targets
109    if init_function_sentinel is not None:
110        cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
111
112    # From https://docs.python.org/2/c-api/arg.html:
113    # Starting with Python 2.5 the type of the length argument to
114    # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
115    # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
116    # including Python.h. If the macro is defined, length is a Py_ssize_t
117    # rather than an int.
118
119    # Because <Python.h> if often included before includes.h/config.h
120    # This must be in the -D compiler options
121    cflags += ' -DPY_SSIZE_T_CLEAN=1'
122
123    source = bld.EXPAND_VARIABLES(source, vars=vars)
124
125    if realname is not None:
126        link_name = 'python/%s' % realname
127    else:
128        link_name = None
129
130    bld.SAMBA_LIBRARY(name,
131                      source=source,
132                      deps=deps,
133                      public_deps=public_deps,
134                      includes=includes,
135                      cflags=cflags,
136                      cflags_end=cflags_end,
137                      local_include=local_include,
138                      vars=vars,
139                      realname=realname,
140                      link_name=link_name,
141                      pyext=True,
142                      target_type='PYTHON',
143                      install_path='${PYTHONARCHDIR}',
144                      allow_undefined_symbols=True,
145                      install=install,
146                      enabled=enabled)
147
148Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
149
150
151def pyembed_libname(bld, name):
152    if bld.env['PYTHON_SO_ABI_FLAG']:
153        return name + bld.env['PYTHON_SO_ABI_FLAG']
154    else:
155        return name
156
157Build.BuildContext.pyembed_libname = pyembed_libname
158
159
160