mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-05 21:31:08 -06:00
VS: refactor EnumerateAndChooseVSInstance
Move all VS instance enumeration code using COM interface to EnumerateVSInstancesWithCOM
This commit is contained in:
@@ -295,6 +295,39 @@ bool cmVSSetupAPIHelper::IsEWDKEnabled()
|
||||
return false;
|
||||
}
|
||||
|
||||
bool cmVSSetupAPIHelper::EnumerateVSInstancesWithCOM(
|
||||
std::vector<VSInstanceInfo>& VSInstances)
|
||||
{
|
||||
if (initializationFailure || setupConfig == NULL || setupConfig2 == NULL ||
|
||||
setupHelper == NULL)
|
||||
return false;
|
||||
|
||||
SmartCOMPtr<IEnumSetupInstances> enumInstances = NULL;
|
||||
if (FAILED(
|
||||
setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
|
||||
!enumInstances) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SmartCOMPtr<ISetupInstance> instance;
|
||||
while (SUCCEEDED(enumInstances->Next(1, &instance, NULL)) && instance) {
|
||||
SmartCOMPtr<ISetupInstance2> instance2 = NULL;
|
||||
if (FAILED(
|
||||
instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
|
||||
!instance2) {
|
||||
instance = NULL;
|
||||
continue;
|
||||
}
|
||||
|
||||
VSInstanceInfo instanceInfo;
|
||||
bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
|
||||
instance = instance2 = NULL;
|
||||
if (isInstalled)
|
||||
VSInstances.push_back(instanceInfo);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
|
||||
{
|
||||
bool isVSInstanceExists = false;
|
||||
@@ -321,10 +354,6 @@ bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
|
||||
return true;
|
||||
}
|
||||
|
||||
if (initializationFailure || setupConfig == NULL || setupConfig2 == NULL ||
|
||||
setupHelper == NULL)
|
||||
return false;
|
||||
|
||||
std::string envVSCommonToolsDir;
|
||||
std::string envVSCommonToolsDirEnvName =
|
||||
"VS" + std::to_string(this->Version) + "0COMNTOOLS";
|
||||
@@ -334,72 +363,59 @@ bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
|
||||
cmSystemTools::ConvertToUnixSlashes(envVSCommonToolsDir);
|
||||
}
|
||||
|
||||
std::vector<VSInstanceInfo> vecVSInstances;
|
||||
SmartCOMPtr<IEnumSetupInstances> enumInstances = NULL;
|
||||
if (FAILED(
|
||||
setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
|
||||
!enumInstances) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string const wantVersion = std::to_string(this->Version) + '.';
|
||||
|
||||
bool specifiedLocationNotSpecifiedVersion = false;
|
||||
|
||||
SmartCOMPtr<ISetupInstance> instance;
|
||||
while (SUCCEEDED(enumInstances->Next(1, &instance, NULL)) && instance) {
|
||||
SmartCOMPtr<ISetupInstance2> instance2 = NULL;
|
||||
if (FAILED(
|
||||
instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
|
||||
!instance2) {
|
||||
instance = NULL;
|
||||
|
||||
std::vector<VSInstanceInfo> vecVSInstancesAll;
|
||||
|
||||
// Enumerate VS instances with COM interface
|
||||
if (!EnumerateVSInstancesWithCOM(vecVSInstancesAll)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VSInstanceInfo> vecVSInstances;
|
||||
for (const auto& instanceInfo : vecVSInstancesAll) {
|
||||
// We are looking for a specific major version.
|
||||
if (instanceInfo.Version.size() < wantVersion.size() ||
|
||||
instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
VSInstanceInfo instanceInfo;
|
||||
bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
|
||||
instance = instance2 = NULL;
|
||||
|
||||
if (isInstalled) {
|
||||
// We are looking for a specific major version.
|
||||
if (instanceInfo.Version.size() < wantVersion.size() ||
|
||||
instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!this->SpecifiedVSInstallLocation.empty()) {
|
||||
// We are looking for a specific instance.
|
||||
std::string currentVSLocation = instanceInfo.GetInstallLocation();
|
||||
if (cmSystemTools::ComparePath(currentVSLocation,
|
||||
this->SpecifiedVSInstallLocation)) {
|
||||
if (this->SpecifiedVSInstallVersion.empty() ||
|
||||
instanceInfo.Version == this->SpecifiedVSInstallVersion) {
|
||||
chosenInstanceInfo = instanceInfo;
|
||||
return true;
|
||||
}
|
||||
specifiedLocationNotSpecifiedVersion = true;
|
||||
}
|
||||
} else if (!this->SpecifiedVSInstallVersion.empty()) {
|
||||
// We are looking for a specific version.
|
||||
if (instanceInfo.Version == this->SpecifiedVSInstallVersion) {
|
||||
if (!this->SpecifiedVSInstallLocation.empty()) {
|
||||
// We are looking for a specific instance.
|
||||
std::string currentVSLocation = instanceInfo.GetInstallLocation();
|
||||
if (cmSystemTools::ComparePath(currentVSLocation,
|
||||
this->SpecifiedVSInstallLocation)) {
|
||||
if (this->SpecifiedVSInstallVersion.empty() ||
|
||||
instanceInfo.Version == this->SpecifiedVSInstallVersion) {
|
||||
chosenInstanceInfo = instanceInfo;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// We are not looking for a specific instance.
|
||||
// If we've been given a hint then use it.
|
||||
if (!envVSCommonToolsDir.empty()) {
|
||||
std::string currentVSLocation =
|
||||
cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
|
||||
if (cmSystemTools::ComparePath(currentVSLocation,
|
||||
envVSCommonToolsDir)) {
|
||||
chosenInstanceInfo = instanceInfo;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Otherwise, add this to the list of candidates.
|
||||
vecVSInstances.push_back(instanceInfo);
|
||||
specifiedLocationNotSpecifiedVersion = true;
|
||||
}
|
||||
} else if (!this->SpecifiedVSInstallVersion.empty()) {
|
||||
// We are looking for a specific version.
|
||||
if (instanceInfo.Version == this->SpecifiedVSInstallVersion) {
|
||||
chosenInstanceInfo = instanceInfo;
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// We are not looking for a specific instance.
|
||||
// If we've been given a hint then use it.
|
||||
if (!envVSCommonToolsDir.empty()) {
|
||||
std::string currentVSLocation =
|
||||
cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
|
||||
if (cmSystemTools::ComparePath(currentVSLocation,
|
||||
envVSCommonToolsDir)) {
|
||||
chosenInstanceInfo = instanceInfo;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// Otherwise, add this to the list of candidates.
|
||||
vecVSInstances.push_back(instanceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -118,6 +118,7 @@ private:
|
||||
int ChooseVSInstance(const std::vector<VSInstanceInfo>& vecVSInstances);
|
||||
bool EnumerateAndChooseVSInstance();
|
||||
bool LoadSpecifiedVSInstanceFromDisk();
|
||||
bool EnumerateVSInstancesWithCOM(std::vector<VSInstanceInfo>& VSInstances);
|
||||
|
||||
unsigned int Version;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user