########################################################################################
#
# @file SCompile
#
# @brief Compilation instructions for REFIP build
#
# Copyright (C) RivieraWaves 2009-2015
#
#########################################################################################
#
#import sys
#import os
#import os.path
#
#import SCons.Util
#import scutils


#-----------------------------------------------------------
# Build tool and default settings
#-----------------------------------------------------------


# Get build tool from command line or default
defaulttool = ARGUMENTS.get('BT','armgcc_4_8')
buildtool   = ARGUMENTS.get('BT_ARM', defaulttool).lower()

# sanity checks
assert(buildtool in ('rvds', 'gnuarm','armgcc_4_8','armgcc'))

# create environment
if buildtool == 'rvds':
    tool = scutils.rvds_find()
    env = Environment(tools = [tool])
elif buildtool == 'armgcc_4_8':
    env = Environment(tools = ['armgcc_4_8'], ENV = {'PATH':os.environ['PATH']})
elif buildtool == 'armgcc':
    env = Environment(tools = ['armgcc'], ENV = {'PATH':os.environ['PATH']})
else:
    env = Environment(tools = ['gnuarm'], ENV = {'PATH':os.environ['PATH']})

# save buildtool in environment
env['BUILDTOOL'] = buildtool

# initialize the environment from the command line arguments
scutils.env_init(env)

#-----------------------------------------------------------
# Target specific settings
#-----------------------------------------------------------

# preprocessor macros
env['CPPDEFINES'] = []

# environment  settings
env['PROGNAME'] = 'fw'
env['TYPE'] = 'fw'
env['ARCH'] = 'ble'

# use arm7 by default
if env['CPU'].lower() == 'arm7':
    env['CPU'] = 'arm7tdmi-s'
elif env['CPU'].lower() == 'arm9' or env['CPU'] == '':
    env['CPU'] = 'arm926ej-s'
    


#-----------------------------------------------------------
# Environment configuration
#-----------------------------------------------------------

if buildtool == 'rvds':

    if env['CPU'] == 'Cortex-M1':
       env['ENTRY'] = 'rw_main'
       
    else:
       env['ENTRY'] = '0x00'
       
    # msg 193-D is for undefined preprocessor identifier
    env['CCFLAGS']   = (  ' -Ospace --asm --interleave --diag_style=gnu --debug --dwarf2 --c99 '
                        + ' --diag_warning=C193 --thumb --apcs=/interwork --split_sections ')
                        
    if 'Cortex' in env['CPU']:
         env['CCFLAGS']   += ' -DCFG_CPU_CORTEX_M1 '

    env['ASFLAGS']   =  ' --diag_style=gnu --debug'
    env['LINKFLAGS'] = (' --map --symbols --diag_style gnu --remove '
        + ' --info architecture,common,debug,exceptions,inline,inputs,libraries,sizes,stack,'
        + 'tailreorder,totals,unused --callgraph --entry $ENTRY --datacompressor off '
        + '--diag_suppress L6329 ')

    if env['REM']:
        env.Append(CCFLAGS = ' --remarks --diag_suppress=1301,2530,826 ')
    else:
        env['CCFLAGS'] += '--diag_suppress=177,3017'
        
    if env['WARN'] == 'err':
        env.Append(CCFLAGS = ' --diag_error=warning ')

elif buildtool == 'gnuarm':

    env['CCFLAGS'] = ' -mabi=aapcs -mfloat-abi=soft -mfpu=fpa -g3 -fms-extensions ' \
                   + ' -Wall -Wchar-subscripts -Wformat ' \
                   + ' -Winit-self -Wignored-qualifiers -Wswitch-default -Wunused -Wundef -std=c99 '
    if(env['OPTIM'] not in '0'):
        env['CCFLAGS'] += ' -Wuninitialized '
    env['ASFLAGS']   = ' -mfloat-abi=soft -mfpu=fpa -g'
    env['LINKFLAGS'] = ' --cref --gc-sections'

    if env['SAVETMP']:
        env.Append(CCFLAGS = ' --save-temps ')

    if env['WARN'] == 'err':
        env.Append(CCFLAGS = ' -Werror ')

elif buildtool == 'armgcc_4_8':

    env['CCFLAGS'] = ' -mabi=aapcs -mfloat-abi=soft -mfpu=vfp -g3 -fms-extensions ' \
                   + ' -Wall -Wchar-subscripts -Wformat -Wuninitialized ' \
                   + ' -Winit-self -Wignored-qualifiers -Wswitch-default -Wunused -Wundef -std=c99 '
    env['ASFLAGS']   = ' -mfloat-abi=soft -mfpu=vfp -g'
    env['LINKFLAGS'] = ' --cref --gc-sections'

    if env['SAVETMP']:
        env.Append(CCFLAGS = ' --save-temps ')

    if env['WARN'] == 'err':
        env.Append(CCFLAGS = ' -Werror ')
        
elif buildtool == 'armgcc':

    env['CCFLAGS'] = ' -mabi=aapcs -mfloat-abi=soft -mfpu=vfp -g3 -fms-extensions ' \
                   + ' -Wall -Wchar-subscripts -Wformat -Wuninitialized ' \
                   + ' -Winit-self -Wignored-qualifiers -Wswitch-default -Wunused -Wundef -std=c99 '
    env['ASFLAGS']   = ' -mfloat-abi=soft -mfpu=vfp -g'
    env['LINKFLAGS'] = ' --cref --gc-sections'

    if env['SAVETMP']:
        env.Append(CCFLAGS = ' --save-temps ')

    if env['WARN'] == 'err':
        env.Append(CCFLAGS = ' -Werror ')        
