From 0aac00ff792db9ebe1208e379b767e2d200c5bd4 Mon Sep 17 00:00:00 2001 From: Bassam Tabbara Date: Mon, 14 Nov 2016 11:31:35 -0800 Subject: [PATCH] build: use netgo build tag for static builds pass netgo build tag just for static builds. this required a refactoring of build flags to avoid passing netgo to non-static packages. Fixes #80 --- Makefile | 6 ++++-- build/makelib/golang.mk | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 8265c6e2b..81eea2544 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,6 @@ TAGS ?= # a dynamic binary is produced that requires glibc to be installed STATIC ?= 1 ifeq ($(STATIC),1) -LDFLAGS += -s -extldflags "-static" TAGS += static else TAGS += dynamic @@ -141,7 +140,10 @@ else GO_NONSTATIC_PACKAGES=$(GO_PROJECT) endif -GO_TOOL_FLAGS=$(BUILDFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' +GO_BUILDFLAGS=$(BUILDFLAGS) +GO_LDFLAGS=$(LDFLAGS) +GO_TAGS=$(TAGS) + GO_PKG_DIR ?= $(WORKDIR)/pkg include build/makelib/golang.mk diff --git a/build/makelib/golang.mk b/build/makelib/golang.mk index d6e27c0ae..00ee89c62 100644 --- a/build/makelib/golang.mk +++ b/build/makelib/golang.mk @@ -47,7 +47,9 @@ GO_VENDOR_DIR ?= vendor GO_PKG_DIR ?= # Optional build flags passed to go tools -GO_TOOL_FLAGS ?= +GO_BUILDFLAGS ?= +GO_LDFLAGS ?= +GO_TAGS ?= # Optional CGO flags directories CGO_CFLAGS ?= @@ -96,6 +98,9 @@ else GO_PKG_STATIC_FLAGS := -installsuffix static endif +GO_TOOL_FLAGS=$(GO_BUILDFLAGS) -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)' +GO_STATIC_TOOL_FLAGS=$(GO_BUILDFLAGS) -tags '$(GO_TAGS) netgo' -ldflags '$(GO_LDFLAGS) -s -extldflags "-static"' + # ==================================================================================== # Targets @@ -121,13 +126,13 @@ endif go.build: go.vet go.fmt $(CGO_PREREQS) $(foreach p,$(GO_NONSTATIC_PACKAGES),@CGO_ENABLED=1 $(GO) build -v -i $(GO_PKG_FLAGS) -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_TOOL_FLAGS) $(p)) $(foreach p,$(GO_CGO_PACKAGES),@CGO_ENABLED=1 $(GO) build -v -i $(GO_PKG_FLAGS) -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_TOOL_FLAGS) $(p)) - $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) build -v -i $(GO_PKG_STATIC_FLAGS) -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_TOOL_FLAGS) $(p)) + $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) build -v -i $(GO_PKG_STATIC_FLAGS) -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_TOOL_FLAGS) $(p)) .PHONY: go.install go.install: go.vet go.fmt $(CGO_PREREQS) $(foreach p,$(GO_NONSTATIC_PACKAGES),@CGO_ENABLED=1 $(GO) install -v $(GO_PKG_FLAGS) $(GO_TOOL_FLAGS) $(p)) $(foreach p,$(GO_CGO_PACKAGES),@CGO_ENABLED=1 $(GO) install -v $(GO_PKG_FLAGS) $(GO_TOOL_FLAGS) $(p)) - $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) install -v $(GO_PKG_STATIC_FLAGS) $(GO_TOOL_FLAGS) $(p)) + $(foreach p,$(GO_STATIC_PACKAGES),@CGO_ENABLED=0 $(GO) install -v $(GO_PKG_STATIC_FLAGS) $(GO_STATIC_TOOL_FLAGS) $(p)) .PHONY: go.test go.test: go.vet go.fmt $(CGO_PREREQS)