[flutter_tools] port bash script to use sysctl not uname on macOS (#101308)

diff --git a/bin/internal/update_dart_sdk.sh b/bin/internal/update_dart_sdk.sh
index 3324880..49e4698 100755
--- a/bin/internal/update_dart_sdk.sh
+++ b/bin/internal/update_dart_sdk.sh
@@ -20,12 +20,13 @@
 DART_SDK_PATH_OLD="$DART_SDK_PATH.old"
 ENGINE_STAMP="$FLUTTER_ROOT/bin/cache/engine-dart-sdk.stamp"
 ENGINE_VERSION=`cat "$FLUTTER_ROOT/bin/internal/engine.version"`
+OS="$(uname -s)"
 
 if [ ! -f "$ENGINE_STAMP" ] || [ "$ENGINE_VERSION" != `cat "$ENGINE_STAMP"` ]; then
   command -v curl > /dev/null 2>&1 || {
     >&2 echo
     >&2 echo 'Missing "curl" tool. Unable to download Dart SDK.'
-    case "$(uname -s)" in
+    case "$OS" in
       Darwin)
         >&2 echo 'Consider running "brew install curl".'
         ;;
@@ -42,7 +43,7 @@
   command -v unzip > /dev/null 2>&1 || {
     >&2 echo
     >&2 echo 'Missing "unzip" tool. Unable to extract Dart SDK.'
-    case "$(uname -s)" in
+    case "$OS" in
       Darwin)
         echo 'Consider running "brew install unzip".'
         ;;
@@ -56,20 +57,43 @@
     echo
     exit 1
   }
-  >&2 echo "Downloading Dart SDK from Flutter engine $ENGINE_VERSION..."
 
-  # On x64 stdout is "uname -m: x86_64"
-  # On arm64 stdout is "uname -m: aarch64, arm64_v8a"
-  case "$(uname -m)" in
-    x86_64)
-      ARCH="x64"
-      ;;
-    *)
-      ARCH="arm64"
-      ;;
-  esac
+  # `uname -m` may be running in Rosetta mode, instead query sysctl
+  if [ "$OS" = 'Darwin' ]; then
+    # Allow non-zero exit so we can do control flow
+    set +e
+    # -n means only print value, not key
+    QUERY="sysctl -n hw.optional.arm64"
+    # Do not wrap $QUERY in double quotes, otherwise the args will be treated as
+    # part of the command
+    QUERY_RESULT=$($QUERY 2>/dev/null)
+    if [ $? -eq 1 ]; then
+      # If this command fails, we're certainly not on ARM
+      ARCH='x64'
+    elif [ "$QUERY_RESULT" = '0' ]; then
+      # If this returns 0, we are also not on ARM
+      ARCH='x64'
+    elif [ "$QUERY_RESULT" = '1' ]; then
+      ARCH='arm64'
+    else
+      >&2 echo "'$QUERY' returned unexpected output: '$QUERY_RESULT'"
+      exit 1
+    fi
+    set -e
+  else
+    # On x64 stdout is "uname -m: x86_64"
+    # On arm64 stdout is "uname -m: aarch64, arm64_v8a"
+    case "$(uname -m)" in
+      x86_64)
+        ARCH="x64"
+        ;;
+      *)
+        ARCH="arm64"
+        ;;
+    esac
+  fi
 
-  case "$(uname -s)" in
+  case "$OS" in
     Darwin)
       DART_ZIP_NAME="dart-sdk-darwin-${ARCH}.zip"
       IS_USER_EXECUTABLE="-perm +100"
@@ -88,6 +112,8 @@
       ;;
   esac
 
+  >&2 echo "Downloading $OS $ARCH Dart SDK from Flutter engine $ENGINE_VERSION..."
+
   # Use the default find if possible.
   if [ -e /usr/bin/find ]; then
     FIND=/usr/bin/find