cmGlobalVisualStudio8Generator.cxx 14 KB

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