FreePascal 2.7.1 on Raspberry Pi

[EDIT: Please look here for more up-to-date instructions for Free Pascal 3.1.1 with Lazarus 1.7]

Since I wrote my original article about building FreePascal with Lazarus on the Raspberry Pi there have been a lot of developments.  One of the challenges with this platform is that the rapid pace of change very quickly makes a published set of instructions obsolete. This is now true of the previous scripts I have released which will not build the current trunk source code.

In my last post I covered how I now connect to the Pi from my laptop, which I find a lot more convenient.  In this post I will explain the current method I am using to build an up-to-date FreePascal compiler from the source code available from the official subversion repository.  Whilst the original compiler I built still works on the current release of Raspian, it does not contain many of the recent advances made by the FreePascal developers over the last year, or so.  The good news is that they have added a lot of new features that improve the quality of code generation for ARM chips.  This work has been the result of a concerted effort to support Android devices, but it also benefits developers wanting to target Raspberry Pis, making it a very attractive development language for the Pi.

The standard method for installing FreePascal from the Raspian repositories still works as Debian administrators would expect:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install fpc

However, this installs FreePascal 2.6.0. and since the current stable release is 2.6.4 this is now looking very dated.  The current development release, the one with the useful new features, is 2.7.1.

The source code for 2.7.1 can be downloaded using subversion, which needs to be installed before attempting to follow these instructions:

 sudo apt-get install subversion

The source code can then be retrieved using the following command.  I create a folder called FreePascal that I used to contain the various sources I use and the following command should be executed from there:

svn co http://svn.freepascal.org/svn/fpc/trunk 2.7.1

Also, if you are using a new install of Raspian, the default 2GB partition size is far to small for building FreePascal, I would recommend resizing the partition to take advantage of any additional space available on your SD card using the ‘raspi-config’ utility.

To be able to build 2.7.1, the required seed compiler is currently 2.6.4.  So to do this properly, we would need to build several sucessive releases of FreePascal to match these build requirements, which would be a very time consuming process.  Fortunately, I am not the only person interested in building FreePascal on the Pi; a very useful recent contribution was made by Thaddy de Koning.  He has built a 2.7.1 seed compiler which he has published here.  It is currently only a month old and he has also detailed the build method he uses here, the actual make command he uses is:

sudo make clean all distinstall OPT="-dREVINC -dFPC_ARMHF -CX 
-CpARMV6 -CfVFPV2 -CaEABIHF -OpARMV6 -O2 -OoFASTMATH -XX -Xs"

This assumes there is the required 2.6.4 compiler installed.  This is clearly not the case if you have a bare Raspian image installed.  Fortunately you can pass an option to the make script indicating the location of an unistalled seed compiler.  I extracted Thaddy’s actual compiler binary ‘lib/fpc/2.7.1/ppcarm’ from the above archive and copied it to my FreePascal working folder on the Pi.  I also renamed it ‘thaddyppcarm’ to avoid confusion once I start creating my own copies of this file.

Unfortunately, whilst trying to execute this process, I ran into a couple of problems. Firstly, as mentioned, the current make scripts expect 2.6.4 as the seed compiler.  Since Thaddy’s is later than this there is no problem with using it, except that the make script now performs version checking.  This can be suppressed by adding ‘OVERRIDEVERSIONCHECK=1’ to the command line.  The second problem was that the build process would halt with the following error:

Fatal: Can not open include file "revision.inc"

I don’t know why this should fail, or what the expected behaviour should be, but I found that process would complete successfully if the ‘REVINC’ option was removed from the command line.  So the actual make command that worked for me was:

sudo make clean all distinstall OPT="-dFPC_ARMHF -CX -CpARMV6 
-CfVFPV2 -CaEABIHF -OpARMV6 -O2 -OoFASTMATH -XX -Xs" 
OVERRIDEVERSIONCHECK=1 FPC=/home/pi/FreePascal/thaddyppcarm

Incidentally, the other options on this command line enable the additional processor support I mentioned earlier.  Most notably ‘FPC_ARMHF’ enables hardware floating point which will make a huge difference to the performance of computationally intensive programs.

To install the new compiler to its usual location I then use the same sequence of commands as given in the original scripts.  So copying the compiler and recreating it’s global symlinks requires:

sudo make install PP=compiler/ppcarm PREFIX=/usr
sudo rm -f /usr/bin/ppcarm
sudo ln -s /usr/lib/fpc/2.7.1/ppcarm /usr/bin/ppcarm

After which you will need to do the same for the compiler source:

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

Lastly the fpc configuration file needs to be created with:

sudo /usr/lib/fpc/2.7.1/samplecfg /usr/lib/fpc/2.7.1 /etc

After completing all of this you should have a fully installed FreePascal Arm 6 compiler with hard float support.  The screen shot below shows this compiler being used to build a test X11 program which ran successfully on the Raspberry Pi over an SSH connection from my laptop:

FreePascalCommandLine

I have also updated the original build script to automate this process of rebuilding the compiler from the main subversion repository and it is available for download here.  At the moment, I do not need Lazarus on the Raspberry Pi, but it should be possible build it following my original instructions using this compiler instead.  One further thing to note; if you have not installed the fpc 2.6.0 from the repository, you will not have some of the required developer packages installed and will therefore have to install these manually.  For example the X11 test program cannot be linked without installing the ‘xorg-dev’ package with:

sudo apt-get install xorg-dev

 

Return to the Raspberry Pi!

Since I published my last post on the fencing score box system I have been contacted by a hardware manufacturer who is interested in embedding a Raspberry Pi in their new range of equipment.  As a result, this has put the Pi firmly back on my agenda!  I will return to the build process for the FreePascal compiler in another article, but before then I want to cover the method I am now using to connect to the Pi as I think it is far more convenient for a developer, than having the Pi set-up with a dedicated screen, keyboard and mouse.

The basic idea is to create a local subnet via a direct cable connection to the Pi and then interact with the Pi via SSH using the laptops own peripherals.  There are quite a number of ways of doing this, but I will describe the process I use as I think it is the simplest to set-up (at least from an Kubuntu/Ubuntu point of view).  To start with the Pi needs to be attached to a power source.  This can be a micro-USB mains charger, or a USB to micro-USB cable to draw the power off the laptop.  If you choose the second method, be sure your laptop is capable of reliably supplying the additional current required.  I prefer to power the Pi independently, to avoid any issue with straining my already hot laptop PSU.

Next the Pi is connect to the laptop via a standard Ethernet cable.  A cross-over cable is not required as the Pi is smart enough to detect this type of connection and reconfigure itself accordingly.  Once plugged in, you will need to know the IP address allocated to the Pi to be able to communicate.  This is where the confusion begins.  There seem to be four approaches to doing this.  Two require editing configuration files on the PI itself.  As with any editing of configuration files, it is always a wise precaution to make a backup copy of the file before making changes!  To edit configuration files on the Pi, I plugged the Raspian SD card directly into my laptop and edited it there before re-inserting it into the Pi.

The first way to allocate an IP address is to edit the ‘/etc/network/interfaces’ file on the Pi to define a static address for the eth0 interface.  This is the first method I tried, but I could not get this to work.  I suspect there are other start-up processes on the Pi that override this Linux configuration method.

After a bit more googling I found the second method.  It is also possible to pass a boot command line argument to the Pi that allocates an IP address.  This is achieved by editing the ‘cmdline.txt’ file in the boot partition of the Pi by adding ‘ip=169.254.0.1’, for example, to the end of this file.  Care must be taken not to insert a return as this must be all on one line.  This enabled a basic SSH connection to the Pi with the command ‘ssh pi@169.254.0.1’. However, the Pi was isolated from the wider network by the host laptop, so for example: ‘sudo apt-get update’ would not connect to the repositories.

The last two solutions solve this problem.  One is to set-up the laptop as a full-blown DHCP server.  This is certainly possible, but I feel it is overkill for what I wanted to achieve.  The last solution is to use internet connection sharing on the laptop, to allow network traffic to be passed through the laptop’s network connections.  My laptop uses an internal WIFI card to connect to our home ADSL rooter, or at work, the company’s wireless network.  This leaves the Ethernet port free to connect to the Pi. Internet connection sharing, permits traffic to be passed from one interface to the other.  In Kubuntu, and I believe Ubuntu also, this can quickly be set through the network connection editor.

ConnectionPropertiesThe default mode for cable connections is ‘Automatic’ on the ‘IPv4’ tab.  Changing this to ‘Shared to other computers’ sets up the interface for internet connection sharing.  I also set the option to ignore ‘IPv6’ connections as I did find an article which suggests this can cause problems with a Pi, although I don’t know if that is really required.  I also switched off automatic connection as I want to be able to manually choose between this and a standard DCHP connection.

ConnectionEditor

I also discovered that once internet sharing was enabled, it was not necessary to set the boot parameter IP address as one was allocated anyway.  In my case it was ‘10.42.0.44’. This can be discovered with the arp command.  Knowing this, I then edited the ‘/etc/hosts’ file on my laptop to look like this:

127.0.0.1 localhost
127.0.1.1 laptop01
10.42.0.44 rpi

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

This enabled the SSH command to connect using the name ‘rpi’ rather than having to remember the IP address.  Its worth pointing out two useful features of SSH for those that are new to it.  The first is the ‘-X’ parameter.  This enables remote X Windows applications to be started over SSH.  The second is that modern Linux desktops can use it as a networking protocol to provide remote folders in their visual file managers.

Desktop

In this screenshot I have started the Pi’s Midori browser via an X Windows link and opened a Dolphin file browser looking at the Pi’s FreePascal folder.  So for Linux the full SSH command line for the default pi user looks like this:

ssh -X pi@rpi

Its worth mentioning for Windows users, that they can also use SSH by installing a utility called Putty.  I have not used this myself, but I have read several articles where this has been successfully used to achieve the same working environment as this.  At some point, I will also investigate using RSA keys which may be combined with SSH to avoid having to use passwords every time you connect.