tool_backend.dart 4.22 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
// @dart = 2.9

7
// Do not add package imports to this file.
8
import 'dart:convert'; // ignore: dart_convert_import.
9
import 'dart:io'; // ignore: dart_io_import.
10

11
/// Executes the required flutter tasks for a desktop build.
12 13
Future<void> main(List<String> arguments) async {
  final String targetPlatform = arguments[0];
14
  final String buildMode = arguments[1].toLowerCase();
15

16 17 18 19
  final String dartDefines = Platform.environment['DART_DEFINES'];
  final bool dartObfuscation = Platform.environment['DART_OBFUSCATION'] == 'true';
  final String extraFrontEndOptions = Platform.environment['EXTRA_FRONT_END_OPTIONS'];
  final String extraGenSnapshotOptions = Platform.environment['EXTRA_GEN_SNAPSHOT_OPTIONS'];
20 21
  final String flutterEngine = Platform.environment['FLUTTER_ENGINE'];
  final String flutterRoot = Platform.environment['FLUTTER_ROOT'];
22
  final String flutterTarget = Platform.environment['FLUTTER_TARGET']
23
    ?? pathJoin(<String>['lib', 'main.dart']);
24
  final String codeSizeDirectory = Platform.environment['CODE_SIZE_DIRECTORY'];
25 26 27
  final String localEngine = Platform.environment['LOCAL_ENGINE'];
  final String projectDirectory = Platform.environment['PROJECT_DIR'];
  final String splitDebugInfo = Platform.environment['SPLIT_DEBUG_INFO'];
28
  final String bundleSkSLPath = Platform.environment['BUNDLE_SKSL_PATH'];
29 30 31 32
  final bool trackWidgetCreation = Platform.environment['TRACK_WIDGET_CREATION'] == 'true';
  final bool treeShakeIcons = Platform.environment['TREE_SHAKE_ICONS'] == 'true';
  final bool verbose = Platform.environment['VERBOSE_SCRIPT_LOGGING'] == 'true';

33 34 35 36 37 38 39 40
  Directory.current = projectDirectory;

  if (localEngine != null && !localEngine.contains(buildMode)) {
    stderr.write('''
ERROR: Requested build with Flutter local engine at '$localEngine'
This engine is not compatible with FLUTTER_BUILD_MODE: '$buildMode'.
You can fix this by updating the LOCAL_ENGINE environment variable, or
by running:
41
  flutter build <platform> --local-engine=host_$buildMode
42
or
43
  flutter build <platform> --local-engine=host_${buildMode}_unopt
44 45 46 47
========================================================================
''');
    exit(1);
  }
48 49 50 51 52 53 54 55
  final String flutterExecutable = pathJoin(<String>[
    flutterRoot,
    'bin',
    if (Platform.isWindows)
      'flutter.bat'
    else
      'flutter'
  ]);
56
  final String bundlePlatform = targetPlatform == 'windows-x64' ? 'windows' : 'linux';
57
  final String target = '${buildMode}_bundle_${bundlePlatform}_assets';
58

59
  final Process assembleProcess = await Process.start(
60 61
    flutterExecutable,
    <String>[
62 63
      if (verbose)
        '--verbose',
64 65
      if (flutterEngine != null) '--local-engine-src-path=$flutterEngine',
      if (localEngine != null) '--local-engine=$localEngine',
66
      'assemble',
67
      '--output=build',
68
      '-dTargetPlatform=$targetPlatform',
69
      '-dTrackWidgetCreation=$trackWidgetCreation',
70
      '-dBuildMode=$buildMode',
71
      '-dTargetFile=$flutterTarget',
72 73
      '-dTreeShakeIcons="$treeShakeIcons"',
      '-dDartObfuscation=$dartObfuscation',
74 75
      if (bundleSkSLPath != null)
        '-iBundleSkSLPath=$bundleSkSLPath',
76 77
      if (codeSizeDirectory != null)
        '-dCodeSizeDirectory=$codeSizeDirectory',
78 79 80 81 82 83 84
      if (splitDebugInfo != null)
        '-dSplitDebugInfo=$splitDebugInfo',
      if (dartDefines != null)
        '--DartDefines=$dartDefines',
      if (extraGenSnapshotOptions != null)
        '--ExtraGenSnapshotOptions=$extraGenSnapshotOptions',
      if (extraFrontEndOptions != null)
85
        '--ExtraFrontEndOptions=$extraFrontEndOptions',
86 87 88 89 90 91 92 93 94 95 96 97 98
      target,
    ],
  );
  assembleProcess.stdout
    .transform(utf8.decoder)
    .transform(const LineSplitter())
    .listen(stdout.writeln);
  assembleProcess.stderr
    .transform(utf8.decoder)
    .transform(const LineSplitter())
    .listen(stderr.writeln);

  if (await assembleProcess.exitCode != 0) {
99 100 101
    exit(1);
  }
}
102 103 104 105 106 107 108 109

/// Perform a simple path join on the segments based on the current platform.
///
/// Does not normalize paths that have repeated separators.
String pathJoin(List<String> segments) {
  final String separator = Platform.isWindows ? r'\' : '/';
  return segments.join(separator);
}