build_info.dart 5.41 KB
Newer Older
1 2 3 4
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
import 'base/context.dart';
6
import 'base/file_system.dart';
7
import 'base/platform.dart';
8
import 'base/utils.dart';
9
import 'globals.dart';
10

11 12
/// Information about a build to be performed or used.
class BuildInfo {
13
  const BuildInfo(this.mode, this.flavor, { this.previewDart2 });
14 15 16 17 18 19 20 21 22 23

  final BuildMode mode;
  /// Represents a custom Android product flavor or an Xcode scheme, null for
  /// using the default.
  ///
  /// If not null, the Gradle build task will be `assembleFlavorMode` (e.g.
  /// `assemblePaidRelease`), and the Xcode build configuration will be
  /// Mode-Flavor (e.g. Release-Paid).
  final String flavor;

24 25 26
  // Whether build should be done using Dart2 Frontend parser.
  final bool previewDart2;

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  static const BuildInfo debug = const BuildInfo(BuildMode.debug, null);
  static const BuildInfo profile = const BuildInfo(BuildMode.profile, null);
  static const BuildInfo release = const BuildInfo(BuildMode.release, null);

  /// Returns whether a debug build is requested.
  ///
  /// Exactly one of [isDebug], [isProfile], or [isRelease] is true.
  bool get isDebug => mode == BuildMode.debug;

  /// Returns whether a profile build is requested.
  ///
  /// Exactly one of [isDebug], [isProfile], or [isRelease] is true.
  bool get isProfile => mode == BuildMode.profile;

  /// Returns whether a release build is requested.
  ///
  /// Exactly one of [isDebug], [isProfile], or [isRelease] is true.
  bool get isRelease => mode == BuildMode.release;

  bool get usesAot => isAotBuildMode(mode);
  bool get supportsEmulator => isEmulatorBuildMode(mode);
  bool get supportsSimulator => isEmulatorBuildMode(mode);
  String get modeName => getModeName(mode);
50 51
}

52
/// The type of build - `debug`, `profile`, or `release`.
53 54
enum BuildMode {
  debug,
55
  profile,
56
  release
57 58
}

59
String getModeName(BuildMode mode) => getEnumName(mode);
60

61 62 63 64 65 66 67 68 69 70
BuildMode getBuildModeForName(String mode) {
  if (mode == 'debug')
    return BuildMode.debug;
  if (mode == 'profile')
    return BuildMode.profile;
  if (mode == 'release')
    return BuildMode.release;
  return null;
}

71 72 73 74 75
// Returns true if the selected build mode uses ahead-of-time compilation.
bool isAotBuildMode(BuildMode mode) {
  return mode == BuildMode.profile || mode == BuildMode.release;
}

76 77 78
// Returns true if the given build mode can be used on emulators / simulators.
bool isEmulatorBuildMode(BuildMode mode) => mode == BuildMode.debug;

79
enum HostPlatform {
80 81
  darwin_x64,
  linux_x64,
82
  windows_x64,
83 84 85
}

String getNameForHostPlatform(HostPlatform platform) {
86 87
  switch (platform) {
    case HostPlatform.darwin_x64:
88
      return 'darwin-x64';
89
    case HostPlatform.linux_x64:
90
      return 'linux-x64';
91 92
    case HostPlatform.windows_x64:
      return 'windows-x64';
93 94
  }
  assert(false);
pq's avatar
pq committed
95
  return null;
96 97 98
}

enum TargetPlatform {
99
  android_arm,
100
  android_x64,
101
  android_x86,
102
  ios,
103
  darwin_x64,
104
  linux_x64,
105 106
  windows_x64,
  fuchsia,
107 108
}

109
String getNameForTargetPlatform(TargetPlatform platform) {
110 111
  switch (platform) {
    case TargetPlatform.android_arm:
112
      return 'android-arm';
113
    case TargetPlatform.android_x64:
114
      return 'android-x64';
115
    case TargetPlatform.android_x86:
116
      return 'android-x86';
117
    case TargetPlatform.ios:
118
      return 'ios';
119
    case TargetPlatform.darwin_x64:
120
      return 'darwin-x64';
121
    case TargetPlatform.linux_x64:
122
      return 'linux-x64';
123 124
    case TargetPlatform.windows_x64:
      return 'windows-x64';
125 126
    case TargetPlatform.fuchsia:
      return 'fuchsia';
127 128
  }
  assert(false);
pq's avatar
pq committed
129
  return null;
130 131
}

132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
TargetPlatform getTargetPlatformForName(String platform) {
  switch (platform) {
    case 'android-arm':
      return TargetPlatform.android_arm;
    case 'android-x64':
      return TargetPlatform.android_x64;
    case 'android-x86':
      return TargetPlatform.android_x86;
    case 'ios':
      return TargetPlatform.ios;
    case 'darwin-x64':
      return TargetPlatform.darwin_x64;
    case 'linux-x64':
      return TargetPlatform.linux_x64;
  }
pq's avatar
pq committed
147
  assert(platform != null);
148 149 150
  return null;
}

151
HostPlatform getCurrentHostPlatform() {
152
  if (platform.isMacOS)
153
    return HostPlatform.darwin_x64;
154
  if (platform.isLinux)
155
    return HostPlatform.linux_x64;
156 157
  if (platform.isWindows)
    return HostPlatform.windows_x64;
158

159
  printError('Unsupported host platform, defaulting to Linux');
160 161

  return HostPlatform.linux_x64;
162
}
163 164 165

/// Returns the top-level build output directory.
String getBuildDirectory() {
166 167
  // TODO(johnmccutchan): Stop calling this function as part of setting
  // up command line argument processing.
168
  if (context == null || config == null)
169 170
    return 'build';

171
  final String buildDir = config.getValue('build-dir') ?? 'build';
172
  if (fs.path.isAbsolute(buildDir)) {
173 174 175 176 177 178 179 180 181 182 183 184 185 186
    throw new Exception(
        'build-dir config setting in ${config.configPath} must be relative');
  }
  return buildDir;
}

/// Returns the Android build output directory.
String getAndroidBuildDirectory() {
  // TODO(cbracken) move to android subdir.
  return getBuildDirectory();
}

/// Returns the AOT build output directory.
String getAotBuildDirectory() {
187
  return fs.path.join(getBuildDirectory(), 'aot');
188 189 190 191
}

/// Returns the asset build output directory.
String getAssetBuildDirectory() {
192
  return fs.path.join(getBuildDirectory(), 'flx');
193 194 195 196
}

/// Returns the iOS build output directory.
String getIosBuildDirectory() {
197
  return fs.path.join(getBuildDirectory(), 'ios');
198
}