1// Copyright 2019 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file 4 5import '../artifacts.dart'; 6import '../base/common.dart'; 7import '../base/file_system.dart'; 8import '../base/io.dart'; 9import '../base/logger.dart'; 10import '../base/process_manager.dart'; 11import '../build_info.dart'; 12import '../cache.dart'; 13import '../convert.dart'; 14import '../globals.dart'; 15import '../project.dart'; 16import '../reporting/reporting.dart'; 17 18import 'msbuild_utils.dart'; 19import 'visual_studio.dart'; 20 21/// Builds the Windows project using msbuild. 22Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {String target}) async { 23 final Map<String, String> environment = <String, String>{ 24 'FLUTTER_ROOT': Cache.flutterRoot, 25 'PROJECT_DIR': windowsProject.project.directory.path, 26 'TRACK_WIDGET_CREATION': (buildInfo?.trackWidgetCreation == true).toString(), 27 }; 28 if (target != null) { 29 environment['FLUTTER_TARGET'] = target; 30 } 31 if (artifacts is LocalEngineArtifacts) { 32 final LocalEngineArtifacts localEngineArtifacts = artifacts; 33 final String engineOutPath = localEngineArtifacts.engineOutPath; 34 environment['FLUTTER_ENGINE'] = fs.path.dirname(fs.path.dirname(engineOutPath)); 35 environment['LOCAL_ENGINE'] = fs.path.basename(engineOutPath); 36 } 37 writePropertySheet(windowsProject.generatedPropertySheetFile, environment); 38 39 final String vcvarsScript = visualStudio.vcvarsPath; 40 if (vcvarsScript == null) { 41 throwToolExit('Unable to find suitable Visual Studio toolchain. ' 42 'Please run `flutter doctor` for more details.'); 43 } 44 45 final String buildScript = fs.path.join( 46 Cache.flutterRoot, 47 'packages', 48 'flutter_tools', 49 'bin', 50 'vs_build.bat', 51 ); 52 53 final String configuration = buildInfo.isDebug ? 'Debug' : 'Release'; 54 final String solutionPath = windowsProject.solutionFile.path; 55 final Stopwatch sw = Stopwatch()..start(); 56 // Run the script with a relative path to the project using the enclosing 57 // directory as the workingDirectory, to avoid hitting the limit on command 58 // lengths in batch scripts if the absolute path to the project is long. 59 final Process process = await processManager.start(<String>[ 60 buildScript, 61 vcvarsScript, 62 fs.path.basename(solutionPath), 63 configuration, 64 ], workingDirectory: fs.path.dirname(solutionPath)); 65 final Status status = logger.startProgress( 66 'Building Windows application...', 67 timeout: null, 68 ); 69 int result; 70 try { 71 process.stderr 72 .transform(utf8.decoder) 73 .transform(const LineSplitter()) 74 .listen(printError); 75 process.stdout 76 .transform(utf8.decoder) 77 .transform(const LineSplitter()) 78 .listen(printTrace); 79 result = await process.exitCode; 80 } finally { 81 status.cancel(); 82 } 83 if (result != 0) { 84 throwToolExit('Build process failed. To view the stack trace, please run `flutter run -d windows -v`.'); 85 } 86 flutterUsage.sendTiming('build', 'vs_build', Duration(milliseconds: sw.elapsedMilliseconds)); 87} 88