cmGlobalVisualStudio8Generator.cxx 14 KB

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