# $Id$

PortSystem 1.0
PortGroup select 1.0
PortGroup compiler_blacklist_versions 1.0

# Whenever this port is bumped for version/revision, please revbump dragonegg-3.[34]-gcc-4.5
name                gcc45
subport             libgcc45 {}

epoch               1
version             4.5.4
revision            12
platforms           darwin
categories          lang
maintainers         mww openmaintainer
# an exception in the license allows dependents to not be GPL
license             {GPL-3+ Permissive}
description         The GNU compiler collection
long_description    The GNU compiler collection, including front ends for \
                    C, C++, Objective-C, Objective-C++, Fortran, and Java.

homepage            http://gcc.gnu.org/
master_sites        ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/gcc/releases/gcc-${version}/:gcc \
                    ftp://ftp.gwdg.de/pub/linux/gcc/releases/gcc-${version}/:gcc \
                    ftp://gcc.ftp.nluug.nl/mirror/languages/gcc/releases/gcc-${version}/:gcc \
                    ftp://gcc.gnu.org/pub/gcc/releases/gcc-${version}/:gcc \
                    gnu:gcc/gcc-${version}:gcc \
                    ftp://sourceware.org/pub/java/:ecj \
                    http://mirrors.kernel.org/sources.redhat.com/java/:ecj \
                    http://www.mirrorservice.org/sites/sources.redhat.com/pub/java/:ecj \
                    http://ftp-stud.fht-esslingen.de/pub/Mirrors/sourceware.org/java/:ecj

use_bzip2           yes

set dcore           gcc-core-${version}.tar.bz2
set dfort           gcc-fortran-${version}.tar.bz2
set dcxx            gcc-g++-${version}.tar.bz2
set djava           gcc-java-${version}.tar.bz2
set dobjc           gcc-objc-${version}.tar.bz2

set ecj             ecj-4.5.jar

distfiles           ${dcore}:gcc ${dfort}:gcc ${dcxx}:gcc ${djava}:gcc ${dobjc}:gcc ${ecj}:ecj
extract.only        ${dcore} ${dfort} ${dcxx} ${djava} ${dobjc}

checksums           gcc-core-${version}.tar.bz2 \
                    rmd160  558a68fad29011b8ba04e1c103eb204bd1e54d5a \
                    sha256  894d90f72cbfc8707e330fa2b1847c443fa97cf9b7f26e86be554709510e624a \
                    gcc-fortran-${version}.tar.bz2 \
                    rmd160  98a4a816e8f0ec8f3081d99af5460fe1afea6ade \
                    sha256  cf0803b4f5334a526dd8da1b2171d6724f50c17346b2d5c509b6abc973d2ef34 \
                    gcc-g++-${version}.tar.bz2 \
                    rmd160  9003882b806822ea7c5c114d060904464c07ca7a \
                    sha256  f7e3bab5e22713fbce46c2acb35fb727748473a34a942ce00393d75e1b09b6b9 \
                    gcc-java-${version}.tar.bz2 \
                    rmd160  719bfe5fd93542e7e3907cce23362c52eef9009c \
                    sha256  c98398da6f8c4c7d1758e291089d99853fdd27112cd3f146b7a8c26e6d762a23 \
                    gcc-objc-${version}.tar.bz2 \
                    rmd160  6c5bf2b75cb61105bfa3fd4c0d2462871c687495 \
                    sha256  92e7f8894a2f372a77d30732831c366289cea74f2a56c53cfec9de2ace413c92 \
                    ecj-4.5.jar \
                    rmd160  d3f4da657f086b6423f74e93f001132f4855368a \
                    sha256  98fd128f1d374d9e42fd9d4836bdd249c6d511ebc6c0df17fbc1b9df96c3d781

depends_build       bin:makeinfo:texinfo
depends_lib         port:cctools \
                    port:gmp \
                    port:ld64 \
                    path:lib/libgcc/libgcc_s.1.dylib:libgcc \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr
depends_run         port:gcc_select \
                    port:libgcc45

depends_skip_archcheck-append texinfo gcc_select ld64 cctools
license_noconflict  gmp mpfr ppl libmpc

patchfiles          ppc_fde_encoding.diff \
                    texinfo5.patch

# Handle OS X deployment targets correctly (GCC PR target/63810
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63810>).
patchfiles-append   macosx-version-min.patch

# Don't link with "-flat_namespace -undefined suppress" on Yosemite and
# later (#45483).
patchfiles-append   yosemite-libtool.patch

set major           [join [lrange [split ${version} .-] 0 1] .]

worksrcdir          gcc-${version}

post-extract {
    copy ${distpath}/${ecj} ${worksrcpath}/ecj.jar
}

platform darwin {
    configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
}

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
configure.args      --enable-languages=c,c++,objc,obj-c++,fortran,java \
                    --libdir=${prefix}/lib/${name} \
                    --includedir=${prefix}/include/${name} \
                    --infodir=${prefix}/share/info \
                    --mandir=${prefix}/share/man \
                    --datarootdir=${prefix}/share/gcc-${major} \
                    --with-local-prefix=${prefix} \
                    --with-libiconv-prefix=${prefix} \
                    --with-system-zlib \
                    --disable-nls \
                    --program-suffix=-mp-${major} \
                    --with-gxx-include-dir=${prefix}/include/${name}/c++/ \
                    --with-gmp=${prefix} \
                    --with-mpfr=${prefix} \
                    --with-mpc=${prefix} \
                    --enable-stage1-checking \
                    --disable-multilib \
                    --enable-plugin \
                    --with-build-config=bootstrap-debug \
                    --with-as=${prefix}/bin/as \
                    --with-ld=${prefix}/bin/ld \
                    --with-ar=${prefix}/bin/ar \
                    --with-bugurl=https://trac.macports.org/newticket

configure.env-append \
                    AR_FOR_TARGET=${prefix}/bin/ar \
                    AS_FOR_TARGET=${prefix}/bin/as \
                    LD_FOR_TARGET=${prefix}/bin/ld \
                    NM_FOR_TARGET=${prefix}/bin/nm \
                    OBJDUMP_FOR_TARGET=${prefix}/bin/objdump \
                    RANLIB_FOR_TARGET=${prefix}/bin/ranlib \
                    STRIP_FOR_TARGET=${prefix}/bin/strip \
                    OTOOL=${prefix}/bin/otool \
                    OTOOL64=${prefix}/bin/otool

pre-configure {
    configure.args-append --with-pkgversion="MacPorts ${name} ${version}_${revision}${portvariants}"

    # TODO: Remove when base automatically creates configure.dir (2.2.1?).
    file mkdir ${configure.dir}
}

# http://trac.macports.org/ticket/29067
compiler.blacklist-append gcc-4.0

# http://trac.macports.org/ticket/29104
compiler.blacklist-append {llvm-gcc-4.2 < 2336.1}

# LTO doesn't work for other targets, see ticket #32863
if {![variant_isset universal] && ${build_arch} eq "x86_64"} {
    configure.args-append --enable-lto
}

# "-stdlib" would be passed on to the bootstrap compiler if present
configure.cxx_stdlib

build.dir           ${configure.dir}
build.target        bootstrap-lean
use_parallel_build  yes

destroot.target     install install-info-host

if {${subport} == "libgcc45"} {
    # http://trac.macports.org/ticket/35770
    # http://trac.macports.org/ticket/38814
    # While there can be multiple versions of these runtimes in a single
    # process, it is not possible to pass objects between different versions,
    # so we simplify this by having the libgcc port provide the newest version
    # of these runtimes for all versions of gcc to use.
    #
    # If there is a binary incompatible change to the runtime in a future
    # version of gcc, then the latest version of gcc to provide a given ABI
    # version should continue to provide a subport for that and older gcc
    # versions.

    depends_run-delete port:libgcc45

    configure.args-delete --libdir=${prefix}/lib/${name}
    configure.args-append --libdir=${prefix}/lib/libgcc

    configure.args-delete --enable-languages=c,c++,objc,obj-c++,fortran,java
    configure.args-append --enable-languages=c,c++,objc,obj-c++

    # TODO: Possibly disable bootstrap with appropriate configure flags.
    #       the problem is that libstdc++'s configure script tests for tls support
    #       using the running compiler (not gcc for which libstdc++ is being built).
    #       Thus when we build with clang, we get a mismatch
    # http://trac.macports.org/ticket/36116
    #compiler.blacklist-append {clang < 425}
    #configure.args-append --disable-bootstrap
    #build.target        all

    post-destroot {
        file mkdir ${destroot}${prefix}/lib/libgcc.merged

        set dylibs {libobjc-gnu.2.dylib}

        foreach dylib ${dylibs} {
            move ${destroot}${prefix}/lib/libgcc/${dylib} ${destroot}${prefix}/lib/libgcc.merged
            if {[variant_isset universal]} {
                foreach archdir [glob ${destroot}${prefix}/lib/libgcc/*/] {
                    set archdir_nodestroot [string map "${destroot}/ /" ${archdir}]
                    if {[file exists ${archdir}/${dylib}]} {
                        system "install_name_tool -id ${prefix}/lib/libgcc/${dylib} ${archdir}/${dylib}"
                        foreach link ${dylibs} {
                            system "install_name_tool -change ${archdir_nodestroot}${link} ${prefix}/lib/libgcc/${link} ${archdir}/${dylib}"
                        }
                        system "lipo -create -output ${destroot}${prefix}/lib/libgcc.merged/${dylib}~ ${destroot}${prefix}/lib/libgcc.merged/${dylib} ${archdir}/${dylib} && mv ${destroot}${prefix}/lib/libgcc.merged/${dylib}~ ${destroot}${prefix}/lib/libgcc.merged/${dylib}"
                    }
                }
            }

            # strip debug symbols to supress debugger warnings:
            # http://trac.macports.org/attachment/ticket/34831
            system "strip -x ${destroot}${prefix}/lib/libgcc.merged/${dylib}"
        }

        file delete -force ${destroot}${prefix}/bin              
        file delete -force ${destroot}${prefix}/share
        file delete -force ${destroot}${prefix}/include
        file delete -force ${destroot}${prefix}/lib/libgcc
        file delete -force ${destroot}${prefix}/libexec

        move ${destroot}${prefix}/lib/libgcc.merged ${destroot}${prefix}/lib/libgcc
    }
} else {
post-destroot {
    # 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}/
    }
    file rename ${destroot}${prefix}/share/python/aotcompile.py \
                ${destroot}${prefix}/share/python/aotcompile-45.py
    file rename ${destroot}${prefix}/share/python/classfile.py \
                ${destroot}${prefix}/share/python/classfile-45.py

    # darwin-sections.def gets missed on install for some reason
    if {${os.arch} ne "powerpc"} {
        eval file copy ${worksrcpath}/../gcc-${version}/gcc/config/darwin-sections.def [glob ${destroot}${prefix}/lib/${name}/gcc/*/${version}/plugin/include/config]
    }

    file delete ${destroot}${prefix}/share/info/dir

    foreach file [glob ${destroot}${prefix}/share/{info,man/man7}/*] {
        set extension [file extension ${file}]
        set newfile [regsub "${extension}$" ${file} "-mp-${major}${extension}"]
        
        file rename ${file} ${newfile}
    }

    foreach dylib {libgcc_ext.10.4.dylib libgcc_ext.10.5.dylib libgcc_s.1.dylib libgfortran.3.dylib libstdc++.6.dylib libobjc-gnu.2.dylib libgomp.1.dylib libssp.0.dylib} {
        # Different OS versions (eg: Leopard) or architectures (eg: PPC) don't produce all the dylibs
        # https://trac.macports.org/ticket/40098
        # https://trac.macports.org/ticket/40100
        if {[file exists ${destroot}${prefix}/lib/${name}/${dylib}]} {
            delete ${destroot}${prefix}/lib/${name}/${dylib}
            ln -s ${prefix}/lib/libgcc/${dylib} ${destroot}${prefix}/lib/${name}/${dylib}
        }

        if {[variant_isset universal]} {
            foreach archdir [glob ${destroot}${prefix}/lib/${name}/*/] {
                if {[file exists ${archdir}/${dylib}]} {
                    delete ${archdir}/${dylib}
                    ln -s ${prefix}/lib/libgcc/${dylib} ${archdir}/${dylib}
                }
            }
        }
    }
}

select.group        gcc
select.file         ${filespath}/mp-${name}

}

platform darwin {
    if {${os.major} == 8} {
        # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45248
        configure.args-append --with-dwarf2
    }
}

platform powerpc {
    configure.universal_archs ppc ppc64

    # https://trac.macports.org/ticket/39655
    configure.args-delete --enable-plugin
}
platform i386 {
    configure.universal_archs i386 x86_64
}
variant universal {
    configure.args-delete --disable-multilib
}
# the generated compiler doesn't accept -arch
configure.env-append CPP="${configure.cc} -E"
build.env-append CPP="${configure.cc} -E"
configure.cc-append [get_canonical_archflags]
configure.cc_archflags
configure.cxx_archflags
configure.objc_archflags
configure.ld_archflags
configure.universal_cflags
configure.universal_cxxflags
configure.universal_ldflags
configure.universal_args

livecheck.type      regex
livecheck.url       http://ftp.gnu.org/gnu/gcc/
livecheck.regex     gcc-(${major}\\.\[0-9.\]+)/