Embedded Clang


Updated: 20190426

As we're exploring bringing up a C/C++ runtime on our system, I'd like to share a very helpful resource for those using clang/llvm: compiler-rt.

Compiler-rt is an LLVM project that provides implementations of various builtin functions for a variety of architectures. This saves us a lot of heavy lifting when bringing up a new platform, as we can link compiler-rt instead of re-implementing these functions.

While most useful as a complete library, compiler-rt is also a useful source code resource if you need to implement these builtins with a different toolchain. Simply import the required builtin source into your project.

I'll let the compiler-rt project describe the builtins they provide:

builtins - a simple library that provides an implementation of the low-level target-specific hooks required by code generation and other runtime components. For example, when compiling for a 32-bit target, converting a double to a 64-bit unsigned integer is compiling into a runtime call to the __fixunsdfdi function. The builtins library provides optimized implementations of this and other low-level routines, either in target-independent C form, or as a heavily-optimized assembly.

builtins provides full support for the libgcc interfaces on supported targets and high performance hand tuned implementations of commonly used functions like __floatundidf in assembly that are dramatically faster than the libgcc implementations. It should be very easy to bring builtins to support a new target by adding the new routines needed by that target.

Table of Contents:

  1. Prerequisites
  2. Getting compiler-rt
  3. Building compiler-rt
  4. Using compiler-rt in Your Project
  5. Embedded Artistry compiler-rt
  6. Further Reading
  7. Change Log


You will need the llvm-config binary on your platform. This binary is provided when you install llvm.

If you're using OSX, note that Apple does not provide llvm-config with Xcode, so you will need to install mainline llvm to get this binary. See my notes on installing clang/llvm on OSX.

Getting compiler-rt

You can checkout the compiler-rt source with svn:

svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt

If you prefer git, check out the github mirror:

git clone git@github.com:llvm-mirror/compiler-rt.git

I'll leave the folder structure descriptions to the compiler-rt team:

include/ contains headers that can be included in user programs (for example, users may directly call certain function from sanitizer runtimes).
lib/ contains libraries implementations.
lib/builtins is a generic portable implementation of builtins routines.
lib/builtins/(arch) has optimized versions of some routines for the supported architectures.
test/ contains test suites for compiler-rt runtimes.

The lib/builtins/ folder contains the source for the various builtin functions. You can use these items piecemeal in your repository. This is useful if you just need to port specific functions or don't want to deal with installing clang or compiling compiler-rt.

Building compiler-rt

For those who are interested in the compiler-rt builtins library, let's continue our journey.

Once you have llvm-config on your system, you can build compiler-rt with the following commands:

$ mkdir build
$ cd build
$ cmake ../compiler-rt -DLLVM_CONFIG_PATH=/path/to/llvm-config
$ make

The build diectory is important - it's where cmake will place the resulting files.

For those following with homebrew, you could use this command:

$ cmake ../compiler-rt -DLLVM_CONFIG_PATH=$(brew --prefix llvm)/bin/llvm-config
$ make

If you don't have llvm-config, you can still build the project. For cross-compiling, follow the instructions here to configure your CMake build directory.

By default, make will build everything. If you want to build a limited subset, you can run make help and pick the specific items you want to build.

If you want to install libraries, run this additional command:

$ make install

I usually do not run the make install step.

Finding the Right Library

After your build is completed, change to the lib/builtin directory in your build folder. There you will likely see a massive list of files. Here's my example output from compiling with Apple Clang on OSX:


You only need one of these for your system, likely. Which one do you pick?

Here's a quick decoder:

  • hard vs soft: this is floating point. Is your platform configured to support hard or soft floating point operations?
  • static vs pic: is the code compiled as a static library, or are you compiling with position-independent-code? (PIC)
  • i386 vs armv7x: this will be dependent upon your platform's processor. You need to pick the instruction set to match.
  • The last portion of the name is the library format.

Generally, I end up picking this library for my purposes if I am compiling and linking on OSX:


Note the "macho embedded" format - this requires special parsing to use with your embedded system. We will investigate MACHO files further in a future article.

Using compiler-rt in your project

Since compiler-rt builtin libraries do not regularly need updates, I recommend pre-compiling compiler-rt into a library file that can be linked against in your project. It may be worth it to build compiler-rt on your build machine so you have a known source to retrieve updates from.

Once you have built compiler-rt, you can copy the desired library to your project's repository.

You will need to add the -L linker flag to get the location into the library search path. The -l linker flag can be used to include the library itself: -lcompiler_rt.

If you built compiler-rt on OSX, you ended up with a bunch of macho libraries. The macho format will require additional handling that will be described in a future article.

Embedded Artistry compiler-rt

We use Meson for our projects. We have a compiler-rt project that builds with Meson which will build for your native system. Cross-compilation for ARM is also supported using cross-files.

The Embedded Artistry compiler-rt produces static libraries only, because that's what we use on our embedded systems.

To build for your host machine, simply run make (after installing Meson):

$ make

For cross-compilation, you will need to supply a cross-compilation file when creating the build results directory. Some samples are provided in the build/cross folder, and you can create your own as needed.

meson buildresults --cross-file=build/cross/gcc/arm/nrf52840.txt

Change into the buildresults directory and build:

$ cd buildresults
$ ninja

In both cases, the static libraries will be in buildresults. If you enabled a cross-compilation build, a native llibrary and cross-compiled library will present.

Further Reading

Change Log

  • 20190426:
    • Added notes for cross-compiling on ARM
    • Added Table of Contents
    • Added notes on Embedded Artistry compiler-rt repo

Installing LLVM/Clang on OSX

Updated: 20190627

If you are a developer using clang or gcc on OS X, the odds are good that you are using the Xcode build tools.

These tools work well when compiling for your host machine. You can even get the iPhone SDK to work well enough for embedded development using ARM. However, I have encountered many cases where using the Apple clang compiler has resulted in odd behavior or unexpected errors after updating Xcode.

As a result, I recommend using the mainline clang for your embedded development. Using mainline clang on my system proved to take a little more work than I expected, so I hope my notes make setup easier for you.

Manual Compile + Install

If you would like to go the manual compile+install route, please check this guide from the LLVM group. The manual compile+install instructions are also applicable for those not using OS X.

Installing with Homebrew

I use Homebrew to manage extra packages, and I highly recommend it.

Using Homebrew, we can install llvm/clang very easily. Simply run this command:

$ brew install --with-toolchain llvm

It may be worth running the following command first if you already have an existing homebrew installation:

$ brew info llvm

If you see an older llvm version, I recommend updating Homebrew. Your taps may need to be updated to pick up the newest versions.

To update Homebrew, simply run:

$ brew update
$ brew upgrade

After that, you should be able to check the llvm information again and see a newer version.

Where to find llvm/clang

When you install llvm with brew, the new binaries will not automatically be in the path. Note what the formula says:

OS X already provides this software and installing another version in parallel can cause all kinds of trouble.

You can find the binaries here:

$(brew --prefix llvm)/bin

If you want the Homebrew llvm/clang to show up in your PATH, run the following command:

$ echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile

Building and Linking

Here are the compiler variables you should use for Homebrew clang:

export CC := clang
export CXX := $(CC)++

You could also specify the path manually if you don't want to edit your PATH variable:

export CC := /usr/local/opt/llvm/bin/clang
export CXX := $(CC)++

Note that if your tools look for ld or ar, you will likely end up using the Apple tools. Make sure you change your compiler settings to llvm-ar and llvm-ld if you want to utilize the mainline tools.

You will need to add the following flags for compiling and linking:

LDFLAGS += -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib
CPPFLAGS += -I/usr/local/opt/llvm/include -I/usr/local/opt/llvm/include/c++/v1/

You can also check whether brew clang is actually installed before adding these flags. This will help you support users who install clang by other means.

ifeq ($(shell brew info llvm 2>&1 | grep -c "Built from source on"), 1)
#we are using a homebrew clang, need new flags
LDFLAGS += -L/usr/local/opt/llvm/lib -Wl,-rpath,/usr/local/opt/llvm/lib
CPPFLAGS += -I/usr/local/opt/llvm/include -I/usr/local/opt/llvm/include/c++/v1/

If you're getting errors about missing headers like assert.h, make sure to run xcode-select --install. This will populate /usr/include correctly.

Further Reading

Update Log

  • 20190627:
    • Corrected OSX to OS X (Thanks Peter Mortensen)
    • Removed deprecated reference to the embedded-resources Makefile

Clang: Manually Disable Warnings in a Specific Location

Utilizing public or generated code can often introduce warnings into your software builds. Many of these warnings are not serious and developers will often ignore them. This pollutes the warning report and prevents developers from noticing useful warnings in their own software.

Using clang, you can suppress warnings using a #pragma:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wfloat-equal -Wdeprecated"
#include <annoying_warning_creating_header.h>
#pragma clang diagnostic pop

I've often used this approach to silence acceptable warnings from external code.