Skip to content

Commit

Permalink
Fix the genrule for cvd version output
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cjreynol committed Jan 31, 2025
1 parent 4ae6404 commit 6af2fee
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion base/cvd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion base/debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -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

16 changes: 16 additions & 0 deletions tools/buildutils/stamp_helper.sh
Original file line number Diff line number Diff line change
@@ -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`

0 comments on commit 6af2fee

Please sign in to comment.