Compiling custom cross compiler for FPC

Using fpcupdeluxe with self-compiled binutils


The problem

fpcupdeluxe makes it very easy to build fpc from source and add cross compilers to it. It has a very rich library of precompiled cross-platform tools required to compile cross compiling fpc for target platform. However, it's not immediately obvious what to do when such tools are not available for your source/target system configuration.

The solution

This very quick guide is a recreation of steps which allowed me to build a x86_64-linux cross compiler for FPC on x86_64-freebsd.

Step 1: binutils sources

First, download sources for GNU binutils from https://www.gnu.org/software/binutils/

Step 2: configuring binutils for target platform

Inside main directory of binutils, run the configure script. This wiki page is a good starting point for its parameters. I used this command:

./configure \
--target=x86_64-linux \
--prefix=/usr/local/lib/x86_64-linux \
--bindir=/usr/local/bin --mandir=/usr/local/share/man --infodir=/usr/local/share/info \
--program-prefix=x86_64-linux- \
--disable-werror \
--enable-gprofng=no

This will install binutils in /usr/local/bin with prefix x86_64-linux-. I disabled gprofng because it required extra bison dependency, and is not required for cross compiling.

Step 3: build and install

Run make and make install (as root if you're installing outside home directory).

Note that make must be GNU make. On FreeBSD it will most likely be named gmake.

Step 4: make sure fpcupdeluxe can find the crosstools

In my case, fpcupdeluxe wasn't eager to use what I installed inside /usr/local/bin. So instead, I copied all the tools into ~/fpcupdeluxe/cross/bin/x86_64-linux:

mkdir -p ~/fpcupdeluxe/cross/bin/x86_64-linux/
cp /usr/local/bin/x86_64-linux-* ~/fpcupdeluxe/cross/bin/x86_64-linux/

And then removed the prefix from the programs by running this Perl script:

use strict;
use warnings;
use File::Copy qw(mv);

my $prefix = 'x86_64-linux-';
foreach (glob "${prefix}*") {
        my $old = $_;
        s/^$prefix//;
        print "renaming $old to $_\n";
        mv $old, $_;
}

This directory is the default place where fpcupdeluxe searches for crosstools in my installation, but it may be a different one in your case.

Step 5: obtain crosslibs

In addition to crosstools, you also need libraries valid for the target system. You can take them from Linux system lib directories, or just get them from releases conveniently provided by fpcupdeluxe, like the ones from this release.

Crosstools provided by fpcupdeluxe already have proper paths in the archive, so just extract it from base fpcupdeluxe directory.

Step 6: compile the compiler!

Now it should be possible to compile the cross compiler. Open fpcupdeluxe, select the target arch/system and start compiling. If you did the previous steps right, chances are you won't encounter any problems at this stage.

Step 7: compile the program!

With the cross compiler ready, you should be able to compile your program. In my case, I had to include crosslibs in Lazarus in order for FPC to link properly.

That should be it!

The procedure is really easy, but it may be tricky to know exactly what is required and where it should be put. I hope this guide makes it a little easier to understand and execute.


Comments? Suggestions? Send to feedback@bbrtj.eu
Published on 2023-08-13