dockerfile based binary building

Using cross compilation toolchains that work from any platform
Adds darwin/arm64 support and bake targets. Static and dynamic
binary targets are available, both with glibc and musl.

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2021-03-02 23:03:02 -08:00
parent 59fd6f0270
commit 6423da8dcd
8 changed files with 165 additions and 93 deletions

View File

@ -1,6 +1,8 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
set -eu
TARGET=${TARGET:-"build"}
PLATFORM=${PLATFORM:-}
VERSION=${VERSION:-"unknown-version"}
GITCOMMIT=${GITCOMMIT:-$(git rev-parse --short HEAD 2> /dev/null || true)}
@ -20,15 +22,15 @@ export LDFLAGS="\
${LDFLAGS:-} \
"
GOOS="${GOOS:-$(go env GOHOSTOS)}"
GOARCH="${GOARCH:-$(go env GOHOSTARCH)}"
GOOS="$(go env GOOS)"
GOARCH="$(go env GOARCH)"
if [ "${GOARCH}" = "arm" ]; then
GOARM="${GOARM:-$(go env GOHOSTARM)}"
GOARM="$(go env GOARM)"
fi
TARGET="build/docker-$GOOS-$GOARCH"
TARGET="$TARGET/docker-${GOOS}-${GOARCH}"
if [ "${GOARCH}" = "arm" ] && [ -n "${GOARM}" ]; then
TARGET="${TARGET}-v${GOARM}"
TARGET="${TARGET}-v${GOARM}"
fi
if [ "${GOOS}" = "windows" ]; then

View File

@ -1,14 +1,64 @@
#!/usr/bin/env bash
#!/usr/bin/env sh
#
# Build a static binary for the host OS/ARCH
#
set -eu -o pipefail
set -eu
source ./scripts/build/.variables
: "${CGO_ENABLED=}"
: "${GO_LINKMODE=static}"
: "${GO_BUILDMODE=}"
: "${GO_BUILDTAGS=}"
: "${GO_STRIP=}"
echo "Building statically linked $TARGET"
export CGO_ENABLED=0
go build -o "${TARGET}" --ldflags "${LDFLAGS}" "${SOURCE}"
set -x
. ./scripts/build/.variables
ln -sf "$(basename "${TARGET}")" build/docker
if [ -z "$CGO_ENABLED" ]; then
case "$(go env GOOS)" in
linux)
case "$(go env GOARCH)" in
amd64|arm64|arm|s390x)
CGO_ENABLED=1
;;
*)
CGO_ENABLED=0
;;
esac
;;
darwin|windows)
CGO_ENABLED=1
;;
*)
CGO_ENABLED=0
;;
esac
fi
export CGO_ENABLED
if [ "$CGO_ENABLED" = "1" ] && [ "$(go env GOOS)" != "windows" ]; then
case "$(go env GOARCH)" in
mips*|ppc64)
# pie build mode is not supported on mips architectures
;;
*)
GO_BUILDMODE="-buildmode=pie"
;;
esac
GO_BUILDTAGS="$GO_BUILDTAGS pkcs11"
fi
if [ "$CGO_ENABLED" = "1" ] && [ "$GO_LINKMODE" = "static" ] && [ "$(go env GOOS)" = "linux" ]; then
LDFLAGS="$LDFLAGS -extldflags -static"
fi
if [ -n "$GO_STRIP" ]; then
LDFLAGS="$LDFLAGS -s -w"
fi
echo "Building $GO_LINKMODE $(basename "${TARGET}")"
export GO111MODULE=auto
go build -o "${TARGET}" -tags "${GO_BUILDTAGS}" --ldflags "${LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}"
ln -sf "$(basename "${TARGET}")" "$(dirname "${TARGET}")/docker"

View File

@ -1,33 +0,0 @@
#!/usr/bin/env bash
#
# Build a binary for all supported platforms
#
set -eu -o pipefail
BUILDDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
export SHELL=bash
jobs=(
"$BUILDDIR/windows" \
"$BUILDDIR/osx" \
"GOOS=linux GOARCH=amd64 $BUILDDIR/binary" \
"GOOS=linux GOARCH=arm $BUILDDIR/binary" \
"GOOS=linux GOARCH=ppc64le $BUILDDIR/binary" \
"GOOS=linux GOARCH=s390x $BUILDDIR/binary" \
)
# Outside of circleCI run all at once. On circleCI run two at a time because
# each container has access to two cores.
group=${CROSS_GROUP-"all"}
if [ "$group" = "all" ]; then
echo "Building binaries for all platforms"
parallel ::: "${jobs[@]}"
exit 0
fi
declare -i start="$group*2"
parallel ::: "${jobs[@]:$start:2}"

View File

@ -1,24 +0,0 @@
#!/usr/bin/env bash
#
# Build a dynamically linked binary for the host OS/ARCH
#
set -eu -o pipefail
source ./scripts/build/.variables
echo "Building dynamically linked $TARGET"
export CGO_ENABLED=1
case "$(go env GOARCH)" in
mips*|ppc64)
# pie build mode is not supported on mips architectures
GO_BUILDMODE=""
;;
*)
GO_BUILDMODE="-buildmode=pie"
;;
esac
go build -o "${TARGET}" -tags pkcs11 --ldflags "${LDFLAGS}" ${GO_BUILDMODE} "${SOURCE}"
ln -sf "$(basename "${TARGET}")" build/docker

View File

@ -1,22 +0,0 @@
#!/usr/bin/env bash
#
# Build an osx binary from linux
#
set -eu -o pipefail
source ./scripts/build/.variables
export CGO_ENABLED=1
export GOOS=darwin
export GOARCH=amd64
export CC=o64-clang
export CXX=o64-clang++
export LDFLAGS="$LDFLAGS -linkmode external -s"
export LDFLAGS_STATIC_DOCKER='-extld='${CC}
# Override TARGET
TARGET="build/docker-$GOOS-$GOARCH"
echo "Building $TARGET"
go build -o "${TARGET}" -tags pkcs11 --ldflags "${LDFLAGS}" "${SOURCE}"