From 074f4677d52ff9dc751b0b28dd68c8cc6fd0eca3 Mon Sep 17 00:00:00 2001 From: Jonathan Gold Date: Fri, 11 May 2018 14:20:52 -0400 Subject: [PATCH] build: Fix ldflags pattern for 1.10 Prior to go 1.10 ldflags would apply to all packages by default. As of go 1.10 it is necessary to specify the package for the flags to apply. This patch checks the go version, and formats the build command accordingly. --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4691108c..74104797 100644 --- a/Makefile +++ b/Makefile @@ -25,6 +25,7 @@ GO_FILES := $(shell find * -name '*.go' -not -path 'old/*' -not -path 'tmp/*') SVERSION := $(or $(SVERSION),$(shell git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --dirty --always)) VERSION := $(or $(VERSION),$(shell git describe --match '[0-9]*\.[0-9]*\.[0-9]*' --tags --abbrev=0)) PROGRAM := $(shell echo $(notdir $(CURDIR)) | cut -f1 -d"-") +PKGNAME := $(shell go list .) ifeq ($(VERSION),$(SVERSION)) RELEASE = 1 else @@ -142,7 +143,12 @@ GOARCH=$(lastword $(subst -, ,$*)) build/mgmt-%: $(GO_FILES) | bindata lang @echo "Building: $(PROGRAM), os/arch: $*, version: $(SVERSION)..." @# reassigning GOOS and GOARCH to make build command copy/pastable - time env GOOS=${GOOS} GOARCH=${GOARCH} go build -i -ldflags "-X main.program=$(PROGRAM) -X main.version=$(SVERSION) ${LDFLAGS}" -o $@ $(BUILD_FLAGS); + @# go 1.10 requires specifying the package for ldflags + @if go version | grep -qE 'go1.9'; then \ + time env GOOS=${GOOS} GOARCH=${GOARCH} go build -i -ldflags "-X main.program=$(PROGRAM) -X main.version=$(SVERSION) ${LDFLAGS}" -o $@ $(BUILD_FLAGS); \ + else \ + time env GOOS=${GOOS} GOARCH=${GOARCH} go build -i -ldflags=$(PKGNAME)="-X main.program=$(PROGRAM) -X main.version=$(SVERSION) ${LDFLAGS}" -o $@ $(BUILD_FLAGS); \ + fi # create a list of binary file names to use as make targets crossbuild_targets = $(addprefix build/mgmt-,$(subst /,-,${GOOSARCHES}))