It’s been a journey. I’m developing passes for LLVM, so I need the source + compiled version of clang in my machine. Following the wiki proved to be outdated, and I’ve been solving on issue at a time from that method. Initially we are to:
- Clone the llvm-project repo, I use the version 22 branch and depth flag to spare some disk:
[bernborgess@e14:~/test]$ git clone https://github.com/llvm/llvm-project \
--depth=1 --branch=release/22.x
- Insert the
shell.nixfrom the wiki - Enter the shell
[bernborgess@e14:~/test]$ nix-shell
evaluation warning: 'targetPlatform' has been renamed to/replaced by 'stdenv.targetPlatform'
[nix-shell:~/test]$
- Create build directory and configure with cmake, using the shell’s flags
[nix-shell:~/test]$ mkdir build && cd build && cmake $cmakeFlags ../llvm-project/llvm
First error I found is:
GCC_INSTALL_PREFIX is deprecated and will be removed
We just remove this flag. Then starting compilation complaigns:
LIBCXXABI_USE_LLVM_UNWINDER is set to ON, but libunwind is not specified in
LLVM_ENABLE_RUNTIMES.
Then I add libunwind to the flags but then we get the error
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.
Guess I’ll try to turn it OFF then!
-DLIBCXXABI_USE_LLVM_UNWINDER=OFF
Now at last, I’m building with all the modifications but I get this error at the end:
/nix/store/p2vkw5s89ff1fs2d2rxqxiqil9s0jpcm-binutils-2.46/bin/ld.bfd: cannot find crti.o: No such file or directory
/nix/store/p2vkw5s89ff1fs2d2rxqxiqil9s0jpcm-binutils-2.46/bin/ld.bfd: cannot find crtbeginS.o: No such file or directory
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
I would like help understanding THIS, why does it fail at linking? Why is it using clang++, given I specified to use g++? Messing with the $LIBRARY env var didn’t help. For reference this is the final shell.nix I ended up working with:
with import <nixpkgs> {};
let
gccForLibs = stdenv.cc.cc;
in stdenv.mkDerivation {
name = "llvm-env";
buildInputs = [
bashInteractive
python3
ninja
cmake
llvmPackages_latest.llvm
];
NIX_LDFLAGS="-L${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} ";
CFLAGS="-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -B ${stdenv.cc.libc}/lib";
LIBRARY_PATH="${stdenv.cc.libc}/lib";
cmakeFlags = [
#"-DGCC_INSTALL_PREFIX=${gcc}" GCC_INSTALL_PREFIX is deprecated and will be removed.
"-DCMAKE_C_COMPILER=${gcc}/bin/gcc" # Added these explicitly
"-DCMAKE_CXX_COMPILER=${gcc}/bin/g++"
"-DC_INCLUDE_DIRS=${stdenv.cc.libc.dev}/include"
"-GNinja"
"-DCMAKE_BUILD_TYPE=Release"
"-DCMAKE_INSTALL_PREFIX=../inst"
"-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON"
"-DLLVM_ENABLE_PROJECTS=clang"
"-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi"
"-DLIBCXXABI_USE_LLVM_UNWINDER=OFF" # Disabled this feature
"-DLLVM_TARGETS_TO_BUILD=host"
];
}
Also, After this issue is fixed, I’ll be updating the wiki for the current working way to do this, so help is VERY MUCH appreciated!