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

# 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.5.0
revision            1
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  f2fe20696c8197e0d34557a4b72e66f02c006a4a \
                    sha256  eed4dd5fc3cd9f52cb3a51a4fde1728cb19ec76292f559518e83652e7437befe \
                    size    136051785 \
                    ${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])} {
    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"
    }
}

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-Amend-pie-options-when-linking-mdynamic-no-pi.patch \
                        0064-LRA-rs6000-Darwin-Amend-lo_sum-use-for-forced-consta.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

# Additional bugfixes to support legacy macOS
patchfiles-append       0067-gcc-fix-build-by-ISO-C-98-compiler.patch

# 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.cc_archflags
configure.cxx_archflags
configure.objc_archflags
configure.ld_archflags

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

configure.ccache    no
configure.distcc    no

pre-configure {
    file mkdir ${workpath}/bins
    ln -s /usr/bin/ld ${workpath}/bins/ld
    ln -s /usr/bin/as ${workpath}/bins/as
    ln -s /usr/bin/cpp ${workpath}/bins/cpp
}

# add our bins directory to the front of the PATH
configure.env-append    PATH=${workpath}/bins:/usr/bin:/bin:/usr/sbin:/sbin
build.env-append        PATH=${workpath}/bins:/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 {
        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
}

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 \
                    --with-bugurl=https://trac.macports.org/newticket

# due to the new linker (which was introduced in Xcode 15: https://developer.apple.com/documentation/xcode-release-notes/xcode-15-release-notes),
# this port requires 'ld-classic' to build successfully with the toolchains from Xcode 15 or Command Line Tools 15.
#
# TODO: This is a temporary solution, the classic linker will be removed in a future release by Apple.
if { ( [vercmp ${xcodeversion} 15 ] >= 0 ) || ( [vercmp ${xcodecltversion} 15 ] >= 0 ) } {
    configure.args-append \
                    --with-ld=/Library/Developer/CommandLineTools/usr/bin/ld-classic
}

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

# By default Catalina and Mojave support a single arch on MacPorts
# universal building is also not working properly on Tiger (or Leopard)

if {(${os.major} < 18 || ${os.major} > 19) && ${os.major} > 8} {
    universal_variant yes
    default_variants  +universal
} else {
    universal_variant no
    configure.args-append --disable-multilib
}

platform powerpc {
    configure.universal_archs ppc ppc64
}
platform i386 {
    configure.universal_archs i386 x86_64
}
platform arm {
    configure.universal_archs x86_64 arm64
}

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

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

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}/bin/gcc
configure.cxx           ${prefix}/bin/gcc++
"

livecheck.type      none
