github.com/astaxie/beego@v1.12.3/scripts/gobuild.sh (about) 1 #!/bin/bash 2 3 # WARNING: DO NOT EDIT, THIS FILE IS PROBABLY A COPY 4 # 5 # The original version of this file is located in the https://github.com/istio/common-files repo. 6 # If you're looking at this file in a different repo and want to make a change, please go to the 7 # common-files repo, make the change there and check it in. Then come back to this repo and run 8 # "make update-common". 9 10 # Copyright Istio Authors. All Rights Reserved. 11 # 12 # Licensed under the Apache License, Version 2.0 (the "License"); 13 # you may not use this file except in compliance with the License. 14 # You may obtain a copy of the License at 15 # 16 # http://www.apache.org/licenses/LICENSE-2.0 17 # 18 # Unless required by applicable law or agreed to in writing, software 19 # distributed under the License is distributed on an "AS IS" BASIS, 20 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 # See the License for the specific language governing permissions and 22 # limitations under the License. 23 24 # This script builds and version stamps the output 25 26 # adatp to beego 27 28 VERBOSE=${VERBOSE:-"0"} 29 V="" 30 if [[ "${VERBOSE}" == "1" ]];then 31 V="-x" 32 set -x 33 fi 34 35 SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 36 37 OUT=${1:?"output path"} 38 shift 39 40 set -e 41 42 BUILD_GOOS=${GOOS:-linux} 43 BUILD_GOARCH=${GOARCH:-amd64} 44 GOBINARY=${GOBINARY:-go} 45 GOPKG="$GOPATH/pkg" 46 BUILDINFO=${BUILDINFO:-""} 47 STATIC=${STATIC:-1} 48 LDFLAGS=${LDFLAGS:--extldflags -static} 49 GOBUILDFLAGS=${GOBUILDFLAGS:-""} 50 # Split GOBUILDFLAGS by spaces into an array called GOBUILDFLAGS_ARRAY. 51 IFS=' ' read -r -a GOBUILDFLAGS_ARRAY <<< "$GOBUILDFLAGS" 52 53 GCFLAGS=${GCFLAGS:-} 54 export CGO_ENABLED=0 55 56 if [[ "${STATIC}" != "1" ]];then 57 LDFLAGS="" 58 fi 59 60 # gather buildinfo if not already provided 61 # For a release build BUILDINFO should be produced 62 # at the beginning of the build and used throughout 63 if [[ -z ${BUILDINFO} ]];then 64 BUILDINFO=$(mktemp) 65 "${SCRIPTPATH}/report_build_info.sh" > "${BUILDINFO}" 66 fi 67 68 69 # BUILD LD_EXTRAFLAGS 70 LD_EXTRAFLAGS="" 71 72 while read -r line; do 73 LD_EXTRAFLAGS="${LD_EXTRAFLAGS} -X ${line}" 74 done < "${BUILDINFO}" 75 76 # verify go version before build 77 # NB. this was copied verbatim from Kubernetes hack 78 minimum_go_version=go1.13 # supported patterns: go1.x, go1.x.x (x should be a number) 79 IFS=" " read -ra go_version <<< "$(${GOBINARY} version)" 80 if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then 81 echo "Warning: Detected that you are using an older version of the Go compiler. Beego requires ${minimum_go_version} or greater." 82 fi 83 84 CURRENT_BRANCH=$(git branch | grep '*') 85 CURRENT_BRANCH=${CURRENT_BRANCH:2} 86 87 BUILD_TIME=$(date +%Y-%m-%d--%T) 88 89 LD_EXTRAFLAGS="${LD_EXTRAFLAGS} -X github.com/astaxie/beego.GoVersion=${go_version[2]:2}" 90 LD_EXTRAFLAGS="${LD_EXTRAFLAGS} -X github.com/astaxie/beego.GitBranch=${CURRENT_BRANCH}" 91 LD_EXTRAFLAGS="${LD_EXTRAFLAGS} -X github.com/astaxie/beego.BuildTime=$BUILD_TIME" 92 93 OPTIMIZATION_FLAGS="-trimpath" 94 if [ "${DEBUG}" == "1" ]; then 95 OPTIMIZATION_FLAGS="" 96 fi 97 98 99 100 echo "BUILD_GOARCH: $BUILD_GOARCH" 101 echo "GOPKG: $GOPKG" 102 echo "LD_EXTRAFLAGS: $LD_EXTRAFLAGS" 103 echo "GO_VERSION: ${go_version[2]}" 104 echo "BRANCH: $CURRENT_BRANCH" 105 echo "BUILD_TIME: $BUILD_TIME" 106 107 time GOOS=${BUILD_GOOS} GOARCH=${BUILD_GOARCH} ${GOBINARY} build \ 108 ${V} "${GOBUILDFLAGS_ARRAY[@]}" ${GCFLAGS:+-gcflags "${GCFLAGS}"} \ 109 -o "${OUT}" \ 110 ${OPTIMIZATION_FLAGS} \ 111 -pkgdir="${GOPKG}/${BUILD_GOOS}_${BUILD_GOARCH}" \ 112 -ldflags "${LDFLAGS} ${LD_EXTRAFLAGS}" "${@}"