# -*- 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

PortSystem                            1.0
PortGroup select                      1.0
PortGroup conflicts_build             1.0
PortGroup xcode_workaround            1.0

name                gcc5
epoch               2
version             5.5.0
revision            6
platforms           {darwin >= 10 < 15}
categories          lang
maintainers         nomaintainer
# 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            https://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 \
                    https://www.mirrorservice.org/sites/sources.redhat.com/pub/java/:ecj \
                    http://ftp-stud.fht-esslingen.de/pub/Mirrors/sourceware.org/java/:ecj

dist_subdir         gcc
distname            gcc-${version}
use_xz              yes

set ecj             ecj-4.9.jar

distfiles           ${distname}${extract.suffix}:gcc ${ecj}:ecj

extract.only        ${distname}${extract.suffix}

checksums           ${distname}${extract.suffix} \
                    rmd160  63fdc006c2289f81df664d1fd9b4124f71732e7b \
                    sha256  530cea139d82fe542b358961130c69cfde8b3d14556370b65823d2f91f0ced87 \
                    size    71096120 \
                    ${ecj} \
                    rmd160  eb1b19d9ac0e9e265bf993f38b9576e3c710e91e \
                    sha256  9506e75b862f782213df61af67338eb7a23c35ff425d328affc65585477d34cd \
                    size    1619429

if { ${configure.build_arch} eq "i386" } {

    # build with traditional "gas" assembler in all cases
    # upstream does not typically test gcc with clang as assembler on i386 systems
    # gcc configuration changes with assembler features
    # also innumerable warnings about text section naming suppressed
    patchfiles-append  patch-gcc5-disable-macports-cctools-as-changes.diff

    configure.env-append \
      "DISABLE_MACPORTS_AS_CLANG_SEARCH=1" \
      "DISABLE_XCODE_AS_CLANG_SEARCH=1"

    build.env-append \
      "DISABLE_MACPORTS_AS_CLANG_SEARCH=1" \
      "DISABLE_XCODE_AS_CLANG_SEARCH=1"
}

depends_lib         port:cctools \
                    port:gmp \
                    port:isl18 \
                    port:ld64 \
                    port:zlib \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr
depends_run         port:gcc_select \
                    port:libgcc6

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr ppl libmpc

# Work around parallel building issue on APFS
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81797
patchfiles-append   notparallel-install-headers.patch

# Provide linkage for MacPorts libiconv in gcj
patchfiles-append   gcj-libiconv-linkage.patch

# work around bug in 10.13 headers
patchfiles-append   headers-10.13-fix.patch

# See https://trac.macports.org/ticket/53662 for a similar issue
patchfiles-append   patch-libisl.diff
post-extract {
    ln -s ${prefix}/libexec/isl18/include/isl ${worksrcpath}/gcc
}

patchfiles-append   patch-gcj-unwind.diff

if {[vercmp ${xcodeversion} 10.2] >= 0} {
    # https://trac.macports.org/ticket/58260
    # Patch for Xcode bug, taken from
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89864#c43
    # https://github.com/Homebrew/homebrew-core/pull/39134/files
    patchfiles-append   xcode-bug-_Atomic-fix.patch 
}

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

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

platform darwin {
    if {${os.major} > 14} {
        # Hack around boehm-gc and associated gcj failures from recompilation
        # of libunwind.dylib with Apple Clang in macOS >= 10.11 by reducing
        # alignment to 2
        patchfiles-append   boehm-gc-darwin15-hack.patch
    }
    configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
}

platform darwin 11 {
    # work around a hiccup building this due to MacPort's cctools modifications
    # that forces the use of /usr/bin/clang as assembler, which doesn't work for gcc5 on 10.7.5
    # any clang >= 5.0 would work, but at present, all 10.7.5 users will have clang-9.0
    # https://trac.macports.org/ticket/62072
    # TODO: find a way to force "gas" as assembler
    depends_build-append port:clang-9.0
}

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
configure.args      --enable-languages=c,c++,objc,obj-c++,lto,fortran,java \
                    --libdir=${prefix}/lib/${name} \
                    --includedir=${prefix}/include/${name} \
                    --infodir=${prefix}/share/info \
                    --mandir=${prefix}/share/man \
                    --datarootdir=${prefix}/share/gcc-${major} \
                    --with-libiconv-prefix=${prefix} \
                    --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}/libexec/isl18 \
                    --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

# see https://lists.macports.org/pipermail/macports-dev/2017-August/036209.html
# --disable-tls does not limit functionality
# it only determines how std::call_once works
configure.args-append \
                    --disable-tls

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}"

    if {${configure.sdkroot} ne ""} {
        # We should be using --with-build-sysroot here.  Using --with-sysroot
        # changes the behavior of the installed gcc to look in that sysroot
        # by default instead of /.  Using --with-build-sysroot is supposed
        # to be used during the build but not impact the installed product.
        # Unfortunately, the build fails because the value doesn't get
        # plumbed everywhere it is supposed to.
        #
        # https://trac.macports.org/ticket/53726
        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79885
        #
        # Note that macports toolchains will fail to compile gcc unless
        # the DevSDK is installed due to another gcc build system bug:
        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80203
        configure.args-append --with-sysroot="${configure.sdkroot}"
    }

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

# gcc cannot build if libunwind-headers is active
conflicts_build-append libunwind-headers

# 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

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 (e.g. Leopard) or architectures (e.g. 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_exists universal] && [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}
                }
            }
        }
    }
    move ${destroot}${prefix}/lib/${name}/pkgconfig/libgcj-${major}.pc ${destroot}${prefix}/lib/pkgconfig/
}

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" \
    "CXXCPP=${configure.cxx} -E"
build.env-append \
    "CPP=${configure.cc} -E" \
    "CXXCPP=${configure.cxx} -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       https://ftp.gnu.org/gnu/gcc/
livecheck.regex     gcc-(${major}\\.\[0-9.\]+)/
