From 2cc992a1818ae2c18943f0be51a55b3cfa126dff Mon Sep 17 00:00:00 2001 From: xinyangli Date: Mon, 25 Mar 2024 17:29:46 +0800 Subject: [PATCH] am: build with nix --- .gitignore | 3 +- .gitmodules | 2 +- abstract-machine/default.nix | 26 ++++++++++++++++ flake.nix | 60 +++++++++++++----------------------- 4 files changed, 50 insertions(+), 41 deletions(-) create mode 100644 abstract-machine/default.nix diff --git a/.gitignore b/.gitignore index 4ed3ed8..44a51ce 100644 --- a/.gitignore +++ b/.gitignore @@ -10,4 +10,5 @@ !init.sh /fceux-am /nvboard -/am-kernels +**/.cache +**/result diff --git a/.gitmodules b/.gitmodules index d7bc671..3d834b3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,3 @@ [submodule "am-kernels"] path = am-kernels - url = ./am-kernels/ + url = https://git.xinyang.life/xin/am-kernels.git diff --git a/abstract-machine/default.nix b/abstract-machine/default.nix new file mode 100644 index 0000000..1f1f67d --- /dev/null +++ b/abstract-machine/default.nix @@ -0,0 +1,26 @@ +{ stdenv, + lib, + cmake, + SDL2, + isa ? "native", + platform ? "NEMU" +}: +stdenv.mkDerivation { + pname = "abstract-machine"; + version = "2024.02.18"; + + src = ./.; + + cmakeFlags = [ + (lib.cmakeFeature "ISA" isa) + (lib.cmakeBool "__PLATFORM_${lib.strings.toUpper platform}__" true) + ]; + + nativeBuildInputs = [ + cmake + ]; + + buildInputs = [ + + ] ++ (if platform=="native" then [ SDL2 ] else [ ]); +} diff --git a/flake.nix b/flake.nix index 4492e28..42dccac 100644 --- a/flake.nix +++ b/flake.nix @@ -12,54 +12,37 @@ localSystem = system; crossSystem = { config = "riscv32-none-elf"; - abi = "ilp32"; + gcc = { + abi = "ilp32"; + arch = "rv32if"; + }; }; }; in { - packages.nemu = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels; }; + packages.nemu = pkgs.callPackage ./nemu { am-kernels = self.packages.${system}.am-kernels-cmake; }; + packages.abstract-machine = crossPkgs.callPackage ./abstract-machine { isa = "riscv"; platform = "nemu"; }; - packages.am-kernels = crossPkgs.stdenv.mkDerivation rec { - pname = "am-kernels"; + packages.am-kernels-cmake = crossPkgs.stdenv.mkDerivation rec { + pname = "am-kernels-cmake"; version = "2024.02.18"; - src = pkgs.fetchFromGitHub { - owner = "NJU-ProjectN"; - repo = "am-kernels"; - rev = "bb725d6f8223dd7de831c3b692e8c4531e9d01af"; - hash = "sha256-ZHdrw28TN8cMvhhzM469OV7cp0Yp+8yao855HP4+P4A="; - }; + src = ./am-kernels; - AM_HOME = pkgs.fetchFromGitHub { - owner = "xinyangli"; - repo = "abstract-machine"; - rev = "788595aac61c6b2f3b78ca8aa7d08dc33911bca4"; - hash = "sha256-YvWHIBP9tz3HL2TyibftvvQrpkWUDPnviCF4oyLmdjg="; - }; + nativeBuildInputs = [ + pkgs.cmake + ]; - ARCH = "riscv32-nemu"; + cmakeFlags = [ + (pkgs.lib.cmakeFeature "ISA" "riscv") + (pkgs.lib.cmakeFeature "PLATFORM" "nemu") + (pkgs.lib.cmakeFeature "CMAKE_INSTALL_DATADIR" "share") + ]; - patchPhase = '' - sed -i 's/\/bin\/echo/echo/' tests/cpu-tests/Makefile - ''; - - buildPhase = '' - AS=$CC make -C tests/cpu-tests BUILD_DIR=$(pwd)/build ARCH=$ARCH - ''; - - installPhase = '' - mkdir -p $out/share/images $out/share/dump - cp build/riscv32-nemu/*.bin $out/share/images - cp build/riscv32-nemu/*.txt $out/share/dump - ''; - - dontFixup = true; - }; - - devShells.default = pkgs.mkShell { - packages = with pkgs; [ - gdb - ] ++ builtins.attrValues self.packages.${system}; + buildInputs = [ + # SDL2 + self.packages.${system}.abstract-machine + ]; }; devShells.nemu = pkgs.mkShell { @@ -74,4 +57,3 @@ } ); } -