Difference between revisions of "Cross compiling OSX on Linux"

From Lazarus wiki
Jump to navigationJump to search
(→‎Copy SDK: Had some problems)
(→‎See also: New section)
 
(22 intermediate revisions by 6 users not shown)
Line 1: Line 1:
== Cross compiling for OSX on Linux ==
+
== Cross compiling for macOS on Linux ==
  
 
=== Requirements ===
 
=== Requirements ===
 
What you'll need:
 
What you'll need:
  
[1] an Intel Mac running Leopard, Snow Leopard, or Lion
+
* an Intel Mac running Leopard, Snow Leopard, or Lion
 +
* Xcode installed on your Mac or the original or retail operating system DVD (for the SDK)
 +
* a working Linux setup (tested with Debian Squeeze)
 +
* up to date source for FPC (tested with 2.4.x) and Lazarus (tested with 0.9.30)
 +
* the Open Darwin cctools (odcctools)
  
[2] XCode installed on your Mac or the original or retail OSX DVD (for the SDK)
+
{{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. }}
  
[3] a working linux setup (Debian Squeeze in my case)
+
=== STEP 1: copy the SDK from your Mac ===
 +
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.
  
[4] up to date source for FPC (2.4.x) and Lazarus (0.9.30)
+
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:
 +
<syntaxhighlight lang="bash">
 +
su - #become root
 +
cd /opt
 +
# all users should have read access to /Developer/SDKs...
 +
ssh appleuser@applehostname "cd /Developer/SDKs && tar cz MacOSX10.5.sdk" | tar xz
 +
</syntaxhighlight>
  
[5] the Open Darwin cctools (odcctools)
+
=== STEP 2: grab odcctools from SVN ===
 +
Odcctools apparantly provide binutils for OSX/Darwin.
  
Once set up, you won't need the Mac to compile your code.
+
On 64 bit Linux environments, fpc mailing list users have problems with this step. Perhaps this [http://stackoverflow.com/questions/3024255/building-odcctools-in-a-64-bit-os StackOverflow question and answer] can help: in the configure step, you'd apparently need to set
 
+
CC="gcc -m32" CXX="g++ -m32" ./configure blah blah blah
=== Copy SDK ===
 
STEP 1: copy the SDK from your Mac.
 
You need to install XCode from your OSX DVD if you've not already done so and copy the SDK to an appropriate location on your Linux box. I'd
 
recommend MacOSX10.5.sdk (compatible with Leopard) but it depends on how far back you want to be compatible. MacOSX10.5.sdk is located in /Developer/SDKs on the Mac.
 
I put mine at /opt/MacOSX10.5.sdk on my Debian box. Example using scp from your Linux box to copy over the files:
 
 
 
su - #become root
 
cd /opt
 
#regular users should have read access to /Developer/SDKs...
 
#however, in my case, I probably had some symlinks linking to each other, so copying ended up in a loop...
 
#to do: fix this (archive on OSX, copy, unpack, fix permissions with chmod -R +ugo+r *)
 
scp -r appleuser@applehostname:/Developer/SDKs/MacOSX10.5.sdk /opt/
 
 
 
=== Get odcctools ===
 
STEP 2: grab odcctools from SVN (I use mercurial, hence the "hg" stuff)
 
Install Mercurial if you haven't already:
 
aptitude install mercurial
 
mkdir ~/hg #store our hg repositories here, change according to taste
 
cd ~/hg
 
hg clone http://svn.macosforge.org/repository/odcctools/trunk/ odcctools
 
  
 +
<syntaxhighlight lang="bash">
 +
aptitude install subversion #if it is not installed already
 +
mkdir ~/svn #store our repositories here, change according to taste
 +
cd ~/svn
 +
svn checkout http://svn.macosforge.org/repository/odcctools/trunk/ odcctools
 +
</syntaxhighlight>
 
and build it as
 
and build it as
cd ~/hg/odcctools && ./extract.sh && cd odcctools
+
<syntaxhighlight lang="bash">
CC=gcc-4.4 ./configure --target=i386-darwin \
+
cd ~/svn/odcctools
  --prefix=/opt/odcctools --with-sysroot=/opt/MacOSX10.5.sdk
+
bash extract.sh #this creates ~/svn/odcctools/odcctools. Fun, right?
make && make install
+
#if you do not run it in bash, it might run in a c shell and complain
 
+
#about pushd not being there, not applying patches, and all kinds of nastiness.
 +
cd ~/svn/odcctools/odcctools
 +
#configure:
 +
CC=gcc-4.4 ./configure --target=i386-darwin \
 +
--prefix=/opt/odcctools --with-sysroot=/opt/MacOSX10.5.sdk
 +
#compile and install if succesful:
 +
make && make install
 +
</syntaxhighlight>
 +
This installs the tools in /opt/odcctools.
 
I've specified gcc 4.4 but it should be okay with other versions.
 
I've specified gcc 4.4 but it should be okay with other versions.
  
 
+
=== STEP 3: rebuild FPC ===
=== Rebuild FPC ===
+
(my sources are in ~/hg/pascal)
STEP 3: rebuild FPC (my sources are in ~/hg/pascal)
+
<syntaxhighlight lang="bash">
 
+
cd ~/hg/pascal  
cd ~/hg/pascal && hg id -bint
+
#if you use hg/mercurial you can see release version:
  730fd5ffbeb1 16666 fixes_2_4 release_2_4_2
+
hg id -bint
make distclean && FPC=ppc386 make crossall crossinstall \
+
730fd5ffbeb1 16666 fixes_2_4 release_2_4_2
  CPU_TARGET=i386 OS_TARGET=darwin \
+
make distclean && FPC=ppc386 make crossall crossinstall \
  CROSSBINDIR=/opt/odcctools/bin BINUTILSPREFIX=i386-darwin- \
+
CPU_TARGET=i386 OS_TARGET=darwin \
  INSTALL_PREFIX=/opt/cross \
+
CROSSBINDIR=/opt/odcctools/bin BINUTILSPREFIX=i386-darwin- \
  OPT="-gl -gw -godwarfsets -XX -CX -Xd -Fl/opt/MacOSX10.5.sdk/usr/lib"
+
INSTALL_PREFIX=/opt/cross \
 
+
OPT="-gl -gw -godwarfsets -XX -CX -Xd -Fl/opt/MacOSX10.5.sdk/usr/lib"
 +
</syntaxhighlight>
 
Note that the options (OPT) as shown are vital, especially -gw.
 
Note that the options (OPT) as shown are vital, especially -gw.
  
STEP 4: add a darwin (cross-compile) clause to /etc/fpc.cfg
+
=== STEP 4: configure fpc.cfg ===
 
+
Add a darwin (cross-compile) clause to /etc/fpc.cfg:
 
  #IFDEF darwin
 
  #IFDEF darwin
 
  -Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/
 
  -Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/
Line 68: Line 76:
 
  #ENDIF
 
  #ENDIF
  
=== Build Carbon LCL ===
+
=== STEP 5: build the Carbon LCL ===
STEP 5: build the Carbon LCL (in Lazarus)
+
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.
 
 
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.
 
  
You should now be able to use Lazarus in Linux to build for Mac OSX.
+
You should now be able to use Lazarus in Linux to build for macOS.
  
 
=== Gotcha's ===
 
=== Gotcha's ===
There are two more gotcha's when cross-compiling to OS X:
+
There are two more gotcha's when cross-compiling to macOS:
 
+
* Be sure to specify the -gw in your projects to avoid problems
First, be sure to specify the -gw in your projects to avoid problems
 
 
reported in (the unfixable) FPC bug #12001.
 
reported in (the unfixable) FPC bug #12001.
 
+
* 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).
Second, be sure to specify the -XR option pointing to your SDK root, in
 
my case -XR/opt/MacOSX10.5.sdk, or the Darwin linker will try to link to
 
the wrong startup object (/usr/lib/crt1.o).
 
  
 
== Source ==
 
== Source ==
Fpc Mailing list 6 August 2011 post by Bruce titled "Re: Cross Compiling from Linux to a Mac OS X 10.5 or 10.6 target. How? [SOLVED]"
+
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
 
Adapted by BigChimp
  
[[Category:OSX]]
+
== Older instructions ==
 +
This section was taken from the general [[Cross compiling]] page and may still be of interest:
 +
* 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/
 +
* 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
 +
* 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.
 +
* now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.
 +
type:
 +
<syntaxhighlight lang="bash">$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)</syntaxhighlight>
 +
type (iirc):
 +
<syntaxhighlight lang="bash">make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"</syntaxhighlight>
 +
if that succeded you can install it to whereever you want with:
 +
<syntaxhighlight lang="bash">make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc</syntaxhighlight>
 +
 
 +
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
 +
 
 +
* configure your /etc/fpc.cfg file.
 +
add a section like this:
 +
<pre>
 +
#IFDEF powerpc
 +
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
 +
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
 +
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
 +
-k-systemroot $HOME/darwin/cross
 +
#ENDIF
 +
</pre>
 +
 
 +
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.
 +
 
 +
I may have missed some things (or most everything) as it's been a while since I did this.
 +
 
 +
== See also ==
 +
 
 +
* [[Cross compiling]] for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.
 +
 
 +
[[Category:macOS]]
 +
[[Category:Linux]]
 +
[[Category:Cross compilation]]

Latest revision as of 01:41, 26 November 2020

Cross compiling for macOS on Linux

Requirements

What you'll need:

  • an Intel Mac running Leopard, Snow Leopard, or Lion
  • Xcode installed on your Mac or the original or retail operating system DVD (for the SDK)
  • a working Linux setup (tested with Debian Squeeze)
  • up to date source for FPC (tested with 2.4.x) and Lazarus (tested with 0.9.30)
  • the Open Darwin cctools (odcctools)
Warning-icon.png

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.

STEP 1: copy the SDK from your Mac

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.

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:

su - #become root
cd /opt
# all users should have read access to /Developer/SDKs...
ssh appleuser@applehostname "cd /Developer/SDKs && tar cz MacOSX10.5.sdk" | tar xz

STEP 2: grab odcctools from SVN

Odcctools apparantly provide binutils for OSX/Darwin.

On 64 bit Linux environments, fpc mailing list users have problems with this step. Perhaps this StackOverflow question and answer can help: in the configure step, you'd apparently need to set CC="gcc -m32" CXX="g++ -m32" ./configure blah blah blah

aptitude install subversion #if it is not installed already
mkdir ~/svn #store our repositories here, change according to taste
cd ~/svn
svn checkout http://svn.macosforge.org/repository/odcctools/trunk/ odcctools

and build it as

cd ~/svn/odcctools
bash extract.sh #this creates ~/svn/odcctools/odcctools. Fun, right?
#if you do not run it in bash, it might run in a c shell and complain
#about pushd not being there, not applying patches, and all kinds of nastiness.
cd ~/svn/odcctools/odcctools
#configure:
CC=gcc-4.4 ./configure --target=i386-darwin \
 --prefix=/opt/odcctools --with-sysroot=/opt/MacOSX10.5.sdk
#compile and install if succesful:
make && make install

This installs the tools in /opt/odcctools. I've specified gcc 4.4 but it should be okay with other versions.

STEP 3: rebuild FPC

(my sources are in ~/hg/pascal)

cd ~/hg/pascal 
#if you use hg/mercurial you can see release version:
hg id -bint
 730fd5ffbeb1 16666 fixes_2_4 release_2_4_2
make distclean && FPC=ppc386 make crossall crossinstall \
 CPU_TARGET=i386 OS_TARGET=darwin \
 CROSSBINDIR=/opt/odcctools/bin BINUTILSPREFIX=i386-darwin- \
 INSTALL_PREFIX=/opt/cross \
 OPT="-gl -gw -godwarfsets -XX -CX -Xd -Fl/opt/MacOSX10.5.sdk/usr/lib"

Note that the options (OPT) as shown are vital, especially -gw.

STEP 4: configure fpc.cfg

Add a darwin (cross-compile) clause to /etc/fpc.cfg:

#IFDEF darwin
-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/
-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/*
-Fu/opt/cross/lib/fpc/$fpcversion/units/i386-darwin/rtl
-FD/opt/odcctools/bin
#ENDIF

STEP 5: build the Carbon LCL

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.

You should now be able to use Lazarus in Linux to build for macOS.

Gotcha's

There are two more gotcha's when cross-compiling to macOS:

  • Be sure to specify the -gw in your projects to avoid problems

reported in (the unfixable) FPC bug #12001.

  • 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).

Source

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

Older instructions

This section was taken from the general Cross compiling page and may still be of interest:

  • 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/
  • 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
  • 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.
  • now you are ready to crosscompile fpc. basically you need to have the fpc source and have a terminal open there.

type:

$PATH=$PATH:$HOME/darwinroot/cross (or whereever you made the symlinks)

type (iirc):

make all TARGET_OS=darwin TARGET_CPU=powerpc OPT="-Xd -Fl/$HOME/darwinroot/usr/lib"

if that succeded you can install it to whereever you want with:

make install TARGET_OS=darwin TARGET_CPU=powerpc PREFIX=/cross/fpc

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

  • configure your /etc/fpc.cfg file.

add a section like this:

#IFDEF powerpc
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/rtl
-Fu/cross/fpc/lib/fpc/$fpcversion/units/$fpctarget/*
-k-systemroot $HOME/darwin/cross
#ENDIF

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.

I may have missed some things (or most everything) as it's been a while since I did this.

See also

  • Cross compiling for cross compiling from Linux, macOS and Windows along with other useful cross-compilation information.