# -*- 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           compiler_blacklist_versions  1.0
PortGroup           active_variants              1.1
PortGroup           conflicts_build              1.0
PortGroup           cltversion                   1.0

epoch               0

name                gcc11

# Note, ARM builds have their own version below...
version             11.2.0
revision            3

platforms           darwin
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/

# Primary releases
master_sites        https://ftpmirror.gnu.org/gcc/gcc-${version}/ \
                    https://mirror.its.dal.ca/gnu/gcc/gcc-${version}/ \
                    https://mirrors.kernel.org/gnu/gcc/gcc-${version}/ \
                    https://www.mirrorservice.org/sites/ftp.gnu.org/gnu/gcc/gcc-${version}/ \
                    https://ftp-stud.hs-esslingen.de/pub/Mirrors/ftp.gnu.org/gcc/gcc-${version}/ \
                    https://mirror.yongbok.net/gnu/gcc/gcc-${version}/ \
                    http://mirror.koddos.net/gcc/releases/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}

# Use specific patched version, taken from homebrew comment
# https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/gcc.rb
# Branch from the Darwin maintainer of GCC with Apple Silicon support
if { ${os.arch} eq "arm" } {
    version         11.2.0
    revision        1
    master_sites    https://github.com/fxcoudert/gcc/archive
    distname        gcc-${version}-arm-20211126
    checksums       rmd160  ce5d0621028c8ff2bf3ab677b29f6c14e4c86dfe \
                    sha256  39416a5a2a0e00c5096937c456ca7632958271b438398184b9a55f6d437f681d \
                    size    125611219
} else {
    distname        gcc-${version}
    use_xz          yes
    checksums       rmd160  0fdd0b2c0954ccbd32e24f027d7b55fd26dcc627 \
                    sha256  d08edc536b54c372a1010ff6619dd274c0f1603aa49212ba20f7aa2cda36fa8b \
                    size    80888824

    if { ${os.platform} eq "darwin" } {
        # Add preliminary Darwin 21 support to GCC 11.
        # This should be removed, when GCC 11 supports Darwin 21 officially and the
        # respective version is used by macports.
        # See also: https://github.com/iains/gcc-darwin-arm64/commit/20f61faaed3b335d792e38892d826054d2ac9f15
        patchfiles-append patch-darwin21-support.diff
    }
}

patchfiles-append patch-darwin.c.diff patch-darwin.h.diff

subport             libgcc11 { revision [ expr ${revision} + 0 ] }

    depends_lib-append      port:gcc10-bootstrap
    configure.cc            ${prefix}/libexec/gcc10-bootstrap/bin/gcc
    configure.cxx           ${prefix}/libexec/gcc10-bootstrap/bin/g++

patchfiles-append   patch-Make-lang.in.diff

if { ${os.platform} eq "darwin" } {
    # This following patch works around a problem on MacOS 12.0 with fortran
    # See also: https://gcc.gnu.org/git/?p=gcc.git;a=patch;h=fabe8cc41e9b01913e2016861237d1d99d7567bf
    patchfiles-append patch-darwin21-fortran.diff
}

if { ${os.platform} eq "darwin" && ${os.major} >= 17 } {
    # On macOS 10.13 and later, compilation is extremely slow if SIP is
    # disabled; e.g. from ~30m with SIP enabled, to ~6 hours if disabled.
    #
    # This is caused by gcc's build system setting DYLD_LIBRARY_PATH to ensure
    # that the toolchain picks up newly built library versions, which seems to
    # trigger a pathologically slow path in dyld's library resolution.
    #
    # Since dyld ignores DYLD_LIBRARY_PATH when SIP is enabled, the issue is
    # avoided entirely, but at the cost of potentially falling back to incorrect
    # system library paths. The correct, SIP-compatible solution would be gcc
    # adopting @rpath.
    #
    # However, given that SIP is enabled by default in macOS, including on our
    # build bots, we already have to assume that ignoring DYLD_LIBRARY_PATH will
    # still produce a valid toolchain.
    #
    # Given that, we go ahead and disable DYLD_LIBRARY_PATH explicitly, avoiding
    # otherwise unworkably long build-times for hosts with SIP disabled.
    #
    # See also: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84257
    patchfiles-append patch-disable-dyld_library_path.diff
}

if { ${configure.build_arch} eq "i386" } {
    # fix no-pie clang bug bootstrapping gcc on i386
    patchfiles-append patch-iains-i686-clang-bootstrap.diff
}

depends_build-append \
                    port:texinfo
depends_lib-append  port:cctools \
                    port:gmp \
                    path:lib/pkgconfig/isl.pc:isl \
                    port:ld64 \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr \
                    port:zlib
depends_run-append  port:gcc_select \
                    path:share/doc/libgcc/README:libgcc

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}

set gcc_as ${prefix}/bin/as
set gcc_ld ${prefix}/bin/ld
set gcc_ar ${prefix}/bin/ar
set gcc_nm ${prefix}/bin/nm

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-bugurl=https://trac.macports.org/newticket \
                    --enable-host-shared \
                    --with-tune-cpu=G5

# 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

# Disable ccache
configure.ccache    no

if { ${os.platform} eq "darwin" } {
    # gcc has build issues on macOS 11.3 with the use of Xcode 12.5 clang via cctools for ld
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100340
    # https://trac.macports.org/ticket/62775
    if { ([vercmp ${xcodeversion} 12.5] >= 0 && [vercmp ${xcodeversion} 13] < 0) || \
         ([vercmp ${cltversion} 12.5] >= 0 && [vercmp ${cltversion} 13] < 0) } \
    {
        # Skip bootstrap comparison entirely
        post-patch {
            reinplace {s|^do-compare =|do-compare = /usr/bin/true|g} \
                ${worksrcpath}/Makefile.in \
                ${worksrcpath}/config/bootstrap-debug.mk \
                ${worksrcpath}/config/bootstrap-debug-lean.mk \
                ${worksrcpath}/config/bootstrap-debug-lib.mk
        }
    }
}

configure.args-append \
                    --with-as=${gcc_as} \
                    --with-ld=${gcc_ld} \
                    --with-ar=${gcc_ar} \

configure.env-append \
                    AR_FOR_TARGET=${gcc_ar} \
                    AS_FOR_TARGET=${gcc_as} \
                    LD_FOR_TARGET=${gcc_ld} \
                    NM_FOR_TARGET=${gcc_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 {

    # Set package info
    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

        # if the sdkroot is one of the current, rapidly changing SDKS, use the generic one
        configure.args-append --with-sysroot="[regsub {MacOSX1[1-9]\.[0-9]+\.sdk} ${configure.sdkroot} {MacOSX.sdk}]"
    }

}

# https://trac.macports.org/ticket/29067
# https://trac.macports.org/ticket/29104
# https://trac.macports.org/ticket/47996
# https://trac.macports.org/ticket/58493
compiler.blacklist-append {clang < 800} gcc-4.0 *gcc-4.2 {llvm-gcc-4.2 < 2336.1} {macports-clang-3.[4-7]}

# https://build.macports.org/builders/ports-10.13_x86_64-builder/builds/105513/steps/install-port/logs/stdio
# c++/v1/functional:1408:2: error: no member named 'fancy_abort' in namespace 'std::__1'; did you mean simply 'fancy_abort'?
compiler.blacklist-append {clang < 1000}

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

# Is this gcc release supported here.
if { ${os.platform} eq "darwin" && ${os.major} < 9 } {
    known_fail   yes
    pre-fetch {
        ui_error "${name} ${version} is not supported on Darwin ${os.major}"
        return -code error "incompatible macOS 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 libgcc_s.1.1.dylib \
                libgcc_s.2.dylib libgfortran.5.dylib libquadmath.0.dylib libstdc++.6.dylib \
                libobjc-gnu.4.dylib libgomp.1.dylib libitm.1.dylib libssp.0.dylib libasan.6.dylib \
                libubsan.1.dylib libatomic.1.dylib}

if { ${subport} ne ${name}} {

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

    # Activate hack for new libgcc
    # https://trac.macports.org/wiki/PortfileRecipes#deactivatehack
    pre-activate {
        if {![catch {set installed [lindex [registry_active libgcc8] 0]}]} {
            # Extract the epoch of the installed libgcc8
            set _epoch [lindex $installed 5]
            # If < 5 need to deactivate
            if {[vercmp $_epoch 5] < 0} {
                registry_deactivate_composite libgcc8 "" [list ports_nodepcheck 1]
            }
        }
        if {![catch {set installed [lindex [registry_active libgcc10] 0]}]} {
            # Extract the epoch of the installed libgcc9
            set _epoch [lindex $installed 5]
            # If < 3 need to deactivate
            if {[vercmp $_epoch 6] < 0} {
                registry_deactivate_composite libgcc10 "" [list ports_nodepcheck 1]
            }
        }
        if {![catch {set installed [lindex [registry_active libgcc-devel] 0]}]} {
            # Extract the epoch of the installed libgcc-devel
            set _epoch [lindex $installed 5]
            # If < 4 need to deactivate
            if {[vercmp $_epoch 5] < 0} {
                registry_deactivate_composite libgcc-devel "" [list ports_nodepcheck 1]
            }
        }
    }

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

        # Temporary working dir for dylibs
        file mkdir ${destroot}${prefix}/lib/libgcc.merged

        # loop over libs to install
        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}"
            }
        }

        # Remove stuff not provided by libgccX ports
        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 temporary lib dir back in place
        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 {

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

        # loop over libs to install
        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
}
if { ${os.platform} eq "darwin" && ${os.major} >= 20 } {
    platform i386 {
        configure.universal_archs x86_64 arm64
    }
    platform arm {
        configure.universal_archs x86_64 arm64
    }
} else {
    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.url       http://mirror.koddos.net/gcc/releases/
livecheck.regex     gcc-(${major}\\.\[0-9.\]+)/
