Cross compiling/pt

From Free Pascal wiki
Jump to navigationJump to search

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

Introdução

Esta é uma curta introdução para novatos. As seções seguintes descrevem como configurar um sistema para cross-compilação, isto é, trabalhar no Linux and criar executáveis para Win32 (ou FreeBSD, Darwin, etc.). O FreePascal é um compilador e basicamente converte código em binário (linguagem de máquina). Os binários também contêm informação sobre como o sistema operacional inicia o executável. Portanto os binários são específicos para cada plataforma. O FreePascal em si mesmo não precisa de muita configuração. Ele pode criar binários para muitas plataformas. Somente lhe diga para fazer isso. Mas o compilador é somente uma parte. Existe também o assembler e o vinculador, e essas ferramentas não são capazes de criar código cross-plataforma. É por isso que nós precisamos criar vinculadores especiais 'ld' e assembler 'as' para cada plataforma-alvo. Essas são as binutils. Depois de criar as ferramentas cross-plataforma, todas as unidades Pascal do FPC serão cross-compiladas. Por exemplo, existe um system.ppu para cada plataforma-alvo. Assim o seu arquivo de configuração do FPC será configurado de forma que a cross-compilação se torne tão fácil que você esquecer todos os detalhes aborrecidos. O mesmo será feito para a LCL - a biblioteca de componentes do Lazarus. E depois disso você pode fazer cross-compilação de seus programas Pascal para Win32. Ou iniciá-los no Wine ou copiá-los para uma máquina Windows e testá-los.

Passos Básicos

Existem uns poucos passos em comum implicados em cross-compilação que você precisa cumprir em cada caso:

  1. Ter um compilador FreePascal para a plataforma em que você quer compilar.
  2. Ter o código-fonte do FreePascal.
  3. Ou construí-lo dos fontes ou obter os binários das cross-binutils que executem na plataforma em que você está e são desenhadas para construir programas para a plataforma-alvo desejada.
  4. Às vezes você vai precisar de alguns arquivos da plataforma para onde está compilando.

De Linux

Para Linux

Fique claro que isso é para compilar de Linux(x86_64) para Linux((i386).

Há chances de a sua distribuição Linux 64 bits já possibilitar a compilação de programas 32 bits mas devido à maneira como o processo de construção do FPC é desenhado existem algumas coisas que você vai precisar fazer:

  • Primeiro cheque se você já tem os arquivos i386-linux-ld e i386-linux-as:
 bash $ which i386-linux-ld
 bash $ which i386-linux-as

Se você tem esses arquivos, pule para o título "Compilar o FPC".

Se você não tem os arquivos, eu fiz alguns 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 $@
  • Fazê-los um executável:
bash $ chmod +x /usr/bin/i386-linux-as
bash $ chmod +x /usr/bin/i386-linux-ld
  • Compilar o 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.

Para Windows

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.

Para Darwin ou Mac OS X

  • 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 mistated some things (or most everything) as it's been a while since I did this.

De Windows

Para Linux

Marco vd Voort wrote some crossnotes. If somebody has time, he can add them to this wiki.

From Darwin (Mac OS X) i386

from i386 to powerpc

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

Cross compile FAQ

Why cross compile?

So you can develop a program for one OS/CPU and compile it for another OS/CPU without rebooting or switching computers.

Why Linux to Windows and not the other way around?

The main reason for this is that generating unix binaries on a foreign platform (another unix or even Linux distro included) is more complicated. Static linking is already complicated, let alone shared.

You would need the used libraries from the target platforms (gtk, glib,libc etc), and a lot of additional configuring for ld. (library paths, dynlinker path, etc.)

This has been partially done (for the static case), but it is hard since it needs manual postediting of linker files and linker commandline, and a deep understanding about what makes Unix binaries tick.

I want more information on building Freepascal. Where is it?

This is a general FAQ in pdf format about how to build and configure FPC: http://www.stack.nl/~marcov/buildfaq.pdf.