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

name                gcc43
version             4.3.6
revision            15
platforms           darwin
categories          lang
maintainers         nomaintainer
# an exception in the license allows dependents to not be GPL
license             {GPL-3+ GPL-2+ Permissive}
description         The GNU compiler collection
long_description    The GNU compiler collection, including front ends for \
                    C, C++, Objective-C, Objective-C++, Fortran, and Java.

homepage            https://gcc.gnu.org/
master_sites        ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/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_bzip2           yes

set dcore           gcc-core-${version}.tar.bz2
set dfort           gcc-fortran-${version}.tar.bz2
set dcxx            gcc-g++-${version}.tar.bz2
set djava           gcc-java-${version}.tar.bz2
set dobjc           gcc-objc-${version}.tar.bz2

distfiles           ${dcore} ${dfort} ${dcxx} ${djava} ${dobjc}

checksums           gcc-core-${version}.tar.bz2 \
                    rmd160  d4bed0820d5eae5072e30fa66167fda4fc2f0bbc \
                    sha256  86af1703be5cc7f40270a66ac15ebcf6088706a789ae1bf18488dc070777f979 \
                    size    24448503 \
                    gcc-fortran-${version}.tar.bz2 \
                    rmd160  ca92b43af16f6837d99f839a76547cd0610fbc58 \
                    sha256  21dee6ef25fac3df42704465865266e895955db23b8000ade4196e388adb7889 \
                    size    1243731 \
                    gcc-g++-${version}.tar.bz2 \
                    rmd160  90e5836b9b5e16a378b93f093dec78e917ae3f9c \
                    sha256  0e765849e5ea09a662889b323b69955aac06d9a5bc1b1246ffa7ac90d2f8bd80 \
                    size    5717018 \
                    gcc-java-${version}.tar.bz2 \
                    rmd160  bfc0ac76397e81ccfeb9334966359e668a961d8e \
                    sha256  da1d5fa9431457d39841991465f1825fc49f09a7999ca31309bbd5972c95860f \
                    size    18116201 \
                    gcc-objc-${version}.tar.bz2 \
                    rmd160  cde38f66b4b952f52938dc50d7606a0e1ccc7d2f \
                    sha256  0c3c552320d5e052eec8471bd3bf89b3fd10f2ed129ae89861828b24d10c21ce \
                    size    227304

depends_lib         port:cctools \
                    port:gmp \
                    port:ld64 \
                    port:zlib \
                    port:libiconv \
                    port:mpfr
depends_run         port:gcc_select \
                    port:libgcc45

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr

patchfiles          Make-lang.in.diff \
                    darwin10.diff \
                    i386_multilib.diff \
                    ppc_fde_encoding.diff \
                    texinfo5.patch \
                    patch-gcc-texi.diff

# Handle OS X deployment targets correctly (GCC PR target/63810
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63810>).
patchfiles-append   macosx-version-min.patch

# GCC 4.3 is so old that it's not affected by the Libtool
# MACOSX_DEPLOYMENT_TARGET bug (#45483) and is linked correctly on
# Yosemite without patching.

set major           [join [lrange [split ${version} .-] 0 1] .]

worksrcdir          gcc-${version}

platform darwin {
    configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
}

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
configure.args      --enable-languages=c,c++,objc,obj-c++,java,fortran \
                    --libdir=${prefix}/lib/${name} \
                    --includedir=${prefix}/include/${name} \
                    --infodir=${prefix}/share/info \
                    --mandir=${prefix}/share/man \
                    --datadir=${prefix}/share/${name} \
                    --with-local-prefix=${prefix} \
                    --with-libiconv-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} \
                    --enable-stage1-checking \
                    --disable-multilib \
                    --with-as=${prefix}/bin/as \
                    --with-ld=${prefix}/bin/ld \
                    --with-ar=${prefix}/bin/ar \
                    --with-bugurl=https://trac.macports.org/newticket

# 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

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" && ([vercmp $xcodeversion 9.0] > 0)} {
    depends_lib
    depends_run
    archive_sites
    pre-fetch {
        ui_error "building ${name} is not supported with Xcode 9 or greater"
        return -code error {unsupported platform}
    }
}

pre-configure {
    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
        #
        # Note that macports toolchains will fail to compile gcc unless
        # the DevSDK is installed due to another gcc build system bug:
        # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80203
        configure.args-append --with-sysroot="${configure.sdkroot}"
    }

    # TODO: Remove when base automatically creates configure.dir (2.2.1?).
    file mkdir ${configure.dir}
}

# http://trac.macports.org/ticket/29067
compiler.blacklist-append gcc-4.0

# http://trac.macports.org/ticket/29104
compiler.blacklist-append {llvm-gcc-4.2 < 2336.1}

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

# STAGE1_CFLAGS="-O2 -fkeep-inline-functions" : from configure.ac
# BOOT_CFLAGS="-O2 -g" : from Makefile.tpl
# -std=gnu89 : for building with clang
# -D_FORTIFY_SOURCE=0 : The built compiler might not understand newer builtins
#                       exposed in libc headers for fortified string functions.
#                       Yes, we need to set both BOOT_CFLAGS and STAGE1_CFLAGS
#                       because STAGE1_CFLAGS is used with xgcc for libgcc
build.args-append BOOT_CFLAGS="-O2 -g -D_FORTIFY_SOURCE=0" STAGE1_CFLAGS="-O2 -fkeep-inline-functions -D_FORTIFY_SOURCE=0 -std=gnu89"
build.dir           ${configure.dir}
build.target        bootstrap-lean
use_parallel_build  yes

destroot.target     install install-info-host

post-destroot {
    # install/copy ffitarget.h only if we have it
    if {![catch {set ffitarget.h [glob ${destroot}${prefix}/lib/${name}/gcc/*/${version}/include/ffitarget.h]} result]} {
        file copy ${ffitarget.h} ${destroot}${prefix}/include/${name}/
    }

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

    foreach dylib {libgcc_ext.10.4.dylib libgcc_ext.10.5.dylib libgcc_s.1.dylib libgfortran.3.dylib libstdc++.6.dylib libobjc-gnu.2.dylib libgomp.1.dylib libssp.0.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_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}

# Java support does not compile on 10.4/x86
platform darwin 8 i386 {
    configure.args-replace \
        --enable-languages=c,c++,objc,obj-c++,java,fortran \
        --enable-languages=c,c++,objc,obj-c++,fortran
}

platform powerpc {
    configure.universal_archs ppc ppc64
}
platform i386 {
    configure.universal_archs i386 x86_64
}
variant universal {
    configure.args-delete --disable-multilib
}
# the generated compiler doesn't accept -arch
if {[vercmp [macports_version] 2.5.99] >= 0} {
configure.env-append \
    "CPP=${configure.cc} -E" \
    "CXXCPP=${configure.cxx} -E"
build.env-append \
    "CPP=${configure.cc} -E" \
    "CXXCPP=${configure.cxx} -E"
} else {
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_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.regex     gcc-(${major}\\.\[0-9.\]+)/