# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder, Pkg

name = "oneTBB"
version = v"2022.3.0"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/oneapi-src/oneTBB.git",
              "f1862f38f83568d96e814e469ab61f88336cc595"),
    DirectorySource("./bundled"),
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/oneTBB*

if [[ ${target} == *mingw* ]]; then
    atomic_patch -p1 "${WORKSPACE}/srcdir/patches/mingw.patch"

    # `CreateSemaphoreEx` requires at least Windows Vista/Server 2008:
    # https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createsemaphoreexa
    export CXXFLAGS="-D_WIN32_WINNT=0x0600 -DLOAD_LIBRARY_SAFE_CURRENT_DIRS=LOAD_LIBRARY_SEARCH_DEFAULT_DIRS"
fi

if [[ ${target} == i686-linux-musl* ]]; then
    # Disable strong stack protection. Our musl version doesn't
    # provide the symbol `__stack_chk_fail_local` in the way GCC expects.
    atomic_patch -p1 "${WORKSPACE}/srcdir/patches/i686-musl.patch"
fi

if [[ "${target}" == *-freebsd* ]]; then
    # Follow oneTBB makefile fix for missing symbols error: https://cgit.freebsd.org/ports/commit/?id=3677983542cc09a0e5f085e463a895e9e4dce9aa
    export LDFLAGS="${LDFLAGS} -Wl,--undefined-version"
fi

cmake -B build -G Ninja \
    -DCMAKE_INSTALL_PREFIX=${prefix} \
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
    -DCMAKE_BUILD_TYPE=Release \
    -DTBB_STRICT=OFF \
    -DTBB_TEST=OFF \
    -DTBB_EXAMPLES=OFF
cmake --build build --parallel ${nproc}
cmake --install build
"""


platforms = supported_platforms()
platforms = expand_cxxstring_abis(platforms)

# The products that we will ensure are always built
products = [
    LibraryProduct("libtbbmalloc", :libtbbmalloc),
    LibraryProduct("libtbbmalloc_proxy", :libtbbmalloc_proxy),
    LibraryProduct(["libtbb", "libtbb12"], :libtbb),
]

# Dependencies that must be installed before this package can be built
dependencies = Dependency[
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies;
               julia_compat="1.6", lazy_artifacts=true, preferred_gcc_version=v"9")
