cmLocalVisualStudio10Generator.cxx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 "cmLocalVisualStudio10Generator.h"
  4. #include "cmGeneratorTarget.h"
  5. #include "cmGlobalVisualStudio10Generator.h"
  6. #include "cmMakefile.h"
  7. #include "cmVisualStudio10TargetGenerator.h"
  8. #include "cmXMLParser.h"
  9. #include "cm_expat.h"
  10. class cmVS10XMLParser : public cmXMLParser
  11. {
  12. public:
  13. virtual void EndElement(const std::string& /* name */) {}
  14. virtual void CharacterDataHandler(const char* data, int length)
  15. {
  16. if (this->DoGUID) {
  17. if (data[0] == '{') {
  18. // remove surrounding curly brackets
  19. this->GUID.assign(data + 1, length - 2);
  20. } else {
  21. this->GUID.assign(data, length);
  22. }
  23. this->DoGUID = false;
  24. }
  25. }
  26. virtual void StartElement(const std::string& name, const char**)
  27. {
  28. // once the GUID is found do nothing
  29. if (!this->GUID.empty()) {
  30. return;
  31. }
  32. if ("ProjectGUID" == name || "ProjectGuid" == name) {
  33. this->DoGUID = true;
  34. }
  35. }
  36. int InitializeParser()
  37. {
  38. this->DoGUID = false;
  39. int ret = cmXMLParser::InitializeParser();
  40. if (ret == 0) {
  41. return ret;
  42. }
  43. // visual studio projects have a strange encoding, but it is
  44. // really utf-8
  45. XML_SetEncoding(static_cast<XML_Parser>(this->Parser), "utf-8");
  46. return 1;
  47. }
  48. std::string GUID;
  49. bool DoGUID;
  50. };
  51. cmLocalVisualStudio10Generator::cmLocalVisualStudio10Generator(
  52. cmGlobalGenerator* gg, cmMakefile* mf)
  53. : cmLocalVisualStudio7Generator(gg, mf)
  54. {
  55. }
  56. cmLocalVisualStudio10Generator::~cmLocalVisualStudio10Generator()
  57. {
  58. }
  59. void cmLocalVisualStudio10Generator::GenerateTargetsDepthFirst(
  60. cmGeneratorTarget* target, std::vector<cmGeneratorTarget*>& remaining)
  61. {
  62. if (target->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  63. return;
  64. }
  65. // Find this target in the list of remaining targets.
  66. auto it = std::find(remaining.begin(), remaining.end(), target);
  67. if (it == remaining.end()) {
  68. // This target was already handled.
  69. return;
  70. }
  71. // Remove this target from the list of remaining targets because
  72. // we are handling it now.
  73. *it = nullptr;
  74. auto& deps = this->GlobalGenerator->GetTargetDirectDepends(target);
  75. for (auto& d : deps) {
  76. // FIXME: Revise CreateSingleVCProj so we do not have to drop `const` here.
  77. auto dependee = const_cast<cmGeneratorTarget*>(&*d);
  78. GenerateTargetsDepthFirst(dependee, remaining);
  79. // Take the union of visited source files of custom commands
  80. auto visited = GetSourcesVisited(dependee);
  81. GetSourcesVisited(target).insert(visited.begin(), visited.end());
  82. }
  83. if (static_cast<cmGlobalVisualStudioGenerator*>(this->GlobalGenerator)
  84. ->TargetIsFortranOnly(target)) {
  85. this->CreateSingleVCProj(target->GetName(), target);
  86. } else {
  87. cmVisualStudio10TargetGenerator tg(
  88. target, static_cast<cmGlobalVisualStudio10Generator*>(
  89. this->GetGlobalGenerator()));
  90. tg.Generate();
  91. }
  92. }
  93. void cmLocalVisualStudio10Generator::Generate()
  94. {
  95. std::vector<cmGeneratorTarget*> remaining = this->GetGeneratorTargets();
  96. for (auto& t : remaining) {
  97. if (t) {
  98. GenerateTargetsDepthFirst(t, remaining);
  99. }
  100. }
  101. this->WriteStampFiles();
  102. }
  103. void cmLocalVisualStudio10Generator::ReadAndStoreExternalGUID(
  104. const std::string& name, const char* path)
  105. {
  106. cmVS10XMLParser parser;
  107. parser.ParseFile(path);
  108. // if we can not find a GUID then we will generate one later
  109. if (parser.GUID.empty()) {
  110. return;
  111. }
  112. std::string guidStoreName = name;
  113. guidStoreName += "_GUID_CMAKE";
  114. // save the GUID in the cache
  115. this->GlobalGenerator->GetCMakeInstance()->AddCacheEntry(
  116. guidStoreName.c_str(), parser.GUID.c_str(), "Stored GUID",
  117. cmStateEnums::INTERNAL);
  118. }
  119. const char* cmLocalVisualStudio10Generator::ReportErrorLabel() const
  120. {
  121. return ":VCEnd";
  122. }