using BinaryBuilder, Pkg

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

name = "RDKit"
version = v"2026.03.1"

sources = [
    GitSource("https://github.com/rdkit/rdkit.git", "351f8f378f8ad6bbd517980c38896e66bf907af8"),
    DirectorySource("./bundled"),
]

script = raw"""
cd ${WORKSPACE}/srcdir/rdkit

# Windows build fails to link a test, despite the fact we don't want tests.
atomic_patch -p1 ../patches/do-not-build-cffi-test.patch

FLAGS=()
if [[ "${target}" == *-mingw* ]]; then
    FLAGS+=(-DRDK_BUILD_THREADSAFE_SSS=OFF)
fi

mkdir build
cd build
cmake \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
    -DCMAKE_INSTALL_PREFIX=${prefix} \
    -DRDK_INSTALL_INTREE=OFF \
    -DRDK_BUILD_INCHI_SUPPORT=ON \
    -DRDK_BUILD_PYTHON_WRAPPERS=OFF \
    -DRDK_BUILD_CFFI_LIB=ON \
    -DRDK_BUILD_FREETYPE_SUPPORT=ON \
    -DRDK_BUILD_CPP_TESTS=OFF \
    -DRDK_BUILD_SLN_SUPPORT=OFF \
    -DRDK_TEST_MULTITHREADED=OFF \
    "${FLAGS[@]}" \
    ..
make -j${nproc}
make install
"""

# macOS requires a newer SDK for C++20 shared_ptr<T[]> support
sources, script = require_macos_sdk("10.14", sources, script)

platforms = supported_platforms()
# Filter out Windows: boost_jll 1.87.0 does not provide mingw artifacts
filter!(!Sys.iswindows, platforms)
# Filter out FreeBSD: Boost.Stacktrace incompatible with FreeBSD unwind
filter!(!Sys.isfreebsd, platforms)
# Filter out macOS: BinaryBuilder's macOS cross-compilation toolchain uses an
# older libc++ that lacks C++20 std::shared_ptr<T[]> specialization support.
# RDKit 2026.03 uses shared_ptr<double[]> extensively in Code/Numerics/Vector.h.
# This requires an upstream toolchain upgrade, not an SDK version bump.
filter!(!Sys.isapple, platforms)

platforms = expand_cxxstring_abis(platforms)

products = [
    LibraryProduct("librdkitcffi", :librdkitcffi),
]

dependencies = [
    Dependency("FreeType2_jll"; compat="2.13.4"),
    Dependency("boost_jll"; compat="1.87.0"),
    BuildDependency("Eigen_jll"),
    Dependency("Zlib_jll"),
]

build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies;
               # GCC 11 is needed for C++20
               preferred_gcc_version=v"11", julia_compat="1.10")
