From 840d7dd68a7af5fb660e80ca5a923e0adccb26b8 Mon Sep 17 00:00:00 2001 From: Tess Strickland Date: Thu, 8 Jun 2023 13:17:18 +0200 Subject: [PATCH] Use `--target-os` for appropriate precompiled targets. (#127567) This PR adds uses of the `--target-os` command line argument when building kernel sources for precompiled applications for supported target operating systems. The Dart CFE then: * treats `Platform.operatingSystem` as if it were defined as the constant string provided as an argument to the flag, * treats `Platform.pathSeparator` as the appropriate separator for that operating system, * attempts to constant evaluate the initializer for any field annotated with the `vm:platform-const` pragma, and * attempts to constant evaluate all calls to a method annotated with the `vm:platform-const` pragma. The `vm:platform-const` pragma can appear in either library or user code. If the attempt to constant evaluate the field initializer or method call fails, then an error is thrown at kernel compilation time. Addresses #14233. The tests in `packages/flutter_tools/test/general.shard/build_system/targets/common_test.dart` have been adjusted properly to account for the new passed command line arguments. --- .../lib/src/build_system/targets/common.dart | 16 ++++++++++++++++ packages/flutter_tools/lib/src/compile.dart | 6 ++++++ .../build_system/targets/common_test.dart | 8 ++++++++ 3 files changed, 30 insertions(+) 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 82eca5b9ba..039bb5a7fd 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/common.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/common.dart @@ -206,6 +206,21 @@ class KernelSnapshot extends Target { forceLinkPlatform = false; } + final String? targetOS = switch (targetPlatform) { + TargetPlatform.fuchsia_arm64 || TargetPlatform.fuchsia_x64 => 'fuchsia', + TargetPlatform.android || + TargetPlatform.android_arm || + TargetPlatform.android_arm64 || + TargetPlatform.android_x64 || + TargetPlatform.android_x86 => + 'android', + TargetPlatform.darwin => 'macos', + TargetPlatform.ios => 'ios', + TargetPlatform.linux_arm64 || TargetPlatform.linux_x64 => 'linux', + TargetPlatform.windows_x64 => 'windows', + TargetPlatform.tester || TargetPlatform.web_javascript => null, + }; + final PackageConfig packageConfig = await loadPackageConfigWithLogging( packagesFile, logger: environment.logger, @@ -234,6 +249,7 @@ class KernelSnapshot extends Target { dartDefines: decodeDartDefines(environment.defines, kDartDefines), packageConfig: packageConfig, buildDir: environment.buildDir, + targetOS: targetOS, checkDartPluginRegistry: environment.generateDartPluginRegistry, ); if (output == null || output.errorCount != 0) { diff --git a/packages/flutter_tools/lib/src/compile.dart b/packages/flutter_tools/lib/src/compile.dart index 3199e9c902..6774080a54 100644 --- a/packages/flutter_tools/lib/src/compile.dart +++ b/packages/flutter_tools/lib/src/compile.dart @@ -233,6 +233,7 @@ class KernelCompiler { String? initializeFromDill, String? platformDill, Directory? buildDir, + String? targetOS, bool checkDartPluginRegistry = false, required String? packagesPath, required BuildMode buildMode, @@ -293,6 +294,11 @@ class KernelCompiler { if (aot) ...[ '--aot', '--tfa', + // The --target-os flag only makes sense for whole program compilation. + if (targetOS != null) ...[ + '--target-os', + targetOS, + ], ], if (packagesPath != null) ...[ '--packages', 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 6adbd025df..5aab1be653 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 @@ -94,6 +94,8 @@ void main() { '--track-widget-creation', '--aot', '--tfa', + '--target-os', + 'android', '--packages', '/.dart_tool/package_config.json', '--output-dill', @@ -132,6 +134,8 @@ void main() { '--track-widget-creation', '--aot', '--tfa', + '--target-os', + 'android', '--packages', '/.dart_tool/package_config.json', '--output-dill', @@ -171,6 +175,8 @@ void main() { '--track-widget-creation', '--aot', '--tfa', + '--target-os', + 'android', '--packages', '/.dart_tool/package_config.json', '--output-dill', @@ -211,6 +217,8 @@ void main() { '--track-widget-creation', '--aot', '--tfa', + '--target-os', + 'android', '--packages', '/.dart_tool/package_config.json', '--output-dill',