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

set                 gcc_major_v 14
epoch               0
name                gcc${gcc_major_v}

homepage            https://gcc.gnu.org

categories          lang
maintainers         {@barracuda156 macos-powerpc.org:barracuda}
# an exception in the license allows dependents to not be GPL
license             {GPL-3+ Permissive}

description         The GNU compiler collection
long_description    {*}${description}, including front ends for \
                    C, C++, Objective-C, Objective-C++ and Fortran.

# Remember to reset all revision increments below to 0 on new versions
version             ${gcc_major_v}.3.0
revision            0

set libgccname      lib${name}
subport             ${libgccname} { revision [ expr ${revision} + 1 ] }

set libcxxname      ${name}-libcxx

checksums           rmd160  97d7402ec552c057cee7fb487db2123506ef7fde \
                    sha256  e0dc77297625631ac8e50fa92fffefe899a4eb702592da5c32ef04e2293aca3a \
                    size    95212220

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

distname            gcc-${version}
use_xz              yes

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 \
                    port:zstd

# Notice, that gcc needs a functional flex. Normally it is present in system prefix.
# On 10.6.8 on powerpc, however, make sure that /usr/bin/flex has a ppc slice.
# Otherwise the build fails, and activating flex port to resume it will not help.
# If you face a flex-related error, make sure to install the right flex,
# then run port clean and build again.

# gcc10 does not build on recent macOS atm: https://github.com/iains/gcc-10-branch/issues/12
# So default to it on < 10.7 for now.
if {${os.platform} eq "darwin" && ${os.major} < 11} {
    configure.compiler.add_deps \
                    no
    depends_build-append \
                    port:gcc10-bootstrap
    configure.cc    ${prefix}/libexec/gcc10-bootstrap/bin/gcc
    configure.cxx   ${prefix}/libexec/gcc10-bootstrap/bin/g++
}

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

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

proc gcc_arch {arch} {
    switch ${arch} {
        arm64       {return aarch64}
        ppc64       {return powerpc64}
        ppc         {return powerpc}
        default     {return ${arch}}
    }
}

platform darwin {
    set gcc_triple [gcc_arch ${build_arch}]-apple-darwin${os.major}
    configure.pre_args-append --build=${gcc_triple}
}

set gcc_configure_langs {c c++ objc obj-c++ lto fortran m2}
if {${subport} eq ${name} && ${build_arch} ne "i386" && ${os.major} > 8} {
    # jit compiler is not building on i386 systems
    # https://trac.macports.org/ticket/61130

    # On ppc Tiger, the build fails when linking libgccjit.0.dylib:
    # Undefined symbols:
    #   "__Unwind_GetIPInfo", referenced from:
    #       _unwind in libbacktrace.a(backtrace.o)
    lappend gcc_configure_langs jit
}

proc get_clean_sysroot {} {
    global configure.sdkroot configure.sdk_version
    set new_sdk ${configure.sdkroot}
    if { [regexp {MacOSX(.*).sdk} ${configure.sdkroot} -> sdk_v] } {
        if { ${sdk_v} != "" } {
            # If present strip minor version from SDK version
            set sdk_major [lindex [split $sdk_v .] 0]
            ui_debug "Detected versioned SDK ${sdk_v} ${sdk_major} ${configure.sdkroot}"
            set new_sdk [regsub {MacOSX1[1-9]\.[0-9]+\.sdk} ${configure.sdkroot} MacOSX${sdk_major}.sdk]
            if { ${new_sdk} != ${configure.sdkroot} } {
                ui_debug " New SDK ${new_sdk}"
            }
        }
    }
    return ${new_sdk}
}

set mp_extra_rpath  ${prefix}/lib/libgcc

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure

configure.args      --datarootdir=${prefix}/share/gcc-${major} \
                    --disable-multilib \
                    --disable-nls \
                    --enable-checking=release \
                    --enable-host-shared \
                    --enable-languages=[join ${gcc_configure_langs} ","] \
                    --enable-libstdcxx-time \
                    --enable-lto \
                    --infodir=${prefix}/share/info \
                    --libdir=${prefix}/lib/${name} \
                    --mandir=${prefix}/share/man \
                    --program-suffix=-mp-${major} \
                    --with-ar=${prefix}/bin/ar \
                    --with-as=${prefix}/bin/as \
                    --with-bugurl=https://github.com/macos-powerpc/powerpc-ports/issues/new \
                    --with-build-config=bootstrap-debug \
                    --with-darwin-extra-rpath=${mp_extra_rpath} \
                    --with-gmp=${prefix} \
                    --with-gxx-include-dir=${prefix}/include/${name}/c++/ \
                    --with-isl=${prefix} \
                    --with-ld=${prefix}/bin/ld \
                    --with-libiconv-prefix=${prefix} \
                    --with-local-prefix=${prefix} \
                    --with-mpc=${prefix} \
                    --with-mpfr=${prefix} \
                    --with-system-zlib \
                    --with-zstd=${prefix}

if {${subport} ne ${libcxxname}} {
    variant G4 conflicts G5 description "Optimize for G4" {
        configure.args-append \
                    --with-tune-cpu=G4
    }

    variant G5 conflicts G4 description "Optimize for G5" {
        configure.args-append \
                    --with-tune-cpu=G5
    }
}

# The only possible CPU here is 970:
if {${os.platform} eq "darwin" && ${configure.build_arch} eq "ppc64"} {
    default_variants-append \
                    +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 {${subport} eq ${name} && ${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
    # Update for gcc13+ - Similar issues on macOS 10.6, 10.13 ...
    if {([vercmp ${xcodeversion} >= 12.5] && [vercmp ${xcodeversion} < 13]) || \
         ([vercmp ${xcodecltversion} >= 12.5] && [vercmp ${xcodecltversion} < 13]) || \
             ${os.major} < 11 || ${os.major} == 17} \
    {
        # Skip bootstrap comparison entirely
        configure.args-replace --with-build-config=bootstrap-debug --without-build-config
        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
        }
    }
}

if {${os.platform} eq "darwin"} {
    # Patch generated from https://github.com/iains/gcc-14-branch
    # git diff --no-prefix releases/gcc-14.2.0 gcc-14.2-darwin-r1
    patchfiles-append      patch-darwin-gcc-${version}.diff

    # GCC fixinc work-around for sys/ucred.h on OSX10.14.4 no longer seems to work?
    if {${os.major} == 18} {
        patchfiles-append  patch-OSX10.14-ucred-atomic-define.diff
    }

    # This patch has been merged upstream in https://github.com/gcc-mirror/gcc/commit/1cfe4a4d0d4447b364815d5e5c889deb2e533669
    # Remove it when upgrading gcc to a version that has it.
    patchfiles-append      darwin-ppc-fpu.patch

    if {${os.major} == 8} {
        depends_build-append \
                           port:flex

        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117834
        patchfiles-append darwin8-define-PTHREAD_RWLOCK_INITIALIZER.patch

        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117857
        patchfiles-append darwin8-ttyname_r.patch
    }
}

# 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-gcc10-disable-macports-cctools-as-changes.diff

platform darwin {
    patchfiles-append \
                    0001-gcc-config-restore-ppc_intrinsics.h.patch
}

# This was only needed for 10a190. It has not been used with the latest
# builds on 10.6.8 powerpc already. Comment it out first, then drop.
# platform darwin 10 powerpc {
#     patchfiles-append \
#                     0002-darwin.h-adjust-for-10.6-ppc.patch
# }

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

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

if {${os.platform} eq "darwin" && ${os.major} >= 22} {
    if { ([vercmp ${xcodeversion}    >= 15.0] && [vercmp ${xcodeversion}    < 16.0]) ||
         ([vercmp ${xcodecltversion} >= 15.0] && [vercmp ${xcodecltversion} < 16.0]) } {
        # On macOS 13 and newer ensure the 'legacy' linker is used as GCC currently has problems
        # with the new default linker in Xcode 15. See e.g.
        # https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes#Linking
        # https://discussions.apple.com/thread/255137447
        # https://developer.apple.com/forums/thread/737707
        # https://github.com/Homebrew/homebrew-core/issues/145991
        configure.args-replace --with-ld=${prefix}/bin/ld --with-ld=${prefix}/bin/ld-classic
        # Ensure ld64 is installed with the correct variant need for ld-classic
        require_active_variants ld64 ld64_xcode
    }
}

pre-configure {
    # Set package info
    configure.args-append --with-pkgversion="MacOS PowerPC ${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="[get_clean_sysroot]"
    }
}

set libcxx_incpath ${prefix}/libexec/${name}/libc++/include/c++/v1

subport ${libcxxname} {
    # Modern libcxx is unavailable on PPC
    if {${build_arch} ni [list ppc ppc64]} {

    PortGroup compilers     1.0

    compilers.choose        cxx
    compilers.setup         -gcc -fortran -clangdevel -clang34 -clang35 -clang36 \
                            -clang37 -clang50 -clang60 -clang70 -clang80 -clang90
    if { ![clang_variant_isset] } {
        if { ${os.platform} eq "darwin" && ${os.major} < 11 } {
            default_variants-append +clang11
        } else {
            if { ${os.major} < 24 } {
                default_variants-append +clang17
            } else {
                default_variants-append +clang18
            }
        }
    }

    # Find clang/llvm version to use from active variant
    proc getClangVersion {} {
        if { [regexp {clang(.*)} [clang_variant_name] -> clang_v] } {
            return ${clang_v}
        }
        return ""
    }
    set mp_clang_ver [getClangVersion]

    } else {
        # PowerPC version. No -append, we override deps here.
        depends_build       port:libcxx-powerpc

        # Version used in libcxx-powerpc port:
        set mp_clang_ver    7.1.1
    }

    revision                [ expr ${revision} + 1 ]
    description             libc++ header implementation to be used by ${name}
    long_description        {*}${description}
    homepage                https://llvm.org
    license                 NCSA
    depends_extract
    depends_run
    depends_lib
    distfiles
    patchfiles
    extract.rename          no
    use_configure           no
    supported_archs         noarch
    platforms               any

    build {
        # Copy headers from clang-N during build
        file mkdir ${worksrcpath}/headers
        file copy ${prefix}/libexec/llvm-${mp_clang_ver}/include/c++/v1 ${worksrcpath}/headers/
    }
    destroot {
        set base_dir [file dirname ${libcxx_incpath}]
        file mkdir ${destroot}${base_dir}
        file copy ${worksrcpath}/headers/v1 ${destroot}${base_dir}
    }
}

if {${subport} ne ${libcxxname}} {
    variant stdlib_flag description "Enable stdlib command line flag to select c++ runtime" {
        # Enables support for specifying the c++ runtime via `-stdlib=` in a similar
        # way to clang. For more details see the later comments in
        #   https://www.mail-archive.com/gcc-patches@gcc.gnu.org/msg257385.html
        # Note : This 'bakes' the libc++ include directory into gcc,
        # which is then used as the default search location when `-stdlib=libc++`
        # is given. Therefore to have consistency across various OS versions, and to enable
        # modern c++ standards, use a recent macports clang port to provide this.
        if {${subport} eq ${name}} {
            depends_run-append port:${libcxxname}
        }
        configure.args-append --with-gxx-libcxx-include-dir="${libcxx_incpath}"
    }

    # Just to have a better compatibility with mainstream MacPorts on 10.5:
    if {${os.platform} eq "darwin" && ${os.major} > 9} {
        default_variants-append +stdlib_flag
    }
}

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

platform darwin {
    # gcc can't be built by Xcode Clang 14.0.x
    # https://trac.macports.org/ticket/67416
    # https://github.com/iains/gcc-12-branch/issues/6
    if { ([vercmp ${xcodeversion} >= 14.0] && [vercmp ${xcodeversion} < 14.1]) || \
        ([vercmp ${xcodecltversion} >= 14.0] && [vercmp ${xcodecltversion} < 14.1]) } {
        pre-fetch {
            ui_error "${name} cannot be built with Xcode/CLT 14.0.x"
            ui_error "Either upgrade both Xcode and the Command Line Tools to 14.1 or later, or downgrade to 13.4."
            return -code error "incompatible Xcode/CLT version"
        }
    }
}

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

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

destroot.target     install install-info-host

# gcc cannot build if these are active
conflicts_build-append libunwind-headers binutils gdb

# Find dylibs at a given location
proc dylib_list {location} {
    # Note *.*.dylib is to only match versioned dylibs, not the versionless sym links
    set dylibs [glob -directory ${location} -tails *.*.dylib]
    ui_debug "Found at ${location} dylibs : ${dylibs}"
    return ${dylibs}
}

if {${subport} eq ${libgccname}} {
    # No need to build as nothing that isn't provided by libgcc16
    platforms       any

    depends_run     path:share/doc/libgcc/README:libgcc
    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"
    }
}

if {${subport} eq ${name}} {
    depends_run-append  port:gcc_select
    depends_lib-append  port:${libgccname}

    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
        set dylibs [dylib_list ${destroot}${prefix}/lib/${name}]
        foreach dylib ${dylibs} {
            if { [file exists ${prefix}/lib/libgcc/${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}
                        }
                    }
                }
            }
        }

        # Ensure all headers are in versioned dir
        # Required after we stopped using the option the --includedir option
        # See https://github.com/iains/gcc-13-branch/issues/20 for more details.
        foreach h [glob -nocomplain ${destroot}${prefix}/include/*.h] {
            set f [file tail ${h}]
            set newh ${destroot}${prefix}/include/${name}/${f}
            ui_debug "Moving ${h} to ${newh}"
            file rename ${h} ${newh}
        }

    }

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

if {${subport} ne ${libcxxname}} {
    post-destroot {
        if {${os.platform} eq "darwin" && ${os.major} > 8} {
            # Ensure all dylibs in destroot have our extra rpath added ..
            # https://trac.macports.org/ticket/65503
            foreach dlib [ exec find ${destroot}${prefix} -type f -and -name "*.dylib" ] {
                ui_debug "Ensuring DYLIB '${dlib}' has RPATH '${mp_extra_rpath}'"
                # Note install_name_tool returns a failure if the dylib already has the entry
                # We don't want that here so force final status to be true...
                system "install_name_tool -add_rpath ${mp_extra_rpath} ${dlib} > /dev/null 2>&1 ; true"
            }
        }
    }

    if {${os.platform} eq "darwin" && ${os.major} <= 18} {
        switch -- $build_arch {
            i386 -
            x86_64 {
                configure.universal_archs i386 x86_64
            }
            ppc -
            ppc64 {
                configure.universal_archs ppc ppc64
            }
        }
        variant universal {
            configure.args-delete --disable-multilib
        }
    } else {
        universal_variant   no
    }
    # the generated compiler doesn't accept -arch
    configure.env-append \
        "CPP=${configure.cc} -E" \
        "CPP_FOR_BUILD=${configure.cc} -E" \
        "CXXCPP=${configure.cxx} -E"
    build.env-append \
        "CPP=${configure.cc} -E" \
        "CPP_FOR_BUILD=${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       http://mirror.koddos.net/gcc/releases/
livecheck.regex     gcc-(${major}\\.\[0-9.\]+)/
