cmGlobalVisualStudio8Generator.cxx 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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 "cmGlobalVisualStudio8Generator.h"
  4. #include <algorithm>
  5. #include <functional>
  6. #include <ostream>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include <cmext/algorithm>
  10. #include <cmext/memory>
  11. #include "cmCustomCommand.h"
  12. #include "cmCustomCommandLines.h"
  13. #include "cmCustomCommandTypes.h"
  14. #include "cmGeneratedFileStream.h"
  15. #include "cmGeneratorExpression.h"
  16. #include "cmGeneratorTarget.h"
  17. #include "cmGlobalGenerator.h"
  18. #include "cmGlobalVisualStudio7Generator.h"
  19. #include "cmGlobalVisualStudioGenerator.h"
  20. #include "cmList.h"
  21. #include "cmListFileCache.h"
  22. #include "cmLocalGenerator.h"
  23. #include "cmLocalVisualStudio7Generator.h"
  24. #include "cmMakefile.h"
  25. #include "cmPolicies.h"
  26. #include "cmSourceFile.h"
  27. #include "cmStateTypes.h"
  28. #include "cmStringAlgorithms.h"
  29. #include "cmSystemTools.h"
  30. #include "cmTarget.h"
  31. #include "cmTargetDepend.h"
  32. #include "cmValue.h"
  33. #include "cmVisualStudioGeneratorOptions.h"
  34. #include "cmake.h"
  35. struct cmIDEFlagTable;
  36. cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator(
  37. cmake* cm, const std::string& name)
  38. : cmGlobalVisualStudio71Generator(cm)
  39. {
  40. this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
  41. this->Name = name;
  42. this->ExtraFlagTable =
  43. cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8();
  44. }
  45. std::string cmGlobalVisualStudio8Generator::FindDevEnvCommand()
  46. {
  47. // First look for VCExpress.
  48. std::string vsxcmd;
  49. std::string vsxkey =
  50. cmStrCat(R"(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VCExpress\)",
  51. this->GetIDEVersion(), ";InstallDir");
  52. if (cmSystemTools::ReadRegistryValue(vsxkey, vsxcmd,
  53. cmSystemTools::KeyWOW64_32)) {
  54. cmSystemTools::ConvertToUnixSlashes(vsxcmd);
  55. vsxcmd += "/VCExpress.exe";
  56. return vsxcmd;
  57. }
  58. // Now look for devenv.
  59. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  60. }
  61. void cmGlobalVisualStudio8Generator::EnableLanguage(
  62. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  63. {
  64. for (std::string const& it : lang) {
  65. if (it == "ASM_MASM") {
  66. this->MasmEnabled = true;
  67. }
  68. }
  69. this->AddPlatformDefinitions(mf);
  70. cmGlobalVisualStudio7Generator::EnableLanguage(lang, mf, optional);
  71. }
  72. void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
  73. {
  74. if (this->TargetsWindowsCE()) {
  75. mf->AddDefinition("CMAKE_VS_WINCE_VERSION", this->WindowsCEVersion);
  76. }
  77. }
  78. bool cmGlobalVisualStudio8Generator::SetGeneratorPlatform(std::string const& p,
  79. cmMakefile* mf)
  80. {
  81. if (!this->ParseGeneratorPlatform(p, mf)) {
  82. return false;
  83. }
  84. // FIXME: Add CMAKE_GENERATOR_PLATFORM field to set the framework.
  85. // For now, just report the generator's default, if any.
  86. if (cm::optional<std::string> const& targetFrameworkVersion =
  87. this->GetTargetFrameworkVersion()) {
  88. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_VERSION",
  89. *targetFrameworkVersion);
  90. }
  91. if (cm::optional<std::string> const& targetFrameworkIdentifier =
  92. this->GetTargetFrameworkIdentifier()) {
  93. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_IDENTIFIER",
  94. *targetFrameworkIdentifier);
  95. }
  96. if (cm::optional<std::string> const& targetFrameworkTargetsVersion =
  97. this->GetTargetFrameworkTargetsVersion()) {
  98. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_TARGETS_VERSION",
  99. *targetFrameworkTargetsVersion);
  100. }
  101. // The generator name does not contain the platform name, and so supports
  102. // explicit platform specification. We handled that above, so pass an
  103. // empty platform name to our base class implementation so it does not error.
  104. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform("", mf);
  105. }
  106. bool cmGlobalVisualStudio8Generator::ParseGeneratorPlatform(
  107. std::string const& p, cmMakefile* mf)
  108. {
  109. this->GeneratorPlatform.clear();
  110. std::vector<std::string> const fields =
  111. cmTokenize(p, ',', cmTokenizerMode::New);
  112. if (fields.empty()) {
  113. return true;
  114. }
  115. auto fi = fields.begin();
  116. // The first field may be the VS platform.
  117. if (fi->find('=') == fi->npos) {
  118. this->GeneratorPlatform = *fi;
  119. ++fi;
  120. }
  121. std::set<std::string> handled;
  122. // The rest of the fields must be key=value pairs.
  123. for (; fi != fields.end(); ++fi) {
  124. std::string::size_type pos = fi->find('=');
  125. if (pos == fi->npos) {
  126. std::ostringstream e;
  127. /* clang-format off */
  128. e <<
  129. "Generator\n"
  130. " " << this->GetName() << "\n"
  131. "given platform specification\n"
  132. " " << p << "\n"
  133. "that contains a field after the first ',' with no '='."
  134. ;
  135. /* clang-format on */
  136. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  137. return false;
  138. }
  139. std::string const key = fi->substr(0, pos);
  140. std::string const value = fi->substr(pos + 1);
  141. if (!handled.insert(key).second) {
  142. std::ostringstream e;
  143. /* clang-format off */
  144. e <<
  145. "Generator\n"
  146. " " << this->GetName() << "\n"
  147. "given platform specification\n"
  148. " " << p << "\n"
  149. "that contains duplicate field key '" << key << "'."
  150. ;
  151. /* clang-format on */
  152. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  153. return false;
  154. }
  155. if (!this->ProcessGeneratorPlatformField(key, value)) {
  156. std::ostringstream e;
  157. /* clang-format off */
  158. e <<
  159. "Generator\n"
  160. " " << this->GetName() << "\n"
  161. "given platform specification\n"
  162. " " << p << "\n"
  163. "that contains invalid field '" << *fi << "'."
  164. ;
  165. /* clang-format on */
  166. mf->IssueMessage(MessageType::FATAL_ERROR, e.str());
  167. return false;
  168. }
  169. }
  170. return true;
  171. }
  172. bool cmGlobalVisualStudio8Generator::ProcessGeneratorPlatformField(
  173. std::string const& key, std::string const& value)
  174. {
  175. static_cast<void>(key);
  176. static_cast<void>(value);
  177. return false;
  178. }
  179. cm::optional<std::string> const&
  180. cmGlobalVisualStudio8Generator::GetTargetFrameworkVersion() const
  181. {
  182. return this->DefaultTargetFrameworkVersion;
  183. }
  184. cm::optional<std::string> const&
  185. cmGlobalVisualStudio8Generator::GetTargetFrameworkIdentifier() const
  186. {
  187. return this->DefaultTargetFrameworkIdentifier;
  188. }
  189. cm::optional<std::string> const&
  190. cmGlobalVisualStudio8Generator::GetTargetFrameworkTargetsVersion() const
  191. {
  192. return this->DefaultTargetFrameworkTargetsVersion;
  193. }
  194. std::string cmGlobalVisualStudio8Generator::GetGenerateStampList()
  195. {
  196. return "generate.stamp.list";
  197. }
  198. bool cmGlobalVisualStudio8Generator::UseFolderProperty() const
  199. {
  200. // NOLINTNEXTLINE(bugprone-parent-virtual-call)
  201. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  202. }
  203. bool cmGlobalVisualStudio8Generator::AddCheckTarget()
  204. {
  205. // Add a special target on which all other targets depend that
  206. // checks the build system and optionally re-runs CMake.
  207. // Skip the target if no regeneration is to be done.
  208. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  209. return false;
  210. }
  211. std::vector<std::unique_ptr<cmLocalGenerator>> const& generators =
  212. this->LocalGenerators;
  213. auto& lg =
  214. cm::static_reference_cast<cmLocalVisualStudio7Generator>(generators[0]);
  215. auto cc = cm::make_unique<cmCustomCommand>();
  216. cmTarget* tgt = lg.AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  217. std::move(cc));
  218. // Collect the input files used to generate all targets in this
  219. // project.
  220. std::vector<std::string> listFiles;
  221. for (const auto& gen : generators) {
  222. cm::append(listFiles, gen->GetMakefile()->GetListFiles());
  223. }
  224. // Sort the list of input files and remove duplicates.
  225. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  226. auto new_end = std::unique(listFiles.begin(), listFiles.end());
  227. listFiles.erase(new_end, listFiles.end());
  228. auto ptr = cm::make_unique<cmGeneratorTarget>(tgt, &lg);
  229. auto* gt = ptr.get();
  230. lg.AddGeneratorTarget(std::move(ptr));
  231. // Organize in the "predefined targets" folder:
  232. //
  233. if (this->UseFolderProperty()) {
  234. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  235. }
  236. // Create a list of all stamp files for this project.
  237. std::vector<std::string> stamps;
  238. std::string stampList = cmStrCat(
  239. "CMakeFiles/", cmGlobalVisualStudio8Generator::GetGenerateStampList());
  240. {
  241. std::string stampListFile =
  242. cmStrCat(generators[0]->GetMakefile()->GetCurrentBinaryDirectory(), '/',
  243. stampList);
  244. std::string stampFile;
  245. cmGeneratedFileStream fout(stampListFile);
  246. for (const auto& gi : generators) {
  247. stampFile = cmStrCat(gi->GetMakefile()->GetCurrentBinaryDirectory(),
  248. "/CMakeFiles/generate.stamp");
  249. fout << stampFile << '\n';
  250. stamps.push_back(stampFile);
  251. }
  252. }
  253. // Add a custom rule to re-run CMake if any input files changed.
  254. {
  255. // The custom rule runs cmake so set UTF-8 pipes.
  256. bool stdPipesUTF8 = true;
  257. // Add a custom prebuild target to run the VerifyGlobs script.
  258. cmake* cm = this->GetCMakeInstance();
  259. if (cm->DoWriteGlobVerifyTarget()) {
  260. cmCustomCommandLines verifyCommandLines = cmMakeSingleCommandLine(
  261. { cmSystemTools::GetCMakeCommand(), "-P", cm->GetGlobVerifyScript() });
  262. std::vector<std::string> byproducts;
  263. byproducts.push_back(cm->GetGlobVerifyStamp());
  264. cc = cm::make_unique<cmCustomCommand>();
  265. cc->SetByproducts(byproducts);
  266. cc->SetCommandLines(verifyCommandLines);
  267. cc->SetComment("Checking File Globs");
  268. cc->SetStdPipesUTF8(stdPipesUTF8);
  269. lg.AddCustomCommandToTarget(CMAKE_CHECK_BUILD_SYSTEM_TARGET,
  270. cmCustomCommandType::PRE_BUILD,
  271. std::move(cc));
  272. // Ensure ZERO_CHECK always runs in Visual Studio using MSBuild,
  273. // otherwise the prebuild command will not be run.
  274. tgt->SetProperty("VS_GLOBAL_DisableFastUpToDateCheck", "true");
  275. listFiles.push_back(cm->GetGlobVerifyStamp());
  276. }
  277. // Create a rule to re-run CMake.
  278. std::string argS = cmStrCat("-S", lg.GetSourceDirectory());
  279. std::string argB = cmStrCat("-B", lg.GetBinaryDirectory());
  280. std::string const sln =
  281. cmStrCat(lg.GetBinaryDirectory(), '/', lg.GetProjectName(), ".sln");
  282. cmCustomCommandLines commandLines = cmMakeSingleCommandLine(
  283. { cmSystemTools::GetCMakeCommand(), argS, argB, "--check-stamp-list",
  284. stampList, "--vs-solution-file", sln });
  285. if (cm->GetIgnoreCompileWarningAsError()) {
  286. commandLines[0].emplace_back("--compile-no-warning-as-error");
  287. }
  288. if (cm->GetIgnoreLinkWarningAsError()) {
  289. commandLines[0].emplace_back("--link-no-warning-as-error");
  290. }
  291. // Add the rule. Note that we cannot use the CMakeLists.txt
  292. // file as the main dependency because it would get
  293. // overwritten by the CreateVCProjBuildRule.
  294. // (this could be avoided with per-target source files)
  295. cc = cm::make_unique<cmCustomCommand>();
  296. cc->SetOutputs(stamps);
  297. cc->SetDepends(listFiles);
  298. cc->SetCommandLines(commandLines);
  299. cc->SetComment("Checking Build System");
  300. cc->SetEscapeOldStyle(false);
  301. cc->SetStdPipesUTF8(stdPipesUTF8);
  302. if (cmSourceFile* file =
  303. lg.AddCustomCommandToOutput(std::move(cc), true)) {
  304. gt->AddSource(file->ResolveFullPath());
  305. } else {
  306. cmSystemTools::Error(cmStrCat("Error adding rule for ", stamps[0]));
  307. }
  308. }
  309. return true;
  310. }
  311. void cmGlobalVisualStudio8Generator::AddExtraIDETargets()
  312. {
  313. cmGlobalVisualStudio7Generator::AddExtraIDETargets();
  314. if (this->AddCheckTarget()) {
  315. for (auto& LocalGenerator : this->LocalGenerators) {
  316. const auto& tgts = LocalGenerator->GetGeneratorTargets();
  317. // All targets depend on the build-system check target.
  318. for (const auto& ti : tgts) {
  319. if (ti->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  320. ti->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false);
  321. }
  322. }
  323. }
  324. }
  325. }
  326. void cmGlobalVisualStudio8Generator::WriteSolutionConfigurations(
  327. std::ostream& fout, std::vector<std::string> const& configs)
  328. {
  329. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  330. for (std::string const& i : configs) {
  331. fout << "\t\t" << i << '|' << this->GetPlatformName() << " = " << i << '|'
  332. << this->GetPlatformName() << '\n';
  333. }
  334. fout << "\tEndGlobalSection\n";
  335. }
  336. void cmGlobalVisualStudio8Generator::WriteProjectConfigurations(
  337. std::ostream& fout, const std::string& name, cmGeneratorTarget const& target,
  338. std::vector<std::string> const& configs,
  339. const std::set<std::string>& configsPartOfDefaultBuild,
  340. std::string const& platformMapping)
  341. {
  342. std::string guid = this->GetGUID(name);
  343. for (std::string const& i : configs) {
  344. cmList mapConfig;
  345. const char* dstConfig = i.c_str();
  346. if (target.GetProperty("EXTERNAL_MSPROJECT")) {
  347. if (cmValue m = target.GetProperty(
  348. cmStrCat("MAP_IMPORTED_CONFIG_", cmSystemTools::UpperCase(i)))) {
  349. mapConfig.assign(*m);
  350. if (!mapConfig.empty()) {
  351. dstConfig = mapConfig[0].c_str();
  352. }
  353. }
  354. }
  355. fout << "\t\t{" << guid << "}." << i << '|' << this->GetPlatformName()
  356. << ".ActiveCfg = " << dstConfig << '|'
  357. << (!platformMapping.empty() ? platformMapping
  358. : this->GetPlatformName())
  359. << '\n';
  360. auto ci = configsPartOfDefaultBuild.find(i);
  361. if (!(ci == configsPartOfDefaultBuild.end())) {
  362. fout << "\t\t{" << guid << "}." << i << '|' << this->GetPlatformName()
  363. << ".Build.0 = " << dstConfig << '|'
  364. << (!platformMapping.empty() ? platformMapping
  365. : this->GetPlatformName())
  366. << '\n';
  367. }
  368. if (this->NeedsDeploy(target, dstConfig)) {
  369. fout << "\t\t{" << guid << "}." << i << '|' << this->GetPlatformName()
  370. << ".Deploy.0 = " << dstConfig << '|'
  371. << (!platformMapping.empty() ? platformMapping
  372. : this->GetPlatformName())
  373. << '\n';
  374. }
  375. }
  376. }
  377. bool cmGlobalVisualStudio8Generator::NeedsDeploy(
  378. cmGeneratorTarget const& target, const char* config) const
  379. {
  380. cmStateEnums::TargetType const type = target.GetType();
  381. if (type != cmStateEnums::EXECUTABLE &&
  382. type != cmStateEnums::SHARED_LIBRARY) {
  383. // deployment only valid on executables and shared libraries.
  384. return false;
  385. }
  386. if (cmValue prop = target.GetProperty("VS_SOLUTION_DEPLOY")) {
  387. // If set, it dictates behavior
  388. return cmIsOn(
  389. cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator, config));
  390. }
  391. // To be deprecated, disable deployment even if target supports it.
  392. if (cmValue prop = target.GetProperty("VS_NO_SOLUTION_DEPLOY")) {
  393. if (cmIsOn(cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator,
  394. config))) {
  395. // If true, always disable deployment
  396. return false;
  397. }
  398. }
  399. // Legacy behavior, enabled deployment based on 'hard-coded' target
  400. // platforms.
  401. return this->TargetSystemSupportsDeployment();
  402. }
  403. bool cmGlobalVisualStudio8Generator::TargetSystemSupportsDeployment() const
  404. {
  405. return this->TargetsWindowsCE();
  406. }
  407. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  408. {
  409. // Skip over the cmGlobalVisualStudioGenerator implementation!
  410. // We do not need the support that VS <= 7.1 needs.
  411. // NOLINTNEXTLINE(bugprone-parent-virtual-call)
  412. return this->cmGlobalGenerator::ComputeTargetDepends();
  413. }
  414. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  415. std::ostream& fout, const std::string&, const std::string&,
  416. cmGeneratorTarget const* gt)
  417. {
  418. TargetDependSet const& unordered = this->GetTargetDirectDepends(gt);
  419. OrderedTargetDependSet depends(unordered, std::string());
  420. for (cmTargetDepend const& i : depends) {
  421. if (!this->IsInSolution(i)) {
  422. continue;
  423. }
  424. std::string guid = this->GetGUID(i->GetName());
  425. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  426. }
  427. }
  428. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  429. cmGeneratorTarget* target)
  430. {
  431. // Look for utility dependencies that magically link.
  432. auto const& utilities = target->GetUtilities();
  433. return std::any_of(
  434. utilities.begin(), utilities.end(),
  435. [target](BT<std::pair<std::string, bool>> const& ui) {
  436. if (cmGeneratorTarget* depTarget =
  437. target->GetLocalGenerator()->FindGeneratorTargetToUse(
  438. ui.Value.first)) {
  439. if (depTarget->IsInBuildSystem() &&
  440. depTarget->GetProperty("EXTERNAL_MSPROJECT")) {
  441. // This utility dependency names an external .vcproj target.
  442. // We use LinkLibraryDependencies="true" to link to it without
  443. // predicting the .lib file location or name.
  444. return true;
  445. }
  446. }
  447. return false;
  448. });
  449. }
  450. static cmVS7FlagTable cmVS8ExtraFlagTable[] = {
  451. { "CallingConvention", "Gd", "cdecl", "0", 0 },
  452. { "CallingConvention", "Gr", "fastcall", "1", 0 },
  453. { "CallingConvention", "Gz", "stdcall", "2", 0 },
  454. { "Detect64BitPortabilityProblems", "Wp64",
  455. "Detect 64Bit Portability Problems", "true", 0 },
  456. { "ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  457. { "ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  458. // Precompiled header and related options. Note that the
  459. // UsePrecompiledHeader entries are marked as "Continue" so that the
  460. // corresponding PrecompiledHeaderThrough entry can be found.
  461. { "UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  462. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  463. { "PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  464. cmVS7FlagTable::UserValueRequired },
  465. { "UsePrecompiledHeader", "Y-", "Don't use precompiled header", "0", 0 },
  466. // There is no YX option in the VS8 IDE.
  467. // Exception handling mode. If no entries match, it will be FALSE.
  468. { "ExceptionHandling", "GX", "enable c++ exceptions", "1", 0 },
  469. { "ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0 },
  470. { "ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0 },
  471. { "EnablePREfast", "analyze", "", "true", 0 },
  472. { "EnablePREfast", "analyze-", "", "false", 0 },
  473. // Language options
  474. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t", "wchar_t is a built-in type",
  475. "true", 0 },
  476. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  477. "wchar_t is not a built-in type", "false", 0 },
  478. { "", "", "", "", 0 }
  479. };
  480. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  481. {
  482. return cmVS8ExtraFlagTable;
  483. }