Raspberry Pi Lazarus Revisited

Much has changed since my original post about building Lazarus on the Raspberry Pi. Firstly, the Raspberry Pi now consists of quite a family of boards with very different characteristics; the Raspbian operating system has matured greatly and the Free Pascal and Lazarus projects have passed through several releases each.

I have been building Free Pascal and to a lesser extent Lazarus off and on over the last couple of years on the Pi and it is always a challenge!  There are older versions available in the Raspian repository which may be installed via apt-get, but they are quite well behind the current trunk releases of both projects; to the extent that it can make cross-platform portability quite difficult if you have used any of the newer features now available in the later versions.

As I am currently preparing for a project that will target several ARM processors, I thought I would take the opportunity to document as fully as I can the steps required to get a working Pascal IDE on the Raspberry Pi as an ARM reference board.

When working on a Raspberry Pi, it is important to realise that the computing resources available are really at the minimum end of what Lazarus, in particular, was designed to run with.  This is less of a problem on the Pi 2 and 3, but can cause real frustration on the A’s, B’s and Zeros.

Prerequisites
I would recommend starting with a fresh install of Raspian to reduce the chances of conflicts or missing dependencies.  These instructions were tested with the ‘2016-09-23-raspbian-jessie’ image.  Before attempting the build process there are several development libraries that have to be installed for Lazarus to compile.  Subversion is also required to fetch the up-to-date sources of both the compiler and IDE:

sudo apt-get install subversion

The developer packages required by Lazarus can be installed by (removing returns):

sudo apt-get install -y libx11-dev libgdk-pixbuf2.0-dev libcairo2-dev 
gir1.2-coglpango-1.0 libpangox-sudo apt-get install -y libx11-dev 
libgdk-pixbuf2.0-dev libcairo2-dev gir1.2-coglpango-1.0 
libpangox-1.0-dev xorg-dev libgtk2.0-dev libpango1.0-dev1.0-dev 
xorg-dev libgtk2.0-dev libpango1.0-dev

If the target machine is ARM6 (i.e. not a Pi 2 or 3), memory restrictions will cause the build processes to fail at several points.  To get round these problem two steps are required.  Firstly the memory split between the CPU and GPU must be altered to give most of the RAM to the GPU.  This can be changed by:

sudo raspi-config

Then select ‘7 Advanced Options’ and ‘A3 Memory Split’, choose the option for 16Mb.

Secondly, the default SWAP file size of 100Mb is too small for the Lazarus make process.  It can be extended by editing the ‘/etc/dphys-swapfile’ configuration file, changing the ‘CONF_SWAPSIZE=100’ option from 100 to 2000. Obviously, you must have sufficient empty space on your SD card.  I am currently using 16Gb class 10 cards.

Now the make process should work on all Pi’s except the model A.  I have not attempted to build this on a model A myself, but I suspect it might not be possible due to the lower available RAM and given the trouble I have had getting this to compile on a model B.

The build process requires a working folder to store the source code and generate the binary output files.  This can be anywhere the user has write permissions to.  My normal practice is to have a folder called FreePascal that I store in my home folder within a ‘Projects’ folder.  So the full path to the Lazarus folder on a Raspberry Pi would be:

/home/pi/Projects/FreePascal/Lazarus

and the current trunk version of Free Pascal here:

/home/pi/Projects/FreePascal/3.1.1

These paths may be exchanged for any other preference, but the following scripts would need adapting accordingly.

Building Free Pascal
The compiler is self hosted, meaning it is built using itself!  To start things off, there must be an initial Free Pascal seed compiler.  Not only that, but it must be the prior official release version, so 3.1.1 (the current trunk version) requires 3.0.0.  Unfortunately there is no version 3 available for Raspian.  The latest version in the repository is 2.6.4 in the package ‘fp-compiler-2.6.4’.  So before 3.1.1 can be built a 3.0.0 seed compiler must be compiled first!  A seed compiler need not be installed, indeed just the raw executable is the only file required as it will build any other units itself from the available sources.  The compiler package can be downloaded without installing with this command:

sudo apt-get -d install fp-compiler-2.6.4

The actual file will be here:

/var/cache/apt/archives/fp-compiler-2.6.4_2.6.4+dfsg-4+rpi1_armhf.deb

A deb file is a special type of archive. It can be uncompressed and within it, there will be a file called ‘data.tar.xz’.  This is also an archive of the package files.  Extract this to a temporary folder and search for the file ‘ppcarm’.  This is the actual compiler program needed to start the 3.0.0 build process. Copy this file to:

/home/pi/Projects/FreePascal/ppcarm.6hf.2.6.4

Using Subversion, the 3.0.0 source code can be downloaded onto the Pi with:

cd /home/pi/Projects/FreePascal/
svn co http://svn.freepascal.org/svn/fpc/tags/release_3_0_0 3.0.0

The 3.0.0 compiler can then be built with (all make options on a single line):

cd /home/pi/Projects/FreePascal/3.0.0
make clean all OPT="-dARMHF -O-" 
                  FPC=/home/pi/Projects/FreePascal/ppcarm.6hf.2.6.4

The additional pass-through options needed (OPT=”-dARMHF -O-“) ensures the inclusion of hard float support in the compiler and prevents 2.6.4 using optimisations that would fail on 2.6.4 during the build cycle.  This process will take a long time, go and have some lunch/tea/beer!  Once it has finished, check that there is a working 3.0.0 compiler:

/home/pi/Projects/FreePascal/3.0.0/compiler/ppcarm -l

Copy this new executable file to ‘/home/pi/Projects/FreePascal/ppcarm.6hf.3.0.0’ with:

cp /home/pi/Projects/FreePascal/3.0.0/compiler/ppcarm 
                        /home/pi/Projects/FreePascal/ppcarm.6hf.3.0.0

The rest of the 3.0.0 files are no longer needed (unless you wish to install this version) so they can be deleted with:

cd /home/pi/Projects/FreePascal/
sudo rm -r 3.0.0

Moving on to the trunk build, first download the latest Free Pascal source code:

cd /home/pi/Projects/FreePascal/
svn co http://svn.freepascal.org/svn/fpc/trunk 3.1.1

This can then be built with the 3.0.0 seed compiler:

cd /home/pi/Projects/FreePascal/3.1.1
make clean all OPT=-dARMHF 
                    FPC=/home/pi/Projects/FreePascal/ppcarm.6hf.3.0.0

This will also take a long time.  When it has finished, install the new compiler with:

cd /home/pi/Projects/FreePascal/3.1.1
sudo make install PP=compiler/ppcarm PREFIX=/usr
sudo rm -f /usr/bin/ppcarm
sudo ln -s /usr/lib/fpc/3.1.1/ppcarm /usr/bin/ppcarm

Check that this is working with:

fpc -l

The Free Pascal source code can optionally be installed with:

sudo make sourceinstall PREFIX=/usr
sudo rm -f /usr/share/fpcsrc
sudo ln -sf /usr/share/src/fpc-3.1.1/fpc /usr/share/fpcsrc

Finally a new configuration file can be created with:

sudo /usr/lib/fpc/3.1.1/samplecfg /usr/lib/fpc/3.1.1 /etc

Building Lazarus
As with the compiler the first step for Lazarus is to create a working folder and fetch the source code with Subversion:

cd /home/pi/Projects/FreePascal
 svn co http://svn.freepascal.org/svn/lazarus/trunk Lazarus

Building Lazarus is then straight forward with:

cd /home/pi/Projects/FreePascal/Lazarus
make clean all

If this process stops with some thing like this:

Error: (xxx) Can't call the resource compiler "/usr/bin/fpcres", 
                                           switching to external mode

This will be due to exhausting the SWAP file which will need extending further as described above.  It is interesting to watch the build processes’ memory use with the ‘top’ command running in a second shell.  You can actually see the memory drop to zero as the swap file is exhausted at that point.

I don’t usually install Lazarus into the system folders as I regularly have to rebuild the IDE due to my use of various packages.  So instead, I run the program directly from the working folder. I do, however, create a desktop menu entry to integrate the application into the PIXEL desktop as described below.

Pre-built Seed Compilers
To save you many hours of your life, here are the two ARM6HF seed compilers as described above:

https://bitbucket.org/PaulFMichell/fpcutils/downloads/SeedCompilers.zip

The first is extracted from the standard Raspian install and the second was built by the first stage of this process on a model B.  Using the ppcarm.6hf.3.0.0 file you can skip the first two stages of the Free Pascal build process and do something more worthwhile with your time!

lazarusandmenueditor

Finishing Touches
The memory split applied at the beginning can now be reversed using raspi-config.  You need not reduce the SWAP file size unless you are pressed for space on your SD card.  I also like to create a Lazarus menu entry for the GUI.  In PIXEL, there is now a menu editor available on the ‘Preferences’ menu.  Browse to the ‘startlazarus’ program for the command option.  I like to use the icon ‘…/images/icons/Lazarus.png’ for the menu entry as I prefer the paw print graphic to the Cheater’s head, but that is just a personal preference!

3 thoughts on “Raspberry Pi Lazarus Revisited

  1. Pingback: Lazarus on the Raspberry Pi | Michell Computing News

  2. Pingback: FreePascal 2.7.1 on Raspberry Pi | Michell Computing News

  3. Pingback: Installing Lazurus on a Raspberry Pi 3 – Nerdery Blurtery

Leave a Reply