Sign in Join
Wiki copytemporary

Cross compiling OSX on Linux: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11## Cross compiling for macOS on Linux
22
33### Requirements
44
55What you'll need:
66
77- an Intel Mac running Leopard, Snow Leopard, or Lion
88- Xcode installed on your Mac or the original or retail operating system DVD (for the SDK)
99- a working Linux setup (tested with Debian Squeeze)
1010- up to date source for FPC (tested with 2.4.x) and Lazarus (tested with 0.9.30)
1111- the Open Darwin cctools (odcctools)
1212
1313> **Warning:** These instructions are for i386 Linux; trying this on x64 Linux has failed. Update 2013-12-13: cross compiling from Linux for macOS no longer works at all for 10.8 and above. The compiler works fine but viable linker tools (cctools for linux) are not available for Linux anymore. Please adjust instructions if you find a solution for this. The cctools are available in Arch Linux.
1414
1515### STEP 1: copy the SDK from your Mac
1616
1717You need to install Xcode from your operating system DVD if you've not already done so and copy the SDK to an appropriate location on your Linux box.
1818
1919I'd recommend Leopard 10.5.sdk (compatible with Leopard) but it depends on how far back you want to be compatible. The 10.5.sdk is located in /Developer/SDKs on the Mac. I put mine at /opt/Mac/ on my Debian box. Example using ssh from your Linux box to copy over the files:
2020
2121``` bash
2222su - #become root
2323cd /opt
2424# all users should have read access to /Developer/SDKs...
2525ssh appleuser@applehostname "cd /Developer/SDKs && tar cz MacOSX10.5.sdk" | tar xz
2626```
2727
2828### STEP 2: grab odcctools from SVN
2929
3030Odcctools apparantly provide binutils for OSX/Darwin.
3131
3232On 64 bit Linux environments, fpc mailing list users have problems with this step. Perhaps this [StackOverflow question and answer](http://stackoverflow.com/questions/3024255/building-odcctools-in-a-64-bit-os) can help: in the configure step, you'd apparently need to set CC="gcc -m32" CXX="g++ -m32" ./configure blah blah blah
3333
3434``` bash
3535aptitude install subversion #if it is not installed already
3636mkdir ~/svn #store our repositories here, change according to taste
3737cd ~/svn
3838svn checkout http://svn.macosforge.org/repository/odcctools/trunk/ odcctools
3939```
4040
4141and build it as
4242
4343``` bash
4444cd ~/svn/odcctools
4545bash extract.sh #this creates ~/svn/odcctools/odcctools. Fun, right?
4646#if you do not run it in bash, it might run in a c shell and complain
4747#about pushd not being there, not applying patches, and all kinds of nastiness.
4848cd ~/svn/odcctools/odcctools
4949#configure:
5050CC=gcc-4.4 ./configure --target=i386-darwin \
5151 --prefix=/opt/odcctools --with-sysroot=/opt/MacOSX10.5.sdk
5252#compile and install if succesful:
5353make && make install
5454```
5555
5656This installs the tools in /opt/odcctools. I've specified gcc 4.4 but it should be okay with other versions.
5757
5858### STEP 3: rebuild FPC
5959
6060(my sources are in ~/hg/pascal)
6161
6262``` bash
6363cd ~/hg/pascal
6464#if you use hg/mercurial you can see release version:
6565hg id -bint
6666 730fd5ffbeb1 16666 fixes_2_4 release_2_4_2
6767make distclean && FPC=ppc386 make crossall crossinstall \
6868 CPU_TARGET=i386 OS_TARGET=darwin \
6969 CROSSBINDIR=/opt/odcctools/bin BINUTILSPREFIX=i386-darwin- \
7070 INSTALL_PREFIX=/opt/cross \
7171 OPT="-gl -gw -godwarfsets -XX -CX -Xd -Fl/opt/MacOSX10.5.sdk/usr/lib"
7272```
7373
7474Note that the options (OPT) as shown are vital, especially -gw.
7575
7676### STEP 4: configure fpc.cfg
7777
7878Add a darwin (cross-compile) clause to /etc/fpc.cfg:
7979
80-`#IFDEF darwin`\
81-`-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/`\
82-`-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/*`\
83-`-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/rtl`\
84-`-FD/opt/odcctools/bin`\
85-`#ENDIF`
80+```text
81+#IFDEF darwin
82+-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/
83+-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/*
84+-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/rtl
85+-FD/opt/odcctools/bin
86+#ENDIF
87+```
8688
8789### STEP 5: build the Carbon LCL
8890
8991Be sure to specify the Darwin OS target, i386 CPU target and, most importantly, add the -gw option. Perform a Clean+Build of the LCL and the Package Registration.
9092
9193You should now be able to use Lazarus in Linux to build for macOS.
9294
9395### Gotcha's
9496
9597There are two more gotcha's when cross-compiling to macOS:
9698
9799- Be sure to specify the -gw in your projects to avoid problems
98100
99101reported in (the unfixable) FPC bug \#12001.
100102
101103- Be sure to specify the -XR option pointing to your SDK root (e.g. -XR/opt/Mac/Leopard10.5.sdk), or the Darwin linker will try to link to the wrong startup object (/usr/lib/crt1.o).
102104
103105## Source
104106
105107Fpc Mailing list 6 August 2011 post by Bruce titled "Re: Cross Compiling from Linux to Leopard 10.5 or Snow Leopard 10.6 target. How? \[SOLVED\]" Adapted by BigChimp
106108
107109## Older instructions
108110
109111This section was taken from the general [[Cross compiling]] page and may still be of interest:
110112
111113- First you need the binutils for the platform you want to compile to. Download odcctools from this site (use the cvs version) and follow their instructions for installing. <http://www.opendarwin.org/projects/odcctools/>
112114- you need to create a fake root dir like: \$HOME/darwinroot copy at least the /System and /Frameworks and /usr directories (you may have to copy more than this) from your Apple or Darwin computer to \$HOME/darwinroot
113115- now that you have these files make a folder in \$HOME/darwinroot called cross. where ever you installed the odcctools you need to make links for the cross tools to be more fpc friendly. there are a bunch of files from odcc tools called powerpc-apple-darwin-* you need to make links (or rename them) so powerpc-apple-darwin-ld becomes powerpc-darwin-ld, do the same for *-ar and *-as.
114116- now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.
115117
116118type:
117119
118120``` bash
119121$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)
120122```
121123
122124type (iirc):
123125
124126``` bash
125127make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"
126128```
127129
128130if that succeded you can install it to whereever you want with:
129131
130132``` bash
131133make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc
132134```
133135
134136now copy the file ./compiler/ppccross somewhere you will be able to find it as it's the compiler you'll need to build powerpc programs
135137
136138- configure your /etc/fpc.cfg file.
137139
138140add a section like this:
139141
140142 #IFDEF powerpc
141143 -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
142144 -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
143145 -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
144146 -k-systemroot $HOME/darwin/cross
145147 #ENDIF
146148
147149whenever you want to crosscompile you have to have ppccross and the symlinks to powerpc-darwin-* in the PATH and you should be able to just do ppccross someprogie.pas and it will create a darwin executable.
148150
149151I may have missed some things (or most everything) as it's been a while since I did this.
150152
151153## See also
152154
153155- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
154156
155157---
156158*From the Free Pascal wiki, page "Cross compiling OSX on Linux", as it stood on 2020-11-26 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
157159Category: Cross compilation
158160Category: Linux
159161Category: macOS

Back to history