cmGlobalVisualStudioGenerator.cxx 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmGlobalVisualStudioGenerator.h"
  4. #include <cassert>
  5. #include <future>
  6. #include <iostream>
  7. #include <sstream>
  8. #include <system_error>
  9. #include <utility>
  10. #include <cm/iterator>
  11. #include <cm/memory>
  12. #include <cmext/string_view>
  13. #include <windows.h>
  14. #include <objbase.h>
  15. #include <shellapi.h>
  16. #include "cmCallVisualStudioMacro.h"
  17. #include "cmCustomCommand.h"
  18. #include "cmCustomCommandLines.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmGeneratorTarget.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmMakefile.h"
  23. #include "cmMessageType.h"
  24. #include "cmPolicies.h"
  25. #include "cmSourceFile.h"
  26. #include "cmState.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmTarget.h"
  31. #include "cmake.h"
  32. cmGlobalVisualStudioGenerator::cmGlobalVisualStudioGenerator(cmake* cm)
  33. : cmGlobalGenerator(cm)
  34. {
  35. cm->GetState()->SetIsGeneratorMultiConfig(true);
  36. cm->GetState()->SetWindowsShell(true);
  37. cm->GetState()->SetWindowsVSIDE(true);
  38. this->DefaultPlatformName = "Win32";
  39. }
  40. cmGlobalVisualStudioGenerator::~cmGlobalVisualStudioGenerator() = default;
  41. cmGlobalVisualStudioGenerator::VSVersion
  42. cmGlobalVisualStudioGenerator::GetVersion() const
  43. {
  44. return this->Version;
  45. }
  46. void cmGlobalVisualStudioGenerator::SetVersion(VSVersion v)
  47. {
  48. this->Version = v;
  49. }
  50. void cmGlobalVisualStudioGenerator::EnableLanguage(
  51. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  52. {
  53. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME_DEFAULT",
  54. this->DefaultPlatformName);
  55. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  56. }
  57. bool cmGlobalVisualStudioGenerator::SetGeneratorPlatform(std::string const& p,
  58. cmMakefile* mf)
  59. {
  60. if (!this->InitializePlatform(mf)) {
  61. return false;
  62. }
  63. if (this->GetPlatformName() == "x64"_s) {
  64. mf->AddDefinition("CMAKE_FORCE_WIN64", "TRUE");
  65. } else if (this->GetPlatformName() == "Itanium"_s) {
  66. mf->AddDefinition("CMAKE_FORCE_IA64", "TRUE");
  67. }
  68. mf->AddDefinition("CMAKE_VS_PLATFORM_NAME", this->GetPlatformName());
  69. return this->cmGlobalGenerator::SetGeneratorPlatform(p, mf);
  70. }
  71. bool cmGlobalVisualStudioGenerator::InitializePlatform(cmMakefile*)
  72. {
  73. return true;
  74. }
  75. cmValue cmGlobalVisualStudioGenerator::GetDebuggerWorkingDirectory(
  76. cmGeneratorTarget* gt) const
  77. {
  78. if (cmValue ret = gt->GetProperty("VS_DEBUGGER_WORKING_DIRECTORY")) {
  79. return ret;
  80. } else {
  81. return cmGlobalGenerator::GetDebuggerWorkingDirectory(gt);
  82. }
  83. }
  84. std::string const& cmGlobalVisualStudioGenerator::GetPlatformName() const
  85. {
  86. if (!this->GeneratorPlatform.empty()) {
  87. return this->GeneratorPlatform;
  88. }
  89. return this->DefaultPlatformName;
  90. }
  91. char const* cmGlobalVisualStudioGenerator::GetIDEVersion() const
  92. {
  93. switch (this->Version) {
  94. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  95. return "14.0";
  96. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  97. return "15.0";
  98. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  99. return "16.0";
  100. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  101. return "17.0";
  102. }
  103. return "";
  104. }
  105. void cmGlobalVisualStudioGenerator::WriteSLNHeader(std::ostream& fout) const
  106. {
  107. char utf8bom[] = { char(0xEF), char(0xBB), char(0xBF) };
  108. fout.write(utf8bom, 3);
  109. fout << '\n';
  110. switch (this->Version) {
  111. case cmGlobalVisualStudioGenerator::VSVersion::VS14:
  112. // Visual Studio 14 writes .sln format 12.00
  113. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  114. if (this->ExpressEdition) {
  115. fout << "# Visual Studio Express 14 for Windows Desktop\n";
  116. } else {
  117. fout << "# Visual Studio 14\n";
  118. }
  119. break;
  120. case cmGlobalVisualStudioGenerator::VSVersion::VS15:
  121. // Visual Studio 15 writes .sln format 12.00
  122. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  123. if (this->ExpressEdition) {
  124. fout << "# Visual Studio Express 15 for Windows Desktop\n";
  125. } else {
  126. fout << "# Visual Studio 15\n";
  127. }
  128. break;
  129. case cmGlobalVisualStudioGenerator::VSVersion::VS16:
  130. // Visual Studio 16 writes .sln format 12.00
  131. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  132. if (this->ExpressEdition) {
  133. fout << "# Visual Studio Express 16 for Windows Desktop\n";
  134. } else {
  135. fout << "# Visual Studio Version 16\n";
  136. }
  137. break;
  138. case cmGlobalVisualStudioGenerator::VSVersion::VS17:
  139. // Visual Studio 17 writes .sln format 12.00
  140. fout << "Microsoft Visual Studio Solution File, Format Version 12.00\n";
  141. if (this->ExpressEdition) {
  142. fout << "# Visual Studio Express 17 for Windows Desktop\n";
  143. } else {
  144. fout << "# Visual Studio Version 17\n";
  145. }
  146. break;
  147. }
  148. }
  149. std::string cmGlobalVisualStudioGenerator::GetRegistryBase()
  150. {
  151. return cmGlobalVisualStudioGenerator::GetRegistryBase(this->GetIDEVersion());
  152. }
  153. std::string cmGlobalVisualStudioGenerator::GetRegistryBase(char const* version)
  154. {
  155. return cmStrCat(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\)",
  156. version);
  157. }
  158. void cmGlobalVisualStudioGenerator::AddExtraIDETargets()
  159. {
  160. // Add a special target that depends on ALL projects for easy build
  161. // of one configuration only.
  162. for (auto const& it : this->ProjectMap) {
  163. std::vector<cmLocalGenerator*> const& gen = it.second;
  164. // add the ALL_BUILD to the first local generator of each project
  165. if (!gen.empty()) {
  166. // Use no actual command lines so that the target itself is not
  167. // considered always out of date.
  168. auto cc = cm::make_unique<cmCustomCommand>();
  169. cc->SetEscapeOldStyle(false);
  170. cc->SetComment("Build all projects");
  171. cmTarget* allBuild =
  172. gen[0]->AddUtilityCommand("ALL_BUILD", true, std::move(cc));
  173. gen[0]->AddGeneratorTarget(
  174. cm::make_unique<cmGeneratorTarget>(allBuild, gen[0]));
  175. //
  176. // Organize in the "predefined targets" folder:
  177. //
  178. if (this->UseFolderProperty()) {
  179. allBuild->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  180. }
  181. // Now make all targets depend on the ALL_BUILD target
  182. for (cmLocalGenerator const* i : gen) {
  183. for (auto const& tgt : i->GetGeneratorTargets()) {
  184. if (tgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  185. tgt->IsImported()) {
  186. continue;
  187. }
  188. if (!this->IsExcluded(gen[0], tgt.get())) {
  189. allBuild->AddUtility(tgt->GetName(), false);
  190. }
  191. }
  192. }
  193. }
  194. }
  195. // Configure CMake Visual Studio macros, for this user on this version
  196. // of Visual Studio.
  197. this->ConfigureCMakeVisualStudioMacros();
  198. }
  199. void cmGlobalVisualStudioGenerator::ComputeTargetObjectDirectory(
  200. cmGeneratorTarget* gt) const
  201. {
  202. std::string dir =
  203. cmStrCat(gt->GetSupportDirectory(), '/', this->GetCMakeCFGIntDir(), '/');
  204. gt->ObjectDirectory = dir;
  205. }
  206. bool IsVisualStudioMacrosFileRegistered(std::string const& macrosFile,
  207. std::string const& regKeyBase,
  208. std::string& nextAvailableSubKeyName);
  209. void RegisterVisualStudioMacros(std::string const& macrosFile,
  210. std::string const& regKeyBase);
  211. #define CMAKE_VSMACROS_FILENAME "CMakeVSMacros2.vsmacros"
  212. #define CMAKE_VSMACROS_RELOAD_MACRONAME \
  213. "Macros.CMakeVSMacros2.Macros.ReloadProjects"
  214. #define CMAKE_VSMACROS_STOP_MACRONAME "Macros.CMakeVSMacros2.Macros.StopBuild"
  215. void cmGlobalVisualStudioGenerator::ConfigureCMakeVisualStudioMacros()
  216. {
  217. std::string dir = this->GetUserMacrosDirectory();
  218. if (!dir.empty()) {
  219. std::string src = cmStrCat(cmSystemTools::GetCMakeRoot(),
  220. "/Templates/" CMAKE_VSMACROS_FILENAME);
  221. std::string dst = cmStrCat(dir, "/CMakeMacros/" CMAKE_VSMACROS_FILENAME);
  222. // Copy the macros file to the user directory only if the
  223. // destination does not exist or the source location is newer.
  224. // This will allow the user to edit the macros for development
  225. // purposes but newer versions distributed with CMake will replace
  226. // older versions in user directories.
  227. int res;
  228. if (!cmSystemTools::FileTimeCompare(src, dst, &res) || res > 0) {
  229. if (!cmSystemTools::CopyFileAlways(src, dst)) {
  230. std::ostringstream oss;
  231. oss << "Could not copy from: " << src << std::endl
  232. << " to: " << dst << std::endl;
  233. cmSystemTools::Message(oss.str(), "Warning");
  234. }
  235. }
  236. RegisterVisualStudioMacros(dst, this->GetUserMacrosRegKeyBase());
  237. }
  238. }
  239. void cmGlobalVisualStudioGenerator::CallVisualStudioMacro(
  240. MacroName m, std::string const& vsSolutionFile)
  241. {
  242. // If any solution or project files changed during the generation,
  243. // tell Visual Studio to reload them...
  244. std::string dir = this->GetUserMacrosDirectory();
  245. // Only really try to call the macro if:
  246. // - there is a UserMacrosDirectory
  247. // - the CMake vsmacros file exists
  248. // - the CMake vsmacros file is registered
  249. // - there were .sln/.vcproj files changed during generation
  250. //
  251. if (!dir.empty()) {
  252. std::string macrosFile =
  253. cmStrCat(dir, "/CMakeMacros/" CMAKE_VSMACROS_FILENAME);
  254. std::string nextSubkeyName;
  255. if (cmSystemTools::FileExists(macrosFile) &&
  256. IsVisualStudioMacrosFileRegistered(
  257. macrosFile, this->GetUserMacrosRegKeyBase(), nextSubkeyName)) {
  258. if (m == MacroReload) {
  259. std::vector<std::string> filenames;
  260. this->GetFilesReplacedDuringGenerate(filenames);
  261. if (!filenames.empty()) {
  262. std::string projects = cmJoin(filenames, ";");
  263. cmCallVisualStudioMacro::CallMacro(
  264. vsSolutionFile, CMAKE_VSMACROS_RELOAD_MACRONAME, projects,
  265. this->GetCMakeInstance()->GetDebugOutput());
  266. }
  267. } else if (m == MacroStop) {
  268. cmCallVisualStudioMacro::CallMacro(
  269. vsSolutionFile, CMAKE_VSMACROS_STOP_MACRONAME, "",
  270. this->GetCMakeInstance()->GetDebugOutput());
  271. }
  272. }
  273. }
  274. }
  275. std::string cmGlobalVisualStudioGenerator::GetUserMacrosDirectory()
  276. {
  277. return "";
  278. }
  279. std::string cmGlobalVisualStudioGenerator::GetUserMacrosRegKeyBase()
  280. {
  281. return "";
  282. }
  283. bool cmGlobalVisualStudioGenerator::FindMakeProgram(cmMakefile* mf)
  284. {
  285. // Visual Studio generators know how to lookup their build tool
  286. // directly instead of needing a helper module to do it, so we
  287. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  288. if (mf->GetDefinition("CMAKE_MAKE_PROGRAM").IsOff()) {
  289. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetVSMakeProgram());
  290. }
  291. return true;
  292. }
  293. std::string cmGlobalVisualStudioGenerator::GetStartupProjectName(
  294. cmLocalGenerator const* root) const
  295. {
  296. cmValue n = root->GetMakefile()->GetProperty("VS_STARTUP_PROJECT");
  297. if (cmNonempty(n)) {
  298. std::string startup = *n;
  299. if (this->FindTarget(startup)) {
  300. return startup;
  301. }
  302. root->GetMakefile()->IssueMessage(
  303. MessageType::AUTHOR_WARNING,
  304. cmStrCat("Directory property VS_STARTUP_PROJECT specifies target "
  305. "'",
  306. startup, "' that does not exist. Ignoring."));
  307. }
  308. // default, if not specified
  309. return this->GetAllTargetName();
  310. }
  311. bool IsVisualStudioMacrosFileRegistered(std::string const& macrosFile,
  312. std::string const& regKeyBase,
  313. std::string& nextAvailableSubKeyName)
  314. {
  315. bool macrosRegistered = false;
  316. std::string s1;
  317. std::string s2;
  318. // Make lowercase local copies, convert to Unix slashes, and
  319. // see if the resulting strings are the same:
  320. s1 = cmSystemTools::LowerCase(macrosFile);
  321. cmSystemTools::ConvertToUnixSlashes(s1);
  322. std::string keyname;
  323. HKEY hkey = nullptr;
  324. LONG result = ERROR_SUCCESS;
  325. DWORD index = 0;
  326. keyname = cmStrCat(regKeyBase, "\\OtherProjects7");
  327. hkey = nullptr;
  328. result =
  329. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  330. 0, KEY_READ, &hkey);
  331. if (ERROR_SUCCESS == result) {
  332. // Iterate the subkeys and look for the values of interest in each subkey:
  333. wchar_t subkeyname[256];
  334. DWORD cch_subkeyname = cm::size(subkeyname);
  335. wchar_t keyclass[256];
  336. DWORD cch_keyclass = cm::size(keyclass);
  337. FILETIME lastWriteTime;
  338. lastWriteTime.dwHighDateTime = 0;
  339. lastWriteTime.dwLowDateTime = 0;
  340. while (ERROR_SUCCESS ==
  341. RegEnumKeyExW(hkey, index, subkeyname, &cch_subkeyname, 0, keyclass,
  342. &cch_keyclass, &lastWriteTime)) {
  343. // Open the subkey and query the values of interest:
  344. HKEY hsubkey = nullptr;
  345. result = RegOpenKeyExW(hkey, subkeyname, 0, KEY_READ, &hsubkey);
  346. if (ERROR_SUCCESS == result) {
  347. DWORD valueType = REG_SZ;
  348. wchar_t data1[256];
  349. DWORD cch_data1 = sizeof(data1);
  350. RegQueryValueExW(hsubkey, L"Path", 0, &valueType, (LPBYTE)data1,
  351. &cch_data1);
  352. DWORD data2 = 0;
  353. DWORD cch_data2 = sizeof(data2);
  354. RegQueryValueExW(hsubkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  355. &cch_data2);
  356. DWORD data3 = 0;
  357. DWORD cch_data3 = sizeof(data3);
  358. RegQueryValueExW(hsubkey, L"StorageFormat", 0, &valueType,
  359. (LPBYTE)&data3, &cch_data3);
  360. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  361. cmSystemTools::ConvertToUnixSlashes(s2);
  362. if (s2 == s1) {
  363. macrosRegistered = true;
  364. }
  365. std::string fullname = cmsys::Encoding::ToNarrow(data1);
  366. std::string filename;
  367. std::string filepath;
  368. std::string filepathname;
  369. std::string filepathpath;
  370. if (cmSystemTools::FileExists(fullname)) {
  371. filename = cmSystemTools::GetFilenameName(fullname);
  372. filepath = cmSystemTools::GetFilenamePath(fullname);
  373. filepathname = cmSystemTools::GetFilenameName(filepath);
  374. filepathpath = cmSystemTools::GetFilenamePath(filepath);
  375. }
  376. // std::cout << keyname << "\\" << subkeyname << ":" << std::endl;
  377. // std::cout << " Path: " << data1 << std::endl;
  378. // std::cout << " Security: " << data2 << std::endl;
  379. // std::cout << " StorageFormat: " << data3 << std::endl;
  380. // std::cout << " filename: " << filename << std::endl;
  381. // std::cout << " filepath: " << filepath << std::endl;
  382. // std::cout << " filepathname: " << filepathname << std::endl;
  383. // std::cout << " filepathpath: " << filepathpath << std::endl;
  384. // std::cout << std::endl;
  385. RegCloseKey(hsubkey);
  386. } else {
  387. std::cout << "error opening subkey: "
  388. << cmsys::Encoding::ToNarrow(subkeyname) << std::endl;
  389. std::cout << std::endl;
  390. }
  391. ++index;
  392. cch_subkeyname = cm::size(subkeyname);
  393. cch_keyclass = cm::size(keyclass);
  394. lastWriteTime.dwHighDateTime = 0;
  395. lastWriteTime.dwLowDateTime = 0;
  396. }
  397. RegCloseKey(hkey);
  398. } else {
  399. std::cout << "error opening key: " << keyname << std::endl;
  400. std::cout << std::endl;
  401. }
  402. // Pass back next available sub key name, assuming sub keys always
  403. // follow the expected naming scheme. Expected naming scheme is that
  404. // the subkeys of OtherProjects7 is 0 to n-1, so it's ok to use "n"
  405. // as the name of the next subkey.
  406. nextAvailableSubKeyName = std::to_string(index);
  407. keyname = cmStrCat(regKeyBase, "\\RecordingProject7");
  408. hkey = nullptr;
  409. result =
  410. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  411. 0, KEY_READ, &hkey);
  412. if (ERROR_SUCCESS == result) {
  413. DWORD valueType = REG_SZ;
  414. wchar_t data1[256];
  415. DWORD cch_data1 = sizeof(data1);
  416. RegQueryValueExW(hkey, L"Path", 0, &valueType, (LPBYTE)data1, &cch_data1);
  417. DWORD data2 = 0;
  418. DWORD cch_data2 = sizeof(data2);
  419. RegQueryValueExW(hkey, L"Security", 0, &valueType, (LPBYTE)&data2,
  420. &cch_data2);
  421. DWORD data3 = 0;
  422. DWORD cch_data3 = sizeof(data3);
  423. RegQueryValueExW(hkey, L"StorageFormat", 0, &valueType, (LPBYTE)&data3,
  424. &cch_data3);
  425. s2 = cmSystemTools::LowerCase(cmsys::Encoding::ToNarrow(data1));
  426. cmSystemTools::ConvertToUnixSlashes(s2);
  427. if (s2 == s1) {
  428. macrosRegistered = true;
  429. }
  430. // std::cout << keyname << ":" << std::endl;
  431. // std::cout << " Path: " << data1 << std::endl;
  432. // std::cout << " Security: " << data2 << std::endl;
  433. // std::cout << " StorageFormat: " << data3 << std::endl;
  434. // std::cout << std::endl;
  435. RegCloseKey(hkey);
  436. } else {
  437. std::cout << "error opening key: " << keyname << std::endl;
  438. std::cout << std::endl;
  439. }
  440. return macrosRegistered;
  441. }
  442. void WriteVSMacrosFileRegistryEntry(std::string const& nextAvailableSubKeyName,
  443. std::string const& macrosFile,
  444. std::string const& regKeyBase)
  445. {
  446. std::string keyname = cmStrCat(regKeyBase, "\\OtherProjects7");
  447. HKEY hkey = nullptr;
  448. LONG result =
  449. RegOpenKeyExW(HKEY_CURRENT_USER, cmsys::Encoding::ToWide(keyname).c_str(),
  450. 0, KEY_READ | KEY_WRITE, &hkey);
  451. if (ERROR_SUCCESS == result) {
  452. // Create the subkey and set the values of interest:
  453. HKEY hsubkey = nullptr;
  454. wchar_t lpClass[] = L"";
  455. result = RegCreateKeyExW(
  456. hkey, cmsys::Encoding::ToWide(nextAvailableSubKeyName).c_str(), 0,
  457. lpClass, 0, KEY_READ | KEY_WRITE, 0, &hsubkey, 0);
  458. if (ERROR_SUCCESS == result) {
  459. DWORD dw = 0;
  460. std::string s(macrosFile);
  461. std::replace(s.begin(), s.end(), '/', '\\');
  462. std::wstring ws = cmsys::Encoding::ToWide(s);
  463. result =
  464. RegSetValueExW(hsubkey, L"Path", 0, REG_SZ, (LPBYTE)ws.c_str(),
  465. static_cast<DWORD>(ws.size() + 1) * sizeof(wchar_t));
  466. if (ERROR_SUCCESS != result) {
  467. std::cout << "error result 1: " << result << std::endl;
  468. std::cout << std::endl;
  469. }
  470. // Security value is always "1" for sample macros files (seems to be "2"
  471. // if you put the file somewhere outside the standard VSMacros folder)
  472. dw = 1;
  473. result = RegSetValueExW(hsubkey, L"Security", 0, REG_DWORD, (LPBYTE)&dw,
  474. sizeof(DWORD));
  475. if (ERROR_SUCCESS != result) {
  476. std::cout << "error result 2: " << result << std::endl;
  477. std::cout << std::endl;
  478. }
  479. // StorageFormat value is always "0" for sample macros files
  480. dw = 0;
  481. result = RegSetValueExW(hsubkey, L"StorageFormat", 0, REG_DWORD,
  482. (LPBYTE)&dw, sizeof(DWORD));
  483. if (ERROR_SUCCESS != result) {
  484. std::cout << "error result 3: " << result << std::endl;
  485. std::cout << std::endl;
  486. }
  487. RegCloseKey(hsubkey);
  488. } else {
  489. std::cout << "error creating subkey: " << nextAvailableSubKeyName
  490. << std::endl;
  491. std::cout << std::endl;
  492. }
  493. RegCloseKey(hkey);
  494. } else {
  495. std::cout << "error opening key: " << keyname << std::endl;
  496. std::cout << std::endl;
  497. }
  498. }
  499. void RegisterVisualStudioMacros(std::string const& macrosFile,
  500. std::string const& regKeyBase)
  501. {
  502. bool macrosRegistered;
  503. std::string nextAvailableSubKeyName;
  504. macrosRegistered = IsVisualStudioMacrosFileRegistered(
  505. macrosFile, regKeyBase, nextAvailableSubKeyName);
  506. if (!macrosRegistered) {
  507. int count =
  508. cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances("ALL");
  509. // Only register the macros file if there are *no* instances of Visual
  510. // Studio running. If we register it while one is running, first, it has
  511. // no effect on the running instance; second, and worse, Visual Studio
  512. // removes our newly added registration entry when it quits. Instead,
  513. // emit a warning asking the user to exit all running Visual Studio
  514. // instances...
  515. //
  516. if (0 != count) {
  517. std::ostringstream oss;
  518. oss << "Could not register CMake's Visual Studio macros file '"
  519. << CMAKE_VSMACROS_FILENAME "' while Visual Studio is running."
  520. << " Please exit all running instances of Visual Studio before"
  521. << " continuing." << std::endl
  522. << std::endl
  523. << "CMake needs to register Visual Studio macros when its macros"
  524. << " file is updated or when it detects that its current macros file"
  525. << " is no longer registered with Visual Studio." << std::endl;
  526. cmSystemTools::Message(oss.str(), "Warning");
  527. // Count them again now that the warning is over. In the case of a GUI
  528. // warning, the user may have gone to close Visual Studio and then come
  529. // back to the CMake GUI and clicked ok on the above warning. If so,
  530. // then register the macros *now* if the count is *now* 0...
  531. //
  532. count = cmCallVisualStudioMacro::GetNumberOfRunningVisualStudioInstances(
  533. "ALL");
  534. // Also re-get the nextAvailableSubKeyName in case Visual Studio
  535. // wrote out new registered macros information as it was exiting:
  536. //
  537. if (0 == count) {
  538. IsVisualStudioMacrosFileRegistered(macrosFile, regKeyBase,
  539. nextAvailableSubKeyName);
  540. }
  541. }
  542. // Do another if check - 'count' may have changed inside the above if:
  543. //
  544. if (0 == count) {
  545. WriteVSMacrosFileRegistryEntry(nextAvailableSubKeyName, macrosFile,
  546. regKeyBase);
  547. }
  548. }
  549. }
  550. bool cmGlobalVisualStudioGenerator::TargetIsFortranOnly(
  551. cmGeneratorTarget const* gt) const
  552. {
  553. // If there's only one source language, Fortran has to be used
  554. // in order for the sources to compile.
  555. std::set<std::string> languages = gt->GetAllConfigCompileLanguages();
  556. // Consider an explicit linker language property, but *not* the
  557. // computed linker language that may depend on linked targets.
  558. // This allows the project to control the language choice in
  559. // a target with none of its own sources, e.g. when also using
  560. // object libraries.
  561. cmValue linkLang = gt->GetProperty("LINKER_LANGUAGE");
  562. if (cmNonempty(linkLang)) {
  563. languages.insert(*linkLang);
  564. }
  565. // Intel Fortran .vfproj files do support the resource compiler.
  566. languages.erase("RC");
  567. return languages.size() == 1 && *languages.begin() == "Fortran"_s;
  568. }
  569. bool cmGlobalVisualStudioGenerator::IsInSolution(
  570. cmGeneratorTarget const* gt) const
  571. {
  572. return gt->IsInBuildSystem();
  573. }
  574. bool cmGlobalVisualStudioGenerator::IsDepInSolution(
  575. std::string const& targetName) const
  576. {
  577. return !targetName.empty();
  578. }
  579. bool cmGlobalVisualStudioGenerator::TargetCompare::operator()(
  580. cmGeneratorTarget const* l, cmGeneratorTarget const* r) const
  581. {
  582. // Make sure a given named target is ordered first,
  583. // e.g. to set ALL_BUILD as the default active project.
  584. // When the empty string is named this is a no-op.
  585. if (r->GetName() == this->First) {
  586. return false;
  587. }
  588. if (l->GetName() == this->First) {
  589. return true;
  590. }
  591. return l->GetName() < r->GetName();
  592. }
  593. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  594. TargetDependSet const& targets, std::string const& first)
  595. : derived(TargetCompare(first))
  596. {
  597. this->insert(targets.begin(), targets.end());
  598. }
  599. cmGlobalVisualStudioGenerator::OrderedTargetDependSet::OrderedTargetDependSet(
  600. TargetSet const& targets, std::string const& first)
  601. : derived(TargetCompare(first))
  602. {
  603. for (cmGeneratorTarget const* it : targets) {
  604. this->insert(it);
  605. }
  606. }
  607. std::string cmGlobalVisualStudioGenerator::ExpandCFGIntDir(
  608. std::string const& str, std::string const& config) const
  609. {
  610. std::string replace = GetCMakeCFGIntDir();
  611. std::string tmp = str;
  612. for (std::string::size_type i = tmp.find(replace); i != std::string::npos;
  613. i = tmp.find(replace, i)) {
  614. tmp.replace(i, replace.size(), config);
  615. i += config.size();
  616. }
  617. return tmp;
  618. }
  619. void cmGlobalVisualStudioGenerator::AddSymbolExportCommand(
  620. cmGeneratorTarget* gt, std::vector<cmCustomCommand>& commands,
  621. std::string const& configName)
  622. {
  623. cmGeneratorTarget::ModuleDefinitionInfo const* mdi =
  624. gt->GetModuleDefinitionInfo(configName);
  625. if (!mdi || !mdi->DefFileGenerated) {
  626. return;
  627. }
  628. std::vector<std::string> outputs;
  629. outputs.push_back(mdi->DefFile);
  630. std::vector<std::string> empty;
  631. std::vector<cmSourceFile const*> objectSources;
  632. gt->GetObjectSources(objectSources, configName);
  633. std::map<cmSourceFile const*, cmObjectLocations> mapping;
  634. for (cmSourceFile const* it : objectSources) {
  635. mapping[it];
  636. }
  637. gt->LocalGenerator->ComputeObjectFilenames(mapping, configName, gt);
  638. std::string obj_dir = gt->ObjectDirectory;
  639. std::string cmakeCommand = cmSystemTools::GetCMakeCommand();
  640. std::string obj_dir_expanded = obj_dir;
  641. cmSystemTools::ReplaceString(obj_dir_expanded, this->GetCMakeCFGIntDir(),
  642. configName.c_str());
  643. cmSystemTools::MakeDirectory(obj_dir_expanded);
  644. std::string const objs_file = cmStrCat(obj_dir_expanded, "/objects.txt");
  645. cmGeneratedFileStream fout(objs_file.c_str());
  646. if (!fout) {
  647. cmSystemTools::Error(cmStrCat("could not open ", objs_file));
  648. return;
  649. }
  650. auto const useShortPaths = this->UseShortObjectNames()
  651. ? cmObjectLocations::UseShortPath::Yes
  652. : cmObjectLocations::UseShortPath::No;
  653. if (mdi->WindowsExportAllSymbols) {
  654. std::vector<std::string> objs;
  655. for (cmSourceFile const* it : objectSources) {
  656. // Find the object file name corresponding to this source file.
  657. // It must exist because we populated the mapping just above.
  658. auto const& locs = mapping[it];
  659. std::string const& v = locs.GetPath(useShortPaths);
  660. assert(!v.empty());
  661. std::string objFile = cmStrCat(obj_dir, v);
  662. objs.push_back(objFile);
  663. }
  664. std::vector<cmSourceFile const*> externalObjectSources;
  665. gt->GetExternalObjects(externalObjectSources, configName);
  666. for (cmSourceFile const* it : externalObjectSources) {
  667. objs.push_back(it->GetFullPath());
  668. }
  669. for (std::string const& it : objs) {
  670. std::string objFile = it;
  671. // replace $(ConfigurationName) in the object names
  672. cmSystemTools::ReplaceString(objFile, this->GetCMakeCFGIntDir(),
  673. configName);
  674. if (cmHasLiteralSuffix(objFile, ".obj")) {
  675. fout << objFile << "\n";
  676. }
  677. }
  678. }
  679. for (cmSourceFile const* i : mdi->Sources) {
  680. fout << i->GetFullPath() << "\n";
  681. }
  682. cmCustomCommandLines commandLines = cmMakeSingleCommandLine(
  683. { cmakeCommand, "-E", "__create_def", mdi->DefFile, objs_file });
  684. cmCustomCommand command;
  685. command.SetOutputs(outputs);
  686. command.SetCommandLines(commandLines);
  687. command.SetComment("Auto build dll exports");
  688. command.SetBacktrace(gt->Target->GetMakefile()->GetBacktrace());
  689. command.SetWorkingDirectory(".");
  690. command.SetStdPipesUTF8(true);
  691. commands.push_back(std::move(command));
  692. }
  693. static bool OpenSolution(std::string const& sln)
  694. {
  695. HRESULT comInitialized =
  696. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
  697. if (FAILED(comInitialized)) {
  698. return false;
  699. }
  700. HINSTANCE hi = ShellExecuteA(nullptr, "open", sln.c_str(), nullptr, nullptr,
  701. SW_SHOWNORMAL);
  702. CoUninitialize();
  703. return reinterpret_cast<intptr_t>(hi) > 32;
  704. }
  705. bool cmGlobalVisualStudioGenerator::Open(std::string const& bindir,
  706. std::string const& projectName,
  707. bool dryRun)
  708. {
  709. std::string sln = cmStrCat(bindir, '/', projectName, ".sln");
  710. if (dryRun) {
  711. return cmSystemTools::FileExists(sln, true);
  712. }
  713. sln = cmSystemTools::ConvertToOutputPath(sln);
  714. return std::async(std::launch::async, OpenSolution, sln).get();
  715. }