Sign in Join
Wiki copytemporary

Porting Free Pascal: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Porting Free Pascal

OldNewLine
1+This page is the start of a tutorial about how to port Free Pascal to different architectures and operating systems.
2+
3+## Porting to a new Operating System
4+
5+### Overview
6+
7+Porting Free Pascal to a new operating system involves several steps, namely:
8+
9+1. Add a new target to the compiler
10+2. Take the closest existing RTL as a start, copy it and start implementing
11+
12+### Adding a new target
13+
14+This task requires making small changes on several files on the compiler. You need to add your target to both the Makefiles, to the fpmake tool that creates the makefiles and the compiler itself.
15+
16+With your updated fpmake you can then recreate the makefile tree.
17+
18+Below is a example of how a new target is added. This Patch was responsible for adding SymbianOS target to the compiler. Be careful that this adds a i386-symbian target. If your target is for another architecture of course you should change the files on the directories for your particular architecture.
19+
20+ Index: compiler/compiler.pas
21+ ===================================================================
22+ --- compiler/compiler.pas (revision 6039)
23+ +++ compiler/compiler.pas (working copy)
24+ @@ -107,6 +107,9 @@
25+ {$ifdef win32}
26+ ,i_win
27+ {$endif win32}
28+ +{$ifdef symbian}
29+ + ,i_symbian
30+ +{$endif symbian}
31+ ;
32+
33+ function Compile(const cmd:string):longint;
34+ Index: compiler/i386/cputarg.pas
35+ ===================================================================
36+ --- compiler/i386/cputarg.pas (revision 6039)
37+ +++ compiler/i386/cputarg.pas (working copy)
38+ @@ -71,6 +71,9 @@
39+ {$ifndef NOTARGETWATCOM}
40+ ,t_watcom
41+ {$endif}
42+ + {$ifndef NOTARGETSYMBIAN}
43+ + ,t_symbian
44+ + {$endif}
45+
46+ {**************************************
47+ Assemblers
48+ Index: compiler/systems.pas
49+ ===================================================================
50+ --- compiler/systems.pas (revision 6039)
51+ +++ compiler/systems.pas (working copy)
52+ @@ -136,7 +136,9 @@
53+ system_x86_64_embedded, { 55 }
54+ system_mips_embedded, { 56 }
55+ system_arm_embedded, { 57 }
56+ - system_powerpc64_embedded { 58 }
57+ + system_powerpc64_embedded, { 58 }
58+ + system_i386_symbian, { 59 }
59+ + system_arm_symbian { 60 }
60+ );
61+
62+ tasm = (as_none
63+ ===================================================================
64+ --- compiler/utils/ppudump.pp (revision 20515)
65+ +++ compiler/utils/ppudump.pp (working copy)
66+ @@ -147,7 +147,8 @@
67+ { 67 } 'Linux-MIPSel',
68+ { 68 } 'NativeNT-i386',
69+ - { 69 } 'iPhoneSim-i386'
70+ + { 69 } 'iPhoneSim-i386',
71+ + { 70 } 'Wii-powerpc'
72+ );
73+
74+ const
75+ Index: rtl/Makefile.fpc
76+ ===================================================================
77+ --- rtl/Makefile.fpc (revision 6038)
78+ +++ rtl/Makefile.fpc (working copy)
79+ @@ -28,6 +28,7 @@
80+ dirs_solaris=solaris
81+ dirs_gba=gba
82+ dirs_nds=nds
83+ +dirs_symbian=symbian
84+
85+ [install]
86+ fpcpackage=y
87+ Index: utils/fpcm/fpcmake.ini
88+ ===================================================================
89+ --- utils/fpcm/fpcmake.ini (revision 6038)
90+ +++ utils/fpcm/fpcmake.ini (working copy)
91+ @@ -908,6 +908,12 @@
92+ SHORTSUFFIX=gba
93+ endif
94+
95+ +# Symbian OS
96+ +ifeq ($(OS_TARGET),symbian)
97+ +SHAREDLIBEXT=.dll
98+ +SHORTSUFFIX=symbian
99+ +endif
100+ +
101+ else
102+ # long version for 1.0.x - target specific extensions
103+
104+ Index: utils/fpcm/fpcmmain.pp
105+ ===================================================================
106+ --- utils/fpcm/fpcmmain.pp (revision 6038)
107+ +++ utils/fpcm/fpcmmain.pp (working copy)
108+ @@ -70,7 +70,7 @@
109+ o_linux,o_go32v2,o_win32,o_os2,o_freebsd,o_beos,o_netbsd,
110+ o_amiga,o_atari, o_solaris, o_qnx, o_netware, o_openbsd,o_wdosx,
111+ o_palmos,o_macos,o_darwin,o_emx,o_watcom,o_morphos,o_netwlibc,
112+ - o_win64,o_wince,o_gba,o_nds,o_embedded
113+ + o_win64,o_wince,o_gba,o_nds,o_embedded,o_symbian
114+ );
115+
116+ TTargetSet=array[tcpu,tos] of boolean;
117+ @@ -88,14 +88,14 @@
118+ 'linux','go32v2','win32','os2','freebsd','beos','netbsd',
119+ 'amiga','atari','solaris', 'qnx', 'netware','openbsd','wdosx',
120+ 'palmos','macos','darwin','emx','watcom','morphos','netwlibc',
121+ - 'win64','wince','gba','nds','embedded'
122+ + 'win64','wince','gba','nds','embedded','symbian'
123+ );
124+
125+ OSSuffix : array[TOS] of string=(
126+ '_linux','_go32v2','_win32','_os2','_freebsd','_beos','_netbsd',
127+ '_amiga','_atari','_solaris', '_qnx', '_netware','_openbsd','_wdosx',
128+ '_palmos','_macos','_darwin','_emx','_watcom','_morphos','_netwlibc',
129+ - '_win64','_wince','_gba','_nds','_embedded'
130+ + '_win64','_wince','_gba','_nds','_embedded','_symbian'
131+ );
132+
133+ { This table is kept OS,Cpu because it is easier to maintain (PFV) }
134+ @@ -126,7 +126,8 @@
135+ { wince }( true, false, false, false, false, true, false),
136+ { gba } ( false, false, false, false, false, true, false),
137+ { nds } ( false, false, false, false, false, true, false),
138+ - { embedded }( true, true, true, true, true, true, true)
139+ + { embedded }( true, true, true, true, true, true, true),
140+ + { symbian } ( true, false, false, false, false, true, false)
141+ );
142+
143+ type
144+
145+You will also need to add a i_target.pas and t_target.pas files to the compiler directory. Just copy an existing file for a similar platform and start working from there.
146+
147+If you want your target to use an internal linker (ELF, COFF, etc.) you need to add your target to that one, too. For the target i386-nativent the following change was done in the COFF output generator:
148+
149+ Index: compiler/ogcoff.pas
150+ ===================================================================
151+ --- compiler/ogcoff.pas (Revision 14564)
152+ +++ compiler/ogcoff.pas (Revision 14565)
153+ @@ -2906,7 +2918,7 @@
154+ idtxt : 'PECOFF';
155+ asmbin : '';
156+ asmcmd : '';
157+ - supported_targets : [system_i386_win32];
158+ + supported_targets : [system_i386_win32,system_i386_nativent];
159+ flags : [af_outputbinary,af_smartlink_sections];
160+ labelprefix : '.L';
161+ comment : '';
162+
163+You might also need to add your target to one of the assemblers for the target platform. E. g. if you're targeting an i386 system you might add your target like the following (the line breaks annotated with {cr} were added to keep the layout of the article intact):
164+
165+ Index: compiler/x86/agx86att.pas
166+ ===================================================================
167+ --- compiler/x86/agx86att.pas (Revision 15151)
168+ +++ compiler/x86/agx86att.pas (Arbeitskopie)
169+ @@ -380,7 +380,8 @@
170+ asmcmd : '--32 -o $OBJ $ASM';
171+ supported_targets : [system_i386_GO32V2,system_i386_linux,system_i386_Win32,{cr}
172+ system_i386_freebsd,system_i386_solaris,system_i386_beos,
173+ system_i386_netbsd,system_i386_Netware,system_i386_qnx,
174+ system_i386_wdosx,system_i386_openbsd,{cr}
175+ - system_i386_netwlibc,system_i386_wince,system_i386_embedded,{cr}
176+ system_i386_symbian,system_i386_haiku,system_x86_6432_linux];
177+ + system_i386_netwlibc,system_i386_wince,system_i386_embedded,{cr}
178+ system_i386_symbian,system_i386_haiku,system_x86_6432_linux,
179+ + system_i386_nativent];
180+ flags : [af_allowdirect,af_needar,af_smartlink_sections,af_supports_dwarf];
181+ labelprefix : '.L';
182+ comment : '# ';
183+
184+*Note*: In this case the change is only needed to get the "-al" option of the compiler, which needs the GNU assembler on Windows, working for i386-nativent.
185+
186+### Compiling your target
187+
188+Now that you have a target you will want to see if it works and spot compilation problems by compiling the compiler. To do this, open a console, navigate to your fpc subversion source code directory and build the compiler:
189+
190+ PATH=C:\Path_latest_fpc_stable
191+ cd compiler
192+ make arm
193+
194+And to build your RTL:
195+
196+ cd rtl\my_new_os
197+ make FPC=c:\fpc_subversion\compiler\ppcarm -XP$FPCTARGET-
198+
199+While trying to fix (or implement) RTL for your new target, information found in [[System unit structure]] may be useful.
200+
201+## Porting to a new Architecture
202+
203+### Step 1 - The Binutils
204+
205+The first step in porting to a new architecture is getting and understanding how the binutils of the platform work.
206+
207+### Step 2 - Code Skeleton
208+
209+You can start porting Free Pascal to a new Architecture by creating a new Lazarus project and a new skeleton for it. You can get the Lazarus project for ARM for example, rename it, lets say to ppcmipsel.lpi, modify it in a text editor to refer to mips directory instead of arm and then modify it in Lazarus in the Compiler Options Dialog in the Others tab and set the compiler directive to -dmipsel instead of -darm.
210+
211+## See also
212+
213+- [[Porting Free Pascal Example]]
214+- Paul Robinson is doing a step-by-step demonstration of his attempts to create a port for the IBM 370 mainframe (the [[ZSeries]] using the OS/VS1 operating system, explaining how he's checking everything, and what's happening along the way. You can read about how he's coming along at [[ZSeries/Part 1|Part 1]] of his article. (Note that the article changes as he learns more about how the compiler works.)
215+- [Structure of the compiler (FPC internals)](https://wiki.freepascal.org/FPC_internals)
216+
217+---
218+*From the Free Pascal wiki, page "Porting Free Pascal", as it stood on 2020-08-19 after a change by Xhajt03, 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).*
219+Category: FPC internals
220+Category: Tutorials

Back to history