Skip to content

Commit 4cd0a32

Browse files
[flutter_tools] Fix analyze size on arm64 (#141317)
Fixes flutter/flutter#140659
1 parent 0c66636 commit 4cd0a32

File tree

3 files changed

+82
-24
lines changed

3 files changed

+82
-24
lines changed

packages/flutter_tools/lib/src/build_system/targets/macos.dart

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -455,22 +455,22 @@ abstract class MacOSBundleFlutterAssets extends Target {
455455
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
456456
<plist version="1.0">
457457
<dict>
458-
<key>CFBundleDevelopmentRegion</key>
459-
<string>en</string>
460-
<key>CFBundleExecutable</key>
461-
<string>App</string>
462-
<key>CFBundleIdentifier</key>
463-
<string>io.flutter.flutter.app</string>
464-
<key>CFBundleInfoDictionaryVersion</key>
465-
<string>6.0</string>
466-
<key>CFBundleName</key>
467-
<string>App</string>
468-
<key>CFBundlePackageType</key>
469-
<string>FMWK</string>
470-
<key>CFBundleShortVersionString</key>
471-
<string>1.0</string>
472-
<key>CFBundleVersion</key>
473-
<string>1.0</string>
458+
<key>CFBundleDevelopmentRegion</key>
459+
<string>en</string>
460+
<key>CFBundleExecutable</key>
461+
<string>App</string>
462+
<key>CFBundleIdentifier</key>
463+
<string>io.flutter.flutter.app</string>
464+
<key>CFBundleInfoDictionaryVersion</key>
465+
<string>6.0</string>
466+
<key>CFBundleName</key>
467+
<string>App</string>
468+
<key>CFBundlePackageType</key>
469+
<string>FMWK</string>
470+
<key>CFBundleShortVersionString</key>
471+
<string>1.0</string>
472+
<key>CFBundleVersion</key>
473+
<string>1.0</string>
474474
</dict>
475475
</plist>
476476

packages/flutter_tools/lib/src/macos/build_macos.dart

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,25 @@ Future<void> _writeCodeSizeAnalysis(BuildInfo buildInfo, SizeAnalyzer? sizeAnaly
181181
if (buildInfo.codeSizeDirectory == null || sizeAnalyzer == null) {
182182
return;
183183
}
184-
final String arch = DarwinArch.x86_64.name;
185-
final File aotSnapshot = globals.fs.directory(buildInfo.codeSizeDirectory)
186-
.childFile('snapshot.$arch.json');
187-
final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
188-
.childFile('trace.$arch.json');
184+
final File? aotSnapshot = DarwinArch.values.map<File?>((DarwinArch arch) {
185+
return globals.fs.directory(buildInfo.codeSizeDirectory).childFile('snapshot.${arch.name}.json');
186+
// Pick the first if there are multiple for simplicity
187+
}).firstWhere(
188+
(File? file) => file!.existsSync(),
189+
orElse: () => null,
190+
);
191+
if (aotSnapshot == null) {
192+
throw StateError('No code size snapshot file (snapshot.<ARCH>.json) found in ${buildInfo.codeSizeDirectory}');
193+
}
194+
final File? precompilerTrace = DarwinArch.values.map<File?>((DarwinArch arch) {
195+
return globals.fs.directory(buildInfo.codeSizeDirectory).childFile('trace.${arch.name}.json');
196+
}).firstWhere(
197+
(File? file) => file!.existsSync(),
198+
orElse: () => null,
199+
);
200+
if (precompilerTrace == null) {
201+
throw StateError('No precompiler trace file (trace.<ARCH>.json) found in ${buildInfo.codeSizeDirectory}');
202+
}
189203

190204
// This analysis is only supported for release builds.
191205
// Attempt to guess the correct .app by picking the first one.

packages/flutter_tools/test/commands.shard/hermetic/build_macos_test.dart

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,49 @@ STDERR STUFF
585585
Platform: () => macosPlatform,
586586
});
587587

588-
testUsingContext('Performs code size analysis and sends analytics', () async {
588+
testUsingContext('code size analysis throws StateError if no code size snapshot generated by gen_snapshot', () async {
589+
final BuildCommand command = BuildCommand(
590+
artifacts: artifacts,
591+
androidSdk: FakeAndroidSdk(),
592+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
593+
fileSystem: fileSystem,
594+
logger: logger,
595+
processUtils: processUtils,
596+
osUtils: FakeOperatingSystemUtils(),
597+
);
598+
createMinimalMockProjectFiles();
599+
600+
fileSystem.file('build/macos/Build/Products/Release/Runner.app/App')
601+
..createSync(recursive: true)
602+
..writeAsBytesSync(List<int>.generate(10000, (int index) => 0));
603+
604+
expect(
605+
() => createTestCommandRunner(command).run(
606+
const <String>['build', 'macos', '--no-pub', '--analyze-size']
607+
),
608+
throwsA(
609+
isA<StateError>().having(
610+
(StateError err) => err.message,
611+
'message',
612+
'No code size snapshot file (snapshot.<ARCH>.json) found in build/flutter_size_01',
613+
),
614+
),
615+
);
616+
617+
expect(testLogger.statusText, isEmpty);
618+
}, overrides: <Type, Generator>{
619+
FileSystem: () => fileSystem,
620+
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
621+
// we never generate code size snapshot here
622+
setUpFakeXcodeBuildHandler('Release'),
623+
]),
624+
Platform: () => macosPlatform,
625+
FeatureFlags: () => TestFeatureFlags(isMacOSEnabled: true),
626+
FileSystemUtils: () => FileSystemUtils(fileSystem: fileSystem, platform: macosPlatform),
627+
Usage: () => usage,
628+
Analytics: () => fakeAnalytics,
629+
});
630+
testUsingContext('Performs code size analysis and sends analytics from arm64 host', () async {
589631
final BuildCommand command = BuildCommand(
590632
artifacts: artifacts,
591633
androidSdk: FakeAndroidSdk(),
@@ -614,8 +656,10 @@ STDERR STUFF
614656
}, overrides: <Type, Generator>{
615657
FileSystem: () => fileSystem,
616658
ProcessManager: () => FakeProcessManager.list(<FakeCommand>[
659+
// These are generated by gen_snapshot because flutter assemble passes
660+
// extra flags specifying this output path
617661
setUpFakeXcodeBuildHandler('Release', onRun: () {
618-
fileSystem.file('build/flutter_size_01/snapshot.x86_64.json')
662+
fileSystem.file('build/flutter_size_01/snapshot.arm64.json')
619663
..createSync(recursive: true)
620664
..writeAsStringSync('''
621665
[
@@ -626,7 +670,7 @@ STDERR STUFF
626670
"s": 2400
627671
}
628672
]''');
629-
fileSystem.file('build/flutter_size_01/trace.x86_64.json')
673+
fileSystem.file('build/flutter_size_01/trace.arm64.json')
630674
..createSync(recursive: true)
631675
..writeAsStringSync('{}');
632676
}),

0 commit comments

Comments
 (0)