# 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
using BinaryBuilderBase: sanitize

name = "libblastrampoline"

# Collection of sources required to build libblastrampoline
function lbt_sources(version::VersionNumber; kwargs...)
    lbt_version_commit = Dict(
        v"5.4.0"  => "d00e6ca235bb747faae4c9f3a297016cae6959ed",
        v"5.11.2" => "c48da8a1225c2537ff311c28ef395152fb879eae",
        v"5.12.0" => "b127bc8dd4758ffc064340fff2aef4ead552f386",
        v"5.13.1" => "f26278e83ddc9035ae7695da597f1a5b26a4c62b",
        v"5.14.0" => "4cca50aa5101a4ed5f2c85e4760090a23d4aa5a5",
        v"5.15.0" => "072b5f67895bec0b92f8c83194567c1c48e9833d",
    )

    return [
        GitSource("https://github.com/JuliaLinearAlgebra/libblastrampoline.git",
                  lbt_version_commit[version]),
    ]
end

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

if [[ ${bb_full_target} == *-sanitize+memory* ]]; then
    # Install msan runtime (for clang)
    cp -rL ${libdir}/linux/* /opt/x86_64-linux-musl/lib/clang/*/lib/linux/
fi

make -j${nproc} prefix=${prefix} install
"""

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = supported_platforms()
push!(platforms, Platform("x86_64", "linux"; sanitize="memory"))

# The products that we will ensure are always built
products = [
    LibraryProduct("libblastrampoline", :libblastrampoline)
]

llvm_version = v"13.0.1"

# Dependencies that must be installed before this package can be built
dependencies = [
    BuildDependency(PackageSpec(name="LLVMCompilerRT_jll",
                                uuid="4e17d02c-6bf5-513e-be62-445f41c75a11",
                                version=string(llvm_version));
    platforms=filter(p -> sanitize(p)=="memory", platforms)),
]
