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

PortSystem 1.0
PortGroup select 1.0
PortGroup compiler_blacklist_versions 1.0

name                gcc7
subport             libgcc-devel {}

epoch               1
version             7-20161023
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, prerelease BETA
long_description    The GNU compiler collection, including front ends for \
                    C, C++, Objective-C, Objective-C++, and Fortran. \
                    This is a prerelease BETA version!

homepage            http://gcc.gnu.org/
master_sites        ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/gcc/snapshots/${version}/ \
                    ftp://gcc.gnu.org/pub/gcc/snapshots/${version}/ \
                    http://gcc.skazkaforyou.com/snapshots/${version}/

distname            gcc-${version}
use_bzip2           yes

checksums           rmd160  769a4c3ecc1664521f03b477d9d65a53fce01ee6 \
                    sha256  73af56dc08d1377c334584744aa07553c8ea87658ce26b79307b674e55ce5d08

depends_lib         port:cctools \
                    port:gmp \
                    path:lib/pkgconfig/isl.pc:isl \
                    port:ld64 \
                    port:libgcc-devel \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr ppl libmpc

set major           [lindex [split ${version} .-] 0]

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++,lto,fortran \
                    --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-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} \
                    --with-isl=${prefix} \
                    --enable-stage1-checking \
                    --disable-multilib \
                    --enable-lto \
                    --enable-libstdcxx-time \
                    --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}

# https://trac.macports.org/ticket/47996
compiler.blacklist-append {clang < 300}

# "-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} eq "libgcc-devel"} {
    conflicts       libgcc

    # 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_lib-delete port:libgcc-devel

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

    # 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

        # Note that we really don't want to include libgcc_ext.10.[45].dylib here, but install_name_tool
        # doesn't know how to change the id of stubs, and it's easier than recreating them for each
        # gcc port.
        set dylibs {libgcc_ext.10.4.dylib libgcc_ext.10.5.dylib libgcc_s.1.dylib libgfortran.3.dylib libquadmath.0.dylib libstdc++.6.dylib libobjc-gnu.4.dylib libgomp.1.dylib libitm.1.dylib libssp.0.dylib libasan.0.dylib libatomic.1.dylib}

        foreach dylib ${dylibs} {
            # 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/libgcc/${dylib}]} {
                continue
            }

            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
            if {! [string match *libgcc_ext* ${dylib}]} {
                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

        # For binary compatibility with binaries that linked against the old libstdcxx port
        ln -s libgcc/libstdc++.6.dylib ${destroot}${prefix}/lib/libstdc++.6.dylib
    }
} else {
depends_run-append         port:gcc_select

post-destroot {
    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 libquadmath.0.dylib libstdc++.6.dylib libobjc-gnu.4.dylib libgomp.1.dylib libitm.1.dylib libssp.0.dylib libasan.0.dylib libatomic.1.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 powerpc {
    configure.universal_archs ppc ppc64
}
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-append ${configure.cxx_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       ftp://gcc.gnu.org/pub/gcc/snapshots/
livecheck.regex     LATEST-${major} -> (${major}-\[0-9\]+)