shared.sh 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/env bash
# Copyright 2014 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# ---------------------------------- NOTE ---------------------------------- #
#
# Please keep the logic in this file consistent with the logic in the
# `shared.bat` script in the same directory to ensure that Flutter & Dart continue
# to work across all platforms!
#
# -------------------------------------------------------------------------- #

set -e

16
# Needed because if it is set, cd may print the path it changed to.
17 18
unset CDPATH

19
function pub_upgrade_with_retry {
20 21 22
  local total_tries="10"
  local remaining_tries=$((total_tries - 1))
  while [[ "$remaining_tries" -gt 0 ]]; do
23
    (cd "$FLUTTER_TOOLS_DIR" && "$DART" pub upgrade --suppress-analytics) && break
24
    >&2 echo "Error: Unable to 'pub upgrade' flutter tool. Retrying in five seconds... ($remaining_tries tries left)"
25 26 27 28 29
    remaining_tries=$((remaining_tries - 1))
    sleep 5
  done

  if [[ "$remaining_tries" == 0 ]]; then
30
    >&2 echo "Command 'pub upgrade' still failed after $total_tries tries, giving up."
31 32 33 34 35
    return 1
  fi
  return 0
}

36 37 38 39
# Trap function for removing any remaining lock file at exit.
function _rmlock () {
  [ -n "$FLUTTER_UPGRADE_LOCK" ] && rm -rf "$FLUTTER_UPGRADE_LOCK"
}
40

41 42 43
# Determines which lock method to use, based on what is available on the system.
# Returns a non-zero value if the lock was not acquired, zero if acquired.
function _lock () {
44
  if hash flock 2>/dev/null; then
45
    flock --nonblock --exclusive 7 2>/dev/null
46 47
  elif hash shlock 2>/dev/null; then
    shlock -f "$1" -p $$
48 49
  else
    mkdir "$1" 2>/dev/null
50
  fi
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
}

# Waits for an update lock to be acquired.
#
# To ensure that we don't simultaneously update Dart in multiple parallel
# instances, we try to obtain an exclusive lock on this file descriptor (and
# thus this script's source file) while we are updating Dart and compiling the
# script. To do this, we try to use the command line program "flock", which is
# available on many Unix-like platforms, in particular on most Linux
# distributions. You give it a file descriptor, and it locks the corresponding
# file, having inherited the file descriptor from the shell.
#
# Complicating matters, there are two major scenarios where this will not
# work.
#
# The first is if the platform doesn't have "flock", for example on macOS. There
# is not a direct equivalent, so on platforms that don't have flock, we fall
68 69 70 71
# back to using trying to use the shlock command, and if that doesn't exist,
# then we use mkdir as an atomic operation to create a lock directory. If mkdir
# is able to create the directory, then the lock is acquired. To determine if we
# have "flock" or "shlock" available, we use the "hash" shell built-in.
72
#
73 74 75 76 77 78 79 80 81
# The second complication is on network file shares. On NFS, to obtain an
# exclusive lock you need a file descriptor that is open for writing. Thus, we
# ignore errors from flock by redirecting all output to /dev/null, since users
# will typically not care about errors from flock and are more likely to be
# confused by them than helped. The "shlock" method doesn't work for network
# shares, since it is PID-based. The "mkdir" method does work over NFS
# implementations that support atomic directory creation (which is most of
# them). The "schlock" and "flock" commands are more reliable than the mkdir
# method, however, or we would use mkdir in all cases.
82 83 84 85 86 87 88 89 90 91 92 93 94 95
#
# The upgrade_flutter function calling _wait_for_lock is executed in a subshell
# with a redirect that pipes the source of this script into file descriptor 7.
# A flock lock is released when this subshell exits and file descriptor 7 is
# closed. The mkdir lock is released via an exit trap from the subshell that
# deletes the lock directory.
function _wait_for_lock () {
  FLUTTER_UPGRADE_LOCK="$FLUTTER_ROOT/bin/cache/.upgrade_lock"
  local waiting_message_displayed
  while ! _lock "$FLUTTER_UPGRADE_LOCK"; do
    if [[ -z $waiting_message_displayed ]]; then
      # Print with a return so that if the Dart code also prints this message
      # when it does its own lock, the message won't appear twice. Be sure that
      # the clearing printf below has the same number of space characters.
96
      printf "Waiting for another flutter command to release the startup lock...\r" >&2;
97 98 99 100
      waiting_message_displayed="true"
    fi
    sleep .1;
  done
101 102
  if [[ $waiting_message_displayed == "true" ]]; then
    # Clear the waiting message so it doesn't overlap any following text.
103
    printf "                                                                  \r" >&2;
104
  fi
105 106 107 108 109 110 111 112 113 114 115
  unset waiting_message_displayed
  # If the lock file is acquired, make sure that it is removed on exit.
  trap _rmlock INT TERM EXIT
}

# This function is always run in a subshell. Running the function in a subshell
# is required to make sure any lock directory is cleaned up by the exit trap in
# _wait_for_lock.
function upgrade_flutter () (
  mkdir -p "$FLUTTER_ROOT/bin/cache"

116
  local revision="$(cd "$FLUTTER_ROOT"; git rev-parse HEAD)"
117
  local compilekey="$revision:$FLUTTER_TOOL_ARGS"
118 119 120

  # Invalidate cache if:
  #  * SNAPSHOT_PATH is not a file, or
121 122 123
  #  * STAMP_PATH is not a file, or
  #  * STAMP_PATH is an empty file, or
  #  * Contents of STAMP_PATH is not what we are going to compile, or
124
  #  * pubspec.yaml last modified after pubspec.lock
125 126 127 128
  if [[ ! -f "$SNAPSHOT_PATH" || \
        ! -s "$STAMP_PATH" || \
        "$(cat "$STAMP_PATH")" != "$compilekey" || \
        "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
129 130 131 132 133 134 135
    # Waits for the update lock to be acquired. Placing this check inside the
    # conditional allows the majority of flutter/dart installations to bypass
    # the lock entirely, but as a result this required a second verification that
    # the SDK is up to date.
    _wait_for_lock

    # A different shell process might have updated the tool/SDK.
136
    if [[ -f "$SNAPSHOT_PATH" && -s "$STAMP_PATH" && "$(cat "$STAMP_PATH")" == "$compilekey" && "$FLUTTER_TOOLS_DIR/pubspec.yaml" -ot "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
137 138 139
      exit $?
    fi

140
    # Fetch Dart...
141
    rm -f "$FLUTTER_ROOT/version"
142
    rm -f "$FLUTTER_ROOT/bin/cache/flutter.version.json"
143 144 145
    touch "$FLUTTER_ROOT/bin/cache/.dartignore"
    "$FLUTTER_ROOT/bin/internal/update_dart_sdk.sh"

146 147 148 149 150
    if [[ "$BIN_NAME" == 'dart' ]]; then
      # Don't try to build tool
      return
    fi

151
    >&2 echo Building flutter tool...
152 153

    # Prepare packages...
154 155
    if [[ "$CI" == "true" || "$BOT" == "true" || "$CONTINUOUS_INTEGRATION" == "true" || "$CHROME_HEADLESS" == "1" ]]; then
      PUB_ENVIRONMENT="$PUB_ENVIRONMENT:flutter_bot"
156 157
    else
      export PUB_SUMMARY_ONLY=1
158 159
    fi
    export PUB_ENVIRONMENT="$PUB_ENVIRONMENT:flutter_install"
160
    pub_upgrade_with_retry
161

162 163 164 165 166 167 168 169
    # Move the old snapshot - we can't just overwrite it as the VM might currently have it
    # memory mapped (e.g. on flutter upgrade). For downloading a new dart sdk the folder is moved,
    # so we take the same approach of moving the file here.
    SNAPSHOT_PATH_OLD="$SNAPSHOT_PATH.old"
    if [ -f "$SNAPSHOT_PATH" ]; then
      mv "$SNAPSHOT_PATH" "$SNAPSHOT_PATH_OLD"
    fi

170
    # Compile...
171
    "$DART" --verbosity=error --disable-dart-dev $FLUTTER_TOOL_ARGS --snapshot="$SNAPSHOT_PATH" --snapshot-kind="app-jit" --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" --no-enable-mirrors "$SCRIPT_PATH" > /dev/null
172
    echo "$compilekey" > "$STAMP_PATH"
173 174 175 176 177

    # Delete any temporary snapshot path.
    if [ -f "$SNAPSHOT_PATH_OLD" ]; then
      rm -f "$SNAPSHOT_PATH_OLD"
    fi
178
  fi
179 180 181
  # The exit here is extraneous since the function is run in a subshell, but
  # this serves as documentation that running the function in a subshell is
  # required to make sure any lock directory created by mkdir is cleaned up.
182
  exit $?
183
)
184 185

# This function is intended to be executed by entrypoints (e.g. `//bin/flutter`
186 187
# and `//bin/dart`). PROG_NAME and BIN_DIR should already be set by those
# entrypoints.
188 189 190
function shared::execute() {
  export FLUTTER_ROOT="$(cd "${BIN_DIR}/.." ; pwd -P)"

191 192 193 194 195 196
  # If present, run the bootstrap script first
  BOOTSTRAP_PATH="$FLUTTER_ROOT/bin/internal/bootstrap.sh"
  if [ -f "$BOOTSTRAP_PATH" ]; then
    source "$BOOTSTRAP_PATH"
  fi

197 198 199 200 201 202 203 204
  FLUTTER_TOOLS_DIR="$FLUTTER_ROOT/packages/flutter_tools"
  SNAPSHOT_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.snapshot"
  STAMP_PATH="$FLUTTER_ROOT/bin/cache/flutter_tools.stamp"
  SCRIPT_PATH="$FLUTTER_TOOLS_DIR/bin/flutter_tools.dart"
  DART_SDK_PATH="$FLUTTER_ROOT/bin/cache/dart-sdk"

  DART="$DART_SDK_PATH/bin/dart"

205 206
  # If running over git-bash, overrides the default UNIX executables with win32
  # executables
207
  case "$(uname -s)" in
208
    MINGW* | MSYS* )
209 210 211 212
      DART="$DART.exe"
      ;;
  esac

213 214
  # Test if running as superuser – but don't warn if running within Docker or CI.
  if [[ "$EUID" == "0" && ! -f /.dockerenv && "$CI" != "true" && "$BOT" != "true" && "$CONTINUOUS_INTEGRATION" != "true" ]]; then
215 216 217 218
    >&2 echo "   Woah! You appear to be trying to run flutter as root."
    >&2 echo "   We strongly recommend running the flutter tool without superuser privileges."
    >&2 echo "  /"
    >&2 echo "📎"
219 220 221 222
  fi

  # Test if Git is available on the Host
  if ! hash git 2>/dev/null; then
223
    >&2 echo "Error: Unable to find git in your PATH."
224 225 226 227 228
    exit 1
  fi
  # Test if the flutter directory is a git clone (otherwise git rev-parse HEAD
  # would fail)
  if [[ ! -e "$FLUTTER_ROOT/.git" ]]; then
229 230 231 232
    >&2 echo "Error: The Flutter directory is not a clone of the GitHub project."
    >&2 echo "       The flutter tool requires Git in order to operate properly;"
    >&2 echo "       to install Flutter, see the instructions at:"
    >&2 echo "       https://flutter.dev/get-started"
233 234 235
    exit 1
  fi

236 237
  BIN_NAME="$(basename "$PROG_NAME")"

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  # File descriptor 7 is prepared here so that we can use it with
  # flock(1) in _lock() (see above).
  #
  # We use number 7 because it's a luckier number than 3; luck is
  # important when making locks work reliably. Also because that way
  # if anyone is redirecting other file descriptors there's less
  # chance of a conflict.
  #
  # In any case, the file we redirect into this file descriptor is
  # this very source file you are reading right now, because that's
  # the only file we can truly guarantee exists, since we're running
  # it. We don't use PROG_NAME because otherwise if you run `dart` and
  # `flutter` simultaneously they'll end up using different lock files
  # and will corrupt each others' downloads.
  #
  # SHARED_NAME itself is prepared by the caller script.
  upgrade_flutter 7< "$SHARED_NAME"
255 256 257

  case "$BIN_NAME" in
    flutter*)
258 259
      # FLUTTER_TOOL_ARGS aren't quoted below, because it is meant to be
      # considered as separate space-separated args.
260
      exec "$DART" --disable-dart-dev --packages="$FLUTTER_TOOLS_DIR/.dart_tool/package_config.json" $FLUTTER_TOOL_ARGS "$SNAPSHOT_PATH" "$@"
261 262
      ;;
    dart*)
263
      exec "$DART" "$@"
264 265
      ;;
    *)
266
      >&2 echo "Error! Executable name $BIN_NAME not recognized!"
267 268 269 270
      exit 1
      ;;
  esac
}