From c9f7b916ff1883881fe8e165fcf591e1e64370d5 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Tue, 6 Aug 2024 11:53:41 +0800 Subject: [PATCH] [nix] refactor the rocketv subattr Signed-off-by: Avimitin --- nix/t1/default.nix | 4 - rocketemu/default.nix | 116 ++++++++++-------- rocketemu/dpi/default.nix | 8 +- .../nix/mlirbc.nix | 3 +- .../rocketv-rtl.nix => rocketemu/nix/rtl.nix | 4 +- .../nix/verilated-csrc.nix | 4 +- .../configs/{RocketTile.json => default.json} | 0 7 files changed, 77 insertions(+), 62 deletions(-) rename nix/t1/rocketv-mlirbc.nix => rocketemu/nix/mlirbc.nix (91%) rename nix/t1/rocketv-rtl.nix => rocketemu/nix/rtl.nix (84%) rename nix/t1/rocketv-verilated-csrc.nix => rocketemu/nix/verilated-csrc.nix (97%) rename rocketv/configs/{RocketTile.json => default.json} (100%) diff --git a/nix/t1/default.nix b/nix/t1/default.nix index 12c6e68db7..2d5829d422 100644 --- a/nix/t1/default.nix +++ b/nix/t1/default.nix @@ -35,10 +35,6 @@ lib.makeScope newScope configgen = _millOutput.configgen // { meta.mainProgram = "configgen"; }; t1package = _millOutput.t1package; - # FIXME: move all the rocketv file to an individual directory and put all attribute into one scope. - rocketv-mlirbc = self.callPackage ./rocketv-mlirbc.nix { }; - rocketv-rtl = self.callPackage ./rocketv-rtl.nix { }; - rocketv-verilated-csrc = self.callPackage ./rocketv-verilated-csrc.nix { }; rocketv = self.callPackage ../../rocketemu { }; omreader-unwrapped = self.callPackage ./omreader.nix { }; diff --git a/rocketemu/default.nix b/rocketemu/default.nix index cb5c34a1e9..99d9e3a589 100644 --- a/rocketemu/default.nix +++ b/rocketemu/default.nix @@ -3,56 +3,74 @@ , rustPlatform , libspike , zlib -, rocketv-verilated-csrc }: -lib.makeScope newScope (scope: rec { - c-dpi-lib = scope.callPackage ./dpi { }; - - # FIXME: merge with difftest and put it under the nix/pkgs - spike_interfaces = scope.callPackage ../difftest/spike_interfaces { }; - - emu = rustPlatform.buildRustPackage { - name = "rocketemu"; - - src = with lib.fileset; toSource { - root = ./.; - fileset = unions [ - ./driver - ./offline - ./spike_rs - ./test_common - ./Cargo.lock - ./Cargo.toml - ]; - }; - - buildInputs = [ - zlib - spike_interfaces - ]; - - # FIXME: can we hack this into derivations, so that we don't need to specify library dir explicitly? - env = - let - toLib = drv: "${drv}/lib"; - in - { - ROCKET_DPI_DIR = toLib c-dpi-lib; - TESTBENCH_LIB_DIR = toLib rocketv-verilated-csrc; - SPIKE_LIB_DIR = toLib libspike; - SPIKE_INTERFACES_LIB_DIR = toLib spike_interfaces; - }; +let + configsDirectory = ../rocketv/configs; + # allConfigs is a (configName -> configJsonPath) map + allConfigs = lib.mapAttrs' + (fileName: fileType: + assert fileType == "regular" && lib.hasSuffix ".json" fileName; + lib.nameValuePair + (lib.removeSuffix ".json" fileName) + (lib.path.append configsDirectory fileName)) + (builtins.readDir configsDirectory); +in +lib.mapAttrs + (configName: configPath: ( + lib.makeScope newScope (scope: rec { + rocket-config = configPath; + mlirbc = scope.callPackage ./nix/mlirbc.nix { }; + rtl = scope.callPackage ./nix/rtl.nix { }; + verilated-csrc = scope.callPackage ./nix/verilated-csrc.nix { }; + + c-dpi-lib = scope.callPackage ./dpi { }; + + # FIXME: merge with difftest and put it under the nix/pkgs + spike_interfaces = scope.callPackage ../difftest/spike_interfaces { }; + + emu = rustPlatform.buildRustPackage { + name = "rocketemu"; - cargoLock = { - lockFile = ./Cargo.lock; - }; + src = with lib.fileset; toSource { + root = ./.; + fileset = unions [ + ./driver + ./offline + ./spike_rs + ./test_common + ./Cargo.lock + ./Cargo.toml + ]; + }; - outputs = [ "out" "driver" "offline" ]; + buildInputs = [ + zlib + spike_interfaces + ]; - postInstall = '' - mkdir -p $driver/bin $offline/bin - ln -s $out/bin/driver $driver/bin/driver - ln -s $out/bin/offline $driver/bin/offline - ''; - }; -}) + env = + let + toLib = drv: "${drv}/lib"; + in + { + ROCKET_DPI_DIR = toLib c-dpi-lib; + TESTBENCH_LIB_DIR = toLib verilated-csrc; + SPIKE_LIB_DIR = toLib libspike; + SPIKE_INTERFACES_LIB_DIR = toLib spike_interfaces; + }; + + cargoLock = { + lockFile = ./Cargo.lock; + }; + + outputs = [ "out" "driver" "offline" ]; + + postInstall = '' + mkdir -p $driver/bin $offline/bin + ln -s $out/bin/driver $driver/bin/driver + ln -s $out/bin/offline $driver/bin/offline + ''; + }; + }) + )) # end of mapAttr + allConfigs diff --git a/rocketemu/dpi/default.nix b/rocketemu/dpi/default.nix index f8714b4b66..6c4e1faff7 100644 --- a/rocketemu/dpi/default.nix +++ b/rocketemu/dpi/default.nix @@ -3,7 +3,7 @@ , stdenv , cmake , ninja -, rocketv-verilated-csrc +, verilated-csrc }: stdenv.mkDerivation { name = "rocketv-emulator"; @@ -16,12 +16,12 @@ stdenv.mkDerivation { verilator ]; - cmakeFlags = lib.optionals rocketv-verilated-csrc.enable-trace [ + cmakeFlags = lib.optionals verilated-csrc.enable-trace [ "-DVM_TRACE=ON" ]; env = { - VERILATED_INC_DIR = "${rocketv-verilated-csrc}/include"; - VERILATED_LIB_DIR = "${rocketv-verilated-csrc}/lib"; + VERILATED_INC_DIR = "${verilated-csrc}/include"; + VERILATED_LIB_DIR = "${verilated-csrc}/lib"; }; } diff --git a/nix/t1/rocketv-mlirbc.nix b/rocketemu/nix/mlirbc.nix similarity index 91% rename from nix/t1/rocketv-mlirbc.nix rename to rocketemu/nix/mlirbc.nix index c01bd503ac..65a25e5036 100644 --- a/nix/t1/rocketv-mlirbc.nix +++ b/rocketemu/nix/mlirbc.nix @@ -4,6 +4,7 @@ , circt , elaborator +, rocket-config }: stdenvNoCC.mkDerivation { name = "t1-rocketv-elaborated.mlirbc"; @@ -12,7 +13,7 @@ stdenvNoCC.mkDerivation { buildCommand = '' mkdir elaborate - elaborator rocketemu --target-dir elaborate --rocket-config ${../../rocketv/configs/RocketTile.json} + elaborator rocketemu --target-dir elaborate --rocket-config ${rocket-config} firtool elaborate/*.fir \ --annotation-file elaborate/*.anno.json \ --emit-bytecode \ diff --git a/nix/t1/rocketv-rtl.nix b/rocketemu/nix/rtl.nix similarity index 84% rename from nix/t1/rocketv-rtl.nix rename to rocketemu/nix/rtl.nix index f67521627f..053078f821 100644 --- a/nix/t1/rocketv-rtl.nix +++ b/rocketemu/nix/rtl.nix @@ -2,7 +2,7 @@ , lib , circt -, rocketv-mlirbc +, mlirbc }: let @@ -21,6 +21,6 @@ stdenvNoCC.mkDerivation { buildCommand = '' mkdir -p $out - firtool ${rocketv-mlirbc} ${mfcArgs} -o $out + firtool ${mlirbc} ${mfcArgs} -o $out ''; } diff --git a/nix/t1/rocketv-verilated-csrc.nix b/rocketemu/nix/verilated-csrc.nix similarity index 97% rename from nix/t1/rocketv-verilated-csrc.nix rename to rocketemu/nix/verilated-csrc.nix index eb5b4127f6..f32ade7af6 100644 --- a/nix/t1/rocketv-verilated-csrc.nix +++ b/rocketemu/nix/verilated-csrc.nix @@ -1,7 +1,7 @@ { lib , fetchgit , stdenv -, rocketv-rtl +, rtl , verilator , enable-trace ? true , zlib @@ -21,7 +21,7 @@ in stdenv.mkDerivation { name = "t1-rocketv-verilated"; - src = rocketv-rtl; + src = rtl; nativeBuildInputs = [ verilator ]; diff --git a/rocketv/configs/RocketTile.json b/rocketv/configs/default.json similarity index 100% rename from rocketv/configs/RocketTile.json rename to rocketv/configs/default.json