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

github.setup        oneapi-src oneTBB 2023.1.0 v
name                [string tolower ${github.project}]
revision            0
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  f4f22d2e385ee6168f02dfe4f2b78ffd5567aefa \
                    sha256  191288b52e1e6b17198000b64d77d194bb65e791be46ebc606e9b091781e2070 \
                    size    7722887
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

# ittnotify is supposed to be used only on x86:
patchfiles-append   patch-no-ittnotify.diff

# Fix regression since 2021.13.0.
# Program received signal EXC_BAD_ACCESS, Could not access memory.
# Reason: KERN_INVALID_ADDRESS at address: 0x073dd484
# 0x073dd484 in scalable_aligned_free ()
# (gdb) bt
# #0  0x073dd484 in scalable_aligned_free ()
# #1  0x01847d58 in tbb::detail::r1::global_control_release ()
# #2  0x01848700 in tbb::detail::r1::__TBB_InitOnce::~__TBB_InitOnce ()
# #3  0x90e7eaa0 in __cxa_finalize ()
# #4  0x90e7e8a0 in exit ()
# #5  0x000074bc in start ()
# TODO: this fixes a proximate cause and allows executables not to segfault
# (tested with cabin and scip). This does not guarantee the bug won't pop up elsewhere,
# so it still needs to be debugged and possibly fixed further.
patchfiles-append   patch-fix-runtime-segfault.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 {${configure.build_arch} ni [list ppc ppc64]} {
    # In Rosetta this setting breaks the build; restrict it to non-PPC:
#     cmake.set_osx_architectures no
# }

if {${os.platform} eq "darwin" && ${os.major} < 11} {
    # 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
}

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

if {${configure.build_arch} in [list ppc ppc64]} {
    # Rather than disabling -Werror altogether, just disable these:
    configure.cxxflags-append \
                    -Wno-error=parentheses \
                    -Wno-error=unused-function
}

if {[string match *gcc* ${configure.compiler}] && ${configure.build_arch} in [list i386 ppc]} {
    # ___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.]+)}
