You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

golang.sh 6.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # -----------------------------------------------------------------------------
  16. # CHANGELOG
  17. # KubeEdge Authors:
  18. # To Get Detail Version Info for KubeEdge Project
  19. set -o errexit
  20. set -o nounset
  21. set -o pipefail
  22. YES="y"
  23. NO="n"
  24. sedna::version::get_version_info() {
  25. GIT_COMMIT=$(git rev-parse "HEAD^{commit}" 2>/dev/null)
  26. if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  27. GIT_TREE_STATE="clean"
  28. else
  29. GIT_TREE_STATE="dirty"
  30. fi
  31. GIT_VERSION=$(git describe --tags --abbrev=14 "${GIT_COMMIT}^{commit}" 2>/dev/null)
  32. # This translates the "git describe" to an actual semver.org
  33. # compatible semantic version that looks something like this:
  34. # v1.1.0-alpha.0.6+84c76d1142ea4d
  35. #
  36. # TODO: We continue calling this "git version" because so many
  37. # downstream consumers are expecting it there.
  38. #
  39. # These regexes are painful enough in sed...
  40. # We don't want to do them in pure shell, so disable SC2001
  41. # shellcheck disable=SC2001
  42. DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g")
  43. if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
  44. # shellcheck disable=SC2001
  45. # We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
  46. GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  47. elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
  48. # shellcheck disable=SC2001
  49. # We have distance to base tag (v1.1.0-1-gCommitHash)
  50. GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
  51. fi
  52. if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then
  53. # git describe --dirty only considers changes to existing files, but
  54. # that is problematic since new untracked .go files affect the build,
  55. # so use our idea of "dirty" from git status instead.
  56. GIT_VERSION+="-dirty"
  57. fi
  58. # Try to match the "git describe" output to a regex to try to extract
  59. # the "major" and "minor" versions and whether this is the exact tagged
  60. # version or whether the tree is between two tagged versions.
  61. if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
  62. GIT_MAJOR=${BASH_REMATCH[1]}
  63. GIT_MINOR=${BASH_REMATCH[2]}
  64. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  65. GIT_MINOR+="+"
  66. fi
  67. fi
  68. # uncomment until we have tags
  69. : <<EOF
  70. # If GIT_VERSION is not a valid Semantic Version, then refuse to build.
  71. if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  72. echo "GIT_VERSION should be a valid Semantic Version. Current value: ${GIT_VERSION}" >&2
  73. echo "Please see more details here: https://semver.org" >&2
  74. exit 1
  75. fi
  76. EOF
  77. }
  78. # Get the value that needs to be passed to the -ldflags parameter of go build
  79. sedna::version::ldflags() {
  80. sedna::version::get_version_info > /dev/null
  81. local -a ldflags
  82. function add_ldflag() {
  83. local key=${1}
  84. local val=${2}
  85. # If you update these, also update the list pkg/version/def.bzl.
  86. ldflags+=(
  87. "-X ${SEDNA_GO_PACKAGE}/pkg/version.${key}=${val}"
  88. )
  89. }
  90. add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')"
  91. if [[ -n ${GIT_COMMIT-} ]]; then
  92. add_ldflag "gitCommit" "${GIT_COMMIT}"
  93. add_ldflag "gitTreeState" "${GIT_TREE_STATE}"
  94. fi
  95. if [[ -n ${GIT_VERSION-} ]]; then
  96. add_ldflag "gitVersion" "${GIT_VERSION}"
  97. fi
  98. if [[ -n ${GIT_MAJOR-} && -n ${GIT_MINOR-} ]]; then
  99. add_ldflag "gitMajor" "${GIT_MAJOR}"
  100. add_ldflag "gitMinor" "${GIT_MINOR}"
  101. fi
  102. # The -ldflags parameter takes a single string, so join the output.
  103. echo "${ldflags[*]-}"
  104. }
  105. # sedna::binaries_from_targets take a list of build targets and return the
  106. # full go package to be built
  107. sedna::golang::binaries_from_targets() {
  108. local target
  109. for target in "$@"; do
  110. echo "${SEDNA_GO_PACKAGE}/${target}"
  111. done
  112. }
  113. sedna::check::env() {
  114. errors=()
  115. if [ -z "$GOPATH" ]; then
  116. errors+="GOPATH environment value not set"
  117. fi
  118. # check other env
  119. # check length of errors
  120. if [[ ${#errors[@]} -ne 0 ]] ; then
  121. local error
  122. for error in "${errors[@]}"; do
  123. echo "Error: "$error >&2
  124. done
  125. exit 1
  126. fi
  127. }
  128. ALL_BINARIES_AND_TARGETS=(
  129. gm:cmd/sedna-gm
  130. lc:cmd/sedna-lc
  131. )
  132. sedna::golang::get_target_by_binary() {
  133. local key=$1
  134. for bt in "${ALL_BINARIES_AND_TARGETS[@]}" ; do
  135. local binary="${bt%%:*}"
  136. if [ "${binary}" == "${key}" ]; then
  137. echo "${bt##*:}"
  138. return
  139. fi
  140. done
  141. echo "can not find binary: $key" >&2
  142. exit 1
  143. }
  144. sedna::golang::get_all_targets() {
  145. local -a targets
  146. for bt in "${ALL_BINARIES_AND_TARGETS[@]}" ; do
  147. targets+=("${bt##*:}")
  148. done
  149. echo ${targets[@]}
  150. }
  151. sedna::golang::get_all_binares() {
  152. local -a binares
  153. for bt in "${ALL_BINARIES_AND_TARGETS[@]}" ; do
  154. binares+=("${bt%%:*}")
  155. done
  156. echo ${binares[@]}
  157. }
  158. IFS=" " read -ra SEDNA_ALL_TARGETS <<< "$(sedna::golang::get_all_targets)"
  159. IFS=" " read -ra SEDNA_ALL_BINARIES<<< "$(sedna::golang::get_all_binares)"
  160. sedna::golang::build_binaries() {
  161. sedna::check::env
  162. local -a targets=()
  163. local binArg
  164. for binArg in "$@"; do
  165. targets+=("$(sedna::golang::get_target_by_binary $binArg)")
  166. done
  167. if [[ ${#targets[@]} -eq 0 ]]; then
  168. targets=("${SEDNA_ALL_TARGETS[@]}")
  169. fi
  170. local -a binaries
  171. while IFS="" read -r binary; do binaries+=("$binary"); done < <(sedna::golang::binaries_from_targets "${targets[@]}")
  172. local goldflags gogcflags
  173. # If GOLDFLAGS is unset, then set it to the a default of "-s -w".
  174. goldflags="${GOLDFLAGS=-s -w -buildid=} $(sedna::version::ldflags)"
  175. gogcflags="${GOGCFLAGS:-}"
  176. mkdir -p ${SEDNA_OUT_BINPATH}
  177. for bin in ${binaries[@]}; do
  178. echo "building $bin"
  179. local name="${bin##*/}"
  180. set -x
  181. go build -o ${SEDNA_OUT_BINPATH}/${name} -gcflags="${gogcflags:-}" -ldflags "${goldflags:-}" $bin
  182. set +x
  183. done
  184. }