# -*- 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 87970 2011-12-14 21:10:43Z takeshi@macports.org $

PortSystem 1.0

name            g95
version         0.92
revision        2
platforms       darwin
categories      lang
maintainers     takeshi
description     Another GNU Fortran 95 compiler
long_description \
                G95 is a stable, production Fortran 95 compiler \
                available for multiple cpu architectures and operating systems. \
                Innovations and optimizations continue to be worked on. \
                Parts of the F2003 standard have been implemented in g95.
homepage        http://g95.org/
master_sites    ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.0.4/:gcc \
                http://ftp.g95.org/v${version}/:g95

if {(${os.platform} == "darwin" && ${os.major} >= 10)||${build_arch}=="ppc64"||${build_arch}=="x86_64"} {
    default_variants +gcc42
    pre-fetch {
        if {![variant_isset gcc42]} {
            error "$name must be built with +gcc42 on ${os.platform} ${os.major} / ${build_arch}."
        }
    }
}

if {[variant_isset gcc42]} {
    set version_gcc 4.2.4
    dist_subdir     gcc42
    array set barch {ppc powerpc ppc64 powerpc64 i386 i386 x86_64 x86_64}
    set triple      $barch(${build_arch})-apple-darwin${os.major}
} else {
    set version_gcc 4.0.4
    dist_subdir     gcc40
    set triple      ${os.arch}-apple-darwin${os.major}
}
set buildhost   "--build=${triple} --host=${triple}"

set dcore       gcc-core-${version_gcc}.tar.bz2
set dg95        ${name}_source.tgz
set gcclib      lib/${name}/${triple}/${version_gcc}
distfiles       ${dcore}:gcc ${dg95}:g95

checksums       ${dcore} md5    193e0a7a471cca70e374974bc5a60137         \
                ${dcore} sha1   55c664b33a4dc6b2130c47da28a1d6daab732ce9 \
                ${dcore} rmd160 af67dc8385942450f6b27ad83a6b9e3d22f779a1 \
                ${dg95}  md5     e2b613769f28e8e0ae6a18d02a052dae \
                ${dg95}  sha1    e6320fc80d8e388aad1fdbde53b2414353692b76 \
                ${dg95}  rmd160  ac00c4cdd6dc0b90ef865040ff338b7f2d9296d1

depends_lib     port:libtool port:libiconv

patchfiles      patch-Makefile.in.diff \
                patch-host-darwin.c.diff \
                patch-darwin-fallback.c.diff

universal_variant no
use_parallel_build  no

extract.only    ${dg95}

pre-fetch {
    file delete ${distpath}/${dg95}
}

post-extract {
    system "tar jxvf ${distpath}/${dcore} -C ${workpath}; \
            cd ${worksrcpath}; tar zxvf libf95.a-${version}.tar.gz"
}

pre-patch {
    ln -s ${workpath}/gcc-${version_gcc} ${worksrcpath}
}

post-patch {
    reinplace "s|gcc-lib|$name|" ${worksrcpath}/configure
    reinplace "s|gcc-lib|$name|" ${worksrcpath}/libf95.a-${version}/configure
    reinplace "s|CFLAGS_FOR_TARGET = -O2 \$(CFLAGS)|CFLAGS_FOR_TARGET = ${configure.cflags}|" ${workpath}/gcc-${version_gcc}/Makefile.in
    reinplace "s|^stage1_cflags=\"|stage1_cflags=\"${configure.cflags} ${configure.cc_archflags} |" ${workpath}/gcc-${version_gcc}/configure
    if {[variant_isset gcc42]} {
      reinplace "s|named_section|get_named_section|" ${worksrcpath}/trans-decl.c
    }
}

# g95 fails to build with clang
if {${configure.compiler} == "clang"} {
    configure.compiler llvm-gcc-4.2
}

pre-configure {
    file mkdir ${workpath}/gcc-${version_gcc}/${name}
    reinplace "s|tjmp %%edx|tjmp *%%edx|" ${workpath}/gcc-${version_gcc}/gcc/config/i386/i386.c
    if {${build_arch} == "i386"} {
        reinplace "s|i686|i386|" ${workpath}/gcc-${version_gcc}/gcc/config/i386/darwin.h
    }
    if {${os.major} != 8} {
        set gcc_configargs "--with-as=/usr/bin/as --with-ld=/usr/bin/ld --with-nm=/usr/bin/nm"
    } else {
        reinplace "s|/usr/bin/libtool|${prefix}/bin/odlibtool|" ${workpath}/gcc-${version_gcc}/gcc/config/darwin.h
        set gcc_configargs "--with-as=${prefix}/bin/odas --with-ld=${prefix}/bin/odld --with-nm=${prefix}/bin/odnm"
    }
    system "export CC=\"${configure.cc}\"; \
            export CPPFLAGS=\"${configure.cppflags}\"; \
            export CFLAGS=\"${configure.cflags} ${configure.cc_archflags}\"; \
            export CFLAGS_FOR_BUILD=\"${configure.cflags} ${configure.cc_archflags}\"; \
            export CFLAGS_FOR_TARGET=\"${configure.cflags}\"; \
            export LDFLAGS=\"${configure.ldflags} ${configure.cc_archflags}\"; \
            cd ${workpath}/gcc-${version_gcc}/${name}; \
            ../configure --prefix=${prefix}/${gcclib} \
                         --enable-languages=c \
                         --disable-multilib \
                         --with-included-gettext \
                         --with-libiconv=${prefix} \
                         --libdir=${prefix}/${gcclib} \
                         ${gcc_configargs} ${buildhost}; \
            make"
    if {[variant_isset gcc42]} {
        system "cd ${workpath}/gcc-${version_gcc}/${name}/gcc; \
                ar d libbackend.a options.o; \
                cd ${workpath}/gcc-${version_gcc}/gcc/; \
                patch -p0 < ${portpath}/files/patch-gcc.c.diff" 
    }
}

configure.args  --with-gcc-dir=${workpath}/gcc-${version_gcc} ${buildhost}

post-build {
    system "cd ${worksrcpath}/libf95.a-${version}; \
           export CC=\"${configure.cc}\"; \
           export CFLAGS=\"${configure.cflags} ${configure.cc_archflags}\"; \
           ./configure --prefix=${prefix} ${buildhost}; \
           make"
}

post-destroot {
    system "cd ${worksrcpath}/libf95.a-${version}; \
            make install DESTDIR=${destroot}; \
            ranlib ${destroot}${prefix}/${gcclib}/libf95.a; \
            ranlib ${destroot}${prefix}/${gcclib}/libgcc.a; \
            ranlib ${destroot}${prefix}/${gcclib}/libgcc_eh.a"
    xinstall -d ${destroot}${prefix}/share/doc/${name}
    xinstall -m 644 ${worksrcpath}/G95Manual.pdf ${destroot}${prefix}/share/doc/${name}
    system "ln -sf ${prefix}/${gcclib}/libf95.a ${destroot}${prefix}/lib/libf95.a"
}

platform darwin 8 {
    patchfiles-delete patch-host-darwin.c.diff patch-darwin-fallback.c.diff
    depends_lib-append port:odcctools
}

variant gcc42 description {link against gcc42} {
    master_sites-delete ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.0.4/:gcc
    master_sites-append ftp://gcc.gnu.org/pub/gcc/releases/gcc-4.2.4/:gcc
    checksums-delete    ${dcore} md5    193e0a7a471cca70e374974bc5a60137         \
                        ${dcore} sha1   55c664b33a4dc6b2130c47da28a1d6daab732ce9 \
                        ${dcore} rmd160 af67dc8385942450f6b27ad83a6b9e3d22f779a1
    checksums-append    ${dcore} md5     b7a4c167f6a05301591b139b356e36d0 \
                        ${dcore} sha1    540fa6949415ded321a7cbe80bbf8befe85e3566 \
                        ${dcore} rmd160  d54581d0a2ecc22718e6be101c6dc524a4a88463
    patchfiles-append   patch-lang.opt.diff patch-darwin.h.diff
    patchfiles-delete   patch-host-darwin.c.diff patch-darwin-fallback.c.diff

    post-configure {
        reinplace "s|\(f951_LDADD\)|(f951_LDADD) ${workpath}/gcc-${version_gcc}/g95/libdecnumber/*.o|" ${worksrcpath}/Makefile
    }

    pre-build {
        if {${os.arch}=="i386"} {
            reinplace "s|\(g95_LDADD\)|(g95_LDADD) ${workpath}/gcc-${version_gcc}/g95/gcc/driver-i386.o|" ${worksrcpath}/Makefile
        }
    }
}
