build_windows.dart 7.17 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 '../convert.dart';
16
import '../globals.dart' as globals;
17
import '../migrations/cmake_custom_command_migration.dart';
18
import '../plugins.dart';
19
import '../project.dart';
20
import 'visual_studio.dart';
21

22 23 24 25 26
// From https://cmake.org/cmake/help/v3.15/manual/cmake-generators.7.html#visual-studio-generators
// This may need to become a getter on VisualStudio in the future to support
// future major versions of Visual Studio.
const String _cmakeVisualStudioGeneratorIdentifier = 'Visual Studio 16 2019';

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

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

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

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

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

65 66
  final String buildModeName = getNameForBuildMode(buildInfo.mode ?? BuildMode.release);
  final Directory buildDirectory = globals.fs.directory(getWindowsBuildDirectory());
67
  final Status status = globals.logger.startProgress(
68 69
    'Building Windows application...',
  );
70 71 72 73 74 75
  try {
    await _runCmakeGeneration(cmakePath, buildDirectory, windowsProject.cmakeFile.parent);
    await _runBuild(cmakePath, buildDirectory, buildModeName);
  } finally {
    status.cancel();
  }
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  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');
    final Map<String, Object> output = await sizeAnalyzer.analyzeAotSnapshot(
      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(
92 93 94
      globals.fs
        .directory(globals.fsUtils.homeDirPath)
        .childDirectory('.flutter-devtools'), 'windows-code-size-analysis', 'json',
95 96 97 98 99
    )..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}',
    );
100 101 102 103 104 105 106 107

    // 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'
    );
108
  }
109 110 111 112 113 114 115 116
}

Future<void> _runCmakeGeneration(String cmakePath, Directory buildDir, Directory sourceDir) async {
  final Stopwatch sw = Stopwatch()..start();

  await buildDir.create(recursive: true);
  int result;
  try {
117
    result = await globals.processUtils.stream(
118 119 120 121 122 123 124 125 126 127 128
      <String>[
        cmakePath,
        '-S',
        sourceDir.path,
        '-B',
        buildDir.path,
        '-G',
        _cmakeVisualStudioGeneratorIdentifier,
      ],
      trace: true,
    );
129
  } on ArgumentError {
130 131 132 133 134 135 136 137 138 139 140
    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));
}

Future<void> _runBuild(String cmakePath, Directory buildDir, String buildModeName) async {
  final Stopwatch sw = Stopwatch()..start();

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

145 146
  int result;
  try {
147
    result = await globals.processUtils.stream(
148
      <String>[
149 150 151 152 153 154 155 156 157
        cmakePath,
        '--build',
        buildDir.path,
        '--config',
        toTitleCase(buildModeName),
        '--target',
        'INSTALL',
        if (globals.logger.isVerbose)
          '--verbose'
158 159 160 161 162 163
      ],
      environment: <String, String>{
        if (globals.logger.isVerbose)
          'VERBOSE_SCRIPT_LOGGING': 'true'
      },
      trace: true,
164
      stdoutErrorMatcher: errorMatcher,
165
    );
166
  } on ArgumentError {
167
    throwToolExit("cmake not found. Run 'flutter doctor' for more information.");
168 169
  }
  if (result != 0) {
170
    throwToolExit('Build process failed.');
171
  }
172
  globals.flutterUsage.sendTiming('build', 'windows-cmake-build', Duration(milliseconds: sw.elapsedMilliseconds));
173
}
174

175 176
/// Writes the generated CMake file with the configuration for the given build.
void _writeGeneratedFlutterConfig(
177 178 179 180
  WindowsProject windowsProject,
  BuildInfo buildInfo,
  String target,
) {
181 182 183
  final Map<String, String> environment = <String, String>{
    'FLUTTER_ROOT': Cache.flutterRoot,
    'FLUTTER_EPHEMERAL_DIR': windowsProject.ephemeralDirectory.path,
184
    'PROJECT_DIR': windowsProject.parent.directory.path,
185 186
    if (target != null)
      'FLUTTER_TARGET': target,
187
    ...buildInfo.toEnvironmentConfig(),
188 189 190 191 192 193 194
  };
  if (globals.artifacts is LocalEngineArtifacts) {
    final LocalEngineArtifacts localEngineArtifacts = globals.artifacts as LocalEngineArtifacts;
    final String engineOutPath = localEngineArtifacts.engineOutPath;
    environment['FLUTTER_ENGINE'] = globals.fs.path.dirname(globals.fs.path.dirname(engineOutPath));
    environment['LOCAL_ENGINE'] = globals.fs.path.basename(engineOutPath);
  }
195
  writeGeneratedCmakeConfig(Cache.flutterRoot, windowsProject, environment);
196
}