Using the Latest LLVM Release on MacOS

October 2018 ยท 3 minute read

MacOS is really frustrating with how it handles its libraries and compilers. It is also frustrating because it ships an unspecified version of LLVM, which generally isn’t the latest stable release. You can, however, with a little tweaking, use the latest version of LLVM or GCC on your Mac, and reliably use it for your C and C++ tooling.

Installation

First, you need to install the latest version of LLVM. Most people nowadays are using Homebrew. If you don’t have it, you can install LLVM from source, which takes a lot of time to compile. If you have brew, you can install LLVM with

brew install llvm

You have several options for installing LLVM, all of which will require brew to compile it from source. I usually install the latest stable version of LLDB and I build it against Homebrew’s Python 2.

brew install llvm --with-python@2 --with-lldb

This took me roughly two hours to build on a 2015 rMBP 13" with an i5.

Once it’s done, add Brew’s LLVM to your path:

echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.zshrc
# or ~/.bashrc depending on your shell

Environment variables

Most build tools (namely CMake and Make) respect certain environment variables, so you should set these once you have the LLVM suite in your path.

I usually go ahead and set up aliases so I can conveniently compile small programs from the command line with the latest version of clang.

For environment variables:

export CC=clang
export CXX=clang++
export LD=ld.lld
export AR=llvm-ar
export RANLIB=llvm-ranlib

Note: if you don’t have LLVM in your $PATH, then you will need to add the full path to your environment variables

These are the aliases I set:

alias cc=$CC
alias c++=$CXX
alias ld=$LD
alias ar=$AR
alias ranlib=$RANLIB

Working with XCode

If you use XCode or xcodebuild, then you will realize that it does not use the versions of LLVM/Clang that you set up in the environment. The LLVM project actually provides a way to build an XCode toolchain that contains everything you need to switch XCode to the latest versions of clang and the other tools you need to compile your projects.

In order to do this, you need to manually build LLVM from source and build the toolchain.

You can follow this tutorial which shows you how to build LLVM from the Github mirrors.

When you get to section 3, change the cmake command slightly so you enable the XCode toolchain target:

mkdir -p build
cd build
cmake -G Ninja \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DLLVM_CREATE_XCODE_TOOLCHAIN=On

I also recommend using Ninja rather than Make to build LLVM, because it will build significantly faster.

Now that you have the XCode toolchain, you can place it in the Toolchains directory in XCode (bump the version as necessary, this guide may not always be up to date).

mv LLVM8.0.1.xctoolchain /Applications/Xcode.app/Contents/Developer/Toolchains

You need to instruct XCode to actually use the toolchain. You can do so in two ways: from your environment variables, and through the XCode app itself.

To set it through an environment variable:

export TOOLCHAINS="LLVM8.0.1"

In Xcode.app, you can select Xcode -> Toolchains -> org.llvm.8.0.1 in the menu.

These steps should generally work with your standard C/C++ Make, CMake, and Xcode setups, though there can be a lot of quirks because of the way people install libraries and how Apple sets up their compiler suite.