# Hey, Emacs! This is a -*- Python -*- file!
#
# Carrick Detweiler's scons build files
#

import os.path



#Set the sources
sources = ['main.c','uart.c']
#Any object files that are not compiled from a .c source file
objs = ['keycode.o']
#
#Set the target name
target = 'main'
#THE processor we are using
MCU = 'atmega1284p'

#Programs to run
CCPROG = 'avr-gcc'
OBJCOPY = 'avr-objcopy'
OBJDUMP = 'avr-objdump'
SIZE = 'avr-size'

for s in sources:
    objs.append(s.replace('.c','.o'))

env = Environment(ENV = {'PATH' : os.environ['PATH']})

############ Setup the compiler ##############
env.Replace(CC = CCPROG)

########### Setup CC Flags ###################
env.AppendUnique(CCFLAGS=['-mmcu=' + MCU])

# Optimization level (0,1,2,3, or s are the levels)
env.AppendUnique(CCFLAGS=['-Os'])

# All warnings enabled
env.AppendUnique(CCFLAGS=['-Wall'])

# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
#
env.AppendUnique(CCFLAGS=['-gstabs'])
#

# Compiler flag to set the C Standard level.
# c89   - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99   - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
env.AppendUnique(CCFLAGS=['-std=gnu89'])

########### Setup linker flags #################

# Minimalistic printf version
PRINTF_LIB_MIN = ['-Wl,-u,vfprintf', '-lprintf_min']

# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = ['-Wl,-u,vfprintf', '-lprintf_flt']

PRINTF_LIB = PRINTF_LIB_MIN

# Minimalistic scanf version
SCANF_LIB_MIN = ['-Wl,-u,vfscanf', '-lscanf_min']

# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = ['-Wl,-u,vfscanf', '-lscanf_flt']

SCANF_LIB = SCANF_LIB_MIN

#The math lib if you want it
MATH_LIB = '-lm'
#MATH_LIB = ''

env.AppendUnique(LINKFLAGS='-mmcu='+MCU)
env.AppendUnique(LINKFLAGS='-gstabs')
env.AppendUnique(LINKFLAGS='-Wall')
env.AppendUnique(LINKFLAGS='-std=gnu99')
env.AppendUnique(LINKFLAGS=[PRINTF_LIB])
#env.AppendUnique(LINKFLAGS=[SCANF_LIB])
#env.AppendUnique(LINKFLAGS=[MATH_LIB])


########### Create custom builders ################


# For outputting hex files
bld = Builder(action = OBJCOPY + ' -O ihex -R .eeprom $SOURCE $TARGET')
env.AppendUnique(BUILDERS = {'Hex' : bld})

# For outputting eeprom files
bld = Builder(action = OBJCOPY + ' -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex  $SOURCE $TARGET')
env.AppendUnique(BUILDERS = {'EEPROM' : bld})

# For outputting the lss file
bld = Builder(action = OBJDUMP + ' -h -S $SOURCE > $TARGET')
env.AppendUnique(BUILDERS = {'Lss' : bld})

# For outputting the elf file
objstr = ""
for o in objs:
    objstr = objstr + " " + o
#bld = Builder(action = CCPROG + ' -mmcu='+MCU+' $SOURCE ' + objstr)
bld = Builder(action = CCPROG + ' -o $TARGET ' + ' -mmcu='+MCU+' ' + objstr)
#bld = Builder(action = 'pwd')
env.AppendUnique(BUILDERS = {'Elf' : bld})

########## Do the build #################

# first create the elf file
#env.Program(target+'.elf', objs)
env.Object(source = sources)
env.Elf(target+'.elf', objs)
#env.Elf(target+'.elf', sources)

env.Hex(target+'.hex',target+'.elf')
env.EEPROM(target+'.eep',target+'.elf')
env.Lss(target+'.lss',target+'.elf')

env.Alias('build',[target+'.elf',target+'.hex',target+'.eep',target+'.lss'])

