Difference between revisions of "Cross compiling"

From Lazarus wiki
Jump to navigationJump to search
Line 1: Line 1:
 
{{Translate}}
 
{{Translate}}
  
=Foreword=
+
=Предисловие=
This is a short introduction for newbies. 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.
+
Это краткое введение для новичков. В следующих разделах описывается, как настроить систему для кросс компиляции, что означает создание бинарных файлов (исполняемых файлов) для различных платформ, которые используются для компиляции - например, работающие под Linux и создание исполняемых файлов Win32 (или FreeBSD, Darvin, и т.д.). В этом случае платформа, используемая для компиляции, как правило называется "хост" (Linux в примере выше), и платформа, на которой вы хотите создавать файлы ваша "цель". FreePascal является компилятором и в основном преобразует исходники в исполняемые файлы (машинный язык). Эти файлы содержат информацию о том, как операционная система начинает исполнять файлы. Кроме того, бинарные файлы обращаются к API, предоставляемые конкретной операционной системой, поэтому различные реализации нашей Run-time библиотеки необходимы для различных операционных систем. Поэтому эти двоичные файлы специфичны для разных платформ. FreePascal сам по себе не нуждается в длительной настройке. Он может создавать исполняемые файлы для многих платформ. Просто скажите ему сделать это.
  
==Host and target on the same CPU==
+
==Хост и цель на том же процессоре==
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.
+
FPC устроен так, что распределенный компилятор может создать машинный код для определенного процессора (потому что разные процессоры нуждаются в различном машинном коде), и он знает конкретные требования для всех поддерживаемых платформ (операционных систем), доступных на данном процессоре. Это означает, что вы можете выполнять кросс-компиляции с тем же компилятором, используемым для собственной компиляции до тех пор, пока Вы придерживаетесь того же процессора.
  
==Host and target on different CPUs==
+
==Хост и цель на разных процессорах==
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.
+
Если вам нужно создать двоичные файлы для различных процессоров, вам нужен специальный кросс-компилятор, то есть компилятор работающий на хост платформе, но который в состоянии создать машинный код для различных процессоров (в случае FPC, например кросс-компилятор должен быть способен работать для всех поддерживаемых платформах доступных на _target_ CPU). Это кросс-компилятор обычно хранится в том же каталоге, что и родной компилятор. Такие кросс-компилятор может быть либо собран вами вручную, или Вы можете использовать готовый распространяемый кросс-компилятор предоставленный для некоторых платформ непосредственно FPC командой (обычно платформы в основном используемые в портативных устройствах, такие как arm-linux или arm-wince, потому что они обычно не используются в качестве хост-платформ). Fpc бинарник может выбрать нужный компилятор (как родной компилятор или кросс-компилятор) для целевого процессора выбирается с помощью параметра-P.
  
==Assembler and 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 | binutils]].
+
Компилятор это только одна часть. Нам также необходимо ассемблер и компоновщик. FPC обеспечивает встроенный ассемблере и/или линкер только для некоторых платформ, другие платформы должны использовать внешние инструменты для этого. И эти средства, как правило, не в состоянии создать двоичные файлы для различных платформ. Вот почему мы должны различать специальный компоновщик 'ld' и ассемблер 'as' для каждой целевой платформы. Это [[Binutils | binutils]].
  
==Units for target==
+
==Юниты для целевой платформы==
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).
+
После создания (или имеющихся/установленных) кросс инструментов, нужно скомпилировать FPC RTL и другие юниты для выбранной целевой платформы. Например, каждая целевая платформа требует другой файл system.ppu (системный юнит), и т.д. Эти юниты могут быть либо скомпилированы с помощью компилятора созданным для компиляции под целевую платформу, либо вы можете потенциально использовать официально распространяемые юниты скомпилированные (и распространяемые) с точно такой же версии FPC (если имеются в полезном формате под конкретную хост-платформу).
  
==Configuration==
+
==Конфигурация==
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.).
+
Когда ваш FPC конфигурационный файл будет подготовлен, кросс-компиляция станет настолько простой, что вы сможете забыть все эти скучные детали. То же самое должно быть сделано для LCL - библиотеки компонентов Lazarus (при её использовании). И после этого вы можете кросс-компилировать программы на Паскале на разные целевой платформы. В результате файлы могут быть скопированы на машину под управлением целевой платформы, или работать под эмулятором (например, Wine для Win32 файлов под Linux, и т.д.).
  
==Basic Steps==
+
==Основные шаги==
There are a few common steps involved in crosscompiling that you must do in every case:
+
Есть несколько общих этапов кросс-компилляции, которые вы должны сделать в каждом конкретном случае:
# Have already a FreePascal compiler for the platform you wish to compile from.
+
# Уже имеющийся FreePascal компилятор для платформы для которой вы хотите скомпилировать.
# You need to have the FreePascal source code (except for the special case of having everything prepared by someone else).
+
# Вы должны иметь исходный код FreePascal (за исключением частного случая если все уже подготовлено кем-то другим).
# 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.
+
# Вы должны либо сделать билд из исходников или получить бинарные файлы кросскомпиллятора cross-Binutils, которые работают на платформе на которой вы находитесь, и предназначены для сборки программ для желаемой целевой платформы.
# Sometimes you will need some files from the target you are compiling to.
+
# Иногда вам будут нужны некоторые файлы из целевой платформы для которой вы компилируете код.
  
 
=From Linux=
 
=From Linux=

Revision as of 23:00, 30 September 2011

Template:Translate

Предисловие

Это краткое введение для новичков. В следующих разделах описывается, как настроить систему для кросс компиляции, что означает создание бинарных файлов (исполняемых файлов) для различных платформ, которые используются для компиляции - например, работающие под Linux и создание исполняемых файлов Win32 (или FreeBSD, Darvin, и т.д.). В этом случае платформа, используемая для компиляции, как правило называется "хост" (Linux в примере выше), и платформа, на которой вы хотите создавать файлы ваша "цель". FreePascal является компилятором и в основном преобразует исходники в исполняемые файлы (машинный язык). Эти файлы содержат информацию о том, как операционная система начинает исполнять файлы. Кроме того, бинарные файлы обращаются к API, предоставляемые конкретной операционной системой, поэтому различные реализации нашей Run-time библиотеки необходимы для различных операционных систем. Поэтому эти двоичные файлы специфичны для разных платформ. FreePascal сам по себе не нуждается в длительной настройке. Он может создавать исполняемые файлы для многих платформ. Просто скажите ему сделать это.

Хост и цель на том же процессоре

FPC устроен так, что распределенный компилятор может создать машинный код для определенного процессора (потому что разные процессоры нуждаются в различном машинном коде), и он знает конкретные требования для всех поддерживаемых платформ (операционных систем), доступных на данном процессоре. Это означает, что вы можете выполнять кросс-компиляции с тем же компилятором, используемым для собственной компиляции до тех пор, пока Вы придерживаетесь того же процессора.

Хост и цель на разных процессорах

Если вам нужно создать двоичные файлы для различных процессоров, вам нужен специальный кросс-компилятор, то есть компилятор работающий на хост платформе, но который в состоянии создать машинный код для различных процессоров (в случае FPC, например кросс-компилятор должен быть способен работать для всех поддерживаемых платформах доступных на _target_ CPU). Это кросс-компилятор обычно хранится в том же каталоге, что и родной компилятор. Такие кросс-компилятор может быть либо собран вами вручную, или Вы можете использовать готовый распространяемый кросс-компилятор предоставленный для некоторых платформ непосредственно FPC командой (обычно платформы в основном используемые в портативных устройствах, такие как arm-linux или arm-wince, потому что они обычно не используются в качестве хост-платформ). Fpc бинарник может выбрать нужный компилятор (как родной компилятор или кросс-компилятор) для целевого процессора выбирается с помощью параметра-P.

Ассемблер и компоновщик

Компилятор это только одна часть. Нам также необходимо ассемблер и компоновщик. FPC обеспечивает встроенный ассемблере и/или линкер только для некоторых платформ, другие платформы должны использовать внешние инструменты для этого. И эти средства, как правило, не в состоянии создать двоичные файлы для различных платформ. Вот почему мы должны различать специальный компоновщик 'ld' и ассемблер 'as' для каждой целевой платформы. Это binutils.

Юниты для целевой платформы

После создания (или имеющихся/установленных) кросс инструментов, нужно скомпилировать FPC RTL и другие юниты для выбранной целевой платформы. Например, каждая целевая платформа требует другой файл system.ppu (системный юнит), и т.д. Эти юниты могут быть либо скомпилированы с помощью компилятора созданным для компиляции под целевую платформу, либо вы можете потенциально использовать официально распространяемые юниты скомпилированные (и распространяемые) с точно такой же версии FPC (если имеются в полезном формате под конкретную хост-платформу).

Конфигурация

Когда ваш FPC конфигурационный файл будет подготовлен, кросс-компиляция станет настолько простой, что вы сможете забыть все эти скучные детали. То же самое должно быть сделано для LCL - библиотеки компонентов Lazarus (при её использовании). И после этого вы можете кросс-компилировать программы на Паскале на разные целевой платформы. В результате файлы могут быть скопированы на машину под управлением целевой платформы, или работать под эмулятором (например, Wine для Win32 файлов под Linux, и т.д.).

Основные шаги

Есть несколько общих этапов кросс-компилляции, которые вы должны сделать в каждом конкретном случае:

  1. Уже имеющийся FreePascal компилятор для платформы для которой вы хотите скомпилировать.
  2. Вы должны иметь исходный код FreePascal (за исключением частного случая если все уже подготовлено кем-то другим).
  3. Вы должны либо сделать билд из исходников или получить бинарные файлы кросскомпиллятора cross-Binutils, которые работают на платформе на которой вы находитесь, и предназначены для сборки программ для желаемой целевой платформы.
  4. Иногда вам будут нужны некоторые файлы из целевой платформы для которой вы компилируете код.

From Linux

To Linux

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.

To Windows

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.

To Darwin or Mac OS X

This section is out of date! New information about cross-compiling from Linux to Darwin is here.

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

From Windows

To Linux

This is less trivial, there is some info in the buildfaq

To GO32v2

Detailed information may be found in Cross-compilation from Win32 to GO32v2.

Win32 To Win64

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

$ make all OS_TARGET=win64 CPU_TARGET=x86_64

and then

$ make crossinstall OS_TARGET=win64 CPU_TARGET=x86_64

From Darwin (Mac OS X) i386

from i386 to powerpc

The official FPC installer for Mac OS X/i386 includes a PowerPC cross-compiler and all units necessary to compile PowerPC programs (use ppcppc instead of ppc386 to compile your programs). The instructions below are only necessary if you want to compile and install a new version from svn.

  • Compile FPC:
 $ cd fpc
 $ make all CPU_TARGET=powerpc -j 2

This creates the powerpc cross-compiler compiler (fpc/compiler/ppcrosspcc) and all units. You can install them using the following commands:

 $ sudo make FPC=`pwd`/compiler/ppc386 install CPU_TARGET=powerpc CROSSINSTALL=1
 $ INSTALLED_VERSION=`./compiler/ppc386 -iV`
 $ sudo mv /usr/local/lib/fpc/$INSTALLED_VERSION/ppcrossppc /usr/local/lib/fpc/$INSTALLED_VERSION/ppcppc

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

To Windows, Linux and others

The package manager fink has packages for crosscompiling to windows, linux, freebsd mainly for Intel Macs, but some for PowerPC Macs, too.

$ fink install fpc-cross-i386-win32

or

$ fink install fpc-cross-arm-linux

install the crosscompilers.

To compile use these commands:

 fpc -Twin32 FILENAME
 fpc -Parm -Tlinux FILENAME

The current list with fpc 2.4.4 is:

 fpc-cross-arm-linux
 fpc-cross-arm-wince
 fpc-cross-i386-darwin
 fpc-cross-i386-freebsd
 fpc-cross-i386-go32v2
 fpc-cross-i386-linux
 fpc-cross-i386-win32
 fpc-cross-i386-wince
 fpc-cross-powerpc-darwin
 fpc-cross-powerpc-linux
 fpc-cross-sparc-linux
 fpc-cross-x86-64-darwin
 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 always the same scheme: First, you need the corresponding 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 Unix 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.

The main problems are:

  1. You need libraries on the host computer (the computer you are crosscompiling from)
  2. There is not one such set libraries as a result of more complex and dynamic Unix versioning
  3. A lot more (linker) parameters and tweaking is required, which are hard to explain for a general case.

If you still persist after this warning, please have a look at the crossnotes document mentioned further up, or the buildfaq.

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

There is a general FAQ in pdf format about how to build and configure FPC: buildfaq

Cross compiling the LCL

Test if you have installed the cross compiled fpc correctly:

Create a hello world program test.pas: <Delphi> program test; begin

 writeln('DATE ',{$i %DATE%});
 writeln('FPCTARGET ',{$i %FPCTARGET%});
 writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
 writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
 writeln('FPCVERSION ',{$i %FPCVERSION%});

end. </Delphi>

And compile it with

fpc -Twin32 -Pi386 test.pas

Replace win32 and i386 with your targets (ex: for Windows 64Bit use win64 and x86_64 respectively "fpc -Twin64 -Px86_64 test.pas"). The program fpc is wrapper that searches the right compiler for the target and executes it.

If this does not work, your cross compiler was not installed correctly. When this works you can cross compile the LCL.

From the IDE:

  • Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
  • Then open Tools / Configure Build Lazarus / Advanced Build Options.
  • Set Target OS and Target CPU.
  • Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
  • Click the Build button.

The LCL will be cross compiled to the new target. The new ppu files are put into their own directories, so the LCL for your normal OS is untouched.

Cross compiling LCL applications

You first need to cross compile the LCL. See above.

Cross compiling applications means: compiling plus linking. When you have cross compiled the LCL the compilation part is easy. Just set in the compiler options of the IDE the Target OS and Target CPU. The tricky part is the linking. If you cross compile a project you may see something like this:

/usr/local/bin/arm-linux-ld: cannot find -lX11

This means you have to install the graphical libraries of the target system. This has nothing to do with FPC/Lazarus, but with cross compiling a library. Some distributions provides pre compiled packages for this. For example Microsoft provides cross compiled libraries for WinCE for Windows. Under Linux you can install Wine to cross compile from Linux to Windows. Some Linux distributions provide 64bit libraries for 32bit systems.