cmGlobalVisualStudioGenerator.cxx 27 KB

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