# Note that this script can accept some limited command-line arguments, run
# `julia build_tarballs.jl --help` to see a usage message.
using BinaryBuilder
version = v"1.18.4"
# We bumped the version number because we updated the dependencies (for new architectures)
ygg_version = v"1.18.7"

name = "Cairo"

sources = [
    ArchiveSource("https://cairographics.org/releases/cairo-1.18.4.tar.xz",
                  "445ed8208a6e4823de1226a74ca319d3600e83f6369f99b14265006599c32ccb"),
]

# Bash recipe for building across all platforms
script = raw"""
# Ensure LZO is not present in the build environment — meson auto-detects it,
# so its mere presence would cause Cairo to link against GPL code.
if [ -f "${includedir}/lzo/lzoconf.h" ]; then
    echo "ERROR: LZO found in build environment!" >&2
    exit 1
fi

cd $WORKSPACE/srcdir/cairo*

# Add nipc_rmid_deferred_release = false for non linux builds to avoid running test
if [[ "${target}" != x86_64-linux-* ]]; then
    sed -i -e "s~cmake_defaults = .*~cmake_defaults = false\nipc_rmid_deferred_release = false~" ${MESON_TARGET_TOOLCHAIN}
elif [[ "${target}" == "${MACHTYPE}" ]]; then
    # Remove system libexpat to avoid confusion
    rm /usr/lib/libexpat.so*
fi

if [[ "${target}" == *-freebsd* ]]; then
    # Fix the error: undefined reference to `backtrace_symbols'
    sed -i -e "s~c_link_args = .*~c_link_args = ['-L${includedir}', '-lexecinfo']~" ${MESON_TARGET_TOOLCHAIN}
    sed -i -e "s~cpp_link_args = .*~cpp_link_args = ['-L${libdir}', '-lexecinfo']~" ${MESON_TARGET_TOOLCHAIN}
fi

mkdir output && cd output

meson .. --cross-file=${MESON_TARGET_TOOLCHAIN} \
    -Dfreetype=enabled \
    -Dfontconfig=enabled \
    -Dtee=enabled \
    -Dpng=enabled \
    -Dzlib=enabled \
    -Dglib=enabled \
    -Ddefault_library=shared \
    -Dtests=disabled \
    -Ddwrite=disabled

if [[ "${target}" == *apple* ]]; then
    # Fix the error: undefined reference to `backtrace_symbols'
    sed -i -e "s~HAVE_CXX11_ATOMIC_PRIMITIVES~HAVE_OS_ATOMIC_OPS~" config.h
    echo "#define SIZEOF_VOID_P 8" >> config.h
    echo "#define HAVE_UINT64_T 1" >> config.h
    echo "#define HAVE_FT_SVG_DOCUMENT 1" >> config.h
fi

ninja -j${nproc}
ninja install
"""

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

# The products that we will ensure are always built
products = [
    LibraryProduct("libcairo-gobject", :libcairo_gobject),
    LibraryProduct("libcairo-script-interpreter", :libcairo_script_interpreter),
    LibraryProduct("libcairo", :libcairo),
]

# Some dependencies are needed only on Linux and FreeBSD
linux_freebsd = filter(p->Sys.islinux(p) || Sys.isfreebsd(p), platforms)

dependencies = [
    BuildDependency("Xorg_xorgproto_jll"; platforms=linux_freebsd),
    Dependency("Glib_jll"; compat="2.84.0"),
    Dependency("Pixman_jll"; compat="0.46.4"),
    Dependency("libpng_jll"; compat="1.6.47"),
    Dependency("Fontconfig_jll"; compat="2.16.0"),
    Dependency("FreeType2_jll"; compat="2.13.4"),
    Dependency("Bzip2_jll"; compat="1.0.9"),
    Dependency("Xorg_libXext_jll"; platforms=linux_freebsd),
    Dependency("Xorg_libXrender_jll"; platforms=linux_freebsd),
    Dependency("Zlib_jll"; compat="1.2.12"),
    # libcairo needs libssp on Windows, which is provided by CSL, but not in all versions of
    # Julia.  Note that above we're copying libssp to libdir for the versions of Julia where
    # this wasn't available.
    Dependency("CompilerSupportLibraries_jll"; platforms=filter(Sys.iswindows, platforms)),
]

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