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

lib2to3 is a library to convert Python 2.x code to Python 3.x. As
such, it is probably not very useful on embedded system targets.

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>
[ Adam Duskett: ported to Python 3.10.0 ]
Signed-off-by: Adam Duskett <aduskett@gmail.com>
---
 Makefile.pre.in | 17 ++++++++++++-----
 configure.ac    |  5 +++++
 setup.py        |  6 +++---
 3 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/Makefile.pre.in b/Makefile.pre.in
index 28cf88e..63fa9fb 100644
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -1395,7 +1395,9 @@ ifeq (@PYDOC@,yes)
 	(cd $(DESTDIR)$(BINDIR); $(LN) -s pydoc$(VERSION) pydoc3)
 endif
 	-rm -f $(DESTDIR)$(BINDIR)/2to3
+ifeq (@LIB2TO3@,yes)
 	(cd $(DESTDIR)$(BINDIR); $(LN) -s 2to3-$(VERSION) 2to3)
+endif
 	if test "x$(LIPO_32BIT_FLAGS)" != "x" ; then \
 		rm -f $(DESTDIR)$(BINDIR)/python3-32$(EXE); \
 		(cd $(DESTDIR)$(BINDIR); $(LN) -s python$(VERSION)-32$(EXE) python3-32$(EXE)) \
@@ -1441,7 +1443,6 @@ LIBSUBDIRS=	asyncio \
 		idlelib idlelib/Icons \
 		importlib importlib/resources importlib/metadata \
 		json \
-		lib2to3 lib2to3/fixes lib2to3/pgen2 \
 		logging \
 		multiprocessing multiprocessing/dummy \
 		re \
@@ -1458,10 +1459,6 @@ LIBSUBDIRS=	asyncio \
 TESTSUBDIRS=	ctypes/test \
 		distutils/tests \
 		idlelib/idle_test \
-		lib2to3/tests \
-		lib2to3/tests/data \
-		lib2to3/tests/data/fixers \
-		lib2to3/tests/data/fixers/myfixes \
 		test test/audiodata \
 		test/capath test/cjkencodings \
 		test/data test/decimaltestdata \
@@ -1535,6 +1532,14 @@ ifeq (@PYDOC@,yes)
 LIBSUBDIRS += pydoc_data
 endif
 
+ifeq (@LIB2TO3@,yes)
+LIBSUBDIRS += lib2to3 lib2to3/fixes lib2to3/pgen2
+TESTSUBDIRS += lib2to3/tests			\
+	lib2to3/tests/data			\
+	lib2to3/tests/data/fixers		\
+	lib2to3/tests/data/fixers/myfixes
+endif
+
 TEST_MODULES=@TEST_MODULES@
 libinstall:	build_all $(srcdir)/Modules/xxmodule.c
 	@for i in $(SCRIPTDIR) $(LIBDEST); \
@@ -1637,10 +1642,12 @@ ifeq (@PYC_BUILD@,yes)
 		-j0 -d $(LIBDEST)/site-packages -f \
 		-x badsyntax $(DESTDIR)$(LIBDEST)/site-packages
 endif
+ifeq (@LIB2TO3@,yes)
 	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/Grammar.txt
 	-PYTHONPATH=$(DESTDIR)$(LIBDEST) $(RUNSHARED) \
 		$(PYTHON_FOR_BUILD) -m lib2to3.pgen2.driver $(DESTDIR)$(LIBDEST)/lib2to3/PatternGrammar.txt
+endif
 
 # bpo-21536: Misc/python-config.sh is generated in the build directory
 # from $(srcdir)Misc/python-config.sh.in.
diff --git a/configure.ac b/configure.ac
index 9079531..34c2ba9 100644
--- a/configure.ac
+++ b/configure.ac
@@ -6014,6 +6014,11 @@ else
 fi
 AC_SUBST(TEST_MODULES)
 
+AC_SUBST(LIB2TO3)
+
+AC_ARG_ENABLE(lib2to3,
+	AS_HELP_STRING([--disable-lib2to3], [disable lib2to3]),
+	[ LIB2TO3="${enableval}" ], [ LIB2TO3=yes ])
 
 # generate output files
 AC_CONFIG_FILES(Makefile.pre Misc/python.pc Misc/python-embed.pc Misc/python-config.sh)
diff --git a/setup.py b/setup.py
index d23f148..663fd44 100644
--- a/setup.py
+++ b/setup.py
@@ -2722,11 +2722,11 @@ def main():
     import warnings
     warnings.filterwarnings("ignore",category=DeprecationWarning)
 
-    scripts = ['Tools/scripts/idle3', 'Tools/scripts/2to3',
-               'Lib/smtpd.py']
+    scripts = ['Tools/scripts/idle3', 'Lib/smtpd.py']
     if not '--disable-pydoc' in sysconfig.get_config_var("CONFIG_ARGS"):
         scripts += [ 'Tools/scripts/pydoc3' ]
-
+    if not '--disable-lib2to3' in sysconfig.get_config_var("CONFIG_ARGS"):
+        scripts += [ 'Tools/scripts/2to3' ]
     setup(# PyPI Metadata (PEP 301)
           name = "Python",
           version = sys.version.split()[0],
-- 
2.30.2