From NixOS Wiki
Jump to: navigation, search

From Wikipedia:

The LLVM compiler infrastructure project is a "collection of modular and reusable compiler and toolchain technologies" used to develop compiler front ends and back ends.

Build LLVM/clang from source

LLVM/clang when compiled from source won't find headers/libraries/startup files without compiler wrapper. However it is possible to use the following shell.nix to get started and provide the missing flags to the compiler manually

with import <nixpkgs> {};
  gccForLibs = stdenv.cc.cc;
in stdenv.mkDerivation {
  name = "llvm-env";
  buildInputs = [

  # where to find libgcc
  # teach clang about C startup file locations
  CFLAGS="-B${gccForLibs}/lib/gcc/${targetPlatform.config}/${gccForLibs.version} -B ${stdenv.cc.libc}/lib";

  cmakeFlags = [
    # Debug for debug builds
    # inst will be our installation prefix
    # change this to enable the projects you need
    # this makes llvm only to produce code for the current platform, this saves CPU time, change it to what you need

After that you can execute the following commands to get a working clang:

 $ git clone https://github.com/llvm/llvm-project/
 $ mkdir build && cd build
 $ cmake $cmakeFlags ../llvm-project/llvm
 $ ninja
 # installs everything to ../inst
 $ ninja install
 $ cd ..

Then assuming you have a test program called main.c:

 $ ./inst/bin/clang $CFLAGS -o main main.c