Cross-Compiling on Linux for
Mac OS X 10.3 - 10.5 (PPC and Intel)
====================================
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 (you need a Mac to unpack it, and repack it
in .zip format, as you most likely can't read the packed format Apple
supplies it in).
- 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
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.
The Start
---------
Pick which compiler you want to compile. Either one should do:
- i686-apple-darwin9
- powerpc-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 (r277) 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. 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
# ./configure --prefix=/usr/$TARGET --target=$TARGET --with-sysroot=/usr/$TARGET
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
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
------------
Next step, is getting some required files installed. You need the 10.4u SDK.
Get it from your Mac, or what ever, and put them in /usr/$TARGET.
It should include 'System/Framework', 'Library', 'usr', and more of that.
If you didn't do it correct, you will notice soon enough in the next step.
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-5465.tar.gz
Feel free to use any newer. Extract the file somewhere, and compile, like:
# cd /var/tmp
# tar zxvf gcc-5465.tar.gz
# mkdir gcc-build
# cd gcc-build
# export PATH=$PATH:/usr/$TARGET/bin
# ../gcc-5465/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
# make
# make install
If you are on a 64bit system, you can run this again (might be safer):
# CFLAGS="-m32" LDFLAGS="-m32" ../gcc-5465/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
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.