build_windows.dart 3.39 KB
Newer Older
1 2 3 4
// Copyright 2019 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 '../artifacts.dart';
6
import '../base/common.dart';
7
import '../base/file_system.dart';
8 9 10 11 12 13 14 15
import '../base/io.dart';
import '../base/logger.dart';
import '../base/process_manager.dart';
import '../build_info.dart';
import '../cache.dart';
import '../convert.dart';
import '../globals.dart';
import '../project.dart';
16
import '../reporting/reporting.dart';
17

18
import 'msbuild_utils.dart';
19
import 'visual_studio.dart';
20

21
/// Builds the Windows project using msbuild.
22
Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {String target}) async {
23 24
  final Map<String, String> environment = <String, String>{
    'FLUTTER_ROOT': Cache.flutterRoot,
25 26
    'PROJECT_DIR': windowsProject.project.directory.path,
    'TRACK_WIDGET_CREATION': (buildInfo?.trackWidgetCreation == true).toString(),
27
  };
28 29 30
  if (target != null) {
    environment['FLUTTER_TARGET'] = target;
  }
31 32 33 34 35 36
  if (artifacts is LocalEngineArtifacts) {
    final LocalEngineArtifacts localEngineArtifacts = artifacts;
    final String engineOutPath = localEngineArtifacts.engineOutPath;
    environment['FLUTTER_ENGINE'] = fs.path.dirname(fs.path.dirname(engineOutPath));
    environment['LOCAL_ENGINE'] = fs.path.basename(engineOutPath);
  }
37 38
  writePropertySheet(windowsProject.generatedPropertySheetFile, environment);

39
  final String vcvarsScript = visualStudio.vcvarsPath;
40
  if (vcvarsScript == null) {
41 42
    throwToolExit('Unable to find suitable Visual Studio toolchain. '
        'Please run `flutter doctor` for more details.');
43 44
  }

45 46 47 48 49 50 51 52 53
  if (!buildInfo.isDebug) {
    const String warning = '🚧 ';
    printStatus(warning * 20);
    printStatus('Warning: Only debug is currently implemented for Windows. This is effectively a debug build.');
    printStatus('See https://github.com/flutter/flutter/issues/38477 for details and updates.');
    printStatus(warning * 20);
    printStatus('');
  }

54 55 56 57 58 59 60 61
  final String buildScript = fs.path.join(
    Cache.flutterRoot,
    'packages',
    'flutter_tools',
    'bin',
    'vs_build.bat',
  );

62
  final String configuration = buildInfo.isDebug ? 'Debug' : 'Release';
63
  final String solutionPath = windowsProject.solutionFile.path;
64
  final Stopwatch sw = Stopwatch()..start();
65 66 67
  // Run the script with a relative path to the project using the enclosing
  // directory as the workingDirectory, to avoid hitting the limit on command
  // lengths in batch scripts if the absolute path to the project is long.
68
  final Process process = await processManager.start(<String>[
69 70
    buildScript,
    vcvarsScript,
71
    fs.path.basename(solutionPath),
72
    configuration,
73
  ], workingDirectory: fs.path.dirname(solutionPath));
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  final Status status = logger.startProgress(
    'Building Windows application...',
    timeout: null,
  );
  int result;
  try {
    process.stderr
      .transform(utf8.decoder)
      .transform(const LineSplitter())
      .listen(printError);
    process.stdout
      .transform(utf8.decoder)
      .transform(const LineSplitter())
      .listen(printTrace);
    result = await process.exitCode;
  } finally {
    status.cancel();
  }
  if (result != 0) {
93
    throwToolExit('Build process failed. To view the stack trace, please run `flutter run -d windows -v`.');
94
  }
95
  flutterUsage.sendTiming('build', 'vs_build', Duration(milliseconds: sw.elapsedMilliseconds));
96
}