[bug#36477,08/31] gnu: python: Fix cross-compilation.
diff mbox series

Message ID 20190708095913.3460-9-m.othacehe@gmail.com
State New
Headers show
Series
  • Fix cross-compilation issues
Related show

Commit Message

Mathieu Othacehe July 8, 2019, 9:58 a.m. UTC
* gnu/packages/patches/python-2.7-search-paths.patch: Add cross-compilation
support.
* gnu/packages/patches/python-3-search-paths.patch: Ditto.
* gnu/packages/patches/python-cross-compile.patch: New patch.
* gnu/local.mk (dist_patch_DATA): Add above new patch.
* gnu/packages/python.scm (python-2.7)[patches]: Add new patch above,
[arguments]: Set _PYTHON_HOST_PLATFORM env variable when cross compiling.
---
 gnu/local.mk                                  |   1 +
 .../patches/python-2.7-search-paths.patch     |  10 +-
 .../patches/python-3-search-paths.patch       |  11 +-
 .../patches/python-cross-compile.patch        | 145 ++++++++++++++++++
 gnu/packages/python.scm                       |   6 +-
 5 files changed, 168 insertions(+), 5 deletions(-)
 create mode 100644 gnu/packages/patches/python-cross-compile.patch

Comments

Ludovic Courtès July 15, 2019, 8:29 p.m. UTC | #1
This is the second patch in this series with this subject, which
suggests the subject could be adjusted.  :-)

Mathieu Othacehe <m.othacehe@gmail.com> skribis:

> * gnu/packages/patches/python-2.7-search-paths.patch: Add cross-compilation
> support.
> * gnu/packages/patches/python-3-search-paths.patch: Ditto.
> * gnu/packages/patches/python-cross-compile.patch: New patch.
> * gnu/local.mk (dist_patch_DATA): Add above new patch.
> * gnu/packages/python.scm (python-2.7)[patches]: Add new patch above,
> [arguments]: Set _PYTHON_HOST_PLATFORM env variable when cross compiling.

[...]

> +++ b/gnu/packages/patches/python-cross-compile.patch
> @@ -0,0 +1,145 @@
> +Patch taken from https://bugs.python.org/issue22724 and augmented with
> +following Nix patch
> +https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
> +to fix the whole cross-compilation circus.
> +
> +---
> + Makefile.pre.in | 14 +++++++-------
> + configure       |  5 ++++-
> + setup.py        |  9 ++++++---
> + 3 files changed, 17 insertions(+), 11 deletions(-)

Heh, a non trivial patch!

> --- a/gnu/packages/python.scm
> +++ b/gnu/packages/python.scm
> @@ -109,7 +109,8 @@
>                                 "python-2-deterministic-build-info.patch"
>                                 "python-2.7-site-prefixes.patch"
>                                 "python-2.7-source-date-epoch.patch"
> -                               "python-2.7-adjust-tests.patch"))
> +                               "python-2.7-adjust-tests.patch"
> +                               "python-cross-compile.patch"))

Note that this triggers a full rebuild, so we’ll probably have to wait
for the next ‘core-updates’ branch, which we’ll hopefully open soonish.

>             'configure 'patch-lib-shells
>             (lambda _
> +             ,@(if (%current-target-system)
> +                   '((setenv "_PYTHON_HOST_PLATFORM" ""))
> +                   '())

It would be great if you could add a comment saying what component
consumes this environment variable.

Otherwise LGTM.

Thanks!

Ludo’.

Patch
diff mbox series

diff --git a/gnu/local.mk b/gnu/local.mk
index 8be4d74dce..7d42202485 100644
--- a/gnu/local.mk
+++ b/gnu/local.mk
@@ -1208,6 +1208,7 @@  dist_patch_DATA =						\
   %D%/packages/patches/python-CVE-2018-14647.patch		\
   %D%/packages/patches/python-axolotl-AES-fix.patch		\
   %D%/packages/patches/python-cairocffi-dlopen-path.patch	\
+  %D%/packages/patches/python-cross-compile.patch		\
   %D%/packages/patches/python-cffi-x87-stack-clean.patch	\
   %D%/packages/patches/python-fix-tests.patch			\
   %D%/packages/patches/python2-larch-coverage-4.0a6-compatibility.patch \
diff --git a/gnu/packages/patches/python-2.7-search-paths.patch b/gnu/packages/patches/python-2.7-search-paths.patch
index ba7235df27..6457819b8a 100644
--- a/gnu/packages/patches/python-2.7-search-paths.patch
+++ b/gnu/packages/patches/python-2.7-search-paths.patch
@@ -3,13 +3,17 @@  looking for headers and libraries.
 
 --- Python-2.7.10/setup.py	2015-10-07 18:33:18.125153186 +0200
 +++ Python-2.7.10/setup.py	2015-10-07 18:33:47.497347552 +0200
-@@ -526,6 +526,10 @@ class PyBuildExt(build_ext):
+@@ -526,6 +526,14 @@ class PyBuildExt(build_ext):
              inc_dirs += ['/system/include', '/atheos/autolnk/include']
              inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
  
 +        # Always honor these variables.
-+        lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
-+        inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
++        if not cross_compiling:
++            lib_dirs += os.getenv('LIBRARY_PATH', '').split(os.pathsep)
++            inc_dirs += os.getenv('C_INCLUDE_PATH', '').split(os.pathsep)
++        else:
++            lib_dirs = os.getenv('CROSS_LIBRARY_PATH', '').split(os.pathsep)
++            inc_dirs = os.getenv('CROSS_C_INCLUDE_PATH', '').split(os.pathsep)
 +
          # OSF/1 and Unixware have some stuff in /usr/ccs/lib (like -ldb)
          if host_platform in ['osf1', 'unixware7', 'openunix8']:
diff --git a/gnu/packages/patches/python-3-search-paths.patch b/gnu/packages/patches/python-3-search-paths.patch
index 73e3f4ccf5..70e6109f18 100644
--- a/gnu/packages/patches/python-3-search-paths.patch
+++ b/gnu/packages/patches/python-3-search-paths.patch
@@ -3,7 +3,7 @@  looking for headers and libraries.
 
 --- setup.py	2015-10-07 23:32:58.891329173 +0200
 +++ setup.py	2015-10-07 23:46:29.653349924 +0200
-@@ -575,8 +575,8 @@
+@@ -600,15 +600,15 @@
          # if a file is found in one of those directories, it can
          # be assumed that no additional -I,-L directives are needed.
          if not cross_compiling:
@@ -14,3 +14,12 @@  looking for headers and libraries.
          else:
              # Add the sysroot paths. 'sysroot' is a compiler option used to
              # set the logical path of the standard system headers and
+             # libraries.
+-            lib_dirs = (self.compiler.library_dirs +
++            lib_dirs = (os.getenv('CROSS_LIBRARY_PATH', '').split(os.pathsep) +
+                         sysroot_paths(('LDFLAGS', 'CC'), system_lib_dirs))
+-            inc_dirs = (self.compiler.include_dirs +
++            inc_dirs = (os.getenv('CROSS_C_INCLUDE_PATH', '').split(os.pathsep) +
+                         sysroot_paths(('CPPFLAGS', 'CFLAGS', 'CC'),
+                                       system_include_dirs))
+         exts = []
diff --git a/gnu/packages/patches/python-cross-compile.patch b/gnu/packages/patches/python-cross-compile.patch
new file mode 100644
index 0000000000..5a470e1852
--- /dev/null
+++ b/gnu/packages/patches/python-cross-compile.patch
@@ -0,0 +1,145 @@ 
+Patch taken from https://bugs.python.org/issue22724 and augmented with
+following Nix patch
+https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/interpreters/python/cpython/2.7/cross-compile.patch
+to fix the whole cross-compilation circus.
+
+---
+ Makefile.pre.in | 14 +++++++-------
+ configure       |  5 ++++-
+ setup.py        |  9 ++++++---
+ 3 files changed, 17 insertions(+), 11 deletions(-)
+
+diff --git a/Makefile.pre.in b/Makefile.pre.in
+index 2a14f3323b..6239fc32fc 100644
+--- a/Makefile.pre.in
++++ b/Makefile.pre.in
+@@ -492,7 +492,7 @@ $(BUILDPYTHON):	Modules/python.o $(LIBRARY) $(LDLIBRARY)
+ 			$(BLDLIBRARY) $(LIBS) $(MODLIBS) $(SYSLIBS) $(LDLAST)
+ 
+ platform: $(BUILDPYTHON) pybuilddir.txt
+-	$(RUNSHARED) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
++	$(RUNSHARED) $(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) -c 'import sys ; from sysconfig import get_platform ; print get_platform()+"-"+sys.version[0:3]' >platform
+ 
+ # Create build directory and generate the sysconfig build-time data there.
+ # pybuilddir.txt contains the name of the build dir and is used for
+@@ -503,7 +503,7 @@ platform: $(BUILDPYTHON) pybuilddir.txt
+ # or removed in case of failure.
+ pybuilddir.txt: $(BUILDPYTHON)
+ 	@echo "none" > ./pybuilddir.txt
+-	$(RUNSHARED) $(PYTHON_FOR_BUILD) -S -m sysconfig --generate-posix-vars ;\
++	$(RUNSHARED) $(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) -S -m sysconfig --generate-posix-vars ;\
+ 	if test $$? -ne 0 ; then \
+ 		echo "generate-posix-vars failed" ; \
+ 		rm -f ./pybuilddir.txt ; \
+@@ -525,7 +525,7 @@ sharedmods: $(BUILDPYTHON) pybuilddir.txt Modules/_math.o
+ 	esac; \
+ 	$(RUNSHARED) CC='$(CC)' LDSHARED='$(BLDSHARED)' OPT='$(OPT)' \
+ 		_TCLTK_INCLUDES='$(TCLTK_INCLUDES)' _TCLTK_LIBS='$(TCLTK_LIBS)' \
+-		$(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
++		$(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) $(srcdir)/setup.py $$quiet build
+ 
+ # Build static library
+ # avoid long command lines, same as LIBRARY_OBJS
+@@ -928,7 +928,7 @@ install:	@FRAMEWORKINSTALLFIRST@ commoninstall bininstall maninstall @FRAMEWORKI
+ 			upgrade) ensurepip="--upgrade" ;; \
+ 			install|*) ensurepip="" ;; \
+ 		esac; \
+-		$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
++		$(RUNSHARED) $(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) -m ensurepip \
+ 			$$ensurepip --root=$(DESTDIR)/ ; \
+ 	fi
+ 
+@@ -939,7 +939,7 @@ altinstall:	commoninstall
+ 			upgrade) ensurepip="--altinstall --upgrade --no-default-pip" ;; \
+ 			install|*) ensurepip="--altinstall --no-default-pip" ;; \
+ 		esac; \
+-		$(RUNSHARED) $(PYTHON_FOR_BUILD) -m ensurepip \
++		$(RUNSHARED) $(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) -m ensurepip \
+ 			$$ensurepip --root=$(DESTDIR)/ ; \
+ 	fi
+ 
+@@ -1270,7 +1270,7 @@ libainstall:	@DEF_MAKE_RULE@ python-config
+ # Install the dynamically loadable modules
+ # This goes into $(exec_prefix)
+ sharedinstall: sharedmods
+-	$(RUNSHARED) $(PYTHON_FOR_BUILD) $(srcdir)/setup.py install \
++	$(RUNSHARED) $(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) $(srcdir)/setup.py install \
+ 	   	--prefix=$(prefix) \
+ 		--install-scripts=$(BINDIR) \
+ 		--install-platlib=$(DESTSHARED) \
+@@ -1344,7 +1344,7 @@ frameworkinstallextras:
+ # This installs a few of the useful scripts in Tools/scripts
+ scriptsinstall:
+ 	SRCDIR=$(srcdir) $(RUNSHARED) \
+-	$(PYTHON_FOR_BUILD) $(srcdir)/Tools/scripts/setup.py install \
++	$(PY_BUILD_ENVIRON) $(PYTHON_FOR_BUILD) $(srcdir)/Tools/scripts/setup.py install \
+ 	--prefix=$(prefix) \
+ 	--install-scripts=$(BINDIR) \
+ 	--root=$(DESTDIR)/
+diff --git a/configure b/configure
+index 67300fe2b6..6050f588c5 100755
+--- a/configure
++++ b/configure
+@@ -741,6 +741,7 @@ CONFIG_ARGS
+ SOVERSION
+ VERSION
+ PYTHON_FOR_BUILD
++PY_BUILD_ENVIRON
+ PYTHON_FOR_REGEN
+ host_os
+ host_vendor
+@@ -2964,7 +2965,8 @@ $as_echo_n "checking for python interpreter for cross build... " >&6; }
+ 	fi
+         { $as_echo "$as_me:${as_lineno-$LINENO}: result: $interp" >&5
+ $as_echo "$interp" >&6; }
+-	PYTHON_FOR_BUILD='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib:$(srcdir)/Lib/$(PLATDIR) '$interp
++	PY_BUILD_ENVIRON='_PYTHON_PROJECT_BASE=$(abs_builddir) _PYTHON_HOST_PLATFORM=$(_PYTHON_HOST_PLATFORM) PYTHONPATH=$(shell test -f pybuilddir.txt && echo $(abs_builddir)/`cat pybuilddir.txt`:)$(srcdir)/Lib:$(srcdir)/Lib/$(PLATDIR)'
++	PYTHON_FOR_BUILD=$interp
+     fi
+ elif test "$cross_compiling" = maybe; then
+     as_fn_error $? "Cross compiling required --host=HOST-TUPLE and --build=ARCH" "$LINENO" 5
+@@ -2974,6 +2976,7 @@ fi
+ 
+ 
+ 
++
+ if test "$prefix" != "/"; then
+     prefix=`echo "$prefix" | sed -e 's/\/$//g'`
+ fi
+diff --git a/setup.py b/setup.py
+index cb47a2339c..472e7e2b26 100644
+--- a/setup.py
++++ b/setup.py
+@@ -497,8 +497,6 @@ class PyBuildExt(build_ext):
+         if not cross_compiling:
+             add_dir_to_list(self.compiler.library_dirs, '/usr/local/lib')
+             add_dir_to_list(self.compiler.include_dirs, '/usr/local/include')
+-        if cross_compiling:
+-            self.add_gcc_paths()
+         self.add_multiarch_paths()
+ 
+         # Add paths specified in the environment variables LDFLAGS and
+@@ -556,7 +554,10 @@ class PyBuildExt(build_ext):
+         # be assumed that no additional -I,-L directives are needed.
+         inc_dirs = self.compiler.include_dirs[:]
+         lib_dirs = self.compiler.library_dirs[:]
+-        if not cross_compiling:
++        if cross_compiling:
++            inc_dirs = []
++            lib_dirs = []
++        else:
+             for d in (
+                 '/usr/include',
+                 ):
+@@ -621,6 +622,8 @@ class PyBuildExt(build_ext):
+         # Some modules that are normally always on:
+         #exts.append( Extension('_weakref', ['_weakref.c']) )
+ 
++        self.compiler.library_dirs = lib_dirs + [ '.' ]
++
+         # array objects
+         exts.append( Extension('array', ['arraymodule.c']) )
+ 
+-- 
+2.17.1
+
diff --git a/gnu/packages/python.scm b/gnu/packages/python.scm
index 1a8cd39de2..724260dabf 100644
--- a/gnu/packages/python.scm
+++ b/gnu/packages/python.scm
@@ -109,7 +109,8 @@ 
                                "python-2-deterministic-build-info.patch"
                                "python-2.7-site-prefixes.patch"
                                "python-2.7-source-date-epoch.patch"
-                               "python-2.7-adjust-tests.patch"))
+                               "python-2.7-adjust-tests.patch"
+                               "python-cross-compile.patch"))
       (modules '((guix build utils)))
       ;; suboptimal to delete failing tests here, but if we delete them in the
       ;; arguments then we need to make sure to strip out that phase when it
@@ -152,6 +153,9 @@ 
           (add-before
            'configure 'patch-lib-shells
            (lambda _
+             ,@(if (%current-target-system)
+                   '((setenv "_PYTHON_HOST_PLATFORM" ""))
+                   '())
              ;; Filter for existing files, since some may not exist in all
              ;; versions of python that are built with this recipe.
              (substitute* (filter file-exists?