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

name = "ICU"
version = v"69.1"

# Collection of sources required to build ICU
sources = [
    ArchiveSource("https://github.com/unicode-org/icu/releases/download/release-$(version.major)-$(version.minor)/icu4c-$(version.major)_$(version.minor)-src.tgz",
                  "4cba7b7acd1d3c42c44bb0c14be6637098c7faf2b330ce876bc5f3b915d09745"),
    DirectorySource("./bundled"),
]

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

# Apply patch to link `libicudata` against the default standard libraries
# to avoid toolchain weirdness when you have a dynamic library that has
# _no_ dependencies (not even `libc`).  See this bug report for more:
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=653457
atomic_patch -p1 $WORKSPACE/srcdir/patches/yes_stdlibs.patch

# Do the native build
(
    cp -r source/ native_build/
    cd native_build
    CC="${CC_BUILD}"
    CXX="${CXX_BUILD}"
    AR="${AR_BUILD}"
    LD="${LD_BUILD}"
    RANLIB="${RANLIB_BUILD}"

    # See https://git.alpinelinux.org/aports/tree/main/icu/APKBUILD?id=334ebffde9dec34becdd628ad56007699e98ea81
    update_configure_scripts
    sed -i -e 's,DU_HAVE_STRTOD_L=1,DU_HAVE_STRTOD_L=0,' configure.ac
    sed -i -e 's,DU_HAVE_STRTOD_L=1,DU_HAVE_STRTOD_L=0,' configure
    for x in ARFLAGS CFLAGS CPPFLAGS CXXFLAGS FFLAGS LDFLAGS; do
        sed -i -e "/^${x} =.*/s:@${x}@::" "config/Makefile.inc.in"
    done

    ./configure --prefix=$prefix --build=${MACHTYPE} \
        ac_cv_prog_ac_ct_AR=${AR} \
        ac_cv_prog_ac_ct_RANLIB=${RANLIB}
    make -j${nproc}
)

# Do the cross build
cd source/

if [[ "${target}" == *-apple-* ]]; then
    # Do not append `-c` flag to ar, which isn't supported by LLVM's ar
    atomic_patch -p1 $WORKSPACE/srcdir/patches/argflags-no--c.patch
    export LDFLAGS="-headerpad_max_install_names"
fi

update_configure_scripts
./configure --prefix=$prefix --host=$target \
    --with-cross-build="/workspace/srcdir/icu/native_build"
make -j${nproc}
make install
"""

# 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(; experimental=true))

# The products that we will ensure are always built
products = [
    LibraryProduct(["libicudata", "icudt$(version.major)"], :libicudata; dont_dlopen=true),
    LibraryProduct(["libicui18n", "icuin$(version.major)"], :libicui18n),
    LibraryProduct(["libicuio", "icuio$(version.major)"], :libicuio),
    LibraryProduct(["libicutest", "icutest$(version.major)"], :libicutest),
    LibraryProduct(["libicutu", "icutu$(version.major)"], :libicutu),
    LibraryProduct(["libicuuc", "icuuc$(version.major)"], :libicuuc),
]

# Dependencies that must be installed before this package can be built
dependencies = Dependency[
]

# Build the tarballs.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies; preferred_gcc_version=v"7", julia_compat="1.6")
