#   Copyright (c) 2007 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.11 2009/11/04 21:03:52 awachtler Exp $

#
# Main Application Sconstruct File
#

#=== modules ==================================================================
import pprint
Import("avr board")
pp = lambda x :  pprint.pprint(x)

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

bdir=Dir(".").path

ccflags = avrlocal.Split("""-Wall -Wundef
             -g$DBGFMT
             -Os
             -mmcu=$CPU
             -fshort-enums
             -D$BOARD_TYPE
             -DBOOT_LOADER_ADDRESS=$BOOT_LOADER_ADDRESS
             %s
              """ % avr['boardopts'][board].get_ccopts())

avrlocal['radiolib'] = avrlocal.subst("radio_${BOARD_TYPE}")
avrlocal['iolib'] = avrlocal.subst("io_${BOARD_TYPE}")
avrlocal['CCFLAGS'] = avrlocal.subst(" ".join(ccflags))
#avrlocal['LINKFLAGS'] = "-Wl,--section-start .register=0x00800020 -Wl,-Map=${TARGET.dir}/${TARGET.filebase}.map -mmcu=$CPU -L%s" % bdir
avrlocal['LINKFLAGS'] = "-Wl,-Map=${TARGET.dir}/${TARGET.filebase}.map -mmcu=$CPU -L%s" % bdir
avrlocal['AR'] = "avr-ar"
avrlocal['LINK'] = "avr-gcc"
avrlocal['RANLIB'] = "avr-ranlib"

#
avrlocal['CPPPATH'] = ["${dir_install.abspath}/inc","./Inc"]
avrlocal['LIBPATH'] = "${dir_install.abspath}/lib"

# === targets =========================================================
apps = []
myboard = avrlocal['boardopts'][board]

applications = {"diag"    : ["diagnostic.c"],
                "rdiag"   : ["diagradio.c"],
                "wuart"   : ["wuart.c"],
                "sniffer" : ["Sniffer/sniffer.c",
                             "Sniffer/sniffer_ctrl.c",
                             "Sniffer/sniffer_scan.c"],
                "wgpio"   : ["wgpio.c"],
                }

for app in applications.keys():
    if app not in myboard.NO_APP:
        avrlocal['app'] = app
        elf = avrlocal.Program('${app}_${BOARD_TYPE}.elf', applications[app],
                                   LIBS=["radio_${BOARD_TYPE}", "io_${BOARD_TYPE}"])
        hex = avrlocal.Command('${app}_${BOARD_TYPE}.hex',
                                elf, "avr-objcopy -O ihex $SOURCE $TARGET")
        apps += [elf, hex]

# === install =========================================================
appinst = avrlocal.Install('${dir_install.abspath}/bin', apps)

# === aliases =========================================================
avrlocal.Alias('a${BOARD_TYPE}', appinst )
