From 0430ebf95cc0ae40d61778d60e92bf9c8401a3ce Mon Sep 17 00:00:00 2001 From: Greg Szabo Date: Wed, 3 Jan 2018 14:58:23 -0500 Subject: [PATCH 1/3] Makefile changes for cross-building and standardized builds using gox --- Makefile | 59 ++++++++++++++++++++++++++++++++++--------- scripts/dist_build.sh | 28 ++------------------ 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index 2aed1acf4..bb1d72d50 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,29 @@ -GOTOOLS = \ +GOTOOLS := \ github.com/mitchellh/gox \ github.com/Masterminds/glide \ github.com/tcnksm/ghr \ gopkg.in/alecthomas/gometalinter.v2 -GOTOOLS_CHECK = gox glide ghr gometalinter.v2 -PACKAGES=$(shell go list ./... | grep -v '/vendor/') -BUILD_TAGS?=tendermint -TMHOME = $${TMHOME:-$$HOME/.tendermint} -BUILD_FLAGS = -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short HEAD`" +GO_MIN_VERSION := 1.9.2 +PACKAGES := $(shell go list ./... | grep -v '/vendor/') +BUILD_TAGS ?= tendermint +TMHOME ?= $(HOME)/.tendermint +GOPATH ?= $(shell go env GOPATH) +GOROOT ?= $(shell go env GOROOT) +GOGCCFLAGS ?= $(shell go env GOGCCFLAGS) +PROD_LDFLAGS ?= -w -s +XC_ARCH ?= 386 amd64 arm +XC_OS ?= solaris darwin freebsd linux windows +XC_OSARCH ?= !darwin/arm !solaris/amd64 !freebsd/amd64 +BUILD_OUTPUT ?= $(GOPATH)/bin/{{.OS}}_{{.Arch}}/tendermint + +GOX_FLAGS = -os="$(XC_OS)" -arch="$(XC_ARCH)" -osarch="$(XC_OSARCH)" -output="$(BUILD_OUTPUT)" +ifeq ($(BUILD_FLAGS_RACE),YES) +RACEFLAG=-race +else +RACEFLAG= +endif +BUILD_FLAGS = -asmflags "-trimpath $(GOPATH)" -gcflags "-trimpath $(GOPATH)" -tags "$(BUILD_TAGS)" -ldflags "$(PROD_LDFLAGS) -X github.com/tendermint/tendermint/version.GitCommit=$(shell git rev-parse --short=7 HEAD)" $(RACEFLAG) +GO_VERSION:=$(shell go version | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+') all: check build test install metalinter @@ -17,27 +33,46 @@ check: check_tools get_vendor_deps ######################################## ### Build +build_cc: + $(shell which gox) $(BUILD_FLAGS) $(GOX_FLAGS) ./cmd/tendermint/ + build: - go build $(BUILD_FLAGS) -o build/tendermint ./cmd/tendermint/ + make build_cc PROD_LDFLAGS="" XC_ARCH=amd64 XC_OS="$(shell uname -s)" BUILD_OUTPUT=$(GOPATH)/bin/tendermint build_race: - go build -race $(BUILD_FLAGS) -o build/tendermint ./cmd/tendermint + $(shell which go) build $(BUILD_FLAGS) -race -o "$(BUILD_OUTPUT)" ./cmd/tendermint/ +#For the future when this is merged: https://github.com/mitchellh/gox/pull/105 +# make build_cc PROD_LDFLAGS="" XC_ARCH=amd64 XC_OS=$(shell uname -s) BUILD_FLAGS_RACE=YES BUILD_OUTPUT=build/tendermint # dist builds binaries for all platforms and packages them for distribution dist: @BUILD_TAGS='$(BUILD_TAGS)' sh -c "'$(CURDIR)/scripts/dist.sh'" install: - go install $(BUILD_FLAGS) ./cmd/tendermint + make build ######################################## ### Tools & dependencies check_tools: - @# https://stackoverflow.com/a/25668869 - @echo "Found tools: $(foreach tool,$(GOTOOLS_CHECK),\ - $(if $(shell which $(tool)),$(tool),$(error "No $(tool) in PATH")))" +ifeq ($(GO_VERSION),) + $(error go not found) +endif +ifneq ($(GO_VERSION),$(GO_MIN_VERSION)) + $(warning WARNING: build will not be deterministic. go version should be $(GO_MIN_VERSION)) +endif +ifneq ($(findstring -fdebug-prefix-map,$(GOGCCFLAGS)),-fdebug-prefix-map) + $(warning WARNING: build will not be deterministic. The compiler does not support the '-fdebug-prefix-map' flag.) +endif +ifneq ($(GOROOT),/usr/local/go) + $(warning WARNING: build will not be deterministic. GOPATH should be set to /usr/local/go) +endif +ifneq ($(findstring $(GOPATH)/bin,$(PATH)),$(GOPATH)/bin) + $(warning WARNING: PATH does not contain GOPATH/bin. Some external dependencies might be unavailable.) +endif +# https://stackoverflow.com/a/25668869 + @echo "Found tools: $(foreach tool,$(notdir $(GOTOOLS)),$(if $(shell which $(tool)),$(tool),$(error "No $(tool) in PATH. Add GOPATH/bin to PATH and run 'make get_tools'")))" get_tools: @echo "--> Installing tools" diff --git a/scripts/dist_build.sh b/scripts/dist_build.sh index 587199e02..e7471c4d5 100755 --- a/scripts/dist_build.sh +++ b/scripts/dist_build.sh @@ -9,32 +9,8 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" # Change into that dir because we expect that. cd "$DIR" -# Get the git commit -GIT_COMMIT="$(git rev-parse --short HEAD)" -GIT_IMPORT="github.com/tendermint/tendermint/version" - -# Determine the arch/os combos we're building for -XC_ARCH=${XC_ARCH:-"386 amd64 arm"} -XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"} - -# Make sure build tools are available. -make tools - -# Get VENDORED dependencies -make get_vendor_deps - -# Build! -# ldflags: -s Omit the symbol table and debug information. -# -w Omit the DWARF symbol table. -echo "==> Building..." -"$(which gox)" \ - -os="${XC_OS}" \ - -arch="${XC_ARCH}" \ - -osarch="!darwin/arm !solaris/amd64 !freebsd/amd64" \ - -ldflags "-s -w -X ${GIT_IMPORT}.GitCommit=${GIT_COMMIT}" \ - -output "build/pkg/{{.OS}}_{{.Arch}}/tendermint" \ - -tags="${BUILD_TAGS}" \ - github.com/tendermint/tendermint/cmd/tendermint +# Make sure build tools are available, get VENDORED dependencies and build +make get_tools get_vendor_deps build_cc # Zip all the files. echo "==> Packaging..." From f67f99c227e66c9bdbda2f3e34b5c7fcbc6f67c2 Mon Sep 17 00:00:00 2001 From: Greg Szabo Date: Wed, 3 Jan 2018 17:24:11 -0500 Subject: [PATCH 2/3] Extended install document with docker option. Added extra checks to developer's build target. --- Makefile | 16 ++++++++++------ docs/install.rst | 13 +++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index bb1d72d50..3aa5e051c 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ TMHOME ?= $(HOME)/.tendermint GOPATH ?= $(shell go env GOPATH) GOROOT ?= $(shell go env GOROOT) GOGCCFLAGS ?= $(shell go env GOGCCFLAGS) -PROD_LDFLAGS ?= -w -s +#LDFLAGS_EXTRA ?= -w -s XC_ARCH ?= 386 amd64 arm XC_OS ?= solaris darwin freebsd linux windows XC_OSARCH ?= !darwin/arm !solaris/amd64 !freebsd/amd64 @@ -22,7 +22,7 @@ RACEFLAG=-race else RACEFLAG= endif -BUILD_FLAGS = -asmflags "-trimpath $(GOPATH)" -gcflags "-trimpath $(GOPATH)" -tags "$(BUILD_TAGS)" -ldflags "$(PROD_LDFLAGS) -X github.com/tendermint/tendermint/version.GitCommit=$(shell git rev-parse --short=7 HEAD)" $(RACEFLAG) +BUILD_FLAGS = -asmflags "-trimpath $(GOPATH)" -gcflags "-trimpath $(GOPATH)" -tags "$(BUILD_TAGS)" -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=$(shell git rev-parse --short=7 HEAD) $(LDFLAGS_EXTRA)" $(RACEFLAG) GO_VERSION:=$(shell go version | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+') all: check build test install metalinter @@ -33,16 +33,20 @@ check: check_tools get_vendor_deps ######################################## ### Build -build_cc: +build_cc: check_tools $(shell which gox) $(BUILD_FLAGS) $(GOX_FLAGS) ./cmd/tendermint/ build: - make build_cc PROD_LDFLAGS="" XC_ARCH=amd64 XC_OS="$(shell uname -s)" BUILD_OUTPUT=$(GOPATH)/bin/tendermint +ifeq ($(OS),Windows_NT) + make build_cc XC_ARCH=amd64 XC_OS=windows BUILD_OUTPUT=$(GOPATH)/bin/tendermint +else + make build_cc XC_ARCH=amd64 XC_OS="$(shell uname -s)" BUILD_OUTPUT=$(GOPATH)/bin/tendermint +endif build_race: +#TODO: Wait for this to be merged: https://github.com/mitchellh/gox/pull/105 Then switch over to make build and remove the go build line. +# make build BUILD_FLAGS_RACE=YES $(shell which go) build $(BUILD_FLAGS) -race -o "$(BUILD_OUTPUT)" ./cmd/tendermint/ -#For the future when this is merged: https://github.com/mitchellh/gox/pull/105 -# make build_cc PROD_LDFLAGS="" XC_ARCH=amd64 XC_OS=$(shell uname -s) BUILD_FLAGS_RACE=YES BUILD_OUTPUT=build/tendermint # dist builds binaries for all platforms and packages them for distribution dist: diff --git a/docs/install.rst b/docs/install.rst index 64fae4cdc..9edc051a6 100644 --- a/docs/install.rst +++ b/docs/install.rst @@ -6,6 +6,19 @@ From Binary To download pre-built binaries, see the `Download page `__. +From Source using Docker +------------------------ + +If you have docker running, all you need is the ``golang`` image to build tendermint. +If you don't, you can get help setting it up `here `__. + +:: + mkdir $HOME/tendermintbin + docker run --rm -it -v $HOME/tendermintbin:/go/bin:Z golang:1.9.2 /bin/bash -c "go-wrapper download github.com/tendermint/tendermint/cmd/tendermint ; make -C /go/src/github.com/tendermint/tendermint get_tools get_vendor_deps build_cc" + +You will find the ``tendermint`` binaries for different architectures and operating systems in your ``$HOME/tendermintbin`` folder. + + From Source ----------- From bb3dc10f24917601ce1a1b25882eecf6ef0a79b0 Mon Sep 17 00:00:00 2001 From: Greg Szabo Date: Fri, 5 Jan 2018 14:22:13 -0500 Subject: [PATCH 3/3] Makefile improvements for deterministic builds based on Bucky's feedback --- Makefile | 28 ++++++++++++++++++++++------ scripts/dist_build.sh | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 3aa5e051c..2a4b14e3f 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ GOGCCFLAGS ?= $(shell go env GOGCCFLAGS) XC_ARCH ?= 386 amd64 arm XC_OS ?= solaris darwin freebsd linux windows XC_OSARCH ?= !darwin/arm !solaris/amd64 !freebsd/amd64 -BUILD_OUTPUT ?= $(GOPATH)/bin/{{.OS}}_{{.Arch}}/tendermint +BUILD_OUTPUT ?= ./build/{{.OS}}_{{.Arch}}/tendermint GOX_FLAGS = -os="$(XC_OS)" -arch="$(XC_ARCH)" -osarch="$(XC_OSARCH)" -output="$(BUILD_OUTPUT)" ifeq ($(BUILD_FLAGS_RACE),YES) @@ -22,8 +22,13 @@ RACEFLAG=-race else RACEFLAG= endif -BUILD_FLAGS = -asmflags "-trimpath $(GOPATH)" -gcflags "-trimpath $(GOPATH)" -tags "$(BUILD_TAGS)" -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=$(shell git rev-parse --short=7 HEAD) $(LDFLAGS_EXTRA)" $(RACEFLAG) +BUILD_FLAGS = -asmflags "-trimpath $(GOPATH)" -gcflags "-trimpath $(GOPATH)" -tags "$(BUILD_TAGS)" -ldflags "-X github.com/tendermint/tendermint/version.GitCommit=$(shell git rev-parse --short=8 HEAD) $(LDFLAGS_EXTRA)" $(RACEFLAG) GO_VERSION:=$(shell go version | grep -o '[[:digit:]]\+.[[:digit:]]\+.[[:digit:]]\+') +#Check that that minor version of GO meets the minimum required +GO_MINOR_VERSION := $(shell grep -o \.[[:digit:]][[:digit:]]*\. <<< $(GO_VERSION) | grep -o [[:digit:]]* ) +GO_MIN_MINOR_VERSION := $(shell grep -o \.[[:digit:]][[:digit:]]*\. <<< $(GO_MIN_VERSION) | grep -o [[:digit:]]* ) +GO_MINOR_VERSION_CHECK := $(shell test $(GO_MINOR_VERSION) -ge $(GO_MIN_MINOR_VERSION) && echo YES) + all: check build test install metalinter @@ -33,14 +38,14 @@ check: check_tools get_vendor_deps ######################################## ### Build -build_cc: check_tools +build_xc: check_tools $(shell which gox) $(BUILD_FLAGS) $(GOX_FLAGS) ./cmd/tendermint/ build: ifeq ($(OS),Windows_NT) - make build_cc XC_ARCH=amd64 XC_OS=windows BUILD_OUTPUT=$(GOPATH)/bin/tendermint + make build_xc XC_ARCH=amd64 XC_OS=windows BUILD_OUTPUT=$(GOPATH)/bin/tendermint else - make build_cc XC_ARCH=amd64 XC_OS="$(shell uname -s)" BUILD_OUTPUT=$(GOPATH)/bin/tendermint + make build_xc XC_ARCH=amd64 XC_OS="$(shell uname -s)" BUILD_OUTPUT=$(GOPATH)/bin/tendermint endif build_race: @@ -63,15 +68,26 @@ check_tools: ifeq ($(GO_VERSION),) $(error go not found) endif +#Check minimum required go version ifneq ($(GO_VERSION),$(GO_MIN_VERSION)) $(warning WARNING: build will not be deterministic. go version should be $(GO_MIN_VERSION)) +ifneq ($(GO_MINOR_VERSION_CHECK),YES) + $(error ERROR: The minor version of Go ($(GO_VERSION)) is lower than the minimum required ($(GO_MIN_VERSION))) +endif endif +#-fdebug-prefix-map switches the temporary, randomized workdir name in the binary to a static text ifneq ($(findstring -fdebug-prefix-map,$(GOGCCFLAGS)),-fdebug-prefix-map) $(warning WARNING: build will not be deterministic. The compiler does not support the '-fdebug-prefix-map' flag.) endif +#GOROOT string is copied into the binary. For deterministic builds, we agree to keep it at /usr/local/go. (Default for golang:1.9.2 docker image, linux and osx.) ifneq ($(GOROOT),/usr/local/go) - $(warning WARNING: build will not be deterministic. GOPATH should be set to /usr/local/go) + $(warning WARNING: build will not be deterministic. GOROOT should be set to /usr/local/go) +endif +#GOPATH string is copied into the binary. Although the -trimpath flag tries to eliminate it, it doesn't do it everywhere in Go 1.9.2. For deterministic builds we agree to keep it at /go. (Default for golang:1.9.2 docker image.) +ifneq ($(GOPATH),/go) + $(warning WARNING: build will not be deterministic. GOPATH should be set to /go) endif +#External dependencies defined in GOTOOLS are built with get_tools. If they are already available on the system (for exmaple using a package manager), then get_tools might not be necessary. ifneq ($(findstring $(GOPATH)/bin,$(PATH)),$(GOPATH)/bin) $(warning WARNING: PATH does not contain GOPATH/bin. Some external dependencies might be unavailable.) endif diff --git a/scripts/dist_build.sh b/scripts/dist_build.sh index e7471c4d5..f1d8779f6 100755 --- a/scripts/dist_build.sh +++ b/scripts/dist_build.sh @@ -10,7 +10,7 @@ DIR="$( cd -P "$( dirname "$SOURCE" )/.." && pwd )" cd "$DIR" # Make sure build tools are available, get VENDORED dependencies and build -make get_tools get_vendor_deps build_cc +make get_tools get_vendor_deps build_xc # Zip all the files. echo "==> Packaging..."