Skip to content

Instantly share code, notes, and snippets.

@intermernet
Created December 29, 2016 02:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intermernet/2e60c9179961facad9e2f87740ae0877 to your computer and use it in GitHub Desktop.
Save intermernet/2e60c9179961facad9e2f87740ae0877 to your computer and use it in GitHub Desktop.
Cross compile Go project and create archive files for executables
#!/bin/bash
# Set project and executable name
PROJ="github.com/Intermernet/ssws"
NAME=${PROJ##*/}
# Set OS and Architecture lists
OSLIST="windows linux darwin"
ARCHLIST="x86 amd64"
# Set Base, Working and Output directories
BASEDIR="/home/mh"
OUT="${BASEDIR}/out"
WORK="${BASEDIR}/work"
# Clean up old build artifacts
if [ -d ${OUT} ]
then
rm ${OUT}/*
rmdir ${OUT}
fi
# Create Work and Output directories
mkdir ${OUT}
mkdir ${WORK}
cd ${WORK}
for OS in ${OSLIST}
do
for ARCH in ${ARCHLIST}
do
if [ ${OS} == "windows" ] # Windows specific values
then
EXT=".exe"
ZIP="zip"
ZEXT=".zip"
else # All other OS types
EXT=""
ZIP="tar cfz"
ZEXT=".tgz"
fi
# Build the executable
(export GOOS=${OS}; export GARCH=${ARCH}; go build -o ${WORK}/${NAME}${EXT} ${PROJ})
# Create the archive
$ZIP ${OUT}/${NAME}_${OS}_${ARCH}${ZEXT} *
# Delete the executable
rm ${WORK}/*
done
done
# Remove Work directory
rmdir ${WORK}
# List output files
ls -l ${OUT}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment