Skip to content

Commit 606956b

Browse files
author
Alessio Treglia
authored
Reproducible buildsystem (#7247)
Provide a simple Docker-based mechanism for application developers to provide reproducible builds. Unlike gaia's current reproducible buildsystem, this does not depend on external tools, e.g. `gitian-builder`. `build-simd-linux` now builds `simd` in a deterministic Linux container.
1 parent cfb5fc0 commit 606956b

File tree

5 files changed

+171
-5
lines changed

5 files changed

+171
-5
lines changed

Makefile

+29-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
PACKAGES_NOSIMULATION=$(shell go list ./... | grep -v '/simulation')
44
PACKAGES_SIMTEST=$(shell go list ./... | grep '/simulation')
5-
VERSION := $(shell echo $(shell git describe) | sed 's/^v//')
5+
VERSION := $(shell echo $(shell git describe --always) | sed 's/^v//')
66
COMMIT := $(shell git log -1 --format='%H')
77
LEDGER_ENABLED ?= true
88
BINDIR ?= $(GOPATH)/bin
@@ -86,12 +86,35 @@ include contrib/devtools/Makefile
8686
build: go.sum
8787
go install -mod=readonly ./...
8888

89-
build-simd: go.sum
89+
simd:
9090
mkdir -p $(BUILDDIR)
9191
go build -mod=readonly $(BUILD_FLAGS) -o $(BUILDDIR) ./simapp/simd
9292

93+
build-simd-all: go.sum
94+
$(if $(shell docker inspect -f '{{ .Id }}' cosmossdk/rbuilder 2>/dev/null),$(info found image cosmossdk/rbuilder),docker pull cosmossdk/rbuilder:latest)
95+
docker rm latest-build || true
96+
docker run --volume=$(CURDIR):/sources:ro \
97+
--env TARGET_OS='darwin linux windows' \
98+
--env APP=simd \
99+
--env VERSION=$(VERSION) \
100+
--env COMMIT=$(COMMIT) \
101+
--env LEDGER_ENABLED=$(LEDGER_ENABLED) \
102+
--name latest-build cosmossdk/rbuilder:latest
103+
docker cp -a latest-build:/home/builder/artifacts/ $(CURDIR)/
104+
93105
build-simd-linux: go.sum
94-
LEDGER_ENABLED=false GOOS=linux GOARCH=amd64 $(MAKE) build-simd
106+
$(if $(shell docker inspect -f '{{ .Id }}' cosmossdk/rbuilder 2>/dev/null),$(info found image cosmossdk/rbuilder),docker pull cosmossdk/rbuilder:latest)
107+
docker rm latest-build || true
108+
docker run --volume=$(CURDIR):/sources:ro \
109+
--env TARGET_OS='linux' \
110+
--env APP=simd \
111+
--env VERSION=$(VERSION) \
112+
--env COMMIT=$(COMMIT) \
113+
--env LEDGER_ENABLED=false \
114+
--name latest-build cosmossdk/rbuilder:latest
115+
docker cp -a latest-build:/home/builder/artifacts/ $(CURDIR)/
116+
mkdir -p $(BUILDDIR)
117+
cp artifacts/simd-*-linux-amd64 $(BUILDDIR)/simd
95118

96119
cosmovisor:
97120
$(MAKE) -C cosmovisor cosmovisor
@@ -119,7 +142,9 @@ distclean: clean
119142
.gitian-builder-cache/
120143

121144
clean:
122-
rm -rf $(BUILDDIR)/
145+
rm -rf \
146+
$(BUILDDIR)/ \
147+
artifacts/
123148

124149
.PHONY: distclean clean
125150

build.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
set -ue
4+
5+
# Expect the following envvars to be set:
6+
# - APP
7+
# - VERSION
8+
# - COMMIT
9+
# - TARGET_OS
10+
# - LEDGER_ENABLED
11+
# - DEBUG
12+
13+
# Source builder's functions library
14+
. /usr/local/share/cosmos-sdk/buildlib.sh
15+
16+
# These variables are now available
17+
# - BASEDIR
18+
# - OUTDIR
19+
20+
# Build for each os-architecture pair
21+
for os in ${TARGET_OS} ; do
22+
archs="`f_build_archs ${os}`"
23+
exe_file_extension="`f_binary_file_ext ${os}`"
24+
for arch in ${archs} ; do
25+
make clean
26+
GOOS="${os}" GOARCH="${arch}" GOROOT_FINAL="$(go env GOROOT)" \
27+
make ${APP} \
28+
LDFLAGS=-buildid=${VERSION} \
29+
VERSION=${VERSION} \
30+
COMMIT=${COMMIT} \
31+
LEDGER_ENABLED=${LEDGER_ENABLED}
32+
mv ./build/${APP}${exe_file_extension} ${OUTDIR}/${APP}-${VERSION}-${os}-${arch}${exe_file_extension}
33+
done
34+
unset exe_file_extension
35+
done
36+
37+
# Generate and display build report
38+
f_generate_build_report ${OUTDIR}
39+
cat ${OUTDIR}/build_report

contrib/images/Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ all: simd-env
33
simd-env:
44
docker build --build-arg UID=$(shell id -u) --build-arg GID=$(shell id -g) --tag cosmossdk/simd-env simd-env
55

6-
.PHONY: all simd-env
6+
rbuilder:
7+
docker build --tag cosmossdk/rbuilder rbuilder
8+
9+
.PHONY: all simd-env rbuilder

contrib/images/rbuilder/Dockerfile

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM golang:1.15.0-buster
2+
ENV DEBIAN_FRONTEND=noninteractive
3+
RUN apt-get update && apt-get --no-install-recommends -y install \
4+
pciutils build-essential git wget \
5+
lsb-release dpkg-dev curl bsdmainutils fakeroot
6+
RUN mkdir -p /usr/local/share/cosmos-sdk/
7+
COPY buildlib.sh /usr/local/share/cosmos-sdk/
8+
RUN useradd -ms /bin/bash -U builder
9+
ARG APP
10+
ARG DEBUG
11+
ENV APP ${APP:-cosmos-sdk}
12+
ENV DEBUG ${DEBUG}
13+
ENV VERSION unknown
14+
ENV COMMIT unknown
15+
ENV LEDGER_ENABLE true
16+
USER builder:builder
17+
WORKDIR /sources
18+
VOLUME [ "/sources" ]
19+
ENTRYPOINT [ "/sources/build.sh" ]

contrib/images/rbuilder/buildlib.sh

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#/bin/bash
2+
3+
f_make_release_tarball() {
4+
SOURCEDIST=${BASEDIR}/${APP}-${VERSION}.tar.gz
5+
6+
git archive --format tar.gz --prefix "${APP}-${VERSION}/" -o "${SOURCEDIST}" HEAD
7+
8+
l_tempdir="$(mktemp -d)"
9+
pushd "${l_tempdir}" >/dev/null
10+
tar xf "${SOURCEDIST}"
11+
rm "${SOURCEDIST}"
12+
find ${APP}-* | sort | tar --no-recursion --mode='u+rw,go+r-w,a+X' --owner=0 --group=0 -c -T - | gzip -9n > "${SOURCEDIST}"
13+
popd >/dev/null
14+
rm -rf "${l_tempdir}"
15+
}
16+
17+
f_setup_pristine_src_dir() {
18+
cd ${pristinesrcdir}
19+
tar --strip-components=1 -xf "${SOURCEDIST}"
20+
go mod download
21+
}
22+
23+
f_build_archs() {
24+
local l_os
25+
26+
l_os=$1
27+
28+
case "${l_os}" in
29+
darwin | windows)
30+
echo 'amd64'
31+
;;
32+
linux)
33+
echo 'amd64 arm64'
34+
;;
35+
*)
36+
echo "unknown OS -- ${l_os}" >&2
37+
return 1
38+
esac
39+
}
40+
41+
f_binary_file_ext() {
42+
[ $1 = windows ] && printf '%s' '.exe' || printf ''
43+
}
44+
45+
f_generate_build_report() {
46+
local l_tempfile
47+
48+
l_tempfile="$(mktemp)"
49+
50+
pushd "${OUTDIR}" >/dev/null
51+
cat >>"${l_tempfile}" <<EOF
52+
App: ${APP}
53+
Version: ${VERSION}
54+
Commit: ${COMMIT}
55+
EOF
56+
echo 'Files:' >> "${l_tempfile}"
57+
md5sum * | sed 's/^/ /' >> "${l_tempfile}"
58+
echo 'Checksums-Sha256:' >> "${l_tempfile}"
59+
sha256sum * | sed 's/^/ /' >> "${l_tempfile}"
60+
mv "${l_tempfile}" build_report
61+
popd >/dev/null
62+
}
63+
64+
[ "x${DEBUG}" = "x" ] || set -x
65+
66+
BASEDIR="$(mktemp -d)"
67+
OUTDIR=$HOME/artifacts
68+
rm -rfv ${OUTDIR}/
69+
mkdir -p ${OUTDIR}/
70+
pristinesrcdir=${BASEDIR}/buildsources
71+
mkdir -p ${pristinesrcdir}
72+
73+
# Make release tarball
74+
f_make_release_tarball
75+
76+
# Extract release tarball and cache dependencies
77+
f_setup_pristine_src_dir
78+
79+
# Move the release tarball to the out directory
80+
mv ${SOURCEDIST} ${OUTDIR}/

0 commit comments

Comments
 (0)