From e5a83f4d4f787998ea3fd5a03a347bb8b4753fde Mon Sep 17 00:00:00 2001 From: Iceber Gu Date: Mon, 27 Nov 2023 12:58:45 +0800 Subject: [PATCH] fix build without tags and add building command printing Signed-off-by: Iceber Gu --- hack/builder-nocgo.sh | 4 ++ hack/builder.sh | 5 +++ ldflags.sh | 89 ++++++++++++++++++++++++------------------- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/hack/builder-nocgo.sh b/hack/builder-nocgo.sh index 1c9ed4a12..972064b09 100755 --- a/hack/builder-nocgo.sh +++ b/hack/builder-nocgo.sh @@ -18,8 +18,12 @@ function build_component() { LDFLAGS=${BUILD_LDFLAGS:-""} if [ -f ./ldflags.sh ]; then source ./ldflags.sh + LDFLAGS+=" $(extra_ldflags)" fi + + set -x CGO_ENABLED=0 go build -ldflags "${LDFLAGS}" -o ${OUTPUT_DIR}/bin/$1 ./cmd/$1 + set +x } build_component $1 diff --git a/hack/builder.sh b/hack/builder.sh index bf055e911..de262aa25 100755 --- a/hack/builder.sh +++ b/hack/builder.sh @@ -91,6 +91,7 @@ function build_component() { local LDFLAGS=${BUILD_LDFLAGS:-""} if [ -f ${CLUSTERPEDIA_REPO}/ldflags.sh ]; then cd ${CLUSTERPEDIA_REPO} && source ./ldflags.sh + LDFLAGS+=" $(extra_ldflags)" fi if [[ "${GOOS}" == "linux" ]]; then @@ -98,9 +99,11 @@ function build_component() { LDFLAGS+=" -extldflags -static" fi + set -x cd $TMP_CLUSTERPEDIA GOPATH=$TMP_GOPATH GO111MODULE=off CGO_ENABLED=1 CC_FOR_TARGET=$CC_FOR_TARGET CC=$CC \ go build -tags "json1 $GOOS" -ldflags "${LDFLAGS}" -o $OUTPUT_DIR/bin/$1 ./cmd/$1 + set +x } cleanup @@ -161,9 +164,11 @@ function build_plugin() { cd ${PLUGIN_REPO} && source ./ldflags.sh fi + set -x cd $TMP_PLUGIN GOPATH=$TMP_GOPATH GO111MODULE=off CGO_ENABLED=1 CC_FOR_TARGET=$CC_FOR_TARGET CC=$CC \ go build -ldflags "${LDFLAGS}" -buildmode=plugin -o $OUTPUT_DIR/plugins/$1 + set +x } copy_plugin_repo diff --git a/ldflags.sh b/ldflags.sh index d09c3fd0f..fef7c1e13 100644 --- a/ldflags.sh +++ b/ldflags.sh @@ -1,42 +1,51 @@ #!/usr/bin/env bash -BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') - -# Git information, used to set clusterpedia version -GIT_VERSION=$(git describe --tags --dirty) -GIT_COMMIT_HASH=$(git rev-parse HEAD) - -GIT_TREESTATE="clean" -GIT_DIFF=$(git diff --quiet >/dev/null 2>&1; if [ $? -eq 1 ]; then echo "1"; fi) -if [ "${GIT_DIFF}" == "1" ]; then - GIT_TREESTATE="dirty" -fi - -KUBE_DEPENDENCE_VERSION=$(go list -m k8s.io/kubernetes | cut -d' ' -f2) - -LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitVersion=${GIT_VERSION}" -LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitCommit=${GIT_COMMIT_HASH}" -LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitTreeState=${GIT_TREESTATE}" -LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.buildDate=${BUILD_DATE}" - -# The `client-go/pkg/version` effects the **User-Agent** when using client-go to request. -# User-Agent="/$(GIT_VERSION) ($(GOOS)/$(GOARCH)) kubernetes/$(GIT_COMMIT_HASH)[/]" -# eg. "clustersynchro-manager/0.4.0 (linux/amd64) kubernetes/fbf0f4f/clustersynchro-manager" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitVersion=${GIT_VERSION}" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitMajor=$(echo ${GIT_VERSION#v} | cut -d. -f1)" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitMinor=$(echo ${GIT_VERSION#v} | cut -d. -f2)" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitCommit=${GIT_COMMIT_HASH}" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitTreeState=${GIT_TREESTATE}" -LDFLAGS+=" -X k8s.io/client-go/pkg/version.buildDate=${BUILD_DATE}" - - -# The `component-base/version` effects the version obtained using Kubernetes OpenAPI. -# OpenAPI Path: /apis/clusterpedia.io/v1beta1/resources/version -# $ kubectl version - -LDFLAGS+=" -X k8s.io/component-base/version.gitVersion=${KUBE_DEPENDENCE_VERSION}" -LDFLAGS+=" -X k8s.io/component-base/version.gitMajor=$(echo ${KUBE_DEPENDENCE_VERSION#v} | cut -d. -f1)" -LDFLAGS+=" -X k8s.io/component-base/version.gitMinor=$(echo ${KUBE_DEPENDENCE_VERSION#v} | cut -d. -f2)" -LDFLAGS+=" -X k8s.io/component-base/version.gitCommit=${GIT_COMMIT_HASH}" -LDFLAGS+=" -X k8s.io/component-base/version.gitTreeState=${GIT_TREESTATE}" -LDFLAGS+=" -X k8s.io/component-base/version.buildDate=${BUILD_DATE}" +set -o errexit +set -o nounset +set -o pipefail + +function extra_ldflags() { + BUILD_DATE=$(date -u +'%Y-%m-%dT%H:%M:%SZ') + + # Git information, used to set clusterpedia version + GIT_VERSION=$(git describe --tags --dirty) + GIT_COMMIT_HASH=$(git rev-parse HEAD) + + GIT_TREESTATE="clean" + GIT_DIFF=$(git diff --quiet >/dev/null 2>&1; if [ $? -eq 1 ]; then echo "1"; fi) + if [ "${GIT_DIFF}" == "1" ]; then + GIT_TREESTATE="dirty" + fi + + KUBE_DEPENDENCE_VERSION=$(go list -m k8s.io/kubernetes | cut -d' ' -f2) + + local LDFLAGS + LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitVersion=${GIT_VERSION}" + LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitCommit=${GIT_COMMIT_HASH}" + LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.gitTreeState=${GIT_TREESTATE}" + LDFLAGS+=" -X github.com/clusterpedia-io/clusterpedia/pkg/version.buildDate=${BUILD_DATE}" + + # The `client-go/pkg/version` effects the **User-Agent** when using client-go to request. + # User-Agent="/$(GIT_VERSION) ($(GOOS)/$(GOARCH)) kubernetes/$(GIT_COMMIT_HASH)[/]" + # eg. "clustersynchro-manager/0.4.0 (linux/amd64) kubernetes/fbf0f4f/clustersynchro-manager" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitVersion=${GIT_VERSION}" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitMajor=$(echo ${GIT_VERSION#v} | cut -d. -f1)" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitMinor=$(echo ${GIT_VERSION#v} | cut -d. -f2)" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitCommit=${GIT_COMMIT_HASH}" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.gitTreeState=${GIT_TREESTATE}" + LDFLAGS+=" -X k8s.io/client-go/pkg/version.buildDate=${BUILD_DATE}" + + + # The `component-base/version` effects the version obtained using Kubernetes OpenAPI. + # OpenAPI Path: /apis/clusterpedia.io/v1beta1/resources/version + # $ kubectl version + + LDFLAGS+=" -X k8s.io/component-base/version.gitVersion=${KUBE_DEPENDENCE_VERSION}" + LDFLAGS+=" -X k8s.io/component-base/version.gitMajor=$(echo ${KUBE_DEPENDENCE_VERSION#v} | cut -d. -f1)" + LDFLAGS+=" -X k8s.io/component-base/version.gitMinor=$(echo ${KUBE_DEPENDENCE_VERSION#v} | cut -d. -f2)" + LDFLAGS+=" -X k8s.io/component-base/version.gitCommit=${GIT_COMMIT_HASH}" + LDFLAGS+=" -X k8s.io/component-base/version.gitTreeState=${GIT_TREESTATE}" + LDFLAGS+=" -X k8s.io/component-base/version.buildDate=${BUILD_DATE}" + + echo $LDFLAGS +}