cmGlobalVisualStudio8Generator.cxx 15 KB

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