Sign in Join
Wiki copytemporary

Cross compiling OSX on Linux: what changed

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

Title: Cross compiling OSX on Linux

OldNewLine
1+## Cross compiling for macOS on Linux
2+
3+### Requirements
4+
5+What you'll need:
6+
7+- an Intel Mac running Leopard, Snow Leopard, or Lion
8+- Xcode installed on your Mac or the original or retail operating system DVD (for the SDK)
9+- a working Linux setup (tested with Debian Squeeze)
10+- up to date source for FPC (tested with 2.4.x) and Lazarus (tested with 0.9.30)
11+- the Open Darwin cctools (odcctools)
12+
13+> **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.
14+
15+### STEP 1: copy the SDK from your Mac
16+
17+You 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.
18+
19+I'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:
20+
21+``` bash
22+su - #become root
23+cd /opt
24+# all users should have read access to /Developer/SDKs...
25+ssh appleuser@applehostname "cd /Developer/SDKs && tar cz MacOSX10.5.sdk" | tar xz
26+```
27+
28+### STEP 2: grab odcctools from SVN
29+
30+Odcctools apparantly provide binutils for OSX/Darwin.
31+
32+On 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
33+
34+``` bash
35+aptitude install subversion #if it is not installed already
36+mkdir ~/svn #store our repositories here, change according to taste
37+cd ~/svn
38+svn checkout http://svn.macosforge.org/repository/odcctools/trunk/ odcctools
39+```
40+
41+and build it as
42+
43+``` bash
44+cd ~/svn/odcctools
45+bash extract.sh #this creates ~/svn/odcctools/odcctools. Fun, right?
46+#if you do not run it in bash, it might run in a c shell and complain
47+#about pushd not being there, not applying patches, and all kinds of nastiness.
48+cd ~/svn/odcctools/odcctools
49+#configure:
50+CC=gcc-4.4 ./configure --target=i386-darwin \
51+ --prefix=/opt/odcctools --with-sysroot=/opt/MacOSX10.5.sdk
52+#compile and install if succesful:
53+make && make install
54+```
55+
56+This installs the tools in /opt/odcctools. I've specified gcc 4.4 but it should be okay with other versions.
57+
58+### STEP 3: rebuild FPC
59+
60+(my sources are in ~/hg/pascal)
61+
62+``` bash
63+cd ~/hg/pascal
64+#if you use hg/mercurial you can see release version:
65+hg id -bint
66+ 730fd5ffbeb1 16666 fixes_2_4 release_2_4_2
67+make distclean && FPC=ppc386 make crossall crossinstall \
68+ CPU_TARGET=i386 OS_TARGET=darwin \
69+ CROSSBINDIR=/opt/odcctools/bin BINUTILSPREFIX=i386-darwin- \
70+ INSTALL_PREFIX=/opt/cross \
71+ OPT="-gl -gw -godwarfsets -XX -CX -Xd -Fl/opt/MacOSX10.5.sdk/usr/lib"
72+```
73+
74+Note that the options (OPT) as shown are vital, especially -gw.
75+
76+### STEP 4: configure fpc.cfg
77+
78+Add a darwin (cross-compile) clause to /etc/fpc.cfg:
79+
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`
86+
87+### STEP 5: build the Carbon LCL
88+
89+Be 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.
90+
91+You should now be able to use Lazarus in Linux to build for macOS.
92+
93+### Gotcha's
94+
95+There are two more gotcha's when cross-compiling to macOS:
96+
97+- Be sure to specify the -gw in your projects to avoid problems
98+
99+reported in (the unfixable) FPC bug \#12001.
100+
101+- 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).
102+
103+## Source
104+
105+Fpc 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
106+
107+## Older instructions
108+
109+This section was taken from the general [[Cross compiling]] page and may still be of interest:
110+
111+- 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/>
112+- 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
113+- 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.
114+- now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.
115+
116+type:
117+
118+``` bash
119+$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)
120+```
121+
122+type (iirc):
123+
124+``` bash
125+make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"
126+```
127+
128+if that succeded you can install it to whereever you want with:
129+
130+``` bash
131+make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc
132+```
133+
134+now 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
135+
136+- configure your /etc/fpc.cfg file.
137+
138+add a section like this:
139+
140+ #IFDEF powerpc
141+ -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
142+ -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
143+ -Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
144+ -k-systemroot $HOME/darwin/cross
145+ #ENDIF
146+
147+whenever 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.
148+
149+I may have missed some things (or most everything) as it's been a while since I did this.
150+
151+## See also
152+
153+- [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
154+
155+---
156+*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).*
157+Category: Cross compilation
158+Category: Linux
159+Category: macOS

Back to history