Skip to content

Instantly share code, notes, and snippets.

@superstes
Last active December 4, 2023 09:43
Show Gist options
  • Save superstes/a3f69a710295edaad444bf36716b45b9 to your computer and use it in GitHub Desktop.
Save superstes/a3f69a710295edaad444bf36716b45b9 to your computer and use it in GitHub Desktop.
Golang - script to compile binaries for many target systems
#!/bin/bash
set -euo pipefail
PROJECT_NAME='test'
# inside '/scripts/' directory
# change to directory from where we can run 'go build'
cd "$(dirname "$0")/../main"
mkdir -p "../build"
rm ../build/*
function compile() {
os="$1" arch="$2"
echo "COMPILING BINARY FOR ${os}-${arch}"
GOOS="$os" GOARCH="$arch" go build -o "../build/${PROJECT_NAME}-${os}-${arch}"
GOOS="$os" GOARCH="$arch" CGO_ENABLED=0 go build -o "../build/${PROJECT_NAME}-${os}-${arch}-CGO0"
if [[ "$os" == "windows" ]]
then
zip "../build/${PROJECT_NAME}-${os}-${arch}.zip" "../build/${PROJECT_NAME}-${os}-${arch}"
zip "../build/${PROJECT_NAME}-${os}-${arch}-CGO0.zip" "../build/${PROJECT_NAME}-${os}-${arch}-CGO0"
else
tar -czf "../build/${PROJECT_NAME}-${os}-${arch}.tar.gz" "../build/${PROJECT_NAME}-${os}-${arch}"
tar -czf "../build/${PROJECT_NAME}-${os}-${arch}-CGO0.tar.gz" "../build/${PROJECT_NAME}-${os}-${arch}-CGO0"
fi
}
compile "linux" "386"
compile "linux" "amd64"
compile "linux" "arm"
compile "linux" "arm64"
compile "freebsd" "386"
compile "freebsd" "amd64"
compile "freebsd" "arm"
compile "openbsd" "386"
compile "openbsd" "amd64"
compile "openbsd" "arm"
compile "darwin" "amd64"
compile "darwin" "arm64"
compile "windows" "386"
compile "windows" "amd64"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment