From 4cd0a3252d2b45e243714b3ce93c5b2313bce671 Mon Sep 17 00:00:00 2001 From: Christopher Fujino Date: Tue, 16 Jan 2024 16:15:28 -0800 Subject: [PATCH] [flutter_tools] Fix analyze size on arm64 (#141317) Fixes https://github.com/flutter/flutter/issues/140659 --- .../lib/src/build_system/targets/macos.dart | 32 ++++++------ .../lib/src/macos/build_macos.dart | 24 +++++++-- .../hermetic/build_macos_test.dart | 50 +++++++++++++++++-- 3 files changed, 82 insertions(+), 24 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_system/targets/macos.dart b/packages/flutter_tools/lib/src/build_system/targets/macos.dart index 7cd1e8193d..0881169ea0 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/macos.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/macos.dart @@ -455,22 +455,22 @@ abstract class MacOSBundleFlutterAssets extends Target { - CFBundleDevelopmentRegion - en - CFBundleExecutable - App - CFBundleIdentifier - io.flutter.flutter.app - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - App - CFBundlePackageType - FMWK - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1.0 + CFBundleDevelopmentRegion + en + CFBundleExecutable + App + CFBundleIdentifier + io.flutter.flutter.app + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + App + CFBundlePackageType + FMWK + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1.0 diff --git a/packages/flutter_tools/lib/src/macos/build_macos.dart b/packages/flutter_tools/lib/src/macos/build_macos.dart index 40e2d52430..146477b63b 100644 --- a/packages/flutter_tools/lib/src/macos/build_macos.dart +++ b/packages/flutter_tools/lib/src/macos/build_macos.dart @@ -181,11 +181,25 @@ Future _writeCodeSizeAnalysis(BuildInfo buildInfo, SizeAnalyzer? sizeAnaly if (buildInfo.codeSizeDirectory == null || sizeAnalyzer == null) { return; } - final String arch = DarwinArch.x86_64.name; - final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory) - .childFile('snapshot.$arch.json'); - final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory) - .childFile('trace.$arch.json'); + final File? aotSnapshot = DarwinArch.values.map((DarwinArch arch) { + return globals.fs.directory(buildInfo.codeSizeDirectory).childFile('snapshot.${arch.name}.json'); + // Pick the first if there are multiple for simplicity + }).firstWhere( + (File? file) => file!.existsSync(), + orElse: () => null, + ); + if (aotSnapshot == null) { + throw StateError('No code size snapshot file (snapshot..json) found in ${buildInfo.codeSizeDirectory}'); + } + final File? precompilerTrace = DarwinArch.values.map((DarwinArch arch) { + return globals.fs.directory(buildInfo.codeSizeDirectory).childFile('trace.${arch.name}.json'); + }).firstWhere( + (File? file) => file!.existsSync(), + orElse: () => null, + ); + if (precompilerTrace == null) { + throw StateError('No precompiler trace file (trace..json) found in ${buildInfo.codeSizeDirectory}'); + } // This analysis is only supported for release builds. // Attempt to guess the correct .app by picking the first one. diff --git a/packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart index 2cd9163b5e..f12b919336 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart @@ -585,7 +585,49 @@ STDERR STUFF Platform: () => macosPlatform, }); - testUsingContext('Performs code size analysis and sends analytics', () async { + testUsingContext('code size analysis throws StateError if no code size snapshot generated by gen_snapshot', () async { + final BuildCommand command = BuildCommand( + artifacts: artifacts, + androidSdk: FakeAndroidSdk(), + buildSystem: TestBuildSystem.all(BuildResult(success: true)), + fileSystem: fileSystem, + logger: logger, + processUtils: processUtils, + osUtils: FakeOperatingSystemUtils(), + ); + createMinimalMockProjectFiles(); + + fileSystem.file('build/macos/Build/Products/Release/Runner.app/App') + ..createSync(recursive: true) + ..writeAsBytesSync(List.generate(10000, (int index) => 0)); + + expect( + () => createTestCommandRunner(command).run( + const ['build', 'macos', '--no-pub', '--analyze-size'] + ), + throwsA( + isA().having( + (StateError err) => err.message, + 'message', + 'No code size snapshot file (snapshot..json) found in build/flutter_size_01', + ), + ), + ); + + expect(testLogger.statusText, isEmpty); + }, overrides: { + FileSystem: () => fileSystem, + ProcessManager: () => FakeProcessManager.list([ + // we never generate code size snapshot here + setUpFakeXcodeBuildHandler('Release'), + ]), + Platform: () => macosPlatform, + FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true), + FileSystemUtils: () => FileSystemUtils(fileSystem: fileSystem, platform: macosPlatform), + Usage: () => usage, + Analytics: () => fakeAnalytics, + }); + testUsingContext('Performs code size analysis and sends analytics from arm64 host', () async { final BuildCommand command = BuildCommand( artifacts: artifacts, androidSdk: FakeAndroidSdk(), @@ -614,8 +656,10 @@ STDERR STUFF }, overrides: { FileSystem: () => fileSystem, ProcessManager: () => FakeProcessManager.list([ + // These are generated by gen_snapshot because flutter assemble passes + // extra flags specifying this output path setUpFakeXcodeBuildHandler('Release', onRun: () { - fileSystem.file('build/flutter_size_01/snapshot.x86_64.json') + fileSystem.file('build/flutter_size_01/snapshot.arm64.json') ..createSync(recursive: true) ..writeAsStringSync(''' [ @@ -626,7 +670,7 @@ STDERR STUFF "s": 2400 } ]'''); - fileSystem.file('build/flutter_size_01/trace.x86_64.json') + fileSystem.file('build/flutter_size_01/trace.arm64.json') ..createSync(recursive: true) ..writeAsStringSync('{}'); }),