assemble.dart 11.1 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'package:meta/meta.dart';

7
import '../base/common.dart';
8
import '../base/file_system.dart';
9
import '../build_info.dart';
10
import '../build_system/build_system.dart';
11 12
import '../build_system/depfile.dart';
import '../build_system/targets/android.dart';
13
import '../build_system/targets/assets.dart';
14
import '../build_system/targets/common.dart';
15 16 17
import '../build_system/targets/ios.dart';
import '../build_system/targets/linux.dart';
import '../build_system/targets/macos.dart';
18
import '../build_system/targets/web.dart';
19
import '../build_system/targets/windows.dart';
20
import '../cache.dart';
21
import '../convert.dart';
22
import '../globals.dart' as globals;
23
import '../project.dart';
24
import '../reporting/reporting.dart';
25 26
import '../runner/flutter_command.dart';

27 28
/// All currently implemented targets.
const List<Target> _kDefaultTargets = <Target>[
29
  // Shared targets
30 31
  CopyAssets(),
  KernelSnapshot(),
32 33
  AotElfProfile(TargetPlatform.android_arm),
  AotElfRelease(TargetPlatform.android_arm),
34 35
  AotAssemblyProfile(),
  AotAssemblyRelease(),
36
  // macOS targets
37
  DebugMacOSFramework(),
38 39 40
  DebugMacOSBundleFlutterAssets(),
  ProfileMacOSBundleFlutterAssets(),
  ReleaseMacOSBundleFlutterAssets(),
41
  // Linux targets
42
  DebugBundleLinuxAssets(),
43 44
  ProfileBundleLinuxAssets(),
  ReleaseBundleLinuxAssets(),
45
  // Web targets
46
  WebServiceWorker(),
47
  ReleaseAndroidApplication(),
48
  // This is a one-off rule for bundle and aot compat.
49
  CopyFlutterBundle(),
50 51 52 53
  // Android targets,
  DebugAndroidApplication(),
  FastStartAndroidApplication(),
  ProfileAndroidApplication(),
54 55 56 57 58 59 60
  // Android ABI specific AOT rules.
  androidArmProfileBundle,
  androidArm64ProfileBundle,
  androidx64ProfileBundle,
  androidArmReleaseBundle,
  androidArm64ReleaseBundle,
  androidx64ReleaseBundle,
61
  // iOS targets
62 63 64
  DebugIosApplicationBundle(),
  ProfileIosApplicationBundle(),
  ReleaseIosApplicationBundle(),
65 66 67
  // Windows targets
  UnpackWindows(),
  DebugBundleWindowsAssets(),
68 69
  ProfileBundleWindowsAssets(),
  ReleaseBundleWindowsAssets(),
70 71
];

72 73 74 75 76 77 78
/// Assemble provides a low level API to interact with the flutter tool build
/// system.
class AssembleCommand extends FlutterCommand {
  AssembleCommand() {
    argParser.addMultiOption(
      'define',
      abbr: 'd',
79
      help: 'Allows passing configuration to a target with --define=target=key=value.',
80
    );
81 82 83 84
    argParser.addOption(
      'performance-measurement-file',
      help: 'Output individual target performance to a JSON file.'
    );
85 86 87 88 89 90 91
    argParser.addMultiOption(
      'input',
      abbr: 'i',
      help: 'Allows passing additional inputs with --input=key=value. Unlike '
      'defines, additional inputs do not generate a new configuration, instead '
      'they are treated as dependencies of the targets that use them.'
    );
92 93 94
    argParser.addOption('depfile', help: 'A file path where a depfile will be written. '
      'This contains all build inputs and outputs in a make style syntax'
    );
95 96 97 98 99 100 101 102
    argParser.addOption('build-inputs', help: 'A file path where a newline '
        'separated file containing all inputs used will be written after a build.'
        ' This file is not included as a build input or output. This file is not'
        ' written if the build fails for any reason.');
    argParser.addOption('build-outputs', help: 'A file path where a newline '
        'separated file containing all outputs used will be written after a build.'
        ' This file is not included as a build input or output. This file is not'
        ' written if the build fails for any reason.');
103 104 105 106
    argParser.addOption('output', abbr: 'o', help: 'A directory where output '
        'files will be written. Must be either absolute or relative from the '
        'root of the current Flutter project.',
    );
107
    argParser.addOption(kExtraGenSnapshotOptions);
108
    argParser.addOption(kExtraFrontEndOptions);
109
    argParser.addOption(kDartDefines);
110 111
    argParser.addOption(
      'resource-pool-size',
112
      help: 'The maximum number of concurrent tasks the build system will run.',
113 114 115
    );
  }

116 117
  @override
  String get description => 'Assemble and build flutter resources.';
118

119 120
  @override
  String get name => 'assemble';
121

122 123 124 125 126 127
  @override
  Future<Map<CustomDimensions, String>> get usageValues async {
    final FlutterProject futterProject = FlutterProject.current();
    if (futterProject == null) {
      return const <CustomDimensions, String>{};
    }
128
    try {
129
      final Environment localEnvironment = createEnvironment();
130 131 132 133
      return <CustomDimensions, String>{
        CustomDimensions.commandBuildBundleTargetPlatform: localEnvironment.defines['TargetPlatform'],
        CustomDimensions.commandBuildBundleIsModule: '${futterProject.isModule}',
      };
134
    } on Exception {
135 136 137
      // We've failed to send usage.
    }
    return const <CustomDimensions, String>{};
138 139
  }

140
  /// The target(s) we are building.
141
  List<Target> createTargets() {
142 143 144
    if (argResults.rest.isEmpty) {
      throwToolExit('missing target name for flutter assemble.');
    }
145
    final String name = argResults.rest.first;
146
    final Map<String, Target> targetMap = <String, Target>{
147
      for (final Target target in _kDefaultTargets)
148 149 150
        target.name: target
    };
    final List<Target> results = <Target>[
151
      for (final String targetName in argResults.rest)
152 153 154 155
        if (targetMap.containsKey(targetName))
          targetMap[targetName]
    ];
    if (results.isEmpty) {
156
      throwToolExit('No target named "$name" defined.');
157
    }
158
    return results;
159 160 161
  }

  /// The environmental configuration for a build invocation.
162
  Environment createEnvironment() {
163
    final FlutterProject flutterProject = FlutterProject.current();
164
    String output = stringArg('output');
165 166 167 168
    if (output == null) {
      throwToolExit('--output directory is required for assemble.');
    }
    // If path is relative, make it absolute from flutter project.
169 170
    if (globals.fs.path.isRelative(output)) {
      output = globals.fs.path.join(flutterProject.directory.path, output);
171
    }
172
    final Environment result = Environment(
173
      outputDir: globals.fs.directory(output),
174 175 176
      buildDir: flutterProject.directory
          .childDirectory('.dart_tool')
          .childDirectory('flutter_build'),
177
      projectDir: flutterProject.directory,
178
      defines: _parseDefines(stringsArg('define')),
179
      inputs: _parseDefines(stringsArg('input')),
180 181
      cacheDir: globals.cache.getRoot(),
      flutterRootDir: globals.fs.directory(Cache.flutterRoot),
182 183 184 185
      artifacts: globals.artifacts,
      fileSystem: globals.fs,
      logger: globals.logger,
      processManager: globals.processManager,
186 187 188
      engineVersion: globals.artifacts.isLocalEngine
        ? null
        : globals.flutterVersion.engineRevision
189 190 191 192
    );
    return result;
  }

193
  Map<String, String> _parseDefines(List<String> values) {
194
    final Map<String, String> results = <String, String>{};
195
    for (final String chunk in values) {
196 197
      final int indexEquals = chunk.indexOf('=');
      if (indexEquals == -1) {
198 199
        throwToolExit('Improperly formatted define flag: $chunk');
      }
200 201
      final String key = chunk.substring(0, indexEquals);
      final String value = chunk.substring(indexEquals + 1);
202 203
      results[key] = value;
    }
204 205 206 207
    // Workaround for extraGenSnapshot formatting.
    if (argResults.wasParsed(kExtraGenSnapshotOptions)) {
      results[kExtraGenSnapshotOptions] = argResults[kExtraGenSnapshotOptions] as String;
    }
208 209 210
    if (argResults.wasParsed(kDartDefines)) {
      results[kDartDefines] = argResults[kDartDefines] as String;
    }
211 212 213
    if (argResults.wasParsed(kExtraFrontEndOptions)) {
      results[kExtraFrontEndOptions] = argResults[kExtraFrontEndOptions] as String;
    }
214 215 216 217 218
    return results;
  }

  @override
  Future<FlutterCommandResult> runCommand() async {
219
    final List<Target> targets = createTargets();
220
    final Target target = targets.length == 1 ? targets.single : _CompositeTarget(targets);
221 222 223 224 225 226 227 228 229
    final BuildResult result = await globals.buildSystem.build(
      target,
      createEnvironment(),
      buildSystemConfig: BuildSystemConfig(
        resourcePoolSize: argResults.wasParsed('resource-pool-size')
          ? int.tryParse(stringArg('resource-pool-size'))
          : null,
        ),
      );
230
    if (!result.success) {
231
      for (final ExceptionMeasurement measurement in result.exceptions.values) {
232 233 234 235 236
        if (measurement.fatal || globals.logger.isVerbose) {
          globals.printError('Target ${measurement.target} failed: ${measurement.exception}',
            stackTrace: measurement.stackTrace
          );
        }
237
      }
238
      throwToolExit('');
239
    }
240
    globals.printTrace('build succeeded.');
241
    if (argResults.wasParsed('build-inputs')) {
242
      writeListIfChanged(result.inputFiles, stringArg('build-inputs'));
243 244
    }
    if (argResults.wasParsed('build-outputs')) {
245
      writeListIfChanged(result.outputFiles, stringArg('build-outputs'));
246
    }
247 248 249 250
    if (argResults.wasParsed('performance-measurement-file')) {
      final File outFile = globals.fs.file(argResults['performance-measurement-file']);
      writePerformanceData(result.performance.values, outFile);
    }
251
    if (argResults.wasParsed('depfile')) {
252
      final File depfileFile = globals.fs.file(stringArg('depfile'));
253
      final Depfile depfile = Depfile(result.inputFiles, result.outputFiles);
254 255 256 257 258
      final DepfileService depfileService = DepfileService(
        fileSystem: globals.fs,
        logger: globals.logger,
      );
      depfileService.writeToFile(depfile, globals.fs.file(depfileFile));
259
    }
260
    return FlutterCommandResult.success();
261 262
  }
}
263 264 265

@visibleForTesting
void writeListIfChanged(List<File> files, String path) {
266
  final File file = globals.fs.file(path);
267 268
  final StringBuffer buffer = StringBuffer();
  // These files are already sorted.
269
  for (final File file in files) {
270
    buffer.writeln(file.path);
271 272 273 274 275 276 277 278 279 280
  }
  final String newContents = buffer.toString();
  if (!file.existsSync()) {
    file.writeAsStringSync(newContents);
  }
  final String currentContents = file.readAsStringSync();
  if (currentContents != newContents) {
    file.writeAsStringSync(newContents);
  }
}
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
/// Output performance measurement data in [outFile].
@visibleForTesting
void writePerformanceData(Iterable<PerformanceMeasurement> measurements, File outFile) {
  final Map<String, Object> jsonData = <String, Object>{
    'targets': <Object>[
      for (final PerformanceMeasurement measurement in measurements)
        <String, Object>{
          'name': measurement.analyicsName,
          'skipped': measurement.skipped,
          'succeeded': measurement.succeeded,
          'elapsedMilliseconds': measurement.elapsedMilliseconds,
        }
    ]
  };
  if (!outFile.parent.existsSync()) {
    outFile.parent.createSync(recursive: true);
  }
  outFile.writeAsStringSync(json.encode(jsonData));
}

302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
class _CompositeTarget extends Target {
  _CompositeTarget(this.dependencies);

  @override
  final List<Target> dependencies;

  @override
  String get name => '_composite';

  @override
  Future<void> build(Environment environment) async { }

  @override
  List<Source> get inputs => <Source>[];

  @override
  List<Source> get outputs => <Source>[];
}