cmGlobalVisualStudio8Generator.cxx 17 KB

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