Cross compiler

From ParabolaWiki
Jump to: navigation, search


1 Cross compiler - ARM

There might be different approaches to create a cross compiler. Arch Linux ARM for instance uses crosstool-NG. To my knowledge both being non-free.

In the contrary here is one approach aiming for a free cross compiler made to build packages for armv7h target architecture on x84_64 host/build architecture. Please keep the following notes in mind:

  • The cross-compiler described here will be configured with some flags

specific for the processor in Beaglebone Black, e. g. concerning floating point operation things if I remember correctly. If you were aiming for a different machine, you most likely need to change the configuration.

  • By no means I am an expert for creating cross-compilers or something.

However, I feel somehow confident about this compiler's ability to correctly compile code because it has successfully compiled several packages which are executed by my Beaglebone Black. One of them being the kernel: linux-libre-am33x. My one and only test machine runs without noticable issues with those packages since about 6-12 months.

  • The package sources are currently stored on my home server. Once some

parabola hacker feels comfortable with them, possibly after reading the sources, some testing, and improvement, she could transfer the sources to a parabola server. Please keep in mind my server changes its IP address on a regular bases (maybe once every 24h) or it might not be available at all.

You should be able to download the sources with

$ git clone git://git.donderklumpen.de/parabola_arm

1.1 Short build instructions

Build and install the packages in the following order:

armv7l-unknown-linux-gnueabihf-binutils
armv7l-unknown-linux-gnueabihf-linux-libre-api-headers
armv7l-unknown-linux-gnueabihf-gcc-stage1
armv7l-unknown-linux-gnueabihf-glibc-headers-and-startfiles
armv7l-unknown-linux-gnueabihf-gcc-stage2
armv7l-unknown-linux-gnueabihf-glibc
armv7l-unknown-linux-gnueabihf-gcc

1.2 Detailed build instructions

These instructions aim for building the cross-compiler in a defined environment using tools provided by the package libretools.

For convinience create a variable storing the directory path pointing to the parabola_arm git repository, e. g.

$ PARABOLA_ARM=$HOME/parabola_arm

Step 1: Make sure the chroot is clean

If it exists already, remove it

$ sudo librechroot delete

Create a fresh chroot copy

$ sudo librechroot make

Remove any previously built or installed packages belonging to this cross-crompiler from the system which is hosting the chroot in the directory /var/cache/pacman/pkg. This directory will be made available to the chroot and might contain whatever old packages destroying the clean and defined build environment which is being aimed for. (Actually I consider this a bug in libretools and plan to submit a bug report on it.)

# ... /var/cache/pacman/pkg/armv7l-unknown-linux-gnueabihf-*

Note, that during the build process described hereafter, the directory /var/cache/pacman/pkg fills up with packages that are installed in the chroot. So in case you stop the build process at any step and start it again from the beginning, make sure to remove the above said armv7l-unknown-linux-gnueabihf-* packages from the /var/cache/pacman/pkg again.

Step 2: Build armv7l-unknown-linux-gnueabihf-binutils

$ cd $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-binutils
$ sudo libremakepkg

Step 3: Build armv7l-unknown-linux-gnueabihf-linux-libre-api-headers

$ cd $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-linux-libre-api-headers
$ sudo libremakepkg

Step 4: Build armv7l-unknown-linux-gnueabihf-gcc-stage1

$ cd  $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-gcc-stage1
$ sudo libremakepkg

Step 5: Build armv7l-unknown-linux-gnueabihf-glibc-headers-and-startfiles

$ cd $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-glibc-headers-and-startfiles
$ sudo libremakepkg

Step 6: Build armv7l-unknown-linux-gnueabihf-gcc-stage2

$ cd  $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-gcc-stage2
$ sudo libremakepkg

Step 7: Install armv7l-unknown-linux-gnueabihf-gcc-stage2 in the chroot

The package armv7l-unknown-linux-gnueabihf-gcc-stage2 must be installed and at the same step the package armv7l-unknown-linux-gnueabihf-gcc-stage1 must be removed. libremakepkg and librechroot don't do this automatically, so it must be done by hand. Therefore enter the chroot

$ sudo librechroot enter

In the chroot install the newly created package armv7l-unknown-linux-gnueabihf-gcc-stage2 (confirm with "y")

# pacman -U 

/repo/armv7l-unknown-linux-gnueabihf-gcc-stage2-4.9.2-1-x86_64.pkg.tar.xz Exit

chroot
# exit

Step 8: Build armv7l-unknown-linux-gnueabihf-glibc

$ cd  $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-glibc
$ sudo libremakepkg

Step 9: Build armv7l-unknown-linux-gnueabihf-gcc

$ cd $PARABOLA_ARM/armv7l-unknown-linux-gnueabihf-gcc
$ sudo libremakepkg

1.3 Installation

Once all the packages are build in the correct order only the following packages need to be installed for the cross-compiler to be used:

armv7l-unknown-linux-gnueabihf-binutils
armv7l-unknown-linux-gnueabihf-linux-libre-api-headers
armv7l-unknown-linux-gnueabihf-glibc
armv7l-unknown-linux-gnueabihf-gcc

The other packages are only needed during the intermediate steps for building these final packages.

1.4 Usage with an example package

This is an example of how the cross-compiler can be used to build some package. The example package source is stored in the same git repository as is the cross-compiler. The package is called linux-libre. It is directly derived from Parabola's linux-libre package and does not involve any additional sources, nor any binaries, nor any additional license. Thus, the derived package should be free as is its parent package. The derived package aims for adding two things:

  1. linux-libre configuration for armv7h architecture and
  2. the possibility of building the package with a cross-compiler.

In the following are the steps that build the linux-libre package for armv7h architecture on a x86_64 machine. The arm machine is not involved in this process at all (no distcc). Compiling and linking is done at the x86_64 machine.

Step 0: Inspect the package sources

Besides the usual PKGBUILD file, the package source also contains the original PKGBUILD file (renamed PKGBUILD.parabola) from the Parabola's package. This file is not involved in the build process at all. It's there for convinience only, so you can easily see what was changed:

$ git diff --no-index PKGBUILD.parabola PKGBUILD

In the same way you can see how the makepkg-cross.conf file differs from your machine's /etc/makepkg.conf

$ git diff --no-index /etc/makepkg.conf makepkg-cross.conf

Step 1: Make sure the chroot is clean

If it exists already, remove it

$ sudo librechroot delete

Create a fresh chroot copy

$ sudo librechroot make

Step 2: Install the cross-compiler

Adopt the path to were you stored the cross-compiler packages

$ sudo librechroot install-file 

armv7l-unknown-linux-gnueabihf-binutils-2.24-8-x86_64.pkg.tar.xz

$ sudo librechroot install-file 

armv7l-unknown-linux-gnueabihf-linux-libre-api-headers-3.16.2_gnu-1-x86_64.pkg.tar.xz

$ sudo librechroot install-file 

armv7l-unknown-linux-gnueabihf-glibc-2.20-4-x86_64.pkg.tar.xz

$ sudo librechroot install-file 

armv7l-unknown-linux-gnueabihf-gcc-4.9.2-1-x86_64.pkg.tar.xz

Step3 : Build the package

$ sudo CROSS_COMPILE=armv7l-unknown-linux-gnueabihf- libremakepkg -- 

--config makepkg-cross.conf

the result from these steps are the following archives which can be installed on a armv7h machine:

linux-libre-3.18.1_gnu-1-armv7h.pkg.tar.xz
linux-libre-docs-3.18.1_gnu-1-armv7h.pkg.tar.xz
linux-libre-headers-3.18.1_gnu-1-armv7h.pkg.tar.xz