From a323a8d1fd0921136cbe6266c48fe74cc86f18bd Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 15 Mar 2021 15:41:42 +0100 Subject: [PATCH 1/8] allow static builds via make STATIC=true --- Makefile | 25 ++++++++++++++++--------- README.md | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index ada6f14..440fab8 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,6 @@ endif GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/bindata.go") GOFMT ?= gofmt -s -GOFLAGS := -i -v -EXTRA_GOFLAGS ?= - MAKE_VERSION := $(shell make -v | head -n 1) ifneq ($(DRONE_TAG),) @@ -38,13 +35,22 @@ else TEA_VERSION ?= $(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//') endif +TAGS ?= +TAGS_STATIC := osusergo,netgo,static_build,$(TAGS) + LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" +LDFLAGS_STATIC := $(LDFLAGS) -extldflags "-fno-PIC -static" -s -w + +GOFLAGS := -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' +# TODO: clean this mess up when there are news on https://github.com/golang/go/issues/26492 +GOFLAGS_STATIC_GOX := $(GOFLAGS) -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC) -s -w' +ifeq ($(STATIC),true) + GOFLAGS := $(GOFLAGS_STATIC_GOX) -buildmode=pie +endif PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/) SOURCES ?= $(shell find . -name "*.go" -type f) -TAGS ?= - ifeq ($(OS), Windows_NT) EXECUTABLE := tea.exe else @@ -130,14 +136,15 @@ test-vendor: vendor check: test .PHONY: install -install: $(wildcard *.go) - $(GO) install -mod=vendor -v -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' +install: $(SOURCES) + @echo "installing to $(GOPATH)/bin/$(EXECUTABLE)" + $(GO) install -v $(GOFLAGS) .PHONY: build build: $(EXECUTABLE) $(EXECUTABLE): $(SOURCES) - $(GO) build -mod=vendor $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@ + $(GO) build -v $(GOFLAGS) -o $@ .PHONY: release release: release-dirs release-os release-compress release-check @@ -151,7 +158,7 @@ release-os: @hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ cd /tmp && $(GO) get -u github.com/mitchellh/gox; \ fi - CGO_ENABLED=0 gox -verbose -cgo=false -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" + CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS_STATIC_GOX) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" .PHONY: release-compress release-compress: diff --git a/README.md b/README.md index cd1665c..d5e4c8b 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ To compile the sources yourself run the following: ```sh git clone https://gitea.com/gitea/tea.git cd tea -make +make STATIC=true ``` ## Contributing -- 2.40.1 From 6131decbe29c114a78396c2e33f2722c8d1ee59d Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Tue, 16 Mar 2021 12:48:39 +0100 Subject: [PATCH 2/8] cleanup makefile remove unused / duplicate variables --- Makefile | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Makefile b/Makefile index 440fab8..c42963d 100644 --- a/Makefile +++ b/Makefile @@ -1,28 +1,14 @@ DIST := dist -IMPORT := code.gitea.io/tea export GO111MODULE=on GO ?= go -SED_INPLACE := sed -i SHASUM ?= shasum -a 256 export PATH := $($(GO) env GOPATH)/bin:$(PATH) -ifeq ($(OS), Windows_NT) - EXECUTABLE := tea.exe -else - EXECUTABLE := tea - UNAME_S := $(shell uname -s) - ifeq ($(UNAME_S),Darwin) - SED_INPLACE := sed -i '' - endif -endif - GOFILES := $(shell find . -name "*.go" -type f ! -path "./vendor/*" ! -path "*/bindata.go") GOFMT ?= gofmt -s -MAKE_VERSION := $(shell make -v | head -n 1) - ifneq ($(DRONE_TAG),) VERSION ?= $(subst v,,$(DRONE_TAG)) TEA_VERSION ?= $(VERSION) @@ -57,9 +43,6 @@ else EXECUTABLE := tea endif -# $(call strip-suffix,filename) -strip-suffix = $(firstword $(subst ., ,$(1))) - .PHONY: all all: build -- 2.40.1 From 6c219abc6c3c86fe0ee37cd21f812ff06032f325 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Wed, 17 Mar 2021 12:53:21 +0100 Subject: [PATCH 3/8] make: fix STATIC=true - -buildmode=pie silently creates dynamically linked executables - also was passing the wrong tags to main.Tags --- Makefile | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index c42963d..4e2fef2 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ DIST := dist export GO111MODULE=on +export CGO_ENABLED=0 GO ?= go SHASUM ?= shasum -a 256 @@ -24,14 +25,17 @@ endif TAGS ?= TAGS_STATIC := osusergo,netgo,static_build,$(TAGS) -LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -LDFLAGS_STATIC := $(LDFLAGS) -extldflags "-fno-PIC -static" -s -w - +LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w GOFLAGS := -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' + # TODO: clean this mess up when there are news on https://github.com/golang/go/issues/26492 -GOFLAGS_STATIC_GOX := $(GOFLAGS) -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC) -s -w' +# note: -buildmode=pie is incompatible with static builds as of go 1.16! +LDFLAGS_STATIC := $(LDFLAGS) -extldflags "-fno-PIC -static" -X "main.Tags=$(TAGS_STATIC)" +GOFLAGS_STATIC := $(GOFLAGS) -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC)' ifeq ($(STATIC),true) - GOFLAGS := $(GOFLAGS_STATIC_GOX) -buildmode=pie + GOFLAGS := $(GOFLAGS_STATIC) -a +else + GOFLAGS := $(GOFLAGS) -buildmode=pie endif PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/) @@ -141,7 +145,7 @@ release-os: @hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ cd /tmp && $(GO) get -u github.com/mitchellh/gox; \ fi - CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS_STATIC_GOX) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" + CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS_STATIC) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" .PHONY: release-compress release-compress: -- 2.40.1 From d71473cdd4f397d37da40593897ad2d171e9c3a0 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 10 May 2021 22:21:20 +0200 Subject: [PATCH 4/8] produce PIE with make STATIC=true this is documented nowhere, but I finally found a way to have both of - static builds - position independent executables exotic flags of arcane build tools ftw! --- Makefile | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index aec9a3c..0e7cf6d 100644 --- a/Makefile +++ b/Makefile @@ -28,16 +28,14 @@ TAGS ?= TAGS_STATIC := osusergo,netgo,static_build,$(TAGS) LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w -GOFLAGS := -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' +GOFLAGS := -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -buildmode=pie -# TODO: clean this mess up when there are news on https://github.com/golang/go/issues/26492 -# note: -buildmode=pie is incompatible with static builds as of go 1.16! -LDFLAGS_STATIC := $(LDFLAGS) -extldflags "-fno-PIC -static" -X "main.Tags=$(TAGS_STATIC)" -GOFLAGS_STATIC := $(GOFLAGS) -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC)' +# TODO: clean up this mess, when there is news on https://github.com/golang/go/issues/26492 +LDFLAGS_STATIC := $(LDFLAGS) -linkmode=external -extldflags "-static-pie" -X "main.Tags=$(TAGS_STATIC)" +GOFLAGS_STATIC := -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC)' ifeq ($(STATIC),true) - GOFLAGS := $(GOFLAGS_STATIC) -a -else - GOFLAGS := $(GOFLAGS) -buildmode=pie + GOFLAGS := $(GOFLAGS_STATIC) + export CGO_ENABLED=1 endif PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/) @@ -133,7 +131,10 @@ install: $(SOURCES) build: $(EXECUTABLE) $(EXECUTABLE): $(SOURCES) - $(GO) build -v $(GOFLAGS) -o $@ +ifeq ($(STATIC),true) + @echo "enabling static build, make sure you have glibc-static (or equivalent) installed" +endif + $(GO) build $(GOFLAGS) -o $@ .PHONY: build-image build-image: -- 2.40.1 From ed0180fd49475f5615eda3abd7bad55c518b045c Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 10 May 2021 22:26:59 +0200 Subject: [PATCH 5/8] simplify makefile variables --- Makefile | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 0e7cf6d..62501c3 100644 --- a/Makefile +++ b/Makefile @@ -23,21 +23,19 @@ else endif TEA_VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(TEA_VERSION)) - TAGS ?= -TAGS_STATIC := osusergo,netgo,static_build,$(TAGS) - LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w -GOFLAGS := -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -buildmode=pie -# TODO: clean up this mess, when there is news on https://github.com/golang/go/issues/26492 -LDFLAGS_STATIC := $(LDFLAGS) -linkmode=external -extldflags "-static-pie" -X "main.Tags=$(TAGS_STATIC)" -GOFLAGS_STATIC := -tags '$(TAGS_STATIC)' -ldflags '$(LDFLAGS_STATIC)' ifeq ($(STATIC),true) - GOFLAGS := $(GOFLAGS_STATIC) - export CGO_ENABLED=1 + # NOTE: clean up this mess, when https://github.com/golang/go/issues/26492 is resolved + TAGS := osusergo,netgo,static_build,$(TAGS) + LDFLAGS := $(LDFLAGS) -linkmode=external -extldflags "-static-pie" -X "main.Tags=$(TAGS)" + export CGO_ENABLED=1 # needed for linkmode=external endif +# override to allow passing additional goflags via make CLI +override GOFLAGS := $(GOFLAGS) -mod=vendor -buildmode=pie -tags '$(TAGS)' -ldflags '$(LDFLAGS)' + PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/) SOURCES ?= $(shell find . -name "*.go" -type f) -- 2.40.1 From da5898c4aae6057e3d97c702cfa25e36a0282e6b Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 10 May 2021 22:37:10 +0200 Subject: [PATCH 6/8] update dockerfile to work with make STATIC=true and add a .dockerignore --- .dockerignore | 2 ++ Dockerfile | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d586e98 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +Dockerfile +tea diff --git a/Dockerfile b/Dockerfile index 695f52d..8d90551 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,16 +2,16 @@ ARG GOVERSION="1.16.2" FROM golang:${GOVERSION}-alpine AS buildenv -ARG CGO_ENABLED="0" ARG GOOS="linux" COPY . $GOPATH/src/ WORKDIR $GOPATH/src RUN apk add --quiet --no-cache \ + build-base \ make \ git && \ - make build + make clean build STATIC=true FROM scratch ARG VERSION="0.7.0" -- 2.40.1 From ec58ba91bfe5afe6f84d1778c80d821e5187ecc2 Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 10 May 2021 22:50:22 +0200 Subject: [PATCH 7/8] fix `make release` --- Makefile | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 62501c3..d56183f 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ ifeq ($(STATIC),true) endif # override to allow passing additional goflags via make CLI -override GOFLAGS := $(GOFLAGS) -mod=vendor -buildmode=pie -tags '$(TAGS)' -ldflags '$(LDFLAGS)' +override GOFLAGS := $(GOFLAGS) -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' PACKAGES ?= $(shell $(GO) list ./... | grep -v /vendor/) SOURCES ?= $(shell find . -name "*.go" -type f) @@ -123,7 +123,7 @@ check: test .PHONY: install install: $(SOURCES) @echo "installing to $(GOPATH)/bin/$(EXECUTABLE)" - $(GO) install -v $(GOFLAGS) + $(GO) install -v -buildmode=pie $(GOFLAGS) .PHONY: build build: $(EXECUTABLE) @@ -132,7 +132,7 @@ $(EXECUTABLE): $(SOURCES) ifeq ($(STATIC),true) @echo "enabling static build, make sure you have glibc-static (or equivalent) installed" endif - $(GO) build $(GOFLAGS) -o $@ + $(GO) build -buildmode=pie $(GOFLAGS) -o $@ .PHONY: build-image build-image: @@ -150,7 +150,7 @@ release-os: @hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ cd /tmp && $(GO) get -u github.com/mitchellh/gox; \ fi - CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS_STATIC) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" + CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" .PHONY: release-compress release-compress: -- 2.40.1 From b0521a73915821c9a8a99d8b4fdd6267d82b39bd Mon Sep 17 00:00:00 2001 From: Norwin Roosen Date: Mon, 10 May 2021 22:57:26 +0200 Subject: [PATCH 8/8] document static_build tag --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d56183f..5a1c971 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w ifeq ($(STATIC),true) # NOTE: clean up this mess, when https://github.com/golang/go/issues/26492 is resolved + # static_build is a defacto standard tag used in go packages TAGS := osusergo,netgo,static_build,$(TAGS) LDFLAGS := $(LDFLAGS) -linkmode=external -extldflags "-static-pie" -X "main.Tags=$(TAGS)" export CGO_ENABLED=1 # needed for linkmode=external @@ -150,7 +151,7 @@ release-os: @hash gox > /dev/null 2>&1; if [ $$? -ne 0 ]; then \ cd /tmp && $(GO) get -u github.com/mitchellh/gox; \ fi - CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm64 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" + CGO_ENABLED=0 gox -verbose -cgo=false $(GOFLAGS) -osarch='!darwin/386 !darwin/arm' -os="windows linux darwin" -arch="386 amd64 arm arm64" -output="$(DIST)/release/tea-$(VERSION)-{{.OS}}-{{.Arch}}" .PHONY: release-compress release-compress: -- 2.40.1