#!/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 ]'
    )
    opt.add_option('--crystal-hz', type='int',
        help='HSE oscillator speed'
    )


# 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/openocd').abspath()
    conf.env.OPENOCD_SCRIPT_FILE = os.path.join( scripts_dir, 'jtag.cfg' )
    if conf.options.bootloader_remap>0:
        conf.env.BOOTLOADER_REMAP_SHIFT_KB = conf.options.bootloader_remap
    conf.define('USE_FULL_LL_DRIVER',1)
    if conf.options.crystal_hz:
        conf.define('HSE_VALUE',conf.options.crystal_hz);

#Build script
def build(bld):
    src = bld.path.ant_glob( 'periph/src/**/*.c' )
    src += bld.path.ant_glob( 'periph/src/**/*.cpp' )
    vendor_path = os.path.join( 'vendor',bld.isix_get_arch(),bld.isix_get_subarch(),
            bld.isix_get_cpu_vendor(), bld.isix_get_cpu_family() )
    vdrv_path =  os.path.join( 'periph','drivers',bld.isix_get_arch(),
                    bld.isix_get_subarch(), bld.isix_get_cpu_vendor(), 'all' )
    vdrvc_path =  os.path.join( 'periph','drivers',bld.isix_get_arch(),
                    bld.isix_get_subarch(), bld.isix_get_cpu_vendor(),
                    bld.isix_get_cpu_family() )
    mach_inc = os.path.join( 'periph/include/arch',bld.isix_get_arch(),
        bld.isix_get_subarch(),
        bld.isix_get_cpu_vendor(), bld.isix_get_cpu_family() )
    arch_inc = os.path.join( 'periph/include/arch',bld.isix_get_arch(),
            bld.isix_get_subarch(), bld.isix_get_cpu_vendor() )
    subarch_inc = os.path.join('periph/include/arch',bld.isix_get_arch(),
            bld.isix_get_subarch(), bld.isix_get_cpu_vendor(),
            bld.isix_get_cpu_family() )
    src += bld.path.ant_glob( vendor_path+'/**/*.cpp' )
    src += bld.path.ant_glob( vendor_path+'/**/*.c' )
    src += bld.path.ant_glob( vdrv_path+'/**/*.cpp')
    src += bld.path.ant_glob( vdrv_path+'/**/*.c')
    src += bld.path.ant_glob( vdrvc_path+'/**/*.cpp')
    src += bld.path.ant_glob( vdrvc_path+'/**/*.c')
    inc = ['./periph/include', vendor_path+'/include',
           arch_inc, mach_inc, subarch_inc ]
    if bld.isix_get_arch()=='arm' and bld.isix_get_subarch()=='cortexm':
        cmsis_path = os.path.join( 'vendor', bld.isix_get_arch(),bld.isix_get_subarch(), 'cmsis' )
        inc += [ cmsis_path + '/include' ];
        inc += [ os.path.join(cmsis_path,'device',bld.isix_get_cpu_vendor(),
            bld.isix_get_cpu_family(),'include') ]
        src += bld.path.ant_glob( cmsis_path + '/**/*.c' )
        src += bld.path.ant_glob( cmsis_path + '/**/*.cpp' )
    bld.stlib( source=src,
            target='periph',
            includes = inc,
            export_includes= inc,
            use = ['foundation'],
    )
    ldpath = os.path.join('scripts',bld.isix_get_arch(),bld.isix_get_subarch(),
                          'flash.ld')
    bld.env.CRT0_LINKER_SCRIPT = bld.path.find_resource(ldpath);

