Dual compile reland (#143262)
This is an attempt at a reland of https://github.com/flutter/flutter/pull/141396 The main changes here that are different than the original PR is fixes to wire up the `flutter test` command properly with the web renderer.
This commit is contained in:
@@ -24,7 +24,6 @@ import '../version.dart';
|
||||
import 'compiler_config.dart';
|
||||
import 'file_generators/flutter_service_worker_js.dart';
|
||||
import 'migrations/scrub_generated_plugin_registrant.dart';
|
||||
import 'web_constants.dart';
|
||||
|
||||
export 'compiler_config.dart';
|
||||
|
||||
@@ -67,23 +66,14 @@ class WebBuilder {
|
||||
String target,
|
||||
BuildInfo buildInfo,
|
||||
ServiceWorkerStrategy serviceWorkerStrategy, {
|
||||
required WebCompilerConfig compilerConfig,
|
||||
required List<WebCompilerConfig> compilerConfigs,
|
||||
String? baseHref,
|
||||
String? outputDirectoryPath,
|
||||
}) async {
|
||||
if (compilerConfig.isWasm) {
|
||||
globals.logger.printBox(
|
||||
title: 'Experimental feature',
|
||||
'''
|
||||
WebAssembly compilation is experimental.
|
||||
$kWasmMoreInfo''',
|
||||
);
|
||||
}
|
||||
|
||||
final bool hasWebPlugins =
|
||||
(await findPlugins(flutterProject)).any((Plugin p) => p.platforms.containsKey(WebPlugin.kConfigKey));
|
||||
final Directory outputDirectory = outputDirectoryPath == null
|
||||
? _fileSystem.directory(getWebBuildDirectory(compilerConfig.isWasm))
|
||||
? _fileSystem.directory(getWebBuildDirectory())
|
||||
: _fileSystem.directory(outputDirectoryPath);
|
||||
outputDirectory.createSync(recursive: true);
|
||||
|
||||
@@ -99,11 +89,7 @@ class WebBuilder {
|
||||
final Stopwatch sw = Stopwatch()..start();
|
||||
try {
|
||||
final BuildResult result = await _buildSystem.build(
|
||||
globals.buildTargets.webServiceWorker(
|
||||
_fileSystem,
|
||||
webRenderer: buildInfo.webRenderer,
|
||||
isWasm: compilerConfig.isWasm,
|
||||
),
|
||||
globals.buildTargets.webServiceWorker(_fileSystem, compilerConfigs),
|
||||
Environment(
|
||||
projectDir: _fileSystem.currentDirectory,
|
||||
outputDir: outputDirectory,
|
||||
@@ -113,7 +99,6 @@ class WebBuilder {
|
||||
kHasWebPlugins: hasWebPlugins.toString(),
|
||||
if (baseHref != null) kBaseHref: baseHref,
|
||||
kServiceWorkerStrategy: serviceWorkerStrategy.cliName,
|
||||
...compilerConfig.toBuildSystemEnvironment(),
|
||||
...buildInfo.toBuildSystemEnvironment(),
|
||||
},
|
||||
artifacts: globals.artifacts!,
|
||||
@@ -146,8 +131,7 @@ class WebBuilder {
|
||||
}
|
||||
|
||||
final String buildSettingsString = _buildEventAnalyticsSettings(
|
||||
config: compilerConfig,
|
||||
buildInfo: buildInfo,
|
||||
configs: compilerConfigs,
|
||||
);
|
||||
|
||||
BuildEvent(
|
||||
@@ -163,14 +147,15 @@ class WebBuilder {
|
||||
));
|
||||
|
||||
final Duration elapsedDuration = sw.elapsed;
|
||||
final String variableName = compilerConfigs.length > 1 ? 'dual-compile' : 'dart2js';
|
||||
_flutterUsage.sendTiming(
|
||||
'build',
|
||||
compilerConfig.isWasm ? 'dart2wasm' : 'dart2js',
|
||||
variableName,
|
||||
elapsedDuration,
|
||||
);
|
||||
_analytics.send(Event.timing(
|
||||
workflow: 'build',
|
||||
variableName: compilerConfig.isWasm ? 'dart2wasm' : 'dart2js',
|
||||
variableName: variableName,
|
||||
elapsedMilliseconds: elapsedDuration.inMilliseconds,
|
||||
));
|
||||
}
|
||||
@@ -222,6 +207,16 @@ enum WebRendererMode implements CliEnum {
|
||||
'FLUTTER_WEB_USE_SKWASM=true',
|
||||
]
|
||||
};
|
||||
|
||||
List<String> updateDartDefines(List<String> inputDefines) {
|
||||
final Set<String> dartDefinesSet = inputDefines.toSet();
|
||||
if (!inputDefines.any((String d) => d.startsWith('FLUTTER_WEB_AUTO_DETECT='))
|
||||
&& inputDefines.any((String d) => d.startsWith('FLUTTER_WEB_USE_SKIA='))) {
|
||||
dartDefinesSet.removeWhere((String d) => d.startsWith('FLUTTER_WEB_USE_SKIA='));
|
||||
}
|
||||
dartDefinesSet.addAll(dartDefines);
|
||||
return dartDefinesSet.toList();
|
||||
}
|
||||
}
|
||||
|
||||
/// The correct precompiled artifact to use for each build and render mode.
|
||||
@@ -257,13 +252,18 @@ const Map<WebRendererMode, Map<NullSafetyMode, HostArtifact>> kDartSdkJsMapArtif
|
||||
};
|
||||
|
||||
String _buildEventAnalyticsSettings({
|
||||
required WebCompilerConfig config,
|
||||
required BuildInfo buildInfo,
|
||||
required List<WebCompilerConfig> configs,
|
||||
}) {
|
||||
final Map<String, Object> values = <String, Object>{
|
||||
...config.buildEventAnalyticsValues,
|
||||
'web-renderer': buildInfo.webRenderer.cliName,
|
||||
};
|
||||
final Map<String, Object> values = <String, Object>{};
|
||||
final List<String> renderers = <String>[];
|
||||
final List<String> targets = <String>[];
|
||||
for (final WebCompilerConfig config in configs) {
|
||||
values.addAll(config.buildEventAnalyticsValues);
|
||||
renderers.add(config.renderer.name);
|
||||
targets.add(config.compileTarget.name);
|
||||
}
|
||||
values['web-renderer'] = renderers.join(',');
|
||||
values['web-target'] = targets.join(',');
|
||||
|
||||
final List<String> sortedList = values.entries
|
||||
.map((MapEntry<String, Object> e) => '${e.key}: ${e.value};')
|
||||
|
||||
@@ -3,58 +3,48 @@
|
||||
// found in the LICENSE file.
|
||||
|
||||
import '../base/utils.dart';
|
||||
import '../convert.dart';
|
||||
import 'compile.dart';
|
||||
|
||||
abstract class WebCompilerConfig {
|
||||
const WebCompilerConfig();
|
||||
enum CompileTarget {
|
||||
js,
|
||||
wasm,
|
||||
}
|
||||
|
||||
/// Returns `true` if `this` represents configuration for the Wasm compiler.
|
||||
///
|
||||
/// Otherwise, `false`–represents the JavaScript compiler.
|
||||
bool get isWasm;
|
||||
sealed class WebCompilerConfig {
|
||||
const WebCompilerConfig({required this.renderer});
|
||||
|
||||
Map<String, String> toBuildSystemEnvironment();
|
||||
/// Returns which target this compiler outputs (js or wasm)
|
||||
CompileTarget get compileTarget;
|
||||
final WebRendererMode renderer;
|
||||
|
||||
Map<String, Object> get buildEventAnalyticsValues => <String, Object>{
|
||||
'wasm-compile': isWasm,
|
||||
};
|
||||
String get buildKey;
|
||||
|
||||
Map<String, Object> get buildEventAnalyticsValues => <String, Object>{};
|
||||
}
|
||||
|
||||
/// Configuration for the Dart-to-Javascript compiler (dart2js).
|
||||
class JsCompilerConfig extends WebCompilerConfig {
|
||||
const JsCompilerConfig({
|
||||
required this.csp,
|
||||
required this.dumpInfo,
|
||||
required this.nativeNullAssertions,
|
||||
required this.optimizationLevel,
|
||||
required this.noFrequencyBasedMinification,
|
||||
required this.sourceMaps,
|
||||
this.csp = false,
|
||||
this.dumpInfo = false,
|
||||
this.nativeNullAssertions = false,
|
||||
this.optimizationLevel = kDart2jsDefaultOptimizationLevel,
|
||||
this.noFrequencyBasedMinification = false,
|
||||
this.sourceMaps = true,
|
||||
super.renderer = WebRendererMode.auto,
|
||||
});
|
||||
|
||||
/// Instantiates [JsCompilerConfig] suitable for the `flutter run` command.
|
||||
const JsCompilerConfig.run({required bool nativeNullAssertions})
|
||||
: this(
|
||||
csp: false,
|
||||
dumpInfo: false,
|
||||
const JsCompilerConfig.run({
|
||||
required bool nativeNullAssertions,
|
||||
required WebRendererMode renderer,
|
||||
}) : this(
|
||||
nativeNullAssertions: nativeNullAssertions,
|
||||
noFrequencyBasedMinification: false,
|
||||
optimizationLevel: kDart2jsDefaultOptimizationLevel,
|
||||
sourceMaps: true,
|
||||
renderer: renderer,
|
||||
);
|
||||
|
||||
/// Creates a new [JsCompilerConfig] from build system environment values.
|
||||
///
|
||||
/// Should correspond exactly with [toBuildSystemEnvironment].
|
||||
factory JsCompilerConfig.fromBuildSystemEnvironment(
|
||||
Map<String, String> defines) =>
|
||||
JsCompilerConfig(
|
||||
csp: defines[kCspMode] == 'true',
|
||||
dumpInfo: defines[kDart2jsDumpInfo] == 'true',
|
||||
nativeNullAssertions: defines[kNativeNullAssertions] == 'true',
|
||||
optimizationLevel: defines[kDart2jsOptimization] ?? kDart2jsDefaultOptimizationLevel,
|
||||
noFrequencyBasedMinification: defines[kDart2jsNoFrequencyBasedMinification] == 'true',
|
||||
sourceMaps: defines[kSourceMapsEnabled] == 'true',
|
||||
);
|
||||
|
||||
/// The default optimization level for dart2js.
|
||||
///
|
||||
/// Maps to [kDart2jsOptimization].
|
||||
@@ -102,17 +92,7 @@ class JsCompilerConfig extends WebCompilerConfig {
|
||||
final bool sourceMaps;
|
||||
|
||||
@override
|
||||
bool get isWasm => false;
|
||||
|
||||
@override
|
||||
Map<String, String> toBuildSystemEnvironment() => <String, String>{
|
||||
kCspMode: csp.toString(),
|
||||
kDart2jsDumpInfo: dumpInfo.toString(),
|
||||
kNativeNullAssertions: nativeNullAssertions.toString(),
|
||||
kDart2jsNoFrequencyBasedMinification: noFrequencyBasedMinification.toString(),
|
||||
kDart2jsOptimization: optimizationLevel,
|
||||
kSourceMapsEnabled: sourceMaps.toString(),
|
||||
};
|
||||
CompileTarget get compileTarget => CompileTarget.js;
|
||||
|
||||
/// Arguments to use in both phases: full JS compile and CFE-only.
|
||||
List<String> toSharedCommandOptions() => <String>[
|
||||
@@ -130,25 +110,29 @@ class JsCompilerConfig extends WebCompilerConfig {
|
||||
if (noFrequencyBasedMinification) '--no-frequency-based-minification',
|
||||
if (csp) '--csp',
|
||||
];
|
||||
|
||||
@override
|
||||
String get buildKey {
|
||||
final Map<String, dynamic> settings = <String, dynamic>{
|
||||
'csp': csp,
|
||||
'dumpInfo': dumpInfo,
|
||||
'nativeNullAssertions': nativeNullAssertions,
|
||||
'noFrequencyBasedMinification': noFrequencyBasedMinification,
|
||||
'optimizationLevel': optimizationLevel,
|
||||
'sourceMaps': sourceMaps,
|
||||
};
|
||||
return jsonEncode(settings);
|
||||
}
|
||||
}
|
||||
|
||||
/// Configuration for the Wasm compiler.
|
||||
class WasmCompilerConfig extends WebCompilerConfig {
|
||||
const WasmCompilerConfig({
|
||||
required this.omitTypeChecks,
|
||||
required this.wasmOpt,
|
||||
this.omitTypeChecks = false,
|
||||
this.wasmOpt = WasmOptLevel.defaultValue,
|
||||
super.renderer = WebRendererMode.auto,
|
||||
});
|
||||
|
||||
/// Creates a new [WasmCompilerConfig] from build system environment values.
|
||||
///
|
||||
/// Should correspond exactly with [toBuildSystemEnvironment].
|
||||
factory WasmCompilerConfig.fromBuildSystemEnvironment(
|
||||
Map<String, String> defines) =>
|
||||
WasmCompilerConfig(
|
||||
omitTypeChecks: defines[kOmitTypeChecks] == 'true',
|
||||
wasmOpt: WasmOptLevel.values.byName(defines[kRunWasmOpt]!),
|
||||
);
|
||||
|
||||
/// Build environment for [omitTypeChecks].
|
||||
static const String kOmitTypeChecks = 'WasmOmitTypeChecks';
|
||||
|
||||
@@ -162,25 +146,31 @@ class WasmCompilerConfig extends WebCompilerConfig {
|
||||
final WasmOptLevel wasmOpt;
|
||||
|
||||
@override
|
||||
bool get isWasm => true;
|
||||
CompileTarget get compileTarget => CompileTarget.wasm;
|
||||
|
||||
bool get runWasmOpt => wasmOpt == WasmOptLevel.full || wasmOpt == WasmOptLevel.debug;
|
||||
|
||||
@override
|
||||
Map<String, String> toBuildSystemEnvironment() => <String, String>{
|
||||
kOmitTypeChecks: omitTypeChecks.toString(),
|
||||
kRunWasmOpt: wasmOpt.name,
|
||||
};
|
||||
bool get runWasmOpt =>
|
||||
wasmOpt == WasmOptLevel.full || wasmOpt == WasmOptLevel.debug;
|
||||
|
||||
List<String> toCommandOptions() => <String>[
|
||||
if (omitTypeChecks) '--omit-type-checks',
|
||||
];
|
||||
if (omitTypeChecks) '--omit-type-checks',
|
||||
];
|
||||
|
||||
@override
|
||||
Map<String, Object> get buildEventAnalyticsValues => <String, Object>{
|
||||
...super.buildEventAnalyticsValues,
|
||||
...toBuildSystemEnvironment(),
|
||||
};
|
||||
...super.buildEventAnalyticsValues,
|
||||
kOmitTypeChecks: omitTypeChecks.toString(),
|
||||
kRunWasmOpt: wasmOpt.name,
|
||||
};
|
||||
|
||||
@override
|
||||
String get buildKey {
|
||||
final Map<String, dynamic> settings = <String, dynamic>{
|
||||
'omitTypeChecks': omitTypeChecks,
|
||||
'wasmOpt': wasmOpt.name,
|
||||
};
|
||||
return jsonEncode(settings);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
enum WasmOptLevel implements CliEnum {
|
||||
@@ -195,8 +185,11 @@ enum WasmOptLevel implements CliEnum {
|
||||
|
||||
@override
|
||||
String get helpText => switch (this) {
|
||||
WasmOptLevel.none => 'wasm-opt is not run. Fastest build; bigger, slower output.',
|
||||
WasmOptLevel.debug => 'Similar to `${WasmOptLevel.full.name}`, but member names are preserved. Debugging is easier, but size is a bit bigger.',
|
||||
WasmOptLevel.full => 'wasm-opt is run. Build time is slower, but output is smaller and faster.',
|
||||
};
|
||||
WasmOptLevel.none =>
|
||||
'wasm-opt is not run. Fastest build; bigger, slower output.',
|
||||
WasmOptLevel.debug =>
|
||||
'Similar to `${WasmOptLevel.full.name}`, but member names are preserved. Debugging is easier, but size is a bit bigger.',
|
||||
WasmOptLevel.full =>
|
||||
'wasm-opt is run. Build time is slower, but output is smaller and faster.',
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user