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

epoch               5
name                gcc8
version             8.5.0
revision            2
subport             libgcc8 { revision 1 }
if {${subport} eq "gcc8"} {
    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++ and Fortran.

homepage            https://gcc.gnu.org/
master_sites        ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/gcc/releases/gcc-${version}/ \
                    http://mirror.koddos.net/gcc/releases/gcc-${version}/ \
                    https://www.mirrorservice.org/sites/ftp.gnu.org/gnu/gcc/gcc-${version}/ \
                    ftp://ftp.gwdg.de/pub/linux/gcc/releases/gcc-${version}/ \
                    ftp://gcc.ftp.nluug.nl/mirror/languages/gcc/releases/gcc-${version}/ \
                    ftp://gcc.gnu.org/pub/gcc/releases/gcc-${version}/ \
                    gnu:gcc/gcc-${version}

distname            gcc-${version}
use_xz              yes

checksums           rmd160  e7f3dedec67864006bf9f7e358817ce713b4476e \
                    sha256  d308841a511bb830a6100397b0042db24ce11f642dab6ea6ee44842e5325ed50 \
                    size    63841008

# Check if this is the last supported gcc version for this system.
# If it is, libgcc8 installs a full runtime, otherwise it only installs
# what is missing from newer libgccX builds.
# NOTE : The logic here must match that in the libgcc port.
#set isLastSupported [ expr ${os.major} < 10 ]

# libgcc8 is currently not the last supported libgcc for any system
set isLastSupported false

patchfiles          patch-fix-libgccjit-soname

if { ${os.major} > 19 } {
    # https://trac.macports.org/ticket/60908
    # Patch for macOS 11 (Darwin20) version numbering taken from
    # https://github.com/iains/gcc-darwin-arm64/commit/556ab512
    patchfiles-append         big-sur-version-fix.diff
    # Build doesn't understand a 11.0 deployment target so force to 10.16
    macosx_deployment_target  10.16
    # Fix detection of sanitizer support
    # https://trac.macports.org/ticket/61494
    patchfiles-append         fix-sanitisers-darwin20.diff
}

if { ${configure.build_arch} eq "i386" && ${os.major} >= 10 } {

    # fix no-pie clang bug bootstrapping gcc on i386
    # https://trac.macports.org/ticket/63161
    patchfiles-append  patch-gcc10-i686-clang-bootstrap-fix.diff
}

# Since GCC 7.4.0, during configure, it detects features supported by target-as.
# On the other hand, MacPorts cctools contain a proxy for 'as' that runs system
# 'as' or one of the supported MacPorts clang's 'as' if it is installed. Here,
# we may encounter a misconfiguration when GCC builds on a system with some
# MacPorts clang, and 'as' is using it. However, on a clean system, it uses
# system 'as' if no MacPorts clang is installed, which may behave differently.
# This can make GCC broken until MacPorts clang is installed. To avoid a stealth
# dependency on the used clang, I enforce building with system 'as'.
# See:
#  - https://trac.macports.org/ticket/68683
#  - https://github.com/gcc-mirror/gcc/commit/b410cf1dc056aab195c5408871ffca932df8a78a
patchfiles-append   patch-gcc7-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 \
                    path:lib/pkgconfig/isl.pc:isl \
                    port:ld64 \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr \
                    port:zlib
depends_run         port:gcc_select
if { ${isLastSupported} } {
    depends_run-append  path:share/doc/libgcc/README:libgcc
} else {
    depends_run-append  port:libgcc8
}

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

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

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

set gcc_configure_langs {c c++ objc obj-c++ lto fortran}
if {${subport} eq "gcc8"} {

    # the jit is not building on i386 systems
    # see https://trac.macports.org/ticket/61130
    if {${build_arch} ne "i386"} {
        lappend gcc_configure_langs jit
    }

}

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
configure.args      --enable-languages=[join ${gcc_configure_langs} ","] \
                    --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 \
                    --enable-host-shared

# 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

# on 10.6.8 building with the system gcc compilers leads to
# bootstrap comparison failures
platform darwin 10 {
    compiler.blacklist-append *gcc-3.* *gcc-4.*
}

if { ${os.platform} eq "darwin" } {
    if { [vercmp ${xcodeversion} >= 12.5] || [vercmp ${xcodecltversion} >= 12.5]} {
        pre-configure {
            ui_warn "Applying '--without-build-config' workaround to Xcode ${xcodeversion} / CLT ${xcodecltversion}"
            ui_warn "If versions > 12.5 please check if it is still required"
        }
        # gcc has build issues on macOS 11.3 with the use of Xcode clang via cctools for ld
        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100340
        # https://trac.macports.org/ticket/62775
        configure.args-append  --without-build-config
    }
}

# cctools' as may pick MacPorts clang when it's instaled on the system,
# which leads to configure GCC for as from modern clang, and not system one.
# When it had happened it makes a stealth dependency for any new enough clang on old system.
# See:
#  - https://trac.macports.org/ticket/68683
#  - https://github.com/gcc-mirror/gcc/commit/b410cf1dc056aab195c5408871ffca932df8a78a
configure.env-append \
                    DISABLE_MACPORTS_AS_CLANG_SEARCH=1
build.env-append    DISABLE_MACPORTS_AS_CLANG_SEARCH=1

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
        configure.args-append --with-sysroot="[regsub {MacOSX1[1-9]\.[0-9]+\.sdk} ${configure.sdkroot} {MacOSX.sdk}]"
    }

    # 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

# On Darwin19+ fails to build for Xcode < 11.3
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90835
# https://trac.macports.org/ticket/59351
# for more background on the issue.
if { ${os.major} >= 19 } {
    if {[vercmp ${xcodeversion} < 11.3]} {
        known_fail  yes
        pre-fetch {
            ui_error "${name} ${version} is (currently) not supported on Darwin ${os.major} Xcode ${xcodeversion}"
            ui_error "See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90835 and"
            ui_error "    https://trac.macports.org/ticket/59351 for more details"
            ui_error "To install this port upgrade Xcode to 11.3 or newer"
            return -code error "incompatible Xcode version"
        }
    }
}

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

# list of dylibs to be installed
# 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.5.dylib libquadmath.0.dylib libstdc++.6.dylib libobjc-gnu.4.dylib libgomp.1.dylib libitm.1.dylib libssp.0.dylib libasan.5.dylib libubsan.1.dylib libatomic.1.dylib}

if {${subport} eq "libgcc8"} {

    # Are we providing the full main runtime ?
    if { ${isLastSupported} } {

        # Always provides primary runtime so always in conflict
        conflicts libgcc-devel

        # 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
        depends_lib-delete   port:zlib
        depends_build-append {*}${depends_lib}
        depends_lib          port:zlib

        configure.args-replace \
            --libdir=${prefix}/lib/${name} \
            --libdir=${prefix}/lib/libgcc

        # see https://trac.macports.org/ticket/54766
        configure.args-replace \
            --includedir=${prefix}/include/${name} \
            --includedir=${prefix}/include/gcc

        configure.args-replace \
            --with-gxx-include-dir=${prefix}/include/${name}/c++/ \
            --with-gxx-include-dir=${prefix}/include/gcc/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

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

                move ${destroot}${prefix}/lib/libgcc/${dylib} ${destroot}${prefix}/lib/libgcc.merged
                if {[variant_exists universal] && [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 [glob -tails -directory ${archdir} *.dylib] {
                                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}/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 {

        # gcc8 runtime versions are identical to that in gcc9, so libgcc8 does not
        # need to provide anything, hence do not build anything.
        # port still defined as needed to satisfy dependency tree

        platforms {darwin < 15}

        depends_run port:libgcc9
        depends_lib

        fetch.type    none
        build         { }
        use_configure no
        patchfiles

        destroot {
            set doc_dir ${destroot}${prefix}/share/doc/${subport}
            xinstall -m 755 -d ${doc_dir}
            system "echo ${subport} provides no runtime > ${doc_dir}/README"
        }

    }

} else {

    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 ${dylibs} {
            # 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}
                    }
                }
            }
        }
    }

    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.\]+)/
