Cross-compiling with clang for RISC-V

I’m trying to create a cross-compilation environment using clang for RISC-V. After reading https://github.com/NixOS/nixpkgs/issues/40934, I managed to get this to evaluate:

{ nixpkgs ? <nixpkgs>
, pkgs ? import nixpkgs { }}:

let
  lib = pkgs.lib;
in rec {

  # gccRiscvPkgs = pkgs.pkgsCross.riscv64;
  clangRiscvPkgs = import nixpkgs { crossSystem = lib.systems.examples.riscv64 // { useLLVM = true; }; };

  hello = clangRiscvPkgs.hello;
}

The problem is that this tries to build a toolchain with LLVM 8, which doesn’t yet have full RISC-V support. This was introduced in LLVM 9. I can see that LLVM 8 is hardcoded in https://github.com/NixOS/nixpkgs/blob/a5661135f29f4b2603c41690a01c3168bc8760ae/pkgs/stdenv/cross/default.nix#L67 for this usecase.

Is there a way to override the version of LLVM without patching nixpkgs?

1 Like

There isn’t a way to do this currently. I think adding an option like llvmVersion or something to crossSystem would make sense though. We might also be able to just use LLVM 9 or 10 always for cross-compilation.

Updating to LLVM 9 seems like a sensible idea, but I’m not sure what the implications are for other architectures.

https://github.com/NixOS/nixpkgs/issues/40934 i just now fixed.

I would like the pkgs/build-support/alternatives from https://github.com/NixOS/nixpkgs/pull/83888/files to handle the LLVM version.

1 Like

The problem with using something like alternatives here is that you can’t just override the “clang” due to bootstrapping dependencies. You need it to be some kind of configuration option.

I was thinking more choosing stdenv.cc. Also I’d love all the choices outlined in https://clang.llvm.org/docs/Toolchain.html to be made independently all sorts of LLVM vs GNU mashups. Once GCC is broken up both should be bootstrapped the same way.

1 Like

Related: https://github.com/NixOS/nixpkgs/pull/107594

1 Like