Failed to configure LLVM for use with Musl

I want to build LLVM-based toolchain with Musl, I have LLVM sources with clang and lld (under `tools` directory) and libunwind, compiler-rt, libcxx and libcxxabi (under `projects` directory). All are the latest versions cloned from GitHub mirror, branch `master`.

I'm trying to configure with the following options:
    CLANG_DEFAULT_CXX_STDLIB = libc++
    CLANG_DEFAULT_RTLIB = compiler-rt
    LIBCXX_CXX_ABI = libcxxabi
    LLVM_DEFAULT_TARGET_TRIPLE = x86_64-pc-linux-musl
    LIBCXXABI_TARGET_TRIPLE = x86_64-pc-linux-musl
    LIBUNWIND_TARGET_TRIPLE = x86_64-pc-linux-musl
    DEFAULT_SYSROOT = /path/to/musl
    GCC_INSTALL_PREFIX = /path/to/gcc-stub
    LLVM_TARGETS_TO_BUILD = X86
    LIBCXXABI_USE_COMPILER_RT = ON
    LIBCXXABI_USE_LLVM_UNWINDER = ON
    LIBCXX_HAS_MUSL_LIBC = ON
    LIBCXX_USE_COMPILER_RT = ON
    LLVM_ENABLE_LIBCXX = ON
    LLVM_ENABLE_LLD = ON

When I try to configure, I get the following error messages:

CMake Warning at cmake/modules/HandleLLVMStdlib.cmake:24 (message):
   Can't specify libc++ with '-stdlib='
Call Stack (most recent call first):
   cmake/config-ix.cmake:15 (include)
   CMakeLists.txt:566 (include)

CMake Error at projects/libunwind/src/CMakeLists.txt:70 (message):
   Compiler doesn't support generation of unwind tables if exception support
   is disabled. Building libunwind DSO with runtime dependency on C++ ABI
   library is not supported.

I'm probably doing something wrong, but I can't figure out what exactly. If I omit some of the flags, I get different errors, but I never get successful configuration.

I'm on Ubuntu 16.04 (if it is important) and have not yet tried on other machines, but I'm pretty sure it is possible to reproduce it.

Regards,
Dmitry Golovin

I want to build LLVM-based toolchain with Musl, I have LLVM sources with clang and lld (under `tools` directory) and libunwind, compiler-rt, libcxx and libcxxabi (under `projects` directory). All are the latest versions cloned from GitHub mirror, branch `master`.

I'm trying to configure with the following options:
   CLANG_DEFAULT_CXX_STDLIB = libc++
   CLANG_DEFAULT_RTLIB = compiler-rt
   LIBCXX_CXX_ABI = libcxxabi
   LLVM_DEFAULT_TARGET_TRIPLE = x86_64-pc-linux-musl
   LIBCXXABI_TARGET_TRIPLE = x86_64-pc-linux-musl
   LIBUNWIND_TARGET_TRIPLE = x86_64-pc-linux-musl
   DEFAULT_SYSROOT = /path/to/musl
   GCC_INSTALL_PREFIX = /path/to/gcc-stub
   LLVM_TARGETS_TO_BUILD = X86
   LIBCXXABI_USE_COMPILER_RT = ON
   LIBCXXABI_USE_LLVM_UNWINDER = ON
   LIBCXX_HAS_MUSL_LIBC = ON
   LIBCXX_USE_COMPILER_RT = ON
   LLVM_ENABLE_LIBCXX = ON
   LLVM_ENABLE_LLD = ON

When I try to configure, I get the following error messages:

CMake Warning at cmake/modules/HandleLLVMStdlib.cmake:24 (message):
  Can't specify libc++ with '-stdlib='
Call Stack (most recent call first):
  cmake/config-ix.cmake:15 (include)
  CMakeLists.txt:566 (include)

^ This seems harmless. You'll probably end up building with libstdc++.

CMake Error at projects/libunwind/src/CMakeLists.txt:70 (message):
  Compiler doesn't support generation of unwind tables if exception support
  is disabled. Building libunwind DSO with runtime dependency on C++ ABI
  library is not supported.

It looks like your host compiler doesn't support -funwind-tables.

Try building with clang?

I've CC'd Saleem who might know more about this.

best,
vedant

>
> I want to build LLVM-based toolchain with Musl, I have LLVM sources with
clang and lld (under `tools` directory) and libunwind, compiler-rt, libcxx
and libcxxabi (under `projects` directory). All are the latest versions
cloned from GitHub mirror, branch `master`.
>
> I'm trying to configure with the following options:
> CLANG_DEFAULT_CXX_STDLIB = libc++
> CLANG_DEFAULT_RTLIB = compiler-rt
> LIBCXX_CXX_ABI = libcxxabi
> LLVM_DEFAULT_TARGET_TRIPLE = x86_64-pc-linux-musl
> LIBCXXABI_TARGET_TRIPLE = x86_64-pc-linux-musl
> LIBUNWIND_TARGET_TRIPLE = x86_64-pc-linux-musl
> DEFAULT_SYSROOT = /path/to/musl
> GCC_INSTALL_PREFIX = /path/to/gcc-stub
> LLVM_TARGETS_TO_BUILD = X86
> LIBCXXABI_USE_COMPILER_RT = ON
> LIBCXXABI_USE_LLVM_UNWINDER = ON
> LIBCXX_HAS_MUSL_LIBC = ON
> LIBCXX_USE_COMPILER_RT = ON
> LLVM_ENABLE_LIBCXX = ON
> LLVM_ENABLE_LLD = ON
>
> When I try to configure, I get the following error messages:
>
> CMake Warning at cmake/modules/HandleLLVMStdlib.cmake:24 (message):
> Can't specify libc++ with '-stdlib='
> Call Stack (most recent call first):
> cmake/config-ix.cmake:15 (include)
> CMakeLists.txt:566 (include)

^ This seems harmless. You'll probably end up building with libstdc++.

>
> CMake Error at projects/libunwind/src/CMakeLists.txt:70 (message):
> Compiler doesn't support generation of unwind tables if exception
support
> is disabled. Building libunwind DSO with runtime dependency on C++ ABI
> library is not supported.

It looks like your host compiler doesn't support -funwind-tables.

Hmm, so we try to generate unwind tables without exceptions as we don't
want to use exceptions in the unwinding code path itself.

It sounds like your compiler might not support `-fno-exceptions
-funwind-tables` together although it supports them together. What target
are you building libunwind for? Linux x86, x86_64 use a DWARF-esque
encoding in eh_frame. However, other targets need unwind tables with
custom information. Perhaps we are being too stringent here. At least
libitm uses both these flags together with gcc, so it and clang should
support this.

Try building with clang?

That should work.

I've CC'd Saleem who might know more about this.

If I try to compile with clang, I get this error:

CMake Error at cmake/modules/CheckAtomic.cmake:49 (message):
Host compiler must support std::atomic!
Call Stack (most recent call first):
cmake/config-ix.cmake:292 (include)
CMakeLists.txt:566 (include)

Finally I found a safe set of options that gets my toolchain to compile. Here it is:

CLANG_DEFAULT_CXX_STDLIB = libc++
CLANG_DEFAULT_RTLIB = compiler-rt
LLVM_DEFAULT_TARGET_TRIPLE = x86_64-pc-linux-musl
GCC_INSTALL_PREFIX = /path/to/gcc-stub

DEFAULT_SYSROOT = /path/to/musl
LLVM_TARGETS_TO_BUILD = X86
LLVM_ENABLE_LLD = ON

It configures and compiles fine (without a single warning!), but there are few problems left.

  1. Clang works for C code, links fine with GNU ld, but not with lld:

$ clang -fuse-ld=lld -v hello.c

clang version 4.0.0 (GitHub - llvm-mirror/clang: Mirror kept for legacy. Moved to https://github.com/llvm/llvm-project d0cab3401199d4d112c52969df80b84470e685f5) (https://github.com/llvm-mirror/llvm 52e39958c8f9fab06d2b4c9549cc20019f92a8b4)
Target: x86_64-pc-linux-musl
Thread model: posix
InstalledDir: /path/to/llvm/bin
“/path/to/llvm/bin/clang-4.0” -cc1 -triple x86_64-pc-linux-musl -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name hello.c -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -v -dwarf-column-info -debugger-tuning=gdb -resource-dir /path/to/llvm/lib/clang/4.0.0 -isysroot /path/to/musl -internal-isystem /path/to/musl/usr/local/include -internal-isystem /path/to/llvm/lib/clang/4.0.0/include -internal-externc-isystem /path/to/musl/include -internal-externc-isystem /path/to/musl/usr/include -fdebug-compilation-dir /path/to/hello -ferror-limit 19 -fmessage-length 150 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/hello-67034f.o -x c hello.c
clang -cc1 version 4.0.0 based upon LLVM 4.0.0svn default target x86_64-pc-linux-musl
ignoring nonexistent directory “/path/to/musl/usr/local/include”
ignoring nonexistent directory “/path/to/musl/usr/include”
#include “…” search starts here:
#include <…> search starts here:
/path/to/llvm/lib/clang/4.0.0/include
/path/to/musl/include
End of search list.
“/path/to/llvm/bin/ld.lld” --sysroot=/path/to/musl -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib/ld-musl-x86_64.so.1 -o a.out /path/to/musl/lib/crt1.o /path/to/musl/lib/crti.o /path/to/musl/lib/crtbegin.o -L/path/to/musl/lib /tmp/hello-67034f.o /path/to/llvm/lib/clang/4.0.0/lib/linux/libclang_rt.builtins-x86_64.a -lc /path/to/llvm/lib/clang/4.0.0/lib/linux/libclang_rt.builtins-x86_64.a /path/to/musl/lib/crtend.o /path/to/musl/lib/crtn.o
/path/to/llvm/bin/ld.lld: error: crt1.c:(function _start_c): cannot preempt symbol ‘__libc_start_main’ defined in /path/to/musl/lib/libc.so
/path/to/llvm/bin/ld.lld: error: hello.c:(function main): cannot preempt symbol ‘printf’ defined in /path/to/musl/lib/libc.so
clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

  1. libcxx’s refers to strtoll_l, strtoull_l, strtof_l, strtod_l, strtold_l that are not in musl, so for use with musl they should be replaced with strtoll, strtoull, strtof, strtod, strtold respectively.

  2. There are few other problems with compiling C++ code, but I didn’t yet figure out what causes them (probably doing something wrong again).

Regards,
Dmitry Golovin