refactor(mk): move build rules into separate scripts (#855)

See https://github.com/ooni/probe/issues/2218
This commit is contained in:
Simone Basso
2022-08-17 13:16:53 +02:00
committed by GitHub
parent 4e99e5030a
commit 5c0368c862
19 changed files with 538 additions and 272 deletions
-24
View File
@@ -1,24 +0,0 @@
#!/bin/sh
# This script is executed by `./mk` when building inside
# an Alpine Linux docker container. Using Alpine Linux, which
# uses musl libc, allows us to emit static binaries.
set -e
if [ "$GOARCH" = "" ]; then
echo 'fatal: GOARCH is not set' 1>&2
exit 1
fi
set -x
apk update
apk upgrade
apk add --no-progress gcc git linux-headers musl-dev
# some of the following exports are redundant but are however
# useful because they provide explicit logging
export GOARM=$GOARM
export CGO_ENABLED=1
export GOOS=linux
export GOARCH=$GOARCH
archname=$GOARCH
if [ "$GOARCH" = "arm" -a "$GOARM" = "7" ]; then
archname="armv7"
fi
go build -o "./CLI/ooniprobe-linux-$archname" -ldflags='-s -w -extldflags "-static"' "$@" ./cmd/ooniprobe
+19
View File
@@ -0,0 +1,19 @@
#!/bin/bash
set -euo pipefail
EXPECTED_GOLANG_VERSION=go$(cat GOVERSION)
printf "checking for go... "
if ! command -v go; then
echo "not found"
exit 1
fi
printf "checking for go version... "
GOLANG_VERSION=$(go version | awk '{print $3}')
echo $GOLANG_VERSION
if [[ $GOLANG_VERSION != $EXPECTED_GOLANG_VERSION ]]; then
echo "FATAL: go version must be $EXPECTED_GOLANG_VERSION instead of $GOLANG_VERSION"
exit 1
fi
+37
View File
@@ -0,0 +1,37 @@
#!/bin/bash
set -euo pipefail
EXPECTED_MINGW_W64_VERSION=${EXPECTED_MINGW_W64_VERSION:-12.1.0} # Allow overriding
printf "checking for x86_64-w64-mingw32-gcc... "
command -v x86_64-w64-mingw32-gcc || {
echo "not found"
exit 1
}
printf "checking for i686-w64-mingw32-gcc... "
command -v i686-w64-mingw32-gcc || {
echo "not found"
exit 1
}
exitcode=0
printf "checking for x86_64-w64-mingw32-gcc version... "
__version_amd64=$(x86_64-w64-mingw32-gcc --version | sed -n 1p | awk '{print $3}')
echo $__version_amd64
[[ "$EXPECTED_MINGW_W64_VERSION" == "$__version_amd64" ]] || {
echo "fatal: x86_64-w64-mingw32-gcc version must be $EXPECTED_MINGW_W64_VERSION instead of $__version_amd64"
exitcode=1
}
printf "checking for i686-w64-mingw32-gcc version... "
__version_386=$(i686-w64-mingw32-gcc --version | sed -n 1p | awk '{print $3}')
echo $__version_386
[[ "$EXPECTED_MINGW_W64_VERSION" == "$__version_386" ]] || {
echo "fatal: i686-w64-mingw32-gcc version must be $EXPECTED_MINGW_W64_VERSION instead of $__version_386"
exitcode=1
}
exit $exitcode
+14
View File
@@ -0,0 +1,14 @@
#!/bin/sh
set -euxo pipefail
apk update
apk upgrade
apk add --no-progress gcc git linux-headers musl-dev
# some of the following exports are redundant but are however
# useful because they provide explicit logging
export CGO_ENABLED=1
export GOARM=$GOARM
export GOOS=$GOOS
export GOARCH=$GOARCH
go build -o ./CLI/$PRODUCT-$GOOS-$OONIARCH \
-ldflags='-s -w -extldflags "-static"' \
$GOLANG_EXTRA_FLAGS $PACKAGE
+59
View File
@@ -0,0 +1,59 @@
#!/bin/bash
set -euo pipefail
if [[ $# -ne 3 ]]; then
echo "" 1>&2
echo "Cross-compiler for a Go PACKAGE producing GOOS/OONIARCH binaries." 1>&2
echo "" 1>&2
echo "usage: $0 GOOS OONIARCH PACKAGE" 1>&2
echo "" 1>&2
echo "GOOS must be one of: android, darwin, linux, windows." 1>&2
echo "" 1>&2
echo "OONIARCH must be one of: 386, amd64, arm64, armv6, armv7." 1>&2
echo "" 1>&2
echo "Features:" 1>&2
echo "" 1>&2
echo "* automatically sets -tags=ooni_psiphon_config when possible;" 1>&2
echo "" 1>&2
echo "* if GOLANG_EXTRA_FLAGS is set, pass it to the Go compiler." 1>&2
echo "" 1>&2
echo "Example:" 1>&2
echo "" 1>&2
echo " ./CLI/go-build-cross linux armv7 ./internal/cmd/miniooni" 1>&2
echo "" 1>&2
exit 1
fi
GOOS=$1
OONIARCH=$2
PACKAGE=$3
if [[ $OONIARCH == armv7 ]]; then
GOARCH=arm
GOARM=7
elif [[ $OONIARCH == armv6 ]]; then
GOARCH=arm
GOARM=6
else
GOARCH=$OONIARCH
GOARM=
fi
if [[ -f ./internal/engine/psiphon-config.json.age &&
-f ./internal/engine/psiphon-config.key ]]; then
OONI_PSIPHON_TAGS=ooni_psiphon_config
else
OONI_PSIPHON_TAGS=""
fi
PRODUCT=$(basename $PACKAGE)
set -x
export CGO_ENABLED=0
export GOOS=$GOOS
export GOARCH=$GOARCH
export GOARM=$GOARM
go build -tags=$OONI_PSIPHON_TAGS -ldflags="-s -w" \
-o ./CLI/$PRODUCT-$GOOS-$OONIARCH ${GOLANG_EXTRA_FLAGS:-} \
$PACKAGE
+45
View File
@@ -0,0 +1,45 @@
#!/bin/bash
set -euo pipefail
if [[ $# -ne 2 ]]; then
echo "" 1>&2
echo "Compiler for a Go PACKAGE producing darwin/GOARCH binaries." 1>&2
echo "" 1>&2
echo "usage: $0 GOARCH PACKAGE" 1>&2
echo "" 1>&2
echo "GOARCH must be one of: amd64, arm64." 1>&2
echo "" 1>&2
echo "Features:" 1>&2
echo "" 1>&2
echo "* automatically sets -tags=ooni_psiphon_config when possible;" 1>&2
echo "" 1>&2
echo "* if GOLANG_EXTRA_FLAGS is set, pass it to the Go compiler." 1>&2
echo "" 1>&2
echo "Example:" 1>&2
echo "" 1>&2
echo " ./CLI/go-build-darwin arm64 ./internal/cmd/miniooni" 1>&2
echo "" 1>&2
exit 1
fi
GOOS=darwin
GOARCH=$1
PACKAGE=$2
if [[ -f ./internal/engine/psiphon-config.json.age &&
-f ./internal/engine/psiphon-config.key ]]; then
OONI_PSIPHON_TAGS=ooni_psiphon_config
else
OONI_PSIPHON_TAGS=""
fi
PRODUCT=$(basename $PACKAGE)
set -x
export CGO_ENABLED=1
export GOOS=$GOOS
export GOARCH=$GOARCH
go build -tags=$OONI_PSIPHON_TAGS -ldflags="-s -w" \
-o ./CLI/$PRODUCT-$GOOS-$GOARCH ${GOLANG_EXTRA_FLAGS:-} \
$PACKAGE
+62
View File
@@ -0,0 +1,62 @@
#!/bin/bash
set -euo pipefail
if [[ $# -ne 2 ]]; then
echo "" 1>&2
echo "Compiler for a Go PACKAGE producing static linux/OONIARCH binaries." 1>&2
echo "" 1>&2
echo "usage: $0 OONIARCH PACKAGE" 1>&2
echo "" 1>&2
echo "OONIARCH must be one of: 386, amd64, arm64, armv6, armv7." 1>&2
echo "" 1>&2
echo "Features:" 1>&2
echo "" 1>&2
echo "* automatically sets -tags=ooni_psiphon_config when possible;" 1>&2
echo "" 1>&2
echo "* if GOLANG_EXTRA_FLAGS is set, pass it to the Go compiler." 1>&2
echo "" 1>&2
echo "Example:" 1>&2
echo "" 1>&2
echo " ./CLI/go-build-linux-static arm64 ./internal/cmd/miniooni" 1>&2
echo "" 1>&2
exit 1
fi
GOLANG_DOCKER_IMAGE=golang:$(cat GOVERSION)-alpine
GOOS=linux
OONIARCH=$1
PACKAGE=$2
if [[ $OONIARCH == armv7 ]]; then
GOARCH=arm
GOARM=7
DOCKER_ARCH=arm/v7
elif [[ $OONIARCH == armv6 ]]; then
GOARCH=arm
GOARM=6
DOCKER_ARCH=arm/v6
else
GOARCH=$OONIARCH
GOARM=
DOCKER_ARCH=$OONIARCH
fi
if [[ -f ./internal/engine/psiphon-config.json.age &&
-f ./internal/engine/psiphon-config.key ]]; then
OONI_PSIPHON_TAGS=ooni_psiphon_config
else
OONI_PSIPHON_TAGS=""
fi
PRODUCT=$(basename $PACKAGE)
set -x
docker pull --platform linux/$DOCKER_ARCH $GOLANG_DOCKER_IMAGE
docker run --platform linux/$DOCKER_ARCH -e PRODUCT=$PRODUCT \
-e GOARM=$GOARM -e GOOS=$GOOS -e GOARCH=$GOARCH \
-e PACKAGE=$PACKAGE -e OONI_PSIPHON_TAGS=$OONI_PSIPHON_TAGS \
-e OONIARCH=$OONIARCH -e GOLANG_EXTRA_FLAGS="${GOLANG_EXTRA_FLAGS:-}" \
-v $(pwd):/ooni -w /ooni $GOLANG_DOCKER_IMAGE ./CLI/go-build-alpine
+55
View File
@@ -0,0 +1,55 @@
#!/bin/bash
set -euo pipefail
if [[ $# -ne 2 ]]; then
echo "" 1>&2
echo "Compiler for a Go PACKAGE producing windows/GOARCH binaries." 1>&2
echo "" 1>&2
echo "usage: $0 GOARCH PACKAGE" 1>&2
echo "" 1>&2
echo "GOARCH must be one of: 386, amd64." 1>&2
echo "" 1>&2
echo "Features:" 1>&2
echo "" 1>&2
echo "* automatically sets -tags=ooni_psiphon_config when possible;" 1>&2
echo "" 1>&2
echo "* if GOLANG_EXTRA_FLAGS is set, pass it to the Go compiler." 1>&2
echo "" 1>&2
echo "Example:" 1>&2
echo "" 1>&2
echo " ./CLI/go-build-windows amd64 ./internal/cmd/miniooni" 1>&2
echo "" 1>&2
exit 1
fi
GOOS=windows
GOARCH=$1
PACKAGE=$2
if [[ -f ./internal/engine/psiphon-config.json.age &&
-f ./internal/engine/psiphon-config.key ]]; then
OONI_PSIPHON_TAGS=ooni_psiphon_config
else
OONI_PSIPHON_TAGS=""
fi
PRODUCT=$(basename $PACKAGE)
if [[ $GOARCH == "amd64" ]]; then
CC=x86_64-w64-mingw32-gcc
elif [[ $GOARCH == "386" ]]; then
CC=i686-w64-mingw32-gcc
else
echo "FATAL: unsupported GOARCH: $GOARCH" 1>&2
exit 1
fi
set -x
export CC=$CC
export CGO_ENABLED=1
export GOOS=$GOOS
export GOARCH=$GOARCH
go build -tags=$OONI_PSIPHON_TAGS -ldflags="-s -w" \
-o ./CLI/$PRODUCT-$GOOS-$GOARCH ${GOLANG_EXTRA_FLAGS:-} \
$PACKAGE