From 6af2feed620c22b31f8f3878b5cce9a5fb20ea1a Mon Sep 17 00:00:00 2001 From: Chad Reynolds Date: Thu, 30 Jan 2025 17:17:49 -0800 Subject: [PATCH] Fix the genrule for `cvd version` output The build runs in a sandbox and not in the working directory the `bazel` command is executed in, so the previous version never had an output to populate the `cvd version` field. Also, `git describe` was returning an old tag. I updated to return the latest HEAD commit hash instead. If the `--working_status_command=path/to/tools/buildutils/stamp_helper.sh` flag is added to a build, the output goes into a file intended for embedding outside information for use inside the build sandbox. The commit hash should not change "often" (like a timestamp would be different every single build), so we mark it as a stable key. Then, if the value changes, every affected action (marked `stamp = 1`) is triggered. Volatile keys do not trigger actions when they change. --- base/cvd/BUILD.bazel | 3 ++- base/debian/rules | 3 ++- tools/buildutils/stamp_helper.sh | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100755 tools/buildutils/stamp_helper.sh diff --git a/base/cvd/BUILD.bazel b/base/cvd/BUILD.bazel index e7a73f43db..f0bc577c1a 100644 --- a/base/cvd/BUILD.bazel +++ b/base/cvd/BUILD.bazel @@ -13,7 +13,8 @@ genrule( name = "build_version_header", srcs = ["build_version.h.in"], outs = ["build/version.h"], - cmd = "sed -e \"s|@VCS_TAG@|`git describe`|\" $< > $@", + cmd = "sed -e \"s|@VCS_TAG@|`cat bazel-out/*-status.txt | grep \"STABLE_HEAD_COMMIT\" | cut -f 2 -d ' '`|\" $< > $@", + stamp = 1, ) filegroup( diff --git a/base/debian/rules b/base/debian/rules index 6742736384..a14f501686 100755 --- a/base/debian/rules +++ b/base/debian/rules @@ -20,7 +20,8 @@ override_dh_installinit: dh_installinit --name=cuttlefish-host-resources dh_installinit +# the `--workspace_status_command` flag path depends on the current working directory of base/cvd override_dh_auto_build: - cd cvd && bazel build --linkopt="-Wl,--build-id=sha1" cuttlefish/host/commands/cvd:cvd --spawn_strategy=local + cd cvd && bazel build --linkopt="-Wl,--build-id=sha1" cuttlefish/host/commands/cvd:cvd --spawn_strategy=local --workspace_status_command=../../tools/buildutils/stamp_helper.sh dh_auto_build diff --git a/tools/buildutils/stamp_helper.sh b/tools/buildutils/stamp_helper.sh new file mode 100755 index 0000000000..ec9e54cec2 --- /dev/null +++ b/tools/buildutils/stamp_helper.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash +# +# https://bazel.build/docs/user-manual#workspace-status +# +# append `--workspace_status_command=path/to/stamp_helper.sh` to build command +# to "stamp" this output to `bazel-out/stable_status.txt`` +# +# NOTES: +# - output must be a "key value" format on a single line +# - output key must begin with "STABLE_" or the key and value end up in +# `volatile_status.txt` +# - changes to `stable_status.txt` values triggers a re-run of the affected +# actions + +echo STABLE_HEAD_COMMIT `git rev-parse HEAD` +