.travis-install.sh 7.7 KB

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