Sign in Join
Wiki copytemporary

Building a Pico Compiler: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11# Building a Pico Compiler
22
33One of FPC's great features is its ability to provide cross compilers. A cross compiler allows you to run on one system and make a binary for another, possibly completely different system. When making binaries for embedded targets, its the only way to go !
44
55### fpcupdeluxe
66
77For most users, the most convenient way to get a cross compiler for the Raspberry Pi Pico is to use [fpcupdeluxe](https://github.com/LongDirtyAnimAlf/fpcupdeluxe) to do it all. It will install just FPC or FPC and Lazarus for you. Note it installs the 'main' version of each, from time to time, one or the other may have bugs (thats the nature of development branches) but not often. Try again in a day or so if you have problems.
88
99But this page is really for those who want to build their own, or just understand the process. Maybe adapt it to another CPU. These instructions focus on using a 64bit Linux system as the host, Windows and perhaps Mac users can probably do something similar.
1010
1111Much more details at [[ARM Embedded Tutorial - Installing Lazarus and Free Pascal]]
1212
1313> **Note:** and this is important, this recipe produces a different file layout than fpupdeluxe. That is for simplicity. Do not mix this with a fpcupdeluxe or results may be unpredictable.
1414
1515### The constraints
1616
1717- If you are a Lazarus user, it makes sense to have a stand alone Lazarus install for your Pico work, you could switch a single install (by changing Tools-\>Options-\>Files-\>Compiler_executable) but it could get annoying. Multiple installs means installing Lazarus from source, remarkably easy.
1818- If you already have a Source install of FPC main December 2023 or later, you can use that, with some care. Again, might be easier and safer to have a dedicated version. You do need an existing install of FPC =\> 3.2.2, if yours is installed in read only space (installed from a deb or rpm) then I suggest you work here with with a fresh copy of FPC source and "install" it into your own space.
1919
2020### Dependencies
2121
2222- An existing, working, FPC322 or later.
2323- Install binutils-arm-none-eabi - that means ones for arm, no operating system (or embedded) and eabi indicates the arm subtype, no 'hf' means 'soft float'.
2424- And, of course, you need Michael's git repo with pico-fpcexamples. These come from <https://github.com/michael-ring/pico-fpcexamples> Note that Michael's repo contain two separate things, some nicely set example projects and, in 'units' a number of libraries (*.obj) and pascal units to link them into a pascal project.
2525
2626### The Process
2727
2828Here are a series of command lines, I do not recommend you run them as a script (although I have) because you MUST check for errors at each step. I keep this in my home directory in a structure like this, change it of course but change the commands if you do ! The commands here will create two fpc-3.3.1 directories, if you have them already, ensure they are empty or change the commands. Note that I have already downloaded fpc.zip to my Downloads dir.
2929
3030### Assumed Dir Structure
3131
32-```
32+```text
3333 $HOME/bin/FPC
3434 fpc-3.2.2 .....
3535 fpc-3.3.1
3636 SRC
3737 fpc-3.2.2 ....
3838 fpc-3.3.1
3939```
4040
4141##### The commands
4242
43-```
43+```text
4444 #!/bin/bash
4545 export FPC_VER="fpc-3.3.1" # note 1
4646 export SRC_DIR="$HOME/testFPC/FPC/SRC/$FPC_VER"
4747 export FPC_DIR="$HOME/testFPC/FPC/$FPC_VER"
4848 UNITDIR="$FPC_DIR"/units/arm-embedded/armv6m
4949
5050 mkdir -p "$FPC_DIR"
5151 mkdir -p "$SRC_DIR"
5252 cd "$SRC_DIR"
5353 unzip ~/Downloads/fpc.zip # note 2
5454 cd fpc
5555 make all # note 3
5656 make install INSTALL_PREFIX="$FPC_DIR"
5757
5858 # note 4
5959 make crossinstall CPU_TARGET=arm OS_TARGET=embedded SUBARCH=armv6m CROSSBINDIR=/usr/bin BINUTILSPREFIX=arm-none-eabi- CROSSOPT="-CpARMV6M -CaEABI" INSTALL_PREFIX="$FPC_DIR" INSTALL_UNITDIR="$UNITDIR"
6060
6161 cp compiler/ppcx64 "$FPC_DIR"/bin/ppcx64 # note 5
6262 cp compiler/ppcrossarm "$FPC_DIR"/bin/ppcrossarm # note 6
6363
6464 cp rtl/embedded/arm/* "$UNITDIR" # note 7
6565
6666 cd "$FPC_DIR"
6767 mkdir -p etc
6868 bin/fpcmkcfg -d basepath="$FPC_DIR" -o etc/fpc.cfg # note 8
6969
7070 cd bin
7171 echo "#!/bin/sh" > fpc.bash # note 9
7272 echo "# A script that ensure when we start fpc here, it ignores all other fpc.cfg" >> fpc.bash
7373 echo "$FPC_DIR""/bin/fpc -n @$FPC_DIR/etc/fpc.cfg \"\$@\"" >> fpc.bash
7474 chmod u+x fpc.bash
7575```
7676
7777- note 1 - right now (March 2024) fpc main thinks of it self as 3.3.1, this may change after the next release of FPC and its possible the necessary updates to make this all work will not be in that release.
7878
7979- note 2 - make sure you have that fpc.zip file there, FPC main Dec 2024 or later. cd ~/Downloads; wget <https://gitlab.com/freepascal.org/fpc/source/-/archive/main/source-main.zip>
8080
8181- note 3 - this takes a while.
8282
8383- note 4 - this takes a while too. And note it places the RTL files in a different place than fpcupdeluxe.
8484
8585- note 5 - this is your host system compiler.
8686
8787- note 6 - and this is the cross compiler, we call fpc and it calls this one.
8888
8989- note 7 - these are some units that the RTL, built earlier.
9090
9191- note 8 - this creates a pretty stock standard fpc.cfg file.
9292
9393- note 9 - this makes a script that calls fpc and passes parameters that ensure fpc takes no regard of any other fpc.cfg file, only the one we just made. The fpc.cfg that controls here is the one in note 8.
9494
9595> **Note:** Whats different from fpcupdeluxe ? This model puts the Run Time Libraries in \$FPC_DIR/units/arm-embedded/armv6m whereas fpcupdeluxe puts them in a more correct \$FPC_DIR/units/arm-embedded/armv6m/eabi/rtl. The problem with the fpcupdeluxe model is that fpc does not look there by default, a patch to fpc.cfg is needed. As this is intended as a simple demo, I decided to keep it as simple as possible. If you prefer, change the UNITDIR variable to "\$FPC_DIR"/units/arm-embedded/armv6m/eabi/rtl and patch your fpc.cfg to include in its search path.
9696
9797### Setup a Lazarus project
9898
9999Doing your Pico development in Lazarus does make things simpler once setup. You can use (some of) Lazarus's help and code tools systems, click compile (but obviously not run) and have all your errors nicely presented. You do not need to make any changes to Lazarus (apart from settings mentioned below), just about any recent Lazarus will do. But, as mentioned above, makes sense to dedicate an install to Pico because of an unusual setting, next papa.
100100
101101Start a new Lazarus project in the usual way, to keep life simple, save it in a directory under 'projects' in Michael's directory tree. Firstly, a one off for this Lazarus instance, note this is NOT saved with the project. Open Options -\> Files and look for the "Compiler executable" entry. Put in, with a full path, 'fpc.bash'. eg /home/YOUR-USER-NAME/bin/FPC/fpc-3.3.1/bin/fpc.bash (note fpcupdeluxe uses fpc.sh)
102102
103103Then, open the Project Options and set the following -
104104
105105Paths Other unit files (-Fu): ../pico-fpcexamples/units Include files (-Fi) : \$(ProjOutDir) Libraries (-Fl); Unit output directory (-FU): lib/\$(TargetCPU)-\$(TargetOS) Target file name (-o) : bin/project1 \[x\] Apply conventions
106106
107107Config and Target \[x\] Use standard compiler config... \[ \] Use additional Target OS (-T) : Embedded Target CPU family (-P) : arm Target processor (-Cp) : ARMV6M
108108
109109Custom Options -Wpraspi_pico -godwarfsets -godwarfcpp -Xu
110110
111-```
111+```text
112112 -XParm-none-eabi-
113113```
114114
115115Note the last of the Custom Options, when you installed binutils-arm-none-eabi, you added a set of binutils for this particular processor. Some systems may call them arm-embedded-??? if yours uses that name scheme, change that Option accordingly.
116116
117117### Compile from Commandline
118118
119119Its also reasonable to build from the command line, here is an example -
120120
121-```
121+```text
122122 #!/bin/bash
123123 echo "------- Using the fpc from 3.3.1 ---------"
124124 export MICHAELS_UNITS="$HOME"/Pico2/projects/pico-fpcexamples/units
125125 "$HOME"/bin/FPC/fpc-3.3.1/bin/fpc.bash -va -Xu -XParm-none-eabi- -Tembedded -Parm -CpARMV6M -Fu"$MICHAELS_UNITS" -Wpraspi_pico project1
126126```
127127
128128Note: -XParm-none-eabi- indicates how the binutils are named. If yours are named differently, change it !
129129
130130### See also
131131
132132- [[ARM Embedded Tutorial - FPC and the Raspberry Pi Pico|ARM Embedded Raspberry Pi Pico Tutorials]]
133133
134134---
135135*From the Free Pascal wiki, page "Building a Pico Compiler", as it stood on 2024-03-10 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
136136Category: ARM
137137Category: Embedded
138138Category: Microcontrollers
139139Category: Operating Systems and Platforms
140140Category: Tutorials

Back to history