cmGlobalVisualStudio8Generator.cxx 14 KB

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