cmGlobalVisualStudio8Generator.cxx 14 KB

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