- Notifications
You must be signed in to change notification settings - Fork334
Mac OS X cross toolchain for Linux, FreeBSD, OpenBSD and Android (Termux)
License
tpoechtrager/osxcross
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The goal of OSXCross is to provide a well working macOS cross toolchain forLinux
,FreeBSD
,OpenBSD
, andAndroid (Termux)
.
OSXCross worksonx86
,x86_64
,arm
andAArch64
/arm64
,
and is able totargetarm64
,arm64e
,x86_64
,x86_64h
andi386
.
arm64
requires macOS 11.0 SDK (or later).arm64e
requires a recent Apple clang compiler.
There is also appc
test branch that has recently seen some daylight.
For cross-compiling for macOS you need
- the Clang/LLVM compiler
- the cctools (lipo, otool, nm, ar, ...) and ld64
- the macOS SDK.
Clang/LLVM is a cross compiler by defaultand is now available on nearly every Linux distribution, so we justneed a properport ofthe cctools/ld64 and the macOS SDK.
OSXCross includes a collection of scripts for preparing the SDK andbuilding the cctools/ld64.
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 macOS,
- the "compiler-rt" runtime library, and
- the
llvm-dsymutil
tool required for debugging.
Note: The "compiler-rt" library can be needed to link code that uses the__builtin_available()
runtime version check.
Basically everything you can build on macOS with clang/gcc should build withthis cross toolchain as well.
OSXCross comes with a minimalistic MacPorts Package Manager.SeeREADME.MACPORTS for more.
Move yourpackaged SDKto the tarballs/ directory.
Then ensure you have the following installed on your system:
Clang 3.9+
,cmake
,git
,patch
,Python
,libssl-dev
(openssl)lzma-dev
,libxml2-dev
,xz
,bzip2
,cpio
,libbz2
,zlib1g-dev
and thebash shell
.
You can run 'sudo tools/get_dependencies.sh' to get these (and theoptional packages) automatically. (outdated)
Optional:
llvm-devel
: For Link Time Optimization supportllvm-devel
: For ld64-bitcode_bundle
supportuuid-devel
: For ld64-random_uuid
support
On Ubuntu trusty you must upgrade CMake to >= 3.2.3 first. Or do this:
curl -sSL https://cmake.org/files/v3.14/cmake-3.14.5-Linux-x86_64.tar.gz| sudo tar -xzC /optexport PATH=/opt/cmake-3.14.5-Linux-x86_64/bin:$PATH
OSXCross usesclang
as the default compiler for building its tools, and alsoas a cross-compiler to create macOS binaries.
Inclang
there is no difference between cross-compilation and nativecompilation, so OSXCross can use a normalclang
install for both. You canuse either aclang
installation you already have, or build your own fromsource.
To build and install your ownclang
from a recent source tree, usinggcc
,run:
./build_clang.sh# Builds a mainline version of Clang ./build_apple_clang.sh# Builds Apple's version of Clang
This installsclang
into/usr/local
. If you want to install somewhereelse, set theINSTALLPREFIX
variable. For example:
INSTALLPREFIX=/opt/clang ./build_clang.sh
To build the cross toolchain (usingclang
), run:
./build.sh
This installs the osxcross toolchain into<path>/target
. If you want to install somewhereelse, set theTARGET_DIR
variable. For example:
TARGET_DIR=/usr/local/osxcross ./build.sh
And/Or, set variableUNATTENDED
to1
to skip the prompt and proceed straight tothe build:
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 thatyou can invoke the cross-compiler.
That's it. See usage examples below.
If you also want to build GCC as a cross-compiler, you can do that by running:
./build_gcc.sh
The script lets you select a GCC version by setting the variableGCC_VERSION
.By default you get C and C++ compilers, but you can tell the script to build aFortran compiler as well:
GCC_VERSION=5.2.0 ENABLE_FORTRAN=1 ./build_gcc.sh
[A gfortran usage example can be foundhere]
Before you do this, make sure you have the GCC build depedencies installed onyour system.
On debian like systems you can install these using:
sudo apt-get install gcc g++ zlib1g-dev libmpc-dev libmpfr-dev libgmp-dev
ATTENTION:
OSXCross does not enable-Werror=implicit-function-declaration
by default.
You can emulate Xcode 12's behavior by setting the environmental variableOSXCROSS_ENABLE_WERROR_IMPLICIT_FUNCTION_DECLARATION
to 1.
OSXCross links libgcc and libstdc++ statically by default (this affects-foc-use-gcc-libstdc++
too). You can turn this behavior off withOSXCROSS_GCC_NO_STATIC_RUNTIME=1
(env).
The build also creates aliases*-g++-libc++
which link with theclang
implementation of the C++ standard library instead of the GCC version. Don'tuse these variants unless you know what you're doing.
Please ensure you have read and understood the Xcode licenseterms before continuing.
The SDKs can be extracted either from full Xcode or from Command LineTools for Xcode.
- [Download Xcode:https://developer.apple.com/download/all/?q=xcode] **
- [Mount Xcode.dmg (Open With -> DiskImageMounter) ***]
- Run:
./tools/gen_sdk_package.sh
(from the OSXCross package) - Copy the packaged SDK (*.tar.* or *.pkg) on a USB Stick
- (On Linux/BSD) Copy or move the SDK into the tarballs/ directory ofOSXCross.
**
-- Xcode up to 16.1 Beta 1 is known to work.
-- Use Firefox if you have problems signing in.
***
-- If you get a dialog with a crossed circle, ignore it.
-- You don't need to install Xcode.
Step 1. and 2. can be skipped if you have Xcode installed.
- Install a recent version of Bash from MacPorts or Tigerbrew
- Download Xcode 4.2 for Snow Leopard
- Mount the disk image with DiskImageMounter or by running
hdiutil attach <xcode>.dmg
- Run:
XCODEDIR=/Volumes/Xcode ./tools/gen_sdk_package.sh
- (On Linux/BSD) Copy or move the SDK into the tarballs/ directory ofOSXCross.
This method may require up to 45 GB of free disk space.
An SSD is recommended for this method.
- Download Xcode like described in 'Packaging the SDK on macOS'
- Install
clang
,make
,libssl-devel
,lzma-devel
andlibxml2-devel
- Run
./tools/gen_sdk_package_pbzx.sh <xcode>.xip
- Copy or move the SDK into the tarballs/ directory
- Download Xcode like described in 'Packaging the SDK on macOS'
- Install
cmake
,libxml2-dev
andfuse
- Run
./tools/gen_sdk_package_darling_dmg.sh <xcode>.dmg
- Copy or move the SDK into the tarballs/ directory
- Download Xcode like described in 'Packaging the SDK on macOS'
- Ensure you have
clang
andmake
installed - Run
./tools/gen_sdk_package_p7zip.sh <xcode>.dmg
- Copy or move the SDK into the tarballs/ directory
- Download Xcode 4.2 for Snow Leopard
- Ensure you are downloading the "Snow Leopard" version
- Install
dmg2img
- Run (as root):
./tools/mount_xcode_image.sh /path/to/xcode.dmg
- Follow the instructions printed by
./tools/mount_xcode_image.sh
- Copy or move the SDK into the tarballs/ directory
- [Download Xcode Command Line Tools:https://developer.apple.com/download/more] ****
- [Mount Command_Line_Tools_for_Xcode.dmg (Open With -> DiskImageMounter)]
- [Install "Command Line Tools.pkg" (Open With -> Installer)]
- Run:
./tools/gen_sdk_package_tools.sh
(from the OSXCross package) - Copy the packaged SDK (*.tar.* or *.pkg) on a USB Stick
- (On Linux/BSD) Copy or move the SDK into the tarballs/ directory ofOSXCross.
****-- Xcode command line tools 12.x are known to work.
Steps 1. to 3. can be skipped if you have Xcode Command line toolsalready installed (e.g., auto-installed by runninggit
orgcc
command from command-line).
This method may require up to 45 GB of free disk space.An SSD is recommended for this method.
- Download Xcode Command Line Tools like described in 'Packaging the SDK from Xcode Command Line Tools on macOS'
- Install
clang
,make
,libssl-devel
,lzma-devel
andlibxml2-devel
- Run
./tools/gen_sdk_package_tools_dmg.sh <command_line_tools_for_xcode>.dmg
- Copy or move the SDK into the tarballs/ directory
Clang:
- 32 bit:
o32-clang++ test.cpp -O3 -o test
ORi386-apple-darwinXX-clang++ test.cpp -O3 -o test
- 64 bit:
o64-clang++ test.cpp -O3 -o test
ORx86_64-apple-darwinXX-clang++ test.cpp -O3 -o test
- 32 bit:
GCC:
- 32 bit:
o32-g++ test.cpp -O3 -o test
ORi386-apple-darwinXX-g++ test.cpp -O3 -o test
- 64 bit:
o64-g++ test.cpp -O3 -o test
ORx86_64-apple-darwinXX-g++ test.cpp -O3 -o test
- 32 bit:
Clang:
- arm64:
oa64-clang++ test.cpp -O3 -o test
ORarm64-apple-darwinXX-clang++ test.cpp -O3 -o test
- arm64e:
oa64e-clang++ test.cpp -O3 -o test
ORarm64e-apple-darwinXX-clang++ test.cpp -O3 -o test
- arm64:
XX= the target version, you can find it out by runningosxcross-conf
andthen seeTARGET
.
You can use the shortcutso32-...
fori386-apple-darwin...
, depending onwhich you prefer.
I'll continue from here on witho32-clang
, but remember,you can simply replace it witho32-gcc
ori386-apple-darwin...
.
make CC=o32-clang CXX=o32-clang++
CC=o32-clang CXX=o32-clang++ ./configure --host=i386-apple-darwinXX
Note: libc++ requires macOS 10.7 or later! If you really need C++11 foran older macOS version, then you can do the following:
- Build GCC so you have an up-to-date libstdc++
- Build your source code with GCC or
clang++-gstdc++
/clang++ -foc-use-gcc-libstdc++
Usage Examples:
Clang:
- C++98:
o32-clang++ -stdlib=libc++ -std=c++98 test.cpp -o test
- 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++17:
o32-clang++ -stdlib=libc++ -std=c++17 test1.cpp -o test
- C++2a:
o32-clang++ -stdlib=libc++ -std=c++20 test1.cpp -o test
- C++98:
Clang (shortcut):
- C++98:
o32-clang++-libc++ -std=c++98 test.cpp -o test
- C++11:
o32-clang++-libc++ -std=c++11 test.cpp -o test
- C++14:
o32-clang++-libc++ -std=c++14 test.cpp -o test
- C++17:
o32-clang++-libc++ -std=c++17 test.cpp -o test
- C++2a:
o32-clang++-libc++ -std=c++20 test.cpp -o test
- C++98:
GCC
- C++11:
o32-g++-libc++ -std=c++11 test.cpp
- C++14:
o32-g++-libc++ -std=c++14 test.cpp -o test
- C++17:
o32-g++-libc++ -std=c++17 test.cpp -o test
- C++2a:
o32-g++-libc++ -std=c++20 test.cpp -o test
- C++11:
- 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
- Clang:
o64-clang++ test.cpp -O3 -arch i386 -arch x86_64 -o test
- GCC:
- 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
- use lipo to generate the universal binary:
x86_64-apple darwinXX-lipo -create test.i386 test.x86_64 -output test
- build the 32 bit binary:
The default deployment target is:
SDK <= 10.13:macOS 10.6
SDK >= 10.14:macOS 10.9
However, there are several ways to override the default value:
- by passing
OSX_VERSION_MIN=10.x
to./build.sh
- by passing
-mmacosx-version-min=10.x
to the compiler - by setting the
MACOSX_DEPLOYMENT_TARGET
environment variable
>= 10.9 also defaults tolibc++
instead oflibstdc++
,
this behavior can be overriden by explicitly passing-stdlib=libstdc++
to clang.
x86_64h defaults tomacOS 10.8
and requires clang 3.5+.
x86_64h = x86_64 with optimizations for the Intel Haswell Architecture.
- multiarch/crossbuild:
various cross-compilers
(Systems: Linux, macOS, Windows,Archs: x86_64,i386, arm, ppc, mips)
in Docker. OSXCross powers the Darwin builds. - Smartmontools
- scripts/wrapper: GPLv2
- cctools/ld64: APSL 2.0
- xar: New BSD