Difference between revisions of "Setup Cross Compile For ARM"

From Free Pascal wiki
Jump to navigationJump to search
m
Line 1: Line 1:
 
{{Translate}}<!--{{Setup Cross Compile For ARM}}-->
 
{{Translate}}<!--{{Setup Cross Compile For ARM}}-->
  
== Cross Compile with FPC for ARM (e.g. Zaurus) ==
+
== How To setup Lazarus/FPC for arm-linux cross compiling. ==
 +
 
 +
With this tutorial you can make a binary file that runs on arm-linux devices. This is tested with a Nokia N900 with "[http://maemo.org/ Maemo 5 OS]" (the gnu/linux Debian based OS, developed by [http://maemo.nokia.com/videos/introducing-maemo-5/ Nokia]. It is an arm-linux device that runs GTK2 and QT4.6).
 +
If you need simply console programs, after [[#FPC for ARM]] section skip to [[#APPENDIX A: Testing console programs]].
 +
 
 +
'''NOTE 1''': All procedures are tested on a Virtual Machine with '''Ubuntu 9.10 - x86''' with Windows 7 as host and VirtualBox 3.0.12 as virtual machine. It is a fresh install, with all updates at today.
 +
 
 +
'''NOTE 2''': I don't know if this tutorial follows the better way to do all things... I'm not big expert about linux dev or commands, so please help me to improve this guide! You can discuss and repost [http://forum.lazarus.freepascal.org/index.php/topic,8084.0.html HERE]
 +
 
 +
'''NOTE 3''': Replace every "/home/user/" with your real user path. All other paths are simply for explanation.
 +
 
 +
 
 +
----
 +
 
 +
 
 +
== Generic things to do ==
 +
 
 +
=== Downloads ===
 +
*any starting compiler from the 2.2.4 branch (e.g. fpc-2.2.4-20091214.i386.rpm) from [http://www.hu.freepascal.org/lazarus HERE] or [http://snapshots.lazarus.shikami.org/ HERE]). It is needed to compile >= 2.2.4 fpc sources
 +
*latest binutils.tar.gz (e.g. binutils-2.20.tar.gz) from [http://ftp.gnu.org/gnu/binutils/ HERE]
 +
*latest [http://ftp//ftp.freepascal.org/pub/fpc/snapshot/trunk/source/fpc.zip fpc.zip] from [http://www.freepascal.org/develop.var HERE]
 +
*latest Lazarus source zip (e.g. lazarus-0.9.29-23129-20091214-src.zip) from [http://www.hu.freepascal.org/lazarus [http://snapshots.lazarus.shikami.org/ HERE] or HERE] (faster for me)
 +
*target libs (You can grab from SDK or from arm-device. ''See "[[#APPENDIX A: Testing console programs]]"'')
 +
 
 +
=== Unpack all sources file here ===
 +
~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
 +
~/fpc_tools/binutils-2.20/
 +
~/lazarus/
 +
~/lazarus/fpc/2.5.1/
 +
 
 +
=== Prepare Target Libs ===
 +
*See "[[#APPENDIX A: Testing console programs]]" to know how/where to find target libs
 +
*Copy all target libs to ~/lazarus/fpc/libcross
 +
 
 +
=== Install dependencies ===
 +
  sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev
 +
 
 +
== Install & Configure Binutils ==
 +
 
 +
=== Compile binutils for arm-linux ===
 +
*If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
 +
*Then type:
 +
cd ~/fpc_tools/binutils-2.20
 +
./configure --target=arm-linux --disable-werror
 +
make
 +
sudo make install
 +
mkdir ~/lazarus/fpc/binutils
 +
ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
 +
ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
 +
sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
 +
 
 +
sudo gedit /usr/local/bin/arm-linux-as
 +
 
 +
*Paste:
 +
 
 +
#!/bin/sh
 +
/usr/local/bin/arm-linux-as_org -meabi=5 $@
 +
 
 +
'''NOTE''': option ''-meabi=5'' force assembler to make binary ''eabi v5'' compatible. E.g. if you need eabi v4 you will change it in ''-meabi=4''.
 +
 
 +
*Save and close gedit
 +
*Type:
 +
 
 +
sudo chmod +x /usr/local/bin/arm-linux-as
 +
ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as
 +
 
 +
== FPC ==
 +
 
 +
=== Install precompiled fpc 2.2.4 branch ===
 +
*Type:
 +
sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
 +
*Be sure that ppc386 is right installed, type:
 +
which ppc386
 +
if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)
 +
=== Compile FPC from sources ===
 +
*Type:
 +
cd ~/lazarus/fpc/2.5.1
 +
make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
 +
sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
 +
sudo rm /usr/bin/ppc386
 +
sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
 +
sudo mkdir /usr/share/fpcsrc/
 +
sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
 +
sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc
 +
== FPC for ARM ==
 +
=== Make FPC able to cross compile for arm-linux ===
 +
*Type:
 +
cd ~/lazarus/fpc/2.5.1/
 +
sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr
 +
=== Create custom fpc.cfg ===
 +
*Type:
 +
gedit ~/.fpc.cfg
 +
*Paste in ~/.fpc.cfg:
 +
#INCLUDE /etc/fpc.cfg
 +
#DEFINE DEMOTEST
 +
#DEFINE DEMOTEST1
 +
#DEFINE LAZARUS
 
   
 
   
* First, setup a cross compilation environment; you can get more information about this here [http://www.cartel-securite.fr/pbiondi/zaurus/crosscompile.html], here [http://www.nautilus6.org/operation/doc/wide-memo-nautilus6-zaurus-ccenv-setup-00.txt] and here [http://projects.buici.com/arm/cross/].
+
-Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
* Install the lastest native fpc [http://sourceforge.net/project/showfiles.php?group_id=89339 from sourceforge]
+
-Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/
* get an arm fpc snapshot from the downloads page of [http://users.pandora.be/Jan.Van.hijfte/qtforfpc/qtedemo.html FPC Qt Binding for Zaurus]
+
* Download it to somedir. The name of the tar file may be different ofcourse
+
-a
<tt>
+
-Sd
cd somedir<br>
+
-Xd
tar zxvf arm-linux-fpc.i386-linux-20050217.tar.gz
+
-Xs
</tt>
+
* you will find a 'lib/fpc/1.9.7/units/arm-linux/...' dir. The version may be '1.9.8' or higher.
+
-O-
* create FPC library directory structure for fpc in a lib dir on your system
+
* most systems use '/usr/local/lib', some use '/usr'
+
#IFDEF CPUARM
<tt>
+
-XP/home/user/lazarus/fpc/binutils/
mkdir -p /usr/local/lib/fpc/1.9.7/units<br>
+
-Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
cp -fr lib/fpc/1.9.7/units/arm-linux /usr/local/lib/fpc/1.9.7/units/
+
-Xr/home/user/lazarus/fpc/libcross
</tt>
+
-XR/home/user/lazarus/fpc/
* copy the fpc cross compiler to its default place
+
-darm
<tt>
+
-Tlinux
cp lib/fpc/1.9.7/ppcarm /usr/local/lib/fpc/1.9.7
+
#ENDIF
</tt>
+
 
* set a soft link to it in a bin dir in your path
+
*save and close gedit
* most distributions use '/usr/local/bin', some '/usr/bin'
+
*Type:
<tt>
+
sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm
ln -sf /usr/local/lib/fpc/1.9.7/ppcarm /usr/local/bin/ppcarm
+
== Lazarus ==
</tt>
+
If you don't need graphical programs, you can skip this section and go directly to [[#Appendix B: Target libs]]
* verify you can call ppcarm, it should return something like '1.9.7' and not 'command not found'
+
=== Compile Lazarus ===
<tt>
+
*Type:
ppcarm -iV
+
cd ~/lazarus
</tt>
+
make clean all
* download [ftp://ftp.freepascal.org/pub/fpc/snapshot/v19/arm-linux/binutils.tar.gz FPC 1.9.x ARM binutils] to somedir
+
=== Start Lazarus ===
<tt>
+
*Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
cd somedir<br>
+
*Type:
tar zxvf binutils.tar.gz<br>
+
./lazarus
</tt>
+
=== Configure Lazarus for corss Compile ===
* copy the 2 programs in the tar to a bin dir in your path
+
*Now you must cross compile the LCL and Package Registration, this part come from [http://wiki.lazarus.freepascal.org/Cross_compiling#Cross_compiling_the_LCL HERE].
<tt>
+
*From the IDE:
cp arm-linux-* /usr/local/bin<br>
+
*#Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
</tt>
+
*#Then open Tools / Configure Build Lazarus / Advanced Build Options.
* create a 'Hello World' program and compile it
+
*#Set Target OS as linux and Target CPU as arm ().
<tt>
+
*#Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
echo "program hello; begin writeln('Hello World'); end." > hello.pas<br>
+
*#Click the Build button.
ppcarm -XParm-linux- hello.pas<br>
+
== Make your first arm-linux project in Lazarus ==
</tt>
+
=== New Project ===
* output should be something like this:
+
*Start new project as application
 +
*Put some component in main form to test (like button, memo and few other things)
 +
=== Set Targets ===
 +
*From the IDE:
 +
*#Set in Project / Compiler Options / Code / Target OS (-T): ''Linux''
 +
*#Set in Project / Compiler Options / Code / Target CPU family (-P): ''arm''
 +
=== Add terget libs to your project ===
 +
*From the IDE:
 +
*#Set in Project / Compiler Options / Linking / Options (-k): ''-L/home/user/lazarus/fpc/libcross''
 +
*#Set in Project / Compiler Options / Compilation / Execute after / Command: ''arm-linux-strip -s /home/kjow/dev/prova/prova''
 +
=== Build Project ===
 +
*Build Project as normal:
 +
*#Hit keys Ctrl+F9
 +
*#Run / Build
 +
=== Time to run ===
 +
*Transfer the binary of your project on device
 +
*Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
 +
chmod +x /path/where/is/YourProject
 +
./path/where/is/YourProject
 +
 
 +
== APPENDIX A: Testing console programs ==
 +
 
 +
=== Create a TestARM.pas ===
 +
*Type:
 +
mkdir ~/dev/TestARM/
 +
gedit ~/dev/TestARM/TestARM.pas
 +
*Paste this code:
 +
program test;
 +
begin
 +
  writeln('DATE ',{$i %DATE%});
 +
  writeln('FPCTARGET ',{$i %FPCTARGET%});
 +
  writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
 +
  writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
 +
  writeln('FPCVERSION ',{$i %FPCVERSION%});
 +
end.
 +
*Type:
 +
cd ~/dev/TestARM/
 +
fpc -Twindows -Pi386 TestARM.pas
 +
=== Time to run ===
 +
*Transfer TestARM on device
 +
*Generally you must give executable permission to ''TestARM'' directly on the device:
 +
chmod +x /path/where/is/TestAMR
 +
./path/where/is/TestAMR
  
  # ppcarm -XParm-linux- hello.pas
+
== Appendix B: Target libs ==
  Free Pascal Compiler version 1.9.7 [2005/02/17] for arm
+
 
  Copyright (c) 1993-2005 by Florian Klaempfl
+
You need target libs to assemble and link binary file. You can find them into the SDK of your device or into the device itself.
  Target OS: Linux for ARM
+
 
  Compiling hello.pas
+
E.g. for Nokia N900 you need to copy /lib/* and /usr/lib/* (no subdirs) and merged to ~/lazarus/fpc/libcross/ then you had to rename each library ''not found'' by linker (ld) to the file name searched by linker itself.
  Assembling hello
+
I mean, if you got this error
  Linking hello
+
 
  1 Lines compiled, 0.2 sec
+
  libX11.so not found (or incompatible)
   
+
 
* Copy it to the home directory of the user 'zaurus' on your zaurus device.
+
and you have in /usr/lib/ a file called ''libX11.so.6.2.0'' you need to rename it into ''libX11.so''
* With a ssh setup this could be as simple as:
+
 
<tt>scp hello zaurus:/home/zaurus</tt>
+
 
* Install terminal program on your Zaurus. For a Zaurus SL6000: see manual on CD.
+
 
* Run the terminal program. On a SL6000 the console will default to user 'zaurus' in its home dir /home/zaurus
+
[[User:Kjow|Kjow]] 18:42, 16 December 2009 (CET)
* Run the program
 
<tt>
 
./hello
 
</tt>
 
* Output should be something like:
 
Hello World
 

Revision as of 19:42, 16 December 2009

Template:Translate

How To setup Lazarus/FPC for arm-linux cross compiling.

With this tutorial you can make a binary file that runs on arm-linux devices. This is tested with a Nokia N900 with "Maemo 5 OS" (the gnu/linux Debian based OS, developed by Nokia. It is an arm-linux device that runs GTK2 and QT4.6). If you need simply console programs, after #FPC for ARM section skip to #APPENDIX A: Testing console programs.

NOTE 1: All procedures are tested on a Virtual Machine with Ubuntu 9.10 - x86 with Windows 7 as host and VirtualBox 3.0.12 as virtual machine. It is a fresh install, with all updates at today.

NOTE 2: I don't know if this tutorial follows the better way to do all things... I'm not big expert about linux dev or commands, so please help me to improve this guide! You can discuss and repost HERE

NOTE 3: Replace every "/home/user/" with your real user path. All other paths are simply for explanation.




Generic things to do

Downloads

Unpack all sources file here

~/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
~/fpc_tools/binutils-2.20/
~/lazarus/
~/lazarus/fpc/2.5.1/

Prepare Target Libs

Install dependencies

 sudo apt-get install -y alien libncurses5-dev libXp-dev libgtk2.0-dev

Install & Configure Binutils

Compile binutils for arm-linux

  • If not already done, extract binutils-2.20.tar.gz in ~/fpc_tools/binutils-2.20
  • Then type:
cd ~/fpc_tools/binutils-2.20
./configure --target=arm-linux --disable-werror
make
sudo make install
mkdir ~/lazarus/fpc/binutils
ln -s /usr/local/bin/arm-linux-ar ~/lazarus/fpc/binutils/ar
ln -s /usr/local/bin/arm-linux-ld ~/lazarus/fpc/binutils/ld
sudo mv /usr/local/bin/arm-linux-as /usr/local/bin/arm-linux-as_org
sudo gedit /usr/local/bin/arm-linux-as
  • Paste:
#!/bin/sh
/usr/local/bin/arm-linux-as_org -meabi=5 $@

NOTE: option -meabi=5 force assembler to make binary eabi v5 compatible. E.g. if you need eabi v4 you will change it in -meabi=4.

  • Save and close gedit
  • Type:
sudo chmod +x /usr/local/bin/arm-linux-as
ln -s /usr/local/bin/arm-linux-as ~/lazarus/fpc/binutils/as

FPC

Install precompiled fpc 2.2.4 branch

  • Type:
sudo alien -i -c /home/user/fpc_tools/fpc_2.2.4/fpc-2.2.4-20091214.i386.rpm
  • Be sure that ppc386 is right installed, type:
which ppc386

if ppc386 is in the path it returns the path (e.g. /usr/bin/ppc386)

Compile FPC from sources

  • Type:
cd ~/lazarus/fpc/2.5.1
make all OPT='-gl -O3p3' PP=/usr/bin/ppc386 (add NOGDB=1 at the end if asked)
sudo make install PP=/usr/bin/ppc386 PREFIX=/usr/ (add NOGDB=1 at the end if asked)
sudo rm /usr/bin/ppc386
sudo ln -s /usr/lib/fpc/2.5.1/ppc386 /usr/bin/ppc386
sudo mkdir /usr/share/fpcsrc/
sudo ln -sf ~/lazarus/fpc/2.5.1/ /usr/share/fpcsrc/
sudo /usr/lib/fpc/2.5.1/samplecfg /usr/lib/fpc/2.5.1/ /etc

FPC for ARM

Make FPC able to cross compile for arm-linux

  • Type:
cd ~/lazarus/fpc/2.5.1/
sudo make crossinstall CPU_TARGET=arm OS_TARGET=linux CROSSBINDIR=/home/user/lazarus/fpc/binutils/ OPT=-dFPC_ARMEL INSTALL_PREFIX=/usr

Create custom fpc.cfg

  • Type:
gedit ~/.fpc.cfg
  • Paste in ~/.fpc.cfg:
#INCLUDE /etc/fpc.cfg
#DEFINE DEMOTEST
#DEFINE DEMOTEST1
#DEFINE LAZARUS

-Fu/usr/lib/fpc/2.5.1/units/$fpctarget/*
-Fl/usr/lib/fpc/2.5.1/units/$fpctarget/rtl/

-a
-Sd
-Xd
-Xs

-O-

#IFDEF CPUARM
-XP/home/user/lazarus/fpc/binutils/
-Xr/usr/lib/fpc/2.5.1/units/arm-linux/rtl/
-Xr/home/user/lazarus/fpc/libcross
-XR/home/user/lazarus/fpc/
-darm
-Tlinux
#ENDIF
  • save and close gedit
  • Type:
sudo ln -sf /usr/lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm

Lazarus

If you don't need graphical programs, you can skip this section and go directly to #Appendix B: Target libs

Compile Lazarus

  • Type:
cd ~/lazarus
make clean all

Start Lazarus

  • Start Lazarus to see that everything is ok and then to compile LCL and Package Registration
  • Type:
./lazarus

Configure Lazarus for corss Compile

  • Now you must cross compile the LCL and Package Registration, this part come from HERE.
  • From the IDE:
    1. Set in Environment / Options .. / Environment / Files the Compiler path to the path to fpc. Normally this is already done.
    2. Then open Tools / Configure Build Lazarus / Advanced Build Options.
    3. Set Target OS as linux and Target CPU as arm ().
    4. Set LCL and Package registration to Build (the middle radio button) and all other to None (left radio buttons).
    5. Click the Build button.

Make your first arm-linux project in Lazarus

New Project

  • Start new project as application
  • Put some component in main form to test (like button, memo and few other things)

Set Targets

  • From the IDE:
    1. Set in Project / Compiler Options / Code / Target OS (-T): Linux
    2. Set in Project / Compiler Options / Code / Target CPU family (-P): arm

Add terget libs to your project

  • From the IDE:
    1. Set in Project / Compiler Options / Linking / Options (-k): -L/home/user/lazarus/fpc/libcross
    2. Set in Project / Compiler Options / Compilation / Execute after / Command: arm-linux-strip -s /home/kjow/dev/prova/prova

Build Project

  • Build Project as normal:
    1. Hit keys Ctrl+F9
    2. Run / Build

Time to run

  • Transfer the binary of your project on device
  • Generally you must give executable permission to the binary directly on the device. If you work in terminal type:
chmod +x /path/where/is/YourProject
./path/where/is/YourProject

APPENDIX A: Testing console programs

Create a TestARM.pas

  • Type:
mkdir ~/dev/TestARM/
gedit ~/dev/TestARM/TestARM.pas
  • Paste this code:
program test;
begin
 writeln('DATE ',{$i %DATE%});
 writeln('FPCTARGET ',{$i %FPCTARGET%});
 writeln('FPCTARGETCPU ',{$i %FPCTARGETCPU%});
 writeln('FPCTARGETOS ',{$i %FPCTARGETOS%});
 writeln('FPCVERSION ',{$i %FPCVERSION%});
end.
  • Type:
cd ~/dev/TestARM/
fpc -Twindows -Pi386 TestARM.pas

Time to run

  • Transfer TestARM on device
  • Generally you must give executable permission to TestARM directly on the device:
chmod +x /path/where/is/TestAMR
./path/where/is/TestAMR

Appendix B: Target libs

You need target libs to assemble and link binary file. You can find them into the SDK of your device or into the device itself.

E.g. for Nokia N900 you need to copy /lib/* and /usr/lib/* (no subdirs) and merged to ~/lazarus/fpc/libcross/ then you had to rename each library not found by linker (ld) to the file name searched by linker itself. I mean, if you got this error

libX11.so not found (or incompatible)

and you have in /usr/lib/ a file called libX11.so.6.2.0 you need to rename it into libX11.so


Kjow 18:42, 16 December 2009 (CET)