Difference between revisions of "Cross compiling/hu"

From Lazarus wiki
Jump to navigationJump to search
Line 149: Line 149:
 
Í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.
 
Í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?==
 
==Miért Unix-ról Windws-ra és nem a másik irányba?==
The main reason for this is that generating unix binaries on a foreign platform (another unix or even Linux distro included) is more complicated.  
+
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.  
  
The main problems are:
+
Az alapvető problémák:
# You need libraries on the host computer (the computer you are crosscompiling from)
+
# Szükséged van függvénytárakra (library-ik) a gazdarendszeren (amelyen a keresztfordítást el akarod végezni)
# There is not one such set libraries as a result of more complex and dynamic Unix versioning
+
# 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.
# A lot more (linker) parameters and tweaking is required, which are hard to explain for a general case.
+
# 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.
  
If you still persist after this warning, please have a look at the crossnotes document mentioned further up, or the buildfaq.
+
Ha a figyelmeztetések ellenére mégis kitartasz, olvasd el a crossnotes dokumentumot amely fentebb van megemlítve, vagy a fordításról szóló GYIK-ot.
  
 
==Több információt szeretnék a FreePascal építéséről. Hol találok?==
 
==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: [http://www.stack.nl/~marcov/buildfaq.pdf pdf változat] vagy [http://www.stack.nl/~marcov/buildfaq/buildfaq.html html változat (gyengébb minőség, ritkábban frissítve)]
 
Ez egy általános GYIK (faq) az FPC építéséről és beállításáról pdf formátumban: [http://www.stack.nl/~marcov/buildfaq.pdf pdf változat] vagy [http://www.stack.nl/~marcov/buildfaq/buildfaq.html html változat (gyengébb minőség, ritkábban frissítve)]

Revision as of 11:27, 1 June 2009

Deutsch (de) English (en) español (es) français (fr) magyar (hu) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN) 中文(台灣)‎ (zh_TW)

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

The compiler is only one part. We also need the assembler and the linker. FPC provides internal assembler and/or linker for just some platforms, other platforms needs to use external tools for that. And these tools are usually not able to create binaries for different platforms. That's why we have to different special linker 'ld' and assembler 'as' for every target platform. These are the binutils.

Unit-ok a célrendszerhez

After creating (or having/installing) the cross tools, one needs FPC RTL and other units compiled for the chosen target platform. For example, every target platform needs a different file system.ppu (System unit), etc. These units may be either compiled using your compiler set up for compilation to the target platform, or you may potentially use officially distributed units compiled (and distributed) with exactly the same FPC version (if available in format useable under the particular host platform).

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.

  • First check if you already have the files i386-linux-ld and i386-linux-as:
 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 $@
  • Make them executable:
bash $ chmod +x /usr/bin/i386-linux-as
bash $ chmod +x /usr/bin/i386-linux-ld
  • Compile FPC:
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

  • First you need the binutils for the platform you want to compile to. Download odcctools from this site (use the cvs version) and follow their instructions for installing. http://www.opendarwin.org/projects/odcctools/
  • you need to create a fake root dir like: $HOME/darwinroot copy at least the /System and /Frameworks and /usr directories (you may have to copy more than this) from your Apple or Darwin computer to $HOME/darwinroot
  • now that you have these files make a folder in $HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as.
  • now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.

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

  • configure your /etc/fpc.cfg file.

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 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

  • Cross binutils

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).

  • Compile FPC:
 $ 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 dokumentumot amely fentebb van megemlítve, vagy a fordításról szóló GYIK-ot.

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)