#!/usr/bin/env python
import platform
import os

#Add options
def options(opt):
    opt.add_option( '--stack-size', default=512, type='int',
        help='Define system stack size. [default: 512]'
    )
    opt.add_option('--bootloader-remap', default=0, type='int',
        help='Bootloader flash remap shift KB [ default: 0kB ]'
    )

# Extra configuration arguments
def configure(conf):
    conf.env.append_value( 'LDFLAGS',
            '-Wl,--defsym=_sys_stack_size=%i' % conf.options.stack_size )
    scripts_dir = conf.path.find_dir('./scripts').abspath()
    conf.env.OPENOCD_SCRIPT_FILE = os.path.join( scripts_dir, 'stm32.cfg' )
    if conf.options.bootloader_remap>0:
        conf.env.BOOTLOADER_REMAP_SHIFT_KB = conf.options.bootloader_remap

#Build script
def build(bld):
    exclude = 'src/stm32f1fmc.cpp'
    src = bld.path.ant_glob( 'src/*.c' )
    src += bld.path.ant_glob( 'src/*.cpp', excl=exclude )
    inc = ['./inc' ]
    bld.stlib( source=src,
            target='stm32',
            includes = inc,
            export_includes= inc,
            use = ['foundation'],
    )
    bld.env.CRT0_LINKER_SCRIPT = bld.path.find_resource( 'scripts/stm32_flash.ld')

