Creating a Free Pascal Compiler/Cross-Compiler for RISC-V
editAbout the Document
editThese 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
editsudo apt update
sudo apt install fpc binutils-riscv64-linux-gnu
Cloning the Source Code
editgit clone --depth 1 https://gitlab.com/freepascal.org/fpc/source.git fpc
cd fpc
Building the Compiler
editmake 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
editmake 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
editFinding 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
editFPC- Path tofpc(in our case fromapt)CPU_TARGET- Target architecture (riscv64)OS_TARGET- Target OS (Linux)CROSSPREFIX- Prefix for the cross-assembler and linkerBINUTILSPREFIX- Prefix for binutils (which are obtained fromapt)CROSSOPT- Parameters for the target FPC-XP...- Prefix for external utilities during target FPC executionINSTALL_PREFIX- Installation path for the compiled compilerOPT- Compiler parameters during build-j$(nproc)- Multithreaded build
Cleanup (if rebuild is necessary)
editmake distclean CPU_TARGET=riscv64 OS_TARGET=linux