Downloaded from:
http://devs.openttd.org/~truebrain/compile-farm/apple-darwin9.txt

Cross-Compiling on Linux for
Mac OS X 10.3 - 10.5 (PPC and Intel)
====================================
Last Updated: 2009-03-29

It often is wroten down as impossible, but it can be done (and our nightlies
 are the living proof of that). You can make OSX binaries on a Linux based
 system. But, the process is not easy. Even worse, in the years we are
 supplying this, we had to change this documentation more often than any other
 target. This is mostly because you can't use a mainstream GCC, but you need
 the one from Apple. When you figured this out, and where to get the
 binutils tools, it in fact is very straight forward.

Requirements:
 - Mac OS X SDK Framework 10.4u.
 - 7zip 4.61+ to extract DMG files.
 - A fast computer, or a lots of time.
 - A working 32bit linux compiler (on 64bit, use -m32 ;)).
 - Bison installed (else it configures, but compile fails).

You get:
 - A i686-apple-darwin9 compiler (gcc and g++)
     For Mac OS X 10.4 - 10.5 Intel
 - A powerpc-apple-darwin9 compiler (gcc and g++)
     For Mac OS X 10.3 - 10.5 PPC
 - A x86_64-apple-darwin9 compiler (gcc and g++)
     For Mac OS X 10.5 Intel (64bit)

Notes:
 - Even on 64bit systems, compile 32bit binaries. It avoids a lot of errors.

Warning:
 - I can't guarantee you this will work, nor that it won't break your system.
 - Please, don't complain to me or ask me about this documentation. It is
    more meant as internal reference for when ever I want to reinstall the
    system. If it doesn't work for you, too bad, nothing to see here, find
    some other source.

Ps:
 - Yes, this also works with Mac OS X 10.5 Intel.
 - Yes, you can also make a x86_64 compiler.


The Start
---------

Pick which compiler you want to compile. Either one should do:
 - i686-apple-darwin9
 - powerpc-apple-darwin9
 - x86_64-apple-darwin9

Now put that in a variable:

 export TARGET=i686-apple-darwin9

We will use this a lot later on, to simplify this documentation, and show you
 it works for any target you pick.


CCTools
-------

To start, you need tools like 'ld', 'ar', ... this you can find in the package
 named 'odcctools'. I used various of sources over the years, the last one
 which seems to produce valid linux binaries, is located here:

 http://iphone-dev.googlecode.com/svn/

A bit good comes of the iPhone development after all ;) To install it, you
 need some minor modifications. Also, I used the 'branches' version, which
 has 9.2 ld version (well, I thought the name was just more cool!)

The current odcctools (r280) seems to contain a bug in the 'as'. It registers
 'word' twice, which gives an error. Sadly enough, both definitions are
 different. But, this patch fixes the problem by commenting one out, hoping
 the value of the other (older) one is correct. The odcctools also contain a
 few other bugs and problems. So load all the patches. After that, it seems to
 compile just fine.

# svn checkout http://iphone-dev.googlecode.com/svn/branches/odcctools-9.2-ld/
# cd odcctools-9.2-ld

# wget http://devs.openttd.org/~truebrain/compile-farm/odcctools_as.patch
# patch -p0 < odcctools_as.patch

# wget http://devs.openttd.org/~truebrain/compile-farm/odcctools_qsort.patch
# patch -p0 < odcctools_qsort.patch

# wget http://devs.openttd.org/~truebrain/compile-farm/odcctools_lipo.patch
# patch -p0 < odcctools_lipo.patch

# wget http://devs.openttd.org/~truebrain/compile-farm/odcctools_ld64.patch
# patch -p0 < odcctools_ld64.patch

# ./configure --prefix=/usr/$TARGET --target=$TARGET --with-sysroot=/usr/$TARGET --enable-ld64

In case you run x86_64-pc-linux-gnu, you might want to consider running this instead:

# LDFLAGS="-m32" CFLAGS="-m32" ./configure --prefix=/usr/$TARGET --target=$TARGET --with-sysroot=/usr/$TARGET --enable-ld64

Next:

# vi Makefile

On the line 'COMPONENTS = ', remove 'otool'. If you want this tool, make sure
 you have a objc++ compiler .. I don't, and I don't care about this tool.

# make
# make install

Now you should have a few useful tools in /usr/$TARGET/bin directory. If not,
 take a step back, and try again.


Mac OS X SDK
------------

Nowedays 7zip support .dmg files too (4.61+ I believe; I used 4.65). So now you
 can extract the files without access to a Mac. First, download xcode 3.1.2
 from the Apple website (you need an account!). Feel free to use any other
 version. The newer the better I guess.

For most targets, you want to extract the 10.4u SDK. This allows your binary
 to run from 10.3 to 10.5. But if you want to go for the x86_64 compiler, you
 need to go for the 10.5 SDK. As 10.4 can't run 64bit code anyway, it shouldn't
 be a real problem.

After downloading, you can extract it with (a long tree of compressed objects
 in other compressed objects):
# mkdir xcode
# cd xcode
# 7z x ../xcode312_2621_developerdvd.dmg
# 7z x 5.hfs
# 7z x Xcode\ Tools/Packages/MacOSX10.4.Universal.pkg
# 7z x Payload
# cpio -i < Payload~
# cp -R SDKs/MacOSX10.4u.sdk/* /usr/$TARGET/
# ln -sf /usr/$TARGET/System/Library/Frameworks /usr/$TARGET/Library/Frameworks

This should give you enough files to continue the compile. You need to use
 'cpio' on the last step, as 7z eats symlinks.

GCC
---

Now the most important part: GCC, your compiler.

Download the latest GCC From the Apple website. I used:

 http://www.opensource.apple.com/darwinsource/tarballs/other/gcc-5490.tar.gz

Feel free to use any newer. Extract the file somewhere.

# cd /var/tmp
# tar zxvf gcc-5490.tar.gz

gcc works nicely out-of-the-box, but Apple introduced one stupid thing: they
 use 'lipo' to check if the host is 32bit or 64bit. Of course it should be
 checking against the target. So we need a patch to force either 32bit
 (which also happens when you don't patch) or 64bit (which you need if you
 want to use x86_64-apple-darwin9). So apply either of those patches:

# cd gcc-5490
# wget http://devs.openttd.org/~truebrain/compile-farm/gcc-32bit.patch
# wget http://devs.openttd.org/~truebrain/compile-farm/gcc-64bit.patch
# patch -p0 < gcc-??bit.patch

Also apply this patch, to avoid some weirdness with CFLAGS:

# wget http://devs.openttd.org/~truebrain/compile-farm/gcc-cflags.patch
# patch -p0 < gcc-cflags.patch

Apply this patch if you don't want /usr/$TARGET/$TARGET dir (it annoys me ..
 it is nothing really ground-breaking or code-changing):

# wget http://devs.openttd.org/~truebrain/compile-farm/gcc-tooldir.patch
# patch -p0 < gcc-tooldir.patch
# cd ..

Now you can compile, like:

# mkdir gcc-build
# cd gcc-build
# export PATH=$PATH:/usr/$TARGET/bin
# ../gcc-5490/configure --prefix=/usr/$TARGET --disable-checking --enable-languages=c,objc,c++,obj-c++ --with-as=/usr/$TARGET/bin/$TARGET-as --with-ld=/usr/$TARGET/bin/$TARGET-ld --target=$TARGET --with-sysroot=/usr/$TARGET --enable-static --enable-shared --disable-nls --disable-multilib

If you are on a 64bit system, you can run the following (avoids -lm errors):

# CFLAGS="-m32" LDFLAGS="-m32" ../gcc-5490/configure --prefix=/usr/$TARGET --disable-checking --enable-languages=c,objc,c++,obj-c++ --with-as=/usr/$TARGET/bin/${TARGET}-as --with-ld=/usr/$TARGET/bin/${TARGET}-ld --target=$TARGET --with-sysroot=/usr/$TARGET --enable-static --enable-shared --disable-nls --disable-multilib

If you are compiling x86_64-apple-darwin9, make sure to replace 'ld' with
 'ld64' in the above configure. It isn't doing this on its own. Also, it looks
 like if you want to use 10.5 SDK for any other target, you need to use 'ld64'
 too. It seems 'ld' is slightly too old to understand a certain section in a
 library of the 10.5 SDK.

Now just compile:

# make
# make install

On a 32bit system, you most likely run into a problem with libstdc++v3. It
 tries to compile something while it is not allowed, or so it tells. I have
 yet no idea why this happens, I just know on a 64bit system this problem
 does not exist. If you solve it, please email me, and I will add your
 addition here.

Now you should have a working gcc and g++ binary in your
 /usr/$TARGET/bin directory. As I said, it is very simple if you
 know which tools to use and what to use for configure flags.


Other libs
----------

If you need libraries like libpng or libz, please start up your Mac OS X, and
 just copy them from there. Make sure that you use the right versions, and if
 possible universal build versions (those with multiple versions in them). It
 is almost impossible to compile such libraries yourself on your
 cross-compiler, and I would advise against it. Copying from a real Mac OS X
 is much easier, and shows much better results. But that is just my advise ;)


Conclusion
----------

There is not really much more to it. If this worked for you, and you want to
 thank me, please donate some money to OpenTTD. It is always very welcome.

