waf: disable-python - add option globally to build system
[nivanova/samba-autobuild/.git] / buildtools / wafsamba / samba_python.py
index 6f9435002e2c39da6d8a1620269c93d6b137c924..40b42fcf79aac80f6d77ae8ef55fee2e758a9ce2 100644 (file)
@@ -1,7 +1,7 @@
 # waf build tool for building IDL files with pidl
 
 import os
-import Build, Logs, Utils
+import Build, Logs, Utils, Configure
 from Configure import conf
 
 @conf
@@ -40,6 +40,18 @@ def SAMBA_CHECK_PYTHON(conf, mandatory=True, version=(2,4,2)):
 
 @conf
 def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
+    if conf.env.disable_python:
+        if mandatory:
+            raise Utils.WafError("Cannot check for python headers when "
+                                 "--disable-python specified")
+
+        conf.msg("python headers", "Check disabled due to --disable-python")
+        # we don't want PYTHONDIR in config.h, as otherwise changing
+        # --prefix causes a complete rebuild
+        del(conf.env.defines['PYTHONDIR'])
+        del(conf.env.defines['PYTHONARCHDIR'])
+        return
+
     if conf.env["python_headers_checked"] == []:
         if conf.env['EXTRA_PYTHON']:
             conf.setenv('extrapython')
@@ -63,13 +75,26 @@ def SAMBA_CHECK_PYTHON_HEADERS(conf, mandatory=True):
     del(conf.env.defines['PYTHONARCHDIR'])
 
 def _check_python_headers(conf, mandatory):
-    conf.check_python_headers(mandatory=mandatory)
+    try:
+        Configure.ConfigurationError
+        conf.check_python_headers(mandatory=mandatory)
+    except Configure.ConfigurationError:
+        if mandatory:
+             raise
 
     if conf.env['PYTHON_VERSION'] > '3':
         abi_pattern = os.path.splitext(conf.env['pyext_PATTERN'])[0]
         conf.env['PYTHON_SO_ABI_FLAG'] = abi_pattern % ''
     else:
         conf.env['PYTHON_SO_ABI_FLAG'] = ''
+    conf.env['PYTHON_LIBNAME_SO_ABI_FLAG'] = (
+        conf.env['PYTHON_SO_ABI_FLAG'].replace('_', '-'))
+
+
+def PYTHON_BUILD_IS_ENABLED(self):
+    return self.CONFIG_SET('HAVE_PYTHON_H')
+
+Build.BuildContext.PYTHON_BUILD_IS_ENABLED = PYTHON_BUILD_IS_ENABLED
 
 
 def SAMBA_PYTHON(bld, name,
@@ -86,13 +111,29 @@ def SAMBA_PYTHON(bld, name,
                  enabled=True):
     '''build a python extension for Samba'''
 
+    # force-disable when we can't build python modules, so
+    # every single call doesn't need to pass this in.
+    if not bld.PYTHON_BUILD_IS_ENABLED():
+        enabled = False
+
     if bld.env['IS_EXTRA_PYTHON']:
         name = 'extra-' + name
 
     # when we support static python modules we'll need to gather
     # the list from all the SAMBA_PYTHON() targets
     if init_function_sentinel is not None:
-        cflags += '-DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
+        cflags += ' -DSTATIC_LIBPYTHON_MODULES=%s' % init_function_sentinel
+
+    # From https://docs.python.org/2/c-api/arg.html:
+    # Starting with Python 2.5 the type of the length argument to
+    # PyArg_ParseTuple(), PyArg_ParseTupleAndKeywords() and PyArg_Parse()
+    # can be controlled by defining the macro PY_SSIZE_T_CLEAN before
+    # including Python.h. If the macro is defined, length is a Py_ssize_t
+    # rather than an int.
+
+    # Because <Python.h> if often included before includes.h/config.h
+    # This must be in the -D compiler options
+    cflags += ' -DPY_SSIZE_T_CLEAN=1'
 
     source = bld.EXPAND_VARIABLES(source, vars=vars)
 
@@ -122,7 +163,10 @@ Build.BuildContext.SAMBA_PYTHON = SAMBA_PYTHON
 
 
 def pyembed_libname(bld, name, extrapython=False):
-    return name + bld.env['PYTHON_SO_ABI_FLAG']
+    if bld.env['PYTHON_SO_ABI_FLAG']:
+        return name + bld.env['PYTHON_SO_ABI_FLAG']
+    else:
+        return name
 
 Build.BuildContext.pyembed_libname = pyembed_libname