iPhone/iPod development

From Lazarus wiki
Jump to navigationJump to search

Statement from Apple

Apple회사의 발표=

Apple has relaxed all restrictions on the development tools used to create iOS apps, as long as the resulting apps do not download any code.

애플은 iOS의 앱(apps)을 개발하기 위하여 사용되는 개발툴의 모든 제한을 해제하였다. 물론 앱 결과물이 어떠한 (악의적?)코드를 다운로드하지 않는다는 조건하에서 이다. [1]

Before this change it was not legally possible to ship FPC-compiled applications for iPhone OS 4.0 and later. 이러한 변화이전에 iPhone OS 4.0과 그 이후버젼을 위한 FPC로 컴파일된 응용프램그램의 배포가 법적으로 가능한 것은 아니다.

Installation

Requirements:

  • Intel Mac (iPhone SDK requirement)
  • Mac OS X 10.5.5 or later (iPhone SDK requirement)
  • iPhone SDK 2.x or later installed (tested with 2.2 - 3.0). Warning: there is a bug in the iPhone SDK 3.0 linker. See below for more information.

Some features:

  • Simulator support
  • the Xcode template transparently supports (requires, actually) integrating Pascal and Objective-C source files into a single application
  • Pascal translation of the OpenGL ES 1.1 C-header from the SDK (this one we can distribute, because it's licensed under an SGI open source license), available in the gles11 unit

Please read the entire ReadMe.rtf on the installer disk image before asking for help. It contains quite a bit of information on things to watch out for (especially when using the Simulator), and how to change the included template when adding extra Pascal source files.

Note that unlike other FPC releases, this installer will install the FPC sources and a script, and will then launch this script in order to compile FPC (after you tell it where you installed the iPhone SDK, so it can generate a Pascal version of some required C headers). This modus operandi is necessary to comply with the iPhone SDK agreement's stipulation that no derived works of the SDK may be distributed. If the script aborts for some reason, you can find it at /Developer/FreePascalCompiler/2.4.0/InstallScript/finish_fpc_iphone_install.command to relaunch it.

There are two installation options, but both require FPC 2.4.0 for Mac OS X/i386 (65MB) to be installed first:

The main difference between the two is that the FPC 2.5.1 snapshot uses VFP instructions instead of software emulation for floating point calculations, and that the MacOSAll unit is available for iPhoneOS, to the extent that its functionality exists on that platform (with interfaces to, a.o., CoreFoundation and CoreAudio). This snapshot also contains support for Objective-Pascal, but it does not yet automatically convert the iPhone-specific headers (and still contains various small errors in the Foundation translations).

Note that to use the FPC 2.5.1 snapshot, you should use the included new 2.5.1 Xcode template. This template has been modified to make it trivial to switch to a different FPC version using the same project file, so that starting a project file from scratch should no longer be necessary in the future. See the included readme file for information on how to switch the used FPC version.

If you encounter problems with the script that is launched when the installer finishes the installation, please always provide the entire contents of the Terminal window in which the script was executed when asking for help.

Update: A small error has been found in the new Xcode template included in the above 2.5.1 template: it always passes -Cfvfpv2 to the compiler, which will cause it to give an error when not compiling for ARM (it selects the VFPv2 floating point unit, which does not exist for Intel). For now, you will have to remove this parameter from the FPC_COMMON_OPTIONS when compiling for the simulator, and re-add it when compiling for the device. See the readme for information on where to find this setting.

(Xcode 4 compatibility: It appears the template works for Xcode 3 only, but not Xcode 4.)

iPhone SDK 3.0

The linker included with the iPhone SDK 3.0 contains a bug that is triggered by the Xcode template included with FPC. You can work around this bug by adding -Wl,-no_order_inits to the "Other Linker" flags in the Build settings of the project. This is already done for the Xcode templates included in the 2.4.0rc1 download above, but you may have to do this manually if you are using a project based on a template from a previous snapshot.

The linker bug has been reported to Apple (rdar://problem/7120987), so it will hopefully be fixed in a future release of the iPhone SDK.

Apple's Licence limitations

Developing applications for iPhone/iPod

FPC cannot work around the requirement of needing an Apple-issued certificate to run self-written programs on an iPhone/iPod Touch. So you have to sign up to Apple's iPhone Developer program ($99/year) to run FPC-compiled programs on the real hardware (just like for regular Xcode-compiled programs).

Without such a certificate, you can only run programs on the simulator (possibly unless you have a jailbroken iPhone/iPod Touch, but in that case you are on your own).

Read more about Apple's iPhone development program here: http://developer.apple.com/iphone/program/

Pascal headers required for developing

It is against the iPhone SDK agreement to distribute derivative works of this SDK, and this includes Pascal translations of framework headers. So it would be quite a challenge (if possible at all) to create and distribute the sources of a pure Pascal program with a GUI that does not violate the iPhone SDK agreement terms at this time. See the ReadMe.rtf (included in the .dmg above) file for more information

Technical Development Limitations

At this time is not known whether and if so how it is possible to copy an application to an iPhone/iPod Touch and to execute it without using Xcode. It's of course possible to write the application using any IDE available (Lazarus, Lightweight IDE, etc) and it should also be possible to make any of them work with the Simulator. However, any application has to be built by Xcode for it to be uploaded to the Device and to debug it.

iPhone Simulator is not iPhone

The iPhone Simulator makes the iPhone OS API in available to regular Mac applications, thereby mimicking the environment that the devices provide. By allowing you to run your applications in Mac OS X, the simulator provides a way to quickly test your application’s functionality without the need for an actual device. However, running applications on the simulator is not the same as running them on devices.

The simulator uses Mac OS X versions of the low-level iPhone OS frameworks instead of the versions that run on devices. In general, the simulator is a good tool for performing initial testing of your application. Keep however in mind that, because the simulator does not emulate device functionality, you must always perform final testing and performance analysis of your application on actual devices. The main reason is that programs running under the simulator can also transparently use any other frameworks/functionality used by the host Mac OS X operating system, which may not be available on the device.

Compiling and running pure Pascal applications on an iPhone/iPod Touch

Plain command line applications work fine on the iPhone/iPod Touch, except that the output will only be visible in the gdb console (if run or debugged from Xcode; press cmd-shift-r, or select Run->Console) or in the device's console log (if started from the iPhone/iPod Touch itself). In case of GUI applications, you will probably require non-distributable header translations due to licensing restrictions as outlined above. You can however still use your own private Pascal header translations (possible generated by an automated tool).

Using only Xcode

  1. Create a new project based on the template
  2. Remove all files except for *Prefix.pch, Info.plist, Frameworks and Products from the project
  3. Add your own Pascal sources (Project->Add to Project...)
  4. Double-click on the project icon at the top left in the Xcode file list, go to the Build tab, and change the FPC_MAIN_FILE setting to the name of the test program you added (for all configurations).
  5. Build the program

Combining command-line compilation and Xcode

If you compile a program on the command line (for Darwin/ARM) and copy it over the built binary in an Xcode project, then if you "run" the program in Xcode, it will code sign and run the binary that you copied over rather than the original program (just make sure that the binary in Xcode has been compiled recently so there are no changes that trigger Xcode to relink it). You can do this using a project based on the default template, without changing anything to the project.

You can find the Xcode binary's location by opening the Products folder in the project overview, right-clicking (ctrl-clicking) on the .app file and selecting "Reveal in Finder". The binary's location is then in program.app/Contents/MacOS (replace program.app with the actual name of the folder selected in the finder). Just overwrite whatever file is in this MacOS folder with the program you compiled (e.g. using the command line cp program).

To compile for Darwin/ARM on the command line (replace "Developer" with the directory in which you installed the SDK):

ppcarm -FD/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin -XR/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.0.sdk program.pas

To compile for the simulator, replace "ppcarm" with "ppc386", and the -XR parameter with the path to a simulator SDK (e.g., -XR/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk).

Compiling and running pure Pascal applications on iPhone simulator

Compiling and linking requires a regular ppc386 compiler. There is little difference between an iPhone Simulator and a regular Mac OS X application, except that iPhoneSim applications are linked to different frameworks compared to regular Mac OS X applications.

Some frameworks are iPhone-specific (UIKit), other frameworks exist for both Mac OS X and the iPhoneOS (CoreFoundation, CoreGraphics, etc). The iPhoneSim SDK provides its own versions of these frameworks. In order to ensure that the SDK's versions of these frameworks are used, you have to to tell the compiler to use appropriate SDK (just like when compiling applications for a real device):

 fpc simtest.pas -XR/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk

where /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.0.sdk is the root of an iPhoneSim SDK folder. This directory can vary depending on where you have installed the iPhone SDK.

There are a at least 3 versions of iPhone SDK available: 2.2.1, 3.0, 3.1. Depending on the version you need, the path changes:

  • SDK version 2.2.1: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk
  • SDK version 3.0: /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.0.sdk

etc.

Building an ARM Cross-compiler

The downloadable FPC/iPhone package contains a ppcarm cross-compiler to build iPhone applications. Should you wish to compile a newer version of the compiler and units from the latest svn sources later on, you can follow these steps:

1) copy /Developer/FreePascalCompiler/2.4.0/Source/rtl/darwin/arm/sig_cpu.inc to the rtl/darwin/arm directory of your svn checkout (that file was generated when you installed the FPC iPhone SDK integration kit)

2) in the top level fpc directory of your checkout, execute the following commands (in the first line, replace "/Developer" with the name of the actual directory where you installed the iPhone SDK):

 export IPHONEPLATFORMBASEDIR=/Developer/Platforms/iPhoneOS.platform/Developer
 make FPC=/usr/local/lib/fpc/2.4.2/ppc386 CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONEPLATFORMBASEDIR}/SDKs/iPhoneOS4.2.sdk/ -ap -Cfvfpv2" all

The above is for an svn trunk checkout. When building FPC 2.4.x, remove the -Cfvfpv2, as that version does not support VFP yet.

3) to install it afterwards:

 sudo make FPC=`pwd`/compiler/ppcrossarm OPT="-ap" CPU_TARGET=arm CROSSOPT="-FD${IPHONEPLATFORMBASEDIR}/usr/bin -XR${IPHONEPLATFORMBASEDIR}/SDKs/iPhoneOS4.2.sdk/ -ap" install CROSSINSTALL=1
 sudo ln -sf ../lib/fpc/2.5.1/ppcrossarm /usr/local/bin/ppcarm

(possibly with an INSTALL_PREFIX=xyz at the end of the "sudo make" line to install it under "xyz" rather than under /usr/local -- in that case, also adjust the "ln" command afterwards)

More information