# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8::et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup           crossgcc 1.0

# This post-extract block must be declared before the one created by crossgcc.setup.
post-extract {
    ln -s gcc-${crossgcc.version} ${workpath}/gcc-${version}
}

crossgcc.setup      msp430-elf 9.3.0
crossgcc.setup_libc newlib 2.4.0

version             9.3.1.2
revision            1

set vers_patch      9.3.1.11
set name_patch      msp430-gcc-${vers_patch}-source-patches
set file_patch      ${name_patch}.tar.bz2

maintainers         {@edilmedeiros gmail.com:jose.edil+macports} \
                    openmaintainer

homepage            http://www.ti.com/tool/msp430-gcc-opensource
master_sites-append https://dr-download.ti.com/software-development/ide-configuration-compiler-or-debugger/MD-LlCjWuAbzH/${version}/:patch

distfiles-append    ${file_patch}:patch
checksums-append    ${file_patch} \
                    md5     8f305461a3b32fc8d1155bf18685f53b \
                    rmd160  a8e5a2ddb2adf4ed9370ec632d361b2e6b2c4613 \
                    sha256  ec6472b034e11e8cfdeb3934b218e5bafbb7a03f3afc0e76536bd9c42653525b \
                    size    283677

depends_run         port:msp430-gcc-support-files

depends_extract-append \
                    port:bzip2

post-extract {
    system -W ${workpath} "${prefix}/bin/bzip2 -dc [shellescape ${distpath}/${file_patch}] | /usr/bin/tar xf -"
}

pre-patch {
    system -W ${worksrcpath} "/usr/bin/patch -p0 < [shellescape ${workpath}/${name_patch}/gcc-9.3.0.patch]"
    system -W ${workpath}/newlib-2.4.0 "/usr/bin/patch -p0 < [shellescape ${workpath}/${name_patch}/newlib-2_4_0.patch]"
}

# gcc_system.diff: See https://gcc.gnu.org/bugzilla/show_bug.cgi?format=multiple&id=111632
# gcc_config_host.diff: See https://github.com/riscv-software-src/homebrew-riscv/issues/47
patchfiles          gcc_system.diff \
                    gcc_config_host.diff

# Use the same flags as TI's build script
configure.args-append \
                    --target=msp430-elf \
                    --enable-languages=c,c++ \
                    --disable-nls \
                    --enable-initfini-array \
                    --enable-target-optspace \
                    --enable-newlib-nano-formatted-io

# Required to build the patched newlib
configure.cflags-append \
                    -Wno-error=int-conversion