cmXCodeObject.cxx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. cmXCodeObject::PrintString(out,j->first);
  141. out << " = ";
  142. j->second->PrintString(out);
  143. out << ";";
  144. }
  145. else if(j->second->TypeValue == OBJECT_LIST)
  146. {
  147. cmXCodeObject::PrintString(out,j->first);
  148. out << " = (";
  149. for(unsigned int k = 0; k < j->second->List.size(); k++)
  150. {
  151. if(j->second->List[k]->TypeValue == STRING)
  152. {
  153. j->second->List[k]->PrintString(out);
  154. out << ", ";
  155. }
  156. else
  157. {
  158. out << "List_" << k << "_TypeValue_IS_NOT_STRING, ";
  159. }
  160. }
  161. out << ");";
  162. }
  163. else
  164. {
  165. cmXCodeObject::PrintString(out,j->first);
  166. out << " = error_unexpected_TypeValue_" <<
  167. j->second->TypeValue << ";";
  168. }
  169. out << separator;
  170. }
  171. cmXCodeObject::Indent(3 *indentFactor, out);
  172. out << "};" << separator;
  173. }
  174. else if(object->TypeValue == OBJECT_REF)
  175. {
  176. cmXCodeObject::PrintString(out,i->first);
  177. out << " = " << object->Object->Id;
  178. if(object->Object->HasComment() && i->first != "remoteGlobalIDString")
  179. {
  180. out << " ";
  181. object->Object->PrintComment(out);
  182. }
  183. out << ";" << separator;
  184. }
  185. else if(object->TypeValue == STRING)
  186. {
  187. cmXCodeObject::PrintString(out,i->first);
  188. out << " = ";
  189. object->PrintString(out);
  190. out << ";" << separator;
  191. }
  192. else
  193. {
  194. out << "what is this?? " << i->first << "\n";
  195. }
  196. }
  197. cmXCodeObject::Indent(2*indentFactor, out);
  198. out << "};\n";
  199. }
  200. //----------------------------------------------------------------------------
  201. void cmXCodeObject::PrintList(std::vector<cmXCodeObject*> const& objs,
  202. std::ostream& out)
  203. {
  204. cmXCodeObject::Indent(1, out);
  205. out << "objects = {\n";
  206. for(unsigned int i = 0; i < objs.size(); ++i)
  207. {
  208. if(objs[i]->TypeValue == OBJECT)
  209. {
  210. objs[i]->Print(out);
  211. }
  212. }
  213. cmXCodeObject::Indent(1, out);
  214. out << "};\n";
  215. }
  216. //----------------------------------------------------------------------------
  217. void cmXCodeObject::CopyAttributes(cmXCodeObject* copy)
  218. {
  219. this->ObjectAttributes = copy->ObjectAttributes;
  220. this->List = copy->List;
  221. this->String = copy->String;
  222. this->Object = copy->Object;
  223. }
  224. //----------------------------------------------------------------------------
  225. void cmXCodeObject::PrintString(std::ostream& os,cmStdString String)
  226. {
  227. // The string needs to be quoted if it contains any characters
  228. // considered special by the Xcode project file parser.
  229. bool needQuote =
  230. (String.empty() ||
  231. String.find_first_of(" <>.+-=@$[],") != String.npos);
  232. const char* quote = needQuote? "\"" : "";
  233. // Print the string, quoted and escaped as necessary.
  234. os << quote;
  235. for(std::string::const_iterator i = String.begin();
  236. i != String.end(); ++i)
  237. {
  238. if(*i == '"')
  239. {
  240. // Escape double-quotes.
  241. os << '\\';
  242. }
  243. os << *i;
  244. }
  245. os << quote;
  246. }
  247. void cmXCodeObject::PrintString(std::ostream& os) const
  248. {
  249. cmXCodeObject::PrintString(os,this->String);
  250. }
  251. //----------------------------------------------------------------------------
  252. void cmXCodeObject::SetString(const char* s)
  253. {
  254. this->String = s;
  255. }