Skip to content

Commit 17a8dc7

Browse files
committed
add a better variable override mechanism
The override mechanism proposed by @stanislav-zaprudskiy in #352 is problematic because --warn-undefined-variables makes noise about it: ```shellSession $ make Makefile:67: warning: undefined variable 'GO_BUILDFLAGS' Makefile:68: warning: undefined variable 'GO_LDFLAGS' Makefile:69: warning: undefined variable 'GO_TESTFLAGS' Makefile:70: warning: undefined variable 'GO_TESTENV' Makefile:71: warning: undefined variable 'GO_BUILDENV' env go build -mod vendor -ldflags '-s -w ... ``` However, through much experimentation, I discovered that the += operator behaves in the intended way, as documented in the new generated comment.
1 parent 7934c38 commit 17a8dc7

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

Makefile

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,12 @@ install-reuse: FORCE
5050

5151
prepare-static-check: FORCE install-golangci-lint install-modernize install-shellcheck install-go-licence-detector install-addlicense install-reuse
5252

53-
GO_BUILDFLAGS = -mod vendor
54-
GO_LDFLAGS =
55-
GO_TESTENV =
56-
GO_BUILDENV =
53+
# To add additional flags or values, specify the variable in the environment, e.g. `GO_BUILDFLAGS='-tags experimental' make`.
54+
# To override the default flags or values, specify the variable on the command line, e.g. `make GO_BUILDFLAGS='-tags experimental'`.
55+
GO_BUILDFLAGS += -mod vendor
56+
GO_LDFLAGS +=
57+
GO_TESTENV +=
58+
GO_BUILDENV +=
5759

5860
# These definitions are overridable, e.g. to provide fixed version/commit values when
5961
# no .git directory is present or to provide a fixed build date for reproducibility.

internal/makefile/makefile.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,12 @@ endif
211211
}
212212

213213
if isGolang {
214-
build.addDefinition("GO_BUILDFLAGS =%s", cfg.Variable("GO_BUILDFLAGS", defaultBuildFlags))
215-
build.addDefinition("GO_LDFLAGS =%s", cfg.Variable("GO_LDFLAGS", strings.TrimSpace(defaultLdFlags)))
216-
build.addDefinition("GO_TESTENV =%s", cfg.Variable("GO_TESTENV", ""))
217-
build.addDefinition("GO_BUILDENV =%s", cfg.Variable("GO_BUILDENV", ""))
214+
build.addDefinition("# To add additional flags or values, specify the variable in the environment, e.g. `GO_BUILDFLAGS='-tags experimental' make`.")
215+
build.addDefinition("# To override the default flags or values, specify the variable on the command line, e.g. `make GO_BUILDFLAGS='-tags experimental'`.")
216+
build.addDefinition("GO_BUILDFLAGS +=%s", cfg.Variable("GO_BUILDFLAGS", defaultBuildFlags))
217+
build.addDefinition("GO_LDFLAGS +=%s", cfg.Variable("GO_LDFLAGS", strings.TrimSpace(defaultLdFlags)))
218+
build.addDefinition("GO_TESTENV +=%s", cfg.Variable("GO_TESTENV", ""))
219+
build.addDefinition("GO_BUILDENV +=%s", cfg.Variable("GO_BUILDENV", ""))
218220
}
219221
if sr.HasBinInfo {
220222
build.addDefinition("")

0 commit comments

Comments
 (0)