# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4
# $Id: Portfile 103033 2013-02-12 08:41:28Z landonf@macports.org $

PortSystem 1.0

name              simulavr
version           1.0.0
revision          2
categories        emulators devel
maintainers       landonf
description       Simulator for Atmel AVR microcontrollers
long_description  The SimulAVR program is a simulator for the Atmel AVR family \
                  of microcontrollers. (ATtiny and ATmega) SimulAVR can be used \
                  either standalone or as a remote target for avr-gdb. 
homepage          http://www.nongnu.org/simulavr/
platforms         darwin
master_sites      http://savannah.nongnu.org/download/simulavr/

checksums           rmd160  0e9eb32f398943449b564e919765348c32f4c600 \
                    sha256  39d93faa3eeae2bee15f682dd6a48fb4d4366addd12a2abebb04c99f87809be7

depends_lib       port:avr-binutils \
                  port:swig-python \
                  port:swig-tcl

patchfiles        patch-src_systemclock.cpp \
                  patch-msleep \
                  patch-src_Makefile.in \
                  patch-at90can-ivs \
                  patch-at90can-elpm

post-patch {
    # Hack the swig macro into detecting a modern version of swig
    reinplace "s|required=1.3.18|required=2.0.8|g" ${worksrcpath}/configure

    # Apply changes from http://savannah.nongnu.org/bugs/?35737 to fix building
    # against the later avr-libc releases
    set regexps {
        "s/SIG_INTERRUPT0/INT0_vect/"
        "s/SIG_INTERRUPT1/INT1_vect/"
        "s/SIG_INTERRUPT2/INT2_vect/"
        "s/SIG_OUTPUT_COMPARE2/TIMER2_COMP_vect/"
        "s/SIG_OUTPUT_COMPARE2A/TIMER2_COMPA_vect/"
        "s/SIG_OUTPUT_COMPARE3B/TIMER3_COMPB_vect/"
        "s/SIG_UART0_RECV/USART0_RX_vect/"
        "s/SIG_UART0_DATA/USART0_UDRE_vect/"
        "s/__attribute__((progmem))/PROGMEM/"
        "s/__attribute__ ((progmem))/PROGMEM/"
    } 

    fs-traverse fpath ${worksrcpath} {
        set ext [file extension "${fpath}"]
        if {[file isfile "${fpath}"] && ($ext == ".c" || $ext == ".h" || $ext == ".cpp")} {
            foreach r $regexps {
                reinplace -locale C $r $fpath
            }
        }
    }

    reinplace "s|^static char unshifted|static const char unshifted|" ${worksrcpath}/examples/atmel_key/scancodes.h
    reinplace {s|prog_char shifted\[\]\[2\]|static const char shifted\[\]\[2\] PROGMEM|g} ${worksrcpath}/examples/atmel_key/scancodes.h
}

configure.args    --with-bfd="${prefix}/avr/host" \
                  --with-libiberty="${prefix}/avr/host"
#configure.args    --mandir=${prefix}/share/man
