From bb0b4db228900df56b1507d09491432ba31ff7ef Mon Sep 17 00:00:00 2001 From: Joakim Roubert Date: Wed, 25 Mar 2020 20:35:07 +0100 Subject: [PATCH] Let variable handle go binary name in scripts (#1532) During e.g. cross-compiling, it is sometimes useful to be able to have another binary than the default one as the compiler. This patch for the build scripts lets "go" be the default value for the variable GO, and the latter is then used as the go compiler. Change-Id: I0adf7a661b26593d9b0ea902a61b631b80e76ae7 Signed-off-by: Joakim Roubert --- scripts/build | 16 +++++++++------- scripts/dev-agent.sh | 4 +++- scripts/dev-server.sh | 4 +++- scripts/log-upload | 4 +++- scripts/package-cli | 6 ++++-- scripts/validate | 6 ++++-- scripts/version.sh | 3 ++- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/scripts/build b/scripts/build index 5e41d66630..4ca6333c4a 100755 --- a/scripts/build +++ b/scripts/build @@ -5,6 +5,8 @@ cd $(dirname $0)/.. . ./scripts/version.sh +GO=${GO-go} + PKG="github.com/rancher/k3s" PKG_CONTAINERD="github.com/containerd/containerd" PKG_RANCHER_CONTAINERD="github.com/rancher/containerd" @@ -66,7 +68,7 @@ fi mkdir -p bin -if [ -z "$GOARM" ] && [ "arm" = "$(go env GOARCH)" ]; then +if [ -z "$GOARM" ] && [ "arm" = "$("${GO}" env GOARCH)" ]; then GOARM=7 fi @@ -98,24 +100,24 @@ if [ ! -x ${INSTALLBIN}/cni ]; then WORKDIR=$TMPDIR/src/github.com/containernetworking/plugins git clone -b $VERSION_CNIPLUGINS https://github.com/rancher/plugins.git $WORKDIR cd $WORKDIR - GOPATH=$TMPDIR CGO_ENABLED=0 go build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC" -o $INSTALLBIN/cni + GOPATH=$TMPDIR CGO_ENABLED=0 "${GO}" build -tags "$TAGS" -ldflags "$LDFLAGS $STATIC" -o $INSTALLBIN/cni ) fi # echo Building agent -# CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/k3s-agent ./cmd/agent/main.go +# CGO_ENABLED=1 "${GO}" build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/k3s-agent ./cmd/agent/main.go echo Building server -CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/containerd ./cmd/server/main.go +CGO_ENABLED=1 "${GO}" build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/containerd ./cmd/server/main.go ln -s containerd ./bin/k3s-agent ln -s containerd ./bin/k3s-server ln -s containerd ./bin/kubectl ln -s containerd ./bin/crictl ln -s containerd ./bin/ctr #echo Building hyperkube -#CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/hyperkube ./vendor/k8s.io/kubernetes/cmd/hyperkube/ +#CGO_ENABLED=1 "${GO}" build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/hyperkube ./vendor/k8s.io/kubernetes/cmd/hyperkube/ #echo Building ctr -#CGO_ENABLED=1 go build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/ctr ./cmd/ctr/main.go +#CGO_ENABLED=1 "${GO}" build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC_SQLITE" -o bin/ctr ./cmd/ctr/main.go # echo Building containerd -# CGO_ENABLED=0 go build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/containerd ./cmd/containerd/ +# CGO_ENABLED=0 "${GO}" build -tags "$TAGS" -ldflags "$VERSIONFLAGS $LDFLAGS $STATIC" -o bin/containerd ./cmd/containerd/ echo Building runc rm -f ./vendor/github.com/opencontainers/runc/runc make EXTRA_LDFLAGS="-w -s" BUILDTAGS="$RUNC_TAGS" -C ./vendor/github.com/opencontainers/runc $RUNC_STATIC diff --git a/scripts/dev-agent.sh b/scripts/dev-agent.sh index e860519c7d..1ec0feaf86 100755 --- a/scripts/dev-agent.sh +++ b/scripts/dev-agent.sh @@ -5,6 +5,8 @@ cd $(dirname $0)/.. . ./scripts/setup-rancher-path.sh +GO=${GO-go} + # Prime sudo sudo echo Compiling @@ -13,7 +15,7 @@ if [ ! -e bin/containerd ]; then ./scripts/package else rm -f ./bin/k3s-agent - go build -tags "apparmor seccomp" -o ./bin/k3s-agent ./cmd/agent/main.go + "${GO}" build -tags "apparmor seccomp" -o ./bin/k3s-agent ./cmd/agent/main.go fi echo Starting agent diff --git a/scripts/dev-server.sh b/scripts/dev-server.sh index ba928b21be..a9f8a1ed1d 100755 --- a/scripts/dev-server.sh +++ b/scripts/dev-server.sh @@ -1,6 +1,8 @@ #!/bin/bash set -e +GO=${GO-go} + mkdir -p $(dirname $0)/../bin cd $(dirname $0)/../bin @@ -10,4 +12,4 @@ if echo -- "$@" | grep -q rootless; then ARGS="" PATH=$(pwd):$PATH fi -go run -tags "apparmor" ../main.go server $ARGS "$@" +"${GO}" run -tags "apparmor" ../main.go server $ARGS "$@" diff --git a/scripts/log-upload b/scripts/log-upload index 10589ac5f7..8db501e3f6 100755 --- a/scripts/log-upload +++ b/scripts/log-upload @@ -7,6 +7,8 @@ umask 077 +GO=${GO-go} + TMPDIR=$(mktemp -d) cleanup() { exit_code=$? @@ -40,7 +42,7 @@ END HOME=${TMPDIR} PATH=$PATH:${HOME}/gsutil -LOG_TGZ=k3s-log-$(date +%s)-$(go env GOARCH)-$(git rev-parse --short HEAD)-$(basename $1).tgz +LOG_TGZ=k3s-log-$(date +%s)-$("${GO}" env GOARCH)-$(git rev-parse --short HEAD)-$(basename $1).tgz tar -cz -f ${TMPDIR}/${LOG_TGZ} -C $(dirname $1) $(basename $1) gsutil cp ${TMPDIR}/${LOG_TGZ} gs://k3s-ci-logs || exit 1 diff --git a/scripts/package-cli b/scripts/package-cli index 4c66ce32df..4ca3a6f27f 100755 --- a/scripts/package-cli +++ b/scripts/package-cli @@ -5,6 +5,8 @@ cd $(dirname $0)/.. . ./scripts/version.sh +GO=${GO-go} + rm -rf bin/crictl bin/kubectl bin/k3s-agent bin/k3s-server bin/kubectl bin/k3s build/data ln -s containerd bin/k3s-agent ln -s containerd bin/k3s-server @@ -49,7 +51,7 @@ fi CMD_NAME=dist/artifacts/k3s${BIN_SUFFIX} -go generate +"${GO}" generate LDFLAGS=" -X github.com/rancher/k3s/pkg/version.Version=$VERSION -X github.com/rancher/k3s/pkg/version.GitCommit=${COMMIT:0:8} @@ -59,6 +61,6 @@ STATIC="-extldflags '-static'" if [ "$DQLITE" = "true" ]; then DQLITE_TAGS="dqlite" fi -CGO_ENABLED=0 go build -tags "$DQLITE_TAGS" -ldflags "$LDFLAGS $STATIC" -o ${CMD_NAME} ./cmd/k3s/main.go +CGO_ENABLED=0 "${GO}" build -tags "$DQLITE_TAGS" -ldflags "$LDFLAGS $STATIC" -o ${CMD_NAME} ./cmd/k3s/main.go ./scripts/build-upload ${CMD_NAME} ${COMMIT} diff --git a/scripts/validate b/scripts/validate index f0d75057fe..6b5644513f 100755 --- a/scripts/validate +++ b/scripts/validate @@ -3,6 +3,8 @@ set -e cd $(dirname $0)/.. +GO=${GO-go} + if ! command -v golangci-lint; then echo Skipping validation: no golangci-lint available exit @@ -13,8 +15,8 @@ if [ ! -e build/data ];then mkdir -p build/data fi -echo Running: go generate -go generate +echo Running: "${GO}" generate +"${GO}" generate echo Running: golangci-lint golangci-lint run diff --git a/scripts/version.sh b/scripts/version.sh index 1ff94f4c71..c6ea2d6896 100755 --- a/scripts/version.sh +++ b/scripts/version.sh @@ -1,6 +1,7 @@ #!/bin/bash -ARCH=${ARCH:-$(go env GOARCH)} +GO=${GO-go} +ARCH=${ARCH:-$("${GO}" env GOARCH)} SUFFIX="-${ARCH}" GIT_TAG=$DRONE_TAG TREE_STATE=clean