From 081e6614696201004b33654e560ab009f307c1bf Mon Sep 17 00:00:00 2001 From: Naxdy Date: Tue, 16 Apr 2024 16:18:04 +0200 Subject: [PATCH] add flake --- .cargo/config.toml | 28 --------- .envrc | 1 + .gitignore | 4 +- build.rs | 5 ++ build.sh | 3 - flake.lock | 151 +++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 95 ++++++++++++++++++++++++++++ 7 files changed, 255 insertions(+), 32 deletions(-) delete mode 100644 .cargo/config.toml create mode 100644 .envrc delete mode 100755 build.sh create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 3f729d3..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,28 +0,0 @@ -[target.'cfg(all(target_arch = "arm", target_os = "none"))'] -# Choose a default "cargo run" tool: -# - probe-run provides flashing and defmt via a hardware debugger, and stack unwind on panic -# - elf2uf2-rs loads firmware over USB when the rp2040 is in boot mode -# - "probe-rs-cli run" is similar to probe-run but it uses the latest probe-rs lib crate -runner = "probe-run --chip RP2040" -# runner = "elf2uf2-rs -d" -# runner = "probe-rs-cli run --chip RP2040 --protocol swd" - -rustflags = [ - "-C", "linker=flip-link", - "-C", "link-arg=--nmagic", - "-C", "link-arg=-Tlink.x", - "-C", "link-arg=-Tdefmt.x", - - # Code-size optimizations. - # trap unreachable can save a lot of space, but requires nightly compiler. - # uncomment the next line if you wish to enable it - # "-Z", "trap-unreachable=no", - "-C", "inline-threshold=5", - "-C", "no-vectorize-loops", -] - -[target.thumbv6m-none-eabi] -runner = "elf2uf2-rs -d" - -[env] -DEFMT_LOG = "debug" \ No newline at end of file diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 9f97022..ed62785 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -target/ \ No newline at end of file +target/ +/result +/.direnv \ No newline at end of file diff --git a/build.rs b/build.rs index d534cc3..9d6f8ab 100644 --- a/build.rs +++ b/build.rs @@ -28,4 +28,9 @@ fn main() { // here, we ensure the build script is only re-run when // `memory.x` is changed. println!("cargo:rerun-if-changed=memory.x"); + + println!("cargo:rustc-link-arg-bins=--nmagic"); + println!("cargo:rustc-link-arg-bins=-Tlink.x"); + println!("cargo:rustc-link-arg-bins=-Tdefmt.x"); + println!("cargo:rustc-linker=flip-link"); } diff --git a/build.sh b/build.sh deleted file mode 100755 index 5219252..0000000 --- a/build.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -(cargo build --target thumbv6m-none-eabi --release && elf2uf2-rs target/thumbv6m-none-eabi/release/rustgcc) || echo "Build failed." \ No newline at end of file diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..c347755 --- /dev/null +++ b/flake.lock @@ -0,0 +1,151 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "inputs": { + "systems": "systems_2" + }, + "locked": { + "lastModified": 1705309234, + "narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "naersk": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1698420672, + "narHash": "sha256-/TdeHMPRjjdJub7p7+w55vyABrsJlt5QkznPYy55vKA=", + "owner": "nmattia", + "repo": "naersk", + "rev": "aeb58d5e8faead8980a807c840232697982d47b9", + "type": "github" + }, + "original": { + "owner": "nmattia", + "repo": "naersk", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1713145326, + "narHash": "sha256-m7+IWM6mkWOg22EC5kRUFCycXsXLSU7hWmHdmBfmC3s=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "53a2c32bc66f5ae41a28d7a9a49d321172af621e", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1706487304, + "narHash": "sha256-LE8lVX28MV2jWJsidW13D2qrHU/RUUONendL2Q/WlJg=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "90f456026d284c22b3e3497be980b2e47d0b28ac", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "naersk": "naersk", + "nixpkgs": "nixpkgs", + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "flake-utils": "flake-utils_2", + "nixpkgs": "nixpkgs_2" + }, + "locked": { + "lastModified": 1713233539, + "narHash": "sha256-dPGrCy5ttx6E3bUOmDynY/cAotRqvoIAimZlbv+Zr1w=", + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "847bc25ebab8dc72a86d2b1f0c088740eebbb1b8", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + }, + "systems_2": { + "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..3212e4c --- /dev/null +++ b/flake.nix @@ -0,0 +1,95 @@ +{ + description = "Firmware for the NaxGCC"; + + inputs = { + nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-23.11"; + + rust-overlay.url = "github:oxalica/rust-overlay"; + + flake-utils.url = "github:numtide/flake-utils"; + + naersk = { + url = "github:nmattia/naersk"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = + { self + , nixpkgs + , rust-overlay + , flake-utils + , naersk + }: (flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + overlays = [ + rust-overlay.overlays.default + ]; + config.allowUnsupportedSystem = true; + }; + + rustToolchain = pkgs.rust-bin.stable.latest.default.override { + extensions = [ + "rust-src" + ]; + targets = [ + "thumbv6m-none-eabi" + ]; + }; + + naersk_lib = naersk.lib.${system}.override { + cargo = rustToolchain; + rustc = rustToolchain; + }; + + CARGO_BUILD_TARGET = "thumbv6m-none-eabi"; + + RUSTFLAGS = [ + "-Clinker=flip-link" + "-Cinline-threshold=5" + "-Cno-vectorize-loops" + ]; + in + { + packages.default = self.packages.${system}.rustgcc-uf2; + + packages.rustgcc-uf2 = pkgs.runCommandLocal "${self.packages.${system}.rustgcc.pname}-uf2-${self.packages.${system}.rustgcc.version}" { } '' + mkdir -p $out/bin + ${pkgs.elf2uf2-rs}/bin/elf2uf2-rs ${self.packages.${system}.rustgcc}/bin/${self.packages.${system}.rustgcc.pname} $out/bin/${self.packages.${system}.rustgcc.pname}.uf2 + ''; + + packages.rustgcc = naersk_lib.buildPackage { + pname = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.name; + version = (builtins.fromTOML (builtins.readFile ./Cargo.toml)).package.version; + + src = self; + + nativeBuildInputs = [ + pkgs.flip-link + ]; + + cargoBuildOptions = _orig: _orig ++ [ + "--target=${CARGO_BUILD_TARGET}" + ]; + + # if a tree falls in the forest and no one is around to hear it, does it make a sound? + DEFMT_LOG = "off"; + + inherit RUSTFLAGS; + }; + + devShells.default = pkgs.mkShell { + nativeBuildInputs = builtins.attrValues { + inherit rustToolchain; + inherit (pkgs) gcc-arm-embedded elf2uf2-rs picotool probe-rs cargo-expand flip-link; + }; + + CARGO_TARGET_THUMBV6M_NONE_EABI_RUNNER = "probe-rs run --chip RP2040 --protocol swd"; + DEFMT_LOG = "debug"; + + inherit CARGO_BUILD_TARGET RUSTFLAGS; + }; + })); +}