From 683227eed8f5fb52eddbabc9597e4dc56f19632d Mon Sep 17 00:00:00 2001
From: xinyangli <lixinyang411@gmail.com>
Date: Tue, 12 Mar 2024 19:10:16 +0800
Subject: [PATCH] circt: init at 1.43.0

---
 default.nix            |  3 +-
 flake.lock             |  6 ++--
 pkgs/circt/default.nix | 72 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 77 insertions(+), 4 deletions(-)
 create mode 100644 pkgs/circt/default.nix

diff --git a/default.nix b/default.nix
index 4789c05..24c13da 100644
--- a/default.nix
+++ b/default.nix
@@ -15,7 +15,8 @@ with pkgs;
   lib = import ./lib { inherit pkgs; }; # functions
   modules = import ./modules; # NixOS modules
   overlays = import ./overlays; # nixpkgs overlays
-
+  
+  circt143 = callPackage ./pkgs/circt { circtVersion = "v143"; };
   nvboard = callPackage ./pkgs/nvboard { };
   ieda = callPackage ./pkgs/ieda { };
   abstract-machine = callPackage ./pkgs/abstract-machine { };
diff --git a/flake.lock b/flake.lock
index 0e3d3ae..ace0286 100644
--- a/flake.lock
+++ b/flake.lock
@@ -2,11 +2,11 @@
   "nodes": {
     "nixpkgs": {
       "locked": {
-        "lastModified": 1704161960,
-        "narHash": "sha256-QGua89Pmq+FBAro8NriTuoO/wNaUtugt29/qqA8zeeM=",
+        "lastModified": 1710222005,
+        "narHash": "sha256-irXySffHz7b82dZIme6peyAu+8tTJr1zyxcfUPhqUrg=",
         "owner": "NixOS",
         "repo": "nixpkgs",
-        "rev": "63143ac2c9186be6d9da6035fa22620018c85932",
+        "rev": "9a9a7552431c4f1a3b2eee9398641babf7c30d0e",
         "type": "github"
       },
       "original": {
diff --git a/pkgs/circt/default.nix b/pkgs/circt/default.nix
new file mode 100644
index 0000000..220c93f
--- /dev/null
+++ b/pkgs/circt/default.nix
@@ -0,0 +1,72 @@
+{ stdenv
+, lib
+, fetchFromGitHub
+, cmake
+, coreutils
+, ninja
+, git
+, circtVersion
+, python3
+}:
+let
+  pythonEnv = python3.withPackages (ps: [ ps.psutil ]);
+  versionHash = {
+    v143 = "sha256-RkjigboswLkLgLkgOGahQLIygCkC3Q9rbVw3LqIzREY=";
+  };
+in
+stdenv.mkDerivation rec {
+  pname = "circt";
+  version = circtVersion;
+  src = fetchFromGitHub {
+    owner = "llvm";
+    repo = "circt";
+    rev = "firtool-${version}";
+    sha256 = versionHash.${circtVersion};
+    fetchSubmodules = true;
+  };
+
+  requiredSystemFeatures = [ "big-parallel" ];
+
+  nativeBuildInputs = [
+    cmake
+    ninja
+    git
+    pythonEnv
+  ];
+
+  cmakeDir = "../llvm/llvm";
+  cmakeFlags = [
+    "-DLLVM_ENABLE_BINDINGS=OFF"
+    "-DLLVM_ENABLE_OCAMLDOC=OFF"
+    "-DLLVM_BUILD_EXAMPLES=OFF"
+    "-DLLVM_OPTIMIZED_TABLEGEN=ON"
+    "-DLLVM_ENABLE_PROJECTS=mlir"
+    "-DLLVM_EXTERNAL_PROJECTS=circt"
+    "-DLLVM_EXTERNAL_CIRCT_SOURCE_DIR=.."
+    "-DCIRCT_LLHD_SIM_ENABLED=OFF"
+  ];
+
+  LIT_FILTER_OUT = if stdenv.cc.isClang then "CIRCT :: Target/ExportSystemC/.*\.mlir" else null;
+
+  preConfigure = ''
+    find ./test -name '*.mlir' -exec sed -i 's|/usr/bin/env|${coreutils}/bin/env|g' {} \;
+  '';
+
+  installPhase = ''
+    runHook preInstall
+    mkdir -p $out/bin
+    mv bin/{{fir,hls}tool,circt-{as,dis,lsp-server,opt,reduce,translate}} $out/bin
+    runHook postInstall
+  '';
+
+  doCheck = true;
+  checkTarget = "check-circt check-circt-integration";
+
+  meta = {
+    description = "Circuit IR compilers and tools";
+    homepage = "https://circt.org/";
+    license = lib.licenses.asl20;
+    maintainers = with lib.maintainers; [ sharzy ];
+    platforms = lib.platforms.all;
+  };
+}