# 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

const YGGDRASIL_DIR = "../.."
include(joinpath(YGGDRASIL_DIR, "platforms", "microarchitectures.jl"))
include(joinpath(YGGDRASIL_DIR, "platforms", "macos_sdks.jl"))

name = "finufft"
version = v"2.5.1"
commit_hash = "679d9ae59fe0146c50da360e5a3fee70ae3aa646"
preferred_gcc_version = v"10"
preferred_llvm_version = v"13.0.1+1"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/flatironinstitute/finufft.git", commit_hash)
]

# Bash recipe for building across all platforms
script = raw"""
cd $WORKSPACE/srcdir/finufft*/
apk del cmake

toolchain="${CMAKE_TARGET_TOOLCHAIN}"
if [[ "${target}" == *-apple-* ]]; then
    toolchain="${CMAKE_TARGET_TOOLCHAIN%.*}_gcc.cmake"
    
    # Apparently, we also need to remove the -ld_classic link option
    sed -i '/add_link_options("-ld_classic")/d' src/CMakeLists.txt
fi

cmake -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_PREFIX_PATH="${prefix}" \
    -DCMAKE_INSTALL_PREFIX="${prefix}" \
    -DCMAKE_TOOLCHAIN_FILE="${toolchain}" \
    -DFINUFFT_FFTW_SUFFIX="" \
    -DFINUFFT_ARCH_FLAGS="" \
    -DFINUFFT_STATIC_LINKING="OFF"
cmake --build build --parallel $nproc
cmake --install build
"""

# Need macOS SDK >= 10.14
sources, script = require_macos_sdk("10.14", sources, script)

platforms = supported_platforms()

# Remove platforms that do not currently build: armv6, armv7, riscv64
filter!(p -> !(contains(arch(p), "armv")), platforms)
filter!(p -> !(contains(arch(p), "riscv64")), platforms)

# ABI expansion
platforms = expand_cxxstring_abis(platforms; skip=!Sys.iswindows)
# Expand for microarchitectures on x86_64 (library doesn't have CPU dispatching)
platforms = expand_microarchitectures(platforms, ["x86_64", "avx", "avx2", "avx512"])

augment_platform_block = """
    $(MicroArchitectures.augment)

    function augment_platform!(platform::Platform)
        # We augment only x86_64
        @static if Sys.ARCH === :x86_64
            augment_microarchitecture!(platform)
        else
            platform
        end
    end
    """

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

# Dependencies that must be installed before this package can be built
dependencies = [
    Dependency(PackageSpec(name="FFTW_jll", uuid="f5851436-0d7a-5f13-b9de-f02708fd171a")),
    # For OpenMP we use libomp from `LLVMOpenMP_jll` where we use LLVM as compiler (non-Mac BSD
    # systems), and libgomp from `CompilerSupportLibraries_jll` everywhere else.
    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae"); platforms=filter(!Sys.isfreebsd, platforms)),
    Dependency(PackageSpec(name="LLVMOpenMP_jll", uuid="1d63c593-3942-5779-bab2-d838dc0a180e"); platforms=filter(Sys.isfreebsd, platforms)),
    # CMake needs higher version than what is bundled.
    HostBuildDependency(PackageSpec(; name="CMake_jll", version = v"3.24.3+0")),

]

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