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).
This commit is contained in:
Jamie Strandboge
2021-09-14 10:35:45 -05:00
committed by GitHub
parent 3895d1ac7b
commit 2d1f3ee3bc

View File

@ -12,7 +12,15 @@ endif
ifdef COMMIT ifdef COMMIT
LDFLAGS += -X main.commit=$(COMMIT) LDFLAGS += -X main.commit=$(COMMIT)
endif 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 are the files that affect building the main binary.
SOURCES := $(shell find . -name '*.go' -not -name '*_test.go') go.mod go.sum SOURCES := $(shell find . -name '*.go' -not -name '*_test.go') go.mod go.sum