Skip to content

Instantly share code, notes, and snippets.

@jmervine
Last active December 25, 2023 16:54
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmervine/7d3f455e923cf2ac3c9e to your computer and use it in GitHub Desktop.
Save jmervine/7d3f455e923cf2ac3c9e to your computer and use it in GitHub Desktop.
Scripts to setup golang cross-compiling and build an application on all setup platforms. Tested on: Linux 3.8.0-36-generic #52~precise1-Ubuntu SMP x86_64
#!/bin/bash
#
# usage: ./golang-crosscompile-build.bash /path/to/target.go
# argument handling
test "$1" && target="$1" # .go file to build
if ! test "$target"
then
echo "target file required"
exit 1
fi
binary="" # default to default
test "$2" && binary="$2" # binary output
# find available build types
platforms=`ls $(go env GOROOT)/pkg | grep -v "obj\|tool\|race"`
if ! test "$platforms"; then
echo "no valid os/arch pairs were found to build"
echo "- see: https://gist.github.com/jmervine/7d3f455e923cf2ac3c9e#file-golang-crosscompile-setup-bash"
exit 1
fi
for platform in ${platforms}
do
split=(${platform//_/ })
goos=${split[0]}
goarch=${split[1]}
# ensure output file name
output="$binary"
test "$output" || output="$(basename $target | sed 's/\.go//')"
# add exe to windows output
[[ "windows" == "$goos" ]] && output="$output.exe"
# set destination path for binary
destination="$(dirname $target)/builds/$goos/$goarch/$output"
echo "GOOS=$goos GOARCH=$goarch go build -x -o $destination $target"
GOOS=$goos GOARCH=$goarch go build -x -o $destination $target
done
#!/bin/bash
#
# usage:
#
# $ sudo ./golang-crosscompile-setup.bash
goroot=$(go env GOROOT)
platforms="darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 freebsd/arm linux/386 linux/amd64 linux/arm windows/386 windows/amd64"
for platform in ${platforms}
do
split=(${platform//\// })
goos=${split[0]}
goarch=${split[1]}
echo "cd $goroot/src && GOOS=$goos GOARCH=$goarch ./make.bash --no-clean 2>&1"
cd $goroot/src && GOOS=$goos GOARCH=$goarch ./make.bash --no-clean 2>&1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment