cmXCodeObject.cxx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. #include <CoreFoundation/CoreFoundation.h> // CFUUIDCreate
  13. //----------------------------------------------------------------------------
  14. const char* cmXCodeObject::PBXTypeNames[] = {
  15. "PBXGroup", "PBXBuildStyle", "PBXProject", "PBXHeadersBuildPhase",
  16. "PBXSourcesBuildPhase", "PBXFrameworksBuildPhase", "PBXNativeTarget",
  17. "PBXFileReference", "PBXBuildFile", "PBXContainerItemProxy",
  18. "PBXTargetDependency", "PBXShellScriptBuildPhase",
  19. "PBXResourcesBuildPhase", "PBXApplicationReference",
  20. "PBXExecutableFileReference", "PBXLibraryReference", "PBXToolTarget",
  21. "PBXLibraryTarget", "PBXAggregateTarget", "XCBuildConfiguration",
  22. "XCConfigurationList",
  23. "PBXCopyFilesBuildPhase",
  24. "None"
  25. };
  26. //----------------------------------------------------------------------------
  27. cmXCodeObject::~cmXCodeObject()
  28. {
  29. this->Version = 15;
  30. }
  31. //----------------------------------------------------------------------------
  32. cmXCodeObject::cmXCodeObject(PBXType ptype, Type type)
  33. {
  34. this->Version = 15;
  35. this->Target = 0;
  36. this->Object =0;
  37. this->IsA = ptype;
  38. if(type == OBJECT)
  39. {
  40. // Set the Id of an Xcode object to a unique string for each instance.
  41. // However the Xcode user file references certain Ids: for those cases,
  42. // override the generated Id using SetId().
  43. //
  44. char cUuid[40] = {0};
  45. CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
  46. CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid);
  47. CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8);
  48. this->Id = cUuid;
  49. CFRelease(s);
  50. CFRelease(uuid);
  51. }
  52. else
  53. {
  54. this->Id =
  55. "Temporary cmake object, should not be referred to in Xcode file";
  56. }
  57. cmSystemTools::ReplaceString(this->Id, "-", "");
  58. if(this->Id.size() > 24)
  59. {
  60. this->Id = this->Id.substr(0, 24);
  61. }
  62. this->TypeValue = type;
  63. if(this->TypeValue == OBJECT)
  64. {
  65. this->AddAttribute("isa", 0);
  66. }
  67. }
  68. //----------------------------------------------------------------------------
  69. void cmXCodeObject::Indent(int level, std::ostream& out)
  70. {
  71. while(level)
  72. {
  73. out << "\t";
  74. level--;
  75. }
  76. }
  77. //----------------------------------------------------------------------------
  78. void cmXCodeObject::Print(std::ostream& out)
  79. {
  80. std::string separator = "\n";
  81. int indentFactor = 1;
  82. if(this->Version > 15
  83. && (this->IsA == PBXFileReference || this->IsA == PBXBuildFile))
  84. {
  85. separator = " ";
  86. indentFactor = 0;
  87. }
  88. cmXCodeObject::Indent(2*indentFactor, out);
  89. out << this->Id << " ";
  90. if(!(this->IsA == PBXGroup && this->Comment.size() == 0))
  91. {
  92. this->PrintComment(out);
  93. }
  94. out << " = {";
  95. if(separator == "\n")
  96. {
  97. out << separator;
  98. }
  99. std::map<std::string, cmXCodeObject*>::iterator i;
  100. cmXCodeObject::Indent(3*indentFactor, out);
  101. out << "isa = " << PBXTypeNames[this->IsA] << ";" << separator;
  102. for(i = this->ObjectAttributes.begin();
  103. i != this->ObjectAttributes.end(); ++i)
  104. {
  105. cmXCodeObject* object = i->second;
  106. if(i->first != "isa")
  107. {
  108. cmXCodeObject::Indent(3*indentFactor, out);
  109. }
  110. else
  111. {
  112. continue;
  113. }
  114. if(object->TypeValue == OBJECT_LIST)
  115. {
  116. out << i->first << " = (" << separator;
  117. for(unsigned int k = 0; k < i->second->List.size(); k++)
  118. {
  119. cmXCodeObject::Indent(4*indentFactor, out);
  120. out << i->second->List[k]->Id << " ";
  121. i->second->List[k]->PrintComment(out);
  122. out << "," << separator;
  123. }
  124. cmXCodeObject::Indent(3*indentFactor, out);
  125. out << ");" << separator;
  126. }
  127. else if(object->TypeValue == ATTRIBUTE_GROUP)
  128. {
  129. std::map<std::string, cmXCodeObject*>::iterator j;
  130. out << i->first << " = {" << separator;
  131. for(j = object->ObjectAttributes.begin(); j !=
  132. object->ObjectAttributes.end(); ++j)
  133. {
  134. cmXCodeObject::Indent(4 *indentFactor, out);
  135. if(j->second->TypeValue == STRING)
  136. {
  137. cmXCodeObject::PrintString(out,j->first);
  138. out << " = ";
  139. j->second->PrintString(out);
  140. out << ";";
  141. }
  142. else if(j->second->TypeValue == OBJECT_LIST)
  143. {
  144. cmXCodeObject::PrintString(out,j->first);
  145. out << " = (";
  146. for(unsigned int k = 0; k < j->second->List.size(); k++)
  147. {
  148. if(j->second->List[k]->TypeValue == STRING)
  149. {
  150. j->second->List[k]->PrintString(out);
  151. out << ", ";
  152. }
  153. else
  154. {
  155. out << "List_" << k << "_TypeValue_IS_NOT_STRING, ";
  156. }
  157. }
  158. out << ");";
  159. }
  160. else
  161. {
  162. cmXCodeObject::PrintString(out,j->first);
  163. out << " = 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. cmXCodeObject::PrintString(out,i->first);
  174. out << " = " << object->Object->Id;
  175. if(object->Object->HasComment() && i->first != "remoteGlobalIDString")
  176. {
  177. out << " ";
  178. object->Object->PrintComment(out);
  179. }
  180. out << ";" << separator;
  181. }
  182. else if(object->TypeValue == STRING)
  183. {
  184. cmXCodeObject::PrintString(out,i->first);
  185. out << " = ";
  186. object->PrintString(out);
  187. out << ";" << separator;
  188. }
  189. else
  190. {
  191. out << "what is this?? " << i->first << "\n";
  192. }
  193. }
  194. cmXCodeObject::Indent(2*indentFactor, out);
  195. out << "};\n";
  196. }
  197. //----------------------------------------------------------------------------
  198. void cmXCodeObject::PrintList(std::vector<cmXCodeObject*> const& objs,
  199. std::ostream& out)
  200. {
  201. cmXCodeObject::Indent(1, out);
  202. out << "objects = {\n";
  203. for(unsigned int i = 0; i < objs.size(); ++i)
  204. {
  205. if(objs[i]->TypeValue == OBJECT)
  206. {
  207. objs[i]->Print(out);
  208. }
  209. }
  210. cmXCodeObject::Indent(1, out);
  211. out << "};\n";
  212. }
  213. //----------------------------------------------------------------------------
  214. void cmXCodeObject::CopyAttributes(cmXCodeObject* copy)
  215. {
  216. this->ObjectAttributes = copy->ObjectAttributes;
  217. this->List = copy->List;
  218. this->String = copy->String;
  219. this->Object = copy->Object;
  220. }
  221. //----------------------------------------------------------------------------
  222. void cmXCodeObject::PrintString(std::ostream& os,std::string String)
  223. {
  224. // The string needs to be quoted if it contains any characters
  225. // considered special by the Xcode project file parser.
  226. bool needQuote =
  227. (String.empty() ||
  228. String.find("//") != String.npos ||
  229. String.find_first_of(" <>.+-=@$[],") != String.npos);
  230. const char* quote = needQuote? "\"" : "";
  231. // Print the string, quoted and escaped as necessary.
  232. os << quote;
  233. for(std::string::const_iterator i = String.begin();
  234. i != String.end(); ++i)
  235. {
  236. if(*i == '"')
  237. {
  238. // Escape double-quotes.
  239. os << '\\';
  240. }
  241. os << *i;
  242. }
  243. os << quote;
  244. }
  245. void cmXCodeObject::PrintString(std::ostream& os) const
  246. {
  247. cmXCodeObject::PrintString(os,this->String);
  248. }
  249. //----------------------------------------------------------------------------
  250. void cmXCodeObject::SetString(const std::string& s)
  251. {
  252. this->String = s;
  253. }