From ab260bacd288b621418c12b60cd2f4ecd20cd798 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Mon, 28 Oct 2019 09:37:29 -0700 Subject: [PATCH] Refactor BuildMode into class, add jit_release configuration (#42476) --- .../flutter_tools/lib/src/build_info.dart | 85 ++++++++++++++----- .../lib/src/build_system/targets/dart.dart | 1 - .../test/general.shard/build_info_test.dart | 24 ++++++ 3 files changed, 88 insertions(+), 22 deletions(-) diff --git a/packages/flutter_tools/lib/src/build_info.dart b/packages/flutter_tools/lib/src/build_info.dart index e0cc68c0f3c..fc6a8c8339e 100644 --- a/packages/flutter_tools/lib/src/build_info.dart +++ b/packages/flutter_tools/lib/src/build_info.dart @@ -113,35 +113,78 @@ class AndroidBuildInfo { final Iterable targetArchs; } -/// The type of build. -enum BuildMode { - debug, - profile, - release, -} +/// A summary of the compilation strategy used for Dart. +class BuildMode { + const BuildMode._(this.name); -const List _kBuildModes = [ - 'debug', - 'profile', - 'release', -]; + factory BuildMode.fromName(String value) { + switch (value) { + case 'debug': + return BuildMode.debug; + case 'profile': + return BuildMode.profile; + case 'release': + return BuildMode.release; + case 'jit_release': + return BuildMode.jitRelease; + } + throw ArgumentError('$value is not a supported build mode'); + } + + /// Built in JIT mode with no optimizations, enabled asserts, and an observatory. + static const BuildMode debug = BuildMode._('debug'); + + /// Built in AOT mode with some optimizations and an observatory. + static const BuildMode profile = BuildMode._('profile'); + + /// Built in AOT mode with all optimizations and no observatory. + static const BuildMode release = BuildMode._('release'); + + /// Built in JIT mode with all optimizations and no observatory. + static const BuildMode jitRelease = BuildMode._('jit_release'); + + static const List values = [ + debug, + profile, + release, + jitRelease, + ]; + static const Set releaseModes = { + release, + jitRelease, + }; + static const Set jitModes = { + debug, + jitRelease, + }; + + /// Whether this mode is considered release. + /// + /// Useful for determining whether we should enable/disable asserts or + /// other development features. + bool get isRelease => releaseModes.contains(this); + + /// Whether this mode is using the jit runtime. + bool get isJit => jitModes.contains(this); + + /// Whether this mode is using the precompiled runtime. + bool get isPrecompiled => !isJit; + + /// The name for this build mode. + final String name; + + @override + String toString() => name; +} /// Return the name for the build mode, or "any" if null. String getNameForBuildMode(BuildMode buildMode) { - return _kBuildModes[buildMode.index]; + return buildMode.name; } /// Returns the [BuildMode] for a particular `name`. BuildMode getBuildModeForName(String name) { - switch (name) { - case 'debug': - return BuildMode.debug; - case 'profile': - return BuildMode.profile; - case 'release': - return BuildMode.release; - } - return null; + return BuildMode.fromName(name); } String validatedBuildNumberForPlatform(TargetPlatform targetPlatform, String buildNumber) { diff --git a/packages/flutter_tools/lib/src/build_system/targets/dart.dart b/packages/flutter_tools/lib/src/build_system/targets/dart.dart index 5260404c695..ed4945fa315 100644 --- a/packages/flutter_tools/lib/src/build_system/targets/dart.dart +++ b/packages/flutter_tools/lib/src/build_system/targets/dart.dart @@ -232,7 +232,6 @@ class KernelSnapshot extends Target { } } - /// Supports compiling a dart kernel file to an ELF binary. abstract class AotElfBase extends Target { const AotElfBase(); diff --git a/packages/flutter_tools/test/general.shard/build_info_test.dart b/packages/flutter_tools/test/general.shard/build_info_test.dart index d404696ecae..9b5c640653d 100644 --- a/packages/flutter_tools/test/general.shard/build_info_test.dart +++ b/packages/flutter_tools/test/general.shard/build_info_test.dart @@ -53,5 +53,29 @@ void main() { buildName = validatedBuildNameForPlatform(TargetPlatform.android_arm, 'abc+-'); expect(buildName, 'abc+-'); }); + + test('build mode configuration is correct', () { + expect(BuildMode.debug.isRelease, false); + expect(BuildMode.debug.isPrecompiled, false); + expect(BuildMode.debug.isJit, true); + + expect(BuildMode.profile.isRelease, false); + expect(BuildMode.profile.isPrecompiled, true); + expect(BuildMode.profile.isJit, false); + + expect(BuildMode.release.isRelease, true); + expect(BuildMode.release.isPrecompiled, true); + expect(BuildMode.release.isJit, false); + + expect(BuildMode.jitRelease.isRelease, true); + expect(BuildMode.jitRelease.isPrecompiled, false); + expect(BuildMode.jitRelease.isJit, true); + + expect(BuildMode.fromName('debug'), BuildMode.debug); + expect(BuildMode.fromName('profile'), BuildMode.profile); + expect(BuildMode.fromName('jit_release'), BuildMode.jitRelease); + expect(BuildMode.fromName('release'), BuildMode.release); + expect(() => BuildMode.fromName('foo'), throwsA(isInstanceOf())); + }); }); }