Installing FPC for iPhone (including FPC 2.5.1 for OSX with Objective Pascal support).

by Ingemar Ragnemalm

Preliminary!

Installing FPC for iPhone is quite a challenge. This is, of course, because it is a work in progress, but also because there are some legal issues that call for special fixes, converting ObjC headers locally.

Note: Many commands require specific versions of various software! References to FPC 2.4.2, iOS SDK 4.3 etc must be modified if you have other versions. This is all I need to install, if you don't have the same versions it will not be exactly what you need - but I still hope it can be helpful.

The text covers both Mac and iOS compilers, and the steps are marked depending on the target. If you want both, do all steps.

Prerequisites

This guide assumes that you have:

If you have other versions you will need to modify commands below! "/usr/local/lib/fpc/2.4.2" should be changed to whatever fpc version you have, "iPhoneOS4.3.sdk" to match your iPhone SDK etc.

Checkout the source to FPC to Developer/ObjectivePascal/fpc (All).

Note: The line below checks out revision 17214 since it works for me. You will want later revisions in the future. To get the latest, skip the -r 17214 part. However, I had trouble with later versions so 17214 is the "safe" revision. I have also tried revision 17324, which worked up to a point (Mac compilers worked, iPhone not).

sudo svn co -r 17214 http://svn.freepascal.org/svn/fpc/trunk /Developer/ObjectivePascal/fpc

Patch a problem in t_bsd.pas (iOS):

You need to make a small change in this file:

/Developer/ObjectivePascal/fpc/compiler/systems/t_bsd.pas

At line 345, make this change:

LinkRes.Add('arm');

to

LinkRes.Add(lower(cputypestr[current_settings.cputype]));

Add a vital missing file (iOS):

The file sig_cpu.inc must be generated. It can not be included for legal reasons. To do this, run this script (a modified version of a script by Jonas Maebe) (part of an installer linked from http://wiki.freepascal.org/iPhone/iPod_development):

sudo sh sigcpuscript.sh

Correct a bug in the Mac interfaces (Mac):

itemAtIndex is missing in NSMenu.inc (/Developer/ObjectivePascal/fpc/packages/cocoaint/src/appkit/NSMenu.inc).

function itemAtIndex(index: NSInteger): NSMenuItem; message 'itemAtIndex:';

Compile and install the Mac compilers (Mac):

(Note: An official FPC version must be installed. I use FPC 2.4.2. If you have another version, you must change the references to 2.4.2 below to what you have.)

Intel compiler (the most important one):

cd /Developer/ObjectivePascal/fpc

sudo make distclean -j 3

sudo make FPC=/usr/local/lib/fpc/2.4.2/ppc386 OPT="-ap" distclean all -j 2

sudo make FPC=`pwd`/compiler/ppc386 install

PPC cross-compiler (skip if you don't intend to make universal binaries):

sudo make distclean -j 3

sudo make FPC=/usr/local/lib/fpc/2.4.2/ppc386 OPT="-ap" distclean CPU_TARGET=powerpc all -j 2

sudo make FPC=`pwd`/compiler/ppcppc CROSSINSTALL=1 install

sudo mv /usr/local/lib/fpc/2.5.1/ppcrossppc /usr/local/lib/fpc/2.5.1/ppcppc

Copy the Cocoa interfaces to the Units folder:

sudo cp -R /Developer/ObjectivePascal/fpc/packages/cocoaint/src /Developer/ObjectivePascal/Units

This is just to have them in a more convenient place.

Compile FPC for iPhone simulator (iOS):

sudo make FPC=/usr/local/lib/fpc/2.4.2/ppc386 OS_TARGET=iphonesim all

sudo make FPC=/usr/local/lib/fpc/2.4.2/ppc386 OS_TARGET=iphonesim install CROSSINSTALL=1

Compile FPC for iPhone (iOS):

export IPHONEPLATFORMBASEDIR=/Developer/Platforms/iPhoneOS.platform/Developer

sudo make FPC=/usr/local/lib/fpc/2.4.2/ppc386 CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONEPLATFORMBASEDIR}/SDKs/iPhoneOS4.3.sdk/ -ap -Cfvfpv2" all

sudo make FPC=`pwd`/compiler/ppcrossarm OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONEPLATFORMBASEDIR}/SDKs/iPhoneOS4.3.sdk/ -ap" install CROSSINSTALL=1

Create iOS interfaces (iOS):

If you don't have iOS version 4.2, you must edit /Developer/ObjectivePascal/fpc/packages/cocoaint/utils/make-ios-headers.sh to specify the proper iOS version.

cd /Developer/ObjectivePascal/fpc/packages/cocoaint/utils/

sudo ./make-ios-headers.sh

Copy uikit-skel to /Developer/ObjectivePascal

sudo cp -r uikit-skel /Developer/ObjectivePascal/.

Compile a program for the iPhone simulator (iOS):

There are two iPhone demos available, Hello World and iArkanoid. iArkanoid is by far the most advanced. You can download it from Ryan Joseph's page (see below). You will need to correct one bug (caused by an interface change at some point after it was written). The fix: Change NSSetPointer to NSSet for the "touches" methods.

/usr/local/lib/fpc/2.5.1/ppcross386 -Cirot -XX -Tiphonesim -Fu/Developer/ObjectivePascal/uikit-skel/src -XR/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk iArk.pas

Run in the simulator (iOS):

/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications/iPhone\ Simulator.app/Contents/MacOS/iPhone\ Simulator -SimulateApplication iArk.app/iArk

Compile for the iPhone/iOS device (iOS):

/usr/local/lib/fpc/2.5.1/ppcrossarm -Cirot -XX -Cfvfpv2 -Fu/Developer/ObjectivePascal/utils/uikit-skel/src -FD/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin -XR/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk myapp.pas

Run on the iPhone/iOS device (iOS):

To do this, I used codesign with the certificate created after registering with Apple. Then I dragged the program to iTunes to install.

Variations to consider

Above, I have avoided options and maybes the best I can to keep things straight. However, when you get into the process, or if you are more used to this kind of installations than I am, then you may want to consider some variations.

If you have other versions that I have, you can't do exactly as I do

First of all, if your version numbers don't match you must edit the command given to match. That's a kind of variation but really just corrections for your setup.

But there are some other variations to consider.

Backup your svn checkout to save time.

An svn checkout takes some time, and if you mess up your installation, you may wish to delete everything and start over. You can do that, but as a precaution to that situation, you can consider this: Make a checkout, and archive it while it is still "clean". And if you checkout more than one revision, keep each revision that way. It can save you time. Much better than trashing the whole checkout.

Backup your working installation

Speaking of backups, the /usr/local/lib/fpc/2.5.1 folder can be well worth backing up once in a while, when it contains something that is reasonably useful, like a working Mac compiler or even more. Then you don't need to fear damaging a working installation by mistyping a command.

Build in a different directory

You can consider making the checkout to a different directory. If you checkout to a folder in your home directory, a lot of sudo's can be skipped, saving you entering the password over and over. Then you need to change all references to /Developer/ObjectivePascal/fpc to whatever you use.

Skip unnecessary steps

Some steps can be skipped, like compiling the PPC compiler, in case you don't care about building universal binaries. In future releases, I expect many of the small corrections to be fixed and unnecessary. You may even skip both the 2.5.1 Mac compilers, if you don't need it or already have it, or install a newer revision for it.

Other installation tools and instructions

I have tried a number of other instructions, scripts and installers. What I write here is what worked for me, but it will certainly appear new installation scripts that will work for my setup. (See references below.)

More demos

I am developing more iPhone demos. Some are already in the current Lightweight IDE distribution, in early versions. They will be updated and expanded. In particular, my demos will be the place to go for nibless demos! For various reasons (portability being one) I mostly work nibless on the Mac and now on the iPhone. There will even be occasional nibless Objective C demos, but mostly FPC of course.

Disclaimer

This material works for me. That is all it does. I am not any expert in FPC's inner workings, not an FPC authority, I am just an enthusiastic FPC user who managed to make FPC work for iPhone and I am sharing that information for anyone who may benefit. There are other people who know a lot more about why I have to make the steps above (copyright problems, code parsers limitations, plain bugs...). This material will grow old and you should check out the relevant links below for information about new versions etc.

There can be mistakes. No guarantees. Although I have been through this installation several times, something may be out of order or mistyped. There can even be dangerous commands, although I assure you that any such mistakes are absolutely unintentional (and I hope they don't exist at all). I do want meaningful constructive feedback like corrections and additions, and if you want to use any of this material for expanding some other text on the subject you can feel free to do so. (Acknowledging this would be nice if it was worth anything to you.) I can also correct links, facts and omissions, refer to newer information... Actually, I usually care about all you say if you are nice.

but

I won't care about any whinings about me not following the Apple Way, about writing nibless code, not using Xcode and such things.

Acknowledgements

Thanks to Jonas Maebe and Phil Hess for the help in figuring this out!

References

The following page is the kind-of-official guide for installing FPC for iPhone, and will probably be the place to go in the future. I call it kind-of-official since it is a wiki, anonymously written by an unknown number of people. I know I could have entered my info there, but I have problems writing in wikis, messing up other people's text. Sorry about that.

http://wiki.freepascal.org/iPhone/iPod_development

You can download FPC here:

http://www.freepascal.org/download.var

You can get Xcode here (even older versions):

http://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wo/5.1.17.2.1.3.3.1.0.1.1.0.3.3.3.3.1

Ryan Joseph has demos and other FPC information:

http://www.thealchemistguild.org/pascal/

There is a main page for Objective Pascal here:

http://www.objectivepascal.com/

Phil Hess has written tutorials here:

http://web.me.com/macpgmr/ObjP/Xcode4/