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

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

name = "Geant4"
version = v"11.4.1"

# Collection of sources required to build
sources = [
    ArchiveSource("https://gitlab.cern.ch/geant4/geant4/-/archive/v$(version)/geant4-v$(version).tar.gz",
                  "99dcf5f9d4f806fb8c4fde85cb2674a42e4ca19833143464ff7efa55c1852140"),
    DirectorySource("./bundled")
]

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

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

mkdir build && cd build
FLAGS=()
if [[ "${target}" != *-w64-* && "${target}" != *-apple-* ]]; then
    FLAGS+=(-DGEANT4_USE_OPENGL_X11=ON)
fi
if [[ "${target}" == *-apple-* ]]; then
    FLAGS+=(-DGEANT4_USE_SYSTEM_ZLIB=ON)
fi
cmake -DCMAKE_INSTALL_PREFIX=${prefix} \
      -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN} \
      -DCMAKE_BUILD_TYPE=Release \
      -DCMAKE_CXX_STANDARD=17 \
      -DGEANT4_INSTALL_DATA=ON \
      -DGEANT4_USE_GDML=ON \
      -DGEANT4_BUILD_TLS_MODEL=global-dynamic \
      "${FLAGS[@]}" \
    ..
make -j${nproc}
make install

install_license ../LICENSE
"""

# Install a newer SDK which supports `std::filesystem`
sources, script = require_macos_sdk("10.15", sources, script)

# These are the platforms we will build for by default, unless further
# platforms are passed in on the command line
platforms = expand_cxxstring_abis(supported_platforms())
platforms = filter(p -> libc(p) != "musl" && 
                        os(p) != "freebsd" && 
                        arch(p) != "armv6l" && 
                        arch(p) != "i686" &&
                        arch(p) != "riscv64", platforms)

# The products that we will ensure are always built
products = [
    LibraryProduct("libG4vis_management", :libG4visManagement),
    LibraryProduct("libG4FR", :libG4FR),
    LibraryProduct("libG4event", :libG4Event),
    LibraryProduct("libG4analysis", :libG4Analysis),
    LibraryProduct("libG4digits_hits", :libG4Digits),
    LibraryProduct("libG4run", :libG4Run),
    LibraryProduct("libG4clhep", :libG4CLHEP),
    LibraryProduct("libG4GMocren", :libG4Mocren),
    LibraryProduct("libG4particles", :libG4Particles),
    LibraryProduct("libG4graphics_reps", :libG4Graphics),
    LibraryProduct("libG4geometry", :libG4Geometry),
    LibraryProduct("libG4modeling", :libG4Modeling),
    LibraryProduct("libG4interfaces", :libG4Interfaces),
    LibraryProduct("libG4mctruth", :libG4MCTruth),
    LibraryProduct("libG4geomtext", :libG4GeomText),
    LibraryProduct("libG4gdml", :libG4GDML),
    LibraryProduct("libG4track", :libG4Track),
    LibraryProduct("libG4error_propagation", :libG4ErrorPropagation),
    LibraryProduct("libG4parmodels", :libG4ParModels),
    LibraryProduct("libG4materials", :libG4Material),
    LibraryProduct("libG4physicslists", :libG4PhysicsLists),
    LibraryProduct("libG4VRML", :libG4VRML),
    LibraryProduct("libG4readout", :libG4Readout),
    LibraryProduct("libG4RayTracer", :libG4RayTracer),
    LibraryProduct("libG4Tree", :libG4Tree),
    LibraryProduct("libG4processes_core", :libG4Processes_core),
    LibraryProduct("libG4processes_hadronic", :libG4Processes_hadronic),
    LibraryProduct("libG4global", :libG4Global),
    LibraryProduct("libG4tracking", :libG4Tracking),
    LibraryProduct("libG4intercoms", :libG4Intercoms),
    LibraryProduct("libG4ptl", :libG4Ptl),
    LibraryProduct("libG4ToolsSG", :libG4ToolsSG),
    FileProduct("share/Geant4/data", :data_dir),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    Dependency("Expat_jll"; compat="2.6.4"),
    Dependency("Xorg_libXmu_jll"),
    Dependency("Libglvnd_jll"),
    Dependency("Xerces_jll"),
    Dependency("Zlib_jll"; compat="1.2.12", platforms=filter(Sys.isapple, platforms)),
    BuildDependency("Xorg_xorgproto_jll"),
]

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