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

# ideally for this bootstrap port we want no deps on any MacPorts port. This port
# should build cleanly from system roots and use and/or link to nothing in MacPorts.

name                gcc10-bootstrap

# Note, this is the last version of gcc which haven't required c++11 for bootstrap
version             10.4.0
revision            0
epoch               0

platforms           darwin
categories          lang
maintainers         {@catap korins.ky:kirill} openmaintainer

# an exception in the license allows dependents to not be GPL
license             {GPL-3+ Permissive}

description         Cross-platform gcc pegged at version ${version}. No dependencies.
long_description    ${description} This was the last version of gcc to not require c++11 for bootstrap.

homepage            https://gcc.gnu.org/

master_sites        http://gcc.gnu.org/pub/gcc/releases/gcc-${version}/:gcc \
                    gnu:gcc/gcc-${version}:gcc \
                    http://gcc.gnu.org/pub/gcc/infrastructure/:gccinfrastructure \
                    gnu:gcc/infrastructure:gccinfrastructure

distname            gcc-${version}
dist_subdir         gcc

set    MPFR         mpfr-4.1.0
set    GMP          gmp-6.2.1
set    MPC          mpc-1.2.1
set    ISL          isl-0.24

distfiles           ${distname}${extract.suffix}:gcc \
                    ${MPFR}.tar.bz2:gccinfrastructure \
                    ${GMP}.tar.bz2:gccinfrastructure \
                    ${MPC}.tar.gz:gccinfrastructure \
                    ${ISL}.tar.bz2:gccinfrastructure

checksums           ${distname}${extract.suffix} \
                    rmd160  becc4bb23891748bf68cbc6acac7e904be8ccf47 \
                    sha256  ab1974017834430de27fd803ade4389602a7d6ca1362496c57bef384b2a4cb07 \
                    size    131962530 \
                    ${MPFR}.tar.bz2 \
                    rmd160  7a6d028b63c864566f62d47a58521e00a2890c28 \
                    sha256  feced2d430dd5a97805fa289fed3fc8ff2b094c02d05287fd6133e7f1f0ec926 \
                    size    1747243 \
                    ${GMP}.tar.bz2 \
                    rmd160  2a4204453eb608bec6bb647ff5a0c47ca4d43878 \
                    sha256  eae9326beb4158c386e39a356818031bd28f3124cf915f8c5b1dc4c7a36b4d7c \
                    size    2493916 \
                    ${MPC}.tar.gz \
                    rmd160  8c25b71d080936aab6ffa84f4f9d99b662d82c47 \
                    sha256  17503d2c395dfcf106b622dc142683c1199431d095367c6aacba6eec30340459 \
                    size    838731 \
                    ${ISL}.tar.bz2 \
                    rmd160  b3bf8e1ad50207d4eebecc47cb4cffdac6581a57 \
                    sha256  fcf78dd9656c10eb8cf9fbd5f59a0b6b01386205fe1934b3b287a0a1898145c0 \
                    size    2261594

# GCC can't be build by clang from Xcode / CLT 14.0.x
# See: 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])} {
    known_fail          yes
    pre-fetch {
        ui_error "GCC build is known to fail on Xcode / CLT 14.0.x"
        return -code error "incompatible Xcode / CLT version"
    }
}

extract {
    foreach f ${distfiles} {
        set f [lindex [split ${f} :] 0]
        ui_debug "Extracting: ${f}"
        if {[string match "*bz2*" ${f}]} {
            system -W ${workpath} "/usr/bin/tar xjf [shellescape ${distpath}/${f}]"
        }
        if {[string match "*gz*" ${f}]}  {
            system -W ${workpath} "/usr/bin/tar xzf [shellescape ${distpath}/${f}]"
        }
    }
}

post-extract {
    ln -s ${workpath}/${MPFR} ${worksrcpath}/mpfr
    ln -s ${workpath}/${GMP}  ${worksrcpath}/gmp
    ln -s ${workpath}/${MPC}  ${worksrcpath}/mpc
    ln -s ${workpath}/${ISL}  ${worksrcpath}/isl
}

# This is patch applies to gmp, isl, mpc and mpfr
patchfiles              patch-dynamic-lookup-11.diff

# Branches from the Darwin maintainer of GCC
# https://github.com/iains/gcc-10-branch/tree/gcc-10-3-apple-si
# Thus, it includes https://github.com/iains/gcc-10-branch/tree/gcc-10-3-darwin
patchfiles-append       0001-Driver-Provide-a-spec-to-insert-rpaths-for-compiler-.patch \
                        0002-Darwin-Handle-rpaths-given-on-the-command-line.patch \
                        0003-Darwin-Allow-for-configuring-Darwin-to-use-embedded-.patch \
                        0004-Darwin-Ada-Add-loader-path-as-a-default-rpath.patch \
                        0005-configure-Darwin-Adjust-RPATH_ENVVAR-for-embedded-ru.patch \
                        0006-Darwin-Use-a-reserved-name-for-the-exception-tables-.patch \
                        0007-CFI-handling-Add-a-hook-to-allow-target-specific-Per.patch \
                        0008-configury-Fix-LEB128-support-for-non-GNU-assemblers.patch \
                        0009-C-Add-the-stdlib-option.patch \
                        0010-Darwin-libgcc-Adjust-min-version-supported-for-the-O.patch \
                        0011-aarch64-Remove-redundant-mult-patterns.patch \
                        0012-aarch64-Don-t-emit-invalid-zero-sign-extend-syntax.patch \
                        0013-fortran-caf_fail_image-expects-no-argument.patch \
                        0014-fortran-Fix-function-arg-types-for-class-objects.patch \
                        0015-fortran-Fix-arg-types-of-_gfortran_is_extension_of.patch \
                        0016-fortran-Fix-argument-types-in-derived-types-procedur.patch \
                        0017-config.sub-config.guess-Import-upstream-2020-11-07.patch \
                        0018-aarch64-Mark-rotate-immediates-with-as-per-DDI0487iF.patch \
                        0019-aarch64-Cast-print-value-per-format-description.patch \
                        0020-Darwin-Arm64-Truncate-char-immediates-in-vector-init.patch \
                        0021-Darwin-Arm64-Proof-of-principle-hack-Fortran-to-use-.patch \
                        0022-Darwin-Arm64-Base-build-changes-to-Darwin-common-cod.patch \
                        0023-Darwin-Arm64-Initial-definitions-for-the-port.patch \
                        0024-Darwin-Arm64-Disable-section-anchors-for-now.patch \
                        0025-Darwin-Arm64-Assign-a-register-for-the-static-chain.patch \
                        0026-Darwin-Arm64-Initial-libgcc-boiler-plate-changes.patch \
                        0027-Darwin-Arm64-Darwin-should-use-an-OS-call-to-clear-c.patch \
                        0028-Darwin-Arm64-Initial-support-for-the-self-host-drive.patch \
                        0029-Darwin-Arm64-Ada-fixes-for-hosted-tools.patch \
                        0030-Darwin-Arm64-Initial-changes-to-libitm.patch \
                        0031-Add-IEEE-code-for-Aarch64-in-libgfortran.patch \
                        0032-Darwin-Arm64-Additional-change-for-darwincpcs-D.4.patch \
                        0033-Darwin-Arm64-Implement-darwinpcs-D.2.patch \
                        0034-Darwin-Arm64-Implement-darwinpcs-mangling-for-va_lis.patch \
                        0035-Darwin-Arm64-Initial-stab-at-darwinpcs-variadic-func.patch \
                        0036-Generic-Mega-hack-to-allow-different-passing-organis.patch \
                        0037-Darwin-Arm64-Adjust-cases-where-stack-spills-pack-di.patch \
                        0038-Darwin-Arm64-Reimplement-the-base-CC-variant.patch \
                        0039-Darwin-Arm64-Account-for-stack-addresses-less-aligne.patch \
                        0040-Generic-More-mega-hack-for-va-normal.patch \
                        0041-Darwin-Arm64-Temporary-proof-of-principle-for-relocs.patch \
                        0042-Darwin-Arm64-Make-code-label-references-use-PAGE-rel.patch \
                        0043-Darwin-Arm64-Avoid-negative-offsets-in-PAGE-relocs.patch \
                        0044-Darwin-Arm64-Make-more-Objective-C-metadata-linker-v.patch \
                        0045-testsuite-Darwin-Arm64-Darwin-does-not-support-sve-a.patch \
                        0046-testsuite-Darwin-Arm64-We-don-t-support-PCH-yet-disa.patch \
                        0047-testsuite-Darwin-Arm64-Fixup-tests-where-the-output-.patch \
                        0048-testsuite-Darwin-Arm64-Fix-up-CFString-tests-for-arm.patch \
                        0049-testsuite-Darwin-Arm64-Amend-segaddr-test.patch \
                        0050-testsuite-Darwin-Arm64-Adjust-pubtypes-lengths-for-A.patch \
                        0051-testsuite-Darwin-Arm64-Adjust-for-revised-section-sp.patch \
                        0052-testsuite-Darwin-Arm64-Disable-the-AAPCS64-tests.patch \
                        0053-testsuite-Darwin-Arm64-Add-U_L_P-to-bf16-mangling-te.patch \
                        0054-testsuite-Darwin-Arm64-Amend-scan-asms-for-arm-synta.patch \
                        0055-testsuite-Darwin-Arm64-Skip-tests-with-an-unsupporte.patch \
                        0056-testsuite-Darwin-Arm64-Skip-tests-with-an-unsupporte.patch \
                        0057-testsuite-Objective-C-Darwin-Arm64-adjust-scan-asms-.patch \
                        0058-testsuite-Darwin-Arm64-Skip-two-unsupported-tests.patch \
                        0059-testsuite-Darwin-Arm64-Skip-more-tests-using-check-f.patch \
                        0060-Darwin-Arm64-Allow-m64-on-the-command-line.patch \
                        0061-aarch64-Darwin-Restrict-offsets-for-prfm.patch \
                        0062-aarch64-Darwin-Match-conditions-for-a-PRFUM-insn.patch

# https://github.com/iains/gcc-10-branch/tree/gcc-10-3-ppc
patchfiles-append       0063-Darwin-crts-Fix-a-build-warning.patch \
                        0064-Darwin-Amend-pie-options-when-linking-mdynamic-no-pi.patch

# Backport preliminary Darwin 21 support to GCC 10 from GCC11
patchfiles-append       0065-Force-to-use-C-11-on-macOS-11.patch \
                        0066-Darwin-Allow-to-using-rpaths-after-darwin20.patch \
                        0067-Darwin-libgcc-include-support-macOS-12.patch

# Backport preliminary Darwin 22 support to GCC 10
patchfiles-append       0068-libstdc-Rename-__null_terminated-to-avoid-collision-.patch

platform darwin 10 powerpc {
    patchfiles-append   patch-SLPPC.diff
}

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

# sterilize MacPorts build environment; we want nothing picked up from MP prefix
compiler.cpath
compiler.library_path

configure.cxx_stdlib

configure.cflags
configure.cxxflags
configure.cppflags
configure.optflags
configure.ldflags

configure.universal_cflags
configure.universal_cxxflags
configure.universal_cppflags
configure.universal_ldflags
configure.universal_args

configure.ccache    no
configure.distcc    no

# sterilize PATH
configure.env-append    PATH=/usr/bin:/bin:/usr/sbin:/sbin
build.env-append        PATH=/usr/bin:/bin:/usr/sbin:/sbin

platform darwin 8 {
    # Tiger comes with make 3.80 and we need 3.81
    set    MAKE_V       make-3.81

    master_sites-append gnu:make:gccmake
    distfiles-append    ${MAKE_V}.tar.gz:gccmake
    checksums-append    make-3.81.tar.gz \
                        rmd160  a713a72875cb9a29568677c98022465c6f55cbbf \
                        sha256  16b77de9f013bcd536b7bc1efbe314223aedfe250f9063e33cbb4dfd347215a2 \
                        size    1564560

    pre-configure {
        file mkdir ${workpath}/bins
        system -W ${workpath}/${MAKE_V} "CC=/usr/bin/cc ./configure && make"
        ln -s ${workpath}/${MAKE_V}/make   ${workpath}/bins/make
    }

    configure.env-append    MAKE=${workpath}/bins/make
    build.env-append        MAKE=${workpath}/bins/make

    configure.env-replace   PATH=/usr/bin:/bin:/usr/sbin:/sbin \
                            PATH=${workpath}/bins:/usr/bin:/bin:/usr/sbin:/sbin

    build.env-replace       PATH=/usr/bin:/bin:/usr/sbin:/sbin \
                            PATH=${workpath}/bins:/usr/bin:/bin:/usr/sbin:/sbin
}

prefix              ${prefix}/libexec/${name}

configure.cmd       ${worksrcpath}/configure
configure.dir       ${workpath}/build
configure.args      --enable-languages=c,c++,objc,obj-c++ \
                    --enable-bootstrap \
                    --with-system-zlib \
                    --without-zstd \
                    --disable-nls \
                    --disable-tls \
                    --disable-multilib \
                    --disable-multiarch \
                    --with-bugurl=https://trac.macports.org/newticket

if {${os.major} >= 18 && ${configure.sdkroot} ne ""} {
    configure.args-append --with-sysroot="[regsub {MacOSX1[0-9]\.[0-9]+\.sdk} ${configure.sdkroot} {MacOSX.sdk}]"
}

# Be default Catalina and Mojave support a single arch at MacPorts
if {${os.major} < 18 || ${os.major} > 19} {
    default_variants    +universal
}

platform darwin 8 {
    configure.args-append \
                    --with-dwarf2
}

merger_arch_flag            yes
merger_arch_compiler        yes
merger_must_run_binaries    yes

set merger_host(arm64)  aarch64-apple-${os.platform}${os.major}
set merger_host(i386)   i386-apple-${os.platform}${os.major}
set merger_host(ppc)    powerpc-apple-${os.platform}${os.major}
set merger_host(ppc64)  powerpc64-apple-${os.platform}${os.major}
set merger_host(x86_64) x86_64-apple-${os.platform}${os.major}

foreach {arch target} [array get merger_host] {
    lappend merger_configure_args(${arch}) --build=${target}
    lappend merger_configure_args(${arch}) --target=${target}

    lappend merger_configure_env(${arch}) BUILD_ARCH=${arch}
    lappend merger_build_env(${arch}) BUILD_ARCH=${arch}
}

if {${universal_possible} && [variant_isset universal]} {
    configure.args-replace \
                     --disable-multilib \
                     --enable-multilib

    configure.args-replace \
                     --disable-multiarch \
                     --enable-multiarch

    set universal_targets ""
    foreach arch ${universal_archs_supported} {
        set universal_targets "${universal_targets},$merger_host(${arch})"
    }

    ui_debug "Selected targets: ${universal_targets}"

    if { [string length ${universal_targets}] > 0 } {
        configure.args-append \
            --enable-targets=[string replace ${universal_targets} 0 0]
    }
} else {
    configure.args-append \
                    --host=$merger_host(${build_arch}) \
                    --build=$merger_host(${build_arch}) \
                    --target=$merger_host(${build_arch}) \
                    --enable-targets=$merger_host(${build_arch})
}

# macOS 10 can build only ppc, disable multilib and multiarch
# See: https://trac.macports.org/ticket/67384
platform darwin 10 powerpc {
    configure.args-replace \
                     --enable-multilib \
                     --disable-multilib

    configure.args-replace \
                     --enable-multiarch \
                     --disable-multiarch
}

# GMP enforces host and target as none-bla-bla that may leads to
# GMP_NUMB_BITS and sizeof(mp_limb_t) are not consistent.
#
# See: https://trac.macports.org/ticket/65498
pre-configure {
    file mkdir ${workpath}/bins

    set gcc [open "${workpath}/bins/gcc" w 0755]
    puts ${gcc} "#!/bin/sh"
    puts ${gcc} "${configure.cc} -arch $\{BUILD_ARCH:-${build_arch}\} \"\$@\""
    close ${gcc}

    set gxx [open "${workpath}/bins/g++" w 0755]
    puts ${gxx} "#!/bin/sh"
    puts ${gxx} "${configure.cxx} -arch $\{BUILD_ARCH:-${build_arch}\} \"\$@\""
    close ${gxx}

    configure.cc    ${workpath}/bins/gcc
    configure.cxx   ${workpath}/bins/g++
}

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

# They differ when universal due to gcc multilib being messy non-universal
destroot.delete_la_files yes

# multilib produces a fat binaries which is tried to be merged by muniversal PG
# lipo failed and it fallback to diff which is also failed => build fails
pre-destroot {
    if {${universal_possible} && [variant_isset universal]} {
        foreach arch ${universal_archs_supported} {
            foreach fl [glob -directory "${workpath}/build-${arch}/$merger_host(${arch})/libgcc" -nocomplain *.dylib] {
                if { ! [catch {system "/usr/bin/lipo \"${fl}\" -thin ${arch} -output \"${fl}.thin\""}] } {
                    # lipo worked, rename file
                    delete "${fl}"
                    file rename "${fl}.thin" "${fl}"
                } else {
                    # lipo failed
                    delete "${fl}.thin"
                }
            }
        }
    }
}

# Cleanup some additional architectures which is built by multilib/multiarch
# all of them brokes rev-upgrade and started a loop to rebuild the port
post-destroot {
    foreach arch {x86_64 ppc64} {
        if {!(${arch} in ${universal_archs_supported})} {
            foreach fl [exec find ${destroot} -name ${arch}] {
                delete ${fl}
            }
        }
    }
}

notes "
To use this bootstrap version of gcc instead of the default compiler, add the\
following lines to the Portfile:

depends_lib-append      port:${name}

configure.cc            \$\{prefix\}/libexec/${name}/bin/gcc
configure.cxx           \$\{prefix\}/libexec/${name}/bin/g++

If you would like to build universal port with this compiler,\
you must use per target compiler. The easy way is using muniversal PG:

PortGroup               muniversal 1.0

if \{\$\{universal_possible\} && \[variant_isset universal\]\} {
    configure.cc        {}
    configure.cxx       {}

    foreach arch \$\{universal_archs_supported\} {
        lappend merger_configure_env(\$\{arch\}) \
                        \"CC=arch -arch \$\{arch\} \$\{prefix\}/libexec/${name}/bin/gcc\"
        lappend merger_configure_env(\$\{arch\}) \
                        \"CXX=arch -arch \$\{arch\} \$\{prefix\}/libexec/${name}/bin/g++\"
    }
} else {
    configure.cc        \$\{prefix\}/libexec/${name}/bin/gcc
    configure.cxx       \$\{prefix\}/libexec/${name}/bin/g++
}
"

livecheck.type      none
