From d4fc2f059963b6464ad09e9c9faa7c070514da2c Mon Sep 17 00:00:00 2001
From: Toon Claes <toon@gitlab.com>
Date: Fri, 25 Mar 2022 14:29:22 +0100
Subject: [PATCH] Makefile: Simplify build target

When building Gitaly binaries there are two ways to do this, with or
without build-id. How this was implemented was hard to read.

This change streamlines how Gitaly binaries are built when not using
build-ids. This makes compilation a lot faster when there are no changes
made.
---
 Makefile | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/Makefile b/Makefile
index 9183f19dbd..9b3db169d2 100644
--- a/Makefile
+++ b/Makefile
@@ -256,6 +256,7 @@ BENCHMARK_REPO   := ${TEST_REPO_DIR}/benchmark.git
 
 # Find all Gitaly commands.
 gitaly_commands      := $(notdir $(shell find ${SOURCE_DIR}/cmd -mindepth 1 -maxdepth 1 -type d -print))
+gitaly_binaries      := $(addprefix ${BUILD_DIR}/bin/, $(gitaly_commands))
 # Find all binaries, including Gitaly's and Git's
 find_command_binaries = $(wildcard ${BUILD_DIR}/bin/*)
 # Find all Go source files.
@@ -316,15 +317,12 @@ help:
 ## Build Go binaries and install required Ruby Gems.
 build: ${SOURCE_DIR}/.ruby-bundle libgit2
 
+$(gitaly_binaries): $(find_go_sources)
 ifdef WITHOUT_BUILD_ID
+	@ # Build all commands at once, which is a lot faster than one-by-one
 	go install -ldflags '${GO_LDFLAGS}' -tags "${GO_BUILD_TAGS}" $(addprefix ${GITALY_PACKAGE}/cmd/, $(gitaly_commands))
-endif
-
-ifndef WITHOUT_BUILD_ID
-build: $(gitaly_commands)
-
-.PHONY: $(gitaly_commands)
-$(gitaly_commands):
+else
+	@ # Because each command will get a unique build-id, build each command separately
 	${Q}go install -ldflags '${GO_LDFLAGS}' -tags "${GO_BUILD_TAGS}" $(addprefix ${GITALY_PACKAGE}/cmd/, $@)
 	@ # To compute a unique and deterministic value for GNU build-id, we build the Go binary a second time.
 	@ # From the first build, we extract its unique and deterministic Go build-id, and use that to derive
@@ -335,6 +333,9 @@ $(gitaly_commands):
 	GO_BUILD_ID=$$( go tool buildid $(addprefix ${BUILD_DIR}/bin/, $@) || openssl rand -hex 32 ) && \
 	GNU_BUILD_ID=$$( echo $$GO_BUILD_ID | sha1sum | cut -d' ' -f1 ) && \
 	go install -ldflags '${GO_LDFLAGS}'" -B 0x$$GNU_BUILD_ID" -tags "${GO_BUILD_TAGS}" $(addprefix ${GITALY_PACKAGE}/cmd/, $@)
+
+# Ensure binaries get a new build-id on every run of `make build`
+.PHONY: $(gitaly_binaries)
 endif
 
 .PHONY: install
-- 
GitLab