Skip to content

Setup Flutter engine environment and build engine on M1 (Apple Silicon ARM64) #96745

@eggfly

Description

@eggfly

Problem 😭

There are some errors if we setup flutter engine environment in standard way, including:

  • gclient sync cannot find third_party/android_tools (including ndk and sdk) and third_party/java packages (openjdk) in arm64 on M1 chip macOS
  • Cannot download prebuilt Dart SDK in arm64, and some header in Dart check 32/64 bit machine matching failed
  • tools/gn shows //out/android_debug/clang_x64/gen_snapshot is not the right target, but it generates a target //out/android_debug/clang_arm64/gen_snapshot instead and failed
  • objcopy command try to find the wrong path
  • Some other errors

Related errors and failure images:
image

See BUILD ON95 oh1ch CRused PNe r1le te Be snecdeg

Pasted Graphic 2

Pasted Graphic 5

Workaround 😊

So I dig into the var host_cpu and find it's maybe originally provided by depot_tools, and I temporarily do some modification, force cpu_arch detech logics to x64 in depot_tools as a workaround:

depot_tools$ git diff 
diff --git a/cipd b/cipd
index 91fbe165..d1903d77 100755
--- a/cipd
+++ b/cipd
@@ -46,13 +46,13 @@ if [ -z $ARCH ]; then
       ARCH="${UNAME}"
       ;;
     aarch64)
-      ARCH=arm64
+      ARCH=amd64
       ;;
     armv7l)
       ARCH=armv6l
       ;;
     arm*)
-      ARCH="${UNAME}"
+      ARCH="amd64"
       ;;
     *86)
       ARCH=386
diff --git a/detect_host_arch.py b/detect_host_arch.py
index cf4bfec7..6a4e8566 100755
--- a/detect_host_arch.py
+++ b/detect_host_arch.py
@@ -44,7 +44,7 @@ def HostArch():
   if host_arch == 'arm64' and platform.architecture()[0] == '32bit':
     host_arch = 'arm'
 
-  return host_arch
+  return 'x64'
 
 def DoMain(_):
   """Hook to be called from gyp without starting a separate python
diff --git a/gclient.py b/gclient.py
index 446be28d..71b087b5 100755
--- a/gclient.py
+++ b/gclient.py
@@ -1283,7 +1283,7 @@ class Dependency(gclient_utils.WorkItem, DependencySettings):
         'checkout_ppc': 'ppc' in self.target_cpu,
         'checkout_s390': 's390' in self.target_cpu,
         'checkout_x64': 'x64' in self.target_cpu,
-        'host_cpu': detect_host_arch.HostArch(),
+        'host_cpu': 'x64',
     }

And also we should let it download in src/flutter/DEPS: ( or maybe this is not a must after cheating in depot_tools):

flutter_engine/src/flutter$ git diff 
diff --git a/DEPS b/DEPS
index c72b4d7e3d..32d7b0a97a 100644
--- a/DEPS
+++ b/DEPS
@@ -66,7 +66,7 @@ vars = {
   'download_dart_sdk': True,
 
   # Checkout Android dependencies only on platforms where we build for Android targets.
-  'download_android_deps': 'host_cpu == "x64" and (host_os == "mac" or host_os == "linux")',
+  'download_android_deps': '(host_os == "mac" or host_os == "linux")',
 
   # Checkout Windows dependencies only if we are building on Windows.
   'download_windows_deps' : 'host_os == "win"',

We also need to disable auto update of depot_tools. depot_tools updates itself automatically when running gclient tool. To disable auto update, set the environment variable DEPOT_TOOLS_UPDATE=0 or run ./update_depot_tools_toggle.py --disable .

Do these mods and rerun gclient sync -D it will prompt an arch change, and download the packages rightly.
The cipd will change from Chrome Infra Package Deployer (cipd 2.6.2 (infra/tools/cipd/mac-arm64@PiaJs-2JM4V3hn1ffZH7wT5QV6z5Rt77aJZaCTRfspYC)) to Chrome Infra Package Deployer (cipd 2.6.2 (infra/tools/cipd/mac-amd64@dsOLt767apUdYzHrNZ7zaQzAOZSEBHZ7ncbfUp_0zfoC))

The cpu arch change prompt:

Detected CIPD client platform change to mac-amd64.
Deleting the existing client to trigger the bootstrap...
Chrome Infra Package Deployer (cipd 2.6.2 (infra/tools/cipd/mac-amd64@dsOLt767apUdYzHrNZ7zaQzAOZSEBHZ7ncbfUp_0zfoC))

Usage:  cipd [command] [arguments]

By forcing it from arm64 to x86, maybe it does two things:

  • The macOS with M1 chip can run x86 program with Rosetta 2 translation, the toolchain, packages, android sdk, ndk, jdk and other binary in x86 will run normally
  • Xcode will build the engine source files into the x86 target arch, this is a magical thing but it really does:
    image

So finally it works for me to build engine from source, on MacBook Air M1 ~ 😂

Pasted Graphic 15

image

Metadata

Metadata

Assignees

Labels

P3Issues that are less important to the Flutter projecta: desktopRunning on desktopd: wikiFlutter or engine wikisengineflutter/engine related. See also e: labels.platform-macBuilding on or for macOS specifically

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions