From 93b9bab9328a55e55655d1912bcb582b6aa27c4f Mon Sep 17 00:00:00 2001 From: Alessio Treglia Date: Fri, 9 Oct 2020 10:23:09 +0100 Subject: [PATCH] simplified reproducible buildsystem (#5477) --- .gitignore | 1 + Makefile | 29 ++++++++++++++++++++++++----- build.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) create mode 100755 build.sh diff --git a/.gitignore b/.gitignore index 8cd4bf484..db0e1f77a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.bak .DS_Store build/* +artifacts/* rpc/test/.tendermint .tendermint remote_dump diff --git a/Makefile b/Makefile index cc4c8cb97..590c73610 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ +#!/usr/bin/make -f + PACKAGES=$(shell go list ./...) -OUTPUT?=build/tendermint +BUILDDIR ?= $(CURDIR)/build BUILD_TAGS?=tendermint LD_FLAGS = -X github.com/tendermint/tendermint/version.GitCommit=`git rev-parse --short=8 HEAD` @@ -56,14 +58,17 @@ include tests.mk ### Build Tendermint ### ############################################################################### -build: - CGO_ENABLED=$(CGO_ENABLED) go build $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' -o $(OUTPUT) ./cmd/tendermint/ +build: $(BUILDDIR)/ + CGO_ENABLED=$(CGO_ENABLED) go build $(BUILD_FLAGS) -tags '$(BUILD_TAGS)' -o $(BUILDDIR)/ ./cmd/tendermint/ .PHONY: build install: CGO_ENABLED=$(CGO_ENABLED) go install $(BUILD_FLAGS) -tags $(BUILD_TAGS) ./cmd/tendermint .PHONY: install +$(BUILDDIR)/: + mkdir -p $@ + ############################################################################### ### Protobuf ### ############################################################################### @@ -142,7 +147,7 @@ draw_deps: get_deps_bin_size: @# Copy of build recipe with additional flags to perform binary size analysis - $(eval $(shell go build -work -a $(BUILD_FLAGS) -tags $(BUILD_TAGS) -o $(OUTPUT) ./cmd/tendermint/ 2>&1)) + $(eval $(shell go build -work -a $(BUILD_FLAGS) -tags $(BUILD_TAGS) -o $(BUILDDIR)/ ./cmd/tendermint/ 2>&1)) @find $(WORK) -type f -name "*.a" | xargs -I{} du -hxs "{}" | sort -rh | sed -e s:${WORK}/::g > deps_bin_size.log @echo "Results can be found here: $(CURDIR)/deps_bin_size.log" .PHONY: get_deps_bin_size @@ -210,7 +215,7 @@ sync-docs: ############################################################################### build-docker: build-linux - cp $(OUTPUT) DOCKER/tendermint + cp $(BUILDDIR)/tendermint DOCKER/tendermint docker build --label=tendermint --tag="tendermint/tendermint" DOCKER rm -rf DOCKER/tendermint .PHONY: build-docker @@ -264,3 +269,17 @@ endif contract-tests: dredd .PHONY: contract-tests + +clean: + rm -rf $(CURDIR)/artifacts/ $(BUILDDIR)/ + +build-reproducible: + docker rm latest-build || true + docker run --volume=$(CURDIR):/sources:ro \ + --env TARGET_PLATFORMS='linux/amd64 linux/arm64 darwin/amd64 windows/amd64' \ + --env APP=tendermint \ + --env COMMIT=$(shell git rev-parse --short=8 HEAD) \ + --env VERSION=$(shell git describe --tags) \ + --name latest-build cosmossdk/rbuilder:latest + docker cp -a latest-build:/home/builder/artifacts/ $(CURDIR)/ +.PHONY: build-reproducible diff --git a/build.sh b/build.sh new file mode 100755 index 000000000..52348b635 --- /dev/null +++ b/build.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +set -ue + +# Expect the following envvars to be set: +# - APP +# - VERSION +# - COMMIT +# - TARGET_OS +# - LEDGER_ENABLED +# - DEBUG + +# Source builder's functions library +. /usr/local/share/cosmos-sdk/buildlib.sh + +# These variables are now available +# - BASEDIR +# - OUTDIR + +# Build for each os-architecture pair +for platform in ${TARGET_PLATFORMS} ; do + # This function sets GOOS, GOARCH, and OS_FILE_EXT environment variables + # according to the build target platform. OS_FILE_EXT is empty in all + # cases except when the target platform is 'windows'. + setup_build_env_for_platform "${platform}" + + make clean + echo Building for $(go env GOOS)/$(go env GOARCH) >&2 + GOROOT_FINAL="$(go env GOROOT)" \ + make build LDFLAGS=-buildid=${VERSION} COMMIT=${COMMIT} + mv ./build/${APP}${OS_FILE_EXT} ${OUTDIR}/${APP}-${VERSION}-$(go env GOOS)-$(go env GOARCH)${OS_FILE_EXT} + + # This function restore the build environment variables to their + # original state. + restore_build_env +done + +# Generate and display build report. +generate_build_report +cat ${OUTDIR}/build_report