Skip to content

cntrump/llvm-xcode-toolchain

Repository files navigation

LLVM Xcode toolchain

Clang toolchain for macOS

archs: arm64, x86_64

requried macOS version: 10.13 and higher

  • projects: clang;clang-tools-extra;cross-project-tests;libc;libclc;lld;lldb;mlir;polly
  • extra_projects: flang
  • runtimes: libunwind;libcxxabi;pstl;libcxx;compiler-rt;openmp

latest release: LLVM Xcode Toolchain

How to install LLVM toolchain

Create Toolchains directory if not exists:

sudo mkdir -p /Library/Developer/Toolchains

Copy LLVM${ver}.xctoolchain to Toolchains directory:

sudo cp -r LLVM12.0.0.xctoolchain /Library/Developer/Toolchains

Or extract LLVM-${ver}-universal.xctoolchain.tar.xz to Toolchains directory:

sudo tar -xvf LLVM-12.0.0-universal.xctoolchain.tar.xz -C /Library/Developer/Toolchains

The format of LLVM toolchain name is: org.llvm.${ver}

org.llvm.14.0.0
org.llvm.13.0.1
org.llvm.13.0.0
org.llvm.12.0.1
org.llvm.12.0.0

Using xcrun --toolchain ${name} find the tool for LLVM toolchain:

CC=$(xcrun --toolchain org.llvm.12.0.0 --find clang)
# /Library/Developer/Toolchains/LLVM12.0.0.xctoolchain/usr/bin/clang

AR=$(xcrun --toolchain org.llvm.12.0.0 --find llvm-ar)
# /Library/Developer/Toolchains/LLVM12.0.0.xctoolchain/usr/bin/llvm-ar

RANLIB=$(xcrun --toolchain org.llvm.12.0.0 --find llvm-ranlib)
# /Library/Developer/Toolchains/LLVM12.0.0.xctoolchain/usr/bin/llvm-ranlib

Xcode: see Using Alternative Toolchains

Develop requriement

Command Line Tools for Xcode contains SDK: macosx

Xcode contains SDK: macosx, iphoneos, tvos, watchos

Don't use clang -fuse-ld=lld on macOS, It's not work correctly.

Use ld builtin Command Line Tools for Xcode or Xcode instead of lld builtin llvm toolchain.

Use xcrun find SDKROOT correctly:

# macOS
clang -isysroot $(xcrun --sdk macosx --show-sdk-path)
# `xcrun --sdk macosx clang` or `SDKROOT=macosx xcrun clang`

# iOS and simulator
clang -isysroot $(xcrun --sdk iphoneos --show-sdk-path)
# `xcrun --sdk iphoneos clang` or `SDKROOT=iphoneos xcrun clang`

clang -isysroot $(xcrun --sdk iphonesimulator --show-sdk-path)
# `xcrun --sdk iphonesimulator clang` or `SDKROOT=iphonesimulator xcrun clang`

# tvOS and simulator
clang -isysroot $(xcrun --sdk appletvos --show-sdk-path)
# `xcrun --sdk appletvos clang` or `SDKROOT=appletvos xcrun clang`

clang -isysroot $(xcrun --sdk appletvsimulator --show-sdk-path)
# `xcrun --sdk appletvsimulator clang` or `SDKROOT=appletvsimulator xcrun clang`

# watchOS and simulator
clang -isysroot $(xcrun --sdk watchos --show-sdk-path)
# `xcrun --sdk watchos clang` or `SDKROOT=watchos xcrun clang`

clang -isysroot $(xcrun --sdk watchsimulator --show-sdk-path)
# `xcrun --sdk watchsimulator clang` or `SDKROOT=watchsimulator xcrun clang`

How to use pstl

Build a pstl demo for example:

pstl requried c++17 and tbb.

tbb requried macOS 10.14 and higher.

CXX="$(xcrun --toolchain org.llvm.12.0.0 --find clang++)"
LLVM_HOME="$(dirname $CXX)/.."

${CXX} -target apple-macosx10.14 \
      -arch arm64 -arch x86_64 \
      -isysroot $(xcrun --sdk macosx --show-sdk-path) \
      -std=gnu++17 -D_LIBCPP_HAS_PARALLEL_ALGORITHMS \
      -I${LLVM_HOME}/include \
      -I/usr/local/include \
      -L/usr/local/lib \
      -ltbb -lc++ \
      main.cpp

Or using xcrun auto configure environment:

LLVM_HOME="$(dirname $(xcrun --toolchain org.llvm.12.0.0 --find clang))/.."

xcrun --toolchain org.llvm.12.0.0 --sdk macosx \
      clang -target apple-macosx10.14 \
      -arch arm64 -arch x86_64 \
      -std=gnu++17 -D_LIBCPP_HAS_PARALLEL_ALGORITHMS \
      -I${LLVM_HOME}/include \
      -I/usr/local/include \
      -L/usr/local/lib \
      -ltbb -lc++ \
      main.cpp

Test pstl:

❯ ./a.out 10000000

parallel 0.752187 s
serial   1.53965 s

How to build app for Mac Catalyst

Mac Catalyst requried: clang version >= 13, -mmaccatalyst-version-min >= 13.1.

[clang][driver][darwin] Add driver support for Mac Catalyst

xcrun --toolchain org.llvm.13.0.0 --sdk macosx \
      clang -target apple-ios13.1-macabi \
            -arch arm64 -arch x86_64 \
            -lc++ -lSystem \
            main.cc

Mac Catalyst must be signed before running:

Signing identity lldb_codesign is created by macos-setup-codesign.sh

codesign -s lldb_codesign a.out

How to build LLVM toolchain

Required: Xcode 13 with C++20 support

Install CMake and Ninja:

Using Macports:

sudo port install cmake ninja

Using Brew:

brew install cmake ninja

My Macports installed at /opt/local, so I added -DCMAKE_IGNORE_PREFIX_PATH="/opt/local" in build-llvm.sh.

Build dependencies: swig, libedit, libncurses, libtbb, zstd

./bootstrap

Build clang toolchain:

./build.sh 14.0.0  # build LLVM 14.0.0

BYW

Since LLVM 15, pre-built arm64 binaries is available in the Official Releases Page.

If you using lldb built by llvm.org, liblldb.dylib link Python with absolute path:

clang+llvm-15.0.3-arm64-apple-darwin21.0 % otool -L lib/liblldb.15.0.3.dylib
lib/liblldb.15.0.3.dylib:
	@rpath/liblldb.15.0.3.dylib (compatibility version 0.0.0, current version 15.0.3)
	/usr/lib/libcompression.dylib (compatibility version 1.0.0, current version 1.0.0)
	/opt/homebrew/opt/[email protected]/Frameworks/Python.framework/Versions/3.10/Python (compatibility version 3.10.0, current version 3.10.0)
	/usr/lib/libncurses.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
	/usr/lib/libform.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
	/usr/lib/libpanel.5.4.dylib (compatibility version 5.4.0, current version 5.4.0)
	/usr/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.9.0)
	/usr/lib/libedit.3.dylib (compatibility version 2.0.0, current version 3.0.0)
	/usr/lib/liblzma.5.dylib (compatibility version 6.0.0, current version 6.3.0)
	/System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1858.112.0)
	/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1858.112.0)
	/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices (compatibility version 1.0.0, current version 1141.1.0)
	/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 60158.100.133)
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)
	/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1300.23.0)
	/usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

About

LLVM toolchain for Xcode, both arm64 and x86_64.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages