build: fix static builds

restructure the build tags and installsuffix usage for golang
to build our projects correctly for static and dynamic builds.
This commit is contained in:
Bassam Tabbara committed 2016-11-16 14:11:29 -08:00
1 parent b10990d197
commit 0216acaa84
4 files changed
+55 -31

No files matched your search

+12 -4
View File
@@ -37,14 +37,13 @@ TAGS += dynamic
endif
# build a position independent executable. This implies dynamic linking
# since statically-linked PIE is not supported by the linker/glibc
# since statically-linked PIE is not supported by the linker/glibc. PIE
# is only supported on Linux.
PIE ?= 0
ifeq ($(PIE),1)
ifeq ($(STATIC),1)
$(error PIE only supported with dynamic linking. Set STATIC=0.)
endif
BUILDFLAGS += -buildmode=pie
TAGS += pie
endif
# if DEBUG is set to 1 debug information is perserved (i.e. not stripped).
@@ -118,7 +117,6 @@ CEPHD_PLATFORM = $(GOOS)_$(GOARCH)
# to force go to rebuild cephd
CEPHD_TOUCH_ON_BUILD = pkg/cephmgr/cephd/dummy.cc
GO_CGO_PACKAGES=$(GO_PROJECT)/cmd/rookd
CGO_LDFLAGS = -L$(abspath $(CEPHD_BUILD_DIR)/$(CEPHD_PLATFORM)/lib)
CGO_PREREQS = cephd.build
@@ -136,8 +134,18 @@ GO_BIN_DIR = $(BIN_DIR)
ifeq ($(STATIC),1)
GO_STATIC_PACKAGES=$(GO_PROJECT)
ifeq ($(ROOKD_SUPPORTED),1)
GO_STATIC_CGO_PACKAGES=$(GO_PROJECT)/cmd/rookd
endif
else
GO_NONSTATIC_PACKAGES=$(GO_PROJECT)
ifeq ($(ROOKD_SUPPORTED),1)
ifeq ($(PIE),1)
GO_NONSTATIC_PIE_PACKAGES+=$(GO_PROJECT)/cmd/rookd
else
GO_NONSTATIC_PACKAGES+= $(GO_PROJECT)/cmd/rookd
endif
endif
endif
GO_BUILDFLAGS=$(BUILDFLAGS)
+15 -4
View File
@@ -161,15 +161,26 @@ RUN GO_VERSION=1.7.3 && \
ENV PATH /usr/local/go/bin:$PATH
# precompile the go standard library for all supported platforms and configurations
# there are three configurations that we compile for:
# - default: CGO_ENABLED=1, defaults tags
# - netgo: CGO_ENABLED=1, enable pure go DNS resolver
# - nocgo: CGO_ENABLED=0, defaults tags, pure Go resolver is the default
RUN \
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC=o64-clang go install -a std && \
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 CC=o64-clang go install -tags netgo -installsuffix static -a std && \
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 CC=o64-clang go install -installsuffix netgo -tags netgo -a std && \
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 CC=o64-clang go install -installsuffix nocgo -a std && \
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go install -a std && \
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go install -tags netgo -installsuffix static -a std &&\
CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go install -installsuffix netgo -tags netgo -a std && \
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go install -installsuffix nocgo -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go install -a std && \
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go install -tags netgo -installsuffix static -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go install -buildmode pie -installsuffix pie -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go install -installsuffix netgo -tags netgo -a std && \
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 CC=aarch64-linux-gnu-gcc go install -installsuffix nocgo -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -a std && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -tags netgo -installsuffix static -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -buildmode pie -installsuffix pie -a std && \
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -installsuffix netgo -tags netgo -a std && \
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -installsuffix nocgo -a std && \
\
CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-gnu-gcc go install -installsuffix race -a std
WORKDIR /build
+1 -1
View File
@@ -1 +1 @@
0.1.9
0.1.10
+27 -22
View File
@@ -20,21 +20,24 @@ ifeq ($(GO_PROJECT),)
$(error the variable $$GO_PROJECT must be set prior to including golang.mk)
endif
# These target do not use cgo ir SWIG and contain all pure go code. They will be
# statically or dynamically linked depending on whether they import the standard
# net, os/user packages.
# These targets will statically or dynamically linked depending on whether they
# import the standard net, os/user packages.
GO_NONSTATIC_PACKAGES ?=
# These targets do not use cgo ir SWIG and contain all pure go code. They will
# These targets will statically or dynamically linked depending on whether they
# import the standard net, os/user packages. Buildmode PIE will be enabled.
GO_NONSTATIC_PIE_PACKAGES ?=
# These targets do not use cgo or SWIG and contain all pure go code. They will
# be forced to link statically even if they import the net or os/user packages.
GO_STATIC_PACKAGES ?=
# These targets are a mix of go and non-go code. They will be linked statically
# or dynamically based on the linker flags passed through ldflags.
GO_CGO_PACKAGES ?=
GO_STATIC_CGO_PACKAGES ?=
ifeq ($(GO_NONSTATIC_PACKAGES)$(GO_STATIC_PACKAGES)$(GO_CGO_PACKAGES),)
$(error please set GO_STATIC_PACKAGES, GO_NONSTATIC_PACKAGES, and/or GO_CGO_PACKAGES prior to including golang.mk)
ifeq ($(GO_NONSTATIC_PACKAGES)$(GO_NONSTATIC_PIE_PACKAGES)$(GO_STATIC_PACKAGES)$(GO_STATIC_CGO_PACKAGES),)
$(error please set GO_NONSTATIC_PACKAGES, GO_NONSTATIC_PIE_PACKAGES, GO_STATIC_PACKAGES and/or GO_STATIC_CGO_PACKAGES prior to including golang.mk)
endif
# Optional. These are sudirs that we look for all go files to test, vet, and fmt
@@ -88,18 +91,18 @@ GOHOST := GOOS=$(GOHOSTOS) GOARCH=$(GOHOSTARCH) go
GO_OUT_DIR := $(abspath $(GO_BIN_DIR)/$(GOOS)_$(GOARCH))
ifeq ($(GOOS),windows)
GO_OUT_EXT := ".exe"
GO_OUT_EXT := .exe
endif
ifneq ($(GO_PKG_DIR),)
GO_PKG_FLAGS := -pkgdir $(abspath $(GO_PKG_DIR)/$(GOOS)_$(GOARCH))
GO_PKG_STATIC_FLAGS := -pkgdir $(abspath $(GO_PKG_DIR)/$(GOOS)_$(GOARCH)_static) -installsuffix static
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"'
# these configurations are matched with the ones in build/cross-image/Dockerfile
GO_NONSTATIC_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)'
GO_NONSTATIC_PIE_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix pie -buildmode pie -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)'
GO_STATIC_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix nocgo -tags '$(GO_TAGS)' -ldflags '$(GO_LDFLAGS)'
GO_STATIC_CGO_FLAGS = $(GO_BUILDFLAGS) $(GO_PKG_FLAGS) -installsuffix netgo -tags '$(GO_TAGS) netgo' -ldflags '$(GO_LDFLAGS) -s -extldflags "-static"'
# ====================================================================================
# Targets
@@ -124,25 +127,27 @@ endif
.PHONY: go.build
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_STATIC_TOOL_FLAGS) $(p))
@$(foreach p,$(GO_NONSTATIC_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_NONSTATIC_FLAGS) $(p);)
@$(foreach p,$(GO_NONSTATIC_PIE_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_NONSTATIC_PIE_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_PACKAGES),CGO_ENABLED=0 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES),CGO_ENABLED=1 $(GO) build -v -i -o $(GO_OUT_DIR)/$(lastword $(subst /, ,$(p)))$(GO_OUT_EXT) $(GO_STATIC_CGO_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_STATIC_TOOL_FLAGS) $(p))
@$(foreach p,$(GO_NONSTATIC_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_NONSTATIC_FLAGS) $(p);)
@$(foreach p,$(GO_NONSTATIC_PIE_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_NONSTATIC_PIE_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_PACKAGES),CGO_ENABLED=0 $(GO) install -v $(GO_STATIC_FLAGS) $(p);)
@$(foreach p,$(GO_STATIC_CGO_PACKAGES),CGO_ENABLED=1 $(GO) install -v $(GO_STATIC_CGO_FLAGS) $(p);)
.PHONY: go.test
go.test: go.vet go.fmt $(CGO_PREREQS)
# this is disabled since it looks like there's a bug in go test where it attempts to install cmd/cgo
# @$(GOHOST) test -v -i $(GO_PKG_FLAGS) $(GO_TOOL_FLAGS) $(GO_ALL_PACKAGES)
@$(GOHOST) test -cover $(GO_PKG_FLAGS) $(GO_TOOL_FLAGS) $(GO_ALL_PACKAGES)
# @$(GOHOST) test -v -i $(GO_PKG_FLAGS) $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
@$(GOHOST) test -cover $(GO_PKG_FLAGS) $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
.PHONY: go.vet
go.vet:
@$(GOHOST) vet $(GO_TOOL_FLAGS) $(GO_ALL_PACKAGES)
@$(GOHOST) vet $(GO_NONSTATIC_FLAGS) $(GO_ALL_PACKAGES)
.PHONY: go.fmt
go.fmt: