From 39e1523442fcbca12843280d11fe7e9b5b965247 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 31 Dec 2018 11:17:28 -0500 Subject: [PATCH 1/3] build: respect environment LDFLAGS and strip the build path golang does not natively respect LDFLAGS, but you can pass them on the command line using -ldflags=-extldflags=... This is important for distributions, in order to provide common functionality such as hardening flags. Also strip the prefixed root source directory from the embedded source file paths. This is not important information for the debugger, which should only care about paths relative to $GOPATH, and results in less build environment metadata leaking into the final binary. (This also aids in reproducible builds when using different build directories, see e.g. https://github.com/golang/go/issues/16860) --- Makefile | 3 +++ script/build | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 250cc92f3..3ee68dd34 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,9 @@ SOURCES = $(shell script/build files) SOURCE_DATE_EPOCH ?= $(shell date +%s) BUILD_DATE = $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" '+%d %b %Y' 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" '+%d %b %Y') HUB_VERSION = $(shell hub version | tail -1) +export LDFLAGS := -extldflags=$(LDFLAGS) +export GCFLAGS := all=-trimpath=$(PWD) +export ASMFLAGS := all=-trimpath=$(PWD) MIN_COVERAGE = 89.4 diff --git a/script/build b/script/build index 458bad0ed..09d1a7cbe 100755 --- a/script/build +++ b/script/build @@ -13,7 +13,11 @@ find_source_files() { build_hub() { mkdir -p "$(dirname "$1")" - go build -ldflags "-X github.com/github/hub/version.Version=`./script/version`" -o "$1" + go build \ + -ldflags "-X github.com/github/hub/version.Version=`./script/version` $LDFLAGS" \ + -gcflags "$GCFLAGS" \ + -asmflags "$ASMFLAGS" \ + -o "$1" } [ $# -gt 0 ] || set -- -o "bin/hub${windows:+.exe}" From d0541a127d4b1bfbd956a9bbe810cdbec706bffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 2 Jan 2019 12:57:06 +0100 Subject: [PATCH 2/3] Ensure that all external LDFLAGS are properly forwarded to `-extldflags` --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3ee68dd34..3dededb84 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ SOURCES = $(shell script/build files) SOURCE_DATE_EPOCH ?= $(shell date +%s) BUILD_DATE = $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" '+%d %b %Y' 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" '+%d %b %Y') HUB_VERSION = $(shell hub version | tail -1) -export LDFLAGS := -extldflags=$(LDFLAGS) +export LDFLAGS := -extldflags='$(LDFLAGS)' export GCFLAGS := all=-trimpath=$(PWD) export ASMFLAGS := all=-trimpath=$(PWD) From dd5388fecd8a693f38c5d437b9e880e4b30c5554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Wed, 2 Jan 2019 12:58:06 +0100 Subject: [PATCH 3/3] Support `trimpath` for Go v1.8/1.9 Go 1.10 release notes: > The go build -asmflags, -gcflags, -gccgoflags, and -ldflags options > now apply by default only to the packages listed directly on the > command line. For example, go build -gcflags=-m mypkg passes the > compiler the -m flag when building mypkg but not its dependencies. The > new, more general form -asmflags=pattern=flags (and similarly for the > others) applies the flags only to the packages matching the pattern. > For example: go install -ldflags=cmd/gofmt=-X=main.version=1.2.3 > cmd/... installs all the commands matching cmd/... but only applies > the -X option to the linker flags for cmd/gofmt. For more details, see > go help build. --- Makefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3dededb84..b4fd2352b 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,10 @@ SOURCES = $(shell script/build files) SOURCE_DATE_EPOCH ?= $(shell date +%s) BUILD_DATE = $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" '+%d %b %Y' 2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" '+%d %b %Y') HUB_VERSION = $(shell hub version | tail -1) +FLAGS_ALL = $(shell go version | grep -q 'go1.[89]' || echo 'all=') export LDFLAGS := -extldflags='$(LDFLAGS)' -export GCFLAGS := all=-trimpath=$(PWD) -export ASMFLAGS := all=-trimpath=$(PWD) +export GCFLAGS := $(FLAGS_ALL)-trimpath='$(PWD)' +export ASMFLAGS := $(FLAGS_ALL)-trimpath='$(PWD)' MIN_COVERAGE = 89.4