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["python_headers_checked"] == []:
44 if conf.env['EXTRA_PYTHON']:
45 conf.setenv('extrapython')
46 _check_python_headers(conf, mandatory=True)
47 conf.setenv('default')
49 _check_python_headers(conf, mandatory)
50 conf.env["python_headers_checked"] = "yes"
52 if conf.env['EXTRA_PYTHON']:
53 extraversion = conf.all_envs['extrapython']['PYTHON_VERSION']
54 if extraversion == conf.env['PYTHON_VERSION']:
55 raise Utils.WafError("extrapython %s is same as main python %s" % (
56 extraversion, conf.env['PYTHON_VERSION']))
58 conf.msg("python headers", "using cache")
60 # we don't want PYTHONDIR in config.h, as otherwise changing
61 # --prefix causes a complete rebuild
62 del(conf.env.defines['PYTHONDIR'])
63 del(conf.env.defines['PYTHONARCHDIR'])
65 def _check_python_headers(conf, mandatory):
67 Configure.ConfigurationError
68 conf.check_python_headers(mandatory=mandatory)
69 except Configure.ConfigurationError:
73 if conf.env['PYTHON_VERSION'] > '3':
74 abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
75 conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
77 conf.env['PYTHON_SO_ABI_FLAG'] = ''
80 def SAMBA_PYTHON(bld, name,
87 init_function_sentinel=None,
92 '''build a python extension for Samba'''
94 if bld.env['IS_EXTRA_PYTHON']:
95 name = 'extra-' + name
97 # when we support static python modules we'll need to gather
98 # the list from all the SAMBA_PYTHON() targets
99 if init_function_sentinel is not None:
100 cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
102 # From https://docs.python.org/2/c-api/arg.html:
103 # Starting with Python 2.5 the type of the length argument to
104 # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
105 # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
106 # including Python.h. If the macro is defined, length is a Py_ssize_t
107 # rather than an int.
109 # Because <Python.h> if often included before includes.h/config.h
110 # This must be in the -D compiler options
111 cflags += ' -DPY_SSIZE_T_CLEAN=1'
113 source = bld.EXPAND_VARIABLES(source, vars=vars)
115 if realname is not None:
116 link_name = 'python_modules/%s' % realname
120 bld.SAMBA_LIBRARY(name,
123 public_deps=public_deps,
126 local_include=local_include,
131 target_type='PYTHON',
132 install_path='${PYTHONARCHDIR}',
133 allow_undefined_symbols=True,
137 Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
140 def pyembed_libname(bld, name, extrapython=False):
141 return name + bld.env['PYTHON_SO_ABI_FLAG']
143 Build.BuildContext.pyembed_libname = pyembed_libname
146 def gen_python_environments(bld, extra_env_vars=()):
147 """Generate all Python environments
149 To be used in a for loop. Normally, the loop body will be executed once.
151 When --extra-python is used, the body will additionaly be executed
152 with the extra-python environment active.
156 if bld.env['EXTRA_PYTHON']:
157 copied = ('GLOBAL_DEPENDENCIES', 'TARGET_TYPE') + tuple(extra_env_vars)
159 bld.all_envs['extrapython'][name] = bld.all_envs['default'][name]
160 default_env = bld.all_envs['default']
161 bld.all_envs['default'] = bld.all_envs['extrapython']
163 bld.all_envs['default'] = default_env
165 Build.BuildContext.gen_python_environments = gen_python_environments