import os

Import('env')

# Target specific SCons command line variables
vars = Variables()
vars.Add(PathVariable('MBED_DIR',          'Path to the mbed source code repository',                os.environ.get('AJ_MBED_DIR'), PathVariable))
vars.Add(PathVariable('ARM_TOOLCHAIN_DIR', 'Path to the GNU ARM toolchain bin folder',               os.environ.get('AJ_ARM_TOOLCHAIN_DIR'), PathVariable.PathIsDir))
vars.Update(env)
Help(vars.GenerateHelpText(env))

# Disable building unit tests
env['build_unit_tests'] = False

# Update environment
if env.has_key('ARM_TOOLCHAIN_DIR'):
    env['ENV']['PATH'] = ';'.join([ env['ENV']['PATH'], env['ARM_TOOLCHAIN_DIR'] ])

# Cross compile setup
cross_prefix = 'arm-none-eabi-'
env.Replace(CC = cross_prefix + 'gcc')
env.Replace(CXX = cross_prefix + 'g++')
env.Replace(LINK = cross_prefix + 'gcc')
env.Replace(AR = cross_prefix + 'ar')
env.Replace(RANLIB = cross_prefix + 'ranlib')

env.Replace(CCCOM           = '$CC $CFLAGS $CCFLAGS $_CCCOMCOM -o $TARGET $SOURCES')
env.Replace(CXXCOM          = '$CXX $CFLAGS $CCFLAGS $CXXFLAGS $_CCCOMCOM -o $TARGET $SOURCES')
env.Replace(CPPDEFPREFIX    = '-D')
env.Replace(OBJSUFFIX       = '.o')
env.Replace(INCPREFIX       = '-I')
env.Replace(LIBPREFIX       = 'lib')
env.Replace(LIBSUFFIX       = '.a')
env.Replace(PROGPREFIX      = '')
env.Replace(PROGSUFFIX      = '.elf')
env.Replace(LIBDIRPREFIX    = '-L')
env.Replace(LIBDIRSUFFIX    = '')
env.Replace(LIBLINKPREFIX   = '-l')
env.Replace(LIBLINKSUFFIX   = '')
env.Replace(LINKCOM         = '$LINK $PRELINKFLAGS $LINKSCRIPT $LIBPATHS -o $TARGET $__RPATH $SOURCES $system_objects $_LIBDIRFLAGS $_LIBFLAGS $LINKFLAGS')
env.Replace(CCFLAGS         = '')
env.Replace(CXXFLAGS        = '')
env.Replace(CPPFLAGS        = '')
env.Replace(ASFLAGS         = '')
env.Replace(ASPPCOM         = '$AS')
env.Replace(ARFLAGS         = 'rc')
env.Replace(ARCOM           = '$AR $ARFLAGS $TARGET $SOURCES')
env.Replace(ASCOM           = '$CC -o $TARGET -c $CFLAGS $CCFLAGS $_CCCOMCOM $SOURCES')

# This was done because scons creates a link file to feed into the linker
# and arm-none-eabi removes '\' when interpreting a linker file. This
# prevents scons from creating a link file and just feeding the command line
# options directly to the compiler/linker
env['MAXLINELENGTH'] = 10000

# Compiler flags
env.Append(CXXFLAGS = ['-std=gnu++98',
                       '-fno-rtti'])

env.Append(CCFLAGS = ['-mcpu=cortex-m4',
                      '-mthumb',
                      '-mfpu=fpv4-sp-d16',
                      '-c',
                      '-g',
                      '-fno-common',
                      '-fmessage-length=0',
                      '-Wall',
                      '-fno-exceptions',
                      '-ffunction-sections',
                      '-Wformat-security',
                      '-Werror=format-security',
                      '-fdata-sections'])

env.Append(PRELINKFLAGS = ['-mcpu=cortex-m4',
                           '-mthumb',
                           '-Wl,--gc-sections',
                           '--specs=nano.specs',
                           '-u', '_printf_float',
                           '-u', '_scanf_float'])

env.Append(LINKSCRIPT = ['-T' + env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/K64FN1M0xxx12.ld'])

env.Append(LIBPATHS = ['-L' + env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM'])

env.Replace(LINKFLAGS = ['-Wl,-Map,${TARGET.base}.map'])

env.Prepend(_LIBFLAGS = ['-Wl,-start-group'])
env.Append(_LIBFLAGS = ['-Wl,-end-group'])

env.Append(LIBS = ['mbed',
                   'stdc++',
                   'gcc',
                   'm',
                   'c'])

# Pre-processor directives
env.Append(CPPDEFINES = {'TARGET_K64F'      : None,
                         'TARGET_M4'        : None,
                         'TARGET_Freescale' : None,
                         'TARGET_KPSDK_MCUS': None,
                         'TARGET_KPSDK_CODE': None,
                         'TARGET_MCU_K64F'  : None,
                         'TARGET_FRDM'      : None,
                         'TOOLCHAIN_GCC_ARM': None,
                         'TOOLCHAIN_GCC'    : None,
                         '__CORTEX_M4'      : None,
                         'ARM_MATH_CM4'     : None,
                         '__FPU_PRESENT'    : '1',
                         'MBED_BUILD_TIMESTAMP': '0.0',
                         '__MBED__'         : '1',
                         'CPU_MK64FN1M0VMD12': None,
                         'FSL_RTOS_MBED'    : None,
                         'TARGET_FF_ARDUINO': None,
                         '__CORTEX_M4'      : None,
                         'FSL_RTOS_MBED'    : None,
                         '_FORTIFY_SOURCE'  : '1'
                     })

# Linker flags
env.Append(LINKFLAGS = [
    '-mcpu=cortex-m3',
    '-mthumb',
    '-Wl,--gc-sections',
    '-Wl,-Map,${TARGET.base}.map',
    '-Wl,--entry=Reset_Handler',
])

env.Append(CPPPATH = [env['MBED_DIR'] + '/mbed-rtos',
                      env['MBED_DIR'] + '/mbed-rtos/rtx',
                      env['MBED_DIR'] + '/mbed-rtos/rtx/TARGET_M4',
                      env['MBED_DIR'] + '/mbed-rtos/rtx/TARGET_M4/TOOLCHAIN_GCC',
                      env['MBED_DIR'] + '/mbed-rtos/rtos',
                      env['MBED_DIR'] + '/SDFileSystem',
                      env['MBED_DIR'] + '/FATFileSystem',
                      env['MBED_DIR'] + '/FATFileSystem/ChaN',
                      env['MBED_DIR'] + '/mbed',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F_Freescale',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/can',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/edma',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/sdhc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/pit',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/flextimer',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/smc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/uart',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/pmc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/mcg',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/port',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/wdog',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/adc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/osc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/enet',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/i2c',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/gpio',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/rtc',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/dmamux',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/sim',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/sai',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/hal/dspi',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/utilities',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/pit',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/clock',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/clock/src',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/enet',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/drivers/interrupt',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/common',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_KPSDK_CODE/common/phyksz8081',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/device',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/device/MK64F12',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TARGET_Freescale/TARGET_KPSDK_MCUS/TARGET_MCU_K64F/TARGET_FRDM',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM'])

# Pre-compiled object files for the K64F
env['system_objects'] = [env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/startup_MK64F12.o',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/cmsis_nvic.o',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/system_MK64F12.o',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/mbed_overrides.o',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/board.o',
                      env['MBED_DIR'] + '/mbed/TARGET_K64F/TOOLCHAIN_GCC_ARM/retarget.o']

env.Install('#dist/include/ajtcl', Glob('src/target/mbedrtos-frdm/*.h'))
env.Install('#dist/include/ajtcl', Glob('src/mbedrtos/*.h'))
env.Install('#dist/include/ajtcl', Glob('src/wsl/*.h'))
env.Install('#dist/include/ajtcl', Glob('src/bsp/*.h'))

# Debug/Release Variants
if env['VARIANT'] == 'debug':
    env.Append(CFLAGS = '-g3')
else:
    env.Append(CFLAGS = '-Os')
    env.Append(LINKFLAGS = '-s')

env['build_shared'] = False
#env['connectivity_options'] = [ 'tcp', 'ardp' ]
