1-----------------------------------------------------------------------------
2-- Name:        docs/mac/M5build.applescript
3-- Purpose:     Automatic build of projects with CodeWarrior 5
4-- Author:      Gilles Depeyrot
5-- Modified by:
6-- Created:     06.10.2001
7-- RCS-ID:      $Id: M5build.applescript 12831 2001-12-02 20:02:17Z GD $
8-- Copyright:   (c) 2001 Gilles Depeyrot
9-- Licence:     wxWindows licence
10-----------------------------------------------------------------------------
11--
12-- This AppleScript automatically recurses through the selected folder looking for
13-- and building CodeWarrior projects.
14-- To use this script, simply open it with the 'Script Editor' and run it.
15--
16
17--
18-- Suffix used to recognize CodeWarrior project files
19--
20property gProjectSuffix : "M5.mcp"
21
22--
23-- Values used to create the log file
24--
25property gEol : "
26"
27property gSeparator : "-------------------------------------------------------------------------------" & gEol
28
29--
30-- Project and build success count
31--
32set theProjectCount to 0
33set theProjectSuccessCount to 0
34
35--
36-- Default log file name
37--
38set theDate to (day of (current date)) & "/" & GetMonthIndex(current date) & "/" & (year of (current date))
39set theLogFileName to "build-" & theDate & ".log"
40
41--
42-- Ask the user to select the wxWindows samples folder
43--
44set theFolder to choose folder with prompt "Select the folder in which to build the projects"
45
46--
47-- Ask the user to choose the build log file
48--
49set theLogFile to choose file name with prompt "Save the build log file" default name theLogFileName
50
51--
52-- Open the log file to record the build log
53--
54set theLogFileRef to open for access theLogFile with write permission
55
56--
57-- Write log file header
58--
59write gSeparator starting at 0 to theLogFileRef
60write "Build log" & gEol to theLogFileRef
61write gSeparator to theLogFileRef
62write "start on " & ((current date) as string) & gEol to theLogFileRef
63write gSeparator to theLogFileRef
64write "building projects in '" & (theFolder as string) & "'" & gEol to theLogFileRef
65write gSeparator to theLogFileRef
66
67--
68-- Build or Rebuild targets?
69--
70set theText to "Build or rebuild projects?"
71set theBuild to button returned of (display dialog theText buttons {"Cancel", "Build", "Rebuild"} default button "Rebuild" with icon note)
72if theBuild is not equal to "Cancel" then
73	--
74	-- Build which targets?
75	--
76	set theText to theBuild & " Classic or Carbon targets?"
77	set theType to button returned of (display dialog theText buttons {"Cancel", "Classic", "Carbon"} default button "Carbon" with icon note)
78	if theType is not equal to "Cancel" then
79		--
80		-- Build Debug or Release targets?
81		--
82		set theText to theBuild & " " & theType & " Debug or " & theType & " Release targets?"
83		set theOption to button returned of (display dialog theText buttons {"Cancel", "Release", "Debug"} default button "Debug" with icon note)
84		if theOption is not equal to "Cancel" then
85			set theTarget to theType & " " & theOption
86			
87			write "building project targets '" & theTarget & "'" & gEol to theLogFileRef
88			write gSeparator to theLogFileRef
89			
90			BuildProjects(theLogFileRef, theFolder, theTarget, theBuild is equal to "Rebuild")
91			
92		end if
93	end if
94end if
95
96--
97-- Write log file footer
98--
99write "successful build of " & theProjectSuccessCount & " projects out of " & theProjectCount & gEol to theLogFileRef
100write gSeparator to theLogFileRef
101write "end on " & ((current date) as string) & gEol to theLogFileRef
102write gSeparator to theLogFileRef
103--
104-- Close the log file
105--
106close access theLogFileRef
107
108--
109-- BuildProjects
110--
111on BuildProjects(inLogFileRef, inFolder, inTarget, inRebuild)
112	global theProjectCount, theProjectSuccessCount
113	
114	tell application "Finder" to update inFolder
115	
116	try
117		tell application "Finder" to set theProject to ((the first file of inFolder whose name ends with gProjectSuffix) as string)
118	on error
119		set theProject to ""
120	end try
121	
122	if theProject is not "" then
123		set theProjectCount to theProjectCount + 1
124		
125		write "building project '" & theProject & "'" & gEol to inLogFileRef
126		
127		tell application "CodeWarrior IDE 4.0.4"
128			--
129			-- Open the project in CodeWarrior
130			--
131			open theProject
132			--
133			-- Change to the requested target
134			--
135			Set Current Target inTarget
136			--
137			-- Remove object code if rebuild requested
138			--
139			if inRebuild then
140				Remove Binaries
141			end if
142			--
143			-- Build/Rebuild the selected target
144			--
145			set theBuildInfo to Make Project with ExternalEditor
146			--
147			-- Close the project
148			--
149			Close Project
150		end tell
151		--
152		-- Report errors to build log file
153		--
154		write gEol to inLogFileRef
155		ReportBuildInfo(inLogFileRef, theBuildInfo)
156		write gSeparator to inLogFileRef
157	end if
158	
159	tell application "Finder" to set theSubFolders to every folder of inFolder whose name does not end with " Data"
160	repeat with theFolder in theSubFolders
161		BuildProjects(inLogFileRef, theFolder, inTarget, inRebuild)
162	end repeat
163	
164end BuildProjects
165
166--
167-- ReportBuildInfo
168--
169on ReportBuildInfo(inLogFileRef, inBuildInfo)
170	global theProjectCount, theProjectSuccessCount
171	
172	set theErrorCount to 0
173	set theWarningCount to 0
174	
175	repeat with theInfo in inBuildInfo
176		tell application "CodeWarrior IDE 4.0.4"
177			set theKind to ((messageKind of theInfo) as string)
178			
179			tell me to write "*** " & theKind & " *** " & message of theInfo & gEol to inLogFileRef
180			try
181				set theFile to ((file of theInfo) as string)
182			on error
183				set theFile to ""
184			end try
185			if theFile is not "" then
186				tell me to write theFile & " line " & lineNumber of theInfo & gEol to inLogFileRef
187			end if
188			tell me to write gEol to inLogFileRef
189		end tell
190		
191		if MessageKindIsError(theKind) then
192			set theErrorCount to theErrorCount + 1
193		else
194			set theWarningCount to theWarningCount + 1
195		end if
196	end repeat
197	
198	if theErrorCount is 0 then
199		set theProjectSuccessCount to theProjectSuccessCount + 1
200		write "build succeeded with " & theWarningCount & " warning(s)" & gEol to inLogFileRef
201	else
202		write "build failed with " & theErrorCount & " error(s) and " & theWarningCount & " warning(s)" & gEol to inLogFileRef
203	end if
204end ReportBuildInfo
205
206--
207-- MessageKindIsError
208--
209on MessageKindIsError(inKind)
210	if inKind is "compiler error" or inKind is "linker error" or inKind is "generic error" then
211		return true
212	else
213		return false
214	end if
215end MessageKindIsError
216
217--
218-- GetMonthIndex
219--
220on GetMonthIndex(inDate)
221	set theMonth to the month of inDate
222	set theMonthList to {January, February, March, April, May, June, July, August, September, October, November, December}
223	repeat with i from 1 to the number of items in theMonthList
224		if theMonth is item i of theMonthList then
225			return i
226		end if
227	end repeat
228end GetMonthIndex
229