I am trying to cross-compile a embedded micro controller code using clang using the following flake:
{
inputs = {
nixpkgs = {
url = "github:NixOS/nixpkgs/nixos-unstable";
};
};
outputs = { self, nixpkgs, flake-utils, ...}@inputs:
flake-utils.lib.eachDefaultSystem (system:
let
local_overlay = final: prev: {
};
pkgs = import nixpkgs {
overlays = [ local_overlay ];
localSystem = system;
crossSystem = system;
};
crossPkgs = import nixpkgs {
overlays = [ local_overlay ];
localSystem = system;
crossSystem = {
config = "arm-none-eabi";
};
};
mve_code = pkgs.writeText "main.c" ''
int main()
{
return 0;
}
'';
in {
packages = rec {
default = test;
test = crossPkgs.stdenv.mkDerivation {
pname = "test";
version = "0.0.0";
src = mve_code;
nativeBuildInputs = [
crossPkgs.clang
];
unpackPhase = ''
cp $src ./main.c
'';
buildPhase = ''
$CC -c main.c -o out
'';
installPhase = ''
mkdir -p $out
cp out $out/out
'';
};
};
});
}
This works if I switch crossPkgs.clang for crossPkgs.gcc, in which case it correctly uses arm-none-eabi-gcc as compiler. With clang it fails with the following error message:
error: builder for ‘/nix/store/vgmi9qlq5c4lwwvh4jwcplg9xh7w1hdh-compiler-rt-libc-arm-none-eabi-21.1.2.drv’ failed with exit code 1;
last 25 log lines:
– Looking for unwind.h - found
– Looking for rpc/xdr.h
– Looking for rpc/xdr.h - not found
– Performing Test COMPILER_RT_HAS_NO_DEFAULT_CONFIG_FLAG
– Performing Test COMPILER_RT_HAS_NO_DEFAULT_CONFIG_FLAG - Failed
CMake Warning at cmake/Modules/CompilerRTUtils.cmake:307 (message):
LLVM source tree not found at “/build/compiler-rt-src-21.1.2/llvm”. You
are not using the monorepo layout. This configuration is DEPRECATED.
Call Stack (most recent call first):
CMakeLists.txt:110 (load_llvm_config)– Could NOT find FFI (missing: FFI_LIBRARIES HAVE_FFI_CALL)
– Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
– Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
CMake Error at /nix/store/slaal54bs9c74mf0h3fr45hyvbcfs9mb-llvm-21.1.2-dev/lib/cmake/llvm/LLVMExports.cmake:1455 (add_library):
ADD_LIBRARY called with SHARED option but the target platform does not
support dynamic linking.
Call Stack (most recent call first):
/nix/store/slaal54bs9c74mf0h3fr45hyvbcfs9mb-llvm-21.1.2-dev/lib/cmake/llvm/LLVMConfig.cmake:357 (include)
cmake/Modules/CompilerRTUtils.cmake:312 (find_package)
CMakeLists.txt:110 (load_llvm_config)– Configuring incomplete, errors occurred!
For full logs, run:
nix log /nix/store/vgmi9qlq5c4lwwvh4jwcplg9xh7w1hdh-compiler-rt-libc-arm-none-eabi-21.1.2.drv
error: 1 dependencies of derivation ‘/nix/store/6bhrsv9drglf3f157w95ihxkbmwwkjgj-arm-none-eabi-clang-wrapper-21.1.2.drv’ failed to build
error: 1 dependencies of derivation ‘/nix/store/nb5p70f6c6kyy81xz3k6l31knzys27rk-test-arm-none-eabi-0.0.0-env.drv’ failed to build
As far as I can see this is a problem with LTO still being build as shared library, as
/nix/store/slaal54bs9c74mf0h3fr45hyvbcfs9mb-llvm-21.1.2-dev/lib/cmake/llvm/LLVMExports.cmake:1455 is
# Create imported target LTO
add_library(LTO SHARED IMPORTED)
while I would expect this to be STATIC.
I get the same error when using x86_64-elf instead of arm-none-eabi, which makes me believe this is not arm specific. It also persists when setting crossSystem explicitly to static using isStatic = true;.
Hopefully someone has any ideas or can point me in the right direction to get this working. It feels like this is just missing a cmake parameter somewhere in LLVM. I didn’t see anything missing when I looked at the LLVM-package, but I can’t claim to fully understand it currently.