C: Difference between revisions

imported>Mic92
add clangd, fix link syntax
imported>Mic92
add clang hacking guide
Line 328: Line 328:
   name = "env";
   name = "env";
}
}
</syntaxHighlight>
== Use a clang compiled from source ==
Unwrapped compilers usually do not any libraries/headers in nix. This is an issue if you work on the clang code base.
Assuming you have build llvm/clang like this
<syntaxHighlight lang=console>
$  git clone https://github.com/llvm/llvm-project
$ cd llvm-project
$ nix-shell -p cmake --command "mkdir build &&  cd build && cmake -DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" -DCMAKE_INSTALL_PREFIX=../install -DCMAKE_BUILD_TYPE=Debug ../llvm && make -j$(nproc)"
</syntaxHighlight>
You can create a wrapper around your local build binaries like this:
<syntaxHighlight lang=nix>
# This file assumes that your llvm binaries are stored in ./build/bin
# impure-clang.nix
{ stdenv, wrapCC, runtimeShell }:                                                               
wrapCC (stdenv.mkDerivation {                                                                   
  name = "impure-clang";                                                                         
  dontUnpack = true;                                                                             
  installPhase = ''                                                                             
    mkdir -p $out/bin                                                                           
    for bin in ${toString (builtins.attrNames (builtins.readDir ./build/bin))}; do 
      cat > $out/bin/$bin <<EOF                                                                 
#!${runtimeShell}                                                                               
exec "${toString ./.}/build/bin/$bin" "\$@"                                         
EOF                                                                                             
      chmod +x $out/bin/$bin                                                                     
    done                                                                                         
  '';                                                                                           
  passthru.isClang = true;                                                                       
})                                                                                               
</syntaxHighlight>
Than you can create a <code>shell.nix</code> like this:
<syntaxHighlight lang=nix>
with import <nixpkgs>;
pkgs.mkShell {
  nativeBuildInputs = [
  cmake
  (callPackage ./impure-clang.nix)
  ];
}
</syntaxHighlight>
And use your self-compiled clang in a nix-shell:
<syntaxHighlight lang=console>
$ nix-shell --command 'clang --version'
</syntaxHighlight>
</syntaxHighlight>