From d63442c1da38820e82986011bcd236659209a492 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos <6349682+vaind@users.noreply.github.com> Date: Fri, 9 Sep 2022 23:13:10 +0200 Subject: [PATCH] refactor: strip all local symbols from macOS and iOS App.framework - reduces app size (#111264) --- .../flutter_tools/lib/src/base/build.dart | 6 +-- .../test/general.shard/base/build_test.dart | 8 +-- .../build_system/targets/common_test.dart | 4 +- .../build_system/targets/macos_test.dart | 4 +- .../ios_content_validation_test.dart | 49 ++++++++----------- .../macos_content_validation_test.dart | 32 +++++++----- .../test/integration.shard/test_utils.dart | 28 +++++++++++ 7 files changed, 79 insertions(+), 52 deletions(-) diff --git a/packages/flutter_tools/lib/src/base/build.dart b/packages/flutter_tools/lib/src/base/build.dart index 08d326848e..037c8f1dbc 100644 --- a/packages/flutter_tools/lib/src/base/build.dart +++ b/packages/flutter_tools/lib/src/base/build.dart @@ -162,7 +162,7 @@ class AOTSnapshotter { } final String assembly = _fileSystem.path.join(outputDir.path, 'snapshot_assembly.S'); - if (platform == TargetPlatform.ios || platform == TargetPlatform.darwin) { + if (targetingApplePlatform) { genSnapshotArgs.addAll([ '--snapshot_kind=app-aot-assembly', '--assembly=$assembly', @@ -181,7 +181,7 @@ class AOTSnapshotter { if (targetingApplePlatform) { stripAfterBuild = shouldStrip; if (stripAfterBuild) { - _logger.printTrace('Will strip AOT snapshot manual after build and dSYM generation.'); + _logger.printTrace('Will strip AOT snapshot manually after build and dSYM generation.'); } } else { stripAfterBuild = false; @@ -331,7 +331,7 @@ class AOTSnapshotter { if (stripAfterBuild) { // See https://www.unix.com/man-page/osx/1/strip/ for arguments - final RunResult stripResult = await _xcode.strip(['-S', appLib, '-o', appLib]); + final RunResult stripResult = await _xcode.strip(['-x', appLib, '-o', appLib]); if (stripResult.exitCode != 0) { _logger.printError('Failed to strip debugging symbols from the generated AOT snapshot - strip terminated with exit code ${stripResult.exitCode}'); return stripResult.exitCode; 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 06e33e19a2..2221dee3b5 100644 --- a/packages/flutter_tools/test/general.shard/base/build_test.dart +++ b/packages/flutter_tools/test/general.shard/base/build_test.dart @@ -262,7 +262,7 @@ void main() { const FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', 'build/foo/App.framework/App', '-o', 'build/foo/App.framework/App', @@ -335,7 +335,7 @@ void main() { const FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', 'build/foo/App.framework/App', '-o', 'build/foo/App.framework/App', @@ -407,7 +407,7 @@ void main() { const FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', 'build/foo/App.framework/App', '-o', 'build/foo/App.framework/App', @@ -476,7 +476,7 @@ void main() { const FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', 'build/foo/App.framework/App', '-o', 'build/foo/App.framework/App', diff --git a/packages/flutter_tools/test/general.shard/build_system/targets/common_test.dart b/packages/flutter_tools/test/general.shard/build_system/targets/common_test.dart index 422890aff3..1ddb400beb 100644 --- a/packages/flutter_tools/test/general.shard/build_system/targets/common_test.dart +++ b/packages/flutter_tools/test/general.shard/build_system/targets/common_test.dart @@ -529,7 +529,7 @@ void main() { FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', '$build/arm64/App.framework/App', '-o', '$build/arm64/App.framework/App', @@ -619,7 +619,7 @@ void main() { FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', '$build/arm64/App.framework/App', '-o', '$build/arm64/App.framework/App', 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 c43454d5b1..7196e88537 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 @@ -503,7 +503,7 @@ void main() { FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', environment.buildDir.childFile('arm64/App.framework/App').path, '-o', environment.buildDir.childFile('arm64/App.framework/App').path, @@ -511,7 +511,7 @@ void main() { FakeCommand(command: [ 'xcrun', 'strip', - '-S', + '-x', environment.buildDir.childFile('x86_64/App.framework/App').path, '-o', environment.buildDir.childFile('x86_64/App.framework/App').path, diff --git a/packages/flutter_tools/test/host_cross_arch.shard/ios_content_validation_test.dart b/packages/flutter_tools/test/host_cross_arch.shard/ios_content_validation_test.dart index c2e03191ed..32fbc4b9c2 100644 --- a/packages/flutter_tools/test/host_cross_arch.shard/ios_content_validation_test.dart +++ b/packages/flutter_tools/test/host_cross_arch.shard/ios_content_validation_test.dart @@ -193,37 +193,28 @@ void main() { }); testWithoutContext('check symbols', () { - final ProcessResult symbols = processManager.runSync( - [ - 'nm', - '-g', - outputAppFrameworkBinary.path, - '-arch', - 'arm64', - ], - ); - final bool aotSymbolsFound = (symbols.stdout as String).contains('_kDartVmSnapshot'); - expect(aotSymbolsFound, buildMode != BuildMode.debug); + final List symbols = + AppleTestUtils.getExportedSymbols(outputAppFrameworkBinary.path); + if (buildMode == BuildMode.debug) { + expect(symbols, isEmpty); + } else { + expect(symbols, equals(AppleTestUtils.requiredSymbols)); + } }); - // dSYM is not created for a debug build so nothing to check. - if (buildMode != BuildMode.debug) { - testWithoutContext('check symbols in dSYM', () { - final ProcessResult nm = processManager.runSync( - [ - 'nm', - '--debug-syms', - '--defined-only', - '--just-symbol-name', - buildAppFrameworkDsymBinary.path, - '-arch', - 'arm64', - ], - ); - final List symbols = (nm.stdout as String).split('\n'); - expect(symbols, contains('_kDartVmSnapshotInstructions')); - }); - } + testWithoutContext('check symbols in dSYM', () { + if (buildMode == BuildMode.debug) { + // dSYM is not created for a debug build. + expect(buildAppFrameworkDsymBinary.existsSync(), isFalse); + } else { + final List symbols = + AppleTestUtils.getExportedSymbols(buildAppFrameworkDsymBinary.path); + expect(symbols, containsAll(AppleTestUtils.requiredSymbols)); + // The actual number of symbols is going to vary but there should + // be "many" in the dSYM. At the time of writing, it was 7656. + expect(symbols.length, greaterThanOrEqualTo(5000)); + } + }); testWithoutContext('xcode_backend embed_and_thin', () { outputFlutterFramework.deleteSync(recursive: true); diff --git a/packages/flutter_tools/test/host_cross_arch.shard/macos_content_validation_test.dart b/packages/flutter_tools/test/host_cross_arch.shard/macos_content_validation_test.dart index 4502f380f5..db0d5c65d6 100644 --- a/packages/flutter_tools/test/host_cross_arch.shard/macos_content_validation_test.dart +++ b/packages/flutter_tools/test/host_cross_arch.shard/macos_content_validation_test.dart @@ -87,19 +87,27 @@ void main() { 'App.framework', )); - _checkFatBinary( - outputAppFramework.childFile('App'), - buildModeLower, - 'dynamically linked shared library', - ); + final File libBinary = outputAppFramework.childFile('App'); + final File libDsymBinary = + buildPath.childFile('App.framework.dSYM/Contents/Resources/DWARF/App'); - // dSYM is not created for a debug build so nothing to check. - if (buildMode != 'Debug') { - _checkFatBinary( - buildPath.childFile('App.framework.dSYM/Contents/Resources/DWARF/App'), - buildModeLower, - 'dSYM companion file', - ); + _checkFatBinary(libBinary, buildModeLower, 'dynamically linked shared library'); + + final List libSymbols = AppleTestUtils.getExportedSymbols(libBinary.path); + + if (buildMode == 'Debug') { + // dSYM is not created for a debug build. + expect(libDsymBinary.existsSync(), isFalse); + expect(libSymbols, isEmpty); + } else { + _checkFatBinary(libDsymBinary, buildModeLower, 'dSYM companion file'); + expect(libSymbols, equals(AppleTestUtils.requiredSymbols)); + final List dSymSymbols = + AppleTestUtils.getExportedSymbols(libDsymBinary.path); + expect(dSymSymbols, containsAll(AppleTestUtils.requiredSymbols)); + // The actual number of symbols is going to vary but there should + // be "many" in the dSYM. At the time of writing, it was 19195. + expect(dSymSymbols.length, greaterThanOrEqualTo(15000)); } expect(outputAppFramework.childLink('Resources'), exists); diff --git a/packages/flutter_tools/test/integration.shard/test_utils.dart b/packages/flutter_tools/test/integration.shard/test_utils.dart index e3c1d5a52f..e0278c7f94 100644 --- a/packages/flutter_tools/test/integration.shard/test_utils.dart +++ b/packages/flutter_tools/test/integration.shard/test_utils.dart @@ -99,3 +99,31 @@ Future pollForServiceExtensionValue({ " attempts responded with '$continuePollingValue'.", ); } + +class AppleTestUtils { + // static only + AppleTestUtils._(); + + static const List requiredSymbols = [ + '_kDartIsolateSnapshotData', + '_kDartIsolateSnapshotInstructions', + '_kDartVmSnapshotData', + '_kDartVmSnapshotInstructions' + ]; + + static List getExportedSymbols(String dwarfPath) { + final ProcessResult nm = processManager.runSync( + [ + 'nm', + '--debug-syms', // nm docs: 'Show all symbols, even debugger only' + '--defined-only', + '--just-symbol-name', + dwarfPath, + '-arch', + 'arm64', + ], + ); + final String nmOutput = (nm.stdout as String).trim(); + return nmOutput.isEmpty ? const [] : nmOutput.split('\n'); + } +}