From 6ab00756a8e30f71f0dc30b1f12333479e7f2757 Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Tue, 14 Dec 2021 16:49:06 -0800 Subject: [PATCH] Build Flutter iOS plugins with all valid architectures (#95293) --- packages/flutter_tools/bin/podhelper.rb | 11 ++- .../ios_content_validation_test.dart | 70 ++++++++++++++++--- 2 files changed, 68 insertions(+), 13 deletions(-) diff --git a/packages/flutter_tools/bin/podhelper.rb b/packages/flutter_tools/bin/podhelper.rb index 6a8185e9d3..97e073dd84 100644 --- a/packages/flutter_tools/bin/podhelper.rb +++ b/packages/flutter_tools/bin/podhelper.rb @@ -33,9 +33,6 @@ end def flutter_additional_ios_build_settings(target) return unless target.platform_name == :ios - # Return if it's not a Flutter plugin (transitive dependency). - return unless target.dependencies.any? { |dependency| dependency.name == 'Flutter' } - # [target.deployment_target] is a [String] formatted as "8.0". inherit_deployment_target = target.deployment_target[/\d+/].to_i < 9 @@ -52,6 +49,14 @@ def flutter_additional_ios_build_settings(target) release_framework_dir = File.expand_path(File.join(artifacts_dir, 'ios-release', 'Flutter.xcframework'), __FILE__) target.build_configurations.each do |build_configuration| + # Build both x86_64 and arm64 simulator archs for all dependencies. If a single plugin does not support arm64 simulators, + # the app and all frameworks will fall back to x86_64. Unfortunately that case is not detectable in this script. + # Therefore all pods must have a x86_64 slice available, or linking a x86_64 app will fail. + build_configuration.build_settings['ONLY_ACTIVE_ARCH'] = 'NO' if build_configuration.type == :debug + + # Skip other updates if it's not a Flutter plugin (transitive dependency). + next unless target.dependencies.any? { |dependency| dependency.name == 'Flutter' } + # Profile can't be derived from the CocoaPods build configuration. Use release framework (for linking only). configuration_engine_dir = build_configuration.type == :debug ? debug_framework_dir : release_framework_dir Dir.new(configuration_engine_dir).each_child do |xcframework_file| diff --git a/packages/flutter_tools/test/integration.shard/ios_content_validation_test.dart b/packages/flutter_tools/test/integration.shard/ios_content_validation_test.dart index 1d3315683d..e8cb730f82 100644 --- a/packages/flutter_tools/test/integration.shard/ios_content_validation_test.dart +++ b/packages/flutter_tools/test/integration.shard/ios_content_validation_test.dart @@ -16,6 +16,7 @@ import 'test_utils.dart'; void main() { group('iOS app validation', () { String flutterRoot; + Directory pluginRoot; String projectRoot; String flutterBin; Directory tempDir; @@ -29,18 +30,20 @@ void main() { 'flutter', ); + // Test a plugin example app to allow plugins validation. processManager.runSync([ flutterBin, ...getLocalEngineArguments(), 'create', '--verbose', '--platforms=ios', - '-i', - 'objc', + '-t', + 'plugin', 'hello', ], workingDirectory: tempDir.path); - projectRoot = tempDir.childDirectory('hello').path; + pluginRoot = tempDir.childDirectory('hello'); + projectRoot = pluginRoot.childDirectory('example').path; }); tearDownAll(() { @@ -56,6 +59,7 @@ void main() { File outputFlutterFrameworkBinary; Directory outputAppFramework; File outputAppFrameworkBinary; + File outputPluginFrameworkBinary; setUpAll(() { processManager.runSync([ @@ -85,11 +89,13 @@ void main() { outputAppFramework = frameworkDirectory.childDirectory('App.framework'); outputAppFrameworkBinary = outputAppFramework.childFile('App'); + + outputPluginFrameworkBinary = frameworkDirectory.childDirectory('hello.framework').childFile('hello'); }); testWithoutContext('flutter build ios builds a valid app', () { - // Should only contain Flutter.framework and App.framework. - expect(frameworkDirectory.listSync().length, 2); + expect(outputPluginFrameworkBinary, exists); + expect(outputAppFrameworkBinary, exists); expect(outputAppFramework.childFile('Info.plist'), exists); @@ -202,17 +208,61 @@ void main() { }, skip: !platform.isMacOS || buildMode != BuildMode.release); // [intended] only makes sense on macos. testWithoutContext('validate obfuscation', () { - final ProcessResult grepResult = processManager.runSync([ + // HelloPlugin class is present in project. + ProcessResult grepResult = processManager.runSync([ 'grep', - '-i', - 'hello', + '-r', + 'HelloPlugin', + pluginRoot.path, + ]); + // Matches exits 0. + expect(grepResult.exitCode, 0); + + // Not present in binary. + grepResult = processManager.runSync([ + 'grep', + 'HelloPlugin', outputAppFrameworkBinary.path, ]); - expect(grepResult.stdout, isNot(contains('matches'))); + // Does not match exits 1. + expect(grepResult.exitCode, 1); }); }); } + testWithoutContext('builds all plugin architectures for simulator', () { + final ProcessResult buildSimulator = processManager.runSync( + [ + flutterBin, + ...getLocalEngineArguments(), + 'build', + 'ios', + '--simulator', + '--verbose', + '--no-codesign', + ], + workingDirectory: projectRoot, + ); + expect(buildSimulator.exitCode, 0); + + final File pluginFrameworkBinary = fileSystem.file(fileSystem.path.join( + projectRoot, + 'build', + 'ios', + 'iphonesimulator', + 'Runner.app', + 'Frameworks', + 'hello.framework', + 'hello', + )); + expect(pluginFrameworkBinary, exists); + final ProcessResult archs = processManager.runSync( + ['file', pluginFrameworkBinary.path], + ); + expect(archs.stdout, contains('Mach-O 64-bit dynamically linked shared library x86_64')); + expect(archs.stdout, contains('Mach-O 64-bit dynamically linked shared library arm64')); + }); + testWithoutContext('build for simulator with all available architectures', () { final ProcessResult buildSimulator = processManager.runSync( [ @@ -250,6 +300,6 @@ void main() { expect(archs.stdout, contains('Mach-O 64-bit dynamically linked shared library arm64')); }); }, skip: !platform.isMacOS, // [intended] only makes sense for macos platform. - timeout: const Timeout(Duration(minutes: 5)) + timeout: const Timeout(Duration(minutes: 7)) ); }