From a65e5d3caf8e076d531191164c23c6240461f675 Mon Sep 17 00:00:00 2001
From: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Date: Wed, 22 Feb 2017 17:07:56 -0800
Subject: [PATCH] Add an option to disable pydoc

It removes 0.5 MB of data from the target plus the pydoc script
itself.

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

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 204f293d53..63e3df0470 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1357,7 +1357,9 @@ bininstall: altbininstall
 	-rm -f $(DESTDIR)$(BINDIR)/idle3
 	(cd $(DESTDIR)$(BINDIR); $(LN) -s idle$(VERSION) idle3)
 	-rm -f $(DESTDIR)$(BINDIR)/pydoc3
+ifeq (@PYDOC@,yes)
 	(cd $(DESTDIR)$(BINDIR); $(LN) -s pydoc$(VERSION) pydoc3)
+endif
 	-rm -f $(DESTDIR)$(BINDIR)/2to3
 	(cd $(DESTDIR)$(BINDIR); $(LN) -s 2to3-$(VERSION) 2to3)
 	if test "x$(LIPO_32BIT_FLAGS)" != "x" ; then \
@@ -1403,7 +1405,7 @@ LIBSUBDIRS=	tkinter site-packages \
 		multiprocessing multiprocessing/dummy \
 		unittest \
 		venv venv/scripts venv/scripts/common venv/scripts/posix \
-		curses pydoc_data \
+		curses \
 		zoneinfo
 
 TESTSUBDIRS=	tkinter/test tkinter/test/test_tkinter \
@@ -1486,6 +1488,10 @@ ifeq (@TEST_MODULES@,yes)
 LIBSUBDIRS += $(TESTSUBDIRS)
 endif
 
+ifeq (@PYDOC@,yes)
+LIBSUBDIRS += pydoc_data
+endif
+
 libinstall:	build_all $(srcdir)/Modules/xxmodule.c
 	@for i in $(SCRIPTDIR) $(LIBDEST); \
 	do \
diff --git a/configure.ac b/configure.ac
index 92e28d02ee..174ed85a7c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -3329,6 +3329,12 @@ if test "$posix_threads" = "yes"; then
       AC_CHECK_FUNCS(pthread_getcpuclockid)
 fi
 
+AC_SUBST(PYDOC)
+
+AC_ARG_ENABLE(pydoc,
+	AS_HELP_STRING([--disable-pydoc], [disable pydoc]),
+	[ PYDOC="${enableval}" ], [ PYDOC=yes ])
+
 AC_SUBST(TEST_MODULES)
 
 AC_ARG_ENABLE(test-modules,
diff --git a/setup.py b/setup.py
index 926c16f58f..1fc5fac90c 100644
--- a/setup.py
+++ b/setup.py
@@ -2586,6 +2586,12 @@ def main():
     # turn off warnings when deprecated modules are imported
     import warnings
     warnings.filterwarnings("ignore",category=DeprecationWarning)
+
+    scripts = ['Tools/scripts/idle3', 'Tools/scripts/2to3',
+               'Lib/smtpd.py']
+    if not '--disable-pydoc' in sysconfig.get_config_var("CONFIG_ARGS"):
+        scripts += [ 'Tools/scripts/pydoc3' ]
+
     setup(# PyPI Metadata (PEP 301)
           name = "Python",
           version = sys.version.split()[0],
@@ -2610,8 +2616,7 @@ def main():
           # If you change the scripts installed here, you also need to
           # check the PyBuildScripts command above, and change the links
           # created by the bininstall target in Makefile.pre.in
-          scripts = ["Tools/scripts/pydoc3", "Tools/scripts/idle3",
-                     "Tools/scripts/2to3"]
+          scripts = scripts
         )
 
 # --install-platlib
-- 
2.25.1