From 1270e9589fd194ffeb5f08d2e6231e43981b112a Mon Sep 17 00:00:00 2001 From: SharzyL Date: Sun, 16 Oct 2022 00:22:08 +0800 Subject: [PATCH] fix nix dirty hack by hacking cc-wrapper --- build.sc | 2 +- shell.nix | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/build.sc b/build.sc index 9a7953e323..06cf357986 100644 --- a/build.sc +++ b/build.sc @@ -298,7 +298,7 @@ object cases extends Module { PathRef(T.ctx.dest / "linker.ld") } def compile: T[PathRef] = T { - os.proc(Seq("/nix/store/jfliw7lqqfchrmnb21xi5d5pmxk1lvyg-clang-14.0.6/bin/clang", "-o", name() + ".elf" ,"--target=riscv32", "-march=rv32gcv", s"-L${musl.compile().path}/lib", s"-L${compilerrt.compile().path}/lib/riscv32", "-mno-relax", s"-T${linkScript().path}") ++ allSourceFiles().map(_.path.toString)).call(T.ctx.dest) + os.proc(Seq("clang", "-o", name() + ".elf" ,"--target=riscv32", "-march=rv32gcv", s"-L${musl.compile().path}/lib", s"-L${compilerrt.compile().path}/lib/riscv32", "-mno-relax", s"-T${linkScript().path}") ++ allSourceFiles().map(_.path.toString)).call(T.ctx.dest) os.proc(Seq("llvm-objcopy", "-O", "binary", "--only-section=.text", name() + ".elf", name())).call(T.ctx.dest) PathRef(T.ctx.dest / name()) } diff --git a/shell.nix b/shell.nix index 585ac14713..182d908c82 100644 --- a/shell.nix +++ b/shell.nix @@ -44,12 +44,24 @@ let ''; }; + # nix cc-wrapper will add --gcc-toolchain to clang flags. However, when we want to use + # our custom libc and compilerrt, clang will only search these libs in --gcc-toolchain + # folder. To avoid this wierd behavior of clang, we need to remove --gcc-toolchain options + # from cc-wrapper + my-cc-wrapper = let cc = myLLVM.clang; in pkgs.runCommand "cc" {} '' + mkdir -p "$out" + cp -rT "${cc}" "$out" + chmod -R +w "$out" + sed -i 's/--gcc-toolchain=[^[:space:]]*//' "$out/nix-support/cc-cflags" + sed -i 's|${cc}|${placeholder "out"}|g' "$out"/bin/* "$out"/nix-support/* + ''; + in pkgs.mkShellNoCC { name = "vector"; buildInputs = with pkgs; [ - myLLVM.clang myLLVM.llvm myLLVM.bintools + my-cc-wrapper jdk mill python3 parallel protobuf ninja verilator antlr4 numactl dtc glibc_multi cmake @@ -60,6 +72,7 @@ in pkgs.mkShellNoCC { fmt glog ]; shellHook = '' - export NIX_CC=" " - ''; # due to some cmake bug + # because we removed --gcc-toolchain from cc-wrapper, we need to add gcc lib path back + export NIX_LDFLAGS_FOR_TARGET="$NIX_LDFLAGS_FOR_TARGET -L${pkgs.gccForLibs.lib}/lib" + ''; }