Last active
December 25, 2023 16:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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