cmXCodeObject.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmXCodeObject.h"
  14. #include "cmSystemTools.h"
  15. //----------------------------------------------------------------------------
  16. const char* cmXCodeObject::PBXTypeNames[] = {
  17. "PBXGroup", "PBXBuildStyle", "PBXProject", "PBXHeadersBuildPhase",
  18. "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXNativeTarget",
  19. "PBXFileReference", "PBXBuildFile", "PBXContainerItemProxy",
  20. "PBXTargetDependency", "PBXShellScriptBuildPhase",
  21. "PBXResourcesBuildPhase", "PBXApplicationReference",
  22. "PBXExecutableFileReference", "PBXLibraryReference", "PBXToolTarget",
  23. "PBXLibraryTarget", "PBXAggregateTarget", "XCBuildConfiguration",
  24. "XCConfigurationList",
  25. "PBXCopyFilesBuildPhase",
  26. "None"
  27. };
  28. //----------------------------------------------------------------------------
  29. cmXCodeObject::~cmXCodeObject()
  30. {
  31. this->Version = 15;
  32. }
  33. //----------------------------------------------------------------------------
  34. cmXCodeObject::cmXCodeObject(PBXType ptype, Type type)
  35. {
  36. this->Version = 15;
  37. this->PBXTargetDependencyValue = 0;
  38. this->Target = 0;
  39. this->Object =0;
  40. this->IsA = ptype;
  41. if(type == OBJECT)
  42. {
  43. cmOStringStream str;
  44. str << (void*)this;
  45. str << (void*)this;
  46. str << (void*)this;
  47. this->Id = str.str();
  48. }
  49. else
  50. {
  51. this->Id =
  52. "Temporary cmake object, should not be refered to in xcode file";
  53. }
  54. cmSystemTools::ReplaceString(this->Id, "0x", "");
  55. this->Id = cmSystemTools::UpperCase(this->Id);
  56. if(this->Id.size() < 24)
  57. {
  58. int diff = 24 - this->Id.size();
  59. for(int i =0; i < diff; ++i)
  60. {
  61. this->Id += "0";
  62. }
  63. }
  64. if(this->Id.size() > 24)
  65. {
  66. this->Id = this->Id.substr(0,24);
  67. }
  68. this->TypeValue = type;
  69. if(this->TypeValue == OBJECT)
  70. {
  71. this->AddAttribute("isa", 0);
  72. }
  73. }
  74. //----------------------------------------------------------------------------
  75. void cmXCodeObject::Indent(int level, std::ostream& out)
  76. {
  77. while(level)
  78. {
  79. out << " ";
  80. level--;
  81. }
  82. }
  83. //----------------------------------------------------------------------------
  84. void cmXCodeObject::Print(std::ostream& out)
  85. {
  86. std::string separator = "\n";
  87. int indentFactor = 1;
  88. if(this->Version > 15
  89. && (this->IsA == PBXFileReference || this->IsA == PBXBuildFile))
  90. {
  91. separator = " ";
  92. indentFactor = 0;
  93. }
  94. cmXCodeObject::Indent(2*indentFactor, out);
  95. out << this->Id << " ";
  96. if(!(this->IsA == PBXGroup && this->Comment.size() == 0))
  97. {
  98. this->PrintComment(out);
  99. }
  100. out << " = {";
  101. if(separator == "\n")
  102. {
  103. out << separator;
  104. }
  105. std::map<cmStdString, cmXCodeObject*>::iterator i;
  106. cmXCodeObject::Indent(3*indentFactor, out);
  107. out << "isa = " << PBXTypeNames[this->IsA] << ";" << separator;
  108. for(i = this->ObjectAttributes.begin();
  109. i != this->ObjectAttributes.end(); ++i)
  110. {
  111. cmXCodeObject* object = i->second;
  112. if(i->first != "isa")
  113. {
  114. cmXCodeObject::Indent(3*indentFactor, out);
  115. }
  116. else
  117. {
  118. continue;
  119. }
  120. if(object->TypeValue == OBJECT_LIST)
  121. {
  122. out << i->first << " = (" << separator;
  123. for(unsigned int k = 0; k < i->second->List.size(); k++)
  124. {
  125. cmXCodeObject::Indent(4*indentFactor, out);
  126. out << i->second->List[k]->Id << " ";
  127. i->second->List[k]->PrintComment(out);
  128. out << "," << separator;
  129. }
  130. cmXCodeObject::Indent(3*indentFactor, out);
  131. out << ");" << separator;
  132. }
  133. else if(object->TypeValue == ATTRIBUTE_GROUP)
  134. {
  135. std::map<cmStdString, cmXCodeObject*>::iterator j;
  136. out << i->first << " = {" << separator;
  137. for(j = object->ObjectAttributes.begin(); j !=
  138. object->ObjectAttributes.end(); ++j)
  139. {
  140. cmXCodeObject::Indent(4 *indentFactor, out);
  141. if(j->second->TypeValue == STRING)
  142. {
  143. out << j->first << " = " << j->second->String << ";";
  144. }
  145. else if(j->second->TypeValue == OBJECT_LIST)
  146. {
  147. out << j->first << " = (";
  148. for(unsigned int k = 0; k < j->second->List.size(); k++)
  149. {
  150. if(j->second->List[k]->TypeValue == STRING)
  151. {
  152. out << j->second->List[k]->String << ", ";
  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 << " = " << object->String << ";" << separator;
  184. }
  185. else
  186. {
  187. out << "what is this?? " << i->first << "\n";
  188. }
  189. }
  190. cmXCodeObject::Indent(2*indentFactor, out);
  191. out << "};\n";
  192. }
  193. //----------------------------------------------------------------------------
  194. void cmXCodeObject::PrintList(std::vector<cmXCodeObject*> const& objs,
  195. std::ostream& out)
  196. {
  197. cmXCodeObject::Indent(1, out);
  198. out << "objects = {\n";
  199. for(unsigned int i = 0; i < objs.size(); ++i)
  200. {
  201. if(objs[i]->TypeValue == OBJECT)
  202. {
  203. objs[i]->Print(out);
  204. }
  205. }
  206. cmXCodeObject::Indent(1, out);
  207. out << "};\n";
  208. }
  209. //----------------------------------------------------------------------------
  210. void cmXCodeObject::CopyAttributes(cmXCodeObject* copy)
  211. {
  212. this->ObjectAttributes = copy->ObjectAttributes;
  213. this->List = copy->List;
  214. this->String = copy->String;
  215. this->Object = copy->Object;
  216. }
  217. //----------------------------------------------------------------------------
  218. void cmXCodeObject::SetString(const char* s)
  219. {
  220. std::string ss = s;
  221. if(ss.size() == 0)
  222. {
  223. this->String = "\"\"";
  224. return;
  225. }
  226. // escape quotes
  227. cmSystemTools::ReplaceString(ss, "\"", "\\\"");
  228. bool needQuote = false;
  229. this->String = "";
  230. if(ss.find_first_of(" <>.+-=@") != ss.npos)
  231. {
  232. needQuote = true;
  233. }
  234. if(needQuote)
  235. {
  236. this->String = "\"";
  237. }
  238. this->String += ss;
  239. if(needQuote)
  240. {
  241. this->String += "\"";
  242. }
  243. }