shared.sh 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#!/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

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

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

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

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

42 43 44
# 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 () {
45
  if hash flock 2>/dev/null; then
46
    flock --nonblock --exclusive 7 2>/dev/null
47 48
  elif hash shlock 2>/dev/null; then
    shlock -f "$1" -p $$
49 50
  else
    mkdir "$1" 2>/dev/null
51
  fi
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
}

# 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
69 70 71 72
# 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.
73
#
74 75 76 77 78 79 80 81 82
# 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.
83 84 85 86 87 88 89 90 91 92 93 94 95 96
#
# 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.
97
      printf "Waiting for another flutter command to release the startup lock...\r" >&2;
98 99 100 101
      waiting_message_displayed="true"
    fi
    sleep .1;
  done
102 103
  if [[ $waiting_message_displayed == "true" ]]; then
    # Clear the waiting message so it doesn't overlap any following text.
104
    printf "                                                                  \r" >&2;
105
  fi
106 107 108 109 110 111 112 113 114 115 116
  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"

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

  # Invalidate cache if:
  #  * SNAPSHOT_PATH is not a file, or
122 123 124
  #  * 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
125
  #  * pubspec.yaml last modified after pubspec.lock
126 127 128 129
  if [[ ! -f "$SNAPSHOT_PATH" || \
        ! -s "$STAMP_PATH" || \
        "$(cat "$STAMP_PATH")" != "$compilekey" || \
        "$FLUTTER_TOOLS_DIR/pubspec.yaml" -nt "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
130 131 132 133 134 135 136
    # 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.
137
    if [[ -f "$SNAPSHOT_PATH" && -s "$STAMP_PATH" && "$(cat "$STAMP_PATH")" == "$compilekey" && "$FLUTTER_TOOLS_DIR/pubspec.yaml" -ot "$FLUTTER_TOOLS_DIR/pubspec.lock" ]]; then
138 139 140
      exit $?
    fi

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

147
    >&2 echo Building flutter tool...
148 149

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

158 159 160 161 162 163 164 165
    # 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

166
    # Compile...
167
    "$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
168
    echo "$compilekey" > "$STAMP_PATH"
169 170 171 172 173

    # Delete any temporary snapshot path.
    if [ -f "$SNAPSHOT_PATH_OLD" ]; then
      rm -f "$SNAPSHOT_PATH_OLD"
    fi
174
  fi
175 176 177
  # 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.
178
  exit $?
179
)
180 181

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

187 188 189 190 191 192
  # If present, run the bootstrap script first
  BOOTSTRAP_PATH="$FLUTTER_ROOT/bin/internal/bootstrap.sh"
  if [ -f "$BOOTSTRAP_PATH" ]; then
    source "$BOOTSTRAP_PATH"
  fi

193 194 195 196 197 198 199 200
  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"

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

209 210
  # 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
211 212 213 214
    >&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 "📎"
215 216 217 218
  fi

  # Test if Git is available on the Host
  if ! hash git 2>/dev/null; then
219
    >&2 echo "Error: Unable to find git in your PATH."
220 221 222 223 224
    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
225 226 227 228
    >&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"
229 230 231
    exit 1
  fi

232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  # 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"
249 250 251 252

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