fix_xcode_ids.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/python
  2. ###############################################################################
  3. # Name: build/osx/fix_xcode_ids.py
  4. # Author: Dimitri Schoolwerth
  5. # Created: 2010-09-08
  6. # Copyright: (c) 2010 wxWidgets team
  7. # Licence: wxWindows licence
  8. ###############################################################################
  9. testFixStage = False
  10. import os
  11. import sys
  12. import re
  13. USAGE = """fix_xcode_ids - Modifies an Xcode project in-place to use the same identifiers (based on name) instead of being different on each regeneration"
  14. Usage: fix_xcode_ids xcode_proj_dir"""
  15. if not testFixStage:
  16. if len(sys.argv) < 2:
  17. print USAGE
  18. sys.exit(1)
  19. projectFile = sys.argv[1] + "/project.pbxproj"
  20. fin = open(projectFile, "r")
  21. strIn = fin.read()
  22. fin.close()
  23. # Xcode identifiers (IDs) consist of 24 hexadecimal digits
  24. idMask = "[A-Fa-f0-9]{24}"
  25. idDict = {}
  26. # convert a name to an identifier for Xcode
  27. def toUuid(name):
  28. from uuid import uuid3, UUID
  29. id = uuid3(UUID("349f853c-91f8-4eba-b9b9-5e9f882e693c"), name).hex[:24].upper()
  30. # Some names can appear twice or even more (depending on number of
  31. # targets), make them unique
  32. while id in idDict.values() :
  33. id = "%024X" % (int(id, 16) + 1)
  34. return id
  35. def insertBuildFileEntry(filePath, fileRefId):
  36. global strIn
  37. print "\tInsert PBXBuildFile for '%s'..." % filePath,
  38. matchBuildFileSection = re.search("/\* Begin PBXBuildFile section \*/\n", strIn)
  39. dirName, fileName = os.path.split(filePath)
  40. fileInSources = fileName + " in Sources"
  41. id = toUuid(fileInSources)
  42. idDict[id] = id
  43. insert = "\t\t%s /* %s */ = {isa = PBXBuildFile; fileRef = %s /* %s */; };\n" % (id, fileInSources, fileRefId, fileName)
  44. strIn = strIn[:matchBuildFileSection.end()] + insert + strIn[matchBuildFileSection.end():]
  45. print "OK"
  46. return id
  47. def insertFileRefEntry(filePath, id = 0):
  48. global strIn
  49. print "\tInsert PBXFileReference for '%s'..." % filePath,
  50. matchFileRefSection = re.search("/\* Begin PBXFileReference section \*/\n", strIn)
  51. dirName, fileName = os.path.split(filePath)
  52. if id == 0:
  53. id = toUuid(fileName)
  54. idDict[id] = id
  55. insert = "\t\t%s /* %s */ = {isa = PBXFileReference; lastKnownFileType = file; name = %s; path = %s; sourceTree = \"<group>\"; };\n" % (id, fileName, fileName, filePath)
  56. strIn = strIn[:matchFileRefSection.end()] + insert + strIn[matchFileRefSection.end():]
  57. print "OK"
  58. return id
  59. def insertSourcesBuildPhaseEntry(id, fileName, insertBeforeFileName, startSearchPos = 0):
  60. global strIn
  61. print "\tInsert PBXSourcesBuildPhase for '%s'..." % fileName,
  62. matchBuildPhase = re.compile(".+ /\* " + insertBeforeFileName + " in Sources \*/,") \
  63. .search(strIn, startSearchPos)
  64. insert = "\t\t\t\t%s /* %s in Sources */,\n" % (id, fileName)
  65. strIn = strIn[:matchBuildPhase.start()] \
  66. + insert \
  67. + strIn[matchBuildPhase.start():]
  68. print "OK"
  69. return matchBuildPhase.start() + len(insert) + len(matchBuildPhase.group(0))
  70. # Detect and fix errors in the project file that might have been introduced.
  71. # Sometimes two source files are concatenated. These are spottable by
  72. # looking for patterns such as "filename.cppsrc/html/"
  73. # Following is a stripped Xcode project containing several problems that
  74. # are solved after finding the error.
  75. strTest = \
  76. """/* Begin PBXBuildFile section */
  77. 95DE8BAB1238EE1800B43069 /* m_fonts.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95DE8BAA1238EE1700B43069 /* m_fonts.cpp */; };
  78. 95DE8BAC1238EE1800B43069 /* m_fonts.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 95DE8BAA1238EE1700B43069 /* m_fonts.cpp */; };
  79. /* End PBXBuildFile section */
  80. /* Begin PBXFileReference section */
  81. 95DE8BAA1238EE1700B43069 /* m_fonts.cpp */ = {isa = PBXFileReference; lastKnownFileType = file; name = m_fonts.cpp; path = ../../src/html/m_dflist.cppsrc/html/m_fonts.cpp; sourceTree = "<group>"; };
  82. /* End PBXFileReference section */
  83. /* Begin PBXGroup section */
  84. 95DE8B831238EE1000B43069 /* html */ = {
  85. isa = PBXGroup;
  86. children = (
  87. 95DE8B841238EE1000B43069 /* src/html */,
  88. 95DE8BA91238EE1700B43069 /* src/html/m_dflist.cppsrc/html */,
  89. 95DE8BCE1238EE1F00B43069 /* src/generic */,
  90. );
  91. name = html;
  92. sourceTree = "<group>";
  93. };
  94. 95DE8B841238EE1000B43069 /* src/html */ = {
  95. isa = PBXGroup;
  96. children = (
  97. 95DE8B851238EE1000B43069 /* chm.cpp */,
  98. 95DE8BAD1238EE1800B43069 /* m_hline.cpp */,
  99. );
  100. name = src/html;
  101. sourceTree = "<group>";
  102. };
  103. 95DE8BA91238EE1700B43069 /* src/html/m_dflist.cppsrc/html */ = {
  104. isa = PBXGroup;
  105. children = (
  106. 95DE8BAA1238EE1700B43069 /* m_fonts.cpp */,
  107. );
  108. name = src/html/m_dflist.cppsrc/html;
  109. sourceTree = "<group>";
  110. };
  111. /* End PBXGroup section */
  112. /* Begin PBXSourcesBuildPhase section */
  113. 404BEE5E10EC83280080E2B8 /* Sources */ = {
  114. files = (
  115. 95DE8BAC1238EE1800B43069 /* m_fonts.cpp in Sources */,
  116. );
  117. runOnlyForDeploymentPostprocessing = 0;
  118. };
  119. D2AAC0C405546C1D00DB518D /* Sources */ = {
  120. files = (
  121. 95DE8BAB1238EE1800B43069 /* m_fonts.cpp in Sources */,
  122. );
  123. runOnlyForDeploymentPostprocessing = 0;
  124. };
  125. /* End PBXSourcesBuildPhase section */"""
  126. if testFixStage:
  127. strIn = strTest
  128. rc = re.compile(".+ (?P<path1>[\w/.]+(\.cpp|\.cxx|\.c))(?P<path2>\w+/[\w/.]+).+")
  129. matchLine = rc.search(strIn)
  130. while matchLine:
  131. line = matchLine.group(0)
  132. # is it a line from the PBXFileReference section containing 2 mixed paths?
  133. # example:
  134. # FEDCBA9876543210FEDCBA98 /* file2.cpp */ = {isa = PBXFileReference; lastKnownFileType = file; name = file2.cpp; path = ../../src/html/file1.cppsrc/html/file2.cpp; sourceTree = "<group>"; };
  135. if line.endswith("};") :
  136. path1 = matchLine.group('path1')
  137. path2 = matchLine.group('path2')
  138. print "Correcting mixed paths '%s' and '%s' at '%s':" % (path1, path2, line)
  139. # if so, make note of the ID used (belongs to path2), remove the line
  140. # and split the 2 paths inserting 2 new entries inside PBXFileReference
  141. fileRefId2 = re.search(idMask, line).group(0)
  142. print "\tDelete the offending PBXFileReference line...",
  143. # delete the PBXFileReference line that was found and which contains 2 mixed paths
  144. strIn = strIn[:matchLine.start()] + strIn[matchLine.end()+1:]
  145. print "OK"
  146. # insert corrected path1 entry in PBXFileReference
  147. fileRefId1 = insertFileRefEntry(path1)
  148. # do the same for path2 (which already had a ID)
  149. path2Corrected = path2
  150. if path2Corrected.startswith('src') :
  151. path2Corrected = '../../' + path2Corrected
  152. insertFileRefEntry(path2Corrected, fileRefId2)
  153. buildPhaseId = {}
  154. # insert a PBXBuildFile entry, 1 for each target
  155. # path2 already has correct PBXBuildFile entries
  156. targetCount = strIn.count("isa = PBXSourcesBuildPhase")
  157. for i in range(0, targetCount):
  158. buildPhaseId[i] = insertBuildFileEntry(path1, fileRefId1)
  159. fileName1 = os.path.split(path1)[1]
  160. dir2, fileName2 = os.path.split(path2)
  161. # refer to each PBXBuildFile in each PBXSourcesBuildPhase
  162. startSearchIndex = 0
  163. for i in range(0, targetCount):
  164. startSearchIndex = insertSourcesBuildPhaseEntry(buildPhaseId[i], fileName1, fileName2, startSearchIndex)
  165. # insert both paths in the group they belong to
  166. matchGroupStart = re.search("/\* %s \*/ = {" % dir2, strIn)
  167. endGroupIndex = strIn.find("};", matchGroupStart.start())
  168. for matchGroupLine in re.compile(".+" + idMask + " /\* (.+) \*/,").finditer(strIn, matchGroupStart.start(), endGroupIndex) :
  169. if matchGroupLine.group(1) > fileName1:
  170. print "\tInsert paths in PBXGroup '%s', just before '%s'..." % (dir2, matchGroupLine.group(1)),
  171. strIn = strIn[:matchGroupLine.start()] \
  172. + "\t\t\t\t%s /* %s */,\n" % (fileRefId1, fileName1) \
  173. + "\t\t\t\t%s /* %s */,\n" % (fileRefId2, fileName2) \
  174. + strIn[matchGroupLine.start():]
  175. print "OK"
  176. break
  177. elif line.endswith("*/ = {") :
  178. print "Delete invalid PBXGroup starting at '%s'..." % line,
  179. find = "};\n"
  180. endGroupIndex = strIn.find(find, matchLine.start()) + len(find)
  181. strIn = strIn[:matchLine.start()] + strIn[endGroupIndex:]
  182. print "OK"
  183. elif line.endswith(" */,") :
  184. print "Delete invalid PBXGroup child '%s'..." % line,
  185. strIn = strIn[:matchLine.start()] + strIn[matchLine.end()+1:]
  186. print "OK"
  187. matchLine = rc.search(strIn)
  188. if testFixStage:
  189. print "------------------------------------------"
  190. print strIn
  191. exit(1)
  192. # key = original ID found in project
  193. # value = ID it will be replaced by
  194. idDict = {}
  195. # some of the strings to match to find definitions of Xcode IDs:
  196. # from PBXBuildFile section:
  197. # 0123456789ABCDEF01234567 /* filename.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEDCBA9876543210FEDCBA98 /* filename.cpp */; };
  198. # from PBXFileReference section:
  199. # FEDCBA9876543210FEDCBA98 /* filename.cpp */ = {isa = PBXFileReference; lastKnownFileType = file; name = any.cpp; path = ../../src/common/filename.cpp; sourceTree = "<group>"; };
  200. # from remaining sections:
  201. # 890123456789ABCDEF012345 /* Name */ = {
  202. # Capture the first comment between /* and */ (file/section name) as a group
  203. rc = re.compile("\s+(" + idMask + ") /\* (.+) \*/ = {.*$", re.MULTILINE)
  204. dict = rc.findall(strIn)
  205. for s in dict:
  206. # s[0] is the original ID, s[1] is the name
  207. assert(not s[0] in idDict)
  208. idDict[s[0]] = toUuid(s[1])
  209. # replace all found identifiers with the new ones
  210. def repl(match):
  211. return idDict[match.group(0)]
  212. strOut = re.sub(idMask, repl, strIn)
  213. fout = open(projectFile, "w")
  214. fout.write(strOut)
  215. fout.close()