From c3a935c5ffe1dc8ff203d99a6b790558cfb028db Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Thu, 21 Dec 2023 22:13:26 +0000 Subject: [PATCH] flake.nix cleanup * Removed verilator overlay. It's sufficient to add the dependencies to buildInputs * Remove unneeded comment and code * Use toolchain from lowrisc-nix --- dependencies/overlay.nix | 1 - dependencies/riscv-gcc-toolchain-lowrisc.nix | 35 --- flake.lock | 2 +- flake.nix | 270 +++++++++---------- 4 files changed, 122 insertions(+), 186 deletions(-) delete mode 100644 dependencies/riscv-gcc-toolchain-lowrisc.nix diff --git a/dependencies/overlay.nix b/dependencies/overlay.nix index b0cc1ddf..aedd9b13 100644 --- a/dependencies/overlay.nix +++ b/dependencies/overlay.nix @@ -1,4 +1,3 @@ final: prev: { - riscv-gcc-toolchain-lowrisc = prev.callPackage ./riscv-gcc-toolchain-lowrisc.nix {}; riscv-isa-sim = prev.callPackage ./riscv-isa-sim.nix {}; } diff --git a/dependencies/riscv-gcc-toolchain-lowrisc.nix b/dependencies/riscv-gcc-toolchain-lowrisc.nix deleted file mode 100644 index 29907a84..00000000 --- a/dependencies/riscv-gcc-toolchain-lowrisc.nix +++ /dev/null @@ -1,35 +0,0 @@ -{ - pkgs, - lib, - stdenv, - fetchzip, - zlib, - ncurses5, -}: -# Used for reference... -# https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/compilers/gcc-arm-embedded/10/default.nix -stdenv.mkDerivation rec { - name = "riscv-gcc-toolchain"; - version = "20220210-1"; - src = fetchzip { - url = "https://github.com/lowRISC/lowrisc-toolchains/releases/download/${version}/lowrisc-toolchain-gcc-rv32imc-${version}.tar.xz"; - sha256 = "1m708xfdzf3jzclm2zw51my3nryvlsfwqkgps3xxa0xnhq4ly1bl"; - }; - - dontConfigure = true; - dontBuild = true; - dontPatchELF = true; # We will do this manually in preFixup - dontStrip = true; - - installPhase = '' - mkdir -p $out - cp -R * $out - ''; - preFixup = '' - find $out -type f ! -name ".o" | while read f; do - patchelf "$f" > /dev/null 2>&1 || continue - patchelf --set-interpreter $(cat ${stdenv.cc}/nix-support/dynamic-linker) "$f" || true - patchelf --set-rpath ${lib.makeLibraryPath ["$out" stdenv.cc.cc ncurses5]} "$f" || true - done - ''; -} diff --git a/flake.lock b/flake.lock index 2c0cdd11..14fb7aa6 100644 --- a/flake.lock +++ b/flake.lock @@ -10,7 +10,7 @@ }, "locked": { "lastModified": 1, - "narHash": "sha256-pI/nwrf15voS37o9QKpbdqHSR8BSmiJULaUcZ7rKL0M=", + "narHash": "sha256-voOrJljp03zLxjNX/v+hRNsCqLGz7u5mNgbUOc4IlhQ=", "path": "./dependencies", "type": "path" }, diff --git a/flake.nix b/flake.nix index 3f813f4d..c358c0fa 100644 --- a/flake.nix +++ b/flake.nix @@ -13,158 +13,130 @@ }; }; - outputs = all @ { + outputs = inputs @ { self, + lowrisc-nix, nixpkgs, flake-utils, deps, ... - }: - ( - flake-utils.lib.eachDefaultSystem (system: let - pkgs = import nixpkgs { - inherit system; - config = {allowUnfree = true;}; - overlays = [ - # Add extra packages we might need - # Currently this contains the lowrisc riscv-toolchain, and spike - deps.overlay_pkgs - # Add all the python packages we need that aren't in nixpkgs - # (See the ./dependencies folder for more info) - (final: prev: { - python3 = prev.python3.override { - packageOverrides = deps.overlay_python; - }; - }) - # Add some missing dependencies to nixpkgs#verilator - (final: prev: { - verilator = prev.verilator.overrideAttrs (oldAttrs: { - propagatedBuildInputs = [final.zlib final.libelf]; - }); - }) - ]; - }; - - # Currently we don't build the riscv-toolchain from src, we use a github release - # (See ./dependencies/riscv-gcc-toolchain-lowrisc.nix) - - # riscv-toolchain (built from src) # BROKEN - # riscv-gcc-toolchain-lowrisc-src = pkgs.callPackage \ - # ./dependencies/riscv_gcc.nix { - # riscv-arch = "rv32imc"; - # }; - - pythonEnv = pkgs.python3.withPackages ( - ps: - with ps; [pip fusesoc edalize pyyaml Mako] - ); - - # This is the final list of dependencies we need to build the project. - project_deps = - [ - pythonEnv - ] - ++ (with pkgs; [ - cmake - openocd - screen - verilator - riscv-gcc-toolchain-lowrisc - gtkwave - srecord - openfpgaloader - # vivado - ]); - in { - packages.dockertest = pkgs.dockerTools.buildImage { - name = "hello-docker"; - copyToRoot = pkgs.buildEnv { - name = "image-root"; - paths = [ - pkgs.coreutils - pkgs.sl - ]; - }; - config = { - Cmd = ["${pkgs.sl}/bin/sl"]; - }; - }; - devShells.default = pkgs.mkShell { - name = "labenv"; - buildInputs = project_deps; - shellHook = '' - # FIXME This works on Ubuntu, may not on other distros. FIXME - export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive - - # HACK fixup some paths to use our sandboxed python environment - # Currently, fusesoc tries to invoke the program 'python3' from the - # PATH, which when running under a nix python environment, resolves - # to the raw python binary, not wrapped and not including the - # environment's packages. Hence, the first time an import is evaluated - # we will error out. - sed -i -- \ - 's|interpreter:.*|interpreter: ${pythonEnv}/bin/python3|g' \ - vendor/lowrisc_ibex/vendor/lowrisc_ip/dv/tools/ralgen/ralgen.core - sed -i -- \ - 's|interpreter:.*|interpreter: ${pythonEnv}/bin/python3|g' \ - vendor/lowrisc_ibex/vendor/lowrisc_ip/ip/prim/primgen.core - - export PS1='labenv(HiPEAC) (ibex-demo-system) \$ ' - - echo - echo - cat ./data/lowrisc.art - echo "---------------------------------------------------" - echo "Welcome to the 'ibex-demo-system' nix environment!" - echo "---------------------------------------------------" - - helpstr=$(cat <<'EOF' - - Build ibex software : - mkdir sw/c/build && pushd sw/c/build && cmake ../ && make && popd - Build ibex simulation verilator model : - fusesoc --cores-root=. run --target=sim --tool=verilator --setup --build lowrisc:ibex:demo_system - Run ibex simulator verilator model : - ./build/lowrisc_ibex_demo_system_0/sim-verilator/Vibex_demo_system -t \ - --meminit=ram,sw/c/build/demo/hello_world/demo - Build ibex-demo-system FPGA bitstream for Arty-A7 : - fusesoc --cores-root=. run --target=synth --setup --build lowrisc:ibex:demo_system - Program Arty-A7 FPGA with bitstream : - openFPGALoader -b arty_a7_35t build/lowrisc_ibex_demo_system_0/synth-vivado/lowrisc_ibex_demo_system_0.bit - Load ibex software to the programmed FPGA : - ./util/load_demo_system.sh run ./sw/c/build/demo/lcd_st7735/lcd_st7735 - Start an OpenOCD instance, connected to the Arty-A7 ibex - openocd -f util/arty-a7-openocd-cfg.tcl - Connect gdb to a running program on the FPGA (In a different terminal to the OpenOCD instance): - riscv32-unknown-elf-gdb -ex "target extended-remote localhost:3333" ./sw/c/build/demo/hello_world/demo - - To leave the environment: - exit - - EOF - ) - helpme(){ echo "$helpstr"; } - helpme - - echo - echo "Run 'helpme' in your shell to see this message again." - echo - ''; - }; - formatter = pkgs.alejandra; - }) - ) - // { - overlay = final: prev: {}; - overlays = {exampleOverlay = self.overlay;}; - - # Utilized by `nix run .#` - # apps.x86_64-linux.hello = { - # type = "app"; - # program = c-hello.packages.x86_64-linux.hello; - # }; - - # Utilized by `nix run . -- ` - # defaultApp.x86_64-linux = self.apps.x86_64-linux.hello; - }; + }: let + all_system_outputs = flake-utils.lib.eachDefaultSystem (system: let + pkgs = import nixpkgs { + inherit system; + config = {allowUnfree = true;}; + overlays = [ + # Add extra packages we might need + # Currently this contains spike + deps.overlay_pkgs + # Add all the python packages we need that aren't in nixpkgs + # (See the ./dependencies folder for more info) + (final: prev: { + python3 = prev.python3.override { + packageOverrides = deps.overlay_python; + }; + }) + ]; + }; + + pythonEnv = pkgs.python3.withPackages ( + ps: + with ps; [pip fusesoc edalize pyyaml Mako] + ); + + # This is the final list of dependencies we need to build the project. + project_deps = + [ + pythonEnv + ] + ++ (with pkgs; [ + cmake + openocd + screen + verilator + + # Currently we don't build the riscv-toolchain from src, we use a github release + # See https://github.com/lowRISC/lowrisc-nix/blob/main/pkgs/lowrisc-toolchain-gcc-rv32imcb.nix + + # riscv-toolchain (built from src) # BROKEN + # riscv-gcc-toolchain-lowrisc-src = pkgs.callPackage \ + # ./dependencies/riscv_gcc.nix { + # riscv-arch = "rv32imc"; + # }; + lowrisc-nix.packages.${system}.lowrisc-toolchain-gcc-rv32imcb + + gtkwave + srecord + openfpgaloader + libelf + zlib + # vivado + ]); + in { + devShells.default = pkgs.mkShell { + name = "labenv"; + buildInputs = project_deps; + shellHook = '' + # FIXME This works on Ubuntu, may not on other distros. FIXME + export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive + + # HACK fixup some paths to use our sandboxed python environment + # Currently, fusesoc tries to invoke the program 'python3' from the + # PATH, which when running under a nix python environment, resolves + # to the raw python binary, not wrapped and not including the + # environment's packages. Hence, the first time an import is evaluated + # we will error out. + sed -i -- \ + 's|interpreter:.*|interpreter: ${pythonEnv}/bin/python3|g' \ + vendor/lowrisc_ibex/vendor/lowrisc_ip/dv/tools/ralgen/ralgen.core + sed -i -- \ + 's|interpreter:.*|interpreter: ${pythonEnv}/bin/python3|g' \ + vendor/lowrisc_ibex/vendor/lowrisc_ip/ip/prim/primgen.core + + export PS1='labenv(HiPEAC) (ibex-demo-system) \$ ' + + echo + echo + cat ./data/lowrisc.art + echo "---------------------------------------------------" + echo "Welcome to the 'ibex-demo-system' nix environment!" + echo "---------------------------------------------------" + + helpstr=$(cat <<'EOF' + + Build ibex software : + mkdir sw/c/build && pushd sw/c/build && cmake ../ && make && popd + Build ibex simulation verilator model : + fusesoc --cores-root=. run --target=sim --tool=verilator --setup --build lowrisc:ibex:demo_system + Run ibex simulator verilator model : + ./build/lowrisc_ibex_demo_system_0/sim-verilator/Vibex_demo_system -t \ + --meminit=ram,sw/c/build/demo/hello_world/demo + Build ibex-demo-system FPGA bitstream for Arty-A7 : + fusesoc --cores-root=. run --target=synth --setup --build lowrisc:ibex:demo_system + Program Arty-A7 FPGA with bitstream : + openFPGALoader -b arty_a7_35t build/lowrisc_ibex_demo_system_0/synth-vivado/lowrisc_ibex_demo_system_0.bit + Load ibex software to the programmed FPGA : + ./util/load_demo_system.sh run ./sw/c/build/demo/lcd_st7735/lcd_st7735 + Start an OpenOCD instance, connected to the Arty-A7 ibex + openocd -f util/arty-a7-openocd-cfg.tcl + Connect gdb to a running program on the FPGA (In a different terminal to the OpenOCD instance): + riscv32-unknown-elf-gdb -ex "target extended-remote localhost:3333" ./sw/c/build/demo/hello_world/demo + + To leave the environment: + exit + + EOF + ) + helpme(){ echo "$helpstr"; } + helpme + + echo + echo "Run 'helpme' in your shell to see this message again." + echo + ''; + }; + formatter = pkgs.alejandra; + }); + in + all_system_outputs; }