summaryrefslogtreecommitdiff
path: root/make/makeLibs.met
blob: 6debd3ade423a98b015d2bb9cfe1ecf49e06e50d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
-- C libs --

--
-- Properties
--
property pRootFolderName : "ncbi" -- name of the toolkit directory
property pRootFolderPath : "" -- full path of the parent of the toolkit directory. Optional

property pProjectFileExt : ".mcp"

property pSaveContinueOnErrors : false
-- true if we want to save compilation errors in a file and continue.
-- false if we want to stop on an error.

-- Whether to recreate existing project files.
property pAlwaysCreateProjects : false

-- Which targets to build.
-- All of the targets are populated all the time. These affect what is compiled & built.
property pCreateMachOTargets : true
property pCreateCarbonTargets : false
property pCreateWinTargets : false

property pCreateOptimizedTargets : true
property pCreateDebugTargets : false
property pCreateProfiledTargets : false

-- Whether to have the projects use CVS or not.
property pProjectsCVSEnabled : true

-- Whether to compile anything.
property pShouldBuild : true

property pStationeryName : "LibraryStationery"

-- the handler GetTargetSpecs should change if these do.
property pTargetKeyWords : "Mach-O Carbon Win32 Debug Final Profile"


global gStartupDisk
global gHomeDir
global gDistribRoot
global gProjectsDir
global gSourceDir
global gIncludeDir
global gRsrcsDir
global gMakeDir

global gProjectData

on ProjectData(projName, features, aFileData)
	
	if (count characters of projName) > 25 then
		set projName to characters 1 through 25 of projName as string
	end if
	
	if features does not contain "C" and features does not contain "C++" then
		copy "C" to end of features
	end if
	set myFeatures to features
	
	return {name:projName, features:myFeatures, rsrcs:{}, settings:�
		{prefixFile:"", ppcProject:{}}, projLibs:{}, sysPaths:{}, fileData:�
		aFileData}
	
end ProjectData

on AddProject(projData)
	copy projData to end of gProjectData
end AddProject

on AddSimpleProject(projName, features, section)
	AddProject(ProjectData(projName, features, {mkFileData(section)}))
end AddSimpleProject

on AddSimpleProjectListFiles(projName, features, section, fileList)
	AddProject(ProjectData(projName, features, {mkFileDataListFiles(section, fileList)}))
end AddSimpleProjectListFiles

on AddSimpleProjectExceptFiles(projName, features, section, fileNotList)
	AddProject(ProjectData(projName, features, {mkFileDataExceptFiles(section, fileNotList)}))
end AddSimpleProjectExceptFiles

on mkFileData(section)
	if (section does not end with ":") then set section to section & ":"
	return {projPath:section, fileList:GetFileNames(section), fileNotList:{}}
end mkFileData

on mkFileDataListFiles(section, fileList)
	if (section does not end with ":") then set section to section & ":"
	set newFileList to {}
	repeat with i in fileList
		if i does not contain "." then set i to i & ".c"
		copy (contents of i) to end of newFileList
	end repeat
	return {projPath:section, fileList:newFileList, fileNotList:{}}
end mkFileDataListFiles

on mkFileDataExceptFiles(section, fileNotList)
	if (section does not end with ":") then set section to section & ":"
	set theFileList to GetFileNames(section)
	set theRealFileList to {}
	repeat with f in theFileList
		if fileNotList does not contain f then
			copy f to the end of theRealFileList
		end if
	end repeat
	return {projPath:section, fileList:theRealFileList, fileNotList:fileNotList}
end mkFileDataExceptFiles

on DataPlusPrefixFile(dataRec, prefix)
	tell application "CodeWarrior IDE"
		
		set the prefixFile of dataRec's settings to prefix
		return dataRec
		
	end tell
end DataPlusPrefixFile

on GetFileNames(relPath)
	set theFiles to list folder (gSourceDir & relPath) without invisibles
	set theFiles to EndsWith(theFiles, ".c") -- Assume C projects not C++
	
	return theFiles
end GetFileNames

on EndsWith(aList, suffix)
	set newList to {}
	repeat with f in aList
		if (f ends with suffix) then
			copy (f as string) to end of newList
		end if
	end repeat
	return newList
end EndsWith

on SetProjectData()
	
	-- Only needed for Carbon
	AddSimpleProject("mitsock", {}, "connect:mitsock")
	
	AddProject(ProjectData("ncbi", {"winprefix"}, {�
		mkFileDataExceptFiles("corelib:", {"drwnflpt.c", "drwnfspc.c", "drwnmfls.c", "ncbiargs.c", "ncbimain.c"}), �
		mkFileData("corelib:morefile:"), �
		mkFileDataListFiles("connect:", {"ncbibuf", "ncbisock", "urlquery"}), �
		mkFileData("ctools:"), �
		mkFileDataExceptFiles("asnlib:", {"asncode.c", "asntool.c"})}))
	
	AddProject(ProjectData("ncbiobj", {}, {�
		mkFileDataExceptFiles("object:", {"objmime.c"}), �
		mkFileDataExceptFiles("access:", {"strucapi.c"}), �
		mkFileData("api:"), �
		mkFileDataExceptFiles("ddv:", {"ddvmain.c"})}))
	
	AddSimpleProject("vibrant", {"winprefix"}, "vibrant")
	
	AddSimpleProjectExceptFiles("ncbicdr", {}, "cdromlib", {"acccn3ds.c", "accentr.c", "accmmdbs.c", "accutils.c"})
	
	AddSimpleProjectExceptFiles("ncbidesk", {}, "desktop", {"sdisplay.c"})
	
	AddSimpleProject("ddvlib", {}, "ddv")
	
	AddProject(DataPlusPrefixFile(ProjectData("ncbimmdb", {}, {�
		mkFileDataListFiles("biostruc:", {�
			"corematx", "dvncode", "objmmdb1", "objmmdb2", "objmmdb3", "mmdbapi", �
			"mmdbapi1", "mmdbapi2", "mmdbapi3", "mmdbapi4", "mmdbentr", "vastsubs", �
			"prunebsc"}), �
		mkFileDataListFiles("object:", {�
			"objmime"}), �
		mkFileDataListFiles("access:", {�
			"strucapi"}), �
		mkFileDataListFiles("biostruc:cn3d:", {�
			"objcn3d"}), �
		mkFileDataListFiles("biostruc:cdd:", {�
			"objcdd", "cddutil", "cddposutil", �
			"thrdalgs", "thrdatd", "thrdbwfi", "thrdcpal", �
			"thrdcpll", "thrdcprl", "thrdcxei", "thrddgri", "thrdrsmp", �
			"thrdsal0", "thrdsalr", "thrdsalu", "thrdsgoi", "thrdslo0", �
			"thrdslor", "thrdslou", "thrdspci", "thrdspea", "thrdspel", �
			"thrdspni", "thrdttb0", "thrdttbi", "thrdzsc"})}), "gif.pfx"))
	
	AddSimpleProject("ncbiNacc", {}, "cdromlib")
	
	AddSimpleProject("vibnet", {}, "network:vibnet")
	
	AddSimpleProject("ncbitool", {}, "tools")
	
	AddSimpleProject("netentr", {}, "network:entrez:client")
	
	AddSimpleProjectExceptFiles("ncbibls3", {}, "network:blast3:client", {"blastcl3.c"})
	
	AddSimpleProjectExceptFiles("ncbiid1", {}, "network:id1arch", {"idfetch.c"})
	
	AddSimpleProjectExceptFiles("ncbimla", {}, "network:medarch:client", {"sybmed.c"})
	
	AddProject(DataPlusPrefixFile(ProjectData("ncbitxc2", {}, {�
		mkFileDataExceptFiles("network:taxon1:taxon2:", {"txcproc.c"}), �
		mkFileDataExceptFiles("network:taxon1:common:", {"checkid.c"})}), "tax2.pfx"))
	
	AddSimpleProjectListFiles("ncbiconn", {"winprefix"}, "connect", �
		{"ncbi_buffer", "ncbi_socket", "ncbi_core", "ncbi_core_c", "ncbi_util", "ncbi_priv", �
			"ncbi_ansi_ext", "ncbi_connection", "ncbi_connector", "ncbi_connutil", �
			"ncbi_file_connector", "ncbi_host_info.c", "ncbi_http_connector", �
			"ncbi_memory_connector", "ncbi_server_info", "ncbi_service", �
			"ncbi_service_connector", "ncbi_dispd", "ncbi_lbsmd_stub", �
			"ncbi_socket_connector", "ncbi_sendmail"})
	
	AddSimpleProjectExceptFiles("netcli", {}, "network:nsclilib", {"ni_encr.c", "ni_list.c", "ni_lib.c"})
	
	AddSimpleProject("ncbispell", {}, "network:spell:client")

	AddSimpleProject("ncbiblast", {}, "algo:blast:core")
	
	AddSimpleProject("ncbiblastapi", {}, "algo:blast:api")
	
end SetProjectData

--
--  Set Global variables
--

on IsOSX()
	tell application "Finder"
		set vers to the version as text
		if second character of vers is equal to "." then
			set vers to "0" & vers
		end if
		return vers > 10 or vers = 10
	end tell
end IsOSX

on ResolveAlias(pathname)
	tell application "Finder"
		if exists folder pathname then
			-- if pathname does not end with ":" then set pathname to pathname & ":"
			return folder pathname as string
		end if
		if exists alias file pathname then return the original item of alias file pathname as string
	end tell
	return ""
end ResolveAlias

on stripVolName(macPath)
	set saveTID to get text item delimiters of AppleScript
	set text item delimiters of AppleScript to ":"
	set strippedPath to (rest of text items of macPath) as string
	set text item delimiters of AppleScript to saveTID
	return strippedPath
end stripVolName

on HomeDir()
	tell application "Finder"
		if my IsOSX() then
			return the home as string
		else
			return gStartupDisk
		end if
	end tell
end HomeDir

on ModuleRoot()
	set modRoot to ""
	try
		set modRoot to ResolveAlias(pRootFolderPath & pRootFolderName)
	end try
	if modRoot is "" then
		set modRoot to ResolveAlias(gHomeDir & pRootFolderName)
	end if
	return modRoot
end ModuleRoot

on GetMyPath()
	set myPath to path to me as string
	if myPath contains "Script Editor" or �
		myPath contains "osascript" or �
		myPath contains "Smile.app" then
		-- Oops! running under script editor. 'me' is Script Editor not this script.
		-- use the location this script is supposed to be in.
		return gDistribRoot & "make:"
	else
		tell application "Finder"
			return container of myPath as string
		end tell
	end if
end GetMyPath

on SetGlobals()
	tell application "Finder"
		
		set gProjectData to {}
		
		set gStartupDisk to startup disk as string
		set gHomeDir to my HomeDir()
		set gDistribRoot to my ModuleRoot()
		
		set gIncludeDir to gDistribRoot & "include:"
		set gSourceDir to gDistribRoot
		set gProjectsDir to gDistribRoot & "lib:"
		set gRsrcsDir to gDistribRoot & "link:macmet:"
		set gMakeDir to my GetMyPath()
		
	end tell
end SetGlobals

on HeaderExists(header, headerDir)
	tell application "Finder"
		if headerDir is "" then set headerDir to folder gIncludeDir
		return exists file header of headerDir
	end tell
end HeaderExists

on IsOlderThan(fileA, fileB)
	tell application "Finder"
		return (the modification date of fileA < the modification date of fileB)
	end tell
end IsOlderThan

on UpdateRenameHeader(canonicalName, canonicalDir, dotHName, dotHDir)
	tell application "Finder"
		
		set needRename to (dotHName is not "")
		if not needRename then set dotHName to canonicalName
		if dotHDir is "" then set dotHDir to folder gIncludeDir
		
		-- This file is only present if the script was interrupted.
		if needRename and my HeaderExists(canonicalName, dotHDir) then
			delete file canonicalName of dotHDir
		end if
		
		-- The native config file.
		-- If it's missing, assume this is a prepared distribution and the header is already up-to-date.
		if not my HeaderExists(canonicalName, canonicalDir) then return
		set canonicalFile to file canonicalName of canonicalDir
		
		set needCopy to true
		if my HeaderExists(dotHName, dotHDir) then
			set dotHFile to file dotHName of dotHDir
			if my IsOlderThan(dotHFile, canonicalFile) then
				delete dotHFile
			else
				set needCopy to false
			end if
		end if
		
		if needCopy then
			duplicate canonicalFile to dotHDir
			if needRename then set name of file canonicalName of dotHDir to dotHName
		end if
		
	end tell
end UpdateRenameHeader

on CopyHeaders()
	CreateFolder(gIncludeDir)
	tell application "Finder"
		
		set corelibFolder to folder (gDistribRoot & "corelib")
		
		my UpdateRenameHeader("ncbilcl.met", corelibFolder, "ncbilcl.h", "")
		
	end tell
end CopyHeaders


on AppendAccessPath(aPath, isRecursive, isUserPath)
	tell application "CodeWarrior IDE"
		if (aPath does not end with ":") then set aPath to aPath & ":"
		
		set aPath to POSIX path of aPath
		set pathsToAdd to {{name:aPath, recursive:isRecursive, origin:root relative, root:"OS X Volume", format:Unix Path}}
		if isUserPath then
			Set Preferences of panel "Access Paths" to {User Paths:pathsToAdd}
		else
			Set Preferences of panel "Access Paths" to {System Paths:pathsToAdd}
		end if
	end tell
end AppendAccessPath


on AppendPrefixFile(filename)
	tell application "CodeWarrior IDE"
		try -- CW ver. 8
			Set Preferences of panel "C/C++ Compiler" to {Prefix File:prefixFile}
		end try
		try -- CW ver. 9
			set oldtext to Get Preferences of {Prefix Text} from panel "C/C++ Preprocessor"
			set oldtext to (Prefix Text of oldtext)
			set prefixFileInclude to (oldtext & return & "#include \"" & filename & "\"")
			Set Preferences of panel "C/C++ Preprocessor" to {Prefix Text:prefixFileInclude}
		end try
	end tell
end AppendPrefixFile


on SetupTarget(proj, targetIndex)
	tell application "CodeWarrior IDE"
		
		set targetName to name of target targetIndex of project document 1
		if targetName contains "Carbon" then
			set targetAPI to "Carbon"
		else if targetName contains "Mach-O" then
			if not my IsOSX() then return -- do not try to populate Mach-O targets on pre-OS X systems.
			set targetAPI to "Mach-O"
		else if targetName contains "Win32" then
			set targetAPI to "Win32"
		end if
		
		if targetName contains "Debug" then
			set targetDebug to true
			set targetProfile to false
		else if targetName contains "Profile" then
			set targetDebug to false
			set targetProfile to true
		else -- if targeName contains "Final" then
			set targetDebug to false
			set targetProfile to false
		end if
		
		-- NOTE: no Profile targets. if pCreateProfiledTargets, we make the debug targets also profile-able.
		
		set the current target of project document 1 to target targetIndex of project document 1
		
		-- initialize variables
		set projRsrcs to {}
		set projFeatures to {}
		set sysPaths to {}
		
		-- Grab the fields of our project record and store them in local variables.
		set projName to proj's name
		set projSettings to proj's settings
		try
			set projFeatures to proj's features
		end try
		try
			set sysPaths to proj's sysPaths
		end try
		try
			set projRsrcs to proj's rsrcs
		end try
		set projLibs to proj's projLibs
		set projFileData to proj's fileData
		
		-- mitsock library isn't needed (and won't compile) as Mach-O
		if projName is "mitsock" and targetAPI is not "Carbon" then
			return
		end if
		
		-- Figure out what our output file and input libraries will be named.
		set targetFilename to projName
		
		if targetAPI is "Carbon" then
			set targetFilename to targetFilename & "_C"
		else if targetAPI is "Mach-O" then
			set targetFilename to targetFilename & "_M"
		else if targetAPI is "Win32" then
			set targetFilename to targetFilename & "_W"
		end if
		
		-- Debug or not.
		if targetDebug then
			set targetFilename to targetFilename & "_D"
		else if targetProfile then
			set targetFilename to targetFilename & "_P"
		else
			set targetFilename to targetFilename & "_O" -- for Optimized.
		end if
		
		set targetFilename to targetFilename & ".lib"
		
		-- Get Preference Panel names.
		if targetAPI is "Mach-O" then
			set targetPanelName to "PPC Mac OS X Project"
			set codeGenPanelName to "PPC CodeGen Mach-O"
		else if targetAPI is "Win32" then
			set targetPanelName to "x86 Project"
			set codeGenPanelName to "x86 CodeGen"
		else
			set targetPanelName to "PPC Project"
			set codeGenPanelName to "PPC CodeGen"
		end if
		
		-- Set the name of the output file (the application)
		Set Preferences of panel targetPanelName to {File Name:targetFilename}
		
		-- If we have any per-project override settings, set them.
		if (count ppcProject of projSettings) > 0 then
			Set Preferences of panel targetPanelName to ppcProject of projSettings
		end if
		
		
		if targetAPI is "Mach-O" then
			my AppendAccessPath(gDistribRoot, true, false) -- end of system paths.
		else
			my AppendAccessPath(gDistribRoot, true, true) -- end of user paths.
		end if
		
		-- Add per-project system paths.
		set pathsToAdd to {}
		repeat with i in sysPaths
			copy {name:i, recursive:false, origin:absolute} to end of pathsToAdd
		end repeat
		if (count pathsToAdd) > 0 then �
			Set Preferences of panel "Access Paths" to {System Paths:pathsToAdd}
		
		-- Set the appropriate prefix file.
		set prefixFile to prefixFile of projSettings
		if targetAPI is "Win32" and projFeatures contains "winprefix" then
			set prefixFile to "Win32Headers.pch"
		else
			if prefixFile is "" then
				set prefixFile to "CarbonPrefix.h"
			else
				set prefixFile to "Carbon-" & prefixFile
			end if
		end if
		if prefixFile is not "" then
			my AppendPrefixFile(prefixFile)
		end if
		
	end tell
	
	-- Add source files.
	repeat with group in projFileData
		set projPath to projPath of group
		set fileList to group's fileList
		
		set filesToAdd to {}
		repeat with i in fileList
			try -- ignore it if the file doesn't exist.
				
				-- NOTE: if the following line is within a tell "Codewarrior" block then
				--  the following Add Files command will fail.
				copy alias (gSourceDir & projPath & i) to end of filesToAdd
			end try
		end repeat
		
		tell application "CodeWarrior IDE"
			Add Files filesToAdd
			
			-- Take out source files that do not belong.
			--set fileNotList to group's fileNotList
			--Remove Files fileNotList
		end tell
	end repeat
	
	tell application "CodeWarrior IDE"
		
		-- take care of debugging and profiling settings
		if targetDebug or targetProfile then
			set the debug of every target file of target targetIndex �
				of project document 1 to true
		end if
		
		
		if targetAPI is "Carbon" then
			if targetProfile then
				(* ===== Panel PPC Processor ===== *)
				Set Preferences of panel codeGenPanelName to �
					{Use Profiler:true}
			else
				(* ===== Panel PPC Processor ===== *)
				Set Preferences of panel codeGenPanelName to �
					{Use Profiler:false}
			end if
		end if
		
		
		-- Add resources.
		set filesToAdd to {}
		if targetAPI is not "Win32" then
			repeat with i in projRsrcs
				copy gRsrcsDir & i to end of filesToAdd
			end repeat
			
			Add Files filesToAdd
		end if
		
	end tell
end SetupTarget


on GetTargetFiles(i)
	tell application "CodeWarrior IDE"
		-- get the target
		if (i > (count of targets of project document 1)) then
			return {}
		end if
		-- get references to all the targets files
		set atarget to get target i of project document 1
		set trefs to (target files of atarget whose linked is true)
	end tell
	set tfiles to {}
	if ((count of trefs) � 0) then
		repeat with tf in trefs
			-- get locations of the targets files (as file objects)
			tell application "CodeWarrior IDE"
				set f to (location of tf)
			end tell
			tell application "Finder"
				-- convert the file's to strings (colon style paths)
				try
					copy f as string to end of tfiles
				on error errmsg number errnum
					-- file in target but does not exist.
					if errnum � -2753 then -- variable not defined.
						error errmsg number errnum
					end if
				end try
			end tell
		end repeat
	end if
	return tfiles
end GetTargetFiles

on UpdateTarget(proj, targetIndex)
	
	tell application "CodeWarrior IDE"
		
		set targetName to name of target targetIndex of project document 1
		if targetName contains "Carbon" then
			set targetAPI to "Carbon"
		else if targetName contains "Mach-O" then
			if not my IsOSX() then return -- do not try to populate Mach-O targets on pre-OS X systems.
			set targetAPI to "Mach-O"
		else if targetName contains "Win32" then
			set targetAPI to "Win32"
		end if
		
		-- Grab the fields of our project record and store them in local variables.
		set projName to proj's name
		
		-- mitsock library isn't needed (and won't compile) except under Carbon.
		if projName is "mitsock" and targetAPI is not "Carbon" then
			return
		end if
		
		set targetName to name of target targetIndex of project document 1
		
		set the current target of project document 1 to target targetIndex of project document 1
		
		-- initialize variables
		set projFeatures to {}
		
		-- Grab the fields of our project record and store them in local variables.
		try
			set projFeatures to proj's features
		end try
		set projFileData to proj's fileData
		
	end tell
	
	-- Update source files
	set targetFileList to my GetTargetFiles(targetIndex)
	repeat with group in projFileData
		set projPath to projPath of group -- projPath is relative.
		
		set fileList to group's fileList
		
		set filesToAdd to {}
		
		repeat with i in fileList
			set fileFullPath to gSourceDir & projPath & i
			set found to targetFileList contains fileFullPath
			if (not found) then
				try -- ignore it if the file doesn't exist.
					
					-- NOTE: if the following line is within a tell "Codewarrior" block then
					--  the following Add Files command will fail.
					
					copy alias (fileFullPath) to end of filesToAdd
				end try
			end if
		end repeat
		
		tell application "CodeWarrior IDE"
			if ((count of filesToAdd) is not 0) then
				Add Files filesToAdd
				set targetFileList to my GetTargetFiles(targetIndex)
			end if
			
			-- Take out source files that do not belong.
			--set fileNotList to group's fileNotList
			--Remove Files fileNotList
		end tell
	end repeat
	
end UpdateTarget

on CreateFolder(folderPath)
	set text item delimiters of AppleScript to ":"
	-- strip off disk name.
	set folderPath to (rest of text items of folderPath) as string
	set pathSoFar to ""
	tell application "Finder"
		repeat with f in (text items of folderPath)
			set longerPath to pathSoFar & ":" & f
			if not (exists folder (longerPath)) then
				make new folder at folder (pathSoFar) with properties {name:f}
			end if
			set pathSoFar to longerPath
		end repeat
	end tell
	set text item delimiters of AppleScript to ""
end CreateFolder

on GetStationeryName(proj)
	set stationeryDir to gProjectsDir & pStationeryName & ":"
	set stationeryName to pStationeryName & pProjectFileExt
	
	CreateFolder(stationeryDir)
	tell application "Finder"
		-- does the Library Stationery already exist?
		if not (exists file (stationeryDir & stationeryName)) then
			-- copy it.
			duplicate file (stationeryName) of folder gRsrcsDir to folder stationeryDir
		end if
	end tell
	return (stationeryDir & stationeryName)
end GetStationeryName

on CreateProject(proj)
	tell application "CodeWarrior IDE"
		try
			get proj's name
		on error
			return
		end try
		
		set projFilename to proj's name & pProjectFileExt
		set projPathname to gProjectsDir & projFilename
		if pAlwaysCreateProjects or not (exists file projPathname of application "Finder") then
			
			try
				close (the first project document whose name is projFilename)
			end try
			
			set stationeryName to my GetStationeryName(proj)
			
			Create Project projPathname from stationery alias stationeryName
			if the name of window 1 is "Project Messages" then
				close first window -- "close window 1" becomes "Close Window 1" (different event)
			end if
			
			repeat with i from 1 to (count targets of project document 1)
				my SetupTarget(proj, i)
			end repeat
		else
			-- project already exists. Make sure it has all right files.
			open (projPathname)
			if the name of window 1 is "Project Messages" then
				close first window -- "close window 1" becomes "Close Window 1" (different event)
			end if
			repeat with i from 1 to (count targets of project document 1)
				my UpdateTarget(proj, i)
			end repeat
		end if
		if pProjectsCVSEnabled then
			--try
			Set Preferences of panel "VCS Setup" to {VCS Active:true, Connection Method:"mwCVS"}
			--end try
		end if
		set the current target of project document 1 to target 1 of project document 1
		Close Project
		
	end tell
end CreateProject

on CreateAllProjects()
	CreateFolder(gProjectsDir)
	CleanupFiles(gProjectsDir)
	
	repeat with proj in gProjectData
		CreateProject(proj)
	end repeat
	
end CreateAllProjects

(*
	a target spec is a string of words "MSL Debug", all of which appear on our list of keywords.
	Convert a Build file name to a target spec by filtering out non-keywords.
	Each Build file creates another target spec.
	A project target must match one of the target specs to compile.
	To match, a target's name must contain each of the words in the target spec.
	An empty target spec matches everything.
	An empty list of target specs (no Build files) matches nothing, 
	But if there are no Build files we make target specs based on
	the script parameters.
*)

on GetTargetSpecs()
	set theFiles to (list folder gMakeDir without invisibles)
	set targetSpecs to {}
	repeat with f in theFiles
		if (f begins with "Build ") then
			set targetSpecs to targetSpecs & MakeTargetSpec(f)
		end if
	end repeat
	-- no Build files? match what the script parameters say to match.
	-- NOTE: This is dependent on pTargetKeyWords and the target names in the stationery.
	if (count items of targetSpecs) is 0 then
		set debugspec to ""
		if pCreateDebugTargets then
			if pCreateMachOTargets then copy ("Mach-O Debug") to end of targetSpecs
			if pCreateCarbonTargets then copy ("Carbon Debug") to end of targetSpecs
			if pCreateWinTargets then copy ("Win32 Debug") to end of targetSpecs
		end if
		if pCreateProfiledTargets then
			if pCreateMachOTargets then copy ("Mach-O Profile") to end of targetSpecs
			if pCreateCarbonTargets then copy ("Carbon Profile") to end of targetSpecs
			if pCreateWinTargets then copy ("Win32 Profile") to end of targetSpecs
		end if
		if pCreateOptimizedTargets then
			if pCreateMachOTargets then copy ("Mach-O Final") to end of targetSpecs
			if pCreateCarbonTargets then copy ("Carbon Final") to end of targetSpecs
			if pCreateWinTargets then copy ("Win32 Final") to end of targetSpecs
		end if
	end if
	return targetSpecs
end GetTargetSpecs

on MakeTargetSpec(f)
	set tspec to ""
	repeat with w in (words of f)
		if pTargetKeyWords contains w then
			set tspec to tspec & w & " "
		end if
	end repeat
	return tspec
end MakeTargetSpec

on OkaytoBuild(targetName, targetSpecs)
	--This target name must match at least one of the target specs.
	repeat with ts in targetSpecs
		if MatchSpec2Target(ts, targetName) then return true
	end repeat
	return false
end OkaytoBuild

on MatchSpec2Target(targSpec, targName)
	-- the targetname must contain all of the words in the target spec.
	repeat with w in (words of targSpec)
		if w is not in targName then return false
	end repeat
	return true
end MatchSpec2Target

on BuildProject(projName, targetSpecs)
	tell application "CodeWarrior IDE"
		open (gProjectsDir & projName & pProjectFileExt)
		if the name of window 1 is "Project Messages" then
			close first window -- "close window 1" becomes "Close Window 1" (different event)
		end if
		repeat with i from 1 to (count targets of project document 1)
			-- do we want to build this target?
			set thisTarget to name of target i of project document 1
			if my OkaytoBuild(thisTarget, targetSpecs) then
				
				set the current target of project document 1 to target i of project document 1
				if pSaveContinueOnErrors then
					try
						Make Project
					on error errmsg number errnum
						if (errnum = 5) then
							set errFileName to (gProjectsDir & projName & "-" & i & ".errs")
							Save Error Window As (file errFileName)
							close first window
						else
							error errmsg number errnum
						end if
					end try
				else -- stop on any error.
					Make Project
				end if
				-- If there were compiler warnings, then a compiler window will be in front.
				-- For whatever reason, this causes the next "set the current target..." to fail.
				-- So check for the window and close it.
				if the name of window 1 is "Errors & Warnings" then
					close first window -- "close window 1" becomes "Close Window 1" (different event)
				end if
			end if
		end repeat
		set the current target of project document 1 to target 1 of project document 1
		Close Project
	end tell
end BuildProject

on BuildAllProjects()
	set targetSpecs to GetTargetSpecs()
	repeat with proj in gProjectData
		try
			set projName to proj's name
		on error
			set projName to ""
		end try
		if projName is not "" then
			BuildProject(projName, targetSpecs)
		end if
	end repeat
	
end BuildAllProjects

on CleanupFiles(thePath)
	-- get rid of all the files and folders starting with xxxx in thePath
	repeat with f in list folder (thePath) without invisibles
		if ((f as string) begins with "xxxx") then
			try
				tell application "Finder" to delete folder (thePath & f)
			end try
			try
				tell application "Finder" to delete file (thePath & f)
			end try
		end if
	end repeat
	-- delete the stationery folder.
	try
		tell application "Finder" to delete folder (thePath & pStationeryName)
	end try
end CleanupFiles

on SignalCompletion()
	beep
end SignalCompletion

with timeout of 60000 seconds
	
	SetGlobals()
	SetProjectData()
	CopyHeaders()
	
	tell application "CodeWarrior IDE" to activate
	
	CreateAllProjects()
	
	if pShouldBuild then
		BuildAllProjects()
	end if
	
	CleanupFiles(gProjectsDir)
	
	SignalCompletion()
	
end timeout