cmXCodeObject.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <algorithm>
  6. #include <cstddef>
  7. #include <iosfwd>
  8. #include <map>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include <cm/string_view>
  13. #include <cmext/algorithm>
  14. class cmGeneratorTarget;
  15. class cmXCodeObject
  16. {
  17. public:
  18. enum Type
  19. {
  20. OBJECT_LIST,
  21. STRING,
  22. ATTRIBUTE_GROUP,
  23. OBJECT_REF,
  24. OBJECT
  25. };
  26. enum PBXType
  27. {
  28. PBXGroup,
  29. PBXBuildStyle,
  30. PBXProject,
  31. PBXHeadersBuildPhase,
  32. PBXSourcesBuildPhase,
  33. PBXFrameworksBuildPhase,
  34. PBXNativeTarget,
  35. PBXFileReference,
  36. PBXBuildFile,
  37. PBXContainerItemProxy,
  38. PBXTargetDependency,
  39. PBXShellScriptBuildPhase,
  40. PBXResourcesBuildPhase,
  41. PBXApplicationReference,
  42. PBXExecutableFileReference,
  43. PBXLibraryReference,
  44. PBXToolTarget,
  45. PBXLibraryTarget,
  46. PBXAggregateTarget,
  47. XCBuildConfiguration,
  48. XCConfigurationList,
  49. PBXCopyFilesBuildPhase,
  50. None
  51. };
  52. class StringVec : public std::vector<std::string>
  53. {
  54. };
  55. static char const* PBXTypeNames[];
  56. virtual ~cmXCodeObject();
  57. cmXCodeObject(PBXType ptype, Type type, std::string id);
  58. Type GetType() const { return this->TypeValue; }
  59. PBXType GetIsA() const { return this->IsA; }
  60. bool IsEmpty() const;
  61. void SetString(cm::string_view s);
  62. std::string const& GetString() const { return this->String; }
  63. void AddAttribute(std::string const& name, cmXCodeObject* value)
  64. {
  65. this->ObjectAttributes[name] = value;
  66. }
  67. void AddAttributeIfNotEmpty(std::string const& name, cmXCodeObject* value)
  68. {
  69. if (value && !value->IsEmpty()) {
  70. AddAttribute(name, value);
  71. }
  72. }
  73. void SetObject(cmXCodeObject* value) { this->Object = value; }
  74. cmXCodeObject* GetObject() { return this->Object; }
  75. void AddObject(cmXCodeObject* value) { this->List.push_back(value); }
  76. size_t GetObjectCount() { return this->List.size(); }
  77. void InsertObject(size_t position, cmXCodeObject* value)
  78. {
  79. if (position < GetObjectCount()) {
  80. this->List.insert(this->List.begin() + position, value);
  81. }
  82. }
  83. void PrependObject(cmXCodeObject* value)
  84. {
  85. this->List.insert(this->List.begin(), value);
  86. }
  87. bool HasObject(cmXCodeObject* o) const
  88. {
  89. return cm::contains(this->List, o);
  90. }
  91. void AddUniqueObject(cmXCodeObject* value)
  92. {
  93. if (!cm::contains(this->List, value)) {
  94. this->List.push_back(value);
  95. }
  96. }
  97. static void Indent(int level, std::ostream& out);
  98. void Print(std::ostream& out);
  99. void PrintAttribute(std::ostream& out, int level,
  100. std::string const& separator, int factor,
  101. std::string const& name, cmXCodeObject const* object,
  102. cmXCodeObject const* parent);
  103. virtual void PrintComment(std::ostream&) {}
  104. static void PrintList(std::vector<cmXCodeObject*> const&, std::ostream& out);
  105. std::string const& GetId() const { return this->Id; }
  106. void SetId(std::string const& id) { this->Id = id; }
  107. cmGeneratorTarget* GetTarget() const { return this->Target; }
  108. void SetTarget(cmGeneratorTarget* t) { this->Target = t; }
  109. std::string const& GetComment() const { return this->Comment; }
  110. bool HasComment() const { return (!this->Comment.empty()); }
  111. cmXCodeObject* GetAttribute(char const* name) const
  112. {
  113. auto const i = this->ObjectAttributes.find(name);
  114. if (i != this->ObjectAttributes.end()) {
  115. return i->second;
  116. }
  117. return nullptr;
  118. }
  119. // search the attribute list for an object of the specified type
  120. cmXCodeObject* GetObject(cmXCodeObject::PBXType t) const
  121. {
  122. for (auto* o : this->List) {
  123. if (o->IsA == t) {
  124. return o;
  125. }
  126. }
  127. return nullptr;
  128. }
  129. void CopyAttributes(cmXCodeObject*);
  130. void AddDependLibrary(std::string const& configName, std::string const& l)
  131. {
  132. this->DependLibraries[configName].push_back(l);
  133. }
  134. std::map<std::string, StringVec> const& GetDependLibraries() const
  135. {
  136. return this->DependLibraries;
  137. }
  138. void AddDependTarget(std::string const& configName, std::string const& tName)
  139. {
  140. this->DependTargets[configName].push_back(tName);
  141. }
  142. std::map<std::string, StringVec> const& GetDependTargets() const
  143. {
  144. return this->DependTargets;
  145. }
  146. std::vector<cmXCodeObject*> const& GetObjectList() const
  147. {
  148. return this->List;
  149. }
  150. void SetComment(std::string const& c) { this->Comment = c; }
  151. static void PrintString(std::ostream& os, std::string const& String);
  152. protected:
  153. void PrintString(std::ostream& os) const;
  154. cmGeneratorTarget* Target;
  155. Type TypeValue;
  156. std::string Id;
  157. PBXType IsA;
  158. int Version;
  159. std::string Comment;
  160. std::string String;
  161. cmXCodeObject* Object;
  162. std::vector<cmXCodeObject*> List;
  163. std::map<std::string, StringVec> DependLibraries;
  164. std::map<std::string, StringVec> DependTargets;
  165. std::map<std::string, cmXCodeObject*> ObjectAttributes;
  166. };