# $Id: Portfile 91468 2012-04-02 19:21:10Z jmr@macports.org $

PortSystem 1.0

name                    llvm-gcc42
version                 2.9
revision                1
categories              lang
platforms               darwin
license                 GPL-2+
maintainers             erickt mfeiri jeremyhu
description             llvm-gcc42 is a gcc compiler frontend for llvm
long_description        llvm-gcc42 integrates the LLVM optimizers and code \
                        generator with the GCC 4.2 parser. This allows LLVM to \
                        compile languages supported by the GCC compiler \
                        frontends, and provides high-fidelity drop-in \
                        compatibility with version 4.2 of GCC.

homepage                http://llvm.org/
master_sites            ${homepage}releases/${version}/

distname                llvm-gcc-4.2-${version}.source
extract.suffix          .tgz

checksums               md5     1a3951880d17f676d36b9edfdea8a727 \
                        sha1    6576576750c9b2afb42838851d51be0b91b12f3e \
                        rmd160  2f4329a5437b214595b66dc4e005ef1f9fea55a2

universal_variant       no

depends_lib             port:llvm-${version} port:gmp port:mpfr port:libiconv

worksrcdir              build

post-extract {
    file mkdir ${worksrcpath}
}

# the generated compiler doesn't accept -arch
configure.cc_archflags
configure.cxx_archflags
configure.objc_archflags
configure.ld_archflags

configure.cmd           ../${distname}/configure
configure.args          --enable-llvm=${prefix}/libexec/llvm-${version} \
                        --enable-languages=c,c++,objc,obj-c++,fortran \
                        --libdir=${prefix}/lib/${name} \
                        --includedir=${prefix}/include/${name} \
                        --infodir=${prefix}/share/info \
                        --mandir=${prefix}/share/man \
                        --with-local-prefix=${prefix} \
                        --with-system-zlib \
                        --disable-nls \
                        --program-prefix=llvm- \
                        --program-suffix=-4.2 \
                        --with-gmp=${prefix} \
                        --with-mpfr=${prefix} \
                        --with-build-time-tools=/usr/bin

build.target	        bootstrap

destroot.target	        install install-info-host

# gcc-4.2 doesn't build with -std=gnu99 (clang's default), so do this for now
if {${configure.compiler} == "clang"} {
    configure.compiler llvm-gcc-4.2
}

# gcc-4.0 produces a bad x86_64 slice, and Leopard's gcc-4.2 doesn't build this properly,
# so use MacPorts-provided apple-gcc-4.2 to build us on 10.[45]
if {${configure.compiler} == "gcc-4.0" || 
    (${configure.compiler} == "gcc-4.2" && ${os.major} < 10)} {
    depends_build-append port:apple-gcc42
    configure.compiler apple-gcc-4.2
}

# STAGE1_CFLAGS="-O2 -fkeep-inline-functions" : from configure.ac
# BOOT_CFLAGS="-O2 -g" : from Makefile.tpl
# -D_FORTIFY_SOURCE=0 : The built compiler might not understand newer builtins
#                       exposed in libc headers for fortified string functions.
#                       Yes, we need to set both BOOT_CFLAGS and STAGE1_CFLAGS
#                       because STAGE1_CFLAGS is used with xgcc for libgcc
build.args-append BOOT_CFLAGS="-O2 -g -D_FORTIFY_SOURCE=0" STAGE1_CFLAGS="-O2 -fkeep-inline-functions -D_FORTIFY_SOURCE=0"

post-destroot {
        system "cd ${destroot}${prefix}/bin/ && ln -s llvm-gcc-4.2 llvm-gcc"
        system "cd ${destroot}${prefix}/bin/ && ln -s llvm-g++-4.2 llvm-g++"

        file delete -force ${destroot}${prefix}/share/man/man7 \
            ${destroot}${prefix}/share/info

        # install/copy ffitarget.h only if we have it
        if {![catch {set ffitarget.h [glob ${destroot}${prefix}/lib/${name}/gcc/*/${version}/include/ffitarget.h]} result]} {
            file copy ${ffitarget.h} ${destroot}${prefix}/include/${name}/
        }
        xinstall -m 755 -d ${destroot}${prefix}/etc/select/gcc
        xinstall -m 444 ${filespath}/mp-llvm-gcc42 ${destroot}${prefix}/etc/select/gcc/
}

platform darwin {
    depends_run-append  port:ld64

    post-extract {
        system "rm -rf ${worksrcpath}/libstdc++-v3"
    }

    post-patch {
        if {${os.major} < 9} {
            reinplace "/vproc.h/d" ${workpath}/${distname}/gcc/libgcov.c
        }
    }

    if {${build_arch} == "i386" } {
        configure.pre_args-append --host=i686-apple-darwin${os.major}
    } else {
        configure.pre_args-append --host=${build_arch}-apple-darwin${os.major}
    }

    if {${os.major} < 10} {
        configure.args-append --with-gxx-include-dir=/usr/include/c++/4.0.0
    } else {
        configure.args-append --with-gxx-include-dir=/usr/include/c++/4.2.1
    }

    configure.args-append --with-slibdir=/usr/lib

    post-destroot {
        if {${build_arch} == "i386" } {
            file mkdir ${destroot}${prefix}/lib/llvm-gcc42/i686-apple-darwin${os.major}/4.2.1
            ln -s ../../../../bin/ld ${destroot}${prefix}/lib/llvm-gcc42/i686-apple-darwin${os.major}/4.2.1/ld
        } else {
            file mkdir ${destroot}${prefix}/lib/llvm-gcc42/${build_arch}-apple-darwin${os.major}/4.2.1
            ln -s ../../../../bin/ld ${destroot}${prefix}/lib/llvm-gcc42/${build_arch}-apple-darwin${os.major}/4.2.1/ld
        }

        # dont install slibs outside of ${prefix}
        foreach f [glob -nocomplain ${destroot}/usr/lib/*] {
            file rename $f ${destroot}${prefix}/lib/${name}/
        }
    }
}

livecheck.type	regex
livecheck.url   ${homepage}releases/
livecheck.regex {'(\d+\.\d+)'}
