# -*- 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 51050 2009-05-17 01:13:19Z takeshi@macports.org $

PortSystem 1.0

name            g95
version         0.91
revision        1
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

set version_gcc 4.0.4
set dcore       gcc-core-${version_gcc}.tar.bz2
dist_subdir     gcc40
set dg95        ${name}_source.tgz
set gcclib      lib/gcc-lib/${os.arch}-apple-${os.platform}${os.version}/${version_gcc}
distfiles       ${dcore}:gcc ${dg95}:g95

checksums       ${dcore} md5    193e0a7a471cca70e374974bc5a60137         \
                ${dcore} sha1   55c664b33a4dc6b2130c47da28a1d6daab732ce9 \
                ${dcore} rmd160 af67dc8385942450f6b27ad83a6b9e3d22f779a1 \
                ${dg95}  md5    35e1852e09b69555bc07f30d0ab4b6ba         \
                ${dg95}  sha1   aa58510c04e357439a4bf34c54d380e9079395d5 \
                ${dg95}  rmd160 f0a8325c0e930d69d2def16eaa3c6c21dfd2b5a8

depends_lib     port:odcctools

patchfiles      patch-Makefile.in.diff

universal_variant no

extract.only    ${dg95}

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

post-extract {
    system "tar jxvf ${distpath}/${dcore} -C ${workpath}"
}

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 {[variant_isset darwin_9]} {
        reinplace "s|i686|i386|" ${workpath}/gcc-${version_gcc}/gcc/config/i386/darwin.h
        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 "unset CPPFLAGS; unset LDFLAGS; \
            export CPPFLAGS=-I${prefix}/include; \
            cd ${workpath}/gcc-${version_gcc}/${name}; \
            ../configure --prefix=${prefix}/${gcclib} --with-libiconv-prefix=${prefix}/include \
            --enable-languages=c ${gcc_configargs} --with-included-gettext; \
            make"
}

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

post-build {
    system "cd ${worksrcpath}; tar zxvf libf95.a-${version}.tar.gz; \
           cd libf95.a-${version}; \
           export CFLAGS='-O2 -fno-common'; \
           ./configure --prefix=${prefix}; \
           make"
}

pre-destroot {
    reinplace "s|${prefix}/${gcclib}|${destroot}${prefix}/${gcclib}|g" ${worksrcpath}/Makefile
    reinplace "s|${prefix}/${gcclib}|${destroot}${prefix}/${gcclib}|g" ${worksrcpath}/libf95.a-${version}/Makefile
}

destroot {
    system "cd ${worksrcpath}; \
           make install prefix=${destroot}${prefix}; \
           cd libf95.a-${version}; \
           make install prefix=${destroot}${prefix}; \
           ranlib ${destroot}${prefix}/${gcclib}/libf95.a; \
           ranlib ${destroot}${prefix}/${gcclib}/libgcc.a; \
           ranlib ${destroot}${prefix}/${gcclib}/libgcc_eh.a"
    file mkdir ${destroot}${prefix}/share/doc/${name}
    file rename ${destroot}${prefix}/G95Manual.pdf ${destroot}${prefix}/share/doc/${name}
    system "ln -sf ${prefix}/${gcclib}/libf95.a ${destroot}${prefix}/lib/libf95.a"
    file mkdir ${destroot}${prefix}/${gcclib}/lib
    system "ln -sf ${prefix}/${gcclib}/libgcc_s.1.0.dylib ${destroot}${prefix}/${gcclib}/lib/libgcc_s.1.0.dylib"
    system "ln -sf ${prefix}/${gcclib}/libgcc_s.1.0.dylib ${destroot}${prefix}/${gcclib}/lib/libgcc_s.dylib"
}

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