cmVSSetupHelper.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. namespace {
  46. const WCHAR* Win10SDKComponent =
  47. L"Microsoft.VisualStudio.Component.Windows10SDK";
  48. const WCHAR* Win81SDKComponent =
  49. L"Microsoft.VisualStudio.Component.Windows81SDK";
  50. const WCHAR* ComponentType = L"Component";
  51. bool LoadVSInstanceVCToolsetVersion(VSInstanceInfo& vsInstanceInfo)
  52. {
  53. std::string const vcRoot = vsInstanceInfo.GetInstallLocation();
  54. std::string vcToolsVersionFile =
  55. vcRoot + "/VC/Auxiliary/Build/Microsoft.VCToolsVersion.default.txt";
  56. std::string vcToolsVersion;
  57. cmsys::ifstream fin(vcToolsVersionFile.c_str());
  58. if (!fin || !cmSystemTools::GetLineFromStream(fin, vcToolsVersion)) {
  59. return false;
  60. }
  61. vcToolsVersion = cmTrimWhitespace(vcToolsVersion);
  62. std::string const vcToolsDir = vcRoot + "/VC/Tools/MSVC/" + vcToolsVersion;
  63. if (!cmSystemTools::FileIsDirectory(vcToolsDir)) {
  64. return false;
  65. }
  66. vsInstanceInfo.VCToolsetVersion = vcToolsVersion;
  67. return true;
  68. }
  69. }
  70. std::string VSInstanceInfo::GetInstallLocation() const
  71. {
  72. return this->VSInstallLocation;
  73. }
  74. cmVSSetupAPIHelper::cmVSSetupAPIHelper(unsigned int version)
  75. : Version(version)
  76. , setupConfig(NULL)
  77. , setupConfig2(NULL)
  78. , setupHelper(NULL)
  79. , initializationFailure(false)
  80. {
  81. comInitialized = CoInitializeEx(NULL, 0);
  82. if (SUCCEEDED(comInitialized)) {
  83. Initialize();
  84. } else {
  85. initializationFailure = true;
  86. }
  87. }
  88. cmVSSetupAPIHelper::~cmVSSetupAPIHelper()
  89. {
  90. setupHelper = NULL;
  91. setupConfig2 = NULL;
  92. setupConfig = NULL;
  93. if (SUCCEEDED(comInitialized))
  94. CoUninitialize();
  95. }
  96. bool cmVSSetupAPIHelper::SetVSInstance(std::string const& vsInstallLocation,
  97. std::string const& vsInstallVersion)
  98. {
  99. this->SpecifiedVSInstallLocation = vsInstallLocation;
  100. cmSystemTools::ConvertToUnixSlashes(this->SpecifiedVSInstallLocation);
  101. this->SpecifiedVSInstallVersion = vsInstallVersion;
  102. chosenInstanceInfo = VSInstanceInfo();
  103. return this->EnumerateAndChooseVSInstance();
  104. }
  105. bool cmVSSetupAPIHelper::IsVSInstalled()
  106. {
  107. return this->EnumerateAndChooseVSInstance();
  108. }
  109. bool cmVSSetupAPIHelper::IsWin10SDKInstalled()
  110. {
  111. return (this->EnumerateAndChooseVSInstance() &&
  112. chosenInstanceInfo.IsWin10SDKInstalled);
  113. }
  114. bool cmVSSetupAPIHelper::IsWin81SDKInstalled()
  115. {
  116. return (this->EnumerateAndChooseVSInstance() &&
  117. chosenInstanceInfo.IsWin81SDKInstalled);
  118. }
  119. bool cmVSSetupAPIHelper::CheckInstalledComponent(
  120. SmartCOMPtr<ISetupPackageReference> package, bool& bWin10SDK,
  121. bool& bWin81SDK)
  122. {
  123. bool ret = false;
  124. bWin10SDK = bWin81SDK = false;
  125. SmartBSTR bstrId;
  126. if (FAILED(package->GetId(&bstrId))) {
  127. return ret;
  128. }
  129. SmartBSTR bstrType;
  130. if (FAILED(package->GetType(&bstrType))) {
  131. return ret;
  132. }
  133. std::wstring id = std::wstring(bstrId);
  134. std::wstring type = std::wstring(bstrType);
  135. // Checks for any version of Win10 SDK. The version is appended at the end of
  136. // the
  137. // component name ex: Microsoft.VisualStudio.Component.Windows10SDK.10240
  138. if (id.find(Win10SDKComponent) != std::wstring::npos &&
  139. type.compare(ComponentType) == 0) {
  140. bWin10SDK = true;
  141. ret = true;
  142. }
  143. if (id.compare(Win81SDKComponent) == 0 && type.compare(ComponentType) == 0) {
  144. bWin81SDK = true;
  145. ret = true;
  146. }
  147. return ret;
  148. }
  149. // Gather additional info such as if VCToolset, WinSDKs are installed, location
  150. // of VS and version information.
  151. bool cmVSSetupAPIHelper::GetVSInstanceInfo(
  152. SmartCOMPtr<ISetupInstance2> pInstance, VSInstanceInfo& vsInstanceInfo)
  153. {
  154. if (pInstance == NULL)
  155. return false;
  156. InstanceState state;
  157. if (FAILED(pInstance->GetState(&state))) {
  158. return false;
  159. }
  160. SmartBSTR bstrVersion;
  161. if (FAILED(pInstance->GetInstallationVersion(&bstrVersion))) {
  162. return false;
  163. } else {
  164. vsInstanceInfo.Version =
  165. cmsys::Encoding::ToNarrow(std::wstring(bstrVersion));
  166. }
  167. // Reboot may have been required before the installation path was created.
  168. SmartBSTR bstrInstallationPath;
  169. if ((eLocal & state) == eLocal) {
  170. if (FAILED(pInstance->GetInstallationPath(&bstrInstallationPath))) {
  171. return false;
  172. } else {
  173. vsInstanceInfo.VSInstallLocation =
  174. cmsys::Encoding::ToNarrow(std::wstring(bstrInstallationPath));
  175. cmSystemTools::ConvertToUnixSlashes(vsInstanceInfo.VSInstallLocation);
  176. }
  177. }
  178. // Check if a compiler is installed with this instance.
  179. if (!LoadVSInstanceVCToolsetVersion(vsInstanceInfo)) {
  180. return false;
  181. }
  182. // Reboot may have been required before the product package was registered
  183. // (last).
  184. if ((eRegistered & state) == eRegistered) {
  185. SmartCOMPtr<ISetupPackageReference> product;
  186. if (FAILED(pInstance->GetProduct(&product)) || !product) {
  187. return false;
  188. }
  189. LPSAFEARRAY lpsaPackages;
  190. if (FAILED(pInstance->GetPackages(&lpsaPackages)) ||
  191. lpsaPackages == NULL) {
  192. return false;
  193. }
  194. int lower = lpsaPackages->rgsabound[0].lLbound;
  195. int upper = lpsaPackages->rgsabound[0].cElements + lower;
  196. IUnknown** ppData = (IUnknown**)lpsaPackages->pvData;
  197. for (int i = lower; i < upper; i++) {
  198. SmartCOMPtr<ISetupPackageReference> package = NULL;
  199. if (FAILED(ppData[i]->QueryInterface(IID_ISetupPackageReference,
  200. (void**)&package)) ||
  201. package == NULL)
  202. continue;
  203. bool win10SDKInstalled = false;
  204. bool win81SDkInstalled = false;
  205. bool ret =
  206. CheckInstalledComponent(package, win10SDKInstalled, win81SDkInstalled);
  207. if (ret) {
  208. vsInstanceInfo.IsWin10SDKInstalled |= win10SDKInstalled;
  209. vsInstanceInfo.IsWin81SDKInstalled |= win81SDkInstalled;
  210. }
  211. }
  212. SafeArrayDestroy(lpsaPackages);
  213. }
  214. return true;
  215. }
  216. bool cmVSSetupAPIHelper::GetVSInstanceInfo(std::string& vsInstallLocation)
  217. {
  218. vsInstallLocation.clear();
  219. bool isInstalled = this->EnumerateAndChooseVSInstance();
  220. if (isInstalled) {
  221. vsInstallLocation = chosenInstanceInfo.GetInstallLocation();
  222. }
  223. return isInstalled;
  224. }
  225. bool cmVSSetupAPIHelper::GetVSInstanceVersion(std::string& vsInstanceVersion)
  226. {
  227. vsInstanceVersion.clear();
  228. bool isInstalled = this->EnumerateAndChooseVSInstance();
  229. if (isInstalled) {
  230. vsInstanceVersion = chosenInstanceInfo.Version;
  231. }
  232. return isInstalled;
  233. }
  234. bool cmVSSetupAPIHelper::GetVCToolsetVersion(std::string& vsToolsetVersion)
  235. {
  236. vsToolsetVersion.clear();
  237. bool isInstalled = this->EnumerateAndChooseVSInstance();
  238. if (isInstalled) {
  239. vsToolsetVersion = chosenInstanceInfo.VCToolsetVersion;
  240. }
  241. return isInstalled && !vsToolsetVersion.empty();
  242. }
  243. bool cmVSSetupAPIHelper::IsEWDKEnabled()
  244. {
  245. std::string envEnterpriseWDK, envDisableRegistryUse;
  246. cmSystemTools::GetEnv("EnterpriseWDK", envEnterpriseWDK);
  247. cmSystemTools::GetEnv("DisableRegistryUse", envDisableRegistryUse);
  248. if (!cmSystemTools::Strucmp(envEnterpriseWDK.c_str(), "True") &&
  249. !cmSystemTools::Strucmp(envDisableRegistryUse.c_str(), "True")) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. bool cmVSSetupAPIHelper::EnumerateAndChooseVSInstance()
  255. {
  256. bool isVSInstanceExists = false;
  257. if (chosenInstanceInfo.VSInstallLocation.compare("") != 0) {
  258. return true;
  259. }
  260. if (this->IsEWDKEnabled()) {
  261. std::string envWindowsSdkDir81, envVSVersion, envVsInstallDir;
  262. cmSystemTools::GetEnv("WindowsSdkDir_81", envWindowsSdkDir81);
  263. cmSystemTools::GetEnv("VisualStudioVersion", envVSVersion);
  264. cmSystemTools::GetEnv("VSINSTALLDIR", envVsInstallDir);
  265. if (envVSVersion.empty() || envVsInstallDir.empty())
  266. return false;
  267. chosenInstanceInfo.VSInstallLocation = envVsInstallDir;
  268. chosenInstanceInfo.Version = envVSVersion;
  269. if (!LoadVSInstanceVCToolsetVersion(chosenInstanceInfo)) {
  270. return false;
  271. }
  272. chosenInstanceInfo.IsWin10SDKInstalled = true;
  273. chosenInstanceInfo.IsWin81SDKInstalled = !envWindowsSdkDir81.empty();
  274. return true;
  275. }
  276. if (initializationFailure || setupConfig == NULL || setupConfig2 == NULL ||
  277. setupHelper == NULL)
  278. return false;
  279. std::string envVSCommonToolsDir;
  280. std::string envVSCommonToolsDirEnvName =
  281. "VS" + std::to_string(this->Version) + "0COMNTOOLS";
  282. if (cmSystemTools::GetEnv(envVSCommonToolsDirEnvName.c_str(),
  283. envVSCommonToolsDir)) {
  284. cmSystemTools::ConvertToUnixSlashes(envVSCommonToolsDir);
  285. }
  286. std::vector<VSInstanceInfo> vecVSInstances;
  287. SmartCOMPtr<IEnumSetupInstances> enumInstances = NULL;
  288. if (FAILED(
  289. setupConfig2->EnumInstances((IEnumSetupInstances**)&enumInstances)) ||
  290. !enumInstances) {
  291. return false;
  292. }
  293. std::string const wantVersion = std::to_string(this->Version) + '.';
  294. SmartCOMPtr<ISetupInstance> instance;
  295. while (SUCCEEDED(enumInstances->Next(1, &instance, NULL)) && instance) {
  296. SmartCOMPtr<ISetupInstance2> instance2 = NULL;
  297. if (FAILED(
  298. instance->QueryInterface(IID_ISetupInstance2, (void**)&instance2)) ||
  299. !instance2) {
  300. instance = NULL;
  301. continue;
  302. }
  303. VSInstanceInfo instanceInfo;
  304. bool isInstalled = GetVSInstanceInfo(instance2, instanceInfo);
  305. instance = instance2 = NULL;
  306. if (isInstalled) {
  307. // We are looking for a specific major version.
  308. if (instanceInfo.Version.size() < wantVersion.size() ||
  309. instanceInfo.Version.substr(0, wantVersion.size()) != wantVersion) {
  310. continue;
  311. }
  312. if (!this->SpecifiedVSInstallLocation.empty()) {
  313. // We are looking for a specific instance.
  314. std::string currentVSLocation = instanceInfo.GetInstallLocation();
  315. if (cmSystemTools::ComparePath(currentVSLocation,
  316. this->SpecifiedVSInstallLocation)) {
  317. if (this->SpecifiedVSInstallVersion.empty() ||
  318. instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  319. chosenInstanceInfo = instanceInfo;
  320. return true;
  321. }
  322. }
  323. } else if (!this->SpecifiedVSInstallVersion.empty()) {
  324. // We are looking for a specific version.
  325. if (instanceInfo.Version == this->SpecifiedVSInstallVersion) {
  326. chosenInstanceInfo = instanceInfo;
  327. return true;
  328. }
  329. } else {
  330. // We are not looking for a specific instance.
  331. // If we've been given a hint then use it.
  332. if (!envVSCommonToolsDir.empty()) {
  333. std::string currentVSLocation =
  334. cmStrCat(instanceInfo.GetInstallLocation(), "/Common7/Tools");
  335. if (cmSystemTools::ComparePath(currentVSLocation,
  336. envVSCommonToolsDir)) {
  337. chosenInstanceInfo = instanceInfo;
  338. return true;
  339. }
  340. }
  341. // Otherwise, add this to the list of candidates.
  342. vecVSInstances.push_back(instanceInfo);
  343. }
  344. }
  345. }
  346. if (vecVSInstances.size() > 0) {
  347. isVSInstanceExists = true;
  348. int index = ChooseVSInstance(vecVSInstances);
  349. chosenInstanceInfo = vecVSInstances[index];
  350. }
  351. return isVSInstanceExists;
  352. }
  353. int cmVSSetupAPIHelper::ChooseVSInstance(
  354. const std::vector<VSInstanceInfo>& vecVSInstances)
  355. {
  356. if (vecVSInstances.size() == 0)
  357. return -1;
  358. if (vecVSInstances.size() == 1)
  359. return 0;
  360. unsigned int chosenIndex = 0;
  361. for (unsigned int i = 1; i < vecVSInstances.size(); i++) {
  362. // If the current has Win10 SDK but not the chosen one, then choose the
  363. // current VS instance
  364. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  365. vecVSInstances[i].IsWin10SDKInstalled) {
  366. chosenIndex = i;
  367. continue;
  368. }
  369. // If the chosen one has Win10 SDK but the current one is not, then look at
  370. // the next VS instance even the current
  371. // instance version may be higher
  372. if (vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  373. !vecVSInstances[i].IsWin10SDKInstalled) {
  374. continue;
  375. }
  376. // If both chosen one and current one doesn't have Win10 SDK but the
  377. // current one has Win8.1 SDK installed,
  378. // then choose the current one
  379. if (!vecVSInstances[chosenIndex].IsWin10SDKInstalled &&
  380. !vecVSInstances[i].IsWin10SDKInstalled &&
  381. !vecVSInstances[chosenIndex].IsWin81SDKInstalled &&
  382. vecVSInstances[i].IsWin81SDKInstalled) {
  383. chosenIndex = i;
  384. continue;
  385. }
  386. // If there is no difference in WinSDKs then look for the highest version
  387. // of installed VS
  388. if ((vecVSInstances[chosenIndex].IsWin10SDKInstalled ==
  389. vecVSInstances[i].IsWin10SDKInstalled) &&
  390. (vecVSInstances[chosenIndex].IsWin81SDKInstalled ==
  391. vecVSInstances[i].IsWin81SDKInstalled) &&
  392. vecVSInstances[chosenIndex].Version < vecVSInstances[i].Version) {
  393. chosenIndex = i;
  394. continue;
  395. }
  396. }
  397. return chosenIndex;
  398. }
  399. bool cmVSSetupAPIHelper::Initialize()
  400. {
  401. if (initializationFailure)
  402. return false;
  403. if (FAILED(comInitialized)) {
  404. initializationFailure = true;
  405. return false;
  406. }
  407. if (FAILED(setupConfig.CoCreateInstance(CLSID_SetupConfiguration, NULL,
  408. IID_ISetupConfiguration,
  409. CLSCTX_INPROC_SERVER)) ||
  410. setupConfig == NULL) {
  411. initializationFailure = true;
  412. return false;
  413. }
  414. if (FAILED(setupConfig.QueryInterface(IID_ISetupConfiguration2,
  415. (void**)&setupConfig2)) ||
  416. setupConfig2 == NULL) {
  417. initializationFailure = true;
  418. return false;
  419. }
  420. if (FAILED(
  421. setupConfig.QueryInterface(IID_ISetupHelper, (void**)&setupHelper)) ||
  422. setupHelper == NULL) {
  423. initializationFailure = true;
  424. return false;
  425. }
  426. initializationFailure = false;
  427. return true;
  428. }