Skip to content

Instantly share code, notes, and snippets.

@Razikus
Last active August 2, 2024 19:44
Show Gist options
  • Save Razikus/86e78534c64529f1f62bf803ebf495c1 to your computer and use it in GitHub Desktop.
Save Razikus/86e78534c64529f1f62bf803ebf495c1 to your computer and use it in GitHub Desktop.
Golang build for all distros all archs and with static versions
#!/bin/bash
VERSION=${VERSION:-devbuild}
COMMIT=${COMMIT:-$(git rev-parse HEAD | head -c 8)}
BUILDTIME=$(date +%s)
LDFLAGS="-X 'main.VERSION=${VERSION}' -X 'main.BUILDTIME=${BUILDTIME}' -X 'main.COMMIT=${COMMIT}'"
# go tool dist list
platforms=(
"aix/ppc64"
"android/386"
"android/amd64"
"android/arm"
"android/arm64"
"darwin/amd64"
"darwin/arm64"
"dragonfly/amd64"
"freebsd/386"
"freebsd/amd64"
"freebsd/arm"
"freebsd/arm64"
"freebsd/riscv64"
"illumos/amd64"
"ios/amd64"
"ios/arm64"
"js/wasm"
"linux/386"
"linux/amd64"
"linux/arm"
"linux/arm64"
"linux/loong64"
"linux/mips"
"linux/mips64"
"linux/mips64le"
"linux/mipsle"
"linux/ppc64"
"linux/ppc64le"
"linux/riscv64"
"linux/s390x"
"netbsd/386"
"netbsd/amd64"
"netbsd/arm"
"netbsd/arm64"
"openbsd/386"
"openbsd/amd64"
"openbsd/arm"
"openbsd/arm64"
"openbsd/ppc64"
"plan9/386"
"plan9/amd64"
"plan9/arm"
"solaris/amd64"
"wasip1/wasm"
"windows/386"
"windows/amd64"
"windows/arm"
"windows/arm64"
)
OUTPUT=builds/
mkdir -p $OUTPUT
if [ -z $BUILDSTATIC ]; then
BUILDSTATIC=true
fi
if [ -z $TOBUILD ]; then
TOBUILD="."
fi
total_platforms=${#platforms[@]}
current=0
for platform in "${platforms[@]}"; do
current=$((current + 1))
GOOS=$(echo $platform | cut -d'/' -f1)
GOARCH=$(echo $platform | cut -d'/' -f2)
current_platform="$platform"
if [ "$GOOS" = "windows" ]; then
echo "[$current/$total_platforms] Processing exe platform: $current_platform"
GOARCH=$GOARCH GOOS=$GOOS go build -ldflags "${LDFLAGS}" -o "${OUTPUT}/${GOOS}_${GOARCH}.exe" -buildmode=exe $TOBUILD
if [ "$BUILDSTATIC" = true ]; then
echo "[$current/$total_platforms] Processing exe platform, static: $current_platform"
CGO_ENABLED=0 GOARCH=$GOARCH GOOS=$GOOS go build -ldflags "${LDFLAGS}" -o "${OUTPUT}/${GOOS}_${GOARCH}-static.exe" -buildmode=exe $TOBUILD
fi
else
echo "[$current/$total_platforms] Processing platform: $current_platform"
GOARCH=$GOARCH GOOS=$GOOS go build -ldflags "${LDFLAGS}" -o "${OUTPUT}/${GOOS}_${GOARCH}" $TOBUILD
if [ "$BUILDSTATIC" = true ]; then
echo "[$current/$total_platforms] Processing platform, static: $current_platform"
CGO_ENABLED=0 GOARCH=$GOARCH GOOS=$GOOS go build -ldflags "${LDFLAGS}" -o "${OUTPUT}/${GOOS}_${GOARCH}-static" $TOBUILD
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment