[fpc-devel] Building compiler, rtl, host, target... (cross compiling)
Sven Barth
pascaldragon at googlemail.com
Thu Apr 7 09:37:31 CEST 2011
Am 07.04.2011 03:06, schrieb Skybuck Flying:
> Ok,
>
> I thought about it a bit and I think I now logically understand what's
> going on and what needs to happen when one wants to build a new
> compiler, possibly for cross compiling and such in relation to the RTL.
>
> First of all "free pascal compiler" is not a "multi-target cross
> compiler" but it is a "dual-target cross compiler" (dual meaning host
> and target can be different ;))
>
The definition of "cross compiler" is that a compiler that runs on CPU A
with operating system B can generate code for operating system C that
runs on CPU D. Not more, not less.
> So starting with the basics: Suppose somebody would get the free pascal
> source codes only and now executables then that person would first need
> to compile:
>
> 1. The compiler
> and
> 2. The run time library
It must be done the other way round as the compiler needs the RTL to run
itself.
>
> For let's say windows.
>
>
> The compiler is a little bit like an application, applications need a
> system unit, the runtime library provides that system unit.
>
> However the "external/user compiler" might already have a system unit...
> so for that purposes the "external/user compiler run time library/system
> unit might be used". The free pascal compiler dependancies on it's own
> RTL could be fixed/modified/temporarely changed to use the external
> compiler's system unit to get the first compiler build working.
>
There is no need to fix up anything. The process is the following:
You have a starting compiler for let's say i386 and you have source code
of FPC and its RTL any you want to have a new shiny i386 Windows compiler.
1. Compile the RTL using the starting compiler (after all the RTL is
just Pascal source that pulls in operating system specific code)
2. Compile the compiler together with the newly compiled RTL
3. Use that new compiler to compile the RTL again (so that new features
are used)
4. Compile the compiler again using the newly compiled RTL.
Now you can Win32 i386 applications using your new compiler (and the RTL)
> One RTL for the host, One RTL for the target.
>
>
> How the compiler makes sense of this I am not yet sure... Perhaps it's a
> question of linking in the NewRTL when a NewPlatform application is
> being build by the new compiler.
>
The RTL for the host is used by the compiler as it was compiled with
that RTL (as every Free Pascal program is) and the RTL for the target
are "just" (compiled) source files that are used(!) by the compiler to
build the final executable (like it's done for every Free Pascal program).
> So if I am correct in my assumption then the any RTL for that matter
> must be "self-standing" and not rely on anything else except it's own
> platform that it was written for.
>
This assumption is indeed correct. The base RTL unit (system.pp) as no
dependency whatsoever and all other units inside the RTL (sysutils,
classes, etc) basically only depend on the system unit.
> So I guess this is where a "half-cross-compiler" comes in... The
> half-cross compiler is capable of compiling a NewRTL and producing some
> kind of assembly text which can then be used to compile the NewRTL to
> some kind of binary (a pascal unit binary?)
>
Every FPC compiler is able to compile the RTL. Just do a "make cycle"
inside the compiler directory and you'll see that the RTL is compiled
mutliple times.
> So if I am correct "porting free pascal compiler and rtl towards a new
> platform" requires the following steps:
>
>
> 1. Step 1 (preparing for first native free pascal compiler)
>
> Install a "external pascal compiler" so that free pascal compiler
> sources can be compiled.
> (ignore free pascal's own rtl if necessary), make changes to the free
> pascal compiler source code if necessary to use the external pascal
> compiler's RTL.
>
The "external pascal compiler" MUST be a Free Pascal compiler as it
needs to compile the RTL for the compiler first. More specifically that
procedure is only supported by the compiler of the last release
(currently 2.4.2 although 2.4.0 works as well, but not 2.2.4 or older).
It also used to be possible to compile FPC and its RTL with Turbo Pascal
(the first FPC was produced this way) or using Delphi. But that time is
long gone.
Regards,
Sven
More information about the fpc-devel
mailing list