build: initial support for building nemu, am-kernels with nix

This commit is contained in:
xinyangli 2024-03-04 11:59:50 +08:00
parent f9808d0a40
commit 3c7c5f060e
No known key found for this signature in database
6 changed files with 139 additions and 123 deletions

View file

@ -47,33 +47,32 @@ endif
### Create the destination directory (`build/$ARCH`)
WORK_DIR = $(shell pwd)
DST_DIR = $(WORK_DIR)/build/$(ARCH)
BUILD_DIR ?= $(WORK_DIR)/build
DST_DIR = $(BUILD_DIR)/$(ARCH)
$(shell mkdir -p $(DST_DIR))
### Compilation targets (a binary image or archive)
IMAGE_REL = build/$(NAME)-$(ARCH)
IMAGE_REL = $(DST_DIR)/$(NAME)-$(ARCH)
IMAGE = $(abspath $(IMAGE_REL))
ARCHIVE = $(WORK_DIR)/build/$(NAME)-$(ARCH).a
ARCHIVE = $(BUILD_DIR)/$(NAME)-$(ARCH).a
### Collect the files to be linked: object files (`.o`) and libraries (`.a`)
OBJS = $(addprefix $(DST_DIR)/, $(addsuffix .o, $(basename $(SRCS))))
LIBS := $(sort $(LIBS) am klib) # lazy evaluation ("=") causes infinite recursions
LINKAGE = $(OBJS) \
$(addsuffix -$(ARCH).a, $(join \
$(addsuffix /build/, $(addprefix $(AM_HOME)/, $(LIBS))), \
$(LIBS) ))
$(addsuffix -$(ARCH).a, $(addprefix $(BUILD_DIR)/, $(LIBS)))
## 3. General Compilation Flags
### (Cross) compilers, e.g., mips-linux-gnu-g++
AS = $(CROSS_COMPILE)gcc
CC = $(CROSS_COMPILE)gcc
CXX = $(CROSS_COMPILE)g++
LD = $(CROSS_COMPILE)ld
AR = $(CROSS_COMPILE)ar
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
READELF = $(CROSS_COMPILE)readelf
AS ?= $(CROSS_COMPILE)gcc
CC ?= $(CROSS_COMPILE)gcc
CXX ?= $(CROSS_COMPILE)g++
LD ?= $(CROSS_COMPILE)ld
AR ?= $(CROSS_COMPILE)ar
OBJDUMP ?= $(CROSS_COMPILE)objdump
OBJCOPY ?= $(CROSS_COMPILE)objcopy
READELF ?= $(CROSS_COMPILE)readelf
### Compilation flags
INC_PATH += $(WORK_DIR)/include $(addsuffix /include/, $(addprefix $(AM_HOME)/, $(LIBS)))

View file

@ -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": {

66
flake.nix Normal file
View file

@ -0,0 +1,66 @@
{
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 {};
packages.am-kernels = crossPkgs.stdenv.mkDerivation rec {
pname = "am-kernels";
version = "2024.02.18";
src = pkgs.fetchFromGitHub {
owner = "NJU-ProjectN";
repo = "am-kernels";
rev = "bb725d6f8223dd7de831c3b692e8c4531e9d01af";
hash = "sha256-ZHdrw28TN8cMvhhzM469OV7cp0Yp+8yao855HP4+P4A=";
};
AM_HOME = pkgs.fetchFromGitHub {
owner = "xinyangli";
repo = "abstract-machine";
rev = "788595aac61c6b2f3b78ca8aa7d08dc33911bca4";
hash = "sha256-YvWHIBP9tz3HL2TyibftvvQrpkWUDPnviCF4oyLmdjg=";
};
ARCH = "riscv32-nemu";
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 --trace
'';
installPhase = ''
mkdir -p $out/bin
cp build/riscv32-nemu/*.bin $out/bin
'';
dontFixup = true;
};
devShells.default = pkgs.mkShell {
packages = with pkgs; [
gdb
] ++ builtins.attrValues self.packages.${system};
};
}
);
}

49
nemu/default.nix Normal file
View file

@ -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; [ ];
};
}

View file

@ -1,80 +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 NEMU_HOME=$NEMU_HOME
echo pwd=$(pwd)
mkdir -p $(pwd)/kconfig
WORK_DIR=$(pwd) obj=$(pwd)/kconfig make --trace -e -f scripts/config.mk WORK_DIR=$(pwd) obj=$(pwd)/kconfig rv32_defconfig
'';
installPhase = ''
BUILD_DIR=$out make install
'';
checkPhase = ''
BUILD_DIR=$out make test
'';
NEMU_HOME = src;
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; [ ];
};
};
}
);
}

View file

@ -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