Instructions for building the Free Pascal Compiler for the RISC-V

Creating a Free Pascal Compiler/Cross-Compiler for RISC-V

edit

About the Document

edit

These instructions describe the process of creating a Free Pascal cross-compiler and compiler on ubuntu 22, 24 and 26. The cross-compiler itself runs on x86_64, but produces executables for RISC-V Linux.

Installing Dependencies

edit
sudo apt update
sudo apt install fpc binutils-riscv64-linux-gnu

Cloning the Source Code

edit
git clone --depth 1 https://gitlab.com/freepascal.org/fpc/source.git fpc
cd fpc

Building the Compiler

edit
make all FPC=/usr/bin/fpc CPU_TARGET=riscv64 OS_TARGET=linux CROSSPREFIX=riscv64-linux-gnu- BINUTILSPREFIX=riscv64-linux-gnu- CROSSOPT="-XPriscv64-linux-gnu-" INSTALL_PREFIX=/opt/native-fpc-riscv -j$(nproc)

To build only the cross compiler, use crossall instead of all.

Installation

edit
make install FPC=/usr/bin/fpc CPU_TARGET=riscv64 OS_TARGET=linux CROSSPREFIX=riscv64-linux-gnu- BINUTILSPREFIX=riscv64-linux-gnu- CROSSOPT="-XPriscv64-linux-gnu-" INSTALL_PREFIX=/opt/native-fpc-riscv -j$(nproc)

To install a cross-compiler you need to write crossinstall instead of install

Creating a Configuration File

edit

Finding a Compiled Version of FPC

ls /opt/native-fpc-riscv/lib/fpc/

Creating a Configuration File (replace X.X.X with the version from the previous step)

/opt/native-fpc-riscv/lib/fpc/X.X.X/samplecfg /opt/native-fpc-riscv/lib/fpc/X.X.X /path/to/fpc.cfg

This script will create the file fpc.cfg at /path/to/fpc.cfg (fpc.cfg should not be included in the path).

Change the line 227,-XP$FPCTARGET- in the resulting fpc.cfg to -XPriscv64-linux-gnu- (which is the prefix binutils should have)

Finally, transfer the entire native-fpc-riscv directory and configuration file to the RISC-V system.

To ensure that the compiled compiler uses this configuration file, run it with the following parameters: -n @/path/to/fpc.cfg (the -n parameter tells the compiler not to look for the configuration file in the standard paths).

Build Parameter Variables

edit
  • FPC - Path to fpc (in our case from apt)
  • CPU_TARGET - Target architecture (riscv64)
  • OS_TARGET - Target OS (Linux)
  • CROSSPREFIX - Prefix for the cross-assembler and linker
  • BINUTILSPREFIX - Prefix for binutils (which are obtained from apt)
  • CROSSOPT - Parameters for the target FPC
  • -XP... - Prefix for external utilities during target FPC execution
  • INSTALL_PREFIX - Installation path for the compiled compiler
  • OPT - Compiler parameters during build
  • -j$(nproc) - Multithreaded build

Cleanup (if rebuild is necessary)

edit
make distclean CPU_TARGET=riscv64 OS_TARGET=linux