cmGlobalVisualStudio8Generator.cxx 16 KB

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