zipplet.co.uk

Electronics hobbyist and software developer

User Tools

Site Tools


fpc:building_for_pi

How to build the freepascal installer for Raspberry Pi

(part of my Freepascal for Raspberry Pi category)

Notes

  • To build freepascal, you need freepascal - “chicken and egg” - I'll cover how below
  • To compile a specific version you should use a compiler version as close to it as possible - for example to compile 3.0.2 you should use compiler version 3.0.0 to build it.
  • An exception is when a major version change occurs (e.g. 2.x.x → 3.x.x), in which case you use the latest version from the previous major version to compile the earliest version of the next major version.
  • Skipping multiple versions (2.4.x → 3.0.2) is not supported, even though it might work.
  • Ideally, look at my official releases on my freepascal releases page here and start with that, and compile the next minor release.
  • If you need a version much newer, get there in steps (e.g. 3.0.0 → 3.1.0 → 3.2.0….)
  • See the freepascal wiki (warning: it can be difficult to find information about compiling - you need to read a lot of pages) for more information.
  • This page will be maintained as a log of sorts, I'll append to the bottom whenever I build a new version detailing how I did it from the previous version I released. In theory, following this page from start to finish will give you the latest release I have published on my unofficial releases page.
  • After each package build (successfully creating a new freepascal installer and confirming it works), you should ideally clean up the machine. install that version and then proceed from that point when building a new version of freepascal. I use a fresh SD card image each time for purity.

Compiler options

Later on, you'll see a command like this used:

export COMPILER_OPTIONS="-dFPC_ARMHF -CpARMV6"

-dFPC_ARMHF enables support for ARM hardware floating point.

-CpARMV6 ensures we compile for ARMv6 CPUs (compatible back to Raspberry Pi 1).

http://wiki.lazarus.freepascal.org/ARM_compiler_options

Hint to avoid broken installer archives

Starting with v3.0.4, my build process is this:

  • Build the next version using the current version
  • Install the next version
  • Delete the archives, and rebuild the next version using the next version
  • Distribute the resulting archive

This seems to fix a few problems, like “lone zero blocks” in the tar archive. While freepascal does a “make cycle” to try to ensure consistency when building the new RTL and compiler, performing the additional step of installing the built compiler and using it to build the entire thing again seems beneficial.

Prerequisites

  • A Raspberry Pi with 512MB or more of RAM (any model, I compile my builds on a Pi 1 B+ successfully)
  • 2GB of free space on the SD card
  • Raspbian Jessie lite or Raspbian Stretch lite operating system (might work on others, untested!)
  • For 512MB models, the GPU split should be set to 16MB
  • No X server / GUI running (takes too much RAM). Be logged in via SSH or use a keyboard + local console.
  • Working fpc compiler to act as a bootstrap compiler (freepascal is compiled with freepascal)

Building FPC 3.0.2 from "nothing"

Getting a bootstrap compiler to build 3.0.2

A bootstrap compiler is required to build any version of freepascal.

For Raspbian Stretch

The official package (version 3.0.0) seems to be unable to compile the compiler, it will fail with:

/usr/bin/ppcarm -Ur -Ur -Xs -O2 -n -Fi../inc -Fi../arm -Fi../unix -Fiarm -FE. -FU/home/pi/fpc/fpcbuild/fpcsrc/rtl/units/arm-linux -darm -dRELEASE -Us -Sg system.pp
arm.inc(18,2) Error: Illegal assembler style specified "GAS"

This problem seems to be fixed in trunk as of writing (2017-11-19) but has not yet made it into an official release tag. It's possible that removing the following line from fpcbuild/fpcsrc/rtl/arm/arm.inc will fix it, but I have not tried:

{$asmmode gas}

As such I instead recommend using my pitools unofficial 3.0.0 installer which does work. To do this first install git if not installed:

sudo apt-get update
sudo apt-get install git

Then checkout pitools:

[email protected]:~ $ cd ~
[email protected]:~ $ git clone https://github.com/zipplet/pitools.git

Now run the freepascal 3.0.0 builder:

[email protected]:~ $ cd pitools/fpc_3_builder/
[email protected]:~/pitools/fpc_3_builder $ sudo ./install.sh

Output should look like this:

------------------------------------
fpc_3_builder version 0.1 (20161120)
Copyright (c) Michael Nixon 2016.
------------------------------------
Determining Pi model...
Board revision is a02082

Your Pi: 2016 Q1 - PCB v1.2 - Pi 3 Model B [1GB] (Manufactured by Sony)
 - Model: 3
 - Sub model: B
 - RAM: 1024MB

This Raspberry Pi can compile freepascal.


Before proceeding, please confirm the following:
1) This script assumes you do not already have freepascal installed, although if you do things should still work correctly.
2) FPC 3.0.0 is not officially available for the Pi yet. Keep this in mind.
3) If you currently use lazarus, STOP NOW. This script does not upgrade lazarus, so it will break!
4) You are running in console mode, and have at least 400MB of RAM free or this will fail.
5) For a Raspberry Pi 1 or Raspberry Pi Zero, please set the GPU split to 16MB first.
6) This can take up to an hour especially on a Pi 1, and will stress your Raspberry Pi. An overclocked Pi may crash!
7) If compilation fails partway through, you may end up with an unusable freepascal compiler. Take a backup of your SD card.


Have you read, confirmed and do you understand all of the above? (y/n) :y

Preparing bootstrap compiler...
Installing bootstrap compiler - this will take a while...
Installing compiler and RTL for arm-linux...
Installing utilities...
Installing fcl-async
Installing fcl-base
Installing fcl-db
Installing fcl-extra
Installing fcl-fpcunit
Installing fcl-image
Installing fcl-js
Installing fcl-json
Installing fcl-net
Installing fcl-passrc
Installing fcl-process
Installing fcl-registry
Installing fcl-res
Installing fcl-web
Installing fcl-xml
Installing a52
Installing aspell
Installing bfd
Installing bzip2
Installing cairo
Installing cdrom
Installing chm
Installing dblib
Installing dbus
Installing dts
Installing fastcgi
Installing fftw
Installing fpgtk
Installing fpindexer
Installing fpmkunit
Installing fppkg
Installing fv
Installing gdbint
Installing gdbm
Installing ggi
Installing gmp
Installing gnome1
Installing graph
Installing gtk1
Installing gtk2
Installing hash
Installing hermes
Installing httpd22
Installing httpd24
Installing ibase
Installing iconvenc
Installing imagemagick
Installing imlib
Installing ldap
Installing libcurl
Installing libgd
Installing libpng
Installing libsee
Installing libvlc
Installing libxml2
Installing lua
Installing mad
Installing modplug
Installing mysql
Installing ncurses
Installing newt
Installing numlib
Installing odbc
Installing oggvorbis
Installing openal
Installing opencl
Installing opengl
Installing openssl
Installing oracle
Installing pasjpeg
Installing paszlib
Installing pcap
Installing postgres
Installing proj4
Installing ptc
Installing pthreads
Installing pxlib
Installing regexpr
Installing rsvg
Installing sdl
Installing sqlite
Installing svgalib
Installing symbolic
Installing syslog
Installing tcl
Installing unzip
Installing users
Installing utmp
Installing uuid
Installing x11
Installing xforms
Installing zlib
Installing zorba
Done.

Installing documentation in /usr/local/share/doc/fpc-2.6.4 ...
Done.

Installing demos in /usr/local/share/doc/fpc-2.6.4/examples ...
Done.

Running on linux
Write permission in /etc.
Writing sample configuration file to /etc/fpc.cfg
Writing sample configuration file to /usr/local/lib/fpc/2.6.4/ide/text/fp.cfg
Writing sample configuration file to /usr/local/lib/fpc/2.6.4/ide/text/fp.ini
Writing sample configuration file to /etc/fppkg.cfg
Writing sample configuration file to /etc/fppkg/default
Success - testing the bootstrap compiler...
Success.

Unpacking the source code for freepascal 3.0.0 - this will take a while...
Setting compiler options for Raspberry Pi 3
This may take a significant amount of time (an hour or so), please be patient.
Building the compiler and RTL (log output stored in /usr/local/fpc/buildlog.log)...
Installing the compiler and RTL (log output stored in /usr/local/fpc/buildlog.log)...
Installing the sources (log output stored in /usr/local/fpc/buildlog.log)...
Setting up the new compiler...
lrwxrwxrwx 1 root root 31 Nov 19 15:09 /usr/local/bin/ppcarm -> /usr/local/lib/fpc/3.0.0/ppcarm
Testing the new compiler...
Success.

Marking this tool as installed...
All done.

For Raspbian Jessie

At the moment, follow the same instructions as for Raspbian Stretch (using pitools to get a working 3.0.0 compiler).

Testing the bootstrap compiler

Run the compiler without an argument to make sure it can be found first:

[email protected]:~ $ fpc
Free Pascal Compiler version 3.0.0 [2017/11/19] for arm
Copyright (c) 1993-2015 by Florian Klaempfl and others

Compile the following test program (save as helloworld.dpr):

program helloworld;

begin
  writeln('Hello, world');
end.

Compile it:

[email protected]:~ $ fpc helloworld.dpr
Free Pascal Compiler version 3.0.0 [2017/11/19] for arm
Copyright (c) 1993-2015 by Florian Klaempfl and others
Target OS: Linux for ARMHF
Compiling helloworld.dpr
Assembling helloworld
Linking helloworld
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
6 lines compiled, 0.6 sec

Test it:

[email protected]:~ $ ./helloworld
Hello, world

Great. Time to proceed.

Setup

Install some packages (subversion to checkout code):

sudo apt-get update
sudo apt-get install subversion

Select a version of FPC, by looking at the list of builds here and comparing to the current official release in order to get the name of the tag you'd like to checkout and build.

In this case, we are interested in: https://svn.freepascal.org/svn/fpcbuild/tags/release_3_0_2/

Obtain the sources from the fpcbuild tree:

[email protected]:~ $ mkdir fpc
[email protected]:~ $ cd fpc
[email protected]:~/fpc $ svn export https://svn.freepascal.org/svn/fpcbuild/tags/release_3_0_2/ fpcbuild

It will take a while. To give you an idea, here's the size of the checkout once finished for me for v3.0.2:

[email protected]:~/fpc $ du -h --max-depth=1
403M    ./fpcbuild
403M    .

Add the documents. Visit the freepascal FTP site here, drill down to the version of interest and then into the docs directory and look for a file called doc-pdf.tar.gz - make note of the URL for the next step.

In this case we are interested in: ftp://ftp.freepascal.org/pub/fpc/dist/3.0.2/docs/doc-pdf.tar.gz

[email protected]:~/fpc $ cd fpcbuild
[email protected]:~/fpc/fpcbuild $ wget ftp://ftp.freepascal.org/pub/fpc/dist/3.0.2/docs/doc-pdf.tar.gz

Disabling GDB (IDE debugging)

Compiling with GDB support in the freepascal IDE requires obtaining some libgdb assembly files. I couldn't find the correct files for the Raspberry Pi build. As the debugging function is not essential, we can disable it to get a successful build.

If you know how to do this with the debugger function intact, please contact me so I can update this guide.

Edit the makepack script:

[email protected]:~/fpc/fpcbuild $ vim install/makepack

Under MAKEDOCS=no add CHECKLIBGDB=no, it should look something like this at the top:

#!/usr/bin/env bash
#
# Shell script to make a FPC .tar package for Linux
# Copyright 1996-2004 Michael Van Canneyt and Peter Vreman
#

set -e

# Set this to "yes" if you want to force making the documentation.
# if it is not equal to yes, the documentation is assumed present in a file doc-pdf.tar.gz
MAKEDOCS=no
CHECKLIBGDB=no

Building the installer for 3.0.2

Make sure you are in the correct directory:

[email protected]:~/fpc/fpcbuild $ pwd
/home/pi/fpc/fpcbuild

Become fakeroot, then set compiler options to enable ARM hardfloat support and build the package:

[email protected]:~/fpc/fpcbuild $ fakeroot
[email protected]:~/fpc/fpcbuild # export COMPILER_OPTIONS="-dFPC_ARMHF -CpARMV6"
[email protected]:~/fpc/fpcbuild # install/makepack

After a while, the last few lines of output should look like this:

Copying install.sh
Creating binary.arm-linux.tar
Creating fpc-3.0.2.arm-linux.tar

Drop out of fakeroot:

[email protected]:~/fpc/fpcbuild # exit
exit

The installer package is fpc-3.0.2.arm-linux.tar - this is the package you can distribute.

Installing freepascal 3.0.2 from the installer you just built

[email protected]:~/fpc/fpcbuild $ cd ~
[email protected]:~ $ tar -xvf fpc/fpcbuild/fpc-3.0.2.arm-linux.tar
fpc-3.0.2.arm-linux/
fpc-3.0.2.arm-linux/binary.arm-linux.tar
fpc-3.0.2.arm-linux/install.sh
fpc-3.0.2.arm-linux/doc-pdf.tar.gz
fpc-3.0.2.arm-linux/demo.tar.gz
[email protected]:~ $ cd fpc-3.0.2.arm-linux
[email protected]:~/fpc-3.0.2.arm-linux $ sudo ./install.sh

Install to /usr/local when asked rather than /usr and enter “y” when asked to install packages, sources, etc.

You might see lots of complaints about the date in the archive being in the future if you only just built the installer, it's fine.

Testing freepascal 3.0.2

Run it and the output should look similar:

[email protected]:~ $ fpc
Free Pascal Compiler version 3.0.2 [2017/11/19] for arm
Copyright (c) 1993-2017 by Florian Klaempfl and others

Ideally create the same hello world sample as earlier (save as helloworld.dpr):

program helloworld;

begin
  writeln('Hello, world');
end.

Compile and run it:

[email protected]:~ $ fpc helloworld.dpr
Free Pascal Compiler version 3.0.2 [2017/11/19] for arm
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for ARMHF
Compiling helloworld.dpr
Assembling helloworld
Linking helloworld
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
6 lines compiled, 0.6 sec
[email protected]:~ $ ./helloworld
Hello, world

This means your freepascal 3.0.2 installer package is good. Save it - next time you build freepascal, you start with this installer.

Can this build of 3.0.2 compile 3.0.2?

Yes it can (edit install/makepack to disable GDB again). I tested this to confirm that the build is good.

Building FPC 3.0.4 RC1 using 3.0.2

URLs for obtaining the source:

Install the 3.0.2 compiler built previously.

Disable GDB support like previously by editing install/makepack and adding CHECKLIBGDB=no just under MAKEDOCS=no.

Now use fakeroot and build the installer:

[email protected]:~/fpc/fpcbuild $ fakeroot
[email protected]:~/fpc/fpcbuild # export COMPILER_OPTIONS="-dFPC_ARMHF -CpARMV6"
[email protected]:~/fpc/fpcbuild # install/makepack

Building FPC 3.0.4 using 3.0.4 RC1

URLs for obtaining the source:

Install the 3.0.4rc1 compiler built previously.

Disable GDB support like previously by editing install/makepack and adding CHECKLIBGDB=no just under MAKEDOCS=no.

Now use fakeroot and build the installer:

[email protected]:~/fpc/fpcbuild $ fakeroot
ro[email protected]:~/fpc/fpcbuild # export COMPILER_OPTIONS="-dFPC_ARMHF -CpARMV6"
[email protected]:~/fpc/fpcbuild # install/makepack

Note: The installer built for 3.0.4 doesn't seem to suffer the “tar: lone zero block” error or “date is in the future!” error anymore. I guess something was fixed here? :)

Help me maintain these builds!

If this helped you out, I'm happy it did. That's enough satisfaction for me. However if you'd like to donate towards my projects, website hosting fees and continuing this work, please see my donations page for instructions. Thank you.

fpc/building_for_pi.txt · Last modified: 2017/12/10 05:58 UTC by Michael Nixon