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 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #!/usr/bin/env bash
  2. # Copyright © 2023 OpenIM. All rights reserved.
  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. # shellcheck disable=SC2034 # Variables sourced in other scripts.
  16. # The server platform we are building on.
  17. readonly OPENIM_SUPPORTED_SERVER_PLATFORMS=(
  18. linux_s390x
  19. linux_mips64
  20. linux_mips64le
  21. darwin_amd64
  22. windows_amd64
  23. linux_amd64
  24. linux_arm64
  25. )
  26. # If we update this we should also update the set of platforms whose standard
  27. # library is precompiled for in build/build-image/cross/Dockerfile
  28. readonly OPENIM_SUPPORTED_CLIENT_PLATFORMS=(
  29. linux_s390x
  30. linux_mips64
  31. linux_mips64le
  32. darwin_amd64
  33. windows_amd64
  34. linux_amd64
  35. linux_arm64
  36. )
  37. # The set of server targets that we are only building for Linux
  38. # If you update this list, please also update build/BUILD.
  39. openim::golang::server_targets() {
  40. local targets=(
  41. openim-apiserver
  42. openim-authz-server
  43. openim-pump
  44. openim-watcher
  45. )
  46. echo "${targets[@]}"
  47. }
  48. IFS=" " read -ra OPENIM_SERVER_TARGETS <<< "$(openim::golang::server_targets)"
  49. readonly OPENIM_SERVER_TARGETS
  50. readonly OPENIM_SERVER_BINARIES=("${OPENIM_SERVER_TARGETS[@]##*/}")
  51. # The set of server targets we build docker images for
  52. openim::golang::server_image_targets() {
  53. # NOTE: this contains cmd targets for openim::build::get_docker_wrapped_binaries
  54. local targets=(
  55. cmd/openim-apiserver
  56. cmd/openim-authz-server
  57. cmd/openim-pump
  58. cmd/openim-watcher
  59. )
  60. echo "${targets[@]}"
  61. }
  62. IFS=" " read -ra OPENIM_SERVER_IMAGE_TARGETS <<< "$(openim::golang::server_image_targets)"
  63. readonly OPENIM_SERVER_IMAGE_TARGETS
  64. readonly OPENIM_SERVER_IMAGE_BINARIES=("${OPENIM_SERVER_IMAGE_TARGETS[@]##*/}")
  65. # ------------
  66. # NOTE: All functions that return lists should use newlines.
  67. # bash functions can't return arrays, and spaces are tricky, so newline
  68. # separators are the preferred pattern.
  69. # To transform a string of newline-separated items to an array, use openim::util::read-array:
  70. # openim::util::read-array FOO < <(openim::golang::dups a b c a)
  71. #
  72. # ALWAYS remember to quote your subshells. Not doing so will break in
  73. # bash 4.3, and potentially cause other issues.
  74. # ------------
  75. # Returns a sorted newline-separated list containing only duplicated items.
  76. openim::golang::dups() {
  77. # We use printf to insert newlines, which are required by sort.
  78. printf "%s\n" "$@" | sort | uniq -d
  79. }
  80. # Returns a sorted newline-separated list with duplicated items removed.
  81. openim::golang::dedup() {
  82. # We use printf to insert newlines, which are required by sort.
  83. printf "%s\n" "$@" | sort -u
  84. }
  85. # Depends on values of user-facing OPENIM_BUILD_PLATFORMS, OPENIM_FASTBUILD,
  86. # and OPENIM_BUILDER_OS.
  87. # Configures OPENIM_SERVER_PLATFORMS and OPENIM_CLIENT_PLATFORMS, then sets them
  88. # to readonly.
  89. # The configured vars will only contain platforms allowed by the
  90. # OPENIM_SUPPORTED* vars at the top of this file.
  91. declare -a OPENIM_SERVER_PLATFORMS
  92. declare -a OPENIM_CLIENT_PLATFORMS
  93. openim::golang::setup_platforms() {
  94. if [[ -n "${OPENIM_BUILD_PLATFORMS:-}" ]]; then
  95. # OPENIM_BUILD_PLATFORMS needs to be read into an array before the next
  96. # step, or quoting treats it all as one element.
  97. local -a platforms
  98. IFS=" " read -ra platforms <<< "${OPENIM_BUILD_PLATFORMS}"
  99. # Deduplicate to ensure the intersection trick with openim::golang::dups
  100. # is not defeated by duplicates in user input.
  101. openim::util::read-array platforms < <(openim::golang::dedup "${platforms[@]}")
  102. # Use openim::golang::dups to restrict the builds to the platforms in
  103. # OPENIM_SUPPORTED_*_PLATFORMS. Items should only appear at most once in each
  104. # set, so if they appear twice after the merge they are in the intersection.
  105. openim::util::read-array OPENIM_SERVER_PLATFORMS < <(openim::golang::dups \
  106. "${platforms[@]}" \
  107. "${OPENIM_SUPPORTED_SERVER_PLATFORMS[@]}" \
  108. )
  109. readonly OPENIM_SERVER_PLATFORMS
  110. openim::util::read-array OPENIM_CLIENT_PLATFORMS < <(openim::golang::dups \
  111. "${platforms[@]}" \
  112. "${OPENIM_SUPPORTED_CLIENT_PLATFORMS[@]}" \
  113. )
  114. readonly OPENIM_CLIENT_PLATFORMS
  115. elif [[ "${OPENIM_FASTBUILD:-}" == "true" ]]; then
  116. OPENIM_SERVER_PLATFORMS=(linux/amd64)
  117. readonly OPENIM_SERVER_PLATFORMS
  118. OPENIM_CLIENT_PLATFORMS=(linux/amd64)
  119. readonly OPENIM_CLIENT_PLATFORMS
  120. else
  121. OPENIM_SERVER_PLATFORMS=("${OPENIM_SUPPORTED_SERVER_PLATFORMS[@]}")
  122. readonly OPENIM_SERVER_PLATFORMS
  123. OPENIM_CLIENT_PLATFORMS=("${OPENIM_SUPPORTED_CLIENT_PLATFORMS[@]}")
  124. readonly OPENIM_CLIENT_PLATFORMS
  125. fi
  126. }
  127. openim::golang::setup_platforms
  128. # The set of client targets that we are building for all platforms
  129. # If you update this list, please also update build/BUILD.
  130. readonly OPENIM_CLIENT_TARGETS=(
  131. iamctl
  132. )
  133. readonly OPENIM_CLIENT_BINARIES=("${OPENIM_CLIENT_TARGETS[@]##*/}")
  134. readonly OPENIM_ALL_TARGETS=(
  135. "${OPENIM_SERVER_TARGETS[@]}"
  136. "${OPENIM_CLIENT_TARGETS[@]}"
  137. )
  138. readonly OPENIM_ALL_BINARIES=("${OPENIM_ALL_TARGETS[@]##*/}")
  139. # Asks golang what it thinks the host platform is. The go tool chain does some
  140. # slightly different things when the target platform matches the host platform.
  141. openim::golang::host_platform() {
  142. echo "$(go env GOHOSTOS)/$(go env GOHOSTARCH)"
  143. }
  144. # Ensure the go tool exists and is a viable version.
  145. openim::golang::verify_go_version() {
  146. if [[ -z "$(command -v go)" ]]; then
  147. openim::log::usage_from_stdin <<EOF
  148. Can't find 'go' in PATH, please fix and retry.
  149. See http://golang.org/doc/install for installation instructions.
  150. EOF
  151. return 2
  152. fi
  153. local go_version
  154. IFS=" " read -ra go_version <<< "$(go version)"
  155. local minimum_go_version
  156. # TODO: Define minimum go version 1.18
  157. minimum_go_version=go1.18.0
  158. 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
  159. openim::log::usage_from_stdin <<EOF
  160. Detected go version: ${go_version[*]}.
  161. OpenIM requires ${minimum_go_version} or greater.
  162. Please install ${minimum_go_version} or later.
  163. EOF
  164. return 2
  165. fi
  166. }
  167. # openim::golang::setup_env will check that the `go` commands is available in
  168. # ${PATH}. It will also check that the Go version is good enough for the
  169. # OpenIM build.
  170. #
  171. # Outputs:
  172. # env-var GOBIN is unset (we want binaries in a predictable place)
  173. # env-var GO15VENDOREXPERIMENT=1
  174. # env-var GO111MODULE=on
  175. openim::golang::setup_env() {
  176. openim::golang::verify_go_version
  177. # Unset GOBIN in case it already exists in the current session.
  178. unset GOBIN
  179. # This seems to matter to some tools
  180. export GO15VENDOREXPERIMENT=1
  181. # Open go module feature
  182. export GO111MODULE=on
  183. # This is for sanity. Without it, user umasks leak through into release
  184. # artifacts.
  185. umask 0022
  186. }