From 01960837e01fa861af127e97ba3c006c0a97072c Mon Sep 17 00:00:00 2001
From: tracer-ysyx <tracer@ysyx.org>
Date: Sun, 3 Mar 2024 23:05:05 +0800
Subject: [PATCH] =?UTF-8?q?>=20compile=20NEMU=20ysyx=5F22040000=20?=
 =?UTF-8?q?=E6=9D=8E=E5=BF=83=E6=9D=A8=20Linux=20calcite=206.6.18=20#1-Nix?=
 =?UTF-8?q?OS=20SMP=20PREEMPT=5FDYNAMIC=20Fri=20Feb=2023=2008:25:28=20UTC?=
 =?UTF-8?q?=202024=20x86=5F64=20GNU/Linux=20=2023:05:05=20=20up=20=20=207:?=
 =?UTF-8?q?22,=20=202=20users,=20=20load=20average:=200.43,=200.37,=200.33?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 abstract-machine/Makefile     |  4 +-
 nemu/flake.lock => flake.lock | 35 ++++------------
 flake.nix                     | 39 +++++++++++++++++
 nemu/.config                  | 74 --------------------------------
 nemu/Makefile                 |  2 +
 nemu/default.nix              | 49 ++++++++++++++++++++++
 nemu/flake.nix                | 79 -----------------------------------
 nemu/scripts/config.mk        |  5 ++-
 result                        |  1 +
 9 files changed, 104 insertions(+), 184 deletions(-)
 rename nemu/flake.lock => flake.lock (54%)
 create mode 100644 flake.nix
 delete mode 100644 nemu/.config
 create mode 100644 nemu/default.nix
 delete mode 100644 nemu/flake.nix
 create mode 120000 result

diff --git a/abstract-machine/Makefile b/abstract-machine/Makefile
index 291a3d5..3a5d60a 100644
--- a/abstract-machine/Makefile
+++ b/abstract-machine/Makefile
@@ -66,8 +66,8 @@ LINKAGE   = $(OBJS) \
 ## 3. General Compilation Flags
 
 ### (Cross) compilers, e.g., mips-linux-gnu-g++
-AS        = $(CROSS_COMPILE)cc
-CC        = $(CROSS_COMPILE)cc
+AS        = $(CROSS_COMPILE)gcc
+CC        = $(CROSS_COMPILE)gcc
 CXX       = $(CROSS_COMPILE)g++
 LD        = $(CROSS_COMPILE)ld
 AR        = $(CROSS_COMPILE)ar
diff --git a/nemu/flake.lock b/flake.lock
similarity index 54%
rename from nemu/flake.lock
rename to flake.lock
index 5c64744..36b9a39 100644
--- a/nemu/flake.lock
+++ b/flake.lock
@@ -5,11 +5,11 @@
         "systems": "systems"
       },
       "locked": {
-        "lastModified": 1701680307,
-        "narHash": "sha256-kAuep2h5ajznlPMD9rnQyffWG8EM/C73lejGofXvdM8=",
+        "lastModified": 1709126324,
+        "narHash": "sha256-q6EQdSeUZOG26WelxqkmR7kArjgWCdw5sfJVHPH/7j8=",
         "owner": "numtide",
         "repo": "flake-utils",
-        "rev": "4022d587cbbfd70fe950c1e2083a02621806a725",
+        "rev": "d465f4819400de7c8d874d50b982301f28a84605",
         "type": "github"
       },
       "original": {
@@ -20,11 +20,11 @@
     },
     "nixpkgs": {
       "locked": {
-        "lastModified": 1704722960,
-        "narHash": "sha256-mKGJ3sPsT6//s+Knglai5YflJUF2DGj7Ai6Ynopz0kI=",
+        "lastModified": 1709237383,
+        "narHash": "sha256-cy6ArO4k5qTx+l5o+0mL9f5fa86tYUX3ozE1S+Txlds=",
         "owner": "NixOS",
         "repo": "nixpkgs",
-        "rev": "317484b1ead87b9c1b8ac5261a8d2dd748a0492d",
+        "rev": "1536926ef5621b09bba54035ae2bb6d806d72ac8",
         "type": "github"
       },
       "original": {
@@ -34,31 +34,10 @@
         "type": "github"
       }
     },
-    "nur-xin": {
-      "inputs": {
-        "nixpkgs": [
-          "nixpkgs"
-        ]
-      },
-      "locked": {
-        "lastModified": 1704680562,
-        "narHash": "sha256-ffec3HL8OgbHB/TvLHILvC3ylou6N+KDtrn4qYVV+U4=",
-        "ref": "refs/heads/master",
-        "rev": "8adf33b6fdd113c645d83feda28622a0b1ef9f83",
-        "revCount": 144,
-        "type": "git",
-        "url": "https://git.xinyang.life/xin/nur.git"
-      },
-      "original": {
-        "type": "git",
-        "url": "https://git.xinyang.life/xin/nur.git"
-      }
-    },
     "root": {
       "inputs": {
         "flake-utils": "flake-utils",
-        "nixpkgs": "nixpkgs",
-        "nur-xin": "nur-xin"
+        "nixpkgs": "nixpkgs"
       }
     },
     "systems": {
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..a4c3830
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,39 @@
+{
+  inputs = {
+    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
+    flake-utils.url = "github:numtide/flake-utils";
+  };
+
+  outputs = { self, ... }@inputs: with inputs;
+    flake-utils.lib.eachDefaultSystem (system:
+      let
+        pkgs = nixpkgs.legacyPackages.${system};
+        crossPkgs = import nixpkgs {
+          localSystem = system;
+          crossSystem = {
+            config = "riscv32-none-elf";
+            abi = "ilp32";
+          };
+        };
+      in
+      {
+        packages.nemu = pkgs.callPackage ./nemu {};
+        
+        devShells.am-kernels = crossPkgs.mkShell {
+          inputsFrom = [
+            self.packages.${system}.nemu
+          ];
+          packages = [
+            pkgs.stdenv.cc
+            pkgs.libllvm
+          ];
+          shellHook = ''
+            export PROJECT_ROOT=/home/xin/repo/ysyx-workbench
+            export AM_HOME=$PROJECT_ROOT/abstract-machine;
+            export NEMU_HOME=$PROJECT_ROOT/nemu;
+          '';
+        };
+      }
+    );
+}
+
diff --git a/nemu/.config b/nemu/.config
deleted file mode 100644
index aa26803..0000000
--- a/nemu/.config
+++ /dev/null
@@ -1,74 +0,0 @@
-#
-# Automatically generated file; DO NOT EDIT.
-# NEMU Configuration Menu
-#
-# CONFIG_ISA_x86 is not set
-# CONFIG_ISA_mips32 is not set
-CONFIG_ISA_riscv=y
-# CONFIG_ISA_loongarch32r is not set
-CONFIG_ISA="riscv32"
-
-#
-# ISA-dependent Options for riscv
-#
-# CONFIG_RV64 is not set
-# CONFIG_RVE is not set
-# end of ISA-dependent Options for riscv
-
-CONFIG_ENGINE_INTERPRETER=y
-CONFIG_ENGINE="interpreter"
-CONFIG_MODE_SYSTEM=y
-CONFIG_TARGET_NATIVE_ELF=y
-# CONFIG_TARGET_SHARE is not set
-# CONFIG_TARGET_AM is not set
-
-#
-# Build Options
-#
-CONFIG_CC_GCC=y
-# CONFIG_CC_GPP is not set
-# CONFIG_CC_CLANG is not set
-CONFIG_CC="gcc"
-# CONFIG_CC_O0 is not set
-# CONFIG_CC_O1 is not set
-CONFIG_CC_O2=y
-# CONFIG_CC_O3 is not set
-CONFIG_CC_OPT="-O2"
-# CONFIG_CC_LTO is not set
-# CONFIG_CC_DEBUG is not set
-CONFIG_CC_ASAN=y
-# end of Build Options
-
-#
-# Testing and Debugging
-#
-CONFIG_TRACE=y
-CONFIG_TRACE_START=0
-CONFIG_TRACE_END=10000
-CONFIG_ITRACE=y
-CONFIG_ITRACE_COND="true"
-# CONFIG_DIFFTEST is not set
-CONFIG_DIFFTEST_REF_PATH="none"
-CONFIG_DIFFTEST_REF_NAME="none"
-# end of Testing and Debugging
-
-#
-# Memory Configuration
-#
-CONFIG_MBASE=0x80000000
-CONFIG_MSIZE=0x8000000
-CONFIG_PC_RESET_OFFSET=0
-# CONFIG_PMEM_MALLOC is not set
-CONFIG_PMEM_GARRAY=y
-CONFIG_MEM_RANDOM=y
-# end of Memory Configuration
-
-# CONFIG_DEVICE is not set
-
-#
-# Miscellaneous
-#
-CONFIG_TIMER_GETTIMEOFDAY=y
-# CONFIG_TIMER_CLOCK_GETTIME is not set
-CONFIG_RT_CHECK=y
-# end of Miscellaneous
diff --git a/nemu/Makefile b/nemu/Makefile
index 4c7ed77..0f360fe 100644
--- a/nemu/Makefile
+++ b/nemu/Makefile
@@ -40,7 +40,9 @@ SRCS-y += $(shell find -L $(DIRS-y) -name "*.c")
 SRCS = $(filter-out $(SRCS-BLACKLIST-y),$(SRCS-y))
 
 # Extract compiler and options from menuconfig
+ifneq ($(CONFIG_CC),)
 CC = $(call remove_quote,$(CONFIG_CC))
+endif
 CFLAGS_BUILD += $(call remove_quote,$(CONFIG_CC_OPT))
 CFLAGS_BUILD += $(if $(CONFIG_CC_LTO),-flto,)
 CFLAGS_BUILD += $(if $(CONFIG_CC_DEBUG),-Og -ggdb3,)
diff --git a/nemu/default.nix b/nemu/default.nix
new file mode 100644
index 0000000..3368ee0
--- /dev/null
+++ b/nemu/default.nix
@@ -0,0 +1,49 @@
+{ pkgs,
+  lib,
+  stdenv
+}:
+
+stdenv.mkDerivation rec {
+  pname = "nemu";
+  version = "2024-03-02";
+
+  src = ./.;
+
+  NEMU_HOME = "/build/nemu";
+  nativeBuildInputs = with pkgs; [
+    gnumake
+    flex
+    bison
+  ];
+
+  buildInputs = with pkgs; [
+    check
+    readline
+    libllvm
+  ];
+
+  configurePhase = ''
+    echo pwd=$(pwd)
+    make alldefconfig
+  '';
+
+  buildPhase = ''
+    make
+  '';
+
+  checkPhase = ''
+    make test
+  '';
+
+  installPhase = ''
+    mkdir -p $out/bin
+    make PREFIX=$out install
+  '';
+
+  meta = with lib; {
+    description = "NJU EMUlator, a full system x86/mips32/riscv32/riscv64 emulator for teaching";
+    homepage = "https://github.com/NJU-ProjectN/nemu.git";
+    license = with licenses; [ ];
+    maintainers = with maintainers; [ ];
+  };
+}
diff --git a/nemu/flake.nix b/nemu/flake.nix
deleted file mode 100644
index c09002b..0000000
--- a/nemu/flake.nix
+++ /dev/null
@@ -1,79 +0,0 @@
-{
-  inputs = {
-    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
-    flake-utils.url = "github:numtide/flake-utils";
-    nur-xin = {
-      url = "git+https://git.xinyang.life/xin/nur.git";
-      inputs.nixpkgs.follows = "nixpkgs";
-    };
-  };
-
-  outputs = { self, ... }@inputs: with inputs;
-    flake-utils.lib.eachDefaultSystem (system:
-      let
-        pkgs = nixpkgs.legacyPackages.${system} //
-          { nur.xin = nur-xin.legacyPackages.${system}; };
-      in
-      {
-        devShells.default = with pkgs; mkShell {
-          packages = [
-            clang-tools
-            rnix-lsp
-            bear
-
-            gdb
-            jre
-
-            gtkwave
-          ];
-
-          inputsFrom = [ self.packages.${system}.nemu ];
-        };
-
-        packages.nemu = with pkgs; stdenv.mkDerivation rec {
-          pname = "nemu";
-          version = "2024-01-02";
-
-          src = ./.;
-
-          nativeBuildInputs = [
-            gnumake
-            flex
-            bison
-            pkg-config
-            python3       # for testing
-          ];
-
-          buildInputs = [
-            check
-            readline
-            libllvm
-          ];
-
-          configurePhase = ''
-            echo pwd=$(pwd)
-          '';
-
-          buildPhase = ''
-            make NEMU_HOME=/build/nemu --trace
-          '';
-
-          installPhase = ''
-            BUILD_DIR=$out make install
-          '';
-
-          checkPhase = ''
-            BUILD_DIR=$out make test
-          '';
-
-          meta = with lib; {
-            description = "NJU EMUlator, a full system x86/mips32/riscv32/riscv64 emulator for teaching";
-            homepage = "https://github.com/NJU-ProjectN/nemu.git";
-            license = with licenses; [ ];
-            maintainers = with maintainers; [ ];
-          };
-        };
-      }
-    );
-}
-
diff --git a/nemu/scripts/config.mk b/nemu/scripts/config.mk
index 0525ee3..01b9d1f 100644
--- a/nemu/scripts/config.mk
+++ b/nemu/scripts/config.mk
@@ -48,6 +48,9 @@ menuconfig: $(MCONF) $(CONF) $(FIXDEP)
 savedefconfig: $(CONF)
 	$(Q)$< $(silent) --$@=configs/defconfig $(Kconfig)
 
+alldefconfig: $(CONF) $(FIXDEP)
+	$(Q)$(CONF) $(silent) --$@ $(Kconfig)
+
 %defconfig: $(CONF) $(FIXDEP)
 	$(Q)$< $(silent) --defconfig=configs/$@ $(Kconfig)
 	$(Q)$< $(silent) --syncconfig $(Kconfig)
@@ -60,7 +63,7 @@ help:
 	@echo  '  savedefconfig   - Save current config as configs/defconfig (minimal config)'
 
 distclean: clean
-	-@rm -rf $(rm-distclean)
+	-rm -rf $(rm-distclean)
 
 .PHONY: help distclean
 
diff --git a/result b/result
new file mode 120000
index 0000000..b299d6f
--- /dev/null
+++ b/result
@@ -0,0 +1 @@
+/nix/store/3x5nz7zcwq0zhcff1d3l9lpmvmnmx2i5-nemu-2024-03-02
\ No newline at end of file