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

name                gcc46
# Whenever this port is bumped for version/revision, please revbump dragonegg-3.4-gcc-4.6
epoch               1
version             4.6.4
revision            15
platforms           {darwin < 17}
categories          lang
maintainers         nomaintainer
# an exception in the license allows dependents to not be GPL
license             {GPL-3+ Permissive}
description         The GNU compiler collection
long_description    The GNU compiler collection, including front ends for \
                    C, C++, Objective-C, Objective-C++, and Fortran.

homepage            https://gcc.gnu.org/
master_sites        ftp://ftp.funet.fi/pub/mirrors/sources.redhat.com/pub/gcc/releases/gcc-${version}/:gcc \
                    ftp://ftp.gwdg.de/pub/linux/gcc/releases/gcc-${version}/:gcc \
                    ftp://gcc.ftp.nluug.nl/mirror/languages/gcc/releases/gcc-${version}/:gcc \
                    ftp://gcc.gnu.org/pub/gcc/releases/gcc-${version}/:gcc \
                    gnu:gcc/gcc-${version}:gcc \
                    ftp://sourceware.org/pub/java/:ecj \
                    http://mirrors.kernel.org/sources.redhat.com/java/:ecj \
                    https://www.mirrorservice.org/sites/sources.redhat.com/pub/java/:ecj \
                    http://ftp-stud.fht-esslingen.de/pub/Mirrors/sourceware.org/java/:ecj

use_bzip2           yes

set ecj             ecj-4.5.jar

distname            gcc-${version}

distfiles           ${distname}${extract.suffix}:gcc ${ecj}:ecj

extract.only        ${distname}${extract.suffix}

checksums           ${distname}${extract.suffix} \
                    md5     b407a3d1480c11667f293bfb1f17d1a4 \
                    rmd160  6a58b053812cf75622bc4579e020cd9471f46916 \
                    sha256  35af16afa0b67af9b8eb15cafb76d2bc5f568540552522f5dc2c88dd45d977e8 \
                    size    72006076 \
                    ${ecj} \
                    rmd160  d3f4da657f086b6423f74e93f001132f4855368a \
                    sha256  98fd128f1d374d9e42fd9d4836bdd249c6d511ebc6c0df17fbc1b9df96c3d781 \
                    size    1470676

if { ${configure.build_arch} eq "i386" } {

    # build with traditional "gas" assembler in all cases
    # upstream does not typically test gcc with clang as assembler on i386 systems
    # gcc configuration changes with assembler features
    # also innumerable warnings about text section naming suppressed
    patchfiles-append  patch-gcc5-disable-macports-cctools-as-changes.diff

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

depends_lib         port:cctools \
                    path:lib/pkgconfig/cloog-isl.pc:cloog \
                    port:gmp \
                    port:ld64 \
                    port:zlib \
                    port:libiconv \
                    port:libmpc \
                    port:mpfr \
                    port:ppl
depends_run         port:gcc_select \
                    port:libgcc6

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr ppl libmpc

patchfiles-append          gcc-4.6-cloog_lang_c.patch \
                    enable_libstdcxx_time_yes.patch \
                    patch-gcc-texi.diff \
                    cloog-isl-overlinking.patch

# Don't check Darwin kernel version (GCC PR target/61407
# <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61407>).
patchfiles-append   remove-kernel-version-check.patch

# 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

# Don't link with "-flat_namespace -undefined suppress" on Yosemite and
# later (#45483).
patchfiles-append   yosemite-libtool.patch

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

post-extract {
    copy ${distpath}/${ecj} ${worksrcpath}/ecj.jar
}

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

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
# 'java' and 'go' lead to compilation errors on 10.5/Xcode 3.x
configure.args      --enable-languages=c,c++,objc,obj-c++,lto,fortran \
                    --libdir=${prefix}/lib/${name} \
                    --includedir=${prefix}/include/${name} \
                    --infodir=${prefix}/share/info \
                    --mandir=${prefix}/share/man \
                    --datarootdir=${prefix}/share/gcc-${major} \
                    --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} \
                    --with-mpc=${prefix} \
                    --with-ppl=${prefix} \
                    --with-cloog=${prefix} --enable-cloog-backend=isl \
                    --disable-cloog-version-check \
                    --enable-stage1-checking \
                    --disable-multilib \
                    --enable-lto \
                    --enable-libstdcxx-time \
                    --with-build-config=bootstrap-debug \
                    --with-as=${prefix}/bin/as \
                    --with-ld=${prefix}/bin/ld \
                    --with-ar=${prefix}/bin/ar \
                    --with-bugurl=https://trac.macports.org/newticket
# remove this asap when upstream implements a check for ppl 1.0
configure.args-append --disable-ppl-version-check

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

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

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 libquadmath.0.dylib libstdc++.6.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_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}
                }
            }
        }
    }
}

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

platform darwin 8 {
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45248
    configure.args-append --with-dwarf2
}

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
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.\]+)/
