2015-07-19 22:28:10 +02:00
## OS X Cross toolchain for Linux, *BSD and Cygwin ##
2013-11-13 20:47:04 +01:00
### WHAT IS THE GOAL OF OSXCROSS? ###
2016-09-17 19:05:09 +02:00
The goal of OSXCross is to provide a well working OS X cross toolchain for
2017-10-25 09:49:35 +02:00
Linux, \*BSD, and Cygwin.
2013-11-13 20:47:04 +01:00
### HOW DOES IT WORK? ###
2017-01-18 11:08:35 +01:00
For cross-compiling for OS X you need
* the Clang/LLVM compiler
* the the [cctools ](http://www.opensource.apple.com/tarballs/cctools )
(ld, lipo, …), and
* the OSX SDK.
2014-04-06 22:27:59 +02:00
[Clang/LLVM is a cross compiler by default ](http://clang.llvm.org/docs/CrossCompilation.html )
2017-01-18 11:08:35 +01:00
and is now available on nearly every Linux distribution, so we just
need a proper [port ](https://github.com/tpoechtrager/cctools-port ) of
the cctools and the OS X SDK.
OSXCross includes a collection of scripts for preparing the SDK and
building the cctools.
It also includes scripts for optionally building
* Clang using gcc (for the case your distribution does not include it),
* an up-to-date vanilla GCC as a cross-compiler for target OS X,
* the "compiler-rt" runtime library, and
* the `llvm-dsymutil` tool required for debugging.
2013-11-13 20:47:04 +01:00
### WHAT CAN I BUILD WITH IT? ###
2016-09-17 19:05:09 +02:00
Basically everything you can build on OS X with clang/gcc should build with
this cross toolchain as well.
2013-11-13 20:47:04 +01:00
2014-12-24 10:52:24 +01:00
### PACKET MANAGERS ###
2016-09-17 19:05:09 +02:00
OSXCross comes with a minimalistic MacPorts Packet Manager.
See [README.MACPORTS ](README.MACPORTS.md ) for more.
2014-12-24 10:52:24 +01:00
2013-11-13 20:47:04 +01:00
### INSTALLATION: ###
2015-07-20 22:41:14 +02:00
*Windows/Cygwin users should follow [README.CYGWIN ](README.CYGWIN.md ).*
2016-09-17 19:05:09 +02:00
Move your
[packaged SDK ](https://github.com/tpoechtrager/osxcross#packaging-the-sdk )
to the tarballs/ directory.
2013-11-13 20:47:04 +01:00
2015-07-19 22:28:10 +02:00
Then ensure you have the following installed on your system:
2013-11-13 20:47:04 +01:00
2015-07-17 23:25:59 +02:00
`Clang 3.2+` , `patch` , `libxml2-devel` (< =10.6 only) and the `bash shell` .
2017-01-18 11:08:35 +01:00
You can run 'sudo tools/get\_dependencies.sh' to get these (and the
optional packages) automatically.
2015-10-24 17:50:27 +02:00
*Optional:*
2015-07-17 23:25:59 +02:00
2017-01-18 11:08:35 +01:00
- `llvm-devel` : For Link Time Optimization support
- `uuid-devel` : For ld64 `-random_uuid` support
- `llvm-devel` + `xar-devel` : For ld64 `-bitcode_bundle` support
2015-10-24 17:50:27 +02:00
2016-09-17 19:05:09 +02:00
You can find xar [here ](https://github.com/mackyle/xar ).
2015-10-24 17:50:27 +02:00
Do not install libxar-dev on Ubuntu, it's a different package.
2013-12-15 19:06:50 +01:00
2014-11-10 20:43:14 +01:00
2016-09-17 19:05:09 +02:00
##### Building Clang #####
2014-11-10 20:43:14 +01:00
2016-09-17 19:05:09 +02:00
OSXCross uses `clang` as the default compiler for building its tools, and also
as a cross-compiler to create OSX binaries.
In `clang` there is no difference between cross-compilation and native
compilation, so OSXCross can use a normal `clang` install for both. You can
use either a `clang` installation you already have, or build your own from
source.
To build and install your own `clang` from a recent source tree, using `gcc` ,
run:
```shell
./build_clang.sh
```
This installs `clang` into `/usr/local` . If you want to install somewhere
else, set the `INSTALLPREFIX` variable. For example:
```shell
INSTALLPREFIX=/opt/clang ./build_clang.sh
```
On debian-like systems you can also use [llvm.org/apt ](http://llvm.org/apt ) to
get a newer version of clang.
But be careful, that repository is known to cause
[troubles ](https://github.com/tpoechtrager/osxcross/issues/16 ).
##### Building OSXCross #####
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
To build the cross toolchain (using `clang` ), run:
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
```shell
./build.sh
```
Or, set variable `UNATTENDED` to `1` to skip the prompt and proceed straight to
the build:
```shell
UNATTENDED=1 ./build.sh
```
(This will search 'tarballs' for your SDK and then build in its own directory.)
**Once this is done:** add `<path>/target/bin` to your PATH variable so that
you can invoke the cross-compiler.
2013-11-13 20:47:04 +01:00
2013-12-15 19:06:50 +01:00
That's it. See usage examples below.
2013-11-13 20:47:04 +01:00
##### Building GCC: #####
2016-09-17 19:05:09 +02:00
If you also want to build GCC as a cross-compiler, you can do that by running:
2014-10-01 19:53:41 +02:00
2016-09-17 19:05:09 +02:00
```shell
./build_gcc.sh
```
2014-12-24 09:54:27 +01:00
2016-09-17 19:05:09 +02:00
The script lets you select a GCC version by setting the variable `GCC_VERSION` .
By default you get C and C++ compilers, but you can tell the script to build a
Fortran compiler as well:
2014-07-17 22:43:29 +02:00
2016-09-17 19:05:09 +02:00
```shell
GCC_VERSION=5.2.0 ENABLE_FORTRAN=1 ./build_gcc.sh
```
2014-07-17 22:43:29 +02:00
2016-09-17 19:05:09 +02:00
\[A gfortran usage example can be found [here ](https://github.com/tpoechtrager/osxcross/issues/28#issuecomment-67047134 )]
Before you do this, make sure you have the GCC build depedencies installed on
your system.
On debian like systems you can install these using:
2014-07-17 22:43:29 +02:00
2016-09-17 19:05:09 +02:00
```shell
sudo apt-get install gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev
```
2013-11-13 20:47:04 +01:00
2014-12-24 09:54:27 +01:00
ATTENTION:
2016-09-17 19:05:09 +02:00
OSXCross links libgcc and libstdc++ statically by default (this affects
`-foc-use-gcc-libstdc++` too). You can turn this behavior off with
`OSXCROSS_GCC_NO_STATIC_RUNTIME=1` (env).
The build also creates aliases `*-g++-libc++` which link with the `clang`
implementation of the C++ standard library instead of the GCC version. Don't
use these variants unless you know what you're doing.
2014-12-24 09:54:27 +01:00
2014-03-26 20:41:55 +01:00
### PACKAGING THE SDK: ###
2015-06-27 19:42:53 +02:00
**[Please ensure you have read and understood the Xcode license
terms before continuing.](https://www.apple.com/legal/sla/docs/xcode.pdf)**
2015-06-27 19:01:56 +02:00
2015-05-23 20:24:43 +02:00
##### Packaging the SDK on Mac OS X: #####
2014-09-27 14:35:42 +02:00
2018-09-08 20:03:08 +02:00
1. [Download [Xcode ](https://download.developer.apple.com/Developer_Tools/Xcode_7.3/Xcode_7.3.dmg ) \*\*]
2014-09-27 14:35:42 +02:00
2. [Mount Xcode.dmg (Open With -> DiskImageMounter) \*\*\*]
2015-05-23 20:24:43 +02:00
3. Run: `./tools/gen_sdk_package.sh` (from the OSXCross package)
2014-09-27 14:35:42 +02:00
4. Copy the packaged SDK (\*.tar.\* or \*.pkg) on a USB Stick
2016-09-17 19:05:09 +02:00
5. (On Linux/BSD) Copy or move the SDK into the tarballs/ directory of
OSXCross.
\*\* Xcode up to 7.3.x is known to work.
2014-09-27 14:35:42 +02:00
2016-09-17 19:05:09 +02:00
\*\*\* If you get a dialog with a crossed circle, ignore it. You don't need
to install Xcode.
2014-09-27 14:35:42 +02:00
Step 1. and 2. can be skipped if you have Xcode installed.
2016-06-25 12:32:14 +02:00
##### Packing the SDK on Linux, Method 1 (works up to Xcode 7.3): #####
2014-09-27 14:35:42 +02:00
2015-06-27 19:01:56 +02:00
1. Download Xcode like described in 'Packaging the SDK on Mac OS X'
2016-06-25 12:32:14 +02:00
2. Install `cmake` , `libxml2-dev` and `fuse`
3. Run `./tools/gen_sdk_package_darling_dmg.sh <xcode>.dmg`
2015-06-27 19:01:56 +02:00
4. Copy or move the SDK into the tarballs/ directory
2013-11-13 20:47:04 +01:00
2016-06-25 12:32:14 +02:00
##### Packing the SDK on Linux, Cygwin (and others), Method 2 (works up to Xcode 7.2): #####
2015-05-23 20:24:43 +02:00
1. Download Xcode like described in 'Packaging the SDK on Mac OS X'
2016-06-25 12:32:14 +02:00
2. Ensure you have `clang` and `make` installed
3. Run `./tools/gen_sdk_package_p7zip.sh <xcode>.dmg`
2015-05-30 21:01:48 +02:00
4. Copy or move the SDK into the tarballs/ directory
2015-05-23 20:24:43 +02:00
2016-06-25 12:32:14 +02:00
##### Packing the SDK on Linux, Method 3 (works up to Xcode 4.2): #####
2015-06-27 19:01:56 +02:00
1. Download Xcode 4.2 for Snow Leopard
2. Ensure you are downloading the "Snow Leopard" version
3. Install `dmg2img`
4. Run (as root): `./tools/mount_xcode_image.sh /path/to/xcode.dmg`
5. Follow the instructions printed by `./tools/mount_xcode_image.sh`
6. Copy or move the SDK into the tarballs/ directory
2015-06-21 14:01:49 +02:00
2013-11-13 21:21:05 +01:00
### USAGE EXAMPLES: ###
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
##### Example. To compile a file called test.cpp, you can run: #####
2013-11-13 20:47:04 +01:00
* Clang:
2016-09-17 19:05:09 +02:00
* 32 bit: `o32-clang++ test.cpp -O3 -o test` OR
`i386-apple-darwinXX-clang++ test.cpp -O3 -o test`
* 64 bit: `o64-clang++ test.cpp -O3 -o test` OR
`x86_64-apple-darwinXX-clang++ test.cpp -O3 -o test`
2013-11-13 20:47:04 +01:00
* GCC:
2016-09-17 19:05:09 +02:00
* 32 bit: `o32-g++ test.cpp -O3 -o test` OR
`i386-apple-darwinXX-g++ test.cpp -O3 -o test`
* 64 bit: `o64-g++ test.cpp -O3 -o test` OR
`x86_64-apple-darwinXX-g++ test.cpp -O3 -o test`
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
XX= the target version, you can find it out by running `osxcross-conf` and
then see `TARGET` .
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
You can use the shortcuts `o32-...` for `i386-apple-darwin...` , depending on
which you prefer.
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
*I'll continue from here on with `o32-clang` , but remember,
2014-04-25 21:06:24 +02:00
you can simply replace it with `o32-gcc` or `i386-apple-darwin...` .*
2013-11-13 20:47:04 +01:00
##### Building Makefile based projects: #####
2013-11-13 20:57:28 +01:00
* `make CC=o32-clang CXX=o32-clang++`
2013-11-13 20:47:04 +01:00
##### Building automake based projects: #####
2013-11-13 20:57:28 +01:00
* `CC=o32-clang CXX=o32-clang++ ./configure --host=i386-apple-darwinXX`
2013-11-13 20:47:04 +01:00
2013-12-15 19:06:50 +01:00
##### Building test.cpp with libc++: #####
2016-09-17 19:05:09 +02:00
Note: libc++ requires Mac OS X 10.7 or newer! If you really need C++11 for
2014-04-06 22:27:59 +02:00
an older OS X version, then you can do the following:
1. Build GCC so you have an up-to-date libstdc++
2016-09-17 19:05:09 +02:00
2. Build your source code with GCC or
`clang++-gstdc++` / `clang++ -foc-use-gcc-libstdc++`
2014-04-06 22:27:59 +02:00
Usage Examples:
2013-12-15 19:06:50 +01:00
* Clang:
* C++98: `o32-clang++ -stdlib=libc++ test.cpp -o test`
2015-08-22 23:15:27 +02:00
* C++11: `o32-clang++ -stdlib=libc++ -std=c++11 test1.cpp -o test`
* C++14: `o32-clang++ -stdlib=libc++ -std=c++14 test1.cpp -o test`
* C++1z: `o32-clang++ -stdlib=libc++ -std=c++1z test1.cpp -o test`
2013-12-15 19:06:50 +01:00
* Clang (shortcut):
* C++98: `o32-clang++-libc++ test.cpp -o test`
* C++11: `o32-clang++-libc++ -std=c++11 test.cpp -o test`
2015-08-22 23:15:27 +02:00
* C++14: `o32-clang++-libc++ -std=c++14 test.cpp -o test`
* C++1z: `o32-clang++-libc++ -std=c++1z test.cpp -o test`
2013-12-15 19:06:50 +01:00
2015-08-22 23:15:27 +02:00
* GCC
2013-12-15 19:06:50 +01:00
2015-08-22 23:15:27 +02:00
* C++11: `o32-g++-libc++ -std=c++11 test.cpp`
* C++14: `o32-g++-libc++ -std=c++14 test.cpp -o test`
* C++1z: `o32-g++-libc++ -std=c++1z test.cpp -o test`
2013-12-15 19:06:50 +01:00
2013-11-13 20:47:04 +01:00
##### Building test1.cpp and test2.cpp with LTO (Link Time Optimization): #####
* build the first object file: `o32-clang++ test1.cpp -O3 -flto -c`
* build the second object file: `o32-clang++ test2.cpp -O3 -flto -c`
* link them with LTO: `o32-clang++ -O3 -flto test1.o test2.o -o test`
##### Building a universal binary: #####
2013-12-15 19:06:50 +01:00
* Clang:
2013-11-13 20:52:07 +01:00
* `o64-clang++ test.cpp -O3 -arch i386 -arch x86_64 -o test`
* GCC:
2013-11-13 20:47:04 +01:00
* build the 32 bit binary: `o32-g++ test.cpp -O3 -o test.i386`
* build the 64 bit binary: `o64-g++ test.cpp -O3 -o test.x86_64`
2016-09-17 19:05:09 +02:00
* use lipo to generate the universal binary:
`x86_64-apple darwinXX-lipo -create test.i386 test.x86_64 -output test`
2013-11-13 20:47:04 +01:00
2014-04-25 21:06:24 +02:00
### DEPLOYMENT TARGET: ###
The default deployment target is `Mac OS X 10.5` .
However, there are several ways to override the default value:
1. by passing `OSX_VERSION_MIN=10.x` to `./build.sh`
2. by passing `-mmacosx-version-min=10.x` to the compiler
3. by setting the `MACOSX_DEPLOYMENT_TARGET` environment variable
2016-09-17 19:05:09 +02:00
\>= 10.9 also defaults to `libc++` instead of `libstdc++` , this behavior
2014-04-25 21:06:24 +02:00
can be overriden by explicitly passing `-stdlib=libstdc++` to clang.
2013-11-13 20:47:04 +01:00
2016-09-17 19:05:09 +02:00
x86\_64h defaults to `Mac OS X 10.8` and requires clang 3.5+.
2014-06-20 14:16:57 +02:00
x86\_64h = x86\_64 with optimizations for the Intel Haswell Architecture.
2014-05-18 22:07:37 +02:00
2015-07-17 23:25:59 +02:00
### BUILDING OSXCROSS WITH GCC: ###
You can build OSXCross with GCC this way:
`CC=gcc CXX=g++ ./build.sh`
2015-10-24 17:50:27 +02:00
You will need gcc/g++/gcc-objc 4.7+.
2015-07-17 23:25:59 +02:00
2015-12-21 16:11:37 +01:00
### PROJECTS USING OSXCROSS: ###
2016-09-17 19:05:09 +02:00
* [multiarch/crossbuild ](https://github.com/multiarch/crossbuild ): various
cross-compilers (**Systems**: Linux, OS X, Windows, **Archs** : x86\_64,
i386, arm, ppc, mips) in Docker. OSXCross powers the Darwin builds.
2016-05-13 15:02:18 +02:00
* [Smartmontools ](https://www.smartmontools.org )
2015-12-21 16:11:37 +01:00
2013-11-13 20:47:04 +01:00
### LICENSE: ####
2014-05-10 16:17:16 +02:00
* scripts/wrapper: GPLv2
* cctools/ld64: APSL 2.0
2013-11-13 20:47:04 +01:00
* xar: New BSD
### CREDITS: ####
2015-08-22 23:15:27 +02:00
* [cjacker for the initial cctools port ](https://code.google.com/p/ios-toolchain-based-on-clang-for-linux/source/browse/#svn%2Ftrunk%2Fcctools-porting%2Fpatches )