Update build script to use command line options

This commit is contained in:
Siegfried Siegert 2020-09-04 12:44:54 +02:00
parent b9539d6db9
commit 4cf22e75c4

View File

@ -2,15 +2,10 @@
TARGET=mqttListener
# expand PATH for go tools
export PATH=$PATH:~/go/bin
RED='\e[0;31m'
GREEN='\e[0;32m'
LIGHT_YELLOW='\e[93m'
@ -18,6 +13,11 @@ NC='\e[0m' # No Color
BOLD='\e[1m'
NORMAL='\e[21m'
#######################################################################################
# defaults
GO_BUILD_OPTIONS=-v
#GO_BUILD_OPTIONS="-a -v"
DEFAULT_TARGET=amd64
#######################################################################################
@ -25,31 +25,70 @@ NORMAL='\e[21m'
die() { echo -e "\n${RED}$@${NC}" 1>&2 ; exit 1; }
usage() {
echo "usage: `basename $0` [<TARGET>] [all]"
}
#######################################################################################
# build functions
#cd /src
build_linux_amd64() {
echo -e "\nBuilding for Linux amd64:"
env GOOS=linux GOARCH=amd64 go build ${GO_BUILD_OPTIONS} -ldflags "$(govvv -flags)" -o ${TARGET}-linux-amd64
echo -e "\t\t${GREEN}... done${NC}"
}
if ! command -v govvv &> /dev/null ; then
die "govv is not installed"
build_linux_arm() {
echo -e "\nBuilding for arm:"
env GOOS=linux GOARCH=arm go build ${GO_BUILD_OPTIONS} -ldflags "$(govvv -flags)" -o ${TARGET}-linux-arm
echo -e "\t\t${GREEN}... done${NC}"
echo -e "\nstipping arm binary:"
strip -o ${TARGET}-linux-arm-stripped ${TARGET}-linux-arm
echo -e "\t\t${GREEN}... done${NC}"
}
#######################################################################################
# checks
echo -e "${NC}\n "
if [ ! command -v govvv &> /dev/null ] ; then
die "govvv is not installed"
fi
if [ $# -eq 0 ] ; then
echo -e "\nbuilding for default TARGET ${DEFAULT_TARGET}..."
TARGET=${DEFAULT_TARGET}
else
TARGET=$1
fi
#######################################################################################
# build
echo -e "${NC}\n "
echo -e "\nBuilding for Linux 64-bit:"
env GOOS=linux GOARCH=amd64 go build -a -v -ldflags "$(govvv -flags)" -o ${TARGET}-linux-amd64
echo -e "\t\t${GREEN}... done${NC}"
#cd /src
echo -e "\nBuilding for arm:"
env GOOS=linux GOARCH=arm go build -a -v -ldflags "$(govvv -flags)" -o ${TARGET}-linux-arm
echo -e "\t\t${GREEN}... done${NC}"
if [ "$2" == "all" ] ; then
echo -e "\nforce rebuilding of all packages..."
GO_BUILD_OPTIONS="-a -v"
fi
case "${TARGET}" in
"arm"|"ARM")
build_linux_arm
;;
"amd64"|"AMD64")
build_linux_amd64
;;
*)
usage
die "Target ${TARGET} is not defined"
;;
esac
echo -e "\nstipping arm binary:"
strip -o ${TARGET}-linux-arm-stripped ${TARGET}-linux-arm
echo -e "\t\t${GREEN}... done${NC}"