diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..c4b17d7 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use_flake diff --git a/.gitignore b/.gitignore index 8a43538..b1da1ab 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,6 @@ *.pcap # docship -/output \ No newline at end of file +/output + +.direnv/ diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..8910963 --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1717179513, + "narHash": "sha256-vboIEwIQojofItm2xGCdZCzW96U85l9nDW3ifMuAIdM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "63dacb46bf939521bdc93981b4cbb7ecb58427a0", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..b2678c5 --- /dev/null +++ b/flake.nix @@ -0,0 +1,54 @@ +{ + description = "build environment"; + + inputs.nixpkgs.url = "github:NixOS/nixpkgs/24.05"; + + inputs.flake-utils.url = "github:numtide/flake-utils"; + + outputs = + { self + , nixpkgs + , flake-utils + , + }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + packages = { + default = pkgs.stdenv.mkDerivation { + version = "1.0.0"; + pname = "dev env"; + src = ./.; + + nativeBuildInputs = with pkgs.buildPackages; [ + llvmPackages.clang-unwrapped + llvmPackages.bintools-unwrapped + ]; + + # first path will be computed within build env, the second one will be taken from the environment + installPhase = '' + mkdir -p $out/bin + { echo "export PATH=$PATH:\$PATH"; + echo "exec \$SHELL"; + } >> $out/bin/dev-env + + chmod +x $out/bin/dev-env + ''; + }; + }; + + devShells.default = pkgs.mkShell { + nativeBuildInputs = with pkgs.buildPackages; [ + llvmPackages.clang-unwrapped + llvmPackages.bintools-unwrapped + ]; + + shellHook = '' + ''; + }; + } + ); +} diff --git a/run.sh b/run.sh index 4ba5532..d697882 100755 --- a/run.sh +++ b/run.sh @@ -2,13 +2,32 @@ set -xue QEMU=qemu-system-riscv32 -CC=/opt/homebrew/opt/llvm/bin/clang -OBJCOPY=/opt/homebrew/opt/llvm/bin/llvm-objcopy -CFLAGS="-std=c11 -O2 -g3 -Wall -Wextra --target=riscv32 -ffreestanding -nostdlib" +if [[ -n "$IN_NIX_SHELL" ]]; then +CC=clang +OBJCOPY=llvm-objcopy +else +TOOLCHAIN="${TOOLCHAIN:-}" +if [[ ! -f $TOOLCHAIN/clang ]]; then +if [[ -f /run/current-system/sw/bin/clang ]]; then +TOOLCHAIN=/run/current-system/sw/bin +elif [[ -f /opt/homebrew/opt/llvm/bin/clang ]]; then +TOOLCHAIN=/opt/homebrew/opt/llvm/bin/ +else +echo "Cannot find toolchain" +exit 1 +fi +fi +echo "Using toolchain at ${TOOLCHAIN}" +CC=$TOOLCHAIN/clang +OBJCOPY=$TOOLCHAIN/llvm-objcopy +fi + +CFLAGS="-std=c11 -O2 -gdwarf-4 -g3 -Wall -Wextra --target=riscv32-unknown-elf -fno-stack-protector -ffreestanding -nostdlib" # シェルをビルド $CC $CFLAGS -Wl,-Tuser.ld -Wl,-Map=shell.map -o shell.elf shell.c user.c common.c + $OBJCOPY --set-section-flags .bss=alloc,contents -O binary shell.elf shell.bin $OBJCOPY -Ibinary -Oelf32-littleriscv shell.bin shell.bin.o