cmGlobalVisualStudio8Generator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <cm/memory>
  5. #include <cmext/memory>
  6. #include "cmCustomCommand.h"
  7. #include "cmCustomCommandLines.h"
  8. #include "cmDocumentationEntry.h"
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGeneratorExpression.h"
  11. #include "cmGeneratorTarget.h"
  12. #include "cmLocalVisualStudio7Generator.h"
  13. #include "cmMakefile.h"
  14. #include "cmMessageType.h"
  15. #include "cmSourceFile.h"
  16. #include "cmVisualStudioWCEPlatformParser.h"
  17. #include "cmake.h"
  18. cmGlobalVisualStudio8Generator::cmGlobalVisualStudio8Generator(
  19. cmake* cm, const std::string& name,
  20. std::string const& platformInGeneratorName)
  21. : cmGlobalVisualStudio71Generator(cm, platformInGeneratorName)
  22. {
  23. this->ProjectConfigurationSectionName = "ProjectConfigurationPlatforms";
  24. this->Name = name;
  25. this->ExtraFlagTable = this->GetExtraFlagTableVS8();
  26. }
  27. std::string cmGlobalVisualStudio8Generator::FindDevEnvCommand()
  28. {
  29. // First look for VCExpress.
  30. std::string vsxcmd;
  31. std::string vsxkey =
  32. cmStrCat("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\VCExpress\\",
  33. this->GetIDEVersion(), ";InstallDir");
  34. if (cmSystemTools::ReadRegistryValue(vsxkey.c_str(), vsxcmd,
  35. cmSystemTools::KeyWOW64_32)) {
  36. cmSystemTools::ConvertToUnixSlashes(vsxcmd);
  37. vsxcmd += "/VCExpress.exe";
  38. return vsxcmd;
  39. }
  40. // Now look for devenv.
  41. return this->cmGlobalVisualStudio71Generator::FindDevEnvCommand();
  42. }
  43. void cmGlobalVisualStudio8Generator::EnableLanguage(
  44. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  45. {
  46. for (std::string const& it : lang) {
  47. if (it == "ASM_MASM") {
  48. this->MasmEnabled = true;
  49. }
  50. }
  51. this->AddPlatformDefinitions(mf);
  52. cmGlobalVisualStudio7Generator::EnableLanguage(lang, mf, optional);
  53. }
  54. void cmGlobalVisualStudio8Generator::AddPlatformDefinitions(cmMakefile* mf)
  55. {
  56. if (this->TargetsWindowsCE()) {
  57. mf->AddDefinition("CMAKE_VS_WINCE_VERSION", this->WindowsCEVersion);
  58. }
  59. }
  60. bool cmGlobalVisualStudio8Generator::SetGeneratorPlatform(std::string const& p,
  61. cmMakefile* mf)
  62. {
  63. if (this->PlatformInGeneratorName) {
  64. // This is an old-style generator name that contains the platform name.
  65. // No explicit platform specification is supported, so pass it through
  66. // to our base class implementation, which errors on non-empty platforms.
  67. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform(p, mf);
  68. }
  69. this->GeneratorPlatform = p;
  70. // The generator name does not contain the platform name, and so supports
  71. // explicit platform specification. We handled that above, so pass an
  72. // empty platform name to our base class implementation so it does not error.
  73. return this->cmGlobalVisualStudio7Generator::SetGeneratorPlatform("", mf);
  74. }
  75. std::string cmGlobalVisualStudio8Generator::GetGenerateStampList()
  76. {
  77. return "generate.stamp.list";
  78. }
  79. void cmGlobalVisualStudio8Generator::Configure()
  80. {
  81. this->cmGlobalVisualStudio7Generator::Configure();
  82. }
  83. bool cmGlobalVisualStudio8Generator::UseFolderProperty() const
  84. {
  85. return IsExpressEdition() ? false : cmGlobalGenerator::UseFolderProperty();
  86. }
  87. bool cmGlobalVisualStudio8Generator::AddCheckTarget()
  88. {
  89. // Add a special target on which all other targets depend that
  90. // checks the build system and optionally re-runs CMake.
  91. // Skip the target if no regeneration is to be done.
  92. if (this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION")) {
  93. return false;
  94. }
  95. std::vector<std::unique_ptr<cmLocalGenerator>> const& generators =
  96. this->LocalGenerators;
  97. auto& lg =
  98. cm::static_reference_cast<cmLocalVisualStudio7Generator>(generators[0]);
  99. const char* no_working_directory = nullptr;
  100. std::vector<std::string> no_byproducts;
  101. std::vector<std::string> no_depends;
  102. cmCustomCommandLines no_commands;
  103. cmTarget* tgt = lg.AddUtilityCommand(
  104. CMAKE_CHECK_BUILD_SYSTEM_TARGET, false, no_working_directory,
  105. no_byproducts, no_depends, no_commands, cmPolicies::NEW);
  106. auto ptr = cm::make_unique<cmGeneratorTarget>(tgt, &lg);
  107. auto gt = ptr.get();
  108. lg.AddGeneratorTarget(std::move(ptr));
  109. // Organize in the "predefined targets" folder:
  110. //
  111. if (this->UseFolderProperty()) {
  112. tgt->SetProperty("FOLDER", this->GetPredefinedTargetsFolder());
  113. }
  114. // Create a list of all stamp files for this project.
  115. std::vector<std::string> stamps;
  116. std::string stampList = cmStrCat(
  117. "CMakeFiles/", cmGlobalVisualStudio8Generator::GetGenerateStampList());
  118. {
  119. std::string stampListFile =
  120. cmStrCat(generators[0]->GetMakefile()->GetCurrentBinaryDirectory(), '/',
  121. stampList);
  122. std::string stampFile;
  123. cmGeneratedFileStream fout(stampListFile.c_str());
  124. for (const auto& gi : generators) {
  125. stampFile = cmStrCat(gi->GetMakefile()->GetCurrentBinaryDirectory(),
  126. "/CMakeFiles/generate.stamp");
  127. fout << stampFile << "\n";
  128. stamps.push_back(stampFile);
  129. }
  130. }
  131. // Add a custom rule to re-run CMake if any input files changed.
  132. {
  133. // The custom rule runs cmake so set UTF-8 pipes.
  134. bool stdPipesUTF8 = true;
  135. // Collect the input files used to generate all targets in this
  136. // project.
  137. std::vector<std::string> listFiles;
  138. for (const auto& gen : generators) {
  139. cm::append(listFiles, gen->GetMakefile()->GetListFiles());
  140. }
  141. // Add a custom prebuild target to run the VerifyGlobs script.
  142. cmake* cm = this->GetCMakeInstance();
  143. if (cm->DoWriteGlobVerifyTarget()) {
  144. cmCustomCommandLines verifyCommandLines = cmMakeSingleCommandLine(
  145. { cmSystemTools::GetCMakeCommand(), "-P", cm->GetGlobVerifyScript() });
  146. std::vector<std::string> byproducts;
  147. byproducts.push_back(cm->GetGlobVerifyStamp());
  148. lg.AddCustomCommandToTarget(CMAKE_CHECK_BUILD_SYSTEM_TARGET, byproducts,
  149. no_depends, verifyCommandLines,
  150. cmCustomCommandType::PRE_BUILD,
  151. "Checking File Globs", no_working_directory,
  152. cmPolicies::NEW, stdPipesUTF8);
  153. // Ensure ZERO_CHECK always runs in Visual Studio using MSBuild,
  154. // otherwise the prebuild command will not be run.
  155. tgt->SetProperty("VS_GLOBAL_DisableFastUpToDateCheck", "true");
  156. listFiles.push_back(cm->GetGlobVerifyStamp());
  157. }
  158. // Sort the list of input files and remove duplicates.
  159. std::sort(listFiles.begin(), listFiles.end(), std::less<std::string>());
  160. std::vector<std::string>::iterator new_end =
  161. std::unique(listFiles.begin(), listFiles.end());
  162. listFiles.erase(new_end, listFiles.end());
  163. // Create a rule to re-run CMake.
  164. std::string argS = cmStrCat("-S", lg.GetSourceDirectory());
  165. std::string argB = cmStrCat("-B", lg.GetBinaryDirectory());
  166. std::string const sln =
  167. lg.GetBinaryDirectory() + "/" + lg.GetProjectName() + ".sln";
  168. cmCustomCommandLines commandLines = cmMakeSingleCommandLine(
  169. { cmSystemTools::GetCMakeCommand(), argS, argB, "--check-stamp-list",
  170. stampList, "--vs-solution-file", sln });
  171. // Add the rule. Note that we cannot use the CMakeLists.txt
  172. // file as the main dependency because it would get
  173. // overwritten by the CreateVCProjBuildRule.
  174. // (this could be avoided with per-target source files)
  175. std::string no_main_dependency;
  176. cmImplicitDependsList no_implicit_depends;
  177. if (cmSourceFile* file = lg.AddCustomCommandToOutput(
  178. stamps, no_byproducts, listFiles, no_main_dependency,
  179. no_implicit_depends, commandLines, "Checking Build System",
  180. no_working_directory, cmPolicies::NEW, true, false, false, false, "",
  181. "", stdPipesUTF8)) {
  182. gt->AddSource(file->ResolveFullPath());
  183. } else {
  184. cmSystemTools::Error("Error adding rule for " + stamps[0]);
  185. }
  186. }
  187. return true;
  188. }
  189. void cmGlobalVisualStudio8Generator::AddExtraIDETargets()
  190. {
  191. cmGlobalVisualStudio7Generator::AddExtraIDETargets();
  192. if (this->AddCheckTarget()) {
  193. for (unsigned int i = 0; i < this->LocalGenerators.size(); ++i) {
  194. const auto& tgts = this->LocalGenerators[i]->GetGeneratorTargets();
  195. // All targets depend on the build-system check target.
  196. for (const auto& ti : tgts) {
  197. if (ti->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  198. ti->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false);
  199. }
  200. }
  201. }
  202. }
  203. }
  204. void cmGlobalVisualStudio8Generator::WriteSolutionConfigurations(
  205. std::ostream& fout, std::vector<std::string> const& configs)
  206. {
  207. fout << "\tGlobalSection(SolutionConfigurationPlatforms) = preSolution\n";
  208. for (std::string const& i : configs) {
  209. fout << "\t\t" << i << "|" << this->GetPlatformName() << " = " << i << "|"
  210. << this->GetPlatformName() << "\n";
  211. }
  212. fout << "\tEndGlobalSection\n";
  213. }
  214. void cmGlobalVisualStudio8Generator::WriteProjectConfigurations(
  215. std::ostream& fout, const std::string& name, cmGeneratorTarget const& target,
  216. std::vector<std::string> const& configs,
  217. const std::set<std::string>& configsPartOfDefaultBuild,
  218. std::string const& platformMapping)
  219. {
  220. std::string guid = this->GetGUID(name);
  221. for (std::string const& i : configs) {
  222. std::vector<std::string> mapConfig;
  223. const char* dstConfig = i.c_str();
  224. if (target.GetProperty("EXTERNAL_MSPROJECT")) {
  225. if (cmValue m = target.GetProperty("MAP_IMPORTED_CONFIG_" +
  226. cmSystemTools::UpperCase(i))) {
  227. cmExpandList(*m, mapConfig);
  228. if (!mapConfig.empty()) {
  229. dstConfig = mapConfig[0].c_str();
  230. }
  231. }
  232. }
  233. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  234. << ".ActiveCfg = " << dstConfig << "|"
  235. << (!platformMapping.empty() ? platformMapping
  236. : this->GetPlatformName())
  237. << "\n";
  238. std::set<std::string>::const_iterator ci =
  239. configsPartOfDefaultBuild.find(i);
  240. if (!(ci == configsPartOfDefaultBuild.end())) {
  241. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  242. << ".Build.0 = " << dstConfig << "|"
  243. << (!platformMapping.empty() ? platformMapping
  244. : this->GetPlatformName())
  245. << "\n";
  246. }
  247. if (this->NeedsDeploy(target, dstConfig)) {
  248. fout << "\t\t{" << guid << "}." << i << "|" << this->GetPlatformName()
  249. << ".Deploy.0 = " << dstConfig << "|"
  250. << (!platformMapping.empty() ? platformMapping
  251. : this->GetPlatformName())
  252. << "\n";
  253. }
  254. }
  255. }
  256. bool cmGlobalVisualStudio8Generator::NeedsDeploy(
  257. cmGeneratorTarget const& target, const char* config) const
  258. {
  259. cmStateEnums::TargetType const type = target.GetType();
  260. if (type != cmStateEnums::EXECUTABLE &&
  261. type != cmStateEnums::SHARED_LIBRARY) {
  262. // deployment only valid on executables and shared libraries.
  263. return false;
  264. }
  265. if (cmValue prop = target.GetProperty("VS_SOLUTION_DEPLOY")) {
  266. // If set, it dictates behavior
  267. return cmIsOn(
  268. cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator, config));
  269. }
  270. // To be deprecated, disable deployment even if target supports it.
  271. if (cmValue prop = target.GetProperty("VS_NO_SOLUTION_DEPLOY")) {
  272. if (cmIsOn(cmGeneratorExpression::Evaluate(*prop, target.LocalGenerator,
  273. config))) {
  274. // If true, always disable deployment
  275. return false;
  276. }
  277. }
  278. // Legacy behavior, enabled deployment based on 'hard-coded' target
  279. // platforms.
  280. return this->TargetSystemSupportsDeployment();
  281. }
  282. bool cmGlobalVisualStudio8Generator::TargetSystemSupportsDeployment() const
  283. {
  284. return this->TargetsWindowsCE();
  285. }
  286. bool cmGlobalVisualStudio8Generator::ComputeTargetDepends()
  287. {
  288. // Skip over the cmGlobalVisualStudioGenerator implementation!
  289. // We do not need the support that VS <= 7.1 needs.
  290. return this->cmGlobalGenerator::ComputeTargetDepends();
  291. }
  292. void cmGlobalVisualStudio8Generator::WriteProjectDepends(
  293. std::ostream& fout, const std::string&, const std::string&,
  294. cmGeneratorTarget const* gt)
  295. {
  296. TargetDependSet const& unordered = this->GetTargetDirectDepends(gt);
  297. OrderedTargetDependSet depends(unordered, std::string());
  298. for (cmTargetDepend const& i : depends) {
  299. if (!i->IsInBuildSystem()) {
  300. continue;
  301. }
  302. std::string guid = this->GetGUID(i->GetName());
  303. fout << "\t\t{" << guid << "} = {" << guid << "}\n";
  304. }
  305. }
  306. bool cmGlobalVisualStudio8Generator::NeedLinkLibraryDependencies(
  307. cmGeneratorTarget* target)
  308. {
  309. // Look for utility dependencies that magically link.
  310. for (BT<std::pair<std::string, bool>> const& ui : target->GetUtilities()) {
  311. if (cmGeneratorTarget* depTarget =
  312. target->GetLocalGenerator()->FindGeneratorTargetToUse(
  313. ui.Value.first)) {
  314. if (depTarget->IsInBuildSystem() &&
  315. depTarget->GetProperty("EXTERNAL_MSPROJECT")) {
  316. // This utility dependency names an external .vcproj target.
  317. // We use LinkLibraryDependencies="true" to link to it without
  318. // predicting the .lib file location or name.
  319. return true;
  320. }
  321. }
  322. }
  323. return false;
  324. }
  325. static cmVS7FlagTable cmVS8ExtraFlagTable[] = {
  326. { "CallingConvention", "Gd", "cdecl", "0", 0 },
  327. { "CallingConvention", "Gr", "fastcall", "1", 0 },
  328. { "CallingConvention", "Gz", "stdcall", "2", 0 },
  329. { "Detect64BitPortabilityProblems", "Wp64",
  330. "Detect 64Bit Portability Problems", "true", 0 },
  331. { "ErrorReporting", "errorReport:prompt", "Report immediately", "1", 0 },
  332. { "ErrorReporting", "errorReport:queue", "Queue for next login", "2", 0 },
  333. // Precompiled header and related options. Note that the
  334. // UsePrecompiledHeader entries are marked as "Continue" so that the
  335. // corresponding PrecompiledHeaderThrough entry can be found.
  336. { "UsePrecompiledHeader", "Yu", "Use Precompiled Header", "2",
  337. cmVS7FlagTable::UserValueIgnored | cmVS7FlagTable::Continue },
  338. { "PrecompiledHeaderThrough", "Yu", "Precompiled Header Name", "",
  339. cmVS7FlagTable::UserValueRequired },
  340. { "UsePrecompiledHeader", "Y-", "Don't use precompiled header", "0", 0 },
  341. // There is no YX option in the VS8 IDE.
  342. // Exception handling mode. If no entries match, it will be FALSE.
  343. { "ExceptionHandling", "GX", "enable c++ exceptions", "1", 0 },
  344. { "ExceptionHandling", "EHsc", "enable c++ exceptions", "1", 0 },
  345. { "ExceptionHandling", "EHa", "enable SEH exceptions", "2", 0 },
  346. { "EnablePREfast", "analyze", "", "true", 0 },
  347. { "EnablePREfast", "analyze-", "", "false", 0 },
  348. // Language options
  349. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t", "wchar_t is a built-in type",
  350. "true", 0 },
  351. { "TreatWChar_tAsBuiltInType", "Zc:wchar_t-",
  352. "wchar_t is not a built-in type", "false", 0 },
  353. { "", "", "", "", 0 }
  354. };
  355. cmIDEFlagTable const* cmGlobalVisualStudio8Generator::GetExtraFlagTableVS8()
  356. {
  357. return cmVS8ExtraFlagTable;
  358. }