#
#	File:		Build
#
#	Contains:	The main build script for building Universal ROMs.
#
#	Copyright:	© 1989-1993 by Apple Computer, Inc., all rights reserved.
#
#	Change History (most recent first):
#
#	   <SM3>	 9/21/93	kc		Remove -symbols option. It is now turned on by default in the
#									various makefiles.
#	   <SM2>	 3/31/93	kc		Update -Symbols option and move makeout file into the new text
#									folder.
#		 <1>	 2/21/93	kc		first checked in
#	  <SM43>	 2/11/93	CSS		Rollin build script support for DBlite
#	  <SM42>	 12/8/92	RC		Added Build RISC feature (similar to the way Build LC930 works)
#	  <SM41>	 12/2/92	kc		Update AOpts and MOpts. Moved the line that sets the shell
#									variable "Commands" into the makefiles, so that the output of
#									make can be executed indipendently from the build script.
#	  <SM40>	11/23/92	kc		Fix -p and add -ram option.
#	  <SM39>	11/23/92	kc		Rewrite for new makefiles.

#
#	Setup our aliases, variables, exports and defaults.
#
	Set StartTime `Date -n`
	
	Set Exit 0

	UnSet Targets EchoCmds Quiet Progress BuildSet Overrides

	Set		Usage  "∂
		/——————————————————————————————————————————————————————————————————————————\∂n∂
		| usage:        Build  buildName… [targets…] [options…]                    |∂n∂
		|                                                                          |∂n∂
		| buildNames:   ROM                   68k Universal ROM                    |∂n∂
		|               RISC              	  RISC Universal ROM                   |∂n∂
		|               LC930              	  One meg rom for LC930                |∂n∂
		|               dbLite                One meg rom for dbLite               |∂n∂
		|                                                                          |∂n∂
		| options:      -ao <options>         Assembler options                    |∂n∂
		|               -co <options>         C compiler options                   |∂n∂
		|               -po <options>         Pascal Compiler Options              |∂n∂
		|               -lo <options>         Linker Options                       |∂n∂
		|               -mo <options>         Make Options                         |∂n∂
		|               -d  <equates>         Define (or override) equates         |∂n∂
		|               -e                    Everything is forced to be rebuilt   |∂n∂
		|               -echo                 Echo Build Commands                  |∂n∂
		|               -help                 Print this message                   |∂n∂
		|               -listing              Generating assembly listings         |∂n∂
		|               -p                    Display build progress information   |∂n∂
		|               -q                    Do a quiet build (no progress info.) |∂n∂
		|               -ram	              Build an image for Romulator         |∂n∂
		|               -src <dir>            Use sources in specified directory   |∂n∂
		|               -novectorize          Inhibit vectorization                |∂n∂
		|               -warnings <off | on>  Turn warning off or on.			   |∂n∂
		\——————————————————————————————————————————————————————————————————————————/"


#
#	If we have no parameters, run commando (if we haven't allready).
#


	If !{#}
		If {DoCommando} || {BackgroundShell}
			Build -help
			Exit 0
		End
		Set DoCommando True
		Export DoCommando
		Build…
		Exit
	End


#
#	Don't use dialogs if we didn't use commando.
#
	If !{DoCommando}
		Alias Alert	'Echo > Dev:StdErr'
	End

#
#	Loop thru and parse our parameters.
#
	Loop
		If !{#}								# Done?
			If !"{BuildSet}"
				Alert "You must specify at least one build name or target"
				Exit 0
			End					
			Break
		End
		If "{1}" =~ /"ROM"/					# Build universal ROM?		
			Set BuildSet "{BuildSet} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"RISC"/				# Build RISC?		
			Set BuildSet "{BuildSet} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"LC930"/				# Build LC930?		
			Set BuildSet "{BuildSet} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"dbLite"/				# Build LC930?		
			Set BuildSet "{BuildSet} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"System"/				# Build System? <21C>		
			Set BuildSet "{BuildSet} {1}"
			Shift; Continue
		End
		If "{1}" !~ /-≈/					# Target?		
			Set Targets "{Targets} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"-ao"/					# AOpts
			If {#} < 2
				Alert "You must specify <options> with the “-ao” option."
				Exit 1
			End
			Set AOpts "{AOpts} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-co"/					# COpts
			If {#} < 2
				Alert "You must specify <options> with the “-co” option."
				Exit 1
			End
			Set COpts "{COpts} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-po"/					# POpts
			If {#} < 2
				Alert "You must specify <options> with the “-po” option."
				Exit 1
			End
			Set POpts "{POpts} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-lo"/					# LOpts
			If {#} < 2
				Alert "You must specify <options> with the “-lo” option."
				Exit 1
			End
			Set LOpts "{LOpts} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-mo"/					# MOpts
			If {#} < 2
				Alert "You must specify <options> with the “-mo” option."
				Exit 1
			End
			Set MOpts "{MOpts} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-d"/					# Defines
			If {#} < 2
				Alert "You must specify <equates> with the “-d” option."
				Exit 1
			End
			Set Overrides "{Overrides} {2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-e"/					# Build everything
			Set MOpts "{MOpts} {1}"
			Shift; Continue
		End
		If "{1}" =~ /"-echo"/				# Echo commands
			Set EchoCmds "True"
			Shift; Continue
		End
		If "{1}" =~ /"-h"≈/					# Help
			Echo "{Usage}"
			Exit 0;
		End
		If "{1}" =~ /"-listing"/			# Listing
			Set AOpts "{AOpts} -l"
			Shift; Continue
		End
		If "{1}" =~ /"-p"/					# Progress
			Set Progress "True"
			Shift; Continue
		End
		If "{1}" =~ /"-q"/					# Quiet
			Set Quiet "True"
			Shift; Continue
		End
		If "{1}" =~ /"-ram"/				# Romulator
			Set Overrides "{Overrides} forRomulator=TRUE"
			Shift; Continue
		End
		If "{1}" =~ /"-src"/				# Directory
			If {#} < 2
				Alert "You must specify <dir> with the “-src” option."
				Exit 1
			End
			Set Sources "{2}"
			Shift 2; Continue
		End
		If "{1}" =~ /"-novectorize"/		# Inhibit vectorization
			Set VOpts   "{VOpts} -nopatch"
			Set Overrides "{Overrides} nonVectorizedRom=TRUE"
			Shift; Continue
		End
		If "{1}" =~ /"-warnings"/			# Warnings
			If {#} < 2
				Alert "You must specify <level> with the “-warnings” option."
				Exit 1
			End
			If "{2}" =~ /off/
				Set AOpts   "{AOpts} -w -wb"
				Set COpts   "{COpts} -warnings off"
				Set LOpts   "{LOpts} -w -d"
				Set LibOpts "{LibOpts} -w -d" 
				Set VOpts   "{VOpts} -w"
			Else
				Set COpts "{COpts} -warnings on"

			End
			Shift 2; Continue
		Else								# Unknown Option
			Alert "Unknown Option ∂"{1}∂"."
			Exit 1
		End
		
	End #Loop
	
#
#	Make sure that we FeatureSet as the first target
#
	If "{Targets}"
		Set Targets "FeatureSet {Targets}"
	End

#
#	Set the make options
#
	If {Progress}
		Set MOpts "{MOpts} -h 'Echo Creating ∂"∂{Targ∂}∂"'"
	End

	Set MOpts "{MOpts}	-d AOpts=∂"{AOpts}∂"		∂
						-d COpts=∂"{COpts}∂"		∂
						-d POpts=∂"{POpts}∂"		∂
						-d LOpts=∂"{LOpts}∂"		∂
						-d LibOpts=∂"{LibOpts}∂"	∂
						-d VOpts=∂"{VOpts}∂"		∂
						-d Sources=∂"{Sources}∂" 	∂
						-d Overrides=∂"{Overrides}∂""

#
#	For each build
#
	If !{Quiet}
		Echo "Start Time: `Date -t -c {StartTime}`"
	End
	
	For Build in {BuildSet}		
		
		If "{Build}" =~ /ROM/			# locate the makefile
						
			Set MakeFile	"{Sources}Make:Universal.Make"				
			
			Set MakeOut		"{Sources}BuildResults:Universal:Text:MakeOut"
		
		Else If "{Build}" =~ /RISC/
						
			Set MakeFile	"{Sources}Make:RISC.Make"				
			
			Set MakeOut		"{Sources}BuildResults:RISC:Text:MakeOut"
			
		Else If "{Build}" =~ /LC930/
						
			Set MakeFile	"{Sources}Make:LC930.Make"				
			
			Set MakeOut		"{Sources}BuildResults:LC930:Text:MakeOut"
			
		Else If "{Build}" =~ /dbLite/
						
			Set MakeFile	"{Sources}Make:dbLite.Make"				
			
			Set MakeOut		"{Sources}BuildResults:dbLite:Text:MakeOut"
			
		Else If "{Build}" =~ /System/
						
			Set MakeFile	"{Sources}Make:System.Make"				
			
			Set MakeOut		"{Sources}BuildResults:System:Text:MakeOut"
			
		Else
			Alert "Error: Unknown build “{Build}”"
			Exit 1			
		End

		"{Sources}Tools:Make" -w {MOpts} {Targets} -f "{MakeFile}" > "{MakeOut}" || Exit

		If {EchoCmds}
			Set Echo 1
		End

		Execute "{MakeOut}"											

		Set Echo 0

	End	# For each build

#
#	Calculate the elapsed time
#
	If !{Quiet}

		Set StopTime   `Date -n`
	
		Set TotalTime `Evaluate {StopTime}-{StartTime}`

		Echo "Stop  Time: `Date -t -c {StopTime}`"

		Echo "Total Time: "`Date -t -c {TotalTime} | StreamEdit -e "/≈/ ∂
				  			Replace /•12/ ∂" 00∂"; Replace /[AP]M/ ∂"∂" "`
	End

	Exit 0
#
#	End
#