From 479625b3a34a69ada537d74c4ff626414d05ea54 Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Thu, 3 Apr 2025 11:44:23 +0200 Subject: [PATCH 1/4] repo: removed shell.nix and moved to nix flakes --- .bazelignore | 3 +++ README.md | 8 ++++++- flake.lock | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ shell.nix | 41 ----------------------------------- 5 files changed, 128 insertions(+), 42 deletions(-) create mode 100644 .bazelignore create mode 100644 flake.lock create mode 100644 flake.nix delete mode 100644 shell.nix diff --git a/.bazelignore b/.bazelignore new file mode 100644 index 0000000..7ae00bf --- /dev/null +++ b/.bazelignore @@ -0,0 +1,3 @@ +# While using flakes, bazel might pickup the sources used +# as flake input stored in direnv and lead to a broken build. +.direnv diff --git a/README.md b/README.md index 418fd5d..38d9432 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,12 @@ It has been tested with the Artix-7 [counter example][counter-example], where it ## Usage -First, install [Bazel][bazel] and ensure you have a basic C/C++ toolchain set up. Then run: +First, install [Bazel][bazel] and ensure you have a basic C/C++ toolchain set up. + +> [!NOTE] +> If you are using Nix or NixOS, ensure you have [flakes enabled][enable-flakes] and enter the development shell via `nix develop`. + +Then run: ``` bazel run -c opt //fpga:fpga-as -- --prjxray_db_path=/some/path/prjxray-db/artix7 --part=xc7a35tcsg324-1 < /some/path.fasm > output.bit @@ -66,3 +71,4 @@ Using this metadata, you can search the segbits database for the specific featur [bazel]: https://bazel.build/ [counter-example]: https://github.com/chipsalliance/f4pga-examples/blob/13f11197b33dae1cde3bf146f317d63f0134eacf/xc7/counter_test/counter.v [open-fpga-loader]: https://github.com/trabucayre/openFPGALoader +[enable-flakes]: https://nixos.wiki/wiki/Flakes#Enable_flakes_permanently_in_NixOS diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..893b86e --- /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": 1743583204, + "narHash": "sha256-F7n4+KOIfWrwoQjXrL2wD9RhFYLs2/GGe/MQY1sSdlE=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2c8d3f48d33929642c1c12cd243df4cc7d2ce434", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "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..b1bae5b --- /dev/null +++ b/flake.nix @@ -0,0 +1,57 @@ +{ + description = "fpga-assembler"; + + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = import nixpkgs { + inherit system; + }; + in { + devShells.default = + let + bazel = pkgs.bazel_7; + + # There is too much volatility between even micro-versions of + # newer clang-format. Use slightly older version for now. + clang_for_formatting = pkgs.llvmPackages_17.clang-tools; + + # clang tidy: use latest. + clang_for_tidy = pkgs.llvmPackages_18.clang-tools; + in with pkgs; pkgs.mkShell{ + packages = with pkgs; [ + git + bazel + jdk + bash + gdb + + # For clang-tidy and clang-format. + clang_for_formatting + clang_for_tidy + + # For buildifier, buildozer. + bazel-buildtools + bant + + # Profiling and sanitizers. + linuxPackages_latest.perf + pprof + perf_data_converter + valgrind + + # FPGA utils. + openfpgaloader + ]; + + CLANG_TIDY="${clang_for_tidy}/bin/clang-tidy"; + CLANG_FORMAT="${clang_for_formatting}/bin/clang-format"; + }; + } + ); +} diff --git a/shell.nix b/shell.nix deleted file mode 100644 index 511b076..0000000 --- a/shell.nix +++ /dev/null @@ -1,41 +0,0 @@ -{ pkgs ? import {} }: -let - bazel = pkgs.bazel_7; - - # There is too much volatility between even micro-versions of - # newer clang-format. Use slightly older version for now. - clang_for_formatting = pkgs.llvmPackages_17.clang-tools; - - # clang tidy: use latest. - clang_for_tidy = pkgs.llvmPackages_18.clang-tools; -in -pkgs.mkShell { - name = "fpga-assembler"; - packages = with pkgs; [ - git - bazel - jdk - bash - gdb - - # For clang-tidy and clang-format. - clang_for_formatting - clang_for_tidy - - # For buildifier, buildozer. - bazel-buildtools - bant - - # Profiling and sanitizers. - linuxPackages_latest.perf - pprof - perf_data_converter - valgrind - - # FPGA utils. - openfpgaloader - ]; - - CLANG_TIDY="${clang_for_tidy}/bin/clang-tidy"; - CLANG_FORMAT="${clang_for_formatting}/bin/clang-format"; -} From 6ff89a2d53edc9d74a402c28096450473b67de13 Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Thu, 3 Apr 2025 16:37:31 +0200 Subject: [PATCH 2/4] flake: added package --- flake.nix | 100 +++++++++++++++++++++++++++++++++++++++++++--- fpga/BUILD | 4 +- fpga/xilinx/BUILD | 4 +- 3 files changed, 101 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index b1bae5b..2fa1368 100644 --- a/flake.nix +++ b/flake.nix @@ -6,17 +6,19 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, flake-utils }: + outputs = { self, nixpkgs, flake-utils } @inputs: flake-utils.lib.eachDefaultSystem (system: let pkgs = import nixpkgs { inherit system; }; + common = with pkgs; { + bazel = bazel_7; + jdk = jdk; + }; in { devShells.default = let - bazel = pkgs.bazel_7; - # There is too much volatility between even micro-versions of # newer clang-format. Use slightly older version for now. clang_for_formatting = pkgs.llvmPackages_17.clang-tools; @@ -26,8 +28,8 @@ in with pkgs; pkgs.mkShell{ packages = with pkgs; [ git - bazel - jdk + common.bazel + common.jdk bash gdb @@ -51,7 +53,95 @@ CLANG_TIDY="${clang_for_tidy}/bin/clang-tidy"; CLANG_FORMAT="${clang_for_formatting}/bin/clang-format"; + + shellHook = '' + exec bash + ''; }; + + # Package fpga-assembler. + packages.default = (pkgs.callPackage ( + { + buildBazelPackage, + stdenv, + fetchFromGitHub, + lib, + nix-gitignore, + }: + let + system = stdenv.hostPlatform.system; + registry = fetchFromGitHub { + owner = "bazelbuild"; + repo = "bazel-central-registry"; + rev = "63f3af762b2fdd7acaa7987856cd3ac314eaea09"; + hash = "sha256-ugNzoP0gdrhl9vH1TRdwoevuTsSqjitXnAoMSSTlCgI="; + }; + in + buildBazelPackage { + pname = "fpga-as"; + + version = "0.0.1"; + + src = nix-gitignore.gitignoreSourcePure [] ./.; + + bazelFlags = [ + "--registry" + "file://${registry}" + ]; + + postPatch = '' + patchShebangs scripts/create-workspace-status.sh + ''; + + fetchAttrs = { + hash = + { + aarch64-linux = "sha256-E4VHjDa0qkHmKUNpTBfJi7dhMLcd1z5he+p31/XvUl8="; + x86_64-linux = "sha256-hVBJB0Hsd9sXuEoNcjhTkbPl89vlZT1w39JppCD+n8Y="; + } + .${system} or (throw "No hash for system: ${system}"); + }; + + removeRulesCC = false; + removeLocalConfigCc = false; + removeLocalConfigSh = false; + + nativeBuildInputs = [ + common.jdk + pkgs.git + pkgs.bash + # Convenient tool to enter into the sandbox and start debugging. + pkgs.breakpointHook + ]; + + bazel = common.bazel; + + bazelBuildFlags = [ "-c opt" ]; + bazelTestTargets = [ "//..." ]; + bazelTargets = [ "//fpga:fpga-as" ]; + + buildAttrs = { + installPhase = '' + install -D --strip bazel-bin/fpga/fpga-as "$out/bin/fpga-as" + ''; + }; + + meta = { + description = "Tool to convert FASM to FPGA bitstream."; + homepage = "https://github.com/lromor/fpga-assembler"; + license = lib.licenses.asl20; + platforms = lib.platforms.linux; + }; + } + ) {}).overrideAttrs (final: prev: { + # Fixup the deps so they always contain correrct + # shebangs paths pointing to the store. + deps = prev.deps.overrideAttrs (final: prev: { + installPhase = '' + patchShebangs $bazelOut/external + '' + prev.installPhase; + }); + }); } ); } diff --git a/fpga/BUILD b/fpga/BUILD index d059041..a8fe239 100644 --- a/fpga/BUILD +++ b/fpga/BUILD @@ -9,7 +9,9 @@ package( license( name = "license", package_name = "fpga", - license_kind = "@rules_license//licenses/spdx:Apache-2.0", + license_kinds = [ + "@rules_license//licenses/spdx:Apache-2.0", + ], ) cc_library( diff --git a/fpga/xilinx/BUILD b/fpga/xilinx/BUILD index 644e65d..b2cc22a 100644 --- a/fpga/xilinx/BUILD +++ b/fpga/xilinx/BUILD @@ -9,7 +9,9 @@ package( license( name = "license", package_name = "xilinx", - license_kind = "@rules_license//licenses/spdx:ISC", + license_kinds = [ + "@rules_license//licenses/spdx:ISC" + ], ) cc_library( From d8f225f8b1687a7b81962b8c2b3a45ce4a538676 Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Thu, 3 Apr 2025 23:19:19 +0200 Subject: [PATCH 3/4] bazel: fixed missing comma in build file --- fpga/xilinx/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fpga/xilinx/BUILD b/fpga/xilinx/BUILD index b2cc22a..f506f0c 100644 --- a/fpga/xilinx/BUILD +++ b/fpga/xilinx/BUILD @@ -10,7 +10,7 @@ license( name = "license", package_name = "xilinx", license_kinds = [ - "@rules_license//licenses/spdx:ISC" + "@rules_license//licenses/spdx:ISC", ], ) From db157082d43ca05476f64da7f99e461951b3a38b Mon Sep 17 00:00:00 2001 From: Leonardo Romor Date: Fri, 4 Apr 2025 18:06:49 +0200 Subject: [PATCH 4/4] flake: formatted flake --- flake.nix | 85 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 34 deletions(-) diff --git a/flake.nix b/flake.nix index 2fa1368..59ed25d 100644 --- a/flake.nix +++ b/flake.nix @@ -6,8 +6,14 @@ flake-utils.url = "github:numtide/flake-utils"; }; - outputs = { self, nixpkgs, flake-utils } @inputs: - flake-utils.lib.eachDefaultSystem (system: + outputs = + { + self, + nixpkgs, + flake-utils, + }@inputs: + flake-utils.lib.eachDefaultSystem ( + system: let pkgs = import nixpkgs { inherit system; @@ -16,7 +22,8 @@ bazel = bazel_7; jdk = jdk; }; - in { + in + { devShells.default = let # There is too much volatility between even micro-versions of @@ -25,7 +32,9 @@ # clang tidy: use latest. clang_for_tidy = pkgs.llvmPackages_18.clang-tools; - in with pkgs; pkgs.mkShell{ + in + with pkgs; + pkgs.mkShell { packages = with pkgs; [ git common.bazel @@ -51,8 +60,8 @@ openfpgaloader ]; - CLANG_TIDY="${clang_for_tidy}/bin/clang-tidy"; - CLANG_FORMAT="${clang_for_formatting}/bin/clang-format"; + CLANG_TIDY = "${clang_for_tidy}/bin/clang-tidy"; + CLANG_FORMAT = "${clang_for_formatting}/bin/clang-format"; shellHook = '' exec bash @@ -60,29 +69,30 @@ }; # Package fpga-assembler. - packages.default = (pkgs.callPackage ( - { - buildBazelPackage, - stdenv, - fetchFromGitHub, - lib, - nix-gitignore, - }: - let - system = stdenv.hostPlatform.system; - registry = fetchFromGitHub { - owner = "bazelbuild"; - repo = "bazel-central-registry"; - rev = "63f3af762b2fdd7acaa7987856cd3ac314eaea09"; - hash = "sha256-ugNzoP0gdrhl9vH1TRdwoevuTsSqjitXnAoMSSTlCgI="; - }; - in + packages.default = + (pkgs.callPackage ( + { + buildBazelPackage, + stdenv, + fetchFromGitHub, + lib, + nix-gitignore, + }: + let + system = stdenv.hostPlatform.system; + registry = fetchFromGitHub { + owner = "bazelbuild"; + repo = "bazel-central-registry"; + rev = "63f3af762b2fdd7acaa7987856cd3ac314eaea09"; + hash = "sha256-ugNzoP0gdrhl9vH1TRdwoevuTsSqjitXnAoMSSTlCgI="; + }; + in buildBazelPackage { pname = "fpga-as"; version = "0.0.1"; - src = nix-gitignore.gitignoreSourcePure [] ./.; + src = nix-gitignore.gitignoreSourcePure [ ] ./.; bazelFlags = [ "--registry" @@ -99,7 +109,7 @@ aarch64-linux = "sha256-E4VHjDa0qkHmKUNpTBfJi7dhMLcd1z5he+p31/XvUl8="; x86_64-linux = "sha256-hVBJB0Hsd9sXuEoNcjhTkbPl89vlZT1w39JppCD+n8Y="; } - .${system} or (throw "No hash for system: ${system}"); + .${system} or (throw "No hash for system: ${system}"); }; removeRulesCC = false; @@ -133,15 +143,22 @@ platforms = lib.platforms.linux; }; } - ) {}).overrideAttrs (final: prev: { - # Fixup the deps so they always contain correrct - # shebangs paths pointing to the store. - deps = prev.deps.overrideAttrs (final: prev: { - installPhase = '' - patchShebangs $bazelOut/external - '' + prev.installPhase; - }); - }); + ) { }).overrideAttrs + ( + final: prev: { + # Fixup the deps so they always contain correrct + # shebangs paths pointing to the store. + deps = prev.deps.overrideAttrs ( + final: prev: { + installPhase = + '' + patchShebangs $bazelOut/external + '' + + prev.installPhase; + } + ); + } + ); } ); }