# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

PortSystem          1.0
PortGroup select 1.0

name                upc
version             5.2.0.1
revision            2
categories          lang parallel science
maintainers         {takeshi @tenomoto} openmaintainer
description         Unified Parallel C
license             GPL-3
long_description    \
    The GCC UPC toolset provides a compilation and execution environment \
    for programs written in the UPC (Unified Parallel C) language.
homepage            http://www.gccupc.org/
platforms           darwin
master_sites        http://www.gccupc.org/downloads/upc/rls/${name}-${version}/
distname            ${name}-${version}.src
use_bzip2           yes
checksums           rmd160  d35ec1e77517f7e5edef7ae8b6880bfa1a206eb8 \
                    sha256  45805995497cf7f52902d234f02bcd773e421a77b8b515026825aac46f64c6ad

depends_lib         port:gmp port:mpfr port:libiconv port:libmpc \
                    port:isl14 \
                    port:ld64 \
                    path:share/doc/libgcc/README:libgcc
depends_build       port:gdb \
                    port:cctools

depends_skip_archcheck-append gcc_select ld64 cctools
license_noconflict  gmp mpfr ppl libmpc
worksrcdir          ${name}-${version}

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

if {[info exists build_arch] && ${os.platform} eq "darwin"} {
    configure.cc_archflags
    configure.cxx_archflags
    configure.objc_archflags
    configure.pre_args-append --build=${build_arch}-apple-darwin${os.major}
}

pre-configure {
    if {${os.platform} eq "darwin" && ${os.arch} eq "powerpc"} {
        ui_error "PowerPC is not supported"
    }
    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}"
    }
}

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

# https://trac.macports.org/ticket/47996
compiler.blacklist-append {clang < 300}

# https://trac.macports.org/ticket/59253
compiler.blacklist-append macports-clang-9.0

# fix for use of isl14
patchfiles-append   patch-libisl.diff \
                    patch-gcc-system.h.diff

post-extract {
    xinstall -d -m 0755 ${build.dir}/gcc
    ln -s ${prefix}/libexec/isl14/include/isl ${build.dir}/gcc
}

if {[vercmp ${xcodeversion} 10.2] >= 0} {
    # https://trac.macports.org/ticket/58260
    # Patch for Xcode bug, taken from
    # https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89864#c43
    # https://github.com/Homebrew/homebrew-core/pull/39134/files
    patchfiles-append   xcode-bug-_Atomic-fix.patch
}

configure.dir       ${workpath}/build
configure.cmd       ${worksrcpath}/configure
configure.args      --bindir=${prefix}/lib/${name}/bin \
                    --libdir=${prefix}/lib/${name} \
                    --libexecdir=${prefix}/libexec/${name} \
                    --datarootdir=${prefix}/share/${name} \
                    --with-local-prefix=${prefix} \
                    --with-system-zlib \
                    --disable-nls \
                    --without-gnu-ld \
                    --with-gmp=${prefix} \
                    --with-mpfr=${prefix} \
                    --with-mpc=${prefix} \
                    --with-isl=${prefix}/libexec/isl14 --disable-isl-version-check \
                    --disable-bootstrap \
                    --disable-multilib \
                    --enable-lto \
                    --with-upc-backtrace-gdb=ggdb \
                    --enable-languages=c \
                    --with-as=${prefix}/bin/as \
                    --with-ls=${prefix}/bin/ld \
                    --with-ar=${prefix}/bin/ar \
                    --with-bugurl=https://trac.macports.org/newticket
if {${configure.sdkroot} ne ""} {
    configure.args-append --with-build-sysroot="${configure.sdkroot}"
}


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

configure.cxxflags-append \
                    -std=c++03

if {${os.platform} eq "darwin" && ${build_arch} eq "x86_64"} {
    configure.args-append   --build=${build_arch}-apple-${os.platform}${os.major}
}
use_parallel_build  yes
universal_variant   no

build.dir           ${configure.dir}

post-destroot {
    file delete -force ${destroot}${prefix}/include
    file delete -force ${destroot}${prefix}/share/${name}
    ln -s ${prefix}/lib/${name}/bin/gupc ${destroot}${prefix}/bin/
}

livecheck.type      regex
livecheck.url       ${homepage}
livecheck.regex     {<h3>([0-9]+.[0-9]+.[0-9]+.[0-9]+)</h3>}
