Android/SharkBait/Clang toolchain with sanitizers support for Android

Toolchain work for Android has been progressing in the last few weeks, finally reaching a stage where a completely working Clang/LLVM toolchain is available. The toolchain can build test programs that run on vanilla AOSP as well as Lineage OS, with proper sanitizer support. Though actually plugging this toolchain into Android build process is not tested yet, the sanitizers for the toolchain is built with the toolchain itself, and various problems in the headers has been fixed. This article explains complex parts in the bootstrap script as well as places where manual intervention is needed.

We're going to build pieces of the toolchain in this order to satisfy dependency:


 * GCC toolchain (binutils, libc objects &amp; headers, compiler)
 * LLVM + Clang
 * Compiler-rt builtins
 * Libunwind
 * Libc++abi
 * Libc++
 * Compiler-rt non-builtins (sanitizers, profilers, etc.)

Set up environment and repositories
This part defaults to using  as the target and using the HEAD version of LLVM tools. Edit accordingly if this is not desired. CMake and Ninja is required to follow this guide; install them if they're not present on your system.

Set up GCC for target
GCC is needed for cross compiling with Clang/LLVM. Follow this article on GCC cross-compiling for Android to get a working copy of cross GCC. This should get you through the comment block that reads:

Note that later Clang/LLVM build requires prebuilt libraries in two different locations. Copy the object files accordingly.

LLVM + Clang
Configure options to note:


 * : Enable only AArch64 target. Remember to substitute this if the target is not  (e.g.   for 32 bit)

Compiler-rt builtins
Configure options to note:


 * : Install to Clang &quot;resource path&quot; so that libraries can be automatically found by Clang while compiling.
 * and : Build builtins only as other components require   to build.

Libunwind
Nothing special here.

Libcxxabi
Configure options to note:


 * : Specify libc++ header path for reference by libc++abi during build.
 * : This option name speaks for itself.
 * : This option name speaks for itself.

Remember to install libc++abi headers into the prefix as CMake doesn't generate libc++abi header install rules:

Libcxx
As discussed in this thread, LLVM HEAD at the time of writing uses NDK headers that are newer than the prebuilt NDK version (r16 vs r14). As a result, we have to rebase to remove commit  from Libcxx repository:

Configure options to note:


 * : Use  as the C++ ABI library (instead of   or  ).
 * : Reference to  headers (installed in the previous step).
 * : This option name speaks for itself.

Note that Android by default combines libc++, libc++abi, and libunwind into a single. We'll do this as well so that the executable does not contain stray dynamic link references.

Compiler-rt non-builtin (sanitizers, etc.)
Bionic headers as of commit  misses definition for   in its headers. It is possible that the Linux headers are expected to define this, but  did not define this. Apply this patch on Bionic headers to continue.

Configure options to note:


 * : Install to Clang &quot;resource path&quot; so that libraries can be automatically found by Clang while compiling.
 * and : Build components other than builtins as we have   now.