Cross compiling/hu

This is an old revision of Cross compiling/hu, edited by Gábor at . The current revision is 60504.

/* Unit-ok a célrendszerhez */

Előszó

Ez egy rövid bevezető kezdőknek. The following sections describe how to setup a system to cross compile, that means creating binaries (executables) for a different platform as the one used for compilation - e.g. working under Linux and creating Win32 executables (or those for FreeBSD or Darwin, etc.). In this case, the platform used for compilation is usually referred to as "host" (Linux in the example above) and the platform where you want to run your created binaries is your "target". FreePascal is a compiler and basically converts source into binaries (machine language). These binaries also contain information on how the operating system starts the executables. Moreover, the binaries refer to the APIs provided by the particular operating system, that's why different implementation of our Run-Time Library is necessary for different operating systems. Therefore these binaries are platform specific. FreePascal itself does not need much setup. It can create binaries for many platforms. Just tell it to do so.

Gazda- és célrendszer ugyanolyan CPU-n

FPC is designed so that the distributed compiler can create machine code for a certain CPU (because different CPUs need different machine code) and it knows specific requirements for all supported platforms (operating systems) available on that particular CPU. This means that you can perform cross-compilation with the same compiler used for native compilation as long as you stick to the same CPU.

Gazda és célrendszer eltérő CPU-n

If you need to create binaries for a different CPU, you need a special cross-compiler, i.e. compiler running on the host platform, but able to create machine code for a different CPU (in the case of FPC, such a cross-compiler would be again able to target all supported platforms available on the _target_ CPU). This cross-compiler is then usually stored in the same directory as the native compiler. Such a cross-compiler may be either compiled by yourself, or you can use a ready made distributed cross-compiler provided for some platforms directly by the FPC team (usually platforms mostly used in portable devices like arm-linux or arm-wince, because these are usually not used as host platforms). Fpc binary can then select the right compiler (either the native compiler or the cross-compiler) for the target CPU selected using the -P parameter.

Assembler és linker

A fordító csak egy rész. Szükségünk van még assemblerre és a linkerre is. Az FPC rendelkezik beépített assemblerrel és/vagy linkerrel néhány rendszerhez, más rendszereken külső eszközökre van szüksége. Ezek az eszközök általában nem képesek binárisokat létrehozni eltérő rendszerek számára. Ezért van, hogy speciális linkert 'ld' és assemblert 'as' használunk minden célrendszeren. Ezeket nevezzük binutils-nak.

Unit-ok a célrendszerhez

Miután létrehoztuk (vagy megszereztük/telepítettük) a keresztfordító eszközöket, szükség lesz még az FPC RTL és további unitokra a választott célrendszerre lefordítva. Például minden célrendszerhez eltérő system.ppu (System unit) fájlra van szükség, stb. Ezek a unitok létrehozhatók akár a célrendszerre fordításhoz beállított fordítóval, vagy esetleg használhatod a hivatalosan terjesztett unitokat, amelyek pontosan megegyező FPC változattal készültek illetve lettek kiadva (ha elérhető a gazdarendszeren használható formátumban).

Konfiguráció

Then your fpc config file will be setup, so that cross compilation becomes so easy, that you can forget all the boring details. The same will be done for the LCL - the lazarus component library (if using Lazarus). And after this you can cross compile pascal programs for the (different) target platform. The resulting binaries may then be copied to a machine running the target platform, or run under an emulator (e.g. Wine for Win32 binaries under Linux, etc.).

Alap műveletek

There are a few common steps involved in crosscompiling that you must do in every case:

  1. Have already a FreePascal compiler for the platform you wish to compile from.
  2. You need to have the FreePascal source code (except for the special case of having everything prepared by someone else).
  3. You need to either build from source or obtain binaries of the cross-binutils that run on the platform you are on and are designed to build programs for your desired target platform.
  4. Sometimes you will need some files from the target you are compiling to.

Linux rendszeren

Linux rendszerre

To be clear, the text below is to compile from linux(x86_64) to linux(i386). Information about targetting Linux running on ARM (e.g. Zaurus) may be found in Setup Cross Compile For ARM.

Chances are that your 64 bit linux distrubution is already capable of compiling 32 bit programs but due to the way the fpc build process is designed there are a couple of things you might have to do.

 bash $ which i386-linux-ld
 bash $ which i386-linux-as

If you have these files skip to the "Compile FPC" heading.

I did not have these files so I made a couple of scripts:

#!/bin/bash
# name this file /usr/bin/i386-linux-ld
ld -A elf32-i386 $@
#!/bin/bash
# name this file /usr/bin/i386-linux-as
as --32 $@
bash $ chmod +x /usr/bin/i386-linux-as
bash $ chmod +x /usr/bin/i386-linux-ld
bash $ make all CPU_TARGET=i386

then:

bash $ su -c "make install CPU_TARGET=i386"

That's it. Edit your /etc/fpc.cfg file if needed.

Windows rendszerre

Information on cross-compilation with Lazarus may be found in Cross_compiling_for_Win32_under_Linux

If you are compiling the 2.1.1 or greater branch of fpc you can just do:

bash $ make all OS_TARGET=win32 CPU_TARGET=i386

and then

bash $ su -c "make crossinstall OS_TARGET=win32 CPU_TARGET=i386"

Note: to build for win64 the make command is: make all OS_TARGET=win64 CPU_TARGET=x86_64

The reason for this simplicity is the internal linker included in this version of fpc.

Darwin vagy Mac OS X rendszerre

type:

$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)

type (iirc):

make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"

if that succeded you can install it to whereever you want with:

make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc

now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs

add a section like this:

#IFDEF powerpc
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
-k-systemroot $HOME/darwin/cross
#ENDIF

whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATH and you should be able to just do ppccross someprogie.pas and it will create a darwin executable.

I may have missed some things (or most everything) as it's been a while since I did this.

Windows rendszeren

Linux rendszerre

Marco vd Voort írt a témáról crossnotes címmel feljegyzést. Ha valakinek van ideje hozzáadhatja a wiki oldalhoz.

GO32v2 rendszerre

A részletes információk megtalálhatók a Cross-compilation from Win32 to GO32v2 részben.

Darwin (Mac OS X) i386 rendszeren

i386-ról powerpc-re

First check if you already have the files powerpc-darwin-ld and powerpc-darwin-as:

 $ which powerpc-darwin-ld
 $ which powerpc-darwin-as

If you have these files skip the creation of the symlinks and go to the "Compile FPC" heading.

Actually, the "normal" binutils can be used, since they are universal. Therefore, simple symlinks are enough:

 $ sudo ln -s /usr/bin/as /usr/bin/powerpc-darwin-as
 $ sudo ln -s /usr/bin/ld /usr/bin/powerpc-darwin-ld

The symlinks can be in any other directory, as long as it is in your $PATH (for example /sw/bin when installing through fink).

 $ cd fpc/compiler
 $ make cycle CPU_TARGET=powerpc

This creates the powerpc compiler (fpc/compiler/ppcppc) and the units of the rtl.

In order to create powerpc binaries no actual crosscompiler is needed. The powerpc fpc and ppcppc run fine on IntelMacs using Rosetta.

More test and docs are needed for parallel installation of both and usage.

If there are missing units, check your config file, $HOME/.fpc.cfg or /etc/fpc.cfg or /sw/etc/fpc.cfg You may have to add something like or where ever your units are located.

-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/
-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
-Fu/usr/local/lib/fpc/lib/fpc/$fpcversion/units/$fpctarget/*

Reminder: Universal binaries are created from the individual (i386 and powerpc) binaries using lipo.

Keresztfordítás GYIK (faq)

Mire jó a keresztfordítás?

Így fejleszthetsz programokat egy OS/CPU-n és lefordíthatod egy másik OS/CPU-ra a számítógép újraindítása vagy másik számítógép használata nélkül.

Miért Unix-ról Windws-ra és nem a másik irányba?

A legfőbb ok az, hogy Unix binárisok létrehozása más rendszereken (másik Unix vagy akár Linux is) jóval bonyolultabb.

Az alapvető problémák:

  1. Szükséged van függvénytárakra (library-ik) a gazdarendszeren (amelyen a keresztfordítást el akarod végezni)
  2. Nincs ilyen egységes gyűjtemény függvénytárakból (library) a nagyon összetett és dinamikus Unix verziókezelés eredményeként.
  3. Rengeteg (linker) paraméterezés és finomhangolás elvégzésére van szükség, amiket a legtöbb esetben nagyon nehéz elmagyarázni.

Ha a figyelmeztetések ellenére mégis kitartasz, olvasd el a crossnotes című feljegyzést, amely fentebb van megemlítve vagy a buildfaq fejezetet.

Több információt szeretnék a FreePascal építéséről. Hol találok?

Ez egy általános GYIK (faq) az FPC építéséről és beállításáról pdf formátumban: pdf változat vagy html változat (gyengébb minőség, ritkábban frissítve)