-
-
Save ian-mcdowell/988a56e1d2b738a5e1d18fc6c7164575 to your computer and use it in GitHub Desktop.
Cross-compiling LLVM for iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Bail out on error | |
set -e | |
LLVM_SRC=http://releases.llvm.org/6.0.0/llvm-6.0.0.src.tar.xz | |
CLANG_SRC=http://releases.llvm.org/6.0.0/cfe-6.0.0.src.tar.xz | |
LIBCXX_SRC=http://releases.llvm.org/6.0.0/libcxx-6.0.0.src.tar.xz | |
LIBCXXABI_SRC=http://releases.llvm.org/6.0.0/libcxxabi-6.0.0.src.tar.xz | |
LLVM_SRCDIR=$(pwd)/llvm_src | |
OSX_BUILDDIR=$(pwd)/build_osx | |
IOS_BUILDDIR=$(pwd)/build_ios | |
IOS_SDKROOT=$(xcrun --sdk iphoneos --show-sdk-path) | |
# Check for ninja requirement | |
if [ ! hash ninja 2>/dev/null ]; then | |
echo "Ninja was not found. Please install before executing this script." | |
exit 1 | |
fi | |
# Parse arguments | |
for i in "$@" | |
do | |
case $i in | |
-c|--clean) | |
CLEAN=YES | |
shift | |
;; | |
*) | |
# unknown option | |
;; | |
esac | |
done | |
# Download LLVM into llvm_src | |
if [ ! -d $LLVM_SRCDIR ]; then | |
mkdir $LLVM_SRCDIR | |
curl $LLVM_SRC | tar xz -C $LLVM_SRCDIR --strip-components 1 | |
fi | |
pushd $LLVM_SRCDIR | |
# get clang | |
pushd tools | |
if [ ! -d clang ]; then | |
mkdir clang | |
curl $CLANG_SRC | tar xz -C clang --strip-components 1 | |
fi | |
popd | |
# Get libcxx and libcxxabi | |
pushd projects | |
if [ ! -d libcxx ]; then | |
mkdir libcxx | |
curl $LIBCXX_SRC | tar xz -C libcxx --strip-components 1 | |
fi | |
if [ ! -d libcxxabi ]; then | |
mkdir libcxxabi | |
curl $LIBCXXABI_SRC | tar xz -C libcxxabi --strip-components 1 | |
fi | |
popd | |
# End downloading source | |
popd | |
# compile for OSX (about 6h, 18GB of disk space) | |
if [ $CLEAN ]; then | |
rm -rf $OSX_BUILDDIR | |
fi | |
if [ ! -d $OSX_BUILDDIR ]; then | |
mkdir $OSX_BUILDDIR | |
fi | |
pushd $OSX_BUILDDIR | |
cmake -GNinja $LLVM_SRCDIR | |
time ninja | |
popd | |
# get libcxx and libcxxabi out of the way: | |
rm -rf dontBuild | |
mkdir dontBuild | |
mv llvm_src/projects/libcxx dontBuild | |
mv llvm_src/projects/libcxxabi dontBuild | |
# TODO: some combination of build variables might allow us to build these too. | |
# Right now, they fail. Maybe CFLAGS with: -D__need_size_t -D_LIBCPP_STRING_H_HAS_CONST_OVERLOADS | |
# Now, compile for iOS using the previous build: | |
# About 24h, 13 GB of disk space | |
# Flags you could use: LLVM_LINK_LLVM_DYLIB and BUILD_SHARED_LIBS, to make everything use dynamic libraries | |
# (I did not test these) | |
if [ $CLEAN ]; then | |
rm -rf $IOS_BUILDDIR | |
fi | |
if [ ! -d $IOS_BUILDDIR ]; then | |
mkdir $IOS_BUILDDIR | |
fi | |
pushd $IOS_BUILDDIR | |
cmake -GNinja -DLLVM_TARGET_ARCH=ARM \ | |
-DLLVM_TARGETS_TO_BUILD=ARM \ | |
-DLLVM_DEFAULT_TARGET_TRIPLE=arm64-apple-darwin10 \ | |
-DLLVM_ENABLE_THREADS=OFF \ | |
-DLLVM_TABLEGEN=${OSX_BUILDDIR}/bin/llvm-tblgen \ | |
-DCLANG_TABLEGEN=${OSX_BUILDDIR}/bin/clang-tblgen \ | |
-DCMAKE_OSX_SYSROOT=${IOS_SDKROOT} \ | |
-DCMAKE_C_COMPILER=${OSX_BUILDDIR}/bin/clang \ | |
-DCMAKE_LIBRARY_PATH=${OSX_BUILDDIR}/lib/ \ | |
-DCMAKE_INCLUDE_PATH=${OSX_BUILDDIR}/include/ \ | |
-DCMAKE_C_FLAGS="-arch arm64 -target arm64-apple-darwin10 -I${OSX_BUILDDIR}/include/ -miphoneos-version-min=11" \ | |
-DCMAKE_CXX_FLAGS="-arch arm64 -target arm64-apple-darwin10 -I${OSX_BUILDDIR}/include/c++/v1/ -miphoneos-version-min=11" \ | |
$LLVM_SRCDIR | |
time ninja | |
popd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment