Make Xcode identifiers in generated project files be the same after each run.
[wxWidgets.git] / build / osx / fix_xcode_ids.py
1 #!/usr/bin/python
2
3 ###############################################################################
4 # Name: build/osx/fix_xcode_ids.py
5 # Author: Dimitri Schoolwerth
6 # Created: 2010-09-08
7 # RCS-Id: $Id$
8 # Copyright: (c) 2010 wxWidgets team
9 # Licence: wxWindows licence
10 ###############################################################################
11
12 import sys
13 import re
14 import uuid
15
16 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"
17 Usage: fix_xcode_ids xcode_proj_dir"""
18
19 if len(sys.argv) < 2:
20 print USAGE
21 sys.exit(1)
22
23 projectFile = sys.argv[1] + "/project.pbxproj"
24
25 fin = open(projectFile, "r")
26 strIn = fin.read()
27 fin.close()
28
29
30 # Xcode identifiers (IDs) consist of 24 hexadecimal digits
31 idMask = "[A-Fa-f0-9]{24}"
32
33 # key = original ID found in project
34 # value = ID it will be replaced by
35 idDict = {}
36
37 # some of the strings to match to find definitions of Xcode IDs:
38
39 # from PBXBuildFile section:
40 # 0123456789ABCDEF01234567 /* filename.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FEDCBA9876543210FEDCBA98 /* filename.cpp */; };
41
42 # from PBXFileReference section:
43 # FEDCBA9876543210FEDCBA98 /* filename.cpp */ = {isa = PBXFileReference; lastKnownFileType = file; name = any.cpp; path = ../../src/common/filename.cpp; sourceTree = "<group>"; };
44
45 # from remaining sections:
46 # 890123456789ABCDEF012345 /* Name */ = {
47
48 # Capture the first comment between /* and */ (file/section name) as a group
49 rc = re.compile("\s+(" + idMask + ") /\* (.+) \*/ = {.*$", re.MULTILINE)
50 dict = rc.findall(strIn)
51
52 # convert a name to an identifier for Xcode
53 def toUuid(name):
54 return uuid.uuid3(uuid.NAMESPACE_DNS, name).hex[:24].upper()
55
56 for s in dict:
57 # s[0] is the original ID, s[1] is the name
58 newId = toUuid(s[1])
59 num = 0
60 # Some names can appear twice or even more (depending on number of
61 # targets), make them unique
62 while newId in idDict.values() :
63 num = num + 1
64 newId = toUuid(s[1] + str(num))
65
66 assert(not s[0] in idDict)
67 idDict[s[0]] = newId
68
69
70 # replace all found identifiers with the new ones
71 def repl(match):
72 return idDict[match.group(0)]
73
74 strOut = re.sub(idMask, repl, strIn)
75
76 fout = open(projectFile, "w")
77 fout.write(strOut)
78 fout.close()