# set_up

# Copyright (C) 2002 Paul Pratt

# You can redistribute this file and/or modify it under the terms
# of version 2 of the GNU General Public License as published by
# the Free Software Foundation.  You should have received a copy
# of the license along with with this file; see the file COPYING.

# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# license for more details.

# ###########

# This script is used by the 'build' script to determine
# various parameters of the project, given the target
# information.

# This script is also used by my usual development
# environment scripts, which is why it is a seperate
# file instead of included in the build script.

Set my_lists_d "{my_project_d}lists:"
Export my_lists_d

Set my_progress_log "{my_derived_d}progress"
Export my_progress_log
IF not "`exists -q "{my_progress_log}"`"
	Echo -n > "{my_progress_log}"
END

Set my_built_program "{my_derived_d}{my_project_name}"
Export my_built_program

#----- file lists

Set my_c_files "`Catenate "{my_lists_d}c_files"`"
Export my_c_files

Set my_h_files "`Catenate "{my_lists_d}h_files"`"
Export my_h_files

Set my_r_files "main"
Export my_r_files

#----- Source directories

Set my_c_src_d "{my_project_d}c_src:"
Export my_c_src_d

Set my_r_src_d "{my_project_d}platform:mac:"
Export my_r_src_d

#----- Object directories

Set my_c_obj_d "{my_derived_d}c_obj:"
Export my_c_obj_d
IF not "`exists -q "{my_c_obj_d}"`"
	NewFolder "{my_c_obj_d}"
END

Set my_r_obj_d "{my_derived_d}"
Export my_r_obj_d

#----- target cpu family

IF ({my_target_env} == SANE) || ({my_target_env} == FPU)
	Set my_target_cpufam 68K
END
IF ({my_target_env} == PowerPC) || ({my_target_env} == Carbon)
	Set my_target_cpufam PPC
END
IF ({my_target_env} == Win86)
	Set my_target_cpufam X86
END

#----- check what's available in development environment

IF {my_target_env} == Carbon
	Set NavigationAvail 1
	Set AppearanceAvail 1
ELSE
	Set NavigationAvail 0
	IF "`exists -q "{CIncludes}Navigation.h"`"
		IF {my_target_env} == PowerPC
			IF "`exists -q "{SharedLibraries}NavigationLib"`"
				Set NavigationAvail 1
			END
		END
		IF ({my_target_env} == SANE) || ({my_target_env} == FPU)
			IF "`exists -q "{Libraries}Navigation.o"`"
				Set NavigationAvail 1
			END
		END
	END
	
	Set AppearanceAvail 0
	IF "`exists -q "{CIncludes}Appearance.h"`"
		IF {my_target_env} == PowerPC
			IF "`exists -q "{SharedLibraries}AppearanceLib"`"
				Set AppearanceAvail 1
			END
		END
	END
END

Set DragMgrAvail 1

#----- C Configuration file

Set CnfgGlob "{my_c_src_d}CNFGGLOB.h"
Echo -n > "{CnfgGlob}"

Echo "/* make sure this is correct CNFGGLOB */" >> "{CnfgGlob}"
Echo "#ifndef MyIsCompilingRez" >> "{CnfgGlob}"
IF {my_compiler} == SC_MrC
	IF {my_target_cpufam} == PPC
		Echo "#ifndef __MRC__" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
	END
	IF {my_target_cpufam} == 68K
		Echo "#ifndef __SC__" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
		Echo "#ifdef powerc" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
		Echo "#ifdef __powerc" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
	END
END
IF {my_compiler} == C_PPCC
	IF {my_target_cpufam} == PPC
		Echo "#ifndef __PPCC__" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
	END
	IF {my_target_cpufam} == 68K
		Echo "#ifndef applec" >> "{CnfgGlob}"
		Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
		Echo "#endif" >> "{CnfgGlob}"
	END
END
IF {my_compiler} == metrowerks
	Echo "#ifndef __MWERKS__" >> "{CnfgGlob}"
	Echo "#error "wrong CNFGGLOB.h"" >> "{CnfgGlob}"
	Echo "#endif" >> "{CnfgGlob}"
END
Echo "#endif" >> "{CnfgGlob}"

Echo "" >> "{CnfgGlob}"

IF {my_compiler} == SC_MrC
	IF {my_target_cpufam} == PPC
		Echo "#define MyCompilerMrC 1" >> "{CnfgGlob}"
	ELSE
		Echo "#define MyCompilerSC 1" >> "{CnfgGlob}"
	END
END
IF {my_compiler} == C_PPCC
	IF {my_target_cpufam} == PPC
		Echo "#define MyCompilerPPCC 1" >> "{CnfgGlob}"
	ELSE
		Echo "#define MyCompilerMPWC 1" >> "{CnfgGlob}"
	END
END
IF {my_compiler} == metrowerks
	Echo "#define MyCompilerMetrowerks 1" >> "{CnfgGlob}"
END

IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC) || ({my_compiler} == metrowerks)
	IF ({my_target_cpufam} == PPC) || ({my_target_cpufam} == 68K)
		Echo "" >> "{CnfgGlob}"
		Echo "#define BigEndianUnaligned 1" >> "{CnfgGlob}"
		Echo "" >> "{CnfgGlob}"
		Echo "#define HaveOSTarget 1" >> "{CnfgGlob}"
		Echo "#define MacTarget 1" >> "{CnfgGlob}"
	END
END
IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC) # but not in metrowerks
	IF {my_target_cpufam} == 68K
		Echo "" >> "{CnfgGlob}"
		Echo "#define ShouldUnloadDataInit 1" >> "{CnfgGlob}"
	END
END

IF {my_compiler} == SC_MrC
	IF {my_target_cpufam} == PPC
		Echo "#define MayInline inline" >> "{CnfgGlob}"
	END
END
IF {my_compiler} == metrowerks
	Echo "#define MayInline __inline__" >> "{CnfgGlob}"
END

IF {NavigationAvail}
	Echo "#define NavigationAvail 1" >> "{CnfgGlob}"
END
IF {AppearanceAvail}
	Echo "#define AppearanceAvail 1" >> "{CnfgGlob}"
END
IF {DragMgrAvail}
	Echo "#define DragMgrAvail 1" >> "{CnfgGlob}"
END
IF {my_target_env} == Carbon
	Echo "#define UseCarbonLib 1" >> "{CnfgGlob}"
END

#----  CallR  ---

Set CallR "rez -t rsrc -c RSED"

#----  CallC / CallCFast  ---

IF {my_target_cpufam} == 68K
	IF {my_compiler} == SC_MrC
		Set CallC "SC -proto strict -w 17"
	END
	IF {my_compiler} == metrowerks
		Set CallC "MWC68K -r -intsize 2"
	END
	IF {my_compiler} == C_PPCC
		Set CallC "C -r"
	END
	IF {my_target_env} == FPU #----  For FPU ---
		Set CallC "{CallC} -mc68020 -mc68881"
		IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC)
			Set CallC "{CallC} -elems881"
		END
	END
	IF ({my_compiler} == SC_MrC) || ({my_compiler} == metrowerks)
		Set CallC "{CallC} -align mac68k"
	END
	Set CallC "{CallC} -b"
	IF {my_debug_level} == NoDebug
		Set CallC "{CallC} -mbg off"
	END
	Set CallC "{CallC} -model farCode"
	Set CallCFast "{CallC}"
	IF ({my_debug_level} == NoDebug) || ({my_debug_level} == Debug)
		IF {my_compiler} == SC_MrC
			Set CallC "{CallC} -opt space"
		END
		IF {my_compiler} == metrowerks
			Set CallC "{CallC} -opt size"
		END
		IF ({my_compiler} == SC_MrC) || ({my_compiler} == metrowerks)
			Set CallCFast "{CallCFast} -opt speed"
		END
	END
END

IF {my_target_cpufam} == PPC #---- For PowerPC ---

	IF {my_compiler} == SC_MrC
		# Set CallC "MrC -proto strict -w 17"

		# use the c++ compiler to allow inlines
		Set CallC "MrCpp -w 17"
	END
	IF {my_compiler} == metrowerks
		Set callc "MWCPPC -r"
	END
	IF {my_compiler} == C_PPCC
		Set callc "PPCC -appleext on"
	END

	Set CallC "{CallC} -align mac68k"

	IF ({my_debug_level} != NoDebug) && ({my_compiler} != C_PPCC)
		Set CallC "{CallC} -traceback"
	END
	Set CallCFast "{CallC}"

	IF ({my_debug_level} == NoDebug) || ({my_debug_level} == Debug)
		IF {my_compiler} == SC_MrC
			Set CallC "{CallCFast} -opt speed"
			# Set CallC "{CallC} -opt size"
			# -opt size doesn't seem to be reliable
			Set CallCFast "{CallCFast} -opt speed"
		END
		IF {my_compiler} == metrowerks
			Set CallC "{CallC} -opt global,peep,schedule,l3,size"
			Set CallCFast "{CallC} -opt all"
		END
		IF {my_compiler} == C_PPCC
			Set CallC "{CallC} -opt size"
			Set CallCFast "{CallCFast} -opt speed"
		END
	END

END

#----  LinkLine  ---

IF {my_target_cpufam} == 68K

	IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC)
		Set LinkLine "Link"
	END
	IF {my_compiler} == metrowerks
		Set LinkLine "MWLink68K"
	END

	IF {my_debug_level} == NoDebug
		Set LinkLine "{LinkLine} -rn"
	END

	Set LinkLine "{LinkLine} -model far"

	IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC)
		Set LinkLine "{LinkLine} -srtsg all"
	END
	Set LinkLine "{LinkLine} -sg Main=STDCLIB,SANELIB,CSANELib,SADEV,STDIO"

END

IF {my_target_cpufam} == PPC #---- For PowerPC ---

	IF ({my_compiler} == SC_MrC) || ({my_compiler} == C_PPCC)
		Set LinkLine "PPCLink"
	END
	IF {my_compiler} == metrowerks
		Set LinkLine "MWLinkPPC"
	END

END

# Set LinkLine "{LinkLine} -p"

IF {my_target_env} == Carbon
Set LinkLine "{LinkLine} -m main"
END

Set LinkLine "{LinkLine} -t 'APPL' -c 'MnvM'"

#----  LinkLibs  ---

IF {my_target_env} == SANE #----  For 68k Sane ---

IF ({my_compiler} == SC_MrC)
Set LinkLibs "
{Libraries}Interface.o 
{Libraries}MacRuntime.o 
{Libraries}MathLib.o"
IF {NavigationAvail}
	Set LinkLibs "{LinkLibs} "'"{Libraries}Navigation.o"'
END
END

IF {my_compiler} == metrowerks
Set LinkLibs "
{MW68KLibraries}CPlusPlus.lib 
{MW68KLibraries}MacOS.lib 
{MW68KLibraries}'MathLib68K (2i/8d)'.Lib 
{MW68KLibraries}'ANSI (2i/8d) C++.68K'.Lib 
{MW68KLibraries}'ANSI (2i/8d) C.68K'.Lib 
{MW68KLibraries}PLStringFuncs.glue.lib 
{MW68KLibraries}SIOUX.68K.Lib"
END

IF ({my_compiler} == C_PPCC)
Set LinkLibs "
{Libraries}Interface.o 
{Libraries}Runtime.o 
{CLibraries}CSANELib.o 
{CLibraries}Math.o"
END

END

IF {my_target_env} == FPU #----  For FPU ---

IF ({my_compiler} == SC_MrC)
Set LinkLibs "
{CLibraries}CLib881.o 
{Libraries}Interface.o 
{Libraries}MacRuntime.o 
{Libraries}MathLib881.o 
{CLibraries}StdCLib.o"
END
IF {my_compiler} == metrowerks
Set LinkLibs "
{MW68KLibraries}CPlusPlus.lib 
{MW68KLibraries}MacOS.lib 
{MW68KLibraries}'MathLib68K (2i/f/8d)'.Lib 
{MW68KLibraries}'ANSI (2i/F/8d) C++.68K'.Lib 
{MW68KLibraries}'ANSI (2i/F/8d) C.68K'.Lib 
{MW68KLibraries}PLStringFuncs.glue.lib 
{MW68KLibraries}SIOUX.68K.Lib"
END
IF ({my_compiler} == C_PPCC)
Set LinkLibs "
{CLibraries}CLib881.o 
{CLibraries}CSANELib881.o 
{Libraries}Interface.o 
{Libraries}Runtime.o 
{CLibraries}StdCLib.o"
END

END

IF {my_target_env} == PowerPC #---- For PowerPC ---

IF ({my_compiler} == SC_MrC)
Set LinkLibs "
'{SharedLibraries}MathLib' 
'{SharedLibraries}InterfaceLib'"
# {PPCLibraries}x80.o
IF {NavigationAvail}
	Set LinkLibs "{LinkLibs} "'"{SharedLibraries}NavigationLib"'
END
IF {AppearanceAvail}
	Set LinkLibs "{LinkLibs} "'"{SharedLibraries}AppearanceLib"'
END
IF {DragMgrAvail}
	Set LinkLibs "{LinkLibs} "'"{SharedLibraries}DragLib"'
END
Set LinkLibs "{LinkLibs} "'"{SharedLibraries}StdCLib"'
Set LinkLibs "{LinkLibs} "'"{PPCLibraries}StdCRuntime.o"'
Set LinkLibs "{LinkLibs} "'"{PPCLibraries}PPCToolLibs.o"'
Set LinkLibs "{LinkLibs} "'"{PPCLibraries}PPCCRuntime.o"'
END

IF {my_compiler} == metrowerks
Set LinkLibs " 
{MWPPCLibraries}MathLib 
{MWPPCLibraries}MWCRuntime.Lib 
{MWPPCLibraries}'ANSI C.PPC.Lib' 
{MWPPCLibraries}SIOUX.PPC.Lib 
{MWPPCLibraries}PLStringFuncsPPC.lib 
{MWPPCLibraries}x80.o 
{SharedLibraries}InterfaceLib"
END

IF ({my_compiler} == C_PPCC)
Set LinkLibs " 
 -libRename MathLib.xcoff=MathLib 
 -libRename InterfaceLib.xcoff=InterfaceLib 
 -libRename StdCLib.xcoff=StdCLib 
{PPCLibraries}MathLib.xcoff 
{PPCLibraries}InterfaceLib.xcoff 
{PPCLibraries}StdCLib.xcoff 
{PPCLibraries}StdCRuntime.o 
{PPCLibraries}PPCToolLibs.o 
{PPCLibraries}x80.o 
{PPCLibraries}PPCCRuntime.o"
END

END

IF {my_target_env} == Carbon #---- For PowerPC ---

IF ({my_compiler} == SC_MrC)
Set LinkLibs "
'{SharedLibraries}CarbonLib'"
Set LinkLibs "{LinkLibs} "'"{PPCLibraries}PPCToolLibs.o"'
Set LinkLibs "{LinkLibs} "'"{PPCLibraries}PPCCRuntime.o"'
END

END
