Sign in Join
Wiki copytemporary

Building a Pico Compiler: what changed

Revision 1 to 2. 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-`   $HOME/bin/FPC`\
33-`       fpc-3.2.2  .....`\
34-`       fpc-3.3.1`\
35-`       SRC`\
36-`           fpc-3.2.2 ....`\
37-`           fpc-3.3.1`
32+```
33+ $HOME/bin/FPC
34+ fpc-3.2.2 .....
35+ fpc-3.3.1
36+ SRC
37+ fpc-3.2.2 ....
38+ fpc-3.3.1
39+```
3840
3941##### The commands
4042
41-`   #!/bin/bash`\
42-`   export FPC_VER="fpc-3.3.1"        # note 1`\
43-`   export SRC_DIR="$HOME/testFPC/FPC/SRC/$FPC_VER"`\
44-`   export FPC_DIR="$HOME/testFPC/FPC/$FPC_VER"`\
45-`   UNITDIR="$FPC_DIR"/units/arm-embedded/armv6m`\
46-`   `\
47-`   mkdir -p "$FPC_DIR"`\
48-`   mkdir -p "$SRC_DIR"`\
49-`   cd "$SRC_DIR"`\
50-`   unzip ~/Downloads/fpc.zip         # note 2`\
51-`   cd fpc`\
52-`   make all                          # note 3`\
53-`   make install INSTALL_PREFIX="$FPC_DIR"`\
54-`   `\
55-`                                 # note 4`\
56-`   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"`\
57-`   `\
58-`   cp compiler/ppcx64 "$FPC_DIR"/bin/ppcx64          # note 5`\
59-`   cp compiler/ppcrossarm "$FPC_DIR"/bin/ppcrossarm  # note 6`\
60-`   `\
61-`   cp rtl/embedded/arm/* "$UNITDIR"  # note 7`\
62-`   `\
63-`   cd "$FPC_DIR"`\
64-`   mkdir -p etc`\
65-`   bin/fpcmkcfg -d basepath="$FPC_DIR" -o etc/fpc.cfg  # note 8`\
66-`   `\
67-`   cd bin`\
68-`   echo "#!/bin/sh" > fpc.bash       # note 9`\
69-`   echo "# A script that ensure when we start fpc here, it ignores all other fpc.cfg" >> fpc.bash`\
70-`   echo "$FPC_DIR""/bin/fpc -n @$FPC_DIR/etc/fpc.cfg \"\$@\"" >> fpc.bash`\
71-`   chmod u+x fpc.bash`
43+```
44+ #!/bin/bash
45+ export FPC_VER="fpc-3.3.1" # note 1
46+ export SRC_DIR="$HOME/testFPC/FPC/SRC/$FPC_VER"
47+ export FPC_DIR="$HOME/testFPC/FPC/$FPC_VER"
48+ UNITDIR="$FPC_DIR"/units/arm-embedded/armv6m
49+
50+ mkdir -p "$FPC_DIR"
51+ mkdir -p "$SRC_DIR"
52+ cd "$SRC_DIR"
53+ unzip ~/Downloads/fpc.zip # note 2
54+ cd fpc
55+ make all # note 3
56+ make install INSTALL_PREFIX="$FPC_DIR"
57+
58+ # note 4
59+ 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"
60+
61+ cp compiler/ppcx64 "$FPC_DIR"/bin/ppcx64 # note 5
62+ cp compiler/ppcrossarm "$FPC_DIR"/bin/ppcrossarm # note 6
63+
64+ cp rtl/embedded/arm/* "$UNITDIR" # note 7
65+
66+ cd "$FPC_DIR"
67+ mkdir -p etc
68+ bin/fpcmkcfg -d basepath="$FPC_DIR" -o etc/fpc.cfg # note 8
69+
70+ cd bin
71+ echo "#!/bin/sh" > fpc.bash # note 9
72+ echo "# A script that ensure when we start fpc here, it ignores all other fpc.cfg" >> fpc.bash
73+ echo "$FPC_DIR""/bin/fpc -n @$FPC_DIR/etc/fpc.cfg \"\$@\"" >> fpc.bash
74+ chmod u+x fpc.bash
75+```
7276
7377- 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.
7478
7579- 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>
7680
7781- note 3 - this takes a while.
7882
7983- note 4 - this takes a while too. And note it places the RTL files in a different place than fpcupdeluxe.
8084
8185- note 5 - this is your host system compiler.
8286
8387- note 6 - and this is the cross compiler, we call fpc and it calls this one.
8488
8589- note 7 - these are some units that the RTL, built earlier.
8690
8791- note 8 - this creates a pretty stock standard fpc.cfg file.
8892
8993- 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.
9094
9195> **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.
9296
9397### Setup a Lazarus project
9498
9599Doing 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.
96100
97101Start 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)
98102
99103Then, open the Project Options and set the following -
100104
101105Paths 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
102106
103107Config and Target \[x\] Use standard compiler config... \[ \] Use additional Target OS (-T) : Embedded Target CPU family (-P) : arm Target processor (-Cp) : ARMV6M
104108
105109Custom Options -Wpraspi_pico -godwarfsets -godwarfcpp -Xu
106110
107-`        -XParm-none-eabi-`
111+```
112+ -XParm-none-eabi-
113+```
108114
109115Note 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.
110116
111117### Compile from Commandline
112118
113119Its also reasonable to build from the command line, here is an example -
114120
115-`   #!/bin/bash`\
116-`   echo "------- Using the fpc from 3.3.1 ---------"`\
117-`   export MICHAELS_UNITS="$HOME"/Pico2/projects/pico-fpcexamples/units `\
118-`   "$HOME"/bin/FPC/fpc-3.3.1/bin/fpc.bash -va -Xu -XParm-none-eabi- -Tembedded -Parm -CpARMV6M -Fu"$MICHAELS_UNITS" -Wpraspi_pico project1`
121+```
122+ #!/bin/bash
123+ echo "------- Using the fpc from 3.3.1 ---------"
124+ export MICHAELS_UNITS="$HOME"/Pico2/projects/pico-fpcexamples/units
125+ "$HOME"/bin/FPC/fpc-3.3.1/bin/fpc.bash -va -Xu -XParm-none-eabi- -Tembedded -Parm -CpARMV6M -Fu"$MICHAELS_UNITS" -Wpraspi_pico project1
126+```
119127
120128Note: -XParm-none-eabi- indicates how the binutils are named. If yours are named differently, change it !
121129
122130### See also
123131
124132- [[ARM Embedded Tutorial - FPC and the Raspberry Pi Pico|ARM Embedded Raspberry Pi Pico Tutorials]]
125133
126134---
127135*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).*
128136Category: ARM
129137Category: Embedded
130138Category: Microcontrollers
131139Category: Operating Systems and Platforms
132140Category: Tutorials

Back to history