tool_backend.dart 5.8 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
// Do not add package imports to this file.
6 7
import 'dart:convert'; // flutter_ignore: dart_convert_import.
import 'dart:io'; // flutter_ignore: dart_io_import.
8

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

14
  final String? dartDefines = Platform.environment['DART_DEFINES'];
15
  final bool dartObfuscation = Platform.environment['DART_OBFUSCATION'] == 'true';
16
  final String? frontendServerStarterPath = Platform.environment['FRONTEND_SERVER_STARTER_PATH'];
17 18 19 20
  final String? extraFrontEndOptions = Platform.environment['EXTRA_FRONT_END_OPTIONS'];
  final String? extraGenSnapshotOptions = Platform.environment['EXTRA_GEN_SNAPSHOT_OPTIONS'];
  final String? flutterEngine = Platform.environment['FLUTTER_ENGINE'];
  final String? flutterRoot = Platform.environment['FLUTTER_ROOT'];
21
  final String flutterTarget = Platform.environment['FLUTTER_TARGET']
22
    ?? pathJoin(<String>['lib', 'main.dart']);
23 24
  final String? codeSizeDirectory = Platform.environment['CODE_SIZE_DIRECTORY'];
  final String? localEngine = Platform.environment['LOCAL_ENGINE'];
25
  final String? localEngineHost = Platform.environment['LOCAL_ENGINE_HOST'];
26 27 28
  final String? projectDirectory = Platform.environment['PROJECT_DIR'];
  final String? splitDebugInfo = Platform.environment['SPLIT_DEBUG_INFO'];
  final String? bundleSkSLPath = Platform.environment['BUNDLE_SKSL_PATH'];
29 30 31
  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';
32
  final bool prefixedErrors = Platform.environment['PREFIXED_ERROR_LOGGING'] == 'true';
33

34 35 36 37 38 39 40 41 42
  if (projectDirectory == null) {
    stderr.write('PROJECT_DIR environment variable must be set to the location of Flutter project to be built.');
    exit(1);
  }
  if (flutterRoot == null || flutterRoot.isEmpty) {
    stderr.write('FLUTTER_ROOT environment variable must be set to the location of the Flutter SDK.');
    exit(1);
  }

43 44 45 46 47 48 49 50
  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:
51
  flutter build <platform> --local-engine=<platform>_$buildMode --local-engine-host=host_$buildMode
52
or
53 54 55 56 57 58 59 60 61 62 63 64 65 66
  flutter build <platform> --local-engine=<platform>_${buildMode}_unopt --local-engine-host=host_${buildMode}_unopt
========================================================================
''');
    exit(1);
  }
  if (localEngineHost != null && !localEngineHost.contains(buildMode)) {
    stderr.write('''
ERROR: Requested build with Flutter local engine host at '$localEngineHost'
This engine is not compatible with FLUTTER_BUILD_MODE: '$buildMode'.
You can fix this by updating the LOCAL_ENGINE_HOST environment variable, or
by running:
  flutter build <platform> --local-engine=<platform>_$buildMode --local-engine-host=host_$buildMode
or
  flutter build <platform> --local-engine=<platform>_$buildMode --local-engine-host=host_${buildMode}_unopt
67 68 69 70
========================================================================
''');
    exit(1);
  }
71 72 73 74 75 76
  final String flutterExecutable = pathJoin(<String>[
    flutterRoot,
    'bin',
    if (Platform.isWindows)
      'flutter.bat'
    else
77
      'flutter',
78
  ]);
79
  final String bundlePlatform = targetPlatform.startsWith('windows') ? 'windows' : targetPlatform;
80
  final String target = '${buildMode}_bundle_${bundlePlatform}_assets';
81
  final Process assembleProcess = await Process.start(
82 83
    flutterExecutable,
    <String>[
84 85
      if (verbose)
        '--verbose',
86 87
      if (prefixedErrors)
        '--prefixed-errors',
88 89
      if (flutterEngine != null) '--local-engine-src-path=$flutterEngine',
      if (localEngine != null) '--local-engine=$localEngine',
90
      if (localEngineHost != null) '--local-engine-host=$localEngineHost',
91
      'assemble',
92
      '--no-version-check',
93
      '--output=build',
94
      '-dTargetPlatform=$targetPlatform',
95
      '-dTrackWidgetCreation=$trackWidgetCreation',
96
      '-dBuildMode=$buildMode',
97
      '-dTargetFile=$flutterTarget',
98 99
      '-dTreeShakeIcons="$treeShakeIcons"',
      '-dDartObfuscation=$dartObfuscation',
100
      if (bundleSkSLPath != null)
101
        '-dBundleSkSLPath=$bundleSkSLPath',
102 103
      if (codeSizeDirectory != null)
        '-dCodeSizeDirectory=$codeSizeDirectory',
104 105 106 107 108 109
      if (splitDebugInfo != null)
        '-dSplitDebugInfo=$splitDebugInfo',
      if (dartDefines != null)
        '--DartDefines=$dartDefines',
      if (extraGenSnapshotOptions != null)
        '--ExtraGenSnapshotOptions=$extraGenSnapshotOptions',
110 111
      if (frontendServerStarterPath != null)
        '-dFrontendServerStarterPath=$frontendServerStarterPath',
112
      if (extraFrontEndOptions != null)
113
        '--ExtraFrontEndOptions=$extraFrontEndOptions',
114 115 116 117 118 119 120 121 122 123 124 125 126
      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) {
127 128 129
    exit(1);
  }
}
130 131 132 133 134 135 136 137

/// 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);
}