Make Xcode identifiers in generated project files be the same after each run.
[wxWidgets.git] / build / osx / makeprojects.applescript
1 global oldDelimiters
2 global variables
3 global variablesRef
4 global bakefilesXML
5 global bakefilesXMLRef
6 global projectFile
7
8 global osxBuildFolder
9
10 property test : false
11
12 -- retrieves the files from the xml node including optional conditions
13 on parseSources(theName, theElement, theVariables, theConditions)
14         set AppleScript's text item delimiters to " "
15         set allElements to {}
16         repeat with currElement in XML contents of theElement
17                 if class of currElement is text then
18                         set allElements to allElements & (every text item of currElement)
19                 else
20                         if class of currElement is XML element and XML tag of currElement is "if" then
21                                 if (cond of XML attributes of currElement is in theConditions) then
22                                         set allElements to allElements & (every text item of (item 1 of XML contents of currElement))
23                                 end if
24                         end if
25                 end if
26         end repeat
27         set AppleScript's text item delimiters to oldDelimiters
28         copy {varname:theName, entries:allElements} to end of theVariables
29 end parseSources
30
31 on parseEntry(theElement, theVariables, theConditions)
32         set theName to var of XML attributes of theElement
33         parseSources(theName, theElement, theVariables, theConditions)
34 end parseEntry
35
36 on parseLib(theElement, theVariables, theConditions)
37         set theName to |id| of XML attributes of theElement
38         repeat with currElement in XML contents of theElement
39                 if class of currElement is XML element and XML tag of currElement is "sources" then
40                         parseSources(theName, currElement, theVariables, theConditions)
41                 end if
42         end repeat
43 end parseLib
44
45 on parseNode(anElement, theVariables, theConditions)
46         if class of anElement is XML element and ¬
47                 XML tag of anElement is "set" then
48                 parseEntry(anElement, theVariables, theConditions)
49         else
50                 if class of anElement is XML element and ¬
51                         XML tag of anElement is "lib" then
52                         parseLib(anElement, theVariables, theConditions)
53                 end if
54         end if
55 end parseNode
56
57 -- iterates through the entire xml tree and populates the variables
58 on parseFiles(theXML, theVariables, theConditions)
59         if class of theXML is XML element or class of theXML is XML document then
60                 repeat with anElement in XML contents of theXML
61                         try
62                                 parseNode(anElement, theVariables, theConditions)
63                         on error number -1728
64                                 -- ignore this error
65                         end try
66                 end repeat
67         else if class of theXML is list then
68                 repeat with anElement in theXML
69                         try
70                                 parseNode(anElement, theVariables, theConditions)
71                         on error number -1728
72                                 -- ignore this error
73                         end try
74                 end repeat
75         end if
76 end parseFiles
77
78 -- gets the entries of the variable named theName
79 on getVar(theName)
80         repeat with anElement in variablesRef
81                 if (varname of anElement is theName) then
82                         return entries of anElement
83                 end if
84         end repeat
85 end getVar
86
87 -- adds sources from fileList to a group named container
88 on addNode(theContainer, fileList)
89         tell application "Xcode"
90                 tell project 1
91                         set theTargets to targets
92                         repeat with listItem in fileList
93                                 if (listItem starts with "$(") then
94                                         set AppleScript's text item delimiters to ""
95                                         set variableName to (characters 3 through ((length of listItem) - 1) of listItem) as text
96                                         set AppleScript's text item delimiters to oldDelimiters
97                                         my addNode(theContainer, my getVar(variableName))
98                                 else
99                                         set AppleScript's text item delimiters to "/"
100                                         set currPath to every text item in listItem
101                                         set currFile to "../../" & listItem
102                                         set currFileName to (item -1 in currPath)
103                                         set currGroup to (items 1 through -2 in currPath as text)
104                                         set AppleScript's text item delimiters to oldDelimiters
105                                         try
106                                                 set theGroup to group theContainer
107                                         on error
108                                                 tell root group
109                                                         make new group with properties {name:theContainer}
110                                                 end tell
111                                         end try
112                                         tell group theContainer
113                                                 try
114                                                         set theGroup to group named currGroup
115                                                 on error
116                                                         make new group with properties {name:currGroup}
117                                                 end try
118                                                 tell group currGroup
119                                                         set newFile to make new file reference with properties {name:currFileName, path:currFile, path type:project relative}
120                                                         repeat with theTarget in theTargets
121                                                                 add newFile to (get compile sources phase of theTarget)
122                                                         end repeat
123                                                 end tell
124                                         end tell
125                                 end if
126                         end repeat
127                 end tell
128         end tell
129 end addNode
130
131 -- retrieves contents of file at posixFilePath
132 on readFile(posixFilePath)
133         set fileRef to open for access POSIX file posixFilePath
134         set theData to read fileRef
135         close access fileRef
136         return theData
137 end readFile
138
139 on init()
140         tell application "Xcode"
141                 quit
142         end tell
143         set variablesRef to a reference to variables
144         set bakefilesXMLRef to a reference to bakefilesXML
145         set oldDelimiters to AppleScript's text item delimiters
146         tell application "Finder"
147                 set osxBuildFolder to POSIX path of ((folder of (path to me)) as Unicode text)
148         end tell
149 end init
150
151 -- reads the files list and evaluates the conditions
152 on readFilesList(theFiles, theConditions)
153         set variables to {}
154         repeat with theFile in theFiles
155                 set bakefilesXML to parse XML (readFile(osxBuildFolder & theFile))
156                 parseFiles(bakefilesXMLRef, variablesRef, theConditions)
157         end repeat
158 end readFilesList
159
160 -- creates a new project file from the respective template
161 on instantiateProject(theProject)
162         set projectName to projectName of theProject
163         set template to POSIX file (osxBuildFolder & projectName & "_in.xcodeproj")
164         set projectFile to POSIX file (osxBuildFolder & projectName & ".xcodeproj")
165         tell application "Finder"
166                 try
167                         delete file projectFile
168                 end try
169                 set duplicateProject to duplicate template with replace
170                 set name of duplicateProject to (projectName & ".xcodeproj")
171         end tell
172 end instantiateProject
173
174 -- adds the source files of the nodes of theProject to the xcode project
175 on populateProject(theProject)
176         tell application "Xcode"
177                 open projectFile
178         end tell
179         repeat with theNode in nodes of theProject
180                 -- reopen xcode for each pass, as otherwise the undomanager
181                 -- happens to crash quite frequently
182                 addNode(label of theNode, entries of theNode)
183         end repeat
184         tell application "Xcode"
185                 quit
186         end tell
187         do shell script (osxBuildFolder as text) & "fix_xcode_ids.py \"" & (POSIX path of projectFile as Unicode text) & "\""
188         -- reopen again to let Xcode sort identifiers
189         tell application "Xcode"
190                 open projectFile
191         end tell
192         tell application "Xcode"
193                 quit
194         end tell
195 end populateProject
196
197 on makeProject(theProject)
198         instantiateProject(theProject)
199         readFilesList(bklfiles of theProject, conditions of theProject)
200         populateProject(theProject)
201 end makeProject
202
203 -- main
204
205 init()
206 set theProject to {conditions:{"PLATFORM_MACOSX=='1'", "TOOLKIT=='OSX_CARBON'", "WXUNIV=='0'", "USE_GUI=='1' and WXUNIV=='0'"}, projectName:¬
207         "wxcarbon", bklfiles:{¬
208         "../bakefiles/files.bkl", "../bakefiles/regex.bkl", "../bakefiles/tiff.bkl", "../bakefiles/png.bkl", "../bakefiles/jpeg.bkl", "../bakefiles/scintilla.bkl", "../bakefiles/expat.bkl"}, nodes:{¬
209         {label:"base", entries:{"$(BASE_SRC)"}}, ¬
210         {label:"base", entries:{"$(BASE_AND_GUI_SRC)"}}, ¬
211         {label:"core", entries:{"$(CORE_SRC)"}}, ¬
212         {label:"net", entries:{"$(NET_SRC)"}}, ¬
213         {label:"adv", entries:{"$(ADVANCED_SRC)"}}, ¬
214         {label:"media", entries:{"$(MEDIA_SRC)"}}, ¬
215         {label:"html", entries:{"$(HTML_SRC)"}}, ¬
216         {label:"xrc", entries:{"$(XRC_SRC)"}}, ¬
217         {label:"xml", entries:{"$(XML_SRC)"}}, ¬
218         {label:"opengl", entries:{"$(OPENGL_SRC)"}}, ¬
219         {label:"aui", entries:{"$(AUI_SRC)"}}, ¬
220         {label:"ribbon", entries:{"$(RIBBON_SRC)"}}, ¬
221         {label:"propgrid", entries:{"$(PROPGRID_SRC)"}}, ¬
222         {label:"richtext", entries:{"$(RICHTEXT_SRC)"}}, ¬
223         {label:"stc", entries:{"$(STC_SRC)"}}, ¬
224         {label:"libtiff", entries:{"$(wxtiff)"}}, ¬
225         {label:"libjpeg", entries:{"$(wxjpeg)"}}, ¬
226         {label:"libpng", entries:{"$(wxpng)"}}, ¬
227         {label:"libregex", entries:{"$(wxregex)"}}, ¬
228         {label:"libscintilla", entries:{"$(wxscintilla)"}}, ¬
229         {label:"libexpat", entries:{"$(wxexpat)"}} ¬
230                 }}
231 makeProject(theProject)
232
233 set theProject to {conditions:{"PLATFORM_MACOSX=='1'", "TOOLKIT=='OSX_COCOA'", "WXUNIV=='0'", "USE_GUI=='1' and WXUNIV=='0'"}, projectName:¬
234         "wxcocoa", bklfiles:{¬
235         "../bakefiles/files.bkl", "../bakefiles/regex.bkl", "../bakefiles/tiff.bkl", "../bakefiles/png.bkl", "../bakefiles/jpeg.bkl", "../bakefiles/scintilla.bkl", "../bakefiles/expat.bkl"}, nodes:{¬
236         {label:"base", entries:{"$(BASE_SRC)"}}, ¬
237         {label:"base", entries:{"$(BASE_AND_GUI_SRC)"}}, ¬
238         {label:"core", entries:{"$(CORE_SRC)"}}, ¬
239         {label:"net", entries:{"$(NET_SRC)"}}, ¬
240         {label:"adv", entries:{"$(ADVANCED_SRC)"}}, ¬
241         {label:"media", entries:{"$(MEDIA_SRC)"}}, ¬
242         {label:"html", entries:{"$(HTML_SRC)"}}, ¬
243         {label:"xrc", entries:{"$(XRC_SRC)"}}, ¬
244         {label:"xml", entries:{"$(XML_SRC)"}}, ¬
245         {label:"opengl", entries:{"$(OPENGL_SRC)"}}, ¬
246         {label:"aui", entries:{"$(AUI_SRC)"}}, ¬
247         {label:"ribbon", entries:{"$(RIBBON_SRC)"}}, ¬
248         {label:"propgrid", entries:{"$(PROPGRID_SRC)"}}, ¬
249         {label:"richtext", entries:{"$(RICHTEXT_SRC)"}}, ¬
250         {label:"stc", entries:{"$(STC_SRC)"}}, ¬
251         {label:"libtiff", entries:{"$(wxtiff)"}}, ¬
252         {label:"libjpeg", entries:{"$(wxjpeg)"}}, ¬
253         {label:"libpng", entries:{"$(wxpng)"}}, ¬
254         {label:"libregex", entries:{"$(wxregex)"}}, ¬
255         {label:"libscintilla", entries:{"$(wxscintilla)"}}, ¬
256         {label:"libexpat", entries:{"$(wxexpat)"}} ¬
257                 }}
258 makeProject(theProject)
259
260 set theProject to {conditions:{"PLATFORM_MACOSX=='1'", "TOOLKIT=='OSX_IPHONE'", "WXUNIV=='0'", "USE_GUI=='1' and WXUNIV=='0'"}, projectName:¬
261         "wxiphone", bklfiles:{¬
262         "../bakefiles/files.bkl", "../bakefiles/regex.bkl", "../bakefiles/tiff.bkl", "../bakefiles/png.bkl", "../bakefiles/jpeg.bkl", "../bakefiles/scintilla.bkl", "../bakefiles/expat.bkl"}, nodes:{¬
263         {label:"base", entries:{"$(BASE_SRC)"}}, ¬
264         {label:"base", entries:{"$(BASE_AND_GUI_SRC)"}}, ¬
265         {label:"core", entries:{"$(CORE_SRC)"}}, ¬
266         {label:"net", entries:{"$(NET_SRC)"}}, ¬
267         {label:"adv", entries:{"$(ADVANCED_SRC)"}}, ¬
268         {label:"media", entries:{"$(MEDIA_SRC)"}}, ¬
269         {label:"html", entries:{"$(HTML_SRC)"}}, ¬
270         {label:"xrc", entries:{"$(XRC_SRC)"}}, ¬
271         {label:"xml", entries:{"$(XML_SRC)"}}, ¬
272         {label:"opengl", entries:{"$(OPENGL_SRC)"}}, ¬
273         {label:"aui", entries:{"$(AUI_SRC)"}}, ¬
274         {label:"ribbon", entries:{"$(RIBBON_SRC)"}}, ¬
275         {label:"propgrid", entries:{"$(PROPGRID_SRC)"}}, ¬
276         {label:"richtext", entries:{"$(RICHTEXT_SRC)"}}, ¬
277         {label:"stc", entries:{"$(STC_SRC)"}}, ¬
278         {label:"libtiff", entries:{"$(wxtiff)"}}, ¬
279         {label:"libjpeg", entries:{"$(wxjpeg)"}}, ¬
280         {label:"libpng", entries:{"$(wxpng)"}}, ¬
281         {label:"libregex", entries:{"$(wxregex)"}}, ¬
282         {label:"libscintilla", entries:{"$(wxscintilla)"}}, ¬
283         {label:"libexpat", entries:{"$(wxexpat)"}} ¬
284                 }}
285 makeProject(theProject)
286
287