Cross-compiling to Pi Zero

I am currently preparing for an IOT type project and whilst waiting for the test hardware I thought I would practice setting up an ARM CPU development environment using a Raspberry Pi Zero.  However, instead of building the compiler on the Pi, this time I want to build a cross-compiler on my laptop and then copy my built projects to the Pi for testing.  This has the advantage of using the laptop for the development process, vastly improving compile times at the expense of making debugging a little more complex.  It is also the typical workflow used for developing software for embedded devices.

There is an excellent description of this build procedure on Big Dan’s Blog.  His focus is on building a 2.6.4 compiler for use with Lazarus 1.6.  There is also a slightly dated page describing it on the Free Pascal WIKI.  I am only interested in producing command line programs using the current Free Pascal trunk (3.1.1), so I currently don’t need to build Lazarus as well although I may well come back to this at some point.

Before building the cross-compiler I need to have the matching GNU BinUtils.  It is possible to get pre-built binaries, but it is not difficult to build them from source as required.  As of writing, the latest version is 2.27 and it is available here.  The source needs to be extracted into a working folder and the following commands executed there:

./configure --target=arm-linux --disable-werror
sudo make install

There is a slight complication in that an additional parameter is required for the assembler when used to generate binaries for the Pi.  It defines the required hardware interface version for the output binary (v5 in this case).  The solution suggested in the WIKI is to wrap the assembler in a script that adds this option automatically when the assembler is called.  To do this, I rename the actual binary:

mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as-bin

Then create a Bash script to replace the ‘arm-linux-as’ file:

/usr/local/bin/arm-linux-as-bin -meabi=5 $@

Now that the required binary tools are in place I can move on to building Free Pascal from source.  Here is my script for doing this and installing the cross-compiler:

# Mount a ram drive to act as a fast temporary working area.
sudo mount -o size=8G -t tmpfs none /scratch

# Export the checkout to working folder. 
svn export --force /home/paul/Projects/FreePascal/3.1.1 /scratch/fpctmp

# Change to fpc working folder.
cd /scratch/fpctmp 
# Build the Arm hard float cross compiler for Raspberry Pi.
make crossall OS_TARGET=linux CPU_TARGET=arm CROSSOPT="-dFPC_ARMHF -CX 
# Install the cross compiler.s
sudo make crossinstall OS_TARGET=linux CPU_TARGET=arm 
     INSTALL_BASEDIR=/usr/lib/fpc/3.1.1 CROSSOPT="-dFPC_ARMHF -CX 

# Remove working folder.
sudo umount /scratch

This script is very similar to the one I use to build my main host compiler.  I am working on Kubuntu 16.10 and I use a temporary RAM Drive to improve the overall build time.  The make parameters describe the the target environment.  For Raspberry Pi Zero this is Linux on an Arm V6 CPU with hardware floating point support.

The default install location is ‘/usr/lib/fpc/3.1.1/ppcrossarm’, to use this without having to state the path I created a link at ‘/usr/bin/ppcrossarm’.  This also makes it callable from the main fpc compiler with cross-compile options.  The installation can be verified by calling ‘ppcrossarm -i’ on the command line, which should output:

Free Pascal Compiler version 3.1.1

Compiler date : 2016/11/09
Compiler CPU target: arm

A small test program similar to the one given in the WIKI page can be built and executed for the host computer with the following commands:

fpc info.pas

DATE 2016/11/08

The same program can be built for the Raspberry Pi with the following:

fpc -Tlinux -Parm -CfVFPV2 info.pas

And then executed from the Pi command Line via SSH:

pi@raspberrypi:~ $ ./info
DATE 2016/11/08                                                                
FPCTARGETCPU arm                                                               
FPCTARGETOS Linux                                                              

Lastly, I can use my desktop Lazarus as the working IDE and build for the Pi by setting the ‘Config and Target’ project options as shown in the screen shot below. Note that the ‘-CfVFPV2’ option must be added into the ‘Custom Options’ page as well.

Lazarus Cross-Compile Options

It is even possible to automate the transfer of the binary executable to the Pi Zero with the following command:

sshpass -p 'raspberry' scp test pi@rpi0.local:~

The SSHPASS command enables the automatic submission of the password.  SCP performs the actual copy via the SSH protocol.  SSHPASS is frowned upon for security reasons, but in this case, I am not worried about anyone knowing the passwords on my Pi’s!  SSHPASS can be installed on Ubuntu with:

sudo apt-get install sshpass


One thought on “Cross-compiling to Pi Zero

  1. Pingback: Free Pascal nRF52832 Embedded Target | Michell Computing News

Leave a Reply