Skip to content

Makefile: Fix rebuilding Go binaries in place to add GNU build ID

Back when we added support for GNU build IDs to our binaries we started building our Go binaries twice: the first time we do it so that we can derive a deterministic GNU build ID from the Go build ID, and the second time to embed that derived GNU build ID into the final binary. This has two problems:

1. We build the binary twice, and even though Go caches most of the
   build process this still significantly slows down incremental
   builds of our binaries.

2. We're rebuilding the binary in-place by overwriting the binary
   with no GNU build ID with the one that contains the GNU build ID.
   While I'm not a 100% sure, this seems to leads to issues from
   time to time where the resulting Go binary may be invalid when
   the build got cancelled at the wrong point in time. This then
   broke subsequent rebuilds of the binary.

Ideally, we wouldn't have to care about generating a deterministic GNU build ID at all. But unfortunately, the only part of Go's build infra that supports them is go build, so we have no easy way to avoid the rebuild.

Instead, we can use a very ugly workaround though: when building the binary, we embed a fixed GNU build ID with a known string and put this binary into an intermediate location. We now derive the GNU build ID from that intermediate binary, but instead of rebuilding it we simply replace the known GNU build ID with the derived GNU build ID. Like this we don't have to rebuild the binary but still get the same end result as before.

Edited by Patrick Steinhardt

Merge request reports

Loading