# -*- 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           muniversal 1.0
PortGroup           github 1.0
PortGroup           compiler_blacklist_versions 1.0

github.setup        intel tbb 2020.3 v
revision            0

categories          devel
platforms           darwin
maintainers         nomaintainer
license             Apache-2

description         Intel Threading Building Blocks offers a rich and complete approach \
                    to parallelism in C++

long_description    {*}${description} \
                    It represents a higher-level, task-based parallelism that abstracts \
                    platform details and threading mechanisms for scalability and performance

homepage            https://www.threadingbuildingblocks.org/

checksums           rmd160  64fd69e42540ef8968d9a3e36cb23816ca9de647 \
                    sha256  131d249f3132f643738d71e37162e4ac73f96bdb5064ccd35f03967fdf9b477e \
                    size    2640358

github.livecheck.regex  {([0-9.]+)}

use_configure       no

# Force a compatible compiler
compiler.blacklist-append *clang*

# patchfiles patch-macos.inc.diff \
# patch-macos.gcc.inc.diff
# platform darwin powerpc {
# if {${build_arch} eq "ppc"} {
# patchfiles-append patch-mac32-tbb-export.def.diff
# }
# }

# set tbb_arch(arm64)     arm64
# set tbb_arch(i386)      ia32
set tbb_arch(ppc)       ppc32
set tbb_arch(ppc64)     ppc64
# set tbb_arch(x86_64)    intel64

# if {![info exists tbb_arch(${configure.build_arch})]} {
#     set tbb_arch(${configure.build_arch}) ${configure.build_arch}
# }

set tbb_compiler gcc


set tbb_build_prefix macports

patchfiles        patch-tbb-older-malloc.diff

# build.env-append \
#     CONLY=${configure.cc} \
#     CPLUS=${configure.cxx}

build.env-append \
    CONLY=/usr/bin/gcc \
    CPLUS=/usr/bin/g++

post-patch {
    # http://software.intel.com/en-us/forums/topic/505370
    reinplace "/ -dynamiclib/s|$| -install_name ${prefix}/lib/\$@|g" ${worksrcpath}/build/macos.${tbb_compiler}.inc
}

build.target        tbb tbbmalloc tbbproxy
build.args-append   compiler=${tbb_compiler} \
                    stdlib=${configure.cxx_stdlib} \
                    tbb_build_prefix=${tbb_build_prefix} \
                    use_proxy=1

test.run            yes
test.target         test
test.args-append    ${build.args}

if {${universal_possible} && [variant_isset universal]} {
    foreach arch ${configure.universal_archs} {
        set merger_build_args(${arch}) arch=$tbb_arch(${arch})
        set merger_test_args(${arch}) arch=$tbb_arch(${arch})
    }
} else {
    build.args-append arch=$tbb_arch(${configure.build_arch})
    test.args-append arch=$tbb_arch(${configure.build_arch})
}

# The Makefile does not have an install target.
# http://software.intel.com/en-us/forums/topic/347428
destroot.dir    ${worksrcpath}/build/${tbb_build_prefix}_release
destroot {
    # fix paths
    reinplace -W ${destroot.dir} "s|\\(TBBROOT.\\)\".*\"|\\1\"${prefix}\"|g" tbbvars.sh tbbvars.csh
    reinplace -W ${destroot.dir} "s|\\(tbb_bin.\\)\".*\"|\\1\"${prefix}/lib\"|g" tbbvars.sh tbbvars.csh

    # copy lib, include & bin
    copy {*}[glob ${destroot.dir}/lib*.dylib] ${destroot}${prefix}/lib
    copy {*}[glob ${destroot.dir}/tbbvars.*sh] ${destroot}${prefix}/bin
    copy ${worksrcpath}/include/tbb ${destroot}${prefix}/include/tbb

    # copy examples & docs
    xinstall -d ${destroot}${prefix}/share/${name}
    copy ${worksrcpath}/doc ${destroot}${prefix}/share/${name}/doc
    copy ${worksrcpath}/examples ${destroot}${prefix}/share/${name}/examples
}
