# -*- 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.3.0
revision            3
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           gcc-10.3.0.tar.gz \
                    rmd160  b6da30053864a10e8acdac62b9008b607db1f318 \
                    sha256  8fcf994811ad4e5c7ac908e8cf62af2c1982319e5551f62ae72016064dacdf16 \
                    size    135659248 \
                    mpfr-4.1.0.tar.bz2 \
                    rmd160  7a6d028b63c864566f62d47a58521e00a2890c28 \
                    sha256  feced2d430dd5a97805fa289fed3fc8ff2b094c02d05287fd6133e7f1f0ec926 \
                    size    1747243 \
                    gmp-6.2.1.tar.bz2 \
                    rmd160  2a4204453eb608bec6bb647ff5a0c47ca4d43878 \
                    sha256  eae9326beb4158c386e39a356818031bd28f3124cf915f8c5b1dc4c7a36b4d7c \
                    size    2493916 \
                    mpc-1.2.1.tar.gz \
                    rmd160  8c25b71d080936aab6ffa84f4f9d99b662d82c47 \
                    sha256  17503d2c395dfcf106b622dc142683c1199431d095367c6aacba6eec30340459 \
                    size    838731 \
                    isl-0.24.tar.bz2 \
                    rmd160  b3bf8e1ad50207d4eebecc47cb4cffdac6581a57 \
                    sha256  fcf78dd9656c10eb8cf9fbd5f59a0b6b01386205fe1934b3b287a0a1898145c0 \
                    size    2261594

extract {
    foreach f ${distfiles} {
        set f [lindex [split ${f} :] 0]
        ui_msg "Extracting: ${f}"
        if {[string match "*bz2*" ${f}]} {
            ui_msg "${workpath} \"/usr/bin/tar xjf ${distpath}/${f}\""
            system -W ${workpath} "/usr/bin/tar xjf ${distpath}/${f}"
        }
        if {[string match "*gz*" ${f}]}  {
            ui_msg "${workpath} \"/usr/bin/tar xzf ${distpath}/${f}\""
            system -W ${workpath} "/usr/bin/tar xzf ${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
}

patchfiles  patch-dynamic-lookup-11.diff \
			patch-ppcsl.diff \
			patch-SLPPC-darwin.h.diff \
			patch-iains-apple-si.diff \
			patch-iains-ppc.diff \
			patch-extra-ppc.diff \
			patch-darwin21.diff \
			patch-build-i686.diff
			

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

configure.cmd       ${worksrcpath}/configure
configure.dir       ${workpath}/build
configure.pre_args  --prefix=${prefix}/libexec/${name}
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}]"
}

# set universal_targets ""
# foreach arch ${configure.universal_archs} {
#     switch -- ${arch} {
#         arm64   {set universal_targets "${universal_targets},aarch64-apple-${os.platform}${os.version}"}
#         x86_64  {set universal_targets "${universal_targets},x86_64-apple-${os.platform}${os.version}"}
#         i386    {set universal_targets "${universal_targets},i686-apple-${os.platform}${os.version}"}
#         ppc     {set universal_targets "${universal_targets},powerpc-apple-${os.platform}${os.version}"}
#         ppc64   {set universal_targets "${universal_targets},powerpc64-apple-${os.platform}${os.version}"}
#     }
# }
# 
# variant universal {
#     configure.args-replace \
#                     --disable-multilib \
#                     --enable-multilib
# 
#     configure.args-replace \
#                     --disable-multiarch \
#                     --enable-multiarch
# 
#     if { [string length ${universal_targets}] > 0 } {
#         configure.args-append \
#             --enable-targets=[string replace ${universal_targets} 0 0]
#     }
# }
# 
# default_variants    +universal

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

merger_arch_flag            yes
merger_arch_compiler        yes
merger_must_run_binaries    yes

if {![info exists universal_possible]} {
    set universal_possible [expr {${os.universal_supported} && [llength ${configure.universal_archs}] >= 2}]
}
if {${universal_possible} && [variant_isset universal]} {
    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}

    set merger_configure_args(arm64)  --build=aarch64-apple-${os.platform}${os.major}
    set merger_configure_args(i386)   --build=i386-apple-${os.platform}${os.major}
    set merger_configure_args(ppc)    --build=powerpc-apple-${os.platform}${os.major}
    set merger_configure_args(ppc64)  --build=powerpc64-apple-${os.platform}${os.major}
    set merger_configure_args(x86_64) --build=x86_64-apple-${os.platform}${os.major}
} elseif {${build_arch} eq "arm64"} {
    configure.args-append \
                    --host=aarch64-apple-${os.platform}${os.major} \
                    --build=aarch64-apple-${os.platform}${os.major}
} elseif {${build_arch} eq "ppc"} {
    configure.args-append \
                    --host=powerpc-apple-${os.platform}${os.major} \
                    --build=powerpc-apple-${os.platform}${os.major}
} elseif {${build_arch} eq "ppc64"} {
    configure.args-append \
                    --host=powerpc64-apple-${os.platform}${os.major} \
                    --build=powerpc64-apple-${os.platform}${os.major}
} else {
    configure.args-append \
                    --host=${build_arch}-apple-${os.platform}${os.major} \
                    --build=${build_arch}-apple-${os.platform}${os.major}
}

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

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,\
see clang-11-bootstrap port as example.
"

livecheck.type      none
