From 2d1f3ee3bc7ab2443f14391fb315ee4520d271ab Mon Sep 17 00:00:00 2001 From: Jamie Strandboge Date: Tue, 14 Sep 2021 10:35:45 -0500 Subject: [PATCH] build: allow specifying -gcflags via env (#267) Make targets may call '$(GO_BUILD)', but there is no facility for specifying arguments to 'go build'. As a first step, introduce the GCFLAGS Makefile variable that when unset, operates as always, but when set, adds '-gcflags "$(GCFLAGS)"' to go build. Eg, when unspecified, maintain the current behavior (though with an additional space): $ make CGO_ENABLED=0 go build -ldflags ... When specified, add the specified -gcflags: $ GCFLAGS="all=-N -l" make CGO_ENABLED=0 go build -gcflags "all=-N -l" -ldflags ... This could be useful in various situations such as producing unoptimized builds (like in the above). --- Makefile | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2041772..bde32f6 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,15 @@ endif ifdef COMMIT LDFLAGS += -X main.commit=$(COMMIT) endif -export GO_BUILD=go build -ldflags "$(LDFLAGS)" + +# Use default flags, but allow adding -gcflags "..." if desired. Eg, for debug +# builds, may want to use GCFLAGS="all=-N -l" in the build environment. +GCFLAGS ?= +ifneq ($(GCFLAGS),) +GCFLAGS := -gcflags "$(GCFLAGS)" +endif + +export GO_BUILD=go build $(GCFLAGS) -ldflags "$(LDFLAGS)" # SOURCES are the files that affect building the main binary. SOURCES := $(shell find . -name '*.go' -not -name '*_test.go') go.mod go.sum