.travis-install.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!/bin/bash -e
  2. # Copyright 2017, gRPC Authors 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. #
  16. #
  17. # Install dependencies that aren't available as Ubuntu packages (or already present on macOS).
  18. #
  19. # Everything goes into $HOME/local.
  20. #
  21. # Scripts should add
  22. # - $HOME/local/bin to PATH
  23. # - $HOME/local/lib to LD_LIBRARY_PATH
  24. #
  25. # To speed up the CI we cache the interop test server binaries. We cache the
  26. # binaries with the gRPC version appended to their name as a means of
  27. # invalidating the cache when we bump versions.
  28. # Update .travis.yml if this changes.
  29. BIN_CACHE="$HOME"/bin_cache
  30. ZIP_CACHE="$HOME"/zip_cache
  31. PROTOBUF_VERSION=3.9.1
  32. # We need this to build gRPC C++ for the interop test server(s).
  33. BAZEL_VERSION=0.28.1
  34. GRPC_VERSION=1.23.0
  35. SWIFTFORMAT_VERSION=0.44.13
  36. info() {
  37. printf '\033[0;34m%s\033[0m\n' "$1"
  38. }
  39. success() {
  40. printf '\033[0;32m%s\033[0m\n' "$1"
  41. }
  42. # Install the protoc compiler.
  43. install_protoc() {
  44. echo -en 'travis_fold:start:install.protoc\\r'
  45. info "Installing protoc $PROTOBUF_VERSION"
  46. # Which protoc are we using?
  47. if [ "$TRAVIS_OS_NAME" = "osx" ]; then
  48. PROTOC_ZIP=protoc-${PROTOBUF_VERSION}-osx-x86_64.zip
  49. else
  50. PROTOC_ZIP=protoc-${PROTOBUF_VERSION}-linux-x86_64.zip
  51. fi
  52. CACHED_PROTOC_ZIP="$ZIP_CACHE/$PROTOC_ZIP"
  53. # Is it cached?
  54. if [ ! -f "$CACHED_PROTOC_ZIP" ]; then
  55. # No: download it.
  56. PROTOC_URL=https://github.com/google/protobuf/releases/download/v${PROTOBUF_VERSION}/${PROTOC_ZIP}
  57. info "Downloading protoc from: $PROTOC_URL"
  58. if curl -fSsL "$PROTOC_URL" -o "$CACHED_PROTOC_ZIP"; then
  59. info "Downloaded protoc from: $PROTOC_URL"
  60. else
  61. info "Downloading protoc failed, removing artifacts"
  62. rm "$CACHED_PROTOC_ZIP"
  63. exit 1
  64. fi
  65. else
  66. info "Using cached protoc"
  67. fi
  68. info "Extracting protoc from $CACHED_PROTOC_ZIP"
  69. unzip -q "$CACHED_PROTOC_ZIP" -d local
  70. success "Installed protoc $PROTOBUF_VERSION"
  71. echo -en 'travis_fold:end:install.protoc\\r'
  72. }
  73. # Install Swift.
  74. install_swift() {
  75. echo -en 'travis_fold:start:install.swift\\r'
  76. # Use the Swift provided by Xcode on macOS.
  77. if [ "$TRAVIS_OS_NAME" != "osx" ]; then
  78. info "Installing Swift $SWIFT_VERSION"
  79. if [ -z "${SWIFT_URL}" ]; then
  80. SWIFT_URL=https://swift.org/builds/swift-${SWIFT_VERSION}-release/ubuntu1804/swift-${SWIFT_VERSION}-RELEASE/swift-${SWIFT_VERSION}-RELEASE-ubuntu18.04.tar.gz
  81. fi
  82. info "Downloading Swift from $SWIFT_URL"
  83. curl -fSsL "$SWIFT_URL" -o swift.tar.gz
  84. info "Extracting Swift from swift.tar.gz"
  85. tar -xzf swift.tar.gz --strip-components=2 --directory=local
  86. success "Installed Swift $SWIFT_VERSION"
  87. else
  88. info "Skipping Swift installation: using Swift provided by Xcode"
  89. fi
  90. echo -en 'travis_fold:end:install.swift\\r'
  91. }
  92. # We need to install bazel to so we can build the gRPC interop test server.
  93. install_bazel() {
  94. echo -en 'travis_fold:start:install.bazel\\r'
  95. info "Installing Bazel $BAZEL_VERSION"
  96. # See:
  97. # - https://docs.bazel.build/versions/master/install-os-x.html
  98. # - https://docs.bazel.build/versions/master/install-ubuntu.html
  99. if [ "$TRAVIS_OS_NAME" = "osx" ]; then
  100. BAZEL_URL=https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-darwin-x86_64.sh
  101. else
  102. BAZEL_URL=https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh
  103. fi
  104. info "Downloading Bazel from: $BAZEL_URL"
  105. curl -fSsL $BAZEL_URL -o bazel-installer.sh
  106. chmod +x bazel-installer.sh
  107. info "Running ./bazel-installer.sh"
  108. ./bazel-installer.sh --prefix="$HOME/local"
  109. success "Installed Bazel"
  110. echo -en 'travis_fold:end:install.bazel\\r'
  111. }
  112. # Build the gRPC C++ interop test server and reconnect interop test server.
  113. build_grpc_cpp_server() {
  114. echo -en 'travis_fold:start:install.grpc_cpp_server\\r'
  115. info "Building gRPC $GRPC_VERSION C++ interop servers"
  116. GRPC_INTEROP_SERVER=interop_server-"$GRPC_VERSION"
  117. GRPC_RECONNECT_INTEROP_SERVER=reconnect_interop_server-"$GRPC_VERSION"
  118. # If the servers don't exist: download and build them.
  119. if [ ! -f "$BIN_CACHE/$GRPC_INTEROP_SERVER" ] || [ ! -f "$BIN_CACHE/$GRPC_RECONNECT_INTEROP_SERVER" ]; then
  120. GRPC_URL=https://github.com/grpc/grpc/archive/v${GRPC_VERSION}.tar.gz
  121. info "Downloading gRPC from: $GRPC_URL"
  122. curl -fSsL $GRPC_URL -o grpc.tar.gz
  123. # Extract it to grpc
  124. mkdir grpc
  125. info "Extracting grpc.tar.gz to grpc"
  126. tar -xzf grpc.tar.gz --strip-components=1 --directory=grpc
  127. # Build the interop servers and put them in $BIN_CACHE
  128. (
  129. cd grpc
  130. # Only update progress every second to avoid spamming the logs.
  131. "$HOME"/local/bin/bazel build \
  132. --show_progress_rate_limit=1 \
  133. test/cpp/interop:interop_server \
  134. test/cpp/interop:reconnect_interop_server
  135. # Put them in the $BIN_CACHE
  136. info "Copying interop server to $BIN_CACHE/$GRPC_INTEROP_SERVER"
  137. cp ./bazel-bin/test/cpp/interop/interop_server "$BIN_CACHE/$GRPC_INTEROP_SERVER"
  138. info "Copying interop reconnect server to $BIN_CACHE/$GRPC_RECONNECT_INTEROP_SERVER"
  139. cp ./bazel-bin/test/cpp/interop/reconnect_interop_server "$BIN_CACHE/$GRPC_RECONNECT_INTEROP_SERVER"
  140. )
  141. else
  142. info "Skipping download and build of gRPC C++, using cached binaries"
  143. fi
  144. # We should have cached servers now, copy them to $HOME/local/bin
  145. cp "$BIN_CACHE/$GRPC_INTEROP_SERVER" "$HOME"/local/bin/interop_server
  146. cp "$BIN_CACHE/$GRPC_RECONNECT_INTEROP_SERVER" "$HOME"/local/bin/reconnect_interop_server
  147. success "Copied gRPC interop servers"
  148. echo -en 'travis_fold:end:install.grpc_cpp_server\\r'
  149. }
  150. function install_swiftformat() {
  151. echo -en 'travis_fold:start:install.swiftformat\\r'
  152. info "Installing swiftformat"
  153. if [ ! -f "$BIN_CACHE/swiftformat-$SWIFTFORMAT_VERSION" ]; then
  154. git clone -b "$SWIFTFORMAT_VERSION" --depth 1 "https://github.com/nicklockwood/SwiftFormat"
  155. info "Building swiftformat"
  156. cd SwiftFormat
  157. swift build --product swiftformat
  158. cp "$(swift build --show-bin-path)/swiftformat" "$BIN_CACHE/swiftformat-$SWIFTFORMAT_VERSION"
  159. else
  160. info "Skipping download and build of SwiftFormat, using cached binaries"
  161. fi
  162. # We should have cached swiftformat now, copy it to $HOME/local/bin
  163. cp "$BIN_CACHE/swiftformat-$SWIFTFORMAT_VERSION" "$HOME"/local/bin/swiftformat
  164. success "Installed swiftformat"
  165. echo -en 'travis_fold:end:install.swiftformat\\r'
  166. }
  167. cd
  168. mkdir -p local/bin "$BIN_CACHE" "$ZIP_CACHE"
  169. should_install_protoc=false
  170. should_install_interop_server=false
  171. should_install_swiftformat=false
  172. while getopts "pif" optname; do
  173. case $optname in
  174. p)
  175. should_install_protoc=true
  176. ;;
  177. i)
  178. should_install_interop_server=true
  179. ;;
  180. f)
  181. should_install_swiftformat=true
  182. ;;
  183. \?)
  184. echo "Uknown option $optname"
  185. exit 2
  186. ;;
  187. esac
  188. done
  189. # Always install Swift.
  190. install_swift
  191. if $should_install_protoc; then
  192. install_protoc
  193. fi
  194. if $should_install_interop_server; then
  195. install_bazel
  196. build_grpc_cpp_server
  197. fi
  198. if $should_install_swiftformat; then
  199. # If we're building SwiftFormat we need to know where Swift lives.
  200. export PATH=$HOME/local/bin:$PATH
  201. install_swiftformat
  202. fi
  203. # Verify installation
  204. info "Contents of $HOME/local:"
  205. find "$HOME"/local
  206. success "Install script completed"