cmXCodeObject.cxx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmXCodeObject.h"
  11. #include "cmSystemTools.h"
  12. //----------------------------------------------------------------------------
  13. const char* cmXCodeObject::PBXTypeNames[] = {
  14. "PBXGroup", "PBXBuildStyle", "PBXProject", "PBXHeadersBuildPhase",
  15. "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXNativeTarget",
  16. "PBXFileReference", "PBXBuildFile", "PBXContainerItemProxy",
  17. "PBXTargetDependency", "PBXShellScriptBuildPhase",
  18. "PBXResourcesBuildPhase", "PBXApplicationReference",
  19. "PBXExecutableFileReference", "PBXLibraryReference", "PBXToolTarget",
  20. "PBXLibraryTarget", "PBXAggregateTarget", "XCBuildConfiguration",
  21. "XCConfigurationList",
  22. "PBXCopyFilesBuildPhase",
  23. "None"
  24. };
  25. //----------------------------------------------------------------------------
  26. cmXCodeObject::~cmXCodeObject()
  27. {
  28. this->Version = 15;
  29. }
  30. //----------------------------------------------------------------------------
  31. cmXCodeObject::cmXCodeObject(PBXType ptype, Type type)
  32. {
  33. this->Version = 15;
  34. this->PBXTargetDependencyValue = 0;
  35. this->Target = 0;
  36. this->Object =0;
  37. this->IsA = ptype;
  38. if(type == OBJECT)
  39. {
  40. cmOStringStream str;
  41. str << (void*)this;
  42. str << (void*)this;
  43. str << (void*)this;
  44. this->Id = str.str();
  45. }
  46. else
  47. {
  48. this->Id =
  49. "Temporary cmake object, should not be refered to in xcode file";
  50. }
  51. cmSystemTools::ReplaceString(this->Id, "0x", "");
  52. this->Id = cmSystemTools::UpperCase(this->Id);
  53. if(this->Id.size() < 24)
  54. {
  55. int diff = 24 - this->Id.size();
  56. for(int i =0; i < diff; ++i)
  57. {
  58. this->Id += "0";
  59. }
  60. }
  61. if(this->Id.size() > 24)
  62. {
  63. this->Id = this->Id.substr(0,24);
  64. }
  65. this->TypeValue = type;
  66. if(this->TypeValue == OBJECT)
  67. {
  68. this->AddAttribute("isa", 0);
  69. }
  70. }
  71. //----------------------------------------------------------------------------
  72. void cmXCodeObject::Indent(int level, std::ostream& out)
  73. {
  74. while(level)
  75. {
  76. out << " ";
  77. level--;
  78. }
  79. }
  80. //----------------------------------------------------------------------------
  81. void cmXCodeObject::Print(std::ostream& out)
  82. {
  83. std::string separator = "\n";
  84. int indentFactor = 1;
  85. if(this->Version > 15
  86. && (this->IsA == PBXFileReference || this->IsA == PBXBuildFile))
  87. {
  88. separator = " ";
  89. indentFactor = 0;
  90. }
  91. cmXCodeObject::Indent(2*indentFactor, out);
  92. out << this->Id << " ";
  93. if(!(this->IsA == PBXGroup && this->Comment.size() == 0))
  94. {
  95. this->PrintComment(out);
  96. }
  97. out << " = {";
  98. if(separator == "\n")
  99. {
  100. out << separator;
  101. }
  102. std::map<cmStdString, cmXCodeObject*>::iterator i;
  103. cmXCodeObject::Indent(3*indentFactor, out);
  104. out << "isa = " << PBXTypeNames[this->IsA] << ";" << separator;
  105. for(i = this->ObjectAttributes.begin();
  106. i != this->ObjectAttributes.end(); ++i)
  107. {
  108. cmXCodeObject* object = i->second;
  109. if(i->first != "isa")
  110. {
  111. cmXCodeObject::Indent(3*indentFactor, out);
  112. }
  113. else
  114. {
  115. continue;
  116. }
  117. if(object->TypeValue == OBJECT_LIST)
  118. {
  119. out << i->first << " = (" << separator;
  120. for(unsigned int k = 0; k < i->second->List.size(); k++)
  121. {
  122. cmXCodeObject::Indent(4*indentFactor, out);
  123. out << i->second->List[k]->Id << " ";
  124. i->second->List[k]->PrintComment(out);
  125. out << "," << separator;
  126. }
  127. cmXCodeObject::Indent(3*indentFactor, out);
  128. out << ");" << separator;
  129. }
  130. else if(object->TypeValue == ATTRIBUTE_GROUP)
  131. {
  132. std::map<cmStdString, cmXCodeObject*>::iterator j;
  133. out << i->first << " = {" << separator;
  134. for(j = object->ObjectAttributes.begin(); j !=
  135. object->ObjectAttributes.end(); ++j)
  136. {
  137. cmXCodeObject::Indent(4 *indentFactor, out);
  138. if(j->second->TypeValue == STRING)
  139. {
  140. out << j->first << " = ";
  141. j->second->PrintString(out);
  142. out << ";";
  143. }
  144. else if(j->second->TypeValue == OBJECT_LIST)
  145. {
  146. out << j->first << " = (";
  147. for(unsigned int k = 0; k < j->second->List.size(); k++)
  148. {
  149. if(j->second->List[k]->TypeValue == STRING)
  150. {
  151. j->second->List[k]->PrintString(out);
  152. out << ", ";
  153. }
  154. else
  155. {
  156. out << "List_" << k << "_TypeValue_IS_NOT_STRING, ";
  157. }
  158. }
  159. out << ");";
  160. }
  161. else
  162. {
  163. out << j->first << " = error_unexpected_TypeValue_" <<
  164. j->second->TypeValue << ";";
  165. }
  166. out << separator;
  167. }
  168. cmXCodeObject::Indent(3 *indentFactor, out);
  169. out << "};" << separator;
  170. }
  171. else if(object->TypeValue == OBJECT_REF)
  172. {
  173. out << i->first << " = " << object->Object->Id;
  174. if(object->Object->HasComment() && i->first != "remoteGlobalIDString")
  175. {
  176. out << " ";
  177. object->Object->PrintComment(out);
  178. }
  179. out << ";" << separator;
  180. }
  181. else if(object->TypeValue == STRING)
  182. {
  183. out << i->first << " = ";
  184. object->PrintString(out);
  185. out << ";" << separator;
  186. }
  187. else
  188. {
  189. out << "what is this?? " << i->first << "\n";
  190. }
  191. }
  192. cmXCodeObject::Indent(2*indentFactor, out);
  193. out << "};\n";
  194. }
  195. //----------------------------------------------------------------------------
  196. void cmXCodeObject::PrintList(std::vector<cmXCodeObject*> const& objs,
  197. std::ostream& out)
  198. {
  199. cmXCodeObject::Indent(1, out);
  200. out << "objects = {\n";
  201. for(unsigned int i = 0; i < objs.size(); ++i)
  202. {
  203. if(objs[i]->TypeValue == OBJECT)
  204. {
  205. objs[i]->Print(out);
  206. }
  207. }
  208. cmXCodeObject::Indent(1, out);
  209. out << "};\n";
  210. }
  211. //----------------------------------------------------------------------------
  212. void cmXCodeObject::CopyAttributes(cmXCodeObject* copy)
  213. {
  214. this->ObjectAttributes = copy->ObjectAttributes;
  215. this->List = copy->List;
  216. this->String = copy->String;
  217. this->Object = copy->Object;
  218. }
  219. //----------------------------------------------------------------------------
  220. void cmXCodeObject::PrintString(std::ostream& os) const
  221. {
  222. // The string needs to be quoted if it contains any characters
  223. // considered special by the Xcode project file parser.
  224. bool needQuote =
  225. (this->String.empty() ||
  226. this->String.find_first_of(" <>.+-=@$") != this->String.npos);
  227. const char* quote = needQuote? "\"" : "";
  228. // Print the string, quoted and escaped as necessary.
  229. os << quote;
  230. for(std::string::const_iterator i = this->String.begin();
  231. i != this->String.end(); ++i)
  232. {
  233. if(*i == '"')
  234. {
  235. // Escape double-quotes.
  236. os << '\\';
  237. }
  238. os << *i;
  239. }
  240. os << quote;
  241. }
  242. //----------------------------------------------------------------------------
  243. void cmXCodeObject::SetString(const char* s)
  244. {
  245. this->String = s;
  246. }