# -*- 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           cmake 1.1
PortGroup           github 1.0
PortGroup           legacysupport 1.1

# posix_memalign
legacysupport.newest_darwin_requires_legacy 9

name                onetbb
github.setup        oneapi-src oneTBB 2023.0.0 v
revision            1
categories          devel parallel
license             Apache-2
maintainers         {makr @mohd-akram} \
                    {@barracuda156 macos-powerpc.org:barracuda} \
                    openmaintainer

description         oneAPI Threading Building Blocks
long_description    oneTBB is a flexible C++ library that simplifies the work \
                    of adding parallelism to complex applications, even if you \
                    are not a threading expert.

checksums           rmd160  45fa39702c4f0deec5d6ce183719a53ab6e35acf \
                    sha256  f8767b971ec6aea25dde58ae0f593e94e7aa75a739a86f67967012f69e2199b1 \
                    size    7554268
github.tarball_from archive

# ! Symbol exports must be checked on every update !
# See: https://github.com/OVVO-Financial/NNS/issues/13
patchfiles          patch-onetbb-older-platforms.diff \
                    patch-Restore-32-bit-symbol-export-defs.diff

# Fix for 10.5, adjustment for 10.6.8 ppc:
patchfiles-append   patch-malloc_zone.diff

# 10.6.8 powerpc does not need this.
if {${os.platform} eq "darwin" && ${os.major} < 10} {
    patchfiles-append \
                    patch-onetbb-dispatch-fallback.diff
}

# https://trac.macports.org/ticket/69657
# https://trac.macports.org/ticket/72724
compiler.blacklist-append {clang < 1200}

depends_build-append    path:bin/pkg-config:pkgconfig

depends_lib-append      port:hwloc

compiler.cxx_standard   2011

# OneTBB's build uses CMAKE_OSX_ARCHITECTURES to add -arch flags which may lead
# to missed symbols from exported symbol list on universal build like:
# Undefined symbols for architecture i386:
#  "tbb::detail::r1::wait_bounded_queue_monitor(tbb::detail::r1::concurrent_monitor*, unsigned long, long, tbb::detail::d1::delegate_base&)", referenced from:
#     -exported_symbol[s_list] command line option
# if {${os.arch} ne "powerpc"} {
    # In Rosetta this setting breaks the build; restrict it to non-PPC:
#     cmake.set_osx_architectures no
# }

if {${os.platform} eq "darwin" && ${os.major} < 10} {
    # Undefined symbols for architecture ppc64: "vtable for tbb::detail::r1::unsafe_wait"
    configure.args-append \
                        -DTBB_ENABLE_IPO=OFF
}

configure.args-append   -DTBB_TEST=OFF

# install it into libexec to avoid conflict with tbb port
cmake.install_prefix ${prefix}/libexec/${name}
# but keep this port at prefix's cmake's lib
post-destroot {
    xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake
    foreach cmakedir [glob -type d -nocomplain -tails -directory ${destroot}${cmake.install_prefix}/lib/cmake *] {
        xinstall -d -m 0755 ${destroot}${prefix}/lib/cmake/${cmakedir}
        foreach cmakefile [glob -tails -directory ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir} *.cmake] {
            ln -s ${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile} \
                ${destroot}${prefix}/lib/cmake/${cmakedir}/
            reinplace -q "s|\\\${CMAKE_CURRENT_LIST_DIR}|${cmake.install_prefix}/lib/cmake/${cmakedir}|g" \
                 ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}
            reinplace -q "s|\\\${CMAKE_CURRENT_LIST_FILE}|${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}|g" \
                 ${destroot}${cmake.install_prefix}/lib/cmake/${cmakedir}/${cmakefile}
        }
    }
}

platform powerpc {
    # Rather than disabling -Werror altogether, just disable these:
    configure.cxxflags-append \
                    -Wno-parentheses \
                    -Wno-unused-function
}

if {[string match *gcc* ${configure.compiler}] && ${configure.build_arch} in [list i386 ppc]} {
    # FIXME: for now disable due to:
    # Undefined symbols: "__ZTVN3tbb6detail2r111unsafe_waitE", referenced from:
    # -exported_symbol[s_list] command line option
    configure.args-append \
                    -DTBB_ENABLE_IPO=OFF

    # ___atomic_fetch_add_8, ___atomic_load_8, ___atomic_fetch_sub_8
    # Drop once this is merged: https://github.com/oneapi-src/oneTBB/pull/987
    configure.ldflags-append \
                    -latomic
}

variant tests description {Enable tests} {
    # Ensure test binaries can find dylibs:
    configure.pre_args-replace \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                    -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

    configure.args-delete   -DTBB_TEST=OFF

    if {${configure.cxx_stdlib} ne "libc++"} {
        # To mitigate malloc errors with libstdc++:
        test.env-append     DYLD_LIBRARY_PATH=${prefix}/lib/libgcc
    }
    test.run                yes
}

# This variant is aimed at supported legacy powerpc systems.
# It works on some at the moment, but is not ready to be the default.
# Old Intel systems can be added later upon testing.
# Not needed on 10.6.8 ppc.
if {${os.platform} eq "darwin" && ${configure.build_arch} in [list ppc ppc64]} {
    variant legacy_dispatch description "Enable dispatch via libdispatch-legacy port" {
        if {${os.major} < 10} {
            # Drop fallback code:
            patchfiles-delete   patch-onetbb-dispatch-fallback.diff
        }
        depends_lib-append \
                    port:libdispatch-legacy
        configure.cppflags-append \
                    -I${prefix}/libexec/dispatch/usr/include
        configure.ldflags-append \
                    ${prefix}/libexec/dispatch/usr/lib/libdispatch.a
    }
}

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