########## BEGIN SCONSTRUCT ##########
	
import os

# Ensure tool versions
EnsurePythonVersion(2,7)
EnsureSConsVersion(2,3,1)
		
# Settings file to be read
# Implements: COR009
settings_file = 'settings.py'

# Load all the variables into the environment
if not os.path.exists(settings_file):
	print 'Could not find file ' + settings_file + ' in current directory.'
else:
	vars = Variables(settings_file)
	
	# Add variables to environment along with some help text
	vars.Add('OUTPUT_DIR', 			'Path to build all target files.', 'output')
	vars.Add('LOG_DIR', 			'Path to store all log files.', 'log')
	vars.Add('CPU_TARGET', 			'CPU to build for.  Must match what the compiler is expecting as \'cpu\' option.')
	vars.Add('COMPILER_TOOL', 		'Custom SCons tool to be used for building.  Must match the file name located in the site_tools directory.')
	vars.Add('COMPILER_ROOT', 		'Path to compiler executables.')
	vars.Add('COMPILER_FLAGS', 		'Compiler flags to use for building.')
	vars.Add('COMPILER_DEFINES', 	'Compiler defines to use for building.')
	vars.Add('ASSEMBLER_FLAGS', 	'Assembler flags to use for building.')
	vars.Add('ASSEMBLER_DEFINES', 	'Assembler defines to use for building.')
	vars.Add('LINKER_FLAGS', 		'Linker flags to be used for building.')
	vars.Add('LINKER_FILE', 		'Path to linker file to be used during linking.')
	vars.Add('ADD_LIBRARIES', 		'Names of additional libraries to be included during linking.  These do not need the file extension.')
	vars.Add('LIBRARY_PATHS', 		'Paths to look for the additional libraries.')
	vars.Add('A2L_COMMAND', 		'A command to run in order to build the A2L file.  The name of the A2L file will be passed in as the last argument to this command line.')
	vars.Add('BUILD_TYPE', 			'Type of build to be done.  Options are: DEBUG, PROD, DEVEL')
	vars.Add('PROJ_NAME', 			'Name of the project to be built.')
	vars.Add('SW_VERSION_MSB', 		'Most signification numbers of the SW version.')
	vars.Add('SW_VERSION_LSB', 		'Least signification numbers of the SW version.')
	
	# Initialize the default SCons environment
	env = Environment(variables = vars)

	# Generate variables help text
	env.Help(vars.GenerateHelpText(env))
	
if not GetOption('help'):

	import scons_common
	
	# Setup logging of output
	scons_common.setuplogging(env)

	# Validate the mandatory values of the environment
	scons_common.validate_environment(env)
	
	# Build target/project name
	if env['BUILD_TYPE'] == 'PROD':
		env['PROJ_PREFIX'] = 'P'
	else:
		env['PROJ_PREFIX'] = 'D'
		
	env['TARGET_NAME']	   = env.subst('$PROJ_PREFIX') + '_' + env.subst('$PROJ_NAME') + '_' + env.subst('$SW_VERSION_MSB') + '_' + env.subst('$SW_VERSION_LSB')

############ START SCONSCRIPT ############

	# Clone the environment and setup compiler specific enivronment
	env = env.Clone(tools=[env['COMPILER_TOOL'], 'a2l'])

	# Storage of all objects to be linked
	objects = []

	# Setup build directories for sub-systems
	asw_build_dir 	= os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'ASW')
	bsw_build_dir 	= os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'BSW')
	infra_build_dir = os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'INFRA')
	mcal_build_dir 	= os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'MCAL')
	rte_build_dir 	= os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'RTE')
	rtos_build_dir 	= os.path.join(env.subst('$OUTPUT_DIR'), 'objs', 'RTOS')

	#### ITEMS BELOW HERE WILL NEED MODIFICATION SPECIFIC TO THE PROJECT FOLDER STRUCTURE (EXAMPLE BELOW) ####
	
	# Specify which SConscript files to build
	asw_path	= os.path.join('../../ASW/SConscript')
	bsw_path	= os.path.join('../../BSW/SConscript')
	bsw2_path	= os.path.join('../../BSW/SConscript2')
	infra_path	= os.path.join('../../INFRA/SConscript')
	mcal_path	= os.path.join('../../Targets/TC264/MCAL/SConscript')
	rte_path	= os.path.join('../../RTE/SConscript')
	rtos_path	= os.path.join('../../Targets/TC264/RTOS/SConscript')

	# Compile each subsystem's SConscript file and collect all objects  
	objects += SConscript(infra_path, exports='env', variant_dir=infra_build_dir, duplicate=0)
	objects += SConscript(asw_path, exports='env', variant_dir=asw_build_dir, duplicate=0)
	objects += SConscript(mcal_path, exports='env', variant_dir=mcal_build_dir, duplicate=0)
	objects += SConscript(rte_path, exports='env', variant_dir=rte_build_dir, duplicate=0)
	objects += SConscript(rtos_path, exports='env', variant_dir=rtos_build_dir, duplicate=0)
	objects += SConscript(bsw_path, exports='env', variant_dir=bsw_build_dir, duplicate=0)
	objects += SConscript(bsw2_path, exports='env', variant_dir=bsw_build_dir, duplicate=0)
	# Setup output path for debug, hex and A2L files
	dbg_path 	= os.path.join(env.subst('$OUTPUT_DIR'), 'dbg', env.subst('$TARGET_NAME'))
	hex_path	= os.path.join(env.subst('$OUTPUT_DIR'), 'hex', env.subst('$TARGET_NAME'))
	a2l_path	= os.path.join(env.subst('$OUTPUT_DIR'), 'a2l', env.subst('$TARGET_NAME'))

	# Build final debug, hex and A2L files
	dbg_exe 	= env.Program(dbg_path, objects)
	#hex_exe 	= env.Hex(hex_path, dbg_exe)
	#a2l_file	= env.A2L(a2l_path,hex_exe)

############ END SCONSCRIPT ############

########## END SCONSTRUCT ##########