istio.io/istio@v0.0.0-20240520182934-d79c90f27776/common/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 VERBOSE=${VERBOSE:-"0"} 27 V="" 28 if [[ "${VERBOSE}" == "1" ]];then 29 V="-x" 30 set -x 31 fi 32 33 SCRIPTPATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 34 35 OUT=${1:?"output path"} 36 shift 37 38 set -e 39 40 export BUILD_GOOS=${GOOS:-linux} 41 export BUILD_GOARCH=${GOARCH:-amd64} 42 GOBINARY=${GOBINARY:-go} 43 GOPKG="$GOPATH/pkg" 44 BUILDINFO=${BUILDINFO:-""} 45 STATIC=${STATIC:-1} 46 LDFLAGS=${LDFLAGS:--extldflags -static} 47 GOBUILDFLAGS=${GOBUILDFLAGS:-""} 48 # Split GOBUILDFLAGS by spaces into an array called GOBUILDFLAGS_ARRAY. 49 IFS=' ' read -r -a GOBUILDFLAGS_ARRAY <<< "$GOBUILDFLAGS" 50 51 GCFLAGS=${GCFLAGS:-} 52 export CGO_ENABLED=${CGO_ENABLED:-0} 53 54 if [[ "${STATIC}" != "1" ]];then 55 LDFLAGS="" 56 fi 57 58 # gather buildinfo if not already provided 59 # For a release build BUILDINFO should be produced 60 # at the beginning of the build and used throughout 61 if [[ -z ${BUILDINFO} ]];then 62 BUILDINFO=$(mktemp) 63 "${SCRIPTPATH}/report_build_info.sh" > "${BUILDINFO}" 64 fi 65 66 # BUILD LD_EXTRAFLAGS 67 LD_EXTRAFLAGS="" 68 69 while read -r line; do 70 LD_EXTRAFLAGS="${LD_EXTRAFLAGS} -X ${line}" 71 done < "${BUILDINFO}" 72 73 OPTIMIZATION_FLAGS=(-trimpath) 74 if [ "${DEBUG}" == "1" ]; then 75 OPTIMIZATION_FLAGS=() 76 fi 77 78 time GOOS=${BUILD_GOOS} GOARCH=${BUILD_GOARCH} ${GOBINARY} build \ 79 ${V} "${GOBUILDFLAGS_ARRAY[@]}" ${GCFLAGS:+-gcflags "${GCFLAGS}"} \ 80 -o "${OUT}" \ 81 "${OPTIMIZATION_FLAGS[@]}" \ 82 -pkgdir="${GOPKG}/${BUILD_GOOS}_${BUILD_GOARCH}" \ 83 -ldflags "${LDFLAGS} ${LD_EXTRAFLAGS}" "${@}"