cmVSSetupHelper.cxx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmVSSetupHelper.h"
  4. #include "cmsys/Encoding.hxx"
  5. #include "cmsys/FStream.hxx"
  6. #include "cmStringAlgorithms.h"
  7. #include "cmSystemTools.h"
  8. #ifndef VSSetupConstants
  9. # define VSSetupConstants
  10. /* clang-format off */
  11. const IID IID_ISetupConfiguration = {
  12. 0x42843719, 0xDB4C, 0x46C2,
  13. { 0x8E, 0x7C, 0x64, 0xF1, 0x81, 0x6E, 0xFD, 0x5B }
  14. };
  15. const IID IID_ISetupConfiguration2 = {
  16. 0x26AAB78C, 0x4A60, 0x49D6,
  17. { 0xAF, 0x3B, 0x3C, 0x35, 0xBC, 0x93, 0x36, 0x5D }
  18. };
  19. const IID IID_ISetupPackageReference = {
  20. 0xda8d8a16, 0xb2b6, 0x4487,
  21. { 0xa2, 0xf1, 0x59, 0x4c, 0xcc, 0xcd, 0x6b, 0xf5 }
  22. };
  23. const IID IID_ISetupHelper = {
  24. 0x42b21b78, 0x6192, 0x463e,
  25. { 0x87, 0xbf, 0xd5, 0x77, 0x83, 0x8f, 0x1d, 0x5c }
  26. };
  27. const IID IID_IEnumSetupInstances = {
  28. 0x6380BCFF, 0x41D3, 0x4B2E,
  29. { 0x8B, 0x2E, 0xBF, 0x8A, 0x68, 0x10, 0xC8, 0x48 }
  30. };
  31. const IID IID_ISetupInstance2 = {
  32. 0x89143C9A, 0x05AF, 0x49B0,
  33. { 0xB7, 0x17, 0x72, 0xE2, 0x18, 0xA2, 0x18, 0x5C }
  34. };
  35. const IID IID_ISetupInstance = {
  36. 0xB41463C3, 0x8866, 0x43B5,
  37. { 0xBC, 0x33, 0x2B, 0x06, 0x76, 0xF7, 0xF4, 0x2E }
  38. };
  39. const CLSID CLSID_SetupConfiguration = {
  40. 0x177F0C4A, 0x1CD3, 0x4DE7,
  41. { 0xA3, 0x2C, 0x71, 0xDB, 0xBB, 0x9F, 0xA3, 0x6D }
  42. };
  43. /* clang-format on */
  44. #endif
  45. const WCHAR* Win10SDKComponent =
  46. L"Microsoft.VisualStudio.Component.Windows10SDK";
  47. const WCHAR* Win81SDKComponent =
  48. L"Microsoft.VisualStudio.Component.Windows81SDK";
  49. const WCHAR* ComponentType = L"Component";
  50. std::string VSInstanceInfo::GetInstallLocation() const
  51. {
  52. std::string loc = cmsys::Encoding::ToNarrow(this->VSInstallLocation);
  53. cmSystemTools::ConvertToUnixSlashes(loc);
  54. return loc;
  55. }
  56. cmVSSetupAPIHelper::cmVSSetupAPIHelper(unsigned int version)
  57. : Version(version)
  58. , setupConfig(NULL)
  59. , setupConfig2(NULL)
  60. , setupHelper(NULL)
  61. , initializationFailure(false)
  62. {
  63. comInitialized = CoInitializeEx(NULL, 0);
  64. if (SUCCEEDED(comInitialized)) {
  65. Initialize();
  66. } else {
  67. initializationFailure = true;
  68. }
  69. }
  70. cmVSSetupAPIHelper::~cmVSSetupAPIHelper()
  71. {
  72. setupHelper = NULL;
  73. setupConfig2 = NULL;
  74. setupConfig = NULL;
  75. if (SUCCEEDED(comInitialized))
  76. CoUninitialize();
  77. }
  78. bool cmVSSetupAPIHelper::SetVSInstance(std::string const& vsInstallLocation)
  79. {
  80. this->SpecifiedVSInstallLocation = vsInstallLocation;
  81. cmSystemTools::ConvertToUnixSlashes(this->SpecifiedVSInstallLocation);
  82. chosenInstanceInfo = VSInstanceInfo();
  83. return this->EnumerateAndChooseVSInstance();
  84. }
  85. bool cmVSSetupAPIHelper::IsVSInstalled()
  86. {
  87. return this->EnumerateAndChooseVSInstance();
  88. }
  89. bool cmVSSetupAPIHelper::IsWin10SDKInstalled()
  90. {
  91. return (this->EnumerateAndChooseVSInstance() &&
  92. chosenInstanceInfo.IsWin10SDKInstalled);
  93. }
  94. bool cmVSSetupAPIHelper::IsWin81SDKInstalled()
  95. {
  96. return (this->EnumerateAndChooseVSInstance() &&
  97. chosenInstanceInfo.IsWin81SDKInstalled);
  98. }
  99. bool cmVSSetupAPIHelper::CheckInstalledComponent(
  100. SmartCOMPtr<ISetupPackageReference> package, bool& bWin10SDK,
  101. bool& bWin81SDK)
  102. {
  103. bool ret = false;
  104. bWin10SDK = bWin81SDK = false;
  105. SmartBSTR bstrId;
  106. if (FAILED(package->GetId(&bstrId))) {
  107. return ret;
  108. }
  109. SmartBSTR bstrType;
  110. if (FAILED(package->GetType(&bstrType))) {
  111. return ret;
  112. }
  113. std::wstring id = std::wstring(bstrId);
  114. std::wstring type = std::wstring(bstrType);
  115. // Checks for any version of Win10 SDK. The version is appended at the end of
  116. // the
  117. // component name ex: Microsoft.VisualStudio.Component.Windows10SDK.10240
  118. if (id.find(Win10SDKComponent) != std::wstring::npos &&
  119. type.compare(ComponentType) == 0) {
  120. bWin10SDK = true;
  121. ret = true;
  122. }
  123. if (id.compare(Win81SDKComponent) == 0 && type.compare(ComponentType) == 0) {
  124. bWin81SDK = true;
  125. ret = true;
  126. }
  127. return ret;
  128. }
  129. // Gather additional info such as if VCToolset, WinSDKs are installed, location
  130. // of VS and version information.
  131. bool cmVSSetupAPIHelper::GetVSInstanceInfo(
  132. SmartCOMPtr<ISetupInstance2> pInstance, VSInstanceInfo& vsInstanceInfo)
  133. {
  134. if (pInstance == NULL)
  135. return false;
  136. SmartBSTR bstrId;
  137. if (SUCCEEDED(pInstance->GetInstanceId(&bstrId))) {
  138. vsInstanceInfo.InstanceId = std::wstring(bstrId);
  139. } else {
  140. return false;
  141. }
  142. InstanceState state;
  143. if (FAILED(pInstance->GetState(&state))) {
  144. return false;
  145. }
  146. ULONGLONG ullVersion = 0;
  147. SmartBSTR bstrVersion;
  148. if (FAILED(pInstance->GetInstallationVersion(&bstrVersion))) {
  149. return false;
  150. } else {
  151. vsInstanceInfo.Version = std::wstring(bstrVersion);
  152. if (FAILED(setupHelper->ParseVersion(bstrVersion, &ullVersion))) {
  153. vsInstanceInfo.ullVersion = 0;
  154. } else {
  155. vsInstanceInfo.ullVersion = ullVersion;
  156. }
  157. }
  158. // Reboot may have been required before the installation path was created.
  159. SmartBSTR bstrInstallationPath;
  160. if ((eLocal & state) == eLocal) {
  161. if (FAILED(pInstance->GetInstallationPath(&bstrInstallationPath))) {
  162. return false;
  163. } else {
  164. vsInstanceInfo.VSInstallLocation = std::wstring(bstrInstallationPath);
  165. }
  166. }
  167. // Check if a compiler is installed with this instance.
  168. {
  169. std::string const vcRoot = vsInstanceInfo.GetInstallLocation();
  170. std::string vcToolsVersionFile =
  171. vcRoot + "/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt";
  172. std::string vcToolsVersion;
  173. cmsys::ifstream fin(vcToolsVersionFile.c_str());
  174. if (!fin || !cmSystemTools::GetLineFromStream(fin, vcToolsVersion)) {
  175. return false;
  176. }
  177. vcToolsVersion = cmTrimWhitespace(vcToolsVersion);
  178. std::string const vcToolsDir = vcRoot + "/VC/Tools/MSVC/" + vcToolsVersion;
  179. if (!cmSystemTools::FileIsDirectory(vcToolsDir)) {
  180. return false;
  181. }
  182. vsInstanceInfo.VCToolsetVersion = vcToolsVersion;
  183. }
  184. // Reboot may have been required before the product package was registered
  185. // (last).
  186. if ((eRegistered & state) == eRegistered) {
  187. SmartCOMPtr<ISetupPackageReference> product;
  188. if (FAILED(pInstance->GetProduct(&product)) || !product) {
  189. return false;
  190. }
  191. LPSAFEARRAY lpsaPackages;
  192. if (FAILED(pInstance->GetPackages(&lpsaPackages)) ||
  193. lpsaPackages == NULL) {
  194. return false;
  195. }
  196. int lower = lpsaPackages->rgsabound[0].lLbound;
  197. int upper = lpsaPackages->rgsabound[0].cElements + lower;
  198. IUnknown** ppData = (IUnknown**)lpsaPackages->pvData;
  199. for (int i = lower; i < upper; i++) {
  200. SmartCOMPtr<ISetupPackageReference> package = NULL;
  201. if (FAILED(ppData[i]->QueryInterface(IID_ISetupPackageReference,
  202. (void**)&package)) ||
  203. package == NULL)
  204. continue;
  205. bool win10SDKInstalled = false;
  206. bool win81SDkInstalled = false;
  207. bool ret =
  208. CheckInstalledComponent(package, win10SDKInstalled, win81SDkInstalled);
  209. if (ret) {
  210. vsInstanceInfo.IsWin10SDKInstalled |= win10SDKInstalled;
  211. vsInstanceInfo.IsWin81SDKInstalled |= win81SDkInstalled;
  212. }
  213. }
  214. SafeArrayDestroy(lpsaPackages);
  215. }
  216. return true;
  217. }
  218. bool cmVSSetupAPIHelper::GetVSInstanceInfo(std::string& vsInstallLocation)
  219. {
  220. vsInstallLocation.clear();
  221. bool isInstalled = this->EnumerateAndChooseVSInstance();
  222. if (isInstalled) {
  223. vsInstallLocation = chosenInstanceInfo.GetInstallLocation();
  224. }
  225. return isInstalled;
  226. }
  227. bool cmVSSetupAPIHelper::GetVSInstanceVersion(std::string& vsInstanceVersion)
  228. {
  229. vsInstanceVersion.clear();
  230. bool isInstalled = this->EnumerateAndChooseVSInstance();
  231. if (isInstalled) {
  232. vsInstanceVersion = cmsys::Encoding::ToNarrow(chosenInstanceInfo.Version);
  233. }
  234. return isInstalled;
  235. }
  236. bool cmVSSetupAPIHelper::GetVCToolsetVersion(std::string& vsToolsetVersion)
  237. {
  238. vsToolsetVersion.clear();
  239. bool isInstalled = this->EnumerateAndChooseVSInstance();
  240. if (isInstalled) {
  241. vsToolsetVersion = chosenInstanceInfo.VCToolsetVersion;
  242. }
  243. return isInstalled && !vsToolsetVersion.empty();
  244. }
  245. bool cmVSSetupAPIHelper::IsEWDKEnabled()
  246. {
  247. std::string envEnterpriseWDK, envDisableRegistryUse;
  248. cmSystemTools::GetEnv("EnterpriseWDK", envEnterpriseWDK);
  249. cmSystemTools::GetEnv("DisableRegistryUse", envDisableRegistryUse);
  250. if (!cmSystemTools::Strucmp(envEnterpriseWDK.c_str(), "True") &&
  251. !cmSystemTools::Strucmp(envDisableRegistryUse.c_str(), "True")) {
  252. return true;
  253. }
  254. return false;
  255. }
  256. bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
  257. {
  258. bool isVSInstanceExists = false;
  259. if (chosenInstanceInfo.VSInstallLocation.compare(L"") != 0) {
  260. return true;
  261. }
  262. if (this->IsEWDKEnabled()) {
  263. std::string envWindowsSdkDir81, envVSVersion, envVsInstallDir;
  264. cmSystemTools::GetEnv("WindowsSdkDir_81", envWindowsSdkDir81);
  265. cmSystemTools::GetEnv("VisualStudioVersion", envVSVersion);
  266. cmSystemTools::GetEnv("VSINSTALLDIR", envVsInstallDir);
  267. if (envVSVersion.empty() || envVsInstallDir.empty())
  268. return false;
  269. chosenInstanceInfo.VSInstallLocation =
  270. std::wstring(envVsInstallDir.begin(), envVsInstallDir.end());
  271. chosenInstanceInfo.Version =
  272. std::wstring(envVSVersion.begin(), envVSVersion.end());
  273. chosenInstanceInfo.VCToolsetVersion = envVSVersion;
  274. chosenInstanceInfo.ullVersion = std::stoi(envVSVersion);
  275. chosenInstanceInfo.IsWin10SDKInstalled = true;
  276. chosenInstanceInfo.IsWin81SDKInstalled = !envWindowsSdkDir81.empty();
  277. return true;
  278. }
  279. if (initializationFailure || setupConfig == NULL || setupConfig2 == NULL ||
  280. setupHelper == NULL)
  281. return false;
  282. std::string envVSCommonToolsDir;
  283. std::string envVSCommonToolsDirEnvName =
  284. "VS" + std::to_string(this->Version) + "0COMNTOOLS";
  285. if (cmSystemTools::GetEnv(envVSCommonToolsDirEnvName.c_str(),
  286. envVSCommonToolsDir)) {
  287. cmSystemTools::ConvertToUnixSlashes(envVSCommonToolsDir);
  288. }
  289. std::vector<VSInstanceInfo> vecVSInstances;
  290. SmartCOMPtr<IEnumSetupInstances> enumInstances = NULL;
  291. if (FAILED(
  292. setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
  293. !enumInstances) {
  294. return false;
  295. }
  296. std::wstring const wantVersion = std::to_wstring(this->Version) + L'.';
  297. SmartCOMPtr<ISetupInstance> instance;
  298. while (SUCCEEDED(enumInstances->Next(1, &instance, NULL)) && instance) {
  299. SmartCOMPtr<ISetupInstance2> instance2 = NULL;
  300. if (FAILED(
  301. instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
  302. !instance2) {
  303. instance = NULL;
  304. continue;
  305. }
  306. VSInstanceInfo instanceInfo;
  307. bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
  308. instance = instance2 = NULL;
  309. if (isInstalled) {
  310. // We are looking for a specific major version.
  311. if (instanceInfo.Version.size() < wantVersion.size() ||
  312. instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
  313. continue;
  314. }
  315. if (!this->SpecifiedVSInstallLocation.empty()) {
  316. // We are looking for a specific instance.
  317. std::string currentVSLocation = instanceInfo.GetInstallLocation();
  318. if (cmSystemTools::ComparePath(currentVSLocation,
  319. this->SpecifiedVSInstallLocation)) {
  320. chosenInstanceInfo = instanceInfo;
  321. return true;
  322. }
  323. } else {
  324. // We are not looking for a specific instance.
  325. // If we've been given a hint then use it.
  326. if (!envVSCommonToolsDir.empty()) {
  327. std::string currentVSLocation =
  328. cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
  329. if (cmSystemTools::ComparePath(currentVSLocation,
  330. envVSCommonToolsDir)) {
  331. chosenInstanceInfo = instanceInfo;
  332. return true;
  333. }
  334. }
  335. // Otherwise, add this to the list of candidates.
  336. vecVSInstances.push_back(instanceInfo);
  337. }
  338. }
  339. }
  340. if (vecVSInstances.size() > 0) {
  341. isVSInstanceExists = true;
  342. int index = ChooseVSInstance(vecVSInstances);
  343. chosenInstanceInfo = vecVSInstances[index];
  344. }
  345. return isVSInstanceExists;
  346. }
  347. int cmVSSetupAPIHelper::ChooseVSInstance(
  348. const std::vector<VSInstanceInfo>& vecVSInstances)
  349. {
  350. if (vecVSInstances.size() == 0)
  351. return -1;
  352. if (vecVSInstances.size() == 1)
  353. return 0;
  354. unsigned int chosenIndex = 0;
  355. for (unsigned int i = 1; i < vecVSInstances.size(); i++) {
  356. // If the current has Win10 SDK but not the chosen one, then choose the
  357. // current VS instance
  358. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  359. vecVSInstances[i].IsWin10SDKInstalled) {
  360. chosenIndex = i;
  361. continue;
  362. }
  363. // If the chosen one has Win10 SDK but the current one is not, then look at
  364. // the next VS instance even the current
  365. // instance version may be higher
  366. if (vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  367. !vecVSInstances[i].IsWin10SDKInstalled) {
  368. continue;
  369. }
  370. // If both chosen one and current one doesn't have Win10 SDK but the
  371. // current one has Win8.1 SDK installed,
  372. // then choose the current one
  373. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  374. !vecVSInstances[i].IsWin10SDKInstalled &&
  375. !vecVSInstances[chosenIndex].IsWin81SDKInstalled &&
  376. vecVSInstances[i].IsWin81SDKInstalled) {
  377. chosenIndex = i;
  378. continue;
  379. }
  380. // If there is no difference in WinSDKs then look for the highest version
  381. // of installed VS
  382. if ((vecVSInstances[chosenIndex].IsWin10SDKInstalled ==
  383. vecVSInstances[i].IsWin10SDKInstalled) &&
  384. (vecVSInstances[chosenIndex].IsWin81SDKInstalled ==
  385. vecVSInstances[i].IsWin81SDKInstalled) &&
  386. vecVSInstances[chosenIndex].Version < vecVSInstances[i].Version) {
  387. chosenIndex = i;
  388. continue;
  389. }
  390. }
  391. return chosenIndex;
  392. }
  393. bool cmVSSetupAPIHelper::Initialize()
  394. {
  395. if (initializationFailure)
  396. return false;
  397. if (FAILED(comInitialized)) {
  398. initializationFailure = true;
  399. return false;
  400. }
  401. if (FAILED(setupConfig.CoCreateInstance(CLSID_SetupConfiguration, NULL,
  402. IID_ISetupConfiguration,
  403. CLSCTX_INPROC_SERVER)) ||
  404. setupConfig == NULL) {
  405. initializationFailure = true;
  406. return false;
  407. }
  408. if (FAILED(setupConfig.QueryInterface(IID_ISetupConfiguration2,
  409. (void**)&setupConfig2)) ||
  410. setupConfig2 == NULL) {
  411. initializationFailure = true;
  412. return false;
  413. }
  414. if (FAILED(
  415. setupConfig.QueryInterface(IID_ISetupHelper, (void**)&setupHelper)) ||
  416. setupHelper == NULL) {
  417. initializationFailure = true;
  418. return false;
  419. }
  420. initializationFailure = false;
  421. return true;
  422. }