Skip to content

Commit fe8e5f8

Browse files
authored
Flake update: build sim & toolchain (#16)
1 parent 699a5e4 commit fe8e5f8

File tree

3 files changed

+106
-35
lines changed

3 files changed

+106
-35
lines changed

cpu-rtl

Submodule cpu-rtl deleted from 1f3fb10

flake.lock

Lines changed: 24 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 82 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,52 +3,107 @@
33

44
inputs = {
55
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
6+
nixpkgs-circt.url = "github:NixOS/nixpkgs/nixos-25.05";
67
flake-utils.url = "github:numtide/flake-utils";
78
};
89

9-
outputs = { self, nixpkgs, flake-utils }:
10+
outputs = { self, nixpkgs, nixpkgs-circt, flake-utils }:
1011
flake-utils.lib.eachDefaultSystem ( system:
1112
let
12-
pkgs = import nixpkgs { inherit system; };
13-
gccForLibs = pkgs.stdenv.cc.cc;
13+
pkgs = import nixpkgs { inherit system; config.allowUnfree = true; };
14+
circt = (import nixpkgs-circt { inherit system; }).circt;
15+
pkgsRV = pkgs.pkgsCross.riscv64;
16+
targetLlvmLibraries = pkgsRV.llvmPackages_21;
1417
in rec {
1518

16-
defaultPackage = pkgs.stdenv.mkDerivation {
17-
name = "llvm";
19+
defaultPackage = packages.toolchain;
1820

19-
dontUnpack = true;
21+
packages.toolchain = with pkgsRV; (wrapCCWith rec {
22+
cc = (targetLlvmLibraries.clang-unwrapped.override {
23+
src = ./.;
24+
libllvm = (targetLlvmLibraries.libllvm.override { src = ./.; });
25+
});
26+
# libstdcxx is taken from gcc in an ad-hoc way in cc-wrapper.
27+
libcxx = null;
28+
extraPackages = [ targetLlvmLibraries.compiler-rt ];
29+
extraBuildCommands = ''
30+
rsrc="$out/resource-root"
31+
mkdir "$rsrc"
32+
ln -s "${lib.getLib cc}/lib/clang/*/include" "$rsrc"
33+
echo "-resource-dir=$rsrc" >> $out/nix-support/cc-cflags
34+
ln -s "${targetLlvmLibraries.compiler-rt.out}/lib" "$rsrc/lib"
35+
ln -s "${targetLlvmLibraries.compiler-rt.out}/share" "$rsrc/share"
36+
'';
37+
});
2038

21-
buildInputs = with pkgs; [
39+
packages.sim = pkgs.stdenv.mkDerivation {
40+
name = "fck-china-sim";
41+
# Floating derivation!!!
42+
__impure = true;
43+
44+
src = pkgs.fetchgit {
45+
url = "https://github.com/OpenXiangShan/XiangShan.git";
46+
rev = "0fb84f8ddbfc9480d870f72cc903ac6453c888c9";
47+
fetchSubmodules = true;
48+
leaveDotGit = true;
49+
sha256 = "sha256-C+y//RJxI8FwYWCs8dmYLh8ZGVNCTAnRoiOVuY913Jg=";
50+
deepClone = false;
51+
};
52+
53+
nativeBuildInputs = with pkgs; [
54+
mill
55+
time
56+
git
57+
espresso
58+
verilator
2259
python3
23-
ninja
24-
cmake
2560
];
2661

27-
cmakeFlags = [
28-
"-DUSE_DEPRECATED_GCC_INSTALL_PREFIX=1"
29-
"-DGCC_INSTALL_PREFIX=${pkgs.gcc}"
30-
"-DC_INCLUDE_DIRS=${pkgs.stdenv.cc.libc.dev}/include"
31-
"-GNinja"
32-
# Debug for debug builds
33-
"-DCMAKE_BUILD_TYPE=Release"
34-
# inst will be our installation prefix
35-
"-DCMAKE_INSTALL_PREFIX=../inst"
36-
"-DLLVM_INSTALL_TOOLCHAIN_ONLY=ON"
37-
# change this to enable the projects you need
38-
"-DLLVM_ENABLE_PROJECTS=clang"
39-
# enable libcxx* to come into play at runtimes
40-
"-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi"
41-
# this makes llvm only to produce code for the current platform, this saves CPU time, change it to what you need
42-
"-DLLVM_TARGETS_TO_BUILD=host"
43-
"-S ${self}/llvm"
62+
buildInputs = with pkgs; [
63+
sqlite.dev
64+
zlib.dev
65+
zstd.dev
4466
];
67+
68+
buildPhase = ''
69+
runHook preBuild
70+
71+
# Copy sources
72+
export NOOP_HOME=$out/src
73+
echo src = $src
74+
echo NOOP_HOME = $NOOP_HOME
75+
mkdir -p $NOOP_HOME
76+
cp -r $src/* $src/.* $NOOP_HOME
77+
78+
# Patch shebangs
79+
chmod u+wx -R $NOOP_HOME
80+
patchShebangs --build $NOOP_HOME/scripts/
81+
82+
# Build
83+
export _JAVA_OPTIONS="-XX:+UseZGC -XX:+ZUncommit -XX:ZUncommitDelay=30"
84+
FIRTOOL=${circt}/bin/firtool JVM_XMX=20G make -j8 -C $NOOP_HOME emu
85+
86+
runHook postBuild
87+
'';
88+
89+
installPhase = ''
90+
runHook preInstall
91+
92+
mkdir -p $out/bin
93+
chmod u+x -R $out/src/build/
94+
cp $out/src/build/verilator-compile/emu $out/bin
95+
rm -rf $out/src
96+
97+
runHook postInstall
98+
'';
4599
};
46100

47101
devShell = (defaultPackage.overrideAttrs (oldAttrs: {
48102
name = "llvm-env";
49-
buildInputs = oldAttrs.buildInputs ++ (with pkgs; [ verilator ]);
103+
buildInputs = oldAttrs.buildInputs;
50104
}));
51105

106+
52107
});
53108
}
54109

0 commit comments

Comments
 (0)