cmGlobalVisualStudio8Generator.cxx 14 KB

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