Attaching an updated patch which has been tested to not break native builds.

The patch modifies debian/rules such that for cross-builds, the recommended
build options for GHC are used (i.e. -O0 and building with the embedded libffi).

In order to cross-build GHC for a given architecture, one can now simply use
sbuild with the --host= and --profile= parameters:

sbuild -d sid --host=m68k --profile=cross ghc*dsc

Only caveat is that haskell-devscripts-minimal has to be manually pre-installed
in the build chroot because of the known issue with arch:all packages [1].

Upstream has also added lots of changes to improve cross-builds even more [2],
so we can expect to be smoother with GHC 8.2.0. The biggest thing that is still
lacking for cross-compiled versions of GHC is the missing documentation and
Haddock.

Adrian

> [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=818115
> [2] http://git.haskell.org/ghc.git?a=search&h=HEAD&st=commit&s=cross

-- 
 .''`.  John Paul Adrian Glaubitz
: :' :  Debian Developer - glaub...@debian.org
`. `'   Freie Universitaet Berlin - glaub...@physik.fu-berlin.de
  `-    GPG: 62FF 8A75 84E0 2956 9546  0006 7426 3B37 F5B5 F913
diff -Nru ghc-8.0.2/debian/control ghc-8.0.2/debian/control
--- ghc-8.0.2/debian/control	2017-06-25 21:52:22.000000000 +0200
+++ ghc-8.0.2/debian/control	2017-08-02 20:55:14.000000000 +0200
@@ -6,17 +6,17 @@
 Standards-Version: 3.9.8
 Build-Depends:
   debhelper (>= 10),
-  haskell-devscripts-minimal,
+  haskell-devscripts-minimal <!cross>,
   devscripts,
   grep-dctrl,
   pkg-config,
-  ghc (>= 7.8),
+  ghc:native (>= 7.8),
   libgmp-dev,
   llvm-3.7 [arm64 armel armhf],
-  libffi-dev,
+  libffi-dev <!cross>,
   binutils [arm64 armel armhf],
   libncurses5-dev,
-  python-sphinx,
+  python-sphinx <!cross>,
   dpkg-dev (>= 1.16.1.1)
 Build-Depends-Indep:
   hscolour,
diff -Nru ghc-8.0.2/debian/patches/avoid-CrossCompilerPrefix-stage2.patch ghc-8.0.2/debian/patches/avoid-CrossCompilerPrefix-stage2.patch
--- ghc-8.0.2/debian/patches/avoid-CrossCompilerPrefix-stage2.patch	1970-01-01 01:00:00.000000000 +0100
+++ ghc-8.0.2/debian/patches/avoid-CrossCompilerPrefix-stage2.patch	2017-08-02 21:44:17.000000000 +0200
@@ -0,0 +1,31 @@
+Description: Don't use cross-compile prefix for stage2 cross-builds
+Origin: http://git.haskell.org/ghc.git/commitdiff/f2685df3b10e13f142736f28835e9064334bc143
+Note: Can be dropped for 8.2.0
+Last-Update: 2017-08-02
+
+--- ghc-8.0.2.orig/mk/config.mk.in
++++ ghc-8.0.2/mk/config.mk.in
+@@ -518,11 +518,6 @@ SUPPORTS_THIS_UNIT_ID = @SUPPORTS_THIS_U
+ 
+ WhatGccIsCalled       = @WhatGccIsCalled@
+ GccVersion            = @GccVersion@
+-ifeq "$(phase)" "0"
+-CrossCompilePrefix    =
+-else
+-CrossCompilePrefix    = @CrossCompilePrefix@
+-endif
+ # TargetPlatformFull retains the string passed to configure so we have it in
+ # the necessary format to pass to libffi's configure.
+ TargetPlatformFull    = @TargetPlatformFull@
+@@ -557,6 +552,11 @@ CrossCompiling        = @CrossCompiling@
+ # See Note [Stage1Only vs stage=1]
+ Stage1Only = NO
+ 
++# Installed tools prefix:
++#    we add prefix to crosscompiler GHC only (ghc-stage1),
++#    not cross-built GHC (not ghc-stage2).
++CrossCompilePrefix    = $(if $(filter YES,$(Stage1Only)),@CrossCompilePrefix@,)
++
+ # Install stage 2 by default, or stage 1 in the cross compiler
+ # case. Can be changed to 3
+ INSTALL_GHC_STAGE= $(if $(filter YES,$(Stage1Only)),1,2)
diff -Nru ghc-8.0.2/debian/patches/build-unlit-and-hp2ps-twice.patch ghc-8.0.2/debian/patches/build-unlit-and-hp2ps-twice.patch
--- ghc-8.0.2/debian/patches/build-unlit-and-hp2ps-twice.patch	1970-01-01 01:00:00.000000000 +0100
+++ ghc-8.0.2/debian/patches/build-unlit-and-hp2ps-twice.patch	2017-08-02 12:42:15.000000000 +0200
@@ -0,0 +1,80 @@
+Description: Build unlit and hp2ps twice
+Author: Thomas Miedema <thomasmied...@gmail.com>
+        John Paul Adrian Glaubitz <glaub...@physik.fu-berlin.de>
+
+Index: ghc-8.0.1/utils/ghc-pkg/ghc.mk
+===================================================================
+--- ghc-8.0.1.orig/utils/ghc-pkg/ghc.mk
++++ ghc-8.0.1/utils/ghc-pkg/ghc.mk
+@@ -27,7 +27,7 @@ utils/ghc-pkg_PACKAGE = ghc-pkg
+ # Note [Why build certain utils twice?]
+ #
+ # We build certain utils twice: once with stage0, and once with stage1.
+-# Examples are ghc-pkg and hsc2hs.
++# Examples are ghc-pkg, hsc2hs, hp2ps and unlit.
+ #
+ # These tools are needed during the bootstrapping process, so we have to use
+ # stage0 to build them at first (stage1 doesn't exist yet). (side note: they're
+@@ -38,6 +38,11 @@ utils/ghc-pkg_PACKAGE = ghc-pkg
+ # dynamically linked. But the stage0 copies are either statically linked, or
+ # linked against libraries on the build machine.
+ #
++# Another reason why we can't install the stage0 copies is that they are
++# built to run on the build(=host) platform, but when installing a
++# "cross-compiled stage2 compiler" we need copies that run on the target
++# platform.
++#
+ # Therefore we build fresh copies, using the stage1 compiler, and install them
+ # when you run 'make install'. They are not used for any other purpose.
+ 
+Index: ghc-8.0.1/utils/hp2ps/ghc.mk
+===================================================================
+--- ghc-8.0.1.orig/utils/hp2ps/ghc.mk
++++ ghc-8.0.1/utils/hp2ps/ghc.mk
+@@ -17,10 +17,23 @@ utils/hp2ps_dist_C_SRCS          = AreaB
+                                    Utilities.c
+ utils/hp2ps_dist_EXTRA_LIBRARIES = m
+ utils/hp2ps_dist_PROGNAME        = $(CrossCompilePrefix)hp2ps
+-utils/hp2ps_dist_INSTALL         = YES
++utils/hp2ps_dist_INSTALL         = NO
+ utils/hp2ps_dist_INSTALL_INPLACE = YES
+ 
+ utils/hp2ps_CC_OPTS += $(addprefix -I,$(GHC_INCLUDE_DIRS))
+ 
+ $(eval $(call build-prog,utils/hp2ps,dist,0))
+ 
++utils/hp2ps_dist-install_C_SRCS  = AreaBelow.c Curves.c Error.c Main.c \
++                                   Reorder.c TopTwenty.c AuxFile.c Deviation.c \
++                                   HpFile.c Marks.c Scale.c TraceElement.c \
++                                   Axes.c Dimensions.c Key.c PsFile.c Shade.c \
++                                   Utilities.c
++utils/hp2ps_dist-install_EXTRA_LIBRARIES = m
++utils/hp2ps_dist-install_PROGNAME = hp2ps
++utils/hp2ps_dist-install_TOPDIR  = YES
++utils/hp2ps_dist-install_INSTALL = YES
++utils/hp2ps_dist-install_INSTALL_INPLACE = NO
++
++# See Note [Why build certain utils twice?].
++$(eval $(call build-prog,utils/hp2ps,dist-install,1))
+Index: ghc-8.0.1/utils/unlit/ghc.mk
+===================================================================
+--- ghc-8.0.1.orig/utils/unlit/ghc.mk
++++ ghc-8.0.1/utils/unlit/ghc.mk
+@@ -13,8 +13,16 @@
+ utils/unlit_dist_C_SRCS  = unlit.c
+ utils/unlit_dist_PROGNAME = unlit
+ utils/unlit_dist_TOPDIR  = YES
+-utils/unlit_dist_INSTALL = YES
++utils/unlit_dist_INSTALL = NO
+ utils/unlit_dist_INSTALL_INPLACE = YES
+ 
+ $(eval $(call build-prog,utils/unlit,dist,0))
+ 
++utils/unlit_dist-install_C_SRCS  = unlit.c
++utils/unlit_dist-install_PROGNAME = unlit
++utils/unlit_dist-install_TOPDIR  = YES
++utils/unlit_dist-install_INSTALL = YES
++utils/unlit_dist-install_INSTALL_INPLACE = NO
++
++# See Note [Why build certain utils twice?].
++$(eval $(call build-prog,utils/unlit,dist-install,1))
diff -Nru ghc-8.0.2/debian/patches/series ghc-8.0.2/debian/patches/series
--- ghc-8.0.2/debian/patches/series	2017-06-25 21:52:22.000000000 +0200
+++ ghc-8.0.2/debian/patches/series	2017-08-02 21:41:54.000000000 +0200
@@ -8,3 +8,6 @@
 compiler-cmm-PprC-sparc-alignment.patch
 x32-use-native-x86_64-insn.patch
 osdecommitmemory-compat.patch
+use-stage1-binaries-for-install.patch
+build-unlit-and-hp2ps-twice.patch
+avoid-CrossCompilerPrefix-stage2.patch
diff -Nru ghc-8.0.2/debian/patches/use-stage1-binaries-for-install.patch ghc-8.0.2/debian/patches/use-stage1-binaries-for-install.patch
--- ghc-8.0.2/debian/patches/use-stage1-binaries-for-install.patch	1970-01-01 01:00:00.000000000 +0100
+++ ghc-8.0.2/debian/patches/use-stage1-binaries-for-install.patch	2017-08-02 12:42:15.000000000 +0200
@@ -0,0 +1,26 @@
+Description: Use the stage1 binaries for install
+ In order to be able to perform a cross-build, we need to use
+ the stage1 binaries during installation. Both ghc and ghc-pkg
+ are run during the install target and therefore must be able
+ to run on the build machine.
+ .
+Author: John Paul Adrian Glaubitz <glaub...@physik.fu-berlin.de>
+Last-Update: 2017-01-29
+
+--- ghc-8.0.1.orig/ghc.mk
++++ ghc-8.0.1/ghc.mk
+@@ -958,8 +958,12 @@ INSTALLED_PACKAGE_CONF=$(DESTDIR)$(topdi
+ # Install packages in the right order, so that ghc-pkg doesn't complain.
+ # Also, install ghc-pkg first.
+ ifeq "$(Windows_Host)" "NO"
+-INSTALLED_GHC_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc
+-INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc-pkg
++# Use the inplace/stage1 versions for installation,
++# since the installed versions are built for the target
++#INSTALLED_GHC_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc
++#INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(ghclibexecdir)/bin/ghc-pkg
++INSTALLED_GHC_REAL=$(CURDIR)/inplace/bin/ghc-stage1
++INSTALLED_GHC_PKG_REAL=$(CURDIR)/utils/ghc-pkg/dist/build/tmp/ghc-pkg
+ else
+ INSTALLED_GHC_REAL=$(DESTDIR)$(bindir)/ghc.exe
+ INSTALLED_GHC_PKG_REAL=$(DESTDIR)$(bindir)/ghc-pkg.exe
diff -Nru ghc-8.0.2/debian/rules ghc-8.0.2/debian/rules
--- ghc-8.0.2/debian/rules	2017-06-27 13:47:06.000000000 +0200
+++ ghc-8.0.2/debian/rules	2017-08-02 22:56:55.000000000 +0200
@@ -22,20 +22,21 @@
 export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
 export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
 export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
-# Commented out for now.  The build scripts don't recognise i486 as an
-# architecture and cross compiling isn't supported anyway.
-#ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
-#  confflags += --build $(DEB_HOST_GNU_TYPE)
-#else
-#  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
-#endif
+
+# We're cross-building if DEB_BUILD_GNU_TYPE != DEB_HOST_GNU_TYPE
+ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
+  confflags += --target $(DEB_HOST_GNU_TYPE) --enable-unregisterised
+  BUILD_DOCS=NO
+else
+  confflags += --with-system-libffi
+  BUILD_DOCS=YES
+endif
 
 ProjectVersion=$(shell cat VERSION)
 
 GHC=$(firstword $(shell bash -c "type -p ghc"))
 EXTRA_CONFIGURE_FLAGS=--with-ghc="$(GHC)"
 
-BUILD_HADDOCK_DOCS=YES
 DEB_HOOGLE_TXT_DIR = /usr/lib/ghc-doc/hoogle/
 
 ifneq (,$(filter powerpcspe x32, $(DEB_HOST_ARCH)))
@@ -49,10 +50,24 @@
 	dh_autoreconf perl -- boot
 
 override_dh_auto_configure:
+ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
 	echo "SRC_HC_OPTS += -lffi -optl-pthread" >> mk/build.mk
+else
+	echo "SRC_HC_OPTS += -O0 -H64m" >> mk/build.mk
+	echo "GhcStage1HcOpts = -O" >> mk/build.mk
+	echo "GhcStage2HcOpts = -O0" >> mk/build.mk
+	echo "GhcLibHcOpts = -O" >> mk/build.mk
+	echo "SplitObjs = NO" >> mk/build.mk
+endif
+ifeq (YES,$(BUILD_DOCS))
 	echo "HADDOCK_DOCS := YES" >> mk/build.mk
 	echo "EXTRA_HADDOCK_OPTS += --mathjax=file:///usr/share/javascript/mathjax/MathJax.js" >> mk/build.mk
 	echo "XSLTPROC_OPTS += --nonet" >> mk/build.mk
+else
+	echo "HADDOCK_DOCS := NO" >> mk/build.mk
+	echo "BUILD_SPHINX_HTML := NO" >> mk/build.mk
+	echo "BUILD_SPHINX_PDF := NO" >> mk/build.mk
+endif
 ifneq (,$(filter arm64 armel armhf, $(DEB_HOST_ARCH)))
 	echo "SRC_HC_OPTS     += -optl-B/usr/bin/ld.gold" >> mk/build.mk
 endif
@@ -100,11 +115,12 @@
 	echo 'dvidir  := $$(docdir)'                   >> mk/build.mk
 	echo 'pdfdir  := $$(docdir)'                   >> mk/build.mk
 	echo 'psdir   := $$(docdir)'                   >> mk/build.mk
+	echo 'STRIP_CMD = $(DEB_HOST_GNU_TYPE)-strip'  >> mk/build.mk
+	echo 'RANLIB_CMD = $(DEB_HOST_GNU_TYPE)-ranlib'>> mk/build.mk
 	# We want verbose builds
 	echo 'V=1'                                     >> mk/build.mk
 	./configure $(confflags) --prefix=/usr \
 		$(EXTRA_CONFIGURE_FLAGS) \
-		--with-system-libffi \
 		--with-llc=llc-3.7 \
 		--with-opt=opt-3.7
 
@@ -166,12 +182,10 @@
 	# Check if we have a ghci binary
 	if test -e debian/tmp/usr/bin/ghci-$(ProjectVersion); then \
 	    echo 'ghci=ghc-ghci' >> debian/ghc.substvars ; fi
-
+ifeq (YES,$(BUILD_DOCS))
 	# Add haddock substvars
 	echo "haddock:Depends=haddock-interface-$$(debian/tmp/usr/lib/ghc/bin/haddock --interface-version)" >> debian/ghc-doc.substvars
 	echo "haddock:Provides=haddock-interface-$$(debian/tmp/usr/lib/ghc/bin/haddock --interface-version)" >> debian/ghc.substvars
-
-ifeq (YES,$(BUILD_HADDOCK_DOCS))
 	mkdir -p debian/tmp/usr/lib/ghc-doc
 	cp debian/gen_contents_index debian/tmp/usr/lib/ghc-doc/
 	chmod +x debian/tmp/usr/lib/ghc-doc/gen_contents_index
@@ -181,9 +195,8 @@
 	    mkdir debian/tmp/usr/lib/ghc-doc/haddock/ghc/`basename $$f` ; \
 	    mv $$f/*.haddock debian/tmp/usr/lib/ghc-doc/haddock/ghc/`basename $$f` ; done
 	cd debian/tmp/usr/share/doc/ghc-doc/html/libraries/; ln -s ghc-$(ProjectVersion) ghc
-	install -Dm 644 debian/index.html debian/tmp/usr/share/doc/ghc-doc/index.html
-endif
 
+	install -Dm 644 debian/index.html debian/tmp/usr/share/doc/ghc-doc/index.html
 	# manpages
 	rm -f debian/*.1
 	echo ".so man1/ghc.1" > debian/ghc-$(ProjectVersion).1
@@ -199,14 +212,14 @@
 	echo debian/*.1 > debian/ghc.manpages
 	cp debian/haddock.man debian/haddock.1
 	echo debian/haddock.1 >> debian/ghc.manpages
-
+	find debian/tmp/usr/share/man $(FILES) > debian/ghc.install
+endif
 	# ####################
 	# Now all the files are sorted, create the package filelists
 
 	# ghc
-	find debian/tmp/usr/bin $(FILES)                  > debian/ghc.install
+	find debian/tmp/usr/bin $(FILES)                 >> debian/ghc.install
 #	find debian/tmp/usr/share/ghc* $(FILES)          >> debian/ghc.install
-	find debian/tmp/usr/share/man $(FILES)           >> debian/ghc.install
 	find debian/tmp/usr/lib/ghc $(FILES) ! $(PROF_FILE) >> debian/ghc.install
 	find debian/tmp/var				 >> debian/ghc.install
 	echo debian/tmp/usr/share/lintian/overrides/ghc >> debian/ghc.install
@@ -214,14 +227,14 @@
 	find debian/tmp/usr/lib $(FILES) $(PROF_FILE) > debian/ghc-prof.install
 	echo debian/tmp/usr/share/lintian/overrides/ghc-prof >> debian/ghc-prof.install
 	# ghc-doc
-ifeq (YES,$(BUILD_HADDOCK_DOCS))
+ifeq (YES,$(BUILD_DOCS))
 	mkdir -p debian/tmp/$(DEB_HOOGLE_TXT_DIR)
 	find debian/tmp/usr/share/doc/ghc-doc/html/libraries/*/ -name "*.txt" \
 		-printf "%p $(DEB_HOOGLE_TXT_DIR)/%f\n" >> debian/ghc-doc.links
 	find debian/tmp/usr/share/doc/ghc-doc $(FILES) > debian/ghc-doc.install
 	find debian/tmp/usr/lib/ghc-doc $(FILES)      >> debian/ghc-doc.install
-endif
 	sed -i s,^debian/tmp,, debian/*.install debian/*.links
+endif
 	rm -f debian/ghc.links
 	echo "/var/lib/ghc/package.conf.d /usr/lib/ghc/package.conf.d" >> debian/ghc.links
 

Reply via email to