diff --git a/DEPS b/DEPS index deaf53e4bf63d..9bd38b5b92805 100644 --- a/DEPS +++ b/DEPS @@ -16,7 +16,7 @@ vars = { 'skia_git': 'https://skia.googlesource.com', 'llvm_git': 'https://llvm.googlesource.com', 'skia_revision': 'a183ded9ad67d998a5b0fe4cd86d3ef5402ffb45', - "dart_sdk_revision": "3f8b97e369a83033089608c86c996a3f67897f8c", + "dart_sdk_revision": "93ded8b64ac9b0a35a6a1d6f2d0b88c2c10a76c1", "dart_sdk_git": "git@github.com:shorebirdtech/dart-sdk.git", "updater_git": "https://github.com/shorebirdtech/updater.git", "updater_rev": "34509fca3c65388ebc84cfaea8f38733ce41f41a", diff --git a/engine/src/flutter/shell/common/shorebird/BUILD.gn b/engine/src/flutter/shell/common/shorebird/BUILD.gn index 22b11a614cad1..8c8034a24c301 100644 --- a/engine/src/flutter/shell/common/shorebird/BUILD.gn +++ b/engine/src/flutter/shell/common/shorebird/BUILD.gn @@ -86,7 +86,8 @@ if (shorebird_updater_supported) { "$shorebird_updater_dir/Cargo.lock", "$shorebird_updater_dir/library/Cargo.toml", "$shorebird_updater_dir/library/build.rs", - "$shorebird_updater_dir/library/cbindgen.toml", + "$shorebird_updater_dir/library/cbindgen_dart.toml", + "$shorebird_updater_dir/library/cbindgen_engine.toml", "$shorebird_updater_dir/library/.cargo/config.toml", ] inputs += shorebird_updater_rs_sources diff --git a/packages/flutter_tools/lib/src/base/build.dart b/packages/flutter_tools/lib/src/base/build.dart index 312be7b837b85..5f096636729b4 100644 --- a/packages/flutter_tools/lib/src/base/build.dart +++ b/packages/flutter_tools/lib/src/base/build.dart @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import 'dart:io' show Platform; + import 'package:process/process.dart'; import '../artifacts.dart'; @@ -29,7 +31,9 @@ class GenSnapshot { required Artifacts artifacts, required ProcessManager processManager, required Logger logger, + required FileSystem fileSystem, }) : _artifacts = artifacts, + _fileSystem = fileSystem, _processUtils = ProcessUtils(logger: logger, processManager: processManager); final Artifacts _artifacts; @@ -54,6 +58,75 @@ class GenSnapshot { ' See dartbug.com/30524 for more information.', }; + /// Returns the path to an analyze_snapshot binary that matches gen_snapshot's + /// SDK version, or null if no matching binary can be found. + /// + /// gen_snapshot and analyze_snapshot share a snapshot-format hash baked into + /// each binary; if the hashes disagree, analyze_snapshot rejects gen_snapshot's + /// output with "Wrong full snapshot version" at parse time. Pre-existing binary + /// layouts (e.g. an older `universal/analyze_snapshot_` left behind from + /// a previous BUILD.gn revision) can shadow the freshly built one. Resolve by + /// probing every candidate location and returning only the one whose + /// `--sdk_version` matches gen_snapshot's `--version` output. + String? getAnalyzeSnapshotPath(SnapshotType snapshotType, DarwinArch? darwinArch) { + final Artifact genSnapshotArtifact; + final String analyzeName; + if (snapshotType.platform == TargetPlatform.ios || + snapshotType.platform == TargetPlatform.darwin) { + if (darwinArch == DarwinArch.arm64) { + genSnapshotArtifact = Artifact.genSnapshotArm64; + analyzeName = 'analyze_snapshot_arm64'; + } else { + genSnapshotArtifact = Artifact.genSnapshotX64; + analyzeName = 'analyze_snapshot_x64'; + } + } else { + genSnapshotArtifact = Artifact.genSnapshot; + analyzeName = 'analyze_snapshot'; + } + final String genSnapshotPath = getSnapshotterPath(snapshotType, genSnapshotArtifact); + final String? genVersion = _probeSdkVersion(genSnapshotPath, '--version'); + final String dir = _fileSystem.path.dirname(genSnapshotPath); + // Cached SDK layout puts analyze_snapshot alongside gen_snapshot. Local + // engine layout resolves gen_snapshot via .../universal/gen_snapshot_arm64 + // while analyze_snapshot lives one level up at the build-dir root. Probe + // both. If we successfully read gen_snapshot's version, accept only a + // candidate whose --sdk_version matches; otherwise fall back to the first + // existing candidate (better than failing closed when the version probe + // itself misbehaves). + for (final candidate in [ + _fileSystem.path.join(dir, analyzeName), + _fileSystem.path.join(dir, '..', analyzeName), + ]) { + if (!_fileSystem.file(candidate).existsSync()) { + continue; + } + if (genVersion == null) { + return candidate; + } + final String? candidateVersion = _probeSdkVersion(candidate, '--sdk_version'); + if (candidateVersion == genVersion) { + return candidate; + } + } + return null; + } + + /// Runs [binary] with [versionFlag] and returns the trimmed stderr output, + /// or null if the binary couldn't be invoked or printed nothing. + /// gen_snapshot and analyze_snapshot both write their version line to stderr. + String? _probeSdkVersion(String binary, String versionFlag) { + try { + final RunResult result = _processUtils.runSync([binary, versionFlag]); + final String stderr = result.stderr.trim(); + return stderr.isEmpty ? null : stderr; + } on Exception { + return null; + } + } + + final FileSystem _fileSystem; + Future run({ required SnapshotType snapshotType, DarwinArch? darwinArch, @@ -95,15 +168,18 @@ class AOTSnapshotter { }) : _logger = logger, _fileSystem = fileSystem, _xcode = xcode, + _processUtils = ProcessUtils(logger: logger, processManager: processManager), _genSnapshot = GenSnapshot( artifacts: artifacts, processManager: processManager, logger: logger, + fileSystem: fileSystem, ); final Logger _logger; final FileSystem _fileSystem; final Xcode _xcode; + final ProcessUtils _processUtils; final GenSnapshot _genSnapshot; /// Builds an architecture-specific ahead-of-time compiled snapshot of the specified script. @@ -229,6 +305,22 @@ class AOTSnapshotter { genSnapshotArgs.add(mainPath); final snapshotType = SnapshotType(platform, buildMode); + + final int ddMaxBytes = _readDdMaxBytes(); + if (ddMaxBytes > 0 && usesLinker) { + final int pass1Exit = await _runDdAnalysisPass( + snapshotType: snapshotType, + darwinArch: darwinArch, + outputDir: outputDir, + baseGenSnapshotArgs: genSnapshotArgs, + mainPath: mainPath, + ddMaxBytes: ddMaxBytes, + ); + if (pass1Exit != 0) { + return pass1Exit; + } + } + final int genSnapshotExitCode = await _genSnapshot.run( snapshotType: snapshotType, additionalArgs: genSnapshotArgs, @@ -257,6 +349,126 @@ class AOTSnapshotter { } } + /// Reads the SHOREBIRD_DD_MAX_BYTES env var (preferring `dart-define` over + /// the process environment). Returns 0 if unset, malformed, or non-positive, + /// which signals "no DD pass." + int _readDdMaxBytes() { + const fromDefine = String.fromEnvironment('SHOREBIRD_DD_MAX_BYTES'); + final String? raw = fromDefine.isNotEmpty ? fromDefine : Platform.environment['SHOREBIRD_DD_MAX_BYTES']; + return int.tryParse(raw ?? '') ?? 0; + } + + /// Runs the DD analysis pass: gen_snapshot → ELF + DD identity, then + /// analyze_snapshot to compute the DD table, caller links, and slot mapping. + /// On success, mutates [baseGenSnapshotArgs] to add `--dd_slot_mapping=...` + /// before [mainPath] so the subsequent gen_snapshot run picks it up. + /// Returns the gen_snapshot pass-1 exit code (0 on success). + Future _runDdAnalysisPass({ + required SnapshotType snapshotType, + required DarwinArch? darwinArch, + required Directory outputDir, + required List baseGenSnapshotArgs, + required String mainPath, + required int ddMaxBytes, + }) async { + _logger.printTrace('DD 2-pass build: dd_max_bytes=$ddMaxBytes'); + + String linkPath(String name) => _fileSystem.path.join(outputDir.parent.path, name); + final String elfForAnalysis = linkPath('App_dd_analysis.so'); + final String ddIdentityPath = linkPath('App.dd_identity.link'); + final String ddTablePath = linkPath('App.dd.link'); + final String ddCallerLinksPath = linkPath('App.dd_callers.link'); + final String ddSlotMappingPath = linkPath('App.dd_slots.link'); + + // Pass 1: build ELF for analysis + DD identity. Strip the existing snapshot + // kind/output args from the base set; mainPath must remain at the end. + final elfArgs = [ + ...baseGenSnapshotArgs.where((String a) => + a != mainPath && + !a.startsWith('--snapshot_kind=') && + !a.startsWith('--assembly=') && + !a.startsWith('--elf=')), + '--snapshot_kind=app-aot-elf', + '--elf=$elfForAnalysis', + '--print_dd_function_identity_to=$ddIdentityPath', + mainPath, + ]; + final int pass1Exit = await _genSnapshot.run( + snapshotType: snapshotType, + additionalArgs: elfArgs, + darwinArch: darwinArch, + ); + if (pass1Exit != 0) { + _logger.printError('DD pass 1 (ELF for analysis) failed with exit code $pass1Exit'); + return pass1Exit; + } + + final String? analyzeSnapshotPath = _genSnapshot.getAnalyzeSnapshotPath(snapshotType, darwinArch); + if (analyzeSnapshotPath == null) { + _logger.printError( + 'DD pass: could not find an analyze_snapshot binary whose --sdk_version ' + 'matches gen_snapshot. The release will ship without DD activation; ' + 'patches against it will fall back to on-the-fly DD computation and ' + 'produce a structurally divergent snapshot (devastating link percentage). ' + 'Aborting the build instead.', + ); + _fileSystem.file(elfForAnalysis).deleteSync(); + return 1; + } + + final int ddTableExit = await _processUtils.stream([ + analyzeSnapshotPath, + '--compute_dd_table=$ddTablePath', + '--dd_caller_links=$ddCallerLinksPath', + '--dd_max_bytes=$ddMaxBytes', + elfForAnalysis, + ]); + if (ddTableExit != 0) { + _logger.printError( + 'DD pass: analyze_snapshot --compute_dd_table failed with exit code ' + '$ddTableExit. App.dd.link will not be produced and the release would ' + 'ship without DD activation.', + ); + _fileSystem.file(elfForAnalysis).deleteSync(); + return ddTableExit; + } + + final int ddSlotMappingExit = await _processUtils.stream([ + analyzeSnapshotPath, + '--compute_dd_slot_mapping=$ddSlotMappingPath', + '--dd_table_data=$ddTablePath', + '--dd_caller_links=$ddCallerLinksPath', + '--dd_function_identity=$ddIdentityPath', + elfForAnalysis, + ]); + if (ddSlotMappingExit != 0) { + _logger.printError( + 'DD pass: analyze_snapshot --compute_dd_slot_mapping failed with exit ' + 'code $ddSlotMappingExit. The DD slot mapping will not be available and ' + 'gen_snapshot pass 2 would emit a no-DD snapshot.', + ); + _fileSystem.file(elfForAnalysis).deleteSync(); + return ddSlotMappingExit; + } + + if (!_fileSystem.file(ddSlotMappingPath).existsSync()) { + _logger.printError( + 'DD pass: analyze_snapshot --compute_dd_slot_mapping reported success ' + 'but $ddSlotMappingPath was not produced.', + ); + _fileSystem.file(elfForAnalysis).deleteSync(); + return 1; + } + baseGenSnapshotArgs.insert( + baseGenSnapshotArgs.indexOf(mainPath), + '--dd_slot_mapping=$ddSlotMappingPath', + ); + _logger.printTrace('DD 2-pass build: added --dd_slot_mapping'); + + _fileSystem.file(elfForAnalysis).deleteSync(); + return 0; + } + /// Builds an iOS or macOS framework at [outputPath]/App.framework from the assembly /// source at [assemblyPath]. Future _buildFramework({ diff --git a/packages/flutter_tools/lib/src/build_system/targets/common.dart b/packages/flutter_tools/lib/src/build_system/targets/common.dart index e67e646bebf1b..031ad2e132b07 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/common.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/common.dart @@ -521,11 +521,13 @@ abstract final class LinkSupplement { } void maybeCopy(String name) { - final File file = environment.fileSystem.file( - environment.fileSystem.path.join(inputBuildDir, name), - ); + final path = environment.fileSystem.path.join(inputBuildDir, name); + final File file = environment.fileSystem.file(path); if (file.existsSync()) { file.copySync(environment.fileSystem.path.join(shorebirdDir.path, name)); + environment.logger.printTrace('LinkSupplement: copied $name'); + } else { + environment.logger.printTrace('LinkSupplement: missing $name at $path'); } } @@ -537,5 +539,11 @@ abstract final class LinkSupplement { maybeCopy('App.dispatch_table.json'); maybeCopy('App.ft.link'); maybeCopy('App.field_table.json'); + // DD table files for cascade limiter (produced by 2-pass release build + // when SHOREBIRD_DD_MAX_BYTES is set). + maybeCopy('App.dd.link'); + maybeCopy('App.dd_callers.link'); + maybeCopy('App.dd_identity.link'); + maybeCopy('App.dd_slots.link'); } } diff --git a/packages/flutter_tools/test/general.shard/base/build_test.dart b/packages/flutter_tools/test/general.shard/base/build_test.dart index 90cc0c26ac1db..0f1bbd14e3bad 100644 --- a/packages/flutter_tools/test/general.shard/base/build_test.dart +++ b/packages/flutter_tools/test/general.shard/base/build_test.dart @@ -63,6 +63,7 @@ void main() { artifacts: artifacts, logger: logger, processManager: processManager, + fileSystem: MemoryFileSystem.test(), ); }); diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/macos_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/macos_test.dart index fd3ce981cedb1..ce564fe34db6c 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/macos_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/macos_test.dart @@ -889,7 +889,14 @@ flavors: .createSync(recursive: true); final build = environment.buildDir.path; + // Both archs go through the same code path now (the DD-pass is gated on + // SHOREBIRD_DD_MAX_BYTES which isn't set in this test), so neither + // architecture has an extra async hop. With concurrent Future.wait, the + // archs reach gen_snapshot in iteration order: arm64 first + // (`kDarwinArchs = 'arm64 x86_64'`), x86_64 second. They then + // interleave at each subsequent await point in the same order. processManager.addCommands([ + // arm64 gen_snapshot runs first (iteration order). FakeCommand( command: [ 'Artifact.genSnapshotArm64.TargetPlatform.darwin.release', @@ -900,6 +907,7 @@ flavors: environment.buildDir.childFile('app.dill').path, ], ), + // x86_64 gen_snapshot runs next. FakeCommand( command: [ 'Artifact.genSnapshotX64.TargetPlatform.darwin.release', @@ -910,6 +918,7 @@ flavors: environment.buildDir.childFile('app.dill').path, ], ), + // From here on the two builds interleave: arm64 then x86_64 at each step. FakeCommand( command: [ 'xcrun',