DoK – Where Dreams Come From

C'è chi vede il bicchiere mezzo pieno e chi lo vede mezzo vuoto… Per me, l'acqua che manca, è traboccata fuori ;)

Build the toolchain for iPhone 2.0.x on MacOSX 10.5.x

Posted on | agosto 12, 2008 | 10 Comments

Hi guys !!!
this is another (!?!?!?!) document on how to compile a toolchain for iPhone (with firmware 2.0.x) on MaxOSX 10.5.x. First of all, why a new document ?!?!?!? Simple question and simple response: “Because other documents I found on Internet don’t work for me, so I decided to mix infoz from the NET and try to make my toolchain… :) and… finally it works !!!!!”

Most info about toolchain compilation I get from this good article from Saurik, NerveGas and Drudge.

First of all, you need terminal access to your Mac. If you don’t known how made an ssh/shell access, well… go away 😉
Now, I hope you have installed bison, flex and a svn client… It’s OK ?!?!? Do you have ?!?!? OK… let’s go !!

Create a new directory iPhoneDevel and go on with llvm.
Now, from shell:

svn co llvm-svn -r 42498
pushd llvm-svn
./configure --enable-optimized
sudo make install

If you want to known why you’re still relegate to release 42498 of llvm, you need to read these.

Now, it’s time to checkout some iPhone devel tools:

svn checkout iphone-dev
pushd iphone-dev
pushd include
svn switch
pushd odcctools
svn switch
sudo mkdir -p /usr/local/arm-apple-darwin
mkdir -p build/odcctools
pushd build/odcctools
../../odcctools/configure --target=arm-apple-darwin --disable-ld64

I compile for arm-apple-darwin platform, you can safely change this target with whatever you want ;).
Now, check if you have MacOSX 10.4u SDK (if not, simply download iphone SDK from Apple Site and install everything). If so, simply write this on shell:

export INCPRIVEXT="-isysroot /Developer/SDKs/MacOSX10.4u.sdk"

Now, compile party :)

sudo make install

Ok, first BIG step is gone. Now it’s time to get iPhone FileSystem and mount to /usr/local/share/iphone-filesystem. If you don’t known how to obtain correct File System, simply read this document.
I assume you mounted correctly undecrypted dmg image so, go on mounting iPhone FS and compiling toolchain.

sudo ln -s /Volumes/BigBear5A347.M68OS /usr/local/share/iphone-filesystem
export HEAVENLY=/usr/local/share/iphone-filesystem
pushd include
./configure --with-macosx-sdk=/Developer/SDKs/MacOSX10.4u.sdk
sudo bash
mkdir -p build/csu
pushd build/csu
../../csu/configure --host=arm-apple-darwin
sudo make install

By now I tried successfully to compile gcc 4.0 with MacOSX 10.4u headers, so document I wrote assume you have downloaded and correctly installed these. I try to compile gcc 4.2 with 10.5.x headers and, if all was ok, I update this guide… stay tuneeeeeeeeeeeeeeed :)

Let’s go:

mv llvm-gcc-4.0-iphone/configure llvm-gcc-4.0-iphone/configure.old
sed 's/^FLAGS_FOR_TARGET=$/FLAGS_FOR_TARGET=${FLAGS_FOR_TARGET-}/g' llvm-gcc-4.0-iphone/configure.old > llvm-gcc-4.0-iphone/configure
sudo ln -s /usr/local/arm-apple-darwin/lib/crt1.o \/usr/local/arm-apple-darwin/lib/crt1.10.5.o
mkdir -p build/llvm-gcc-4.0-iphone
pushd build/llvm-gcc-4.0-iphone
export FLAGS_FOR_TARGET="-mmacosx-version-min=10.1"
sh ../../llvm-gcc-4.0-iphone/configure --enable-llvm=`llvm-config --obj-root` \
--enable-languages=c,c++,objc,obj-c++ --target=arm-apple-darwin --enable-sjlj-exceptions \
--with-heavenly=$HEAVENLY --with-as=/usr/local/bin/arm-apple-darwin-as \

Now, it’s time to patch :)
If you don’t patch, you receive a lot of garbage errors like these:

../../../llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm:670:garbage following instruction -- `bls 11f'
../../../llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm:672:garbage following instruction -- `beq 12f'
make[2]: *** [libgcc/./_udivsi3_s.o] Error 1

Why you obtain these errors ?????? Read this page about issue #145.
How you can correct error ?!?!? Simply download this file and save it on iPhoneDevel directory and:

patch ../../llvm-gcc-4.0-iphone/gcc/config/arm/lib1funcs.asm ../../../lib1funcs.asm.diff

Now, final step :)

make LLVM_VERSION_INFO=2.0-svn-iphone-dev-0.3-svn
sudo make install


Now all it’s OK with toolchain. All files are installed on /usr/local/arm-apple-darwin/ and you can safely compile all sources you want.

Before you go out, some little tips :)

If you want to run your app on iPhone, you need to run “ldid -S YourApp” on your iPhone, otherwise you obtain “Killed” because your app need to be signed.

If you obtain a “Segmentation fault” error, a “Bus error” or a trap error on your iPhone when you try to launch your app, something went wrong with your main method. Remember, you need to use something like thah when you create your app:

int main(int argc, char *argv[])
NSAutoreleasePool* pool = [ [ NSAutoreleasePool alloc ] init ];
int AppReturn;
AppReturn = UIApplicationMain( argc, argv, [YourAppLicationName class] );
return AppReturn;
[ pool release ];

Finally, I suggest a Makefile like that when you try to compile :)

CC = /usr/local/bin/arm-apple-darwin-gcc-4.0.1
-I"/usr/local/arm-apple-darwin/include/"  \
-I"/usr/local/lib/gcc/arm-apple-darwin/4.0.1/include/" \
-fobjc-abi-version=2 \
-fobjc-abi-version=2 \
-lobjc  -lstdc++.6 -licucore -llockdown -lz.1 -lxml2.2 \
-framework CoreFoundation       \
-framework Foundation   \
-framework UIKit        \
-L"/usr/local/share/iphone-filesystem/usr/lib/" \
-F"/usr/local/share/iphone-filesystem/System/Library/Frameworks/"       \
all:    $(App)
$(App): main.o YourAppName.o
$(LD) $(LDFLAGS) -o $@ $^
%.o:    %.m
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
rm -f *.o $(App)

Now, it’s all :)

Good compiling :)


10 Responses to “Build the toolchain for iPhone 2.0.x on MacOSX 10.5.x”

  1. Alexei
    agosto 14th, 2008 @ 21:47

    Thanks for the instructions. Here are a couple mistakes:

    1. There is a > in the sed line instead of a >
    2. There is a < in the makefile instead of a <
    3. You must do a
    $ sudo make install
    at the very end (after the last make) in order to have the toolchain actually be installed.


  2. Alexei
    agosto 14th, 2008 @ 21:50

    Okay, so the form actually interprets html. So what I meant to say is that:
    1) there is a &gt; instead of > in the sed line
    2) there is a &lt; instead of < in the Makefile
    3) same as above


  3. Salvatore Ansani
    agosto 15th, 2008 @ 08:09

    10x Alexei,
    Fixed typos 😉


  4. devin
    agosto 29th, 2008 @ 00:20

    Thanks for your version of this tutorial. Very helpful.

    One question I have that you may be able to help me with, is how do you get this toolchain set up in Xcode? I’ve found some other resources on the net, but most of them are outdated and lacking proper instruction.

  5. Salvatore Ansani
    agosto 30th, 2008 @ 19:57

    Uhm.. I don’t use this toolchain in XCode, only from shell. I saw a tutorial about this question. I’ll post link on few hours. Stay tuned :)

  6. ddloro
    settembre 1st, 2008 @ 18:48

    well risking to be noobish but how do i install flex and bison? ^^

  7. 江右东流 » 搭建toolchain For iPhone 2.x
    ottobre 8th, 2008 @ 10:52

    […] 1、 2、 […]

  8. Bookmarks about Macosx
    novembre 7th, 2008 @ 18:30

    […] – bookmarked by 6 members originally found by walterarlenhenry on 2008-10-17 Build the toolchain for iPhone 2.0.x on MacOSX 10.5.x – bookmarked by 2 […]

  9. Ashish
    agosto 10th, 2009 @ 07:30

    hi !!
    Is there a different way of compiling wen using cross compilers ??
    how to compile my .m file using this toolchain??

  10. makaronnik
    settembre 12th, 2010 @ 12:00

    It’s great!

Leave a Reply