From 8e02cebdac536dfb6748da2c50656a26f70d9da7 Mon Sep 17 00:00:00 2001
From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Date: Wed, 22 Feb 2017 16:33:22 -0800
Subject: [PATCH] Add infrastructure to disable the build of certain extensions

Some of the extensions part of the Python core have dependencies on
external libraries (sqlite, tk, etc.) or are relatively big and not
necessarly always useful (CJK codecs for example). By extensions, we
mean part of Python modules that are written in C and therefore
compiled to binary code.

Therefore, we introduce a small infrastructure that allows to disable
some of those extensions. This can be done inside the configure.ac by
adding values to the DISABLED_EXTENSIONS variable (which is a
word-separated list of extensions).

The implementation works as follow :

 * configure.ac defines a DISABLED_EXTENSIONS variable, which is
   substituted (so that when Makefile.pre is generated from
   Makefile.pre.in, the value of the variable is substituted). For
   now, this DISABLED_EXTENSIONS variable is empty, later patches will
   use it.

 * Makefile.pre.in passes the DISABLED_EXTENSIONS value down to the
   variables passed in the environment when calling the setup.py
   script that actually builds and installs those extensions.

 * setup.py is modified so that the existing "disabled_module_list" is
   filled with those pre-disabled extensions listed in
   DISABLED_EXTENSIONS.

Patch ported to python2.7 by Maxime Ripard <ripard@archos.com>, and
then extended by Thomas Petazzoni
<thomas.petazzoni@free-electrons.com>.

Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
[ Andrey Smirnov: ported to Python 3.6 ]
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 Makefile.pre.in | 6 +++++-
 configure.ac    | 2 ++
 setup.py        | 5 ++++-
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 2957c8e5a1..c1cfb96767 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -239,6 +239,8 @@ FILEMODE=	644
 # configure script arguments
 CONFIG_ARGS=	@CONFIG_ARGS@
 
+# disabled extensions
+DISABLED_EXTENSIONS=	@DISABLED_EXTENSIONS@
 
 # Subdirectories with code
 SRCDIRS= 	@SRCDIRS@
@@ -739,6 +741,7 @@ sharedmods: $(PYTHON_FOR_BUILD_DEPS) pybuilddir.txt @LIBMPDEC_INTERNAL@ @LIBEXPA
 	    *) quiet="";; \
 	esac; \
 	echo "$(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' \
+		DISABLED_EXTENSIONS="$(DISABLED_EXTENSIONS)" \
 		$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build"; \
 	$(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' \
 		$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
@@ -2228,7 +2231,8 @@ libainstall: all python-config
 # Install the dynamically loadable modules
 # This goes into $(exec_prefix)
 sharedinstall: all
-	$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/setup.py install \
+	$(RUNSHARED) DISABLED_EXTENSIONS="$(DISABLED_EXTENSIONS)" \
+		$(PYTHON_FOR_BUILD) $(srcdir)/setup.py install \
 	   	--prefix=$(prefix) \
 		--install-scripts=$(BINDIR) \
 		--install-platlib=$(DESTSHARED) \
diff --git a/configure.ac b/configure.ac
index 830885fcb3..5a6a1fe608 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3562,6 +3562,8 @@ LIBS="$withval $LIBS"
 ],
 [AC_MSG_RESULT(no)])
 
+AC_SUBST(DISABLED_EXTENSIONS)
+
 # Check for use of the system expat library
 AC_MSG_CHECKING(for --with-system-expat)
 AC_ARG_WITH(system_expat,
diff --git a/setup.py b/setup.py
index 15d0d4576a..e496ee34c2 100644
--- a/setup.py
+++ b/setup.py
@@ -56,7 +56,10 @@
 
 
 # This global variable is used to hold the list of modules to be disabled.
-DISABLED_MODULE_LIST = []
+try:
+    DISABLED_MODULE_LIST = sysconfig.get_config_var("DISABLED_EXTENSIONS").split(" ")
+except KeyError:
+    DISABLED_MODULE_LIST = list()
 
 # --list-module-names option used by Tools/scripts/generate_module_names.py
 LIST_MODULE_NAMES = False
-- 
2.34.1

