Bootstrapping GHC for a new architecture

Note: In order to be able to bootstrap GHC for a new architecture, the architecture has to be known to GHC. A minimal patch to add support for a new architecture is discussed in https://ghc.haskell.org/trac/ghc/ticket/11209.

Cross-compiling ghc stage1

To bootstrap ghc for a new architecture, it needs to be cross-compiled first. This requires a cross-compiler toolchain (gcc, binutils etc) for the target architecture as well as a working ghc installation on your host architecture.

1. Install the ghc build dependencies:

   $ apt build-dep ghc

2. Install the gcc cross toolchain for the target architecture:

   $ apt install gcc-m68k-linux-gnu g++-m68k-linux-gnu cpp-m68k-linux-gnu

3. Download a not too old version of ghc, we use 8.0.1:

   $ mkdir m68k
   $ cd m68k
   $ wget http://snapshot.debian.org/archive/debian/20160521T223144Z/pool/main/g/ghc/ghc_8.0.1.orig.tar.xz

4. Install libncurses5 and libncurses5-dev for the target architecture on the host machine with the help of MultiArch:

  (as root)
  $ apt install debian-ports-archive-keyring
  $ echo "deb [arch=m68k] http://ftp.ports.debian.org/debian-ports/ unstable main" >> /etc/apt/sources.list
  $ dpkg --add-architecture m68k
  $ apt update
  $ apt install libncurses5:m68k libncurses5-dev:m68k
  $ logout

5. Unpack the source tarball:

   $ tar xf ghc_8.0.1.orig.tar.xz

6. Patch the main makefile to fix a problem with the installation of the stage2 compiler:

diff --git a/ghc.mk b/ghc.mk
index e095ffd..508746d 100644
--- a/ghc.mk
+++ b/ghc.mk
@@ -955,8 +957,12 @@ INSTALLED_PACKAGE_CONF=$(DESTDIR)$(topdir)/package.conf.d
 # 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

7. Copy mk/build.mk.sample to mk/build.mk and enable "quick-cross":

   $ cp -av mk/build.mk.sample mk/build.mk
   $ sed -i 's/#\(BuildFlavour\s*\=\s*quick-cross\)/\1/g' mk/build.mk

8. Run configure with $ARCH-linux-gnu as TARGET where $ARCH is your architecture:

   $ ./configure --enable-unregisterised --target=m68k-linux-gnu

9. Run "make".

   $ make

After this, you will end up with a stage1 compiler that can be installed on the host system with "make install".

Building stage2

To get the stage2 compiler which will be able to run on the target system, edit mk/flavours/quick-cross.mk to set "?Stage1Only" from "YES" to "NO", then run make again:

   $ sed -i 's/\(Stage1Only\s*\=\s*\)YES/\1NO/g' mk/flavours/quick-cross.mk
   $ make install DESTDIR=/path/for/target-ghc

Building the ghc Debian packages on the target

1. Make sure ghc is properly installed and working.

2. Download the source of the ghc package on the target system:

   $ dget -u http://snapshot.debian.org/archive/debian-debug/20160522T042329Z/pool/main/g/ghc/ghc_8.0.1-1.dsc

3. Install the build dependencies for ghc on the target system:

   $ apt-get install debhelper devscripts dh-autoreconf autotools-dev grep-dctrl pkg-config libgmp-dev libffi-dev binutils libncurses5-dev python-sphinx dpkg-dev

4. Remove "ghc" and "haskell-devscripts-minimal" from debian/control in GHC package source.

5. Download haskell-devscripts-minimal and install into chroot root with "dpkg-deb -x haskell-devscripts-minimal*deb".

6. Build the ghc package with:

   $ dpkg-buildpackage -B

Bootstrapping happy and alex

Once ghc has been bootstrapped for the new architecture, happy and alex need to be bootstrapped as well. For this, these two packages support build profiles which can be directly activated with sbuild and both are build natively:

First happy:

$ mkdir happy
$ cd happy
$ dget -u http://snapshot.debian.org/archive/debian/20151222T094900Z/pool/main/h/happy/happy_1.19.5-5.dsc
$ sbuild --arch=m68k --no-arch-all --profiles=stage1 -d unstable happy_1.19.5-5.dsc
$ debsign happy_1.19.5-5_m68k.changes
$ dupload --to debian-ports happy_1.19.5-5_m68k.changes

Install the newly built happy package into your unstable chroot and build alex analogous to happy:

$ mkdir alex
$ cd alex
$ dget -u http://snapshot.debian.org/archive/debian/20160111T094634Z/pool/main/a/alex/alex_3.1.6-1.dsc
$ sbuild --arch=m68k --no-arch-all --profiles=stage1 -d unstable alex_3.1.6-1.dsc
$ debsign alex_3.1.6-1_m68k.changes
$ dupload --to debian-ports alex_3.1.6-1_m68k.changes


CategoryPorts