cmXCodeObject.cxx 7.8 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. cmXCodeObject::Indent(2*indentFactor, out);
  83. if(this->Version > 15
  84. && (this->IsA == PBXFileReference || this->IsA == PBXBuildFile))
  85. {
  86. separator = " ";
  87. indentFactor = 0;
  88. }
  89. out << this->Id;
  90. this->PrintComment(out);
  91. out << " = {";
  92. if(separator == "\n")
  93. {
  94. out << separator;
  95. }
  96. std::map<std::string, cmXCodeObject*>::iterator i;
  97. cmXCodeObject::Indent(3*indentFactor, out);
  98. out << "isa = " << PBXTypeNames[this->IsA] << ";" << separator;
  99. for(i = this->ObjectAttributes.begin();
  100. i != this->ObjectAttributes.end(); ++i)
  101. {
  102. cmXCodeObject* object = i->second;
  103. if(i->first != "isa")
  104. {
  105. cmXCodeObject::Indent(3*indentFactor, out);
  106. }
  107. else
  108. {
  109. continue;
  110. }
  111. if(object->TypeValue == OBJECT_LIST)
  112. {
  113. out << i->first << " = (" << separator;
  114. for(unsigned int k = 0; k < i->second->List.size(); k++)
  115. {
  116. cmXCodeObject::Indent(4*indentFactor, out);
  117. out << i->second->List[k]->Id;
  118. i->second->List[k]->PrintComment(out);
  119. out << "," << separator;
  120. }
  121. cmXCodeObject::Indent(3*indentFactor, out);
  122. out << ");" << separator;
  123. }
  124. else if(object->TypeValue == ATTRIBUTE_GROUP)
  125. {
  126. std::map<std::string, cmXCodeObject*>::iterator j;
  127. out << i->first << " = {";
  128. if(separator == "\n")
  129. {
  130. out << separator;
  131. }
  132. for(j = object->ObjectAttributes.begin(); j !=
  133. object->ObjectAttributes.end(); ++j)
  134. {
  135. cmXCodeObject::Indent(4 *indentFactor, out);
  136. if(j->second->TypeValue == STRING)
  137. {
  138. cmXCodeObject::PrintString(out,j->first);
  139. out << " = ";
  140. j->second->PrintString(out);
  141. out << ";";
  142. }
  143. else if(j->second->TypeValue == OBJECT_LIST)
  144. {
  145. cmXCodeObject::PrintString(out,j->first);
  146. out << " = (";
  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. cmXCodeObject::PrintString(out,j->first);
  164. out << " = error_unexpected_TypeValue_" <<
  165. j->second->TypeValue << ";";
  166. }
  167. out << separator;
  168. }
  169. cmXCodeObject::Indent(3 *indentFactor, out);
  170. out << "};" << separator;
  171. }
  172. else if(object->TypeValue == OBJECT_REF)
  173. {
  174. cmXCodeObject::PrintString(out,i->first);
  175. out << " = " << object->Object->Id;
  176. if(object->Object->HasComment() && i->first != "remoteGlobalIDString")
  177. {
  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. }