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

5
import '../artifacts.dart';
6
import '../base/analyze_size.dart';
7
import '../base/common.dart';
8
import '../base/file_system.dart';
9
import '../base/logger.dart';
10
import '../base/project_migrator.dart';
11
import '../base/utils.dart';
12 13
import '../build_info.dart';
import '../cache.dart';
14
import '../cmake.dart';
15
import '../cmake_project.dart';
16
import '../convert.dart';
17
import '../flutter_plugins.dart';
18
import '../globals.dart' as globals;
19
import '../migrations/cmake_custom_command_migration.dart';
20
import 'install_manifest.dart';
21
import 'visual_studio.dart';
22

23 24 25
/// Update the string when non-backwards compatible changes are made to the UWP template.
const int kCurrentUwpTemplateVersion = 0;

26
/// Builds the Windows project using msbuild.
27
Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {
28 29 30
  String? target,
  VisualStudio? visualStudioOverride,
  SizeAnalyzer? sizeAnalyzer,
31
}) async {
32
  if (!windowsProject.cmakeFile.existsSync()) {
33
    throwToolExit(
34
      'No Windows desktop project configured. See '
35
      'https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
36 37 38
      'to learn about adding Windows support to a project.');
  }

39 40 41 42 43 44 45 46 47
  final List<ProjectMigrator> migrators = <ProjectMigrator>[
    CmakeCustomCommandMigration(windowsProject, globals.logger),
  ];

  final ProjectMigration migration = ProjectMigration(migrators);
  if (!migration.run()) {
    throwToolExit('Unable to migrate project files');
  }

48
  // Ensure that necessary ephemeral files are generated and up to date.
49 50
  _writeGeneratedFlutterConfig(windowsProject, buildInfo, target);
  createPluginSymlinks(windowsProject.parent);
51

52 53 54 55 56 57
  final VisualStudio visualStudio = visualStudioOverride ?? VisualStudio(
    fileSystem: globals.fs,
    platform: globals.platform,
    logger: globals.logger,
    processManager: globals.processManager,
  );
58
  final String? cmakePath = visualStudio.cmakePath;
59 60
  final String? cmakeGenerator = visualStudio.cmakeGenerator;
  if (cmakePath == null || cmakeGenerator == null) {
61 62
    throwToolExit('Unable to find suitable Visual Studio toolchain. '
        'Please run `flutter doctor` for more details.');
63 64
  }

65
  final String buildModeName = getNameForBuildMode(buildInfo.mode);
66
  final Directory buildDirectory = globals.fs.directory(getWindowsBuildDirectory());
67
  final Status status = globals.logger.startProgress(
68 69
    'Building Windows application...',
  );
70
  try {
71 72 73 74 75 76
    await _runCmakeGeneration(
      cmakePath: cmakePath,
      generator: cmakeGenerator,
      buildDir: buildDirectory,
      sourceDir: windowsProject.cmakeFile.parent,
    );
77 78 79 80
    await _runBuild(cmakePath, buildDirectory, buildModeName);
  } finally {
    status.cancel();
  }
81 82 83 84 85 86
  if (buildInfo.codeSizeDirectory != null && sizeAnalyzer != null) {
    final String arch = getNameForTargetPlatform(TargetPlatform.windows_x64);
    final File codeSizeFile = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('snapshot.$arch.json');
    final File precompilerTrace = globals.fs.directory(buildInfo.codeSizeDirectory)
      .childFile('trace.$arch.json');
87
    final Map<String, Object?> output = await sizeAnalyzer.analyzeAotSnapshot(
88 89 90 91 92 93 94 95 96
      aotSnapshot: codeSizeFile,
      // This analysis is only supported for release builds.
      outputDirectory: globals.fs.directory(
        globals.fs.path.join(getWindowsBuildDirectory(), 'runner', 'Release'),
      ),
      precompilerTrace: precompilerTrace,
      type: 'windows',
    );
    final File outputFile = globals.fsUtils.getUniqueFile(
97 98 99
      globals.fs
        .directory(globals.fsUtils.homeDirPath)
        .childDirectory('.flutter-devtools'), 'windows-code-size-analysis', 'json',
100 101 102 103 104
    )..writeAsStringSync(jsonEncode(output));
    // This message is used as a sentinel in analyze_apk_size_test.dart
    globals.printStatus(
      'A summary of your Windows bundle analysis can be found at: ${outputFile.path}',
    );
105 106 107 108 109 110 111 112

    // DevTools expects a file path relative to the .flutter-devtools/ dir.
    final String relativeAppSizePath = outputFile.path.split('.flutter-devtools/').last.trim();
    globals.printStatus(
      '\nTo analyze your app size in Dart DevTools, run the following command:\n'
      'flutter pub global activate devtools; flutter pub global run devtools '
      '--appSizeBase=$relativeAppSizePath'
    );
113
  }
114 115
}

116 117 118 119
/// Build the Windows UWP project.
///
/// Note that this feature is currently unfinished.
Future<void> buildWindowsUwp(WindowsUwpProject windowsProject, BuildInfo buildInfo, {
120 121
  String? target,
  VisualStudio? visualStudioOverride,
122
}) async {
123
  final Directory buildDirectory = globals.fs.directory(getWindowsBuildUwpDirectory());
124 125 126
  if (!windowsProject.existsSync()) {
    throwToolExit(
      'No Windows UWP desktop project configured. See '
127
      'https://docs.flutter.dev/desktop#add-desktop-support-to-an-existing-flutter-app '
128 129 130 131 132 133 134 135
      'to learn about adding Windows support to a project.',
    );
  }
  if (windowsProject.projectVersion != kCurrentUwpTemplateVersion) {
    throwToolExit(
      'The Windows UWP project template and build process has changed. In order to build '
      'you must delete the winuwp directory and re-create the project.',
    );
136 137 138 139
  }
   // Ensure that necessary ephemeral files are generated and up to date.
  _writeGeneratedFlutterConfig(windowsProject, buildInfo, target);
  createPluginSymlinks(windowsProject.parent);
140 141 142 143 144 145 146 147
  await createManifest(
    buildDirectory: buildDirectory,
    logger: globals.logger,
    platform: globals.platform,
    project: windowsProject,
    buildInfo: buildInfo,
    fileSystem: globals.fs,
  );
148 149 150 151 152 153 154

  final VisualStudio visualStudio = visualStudioOverride ?? VisualStudio(
    fileSystem: globals.fs,
    platform: globals.platform,
    logger: globals.logger,
    processManager: globals.processManager,
  );
155
  final String? cmakePath = visualStudio.cmakePath;
156 157
  final String? cmakeGenerator = visualStudio.cmakeGenerator;
  if (cmakePath == null || cmakeGenerator == null) {
158 159 160 161
    throwToolExit('Unable to find suitable Visual Studio toolchain. '
        'Please run `flutter doctor` for more details.');
  }

162
  final String buildModeName = getNameForBuildMode(buildInfo.mode);
163
  final Status status = globals.logger.startProgress(
164
    'Building Windows UWP application...',
165 166
  );
  try {
167 168 169
    // The Cmake re-entrant build does not work for UWP, so the flutter build is
    // run in advance.
    await _runFlutterBuild(buildDirectory, buildInfo, target);
170 171 172 173 174 175
    await _runCmakeGeneration(
      cmakePath: cmakePath,
      generator: cmakeGenerator,
      buildDir: buildDirectory,
      sourceDir: windowsProject.cmakeFile.parent,
    );
176
    await _runBuild(cmakePath, buildDirectory, buildModeName, install: false);
177 178
  } finally {
    status.cancel();
179
  }
180 181 182 183 184 185 186 187
}

const Map<BuildMode, String> _targets = <BuildMode, String>{
  BuildMode.debug: 'debug_bundle_windows_assets_uwp',
  BuildMode.profile: 'profile_bundle_windows_assets_uwp',
  BuildMode.release: 'release_bundle_windows_assets_uwp',
};

188
Future<void> _runFlutterBuild(Directory buildDirectory, BuildInfo buildInfo, String? targetFile) async {
189 190
  await buildDirectory.create(recursive: true);
  int result;
191 192 193 194 195
  String? flutterEngine;
  String? localEngine;
  final Artifacts artifacts = globals.artifacts!;
  if (artifacts is LocalEngineArtifacts) {
    final String engineOutPath = artifacts.engineOutPath;
196 197 198 199
    flutterEngine = globals.fs.path.dirname(globals.fs.path.dirname(engineOutPath));
    localEngine = globals.fs.path.basename(engineOutPath);
  }
  try {
200
    final String? buildMode = _targets[buildInfo.mode];
201 202
    result = await globals.processUtils.stream(
      <String>[
203
        globals.fs.path.join(Cache.flutterRoot!, 'bin', 'flutter'),
204 205 206 207 208 209 210 211 212 213 214 215 216 217
        if (globals.logger.isVerbose)
          '--verbose',
        if (flutterEngine != null) '--local-engine-src-path=$flutterEngine',
        if (localEngine != null) '--local-engine=$localEngine',
        'assemble',
        '--no-version-check',
        '--output=build',
        '-dTargetPlatform=windows-uwp-x64',
        '-dTrackWidgetCreation=${buildInfo.trackWidgetCreation}',
        '-dBuildMode=${getNameForBuildMode(buildInfo.mode)}',
        '-dTargetFile=$targetFile',
        '-dTreeShakeIcons="${buildInfo.treeShakeIcons}"',
        '-dDartObfuscation=${buildInfo.dartObfuscation}',
        if (buildInfo.bundleSkSLPath != null)
218
          '-dBundleSkSLPath=${buildInfo.bundleSkSLPath}',
219 220 221 222 223 224 225 226 227 228
        if (buildInfo.codeSizeDirectory != null)
          '-dCodeSizeDirectory=${buildInfo.codeSizeDirectory}',
        if (buildInfo.splitDebugInfoPath != null)
          '-dSplitDebugInfo=${buildInfo.splitDebugInfoPath}',
        if (buildInfo.dartDefines != null && buildInfo.dartDefines.isNotEmpty)
          '--DartDefines=${encodeDartDefines(buildInfo.dartDefines)}',
        if (buildInfo.extraGenSnapshotOptions != null && buildInfo.extraGenSnapshotOptions.isNotEmpty)
          '--ExtraGenSnapshotOptions=${buildInfo.extraGenSnapshotOptions}',
        if (buildInfo.extraFrontEndOptions != null && buildInfo.extraFrontEndOptions.isNotEmpty)
          '--ExtraFrontEndOptions=${buildInfo.extraFrontEndOptions}',
229 230
        if (buildMode != null)
          buildMode,
231 232 233 234 235 236 237 238 239
      ],
      trace: true,
    );
  } on ArgumentError {
    throwToolExit("cmake not found. Run 'flutter doctor' for more information.");
  }
  if (result != 0) {
    throwToolExit('Unable to generate build files');
  }
240 241
}

242 243 244 245 246 247
Future<void> _runCmakeGeneration({
  required String cmakePath,
  required String generator,
  required Directory buildDir,
  required Directory sourceDir,
}) async {
248 249 250 251 252
  final Stopwatch sw = Stopwatch()..start();

  await buildDir.create(recursive: true);
  int result;
  try {
253
    result = await globals.processUtils.stream(
254 255 256 257 258 259 260
      <String>[
        cmakePath,
        '-S',
        sourceDir.path,
        '-B',
        buildDir.path,
        '-G',
261
        generator,
262 263 264
      ],
      trace: true,
    );
265
  } on ArgumentError {
266 267 268 269 270 271 272 273
    throwToolExit("cmake not found. Run 'flutter doctor' for more information.");
  }
  if (result != 0) {
    throwToolExit('Unable to generate build files');
  }
  globals.flutterUsage.sendTiming('build', 'windows-cmake-generation', Duration(milliseconds: sw.elapsedMilliseconds));
}

274 275 276 277 278 279
Future<void> _runBuild(
  String cmakePath,
  Directory buildDir,
  String buildModeName,
  { bool install = true }
) async {
280 281
  final Stopwatch sw = Stopwatch()..start();

282 283 284 285
  // MSBuild sends all output to stdout, including build errors. This surfaces
  // known error patterns.
  final RegExp errorMatcher = RegExp(r':\s*(?:warning|(?:fatal )?error).*?:');

286 287
  int result;
  try {
288
    result = await globals.processUtils.stream(
289
      <String>[
290 291 292 293
        cmakePath,
        '--build',
        buildDir.path,
        '--config',
294
        sentenceCase(buildModeName),
295 296
        if (install)
          ...<String>['--target', 'INSTALL'],
297 298
        if (globals.logger.isVerbose)
          '--verbose'
299 300 301 302 303 304
      ],
      environment: <String, String>{
        if (globals.logger.isVerbose)
          'VERBOSE_SCRIPT_LOGGING': 'true'
      },
      trace: true,
305
      stdoutErrorMatcher: errorMatcher,
306
    );
307
  } on ArgumentError {
308
    throwToolExit("cmake not found. Run 'flutter doctor' for more information.");
309 310
  }
  if (result != 0) {
311
    throwToolExit('Build process failed.');
312
  }
313
  globals.flutterUsage.sendTiming('build', 'windows-cmake-build', Duration(milliseconds: sw.elapsedMilliseconds));
314
}
315

316 317
/// Writes the generated CMake file with the configuration for the given build.
void _writeGeneratedFlutterConfig(
318 319
  WindowsProject windowsProject,
  BuildInfo buildInfo,
320
  String? target,
321
) {
322
  final Map<String, String> environment = <String, String>{
323
    'FLUTTER_ROOT': Cache.flutterRoot!,
324
    'FLUTTER_EPHEMERAL_DIR': windowsProject.ephemeralDirectory.path,
325
    'PROJECT_DIR': windowsProject.parent.directory.path,
326 327
    if (target != null)
      'FLUTTER_TARGET': target,
328
    ...buildInfo.toEnvironmentConfig(),
329
  };
330 331 332
  final Artifacts artifacts = globals.artifacts!;
  if (artifacts is LocalEngineArtifacts) {
    final String engineOutPath = artifacts.engineOutPath;
333 334 335
    environment['FLUTTER_ENGINE'] = globals.fs.path.dirname(globals.fs.path.dirname(engineOutPath));
    environment['LOCAL_ENGINE'] = globals.fs.path.basename(engineOutPath);
  }
336
  writeGeneratedCmakeConfig(Cache.flutterRoot!, windowsProject, environment);
337
}