Add matcha_kelvin_verilog target

Generate the kelvin.v matches the format of the checked-in version in hw/matcha

Change-Id: I460b3e0733e32dbd70f75041a7772c75f492bc98
diff --git a/.bazelrc b/.bazelrc
index c4e0831..365b9e4 100644
--- a/.bazelrc
+++ b/.bazelrc
@@ -7,3 +7,8 @@
 build --host_copt=-Wno-unused-variable
 build --host_copt=-Wno-unused-value
 build --host_copt=-Wno-uninitialized
+
+# This lets us generate key/value pairs for the workspace which can be used to
+# generate the version information.
+
+build --workspace_status_command=utils/get_workspace_status.sh
diff --git a/README.md b/README.md
index 9c68e06..b697d08 100644
--- a/README.md
+++ b/README.md
@@ -20,5 +20,6 @@
 Verilog source for the Matcha SoC can be generated using:
 
 ```bash
-bazel build //hdl/chisel:kelvin_cc_library_emit_verilog
+bazel clean --expunge  # To generate the ToT sha
+bazel build //hdl/chisel:matcha_kelvin_verilog
 ```
diff --git a/hdl/chisel/BUILD b/hdl/chisel/BUILD
index d260a33..48e7360 100644
--- a/hdl/chisel/BUILD
+++ b/hdl/chisel/BUILD
@@ -145,3 +145,15 @@
     emit_class = "kelvin.EmitVSt",
     module_name = "VSt",
 )
+
+genrule(
+    name = "matcha_kelvin_verilog",
+    srcs = [":Kelvin.v"],
+    outs = ["kelvin.v"],
+    cmd = """
+    echo "// SHA: $$(awk '/KELVIN_BUILD_GIT_VERSION/ { print $$2 }' \
+      bazel-out/volatile-status.txt)" > $@
+    cat $< >> $@
+    """,
+    stamp = 1,  # this provides volatile-status.txt
+)
diff --git a/utils/get_workspace_status.sh b/utils/get_workspace_status.sh
new file mode 100755
index 0000000..730757e
--- /dev/null
+++ b/utils/get_workspace_status.sh
@@ -0,0 +1,18 @@
+#!/bin/bash
+# Copyright 2023 Google LLC
+#
+# This script will be run by bazel when the build process wants to generate
+# information about the status of the workspace.
+#
+# The output will be key-value pairs in the form:
+# KEY1 VALUE1
+#
+# If this script exits with a non-zero exit code, it's considered as a failure
+# and the output will be discarded.
+
+git_rev=$(git rev-parse HEAD)
+if [[ $? != 0 ]];
+then
+  exit 1
+fi
+echo "KELVIN_BUILD_GIT_VERSION ${git_rev}"