Cross compiling/fr

From Free Pascal wiki
Revision as of 22:54, 17 December 2019 by Trev (talk | contribs) (Deleted page categories already in template; rationalised macOS naming)
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)

avant-propos

Ceci est une brève introduction pour les débutants. Les sections suivantes décrivent comment configurer un système pour une compilation croisée, ce qui signifie travailler sous linux et créer des exécutables win32 (ou freebsd ou dawin, ou ...). FreePascal est un compilateur et convertit fondamentalement des sources en fichiers binaire (langage machine). Ces fichiers binaires contiennent également des informations sur la manière dont le système d'exploitation démarre le fichier exécutable. Par conséquent, ces fichiers binaires sont spécifiques à la plate-forme. FreePascal lui-même n'a pas besoin de beaucoup de configuration. Il peut créer des fichiers binaires pour de nombreuses plate-formes. Il suffit de lui dire de le faire. Mais le compilateur est seulement une partie. Il y a aussi l'assembleur et l'éditeur de liens. Et ces outils ne sont pas en mesure de créer du code croisé. C'est pourquoi nous avons à créer un éditeur de lien spécial 'ld' et un assembleur 'as' pour chaque plate-forme cible. Ce sont les binutils. Après la création des outils croisés, toutes les unités pascal seront cross compilées. Par exemple, à ce moment-là il y a un fichier system.ppu pour chaque plate-forme cible. Alors votre fichier de configuration fpc sera configuré, de sorte que la compilation croisée devient si facile, que vous pouvez oublier tous les détails ennuyeux. La même chose sera faite pour la LCL - la bibliothèque de composants de lazarus. Et après cela, vous pouvez faire la compilation croisée des programmes en pascal pour win32. Soit lancer les avec wine soit copiez les vers une machine Windows et testez-les là.

Étapes de base

Il y a plusieurs étapes qui sont obligatoires pour faire de la cross-compilation :

  1. Vous devez avoir le compilateur FreePascal associé à la plateforme de votre machine d'installé.
  2. Vous devez avoir le code source de FreePascal.
  3. Vous devez soit compiler à partir du code source, soit vous procurer des binaires des utilitaires de compilation croisée qui fonctionnent sur la plate-forme où vous compilez et qui sont destinés à compiler des programmes pour la plate-forme cible.
  4. Vous aurez quelquefois besoin de fichiers venant du système cible pour lequel vous compilez.

À partir de Linux

Vers Linux

Pour faire simple, il s'agit de compiler de linux(x86_64) à linux(i386).

Il est vraisemblable que votre distribution Linux 64 peut déjà compiler des programmes 32 bits, mais en raison de la conception de la construction de fpc, vous pourriez avoir deux ou trois choses à faire.

  • vérifiez d'abord que vous avez déjà les fichiers i386-linux-ld et i386-linux-as :
 bash $ which i386-linux-ld
 bash $ which i386-linux-as

Si vous les avez, passez à "Compiler FPC". N'ayant pas ces fichiers, j'ai écrit ces 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 $@
  • Les rendre exécutables :
bash $ chmod +x /usr/bin/i386-linux-as
bash $ chmod +x /usr/bin/i386-linux-ld
  • Compiler FPC:
bash $ make all CPU_TARGET=i386

puis :

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

C'est tout. Editez votre fichier /etc/fpc.cfg si nécessaire.

Vers Windows

Compilation_croisée_pour_Win32_sous_Linux

Si vous compilez la branche 2.1.1 ou supérieure de fpc il suffit de faire :

bash $ make all OS_TARGET=win32 CPU_TARGET=i386

puis

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

Note : Pour construire pour Win64, la commande make est : make all OS_TARGET=win64 CPU_TARGET=x86_64

La raison de cette simplicité est la présence d'un linker interne dans cette version de fpc.

To Darwin or macOS

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

From Windows

To Linux

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

From Darwin (macOS) 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.

From Darwin to Windows, Linux and others

The fpc package of the package manager fink has a 64-bit compiler. In addition, there is a list of packages for crosscompiling to windows, linux, freebsd on various CPUs.

Examples for installing a crosscompiler are:

$ fink install fpc-cross-i386-win32

or

$ fink install fpc-cross-arm-linux

To compile use these commands:

fpc -Pi386 -Twin32 FILENAME
fpc -Parm -Tlinux FILENAME

This command gives the list of cross compilers:

$ fink list fpc-cross

Currently (fpc 3.0.4), it gives:

fpc-cross-arm-gba
fpc-cross-arm-linux
fpc-cross-arm-nds
fpc-cross-arm-wince
fpc-cross-arm-armv4t-embedded
fpc-cross-arm-armv7m-embedded
fpc-cross-i386-darwin
fpc-cross-i386-freebsd
fpc-cross-i386-go32v2
fpc-cross-i386-linux
fpc-cross-i386-nativent
fpc-cross-i386-netbsd
fpc-cross-i386-solaris
fpc-cross-i386-win32
fpc-cross-i386-wince
fpc-cross-jvm-android
fpc-cross-jvm-java
fpc-cross-m68k-linux
fpc-cross-mipsel-linux
fpc-cross-powerpc-linux
fpc-cross-sparc-linux
fpc-cross-x86-64-dragonfly
fpc-cross-x86-64-freebsd
fpc-cross-x86-64-linux
fpc-cross-x86-64-win64

For other platforms (processors and systems) you have to do the setup by yourself. It is basically the same scheme: First, you need the corresponding binutils (See Binutils) and second, the crosscompiler and the run time library. Some more details of the building procedure can be learned from the fink package description files of the crosscompilers from above.

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.