qhiz5g
Last Updated: February 25, 2016
·
505
· catinello
E800f2d442ef5ef3673dfc5cc0fc1315

Cross-Compile Go

Bash Script to Cross-Compile Go on Debian Linux for FreeBSD, MacOS X, Linux or Windows to x86_64, i386 and ARM platforms.

Syntax: build_go.sh /path/to/your/go/project

You need your Go-Tools installed and environment variables (GOROOT, GOPATH) set correctly.

#!/bin/bash

#variables ## look at your exports
#GOROOT=/usr/share/go/src    ## debian specific
#GOPATH=???                  ## depends on your Go location {bin,pkg,src}
PROJECT=$1

ARCH="amd64 arm 386"
OS="freebsd darwin linux windows"

for i in $ARCH; do
        cd $GOROOT
        GOARCH=${i} ./make.bash
        for j in $OS; do
                mkdir -p ${PROJECT}/bin
                cd ${PROJECT}
                if [ "$i" == "386" ]; then
                        GOARCH=${i} GOOS=${j} go build -ldflags "-s" -o bin/$(basename ${PROJECT})-${j}-i${i}
                elif [ "$i" == "amd64" ]; then
                        GOARCH=${i} GOOS=${j} go build -ldflags "-s" -o bin/$(basename ${PROJECT})-${j}-x86_64
                else
                        GOARCH=${i} GOOS=${j} go build -ldflags "-s" -o bin/$(basename ${PROJECT})-${j}-${i}
                fi
        done
done

Example:

$ build_go.sh /home/user/go/src/MyProject

Binary Files are build to /home/user/go/src/MyProject/bin

build_go.sh on github

Say Thanks
Respond