#   Copyright (c) 2007, 2008 Axel Wachtler
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   * Neither the name of the authors nor the names of its contributors
#     may be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.

# $Id: SConscript,v 1.31 2009/06/30 19:42:03 awachtler Exp $

#=== modules ==================================================================
Import("avr board")

#=== setup build environment ==================================================
avrxmpl = avr.Clone()
avrxmpl['BOARD_TYPE'] = board
avrxmpl['CPU'] = avr['boardopts'][board].cpu
avrxmpl['BOOT_LOADER_ADDRESS'] = hex(avr['boardopts'][board].blstart/2)
avrxmpl['BOOT_LOADER_OFFSET'] = hex(avr['boardopts'][board].blstart)
bdir=Dir(".").path

ccflags = avrxmpl.Split("""-Wall -Wundef
             -g$DBGFMT
             -Os
             -mmcu=${CPU}
             -D${BOARD_TYPE}
             -DBOOT_LOADER_ADDRESS=${BOOT_LOADER_ADDRESS}
             %s
              """ % avr['boardopts'][board].get_ccopts())
avrxmpl['CCFLAGS'] = avrxmpl.subst(" ".join(ccflags))
avrxmpl['LINKFLAGS'] = "-Wl,-Map=${TARGET.dir}/${TARGET.filebase}.map -mmcu=$CPU"
avrxmpl['AR'] = "avr-ar"
avrxmpl['LINK'] = "avr-gcc"
avrxmpl['RANLIB'] = "avr-ranlib"
avrxmpl['CPPPATH'] = ["${dir_install.abspath}/inc","./Inc"]
avrxmpl['LIBPATH'] = "${dir_install.abspath}/lib"

xmplexclude = {
    'xmpl_keys'  : 'stkm8 rzusb lgee231 ics230_11',
    'xmpl_key_events'  : 'stkm8 rzusb lgee231 ic230_11',
    'xmpl_leds'  : 'stkm8',
    'xmpl_dbg' : '',
    'xmpl_timer' : '',
    'xmpl_hif'   : 'stkt26 tiny230 lgee231',
    'xmpl_trx_base' : '',
    'xmpl_trx_tx' : '',
    'xmpl_trx_rx' : '',
    'xmpl_trx_echo' : '',
    'xmpl_trx_txaret' : '',
    'xmpl_trx_rxaack' : '',
    'xmpl_linbuf_tx' : '',
    'xmpl_linbuf_rx' : '',
    'xmpl_radio_stream' : '',
    }

xmplboardspecific = {
        "lgee231" : "xmpl_lgee_acc_simple",
    }

glblexclude = "zgbl230 zgbl231 zgbl212"

xapps = []
xapsfiles = []
xmakefiles = []
import pdb

# === build list with example applications ===
xmplapps = []
for e in xmplexclude.keys():
    if board not in xmplexclude[e] and board not in glblexclude:
        xmplapps.append(e)
xmplapps += xmplboardspecific.get(board,"").split()

# === setup the build rules for the example ===
for e in xmplapps:
    avrxmpl['xapp'] = e
    xelf = avrxmpl.Program('${xapp}_${BOARD_TYPE}.elf',
                           '${xapp}.c',
                           LIBS=["radio_${BOARD_TYPE}", "io_${BOARD_TYPE}"])
    xhex = avrxmpl.Command('${xapp}_${BOARD_TYPE}.hex',
                           xelf, "avr-objcopy -O ihex $SOURCE $TARGET")

    # look for "${xxx}" in the XSLT file for getting all variables to be used
    xparm= { 'app' : avrxmpl.subst('${xapp}'),
             'brd' : avrxmpl.subst('${BOARD_TYPE}'),
             'projname' : avrxmpl.subst('${xapp}_${BOARD_TYPE}'),
             'src' : avrxmpl.subst('${xapp}.c'),
             'ccflags' : avrxmpl['CCFLAGS'],
             'part' : avrxmpl['CPU'],
             'f_cpu' : avrxmpl['boardopts'][board].F_CPU
             }
    xaps = avrxmpl.ApsTransformer('${dir_install_xmpl}/${xapp}_${BOARD_TYPE}.aps',
                                  [xelf],
                                  APSTEMPLATE = 'Templates/xmpl.aps',
                                  XSLTFILE = 'Templates/update-apsfile.xsl',
                                  STRINGPARAM = xparm)

    xmfile = avrxmpl.TextTransformer('${dir_install_xmpl}/${xapp}_${BOARD_TYPE}.mk',
                                     '#Templates/xmpl.mk',
                                     STRINGPARAM = xparm)
    xapps += [xelf, xhex]
    xapsfiles.append(xaps)
    xmakefiles.append(xmfile)

#=== targets ==================================================================
xinst = avrxmpl.Install('${dir_install.abspath}/bin', xapps)
avrxmpl.Alias('x${BOARD_TYPE}', xinst  + xapsfiles + xmakefiles)
