cmGlobalVisualStudio8Generator.cxx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 "cmListFileCache.h"
  21. #include "cmLocalGenerator.h"
  22. #include "cmLocalVisualStudio7Generator.h"
  23. #include "cmMakefile.h"
  24. #include "cmPolicies.h"
  25. #include "cmSourceFile.h"
  26. #include "cmStateTypes.h"
  27. #include "cmStringAlgorithms.h"
  28. #include "cmSystemTools.h"
  29. #include "cmTarget.h"
  30. #include "cmTargetDepend.h"
  31. #include "cmValue.h"
  32. #include "cmVisualStudioGeneratorOptions.h"
  33. #include "cmake.h"
  34. struct cmIDEFlagTable;
  35. cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator(
  36. cmake* cm, const std::string& name,
  37. std::string const& platformInGeneratorName)
  38. : cmGlobalVisualStudio71Generator(cm, platformInGeneratorName)
  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("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->PlatformInGeneratorName) {
  82. // This is an old-style generator name that contains the platform name.
  83. // No explicit platform specification is supported, so pass it through
  84. // to our base class implementation, which errors on non-empty platforms.
  85. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform(p, mf);
  86. }
  87. this->GeneratorPlatform = p;
  88. // FIXME: Add CMAKE_GENERATOR_PLATFORM field to set the framework.
  89. // For now, just report the generator's default, if any.
  90. if (cm::optional<std::string> const& targetFrameworkVersion =
  91. this->GetTargetFrameworkVersion()) {
  92. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_VERSION",
  93. *targetFrameworkVersion);
  94. }
  95. if (cm::optional<std::string> const& targetFrameworkIdentifier =
  96. this->GetTargetFrameworkIdentifier()) {
  97. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_IDENTIFIER",
  98. *targetFrameworkIdentifier);
  99. }
  100. if (cm::optional<std::string> const& targetFrameworkTargetsVersion =
  101. this->GetTargetFrameworkTargetsVersion()) {
  102. mf->AddDefinition("CMAKE_VS_TARGET_FRAMEWORK_TARGETS_VERSION",
  103. *targetFrameworkTargetsVersion);
  104. }
  105. // The generator name does not contain the platform name, and so supports
  106. // explicit platform specification. We handled that above, so pass an
  107. // empty platform name to our base class implementation so it does not error.
  108. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform("", mf);
  109. }
  110. cm::optional<std::string> const&
  111. cmGlobalVisualStudio8Generator::GetTargetFrameworkVersion() const
  112. {
  113. return this->DefaultTargetFrameworkVersion;
  114. }
  115. cm::optional<std::string> const&
  116. cmGlobalVisualStudio8Generator::GetTargetFrameworkIdentifier() const
  117. {
  118. return this->DefaultTargetFrameworkIdentifier;
  119. }
  120. cm::optional<std::string> const&
  121. cmGlobalVisualStudio8Generator::GetTargetFrameworkTargetsVersion() const
  122. {
  123. return this->DefaultTargetFrameworkTargetsVersion;
  124. }
  125. std::string cmGlobalVisualStudio8Generator::GetGenerateStampList()
  126. {
  127. return "generate.stamp.list";
  128. }
  129. void cmGlobalVisualStudio8Generator::Configure()
  130. {
  131. this->cmGlobalVisualStudio7Generator::Configure();
  132. }
  133. bool cmGlobalVisualStudio8Generator::UseFolderProperty() const
  134. {
  135. // NOLINTNEXTLINE(bugprone-parent-virtual-call)
  136. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  137. }
  138. bool cmGlobalVisualStudio8Generator::AddCheckTarget()
  139. {
  140. // Add a special target on which all other targets depend that
  141. // checks the build system and optionally re-runs CMake.
  142. // Skip the target if no regeneration is to be done.
  143. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  144. return false;
  145. }
  146. std::vector<std::unique_ptr<cmLocalGenerator>> const& generators =
  147. this->LocalGenerators;
  148. auto& lg =
  149. cm::static_reference_cast<cmLocalVisualStudio7Generator>(generators[0]);
  150. auto cc = cm::make_unique<cmCustomCommand>();
  151. cc->SetCMP0116Status(cmPolicies::NEW);
  152. cmTarget* tgt = lg.AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false,
  153. std::move(cc));
  154. auto ptr = cm::make_unique<cmGeneratorTarget>(tgt, &lg);
  155. auto gt = ptr.get();
  156. lg.AddGeneratorTarget(std::move(ptr));
  157. // Organize in the "predefined targets" folder:
  158. //
  159. if (this->UseFolderProperty()) {
  160. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  161. }
  162. // Create a list of all stamp files for this project.
  163. std::vector<std::string> stamps;
  164. std::string stampList = cmStrCat(
  165. "CMakeFiles/", cmGlobalVisualStudio8Generator::GetGenerateStampList());
  166. {
  167. std::string stampListFile =
  168. cmStrCat(generators[0]->GetMakefile()->GetCurrentBinaryDirectory(), '/',
  169. stampList);
  170. std::string stampFile;
  171. cmGeneratedFileStream fout(stampListFile);
  172. for (const auto& gi : generators) {
  173. stampFile = cmStrCat(gi->GetMakefile()->GetCurrentBinaryDirectory(),
  174. "/CMakeFiles/generate.stamp");
  175. fout << stampFile << "\n";
  176. stamps.push_back(stampFile);
  177. }
  178. }
  179. // Add a custom rule to re-run CMake if any input files changed.
  180. {
  181. // The custom rule runs cmake so set UTF-8 pipes.
  182. bool stdPipesUTF8 = true;
  183. // Collect the input files used to generate all targets in this
  184. // project.
  185. std::vector<std::string> listFiles;
  186. for (const auto& gen : generators) {
  187. cm::append(listFiles, gen->GetMakefile()->GetListFiles());
  188. }
  189. // Add a custom prebuild target to run the VerifyGlobs script.
  190. cmake* cm = this->GetCMakeInstance();
  191. if (cm->DoWriteGlobVerifyTarget()) {
  192. cmCustomCommandLines verifyCommandLines = cmMakeSingleCommandLine(
  193. { cmSystemTools::GetCMakeCommand(), "-P", cm->GetGlobVerifyScript() });
  194. std::vector<std::string> byproducts;
  195. byproducts.push_back(cm->GetGlobVerifyStamp());
  196. cc = cm::make_unique<cmCustomCommand>();
  197. cc->SetByproducts(byproducts);
  198. cc->SetCommandLines(verifyCommandLines);
  199. cc->SetComment("Checking File Globs");
  200. cc->SetCMP0116Status(cmPolicies::NEW);
  201. cc->SetStdPipesUTF8(stdPipesUTF8);
  202. lg.AddCustomCommandToTarget(CMAKE_CHECK_BUILD_SYSTEM_TARGET,
  203. cmCustomCommandType::PRE_BUILD,
  204. std::move(cc));
  205. // Ensure ZERO_CHECK always runs in Visual Studio using MSBuild,
  206. // otherwise the prebuild command will not be run.
  207. tgt->SetProperty("VS_GLOBAL_DisableFastUpToDateCheck", "true");
  208. listFiles.push_back(cm->GetGlobVerifyStamp());
  209. }
  210. // Sort the list of input files and remove duplicates.
  211. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  212. auto new_end = std::unique(listFiles.begin(), listFiles.end());
  213. listFiles.erase(new_end, listFiles.end());
  214. // Create a rule to re-run CMake.
  215. std::string argS = cmStrCat("-S", lg.GetSourceDirectory());
  216. std::string argB = cmStrCat("-B", lg.GetBinaryDirectory());
  217. std::string const sln =
  218. lg.GetBinaryDirectory() + "/" + lg.GetProjectName() + ".sln";
  219. cmCustomCommandLines commandLines = cmMakeSingleCommandLine(
  220. { cmSystemTools::GetCMakeCommand(), argS, argB, "--check-stamp-list",
  221. stampList, "--vs-solution-file", sln });
  222. // Add the rule. Note that we cannot use the CMakeLists.txt
  223. // file as the main dependency because it would get
  224. // overwritten by the CreateVCProjBuildRule.
  225. // (this could be avoided with per-target source files)
  226. cc = cm::make_unique<cmCustomCommand>();
  227. cc->SetOutputs(stamps);
  228. cc->SetDepends(listFiles);
  229. cc->SetCommandLines(commandLines);
  230. cc->SetComment("Checking Build System");
  231. cc->SetCMP0116Status(cmPolicies::NEW);
  232. cc->SetEscapeOldStyle(false);
  233. cc->SetStdPipesUTF8(stdPipesUTF8);
  234. if (cmSourceFile* file =
  235. lg.AddCustomCommandToOutput(std::move(cc), true)) {
  236. gt->AddSource(file->ResolveFullPath());
  237. } else {
  238. cmSystemTools::Error("Error adding rule for " + stamps[0]);
  239. }
  240. }
  241. return true;
  242. }
  243. void cmGlobalVisualStudio8Generator::AddExtraIDETargets()
  244. {
  245. cmGlobalVisualStudio7Generator::AddExtraIDETargets();
  246. if (this->AddCheckTarget()) {
  247. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  248. const auto& tgts = this->LocalGenerators[i]->GetGeneratorTargets();
  249. // All targets depend on the build-system check target.
  250. for (const auto& ti : tgts) {
  251. if (ti->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  252. ti->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false);
  253. }
  254. }
  255. }
  256. }
  257. }
  258. void cmGlobalVisualStudio8Generator::WriteSolutionConfigurations(
  259. std::ostream& fout, std::vector<std::string> const& configs)
  260. {
  261. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  262. for (std::string const& i : configs) {
  263. fout << "\t\t" << i << "|" << this->GetPlatformName() << " = " << i << "|"
  264. << this->GetPlatformName() << "\n";
  265. }
  266. fout << "\tEndGlobalSection\n";
  267. }
  268. void cmGlobalVisualStudio8Generator::WriteProjectConfigurations(
  269. std::ostream& fout, const std::string& name, cmGeneratorTarget const& target,
  270. std::vector<std::string> const& configs,
  271. const std::set<std::string>& configsPartOfDefaultBuild,
  272. std::string const& platformMapping)
  273. {
  274. std::string guid = this->GetGUID(name);
  275. for (std::string const& i : configs) {
  276. std::vector<std::string> mapConfig;
  277. const char* dstConfig = i.c_str();
  278. if (target.GetProperty("EXTERNAL_MSPROJECT")) {
  279. if (cmValue m = target.GetProperty("MAP_IMPORTED_CONFIG_" +
  280. cmSystemTools::UpperCase(i))) {
  281. cmExpandList(*m, mapConfig);
  282. if (!mapConfig.empty()) {
  283. dstConfig = mapConfig[0].c_str();
  284. }
  285. }
  286. }
  287. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  288. << ".ActiveCfg = " << dstConfig << "|"
  289. << (!platformMapping.empty() ? platformMapping
  290. : this->GetPlatformName())
  291. << "\n";
  292. auto ci = configsPartOfDefaultBuild.find(i);
  293. if (!(ci == configsPartOfDefaultBuild.end())) {
  294. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  295. << ".Build.0 = " << dstConfig << "|"
  296. << (!platformMapping.empty() ? platformMapping
  297. : this->GetPlatformName())
  298. << "\n";
  299. }
  300. if (this->NeedsDeploy(target, dstConfig)) {
  301. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  302. << ".Deploy.0 = " << dstConfig << "|"
  303. << (!platformMapping.empty() ? platformMapping
  304. : this->GetPlatformName())
  305. << "\n";
  306. }
  307. }
  308. }
  309. bool cmGlobalVisualStudio8Generator::NeedsDeploy(
  310. cmGeneratorTarget const& target, const char* config) const
  311. {
  312. cmStateEnums::TargetType const type = target.GetType();
  313. if (type != cmStateEnums::EXECUTABLE &&
  314. type != cmStateEnums::SHARED_LIBRARY) {
  315. // deployment only valid on executables and shared libraries.
  316. return false;
  317. }
  318. if (cmValue prop = target.GetProperty("VS_SOLUTION_DEPLOY")) {
  319. // If set, it dictates behavior
  320. return cmIsOn(
  321. cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator, config));
  322. }
  323. // To be deprecated, disable deployment even if target supports it.
  324. if (cmValue prop = target.GetProperty("VS_NO_SOLUTION_DEPLOY")) {
  325. if (cmIsOn(cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator,
  326. config))) {
  327. // If true, always disable deployment
  328. return false;
  329. }
  330. }
  331. // Legacy behavior, enabled deployment based on 'hard-coded' target
  332. // platforms.
  333. return this->TargetSystemSupportsDeployment();
  334. }
  335. bool cmGlobalVisualStudio8Generator::TargetSystemSupportsDeployment() const
  336. {
  337. return this->TargetsWindowsCE();
  338. }
  339. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  340. {
  341. // Skip over the cmGlobalVisualStudioGenerator implementation!
  342. // We do not need the support that VS <= 7.1 needs.
  343. // NOLINTNEXTLINE(bugprone-parent-virtual-call)
  344. return this->cmGlobalGenerator::ComputeTargetDepends();
  345. }
  346. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  347. std::ostream& fout, const std::string&, const std::string&,
  348. cmGeneratorTarget const* gt)
  349. {
  350. TargetDependSet const& unordered = this->GetTargetDirectDepends(gt);
  351. OrderedTargetDependSet depends(unordered, std::string());
  352. for (cmTargetDepend const& i : depends) {
  353. if (!this->IsInSolution(i)) {
  354. continue;
  355. }
  356. std::string guid = this->GetGUID(i->GetName());
  357. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  358. }
  359. }
  360. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  361. cmGeneratorTarget* target)
  362. {
  363. // Look for utility dependencies that magically link.
  364. for (BT<std::pair<std::string, bool>> const& ui : target->GetUtilities()) {
  365. if (cmGeneratorTarget* depTarget =
  366. target->GetLocalGenerator()->FindGeneratorTargetToUse(
  367. ui.Value.first)) {
  368. if (depTarget->IsInBuildSystem() &&
  369. depTarget->GetProperty("EXTERNAL_MSPROJECT")) {
  370. // This utility dependency names an external .vcproj target.
  371. // We use LinkLibraryDependencies="true" to link to it without
  372. // predicting the .lib file location or name.
  373. return true;
  374. }
  375. }
  376. }
  377. return false;
  378. }
  379. static cmVS7FlagTable cmVS8ExtraFlagTable[] = {
  380. { "CallingConvention", "Gd", "cdecl", "0", 0 },
  381. { "CallingConvention", "Gr", "fastcall", "1", 0 },
  382. { "CallingConvention", "Gz", "stdcall", "2", 0 },
  383. { "Detect64BitPortabilityProblems", "Wp64",
  384. "Detect 64Bit Portability Problems", "true", 0 },
  385. { "ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  386. { "ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  387. // Precompiled header and related options. Note that the
  388. // UsePrecompiledHeader entries are marked as "Continue" so that the
  389. // corresponding PrecompiledHeaderThrough entry can be found.
  390. { "UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  391. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  392. { "PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  393. cmVS7FlagTable::UserValueRequired },
  394. { "UsePrecompiledHeader", "Y-", "Don't use precompiled header", "0", 0 },
  395. // There is no YX option in the VS8 IDE.
  396. // Exception handling mode. If no entries match, it will be FALSE.
  397. { "ExceptionHandling", "GX", "enable c++ exceptions", "1", 0 },
  398. { "ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0 },
  399. { "ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0 },
  400. { "EnablePREfast", "analyze", "", "true", 0 },
  401. { "EnablePREfast", "analyze-", "", "false", 0 },
  402. // Language options
  403. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t", "wchar_t is a built-in type",
  404. "true", 0 },
  405. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  406. "wchar_t is not a built-in type", "false", 0 },
  407. { "", "", "", "", 0 }
  408. };
  409. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  410. {
  411. return cmVS8ExtraFlagTable;
  412. }