From 4cf22e75c409b1d44e89134ba562bb6493d2031e Mon Sep 17 00:00:00 2001 From: Siegfried Siegert Date: Fri, 4 Sep 2020 12:44:54 +0200 Subject: [PATCH] Update build script to use command line options --- build.sh | 77 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/build.sh b/build.sh index a5a0ac9..1f7a6af 100755 --- a/build.sh +++ b/build.sh @@ -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` [] [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}"