# 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

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

name = "SCIP_PaPILO"

upstream_version = v"10.0.3"
version = VersionNumber(upstream_version.major * 100, upstream_version.minor * 100, upstream_version.patch * 100)

# Collection of sources required to complete build
sources = [
    ArchiveSource(
        "https://github.com/scipopt/scip/releases/download/v$(upstream_version)/scipoptsuite-$(upstream_version).tgz",
        "b6af618adc62c2f945a531f28eaf65f152a201913fa261000a5b711d5968aa85",
    ),
    DirectorySource("./bundled/")
]

# Bash recipe for building across all platforms
script = raw"""
cd scipoptsuite*

# for soplex threadlocal
export CXXFLAGS="-DTHREADLOCAL=''"

# Enable large file support on mingw
if [[ "${target}" == *-mingw* ]]; then
    export CXXFLAGS="-Wa,-mbig-obj"
fi

# Patch to fix linking with gfortran's library on mingw
# https://github.com/JuliaPackaging/Yggdrasil/pull/8224#issuecomment-2034941690
atomic_patch -p0 $WORKSPACE/srcdir/patches/papilo_cmake.patch

mkdir build
cd build/
cmake -DCMAKE_INSTALL_PREFIX=$prefix\
  -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN}\
  -DCMAKE_BUILD_TYPE=Release\
  -DZIMPL=OFF\
  -DUG=0\
  -DAMPL=0\
  -DGCG=0\
  -DBOOST=ON\
  -DMPFR=ON\
  -DSYM=snauty\
  -DTPI=tny\
  -DIPOPT_DIR=${prefix} \
  -DIPOPT_LIBRARIES=${libdir} \
  -DBLISS_INCLUDE_DIR=${includedir} \
  -DBLISS_LIBRARY=bliss \
  -DBUILD_TESTING=OFF \
  ..
make -j${nproc} scip
make papilo-executable

make install
cp bin/papilo${exeext} "${bindir}/papilo${exeext}"

mkdir -p ${prefix}/share/licenses/SCIP_PaPILO
mv -v ${prefix}/share/licenses/scip ${prefix}/share/licenses/SCIP_PaPILO
mv -v ${prefix}/share/licenses/soplex ${prefix}/share/licenses/SCIP_PaPILO
mv -v ${prefix}/share/licenses/papilo ${prefix}/share/licenses/SCIP_PaPILO
"""

# This requires macOS 10.13
sources, script = require_macos_sdk("10.13", sources, script)

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = supported_platforms(; experimental=true)
platforms = expand_cxxstring_abis(platforms)
platforms = expand_gfortran_versions(platforms)

# Filter out the aarch64 FreeBSD and RISC-V architectures because oneTBB isn't available there yet.
filter!(p -> !(Sys.isfreebsd(p) && arch(p) == "aarch64"), platforms)
filter!(p -> !(Sys.islinux(p) && arch(p) == "riscv64"), platforms)

filter!(platforms) do p
    libgfortran_version(p) >= v"5"
end


# The products that we will ensure are always built
products = [
    ExecutableProduct("papilo", :papilo),
    ExecutableProduct("scip", :scip),
    LibraryProduct("libscip", :libscip),
]

dependencies = [
    Dependency(PackageSpec(name="bliss_jll", uuid="508c9074-7a14-5c94-9582-3d4bc1871065"), compat="=0.77.0"),
    Dependency(PackageSpec(name="boost_jll", uuid="28df3c45-c428-5900-9ff8-a3135698ca75"); compat="=1.87.0"),
    Dependency(PackageSpec(name="Bzip2_jll", uuid="6e34b625-4abd-537c-b88f-471c36dfa7a0"); compat="1.0.9"),
    Dependency(PackageSpec(name="CompilerSupportLibraries_jll", uuid="e66e0078-7015-5450-92f7-15fbd957f2ae")),
    Dependency(PackageSpec(name="GMP_jll", uuid="781609d7-10c4-51f6-84f2-b8444358ff6d"); compat="6.2.1"),
    Dependency(PackageSpec(name="Ipopt_jll", uuid="9cc047cb-c261-5740-88fc-0cf96f7bdcc7"); compat="=300.1400.1900"),
    Dependency(PackageSpec(name="MPFR_jll", uuid="3a97d323-0669-5f0c-9066-3539efd106a3"); compat="4.2.0"),
    Dependency(PackageSpec(name="OpenBLAS32_jll", uuid="656ef2d0-ae68-5445-9ca0-591084a874a2"); compat="0.3.10"),
    Dependency(PackageSpec(name="oneTBB_jll", uuid="1317d2d5-d96f-522e-a858-c73665f53c3e"); compat="2022.0.0"),
    Dependency(PackageSpec(name="Readline_jll", uuid="05236dd9-4125-5232-aa7c-9ec0c9b2c25a")),
    Dependency(PackageSpec(name="Zlib_jll", uuid="83775a58-1f1d-513f-b197-d71354ab007a")),
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(
    ARGS,
    name,
    version,
    sources,
    script,
    platforms,
    products,
    dependencies;
    preferred_gcc_version=v"12",
    julia_compat="1.9",
    clang_use_lld=false,
)
