Unverified Commit 7e05d103 authored by stuartmorgan's avatar stuartmorgan Committed by GitHub

Add support for Visual Studio 2022 (#93426)

parent 9cc47c2b
......@@ -20,11 +20,6 @@ import '../migrations/cmake_custom_command_migration.dart';
import 'install_manifest.dart';
import 'visual_studio.dart';
// From https://cmake.org/cmake/help/v3.14/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';
/// Update the string when non-backwards compatible changes are made to the UWP template.
const int kCurrentUwpTemplateVersion = 0;
......@@ -61,7 +56,8 @@ Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {
processManager: globals.processManager,
);
final String? cmakePath = visualStudio.cmakePath;
if (cmakePath == null) {
final String? cmakeGenerator = visualStudio.cmakeGenerator;
if (cmakePath == null || cmakeGenerator == null) {
throwToolExit('Unable to find suitable Visual Studio toolchain. '
'Please run `flutter doctor` for more details.');
}
......@@ -72,7 +68,12 @@ Future<void> buildWindows(WindowsProject windowsProject, BuildInfo buildInfo, {
'Building Windows application...',
);
try {
await _runCmakeGeneration(cmakePath, buildDirectory, windowsProject.cmakeFile.parent);
await _runCmakeGeneration(
cmakePath: cmakePath,
generator: cmakeGenerator,
buildDir: buildDirectory,
sourceDir: windowsProject.cmakeFile.parent,
);
await _runBuild(cmakePath, buildDirectory, buildModeName);
} finally {
status.cancel();
......@@ -152,7 +153,8 @@ Future<void> buildWindowsUwp(WindowsUwpProject windowsProject, BuildInfo buildIn
processManager: globals.processManager,
);
final String? cmakePath = visualStudio.cmakePath;
if (cmakePath == null) {
final String? cmakeGenerator = visualStudio.cmakeGenerator;
if (cmakePath == null || cmakeGenerator == null) {
throwToolExit('Unable to find suitable Visual Studio toolchain. '
'Please run `flutter doctor` for more details.');
}
......@@ -165,7 +167,12 @@ Future<void> buildWindowsUwp(WindowsUwpProject windowsProject, BuildInfo buildIn
// The Cmake re-entrant build does not work for UWP, so the flutter build is
// run in advance.
await _runFlutterBuild(buildDirectory, buildInfo, target);
await _runCmakeGeneration(cmakePath, buildDirectory, windowsProject.cmakeFile.parent);
await _runCmakeGeneration(
cmakePath: cmakePath,
generator: cmakeGenerator,
buildDir: buildDirectory,
sourceDir: windowsProject.cmakeFile.parent,
);
await _runBuild(cmakePath, buildDirectory, buildModeName, install: false);
} finally {
status.cancel();
......@@ -232,7 +239,12 @@ Future<void> _runFlutterBuild(Directory buildDirectory, BuildInfo buildInfo, Str
}
}
Future<void> _runCmakeGeneration(String cmakePath, Directory buildDir, Directory sourceDir) async {
Future<void> _runCmakeGeneration({
required String cmakePath,
required String generator,
required Directory buildDir,
required Directory sourceDir,
}) async {
final Stopwatch sw = Stopwatch()..start();
await buildDir.create(recursive: true);
......@@ -246,7 +258,7 @@ Future<void> _runCmakeGeneration(String cmakePath, Directory buildDir, Directory
'-B',
buildDir.path,
'-G',
_cmakeVisualStudioGeneratorIdentifier,
generator,
],
trace: true,
);
......
......@@ -167,6 +167,19 @@ class VisualStudio {
]);
}
/// The generator string to pass to CMake to select this Visual Studio
/// version.
String? get cmakeGenerator {
// From https://cmake.org/cmake/help/v3.22/manual/cmake-generators.7.html#visual-studio-generators
switch (_majorVersion) {
case 17:
return 'Visual Studio 17 2022';
case 16:
default:
return 'Visual Studio 16 2019';
}
}
/// The major version of the Visual Studio install, as an integer.
int? get _majorVersion => fullVersion != null ? int.tryParse(fullVersion!.split('.')[0]) : null;
......
......@@ -39,6 +39,20 @@ const Map<String, dynamic> _defaultResponse = <String, dynamic>{
},
};
// A minimum version of a response where a VS 2022 installation was found.
const Map<String, dynamic> _vs2022Response = <String, dynamic>{
'installationPath': visualStudioPath,
'displayName': 'Visual Studio Community 2022',
'installationVersion': '17.0.31903.59',
'isRebootRequired': false,
'isComplete': true,
'isLaunchable': true,
'isPrerelease': false,
'catalog': <String, dynamic>{
'productDisplayVersion': '17.0.0',
},
};
// A minimum version of a response where a Build Tools installation was found.
const Map<String, dynamic> _defaultBuildToolsResponse = <String, dynamic>{
'installationPath': visualStudioPath,
......@@ -732,6 +746,7 @@ void main() {
expect(visualStudio.isAtLeastMinimumVersion, true);
expect(visualStudio.hasNecessaryComponents, true);
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 16 2019'));
});
testWithoutContext('Everything returns good values when Build Tools is present with all components', () {
......@@ -755,6 +770,23 @@ void main() {
expect(visualStudio.cmakePath, equals(cmakePath));
});
testWithoutContext('properties return the right value for Visual Studio 2022', () {
final VisualStudioFixture fixture = setUpVisualStudio();
final VisualStudio visualStudio = fixture.visualStudio;
setMockCompatibleVisualStudioInstallation(
_vs2022Response,
fixture.fileSystem,
fixture.processManager,
);
expect(visualStudio.isInstalled, true);
expect(visualStudio.isAtLeastMinimumVersion, true);
expect(visualStudio.hasNecessaryComponents, true);
expect(visualStudio.cmakePath, equals(cmakePath));
expect(visualStudio.cmakeGenerator, equals('Visual Studio 17 2022'));
});
testWithoutContext('Metadata is for compatible version when latest is missing components', () {
final VisualStudioFixture fixture = setUpVisualStudio();
final VisualStudio visualStudio = fixture.visualStudio;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment