# 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 = "abseil_cpp"
version = v"20250814.1"

# Collection of sources required to complete build
sources = [
    GitSource("https://github.com/abseil/abseil-cpp", "d38452e1ee03523a208362186fd42248ff2609f6"),
]

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

# abseil requires C++17, and its ABI depends on the standard used (see the
# auto-detected options in absl/base/options.h), so propagate cxx_std_17 to
# consumers via the installed CMake config.
cmake -B build -G Ninja \
    -DABSL_PROPAGATE_CXX_STD=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_CXX_STANDARD=17 \
    -DCMAKE_FIND_ROOT_PATH=${prefix} \
    -DCMAKE_INSTALL_PREFIX=${prefix} \
    -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TARGET_TOOLCHAIN}
cmake --build build --parallel ${nproc}
cmake --install build
install_license LICENSE
"""

# In C++17 mode abseil uses std::variant/std::optional/std::any, which need a
# newer libc++ and deployment target than the default Intel macOS shard provides
sources, script = require_macos_sdk("10.14", 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()
platforms = expand_cxxstring_abis(platforms)

# The products that we will ensure are always built
products = [
    LibraryProduct("libabsl_base", :libabsl_base),
    LibraryProduct("libabsl_city", :libabsl_city),
    LibraryProduct("libabsl_civil_time", :libabsl_civil_time),
    LibraryProduct("libabsl_cord", :libabsl_cord),
    LibraryProduct("libabsl_cord_internal", :libabsl_cord_internal),
    LibraryProduct("libabsl_cordz_functions", :libabsl_cordz_functions),
    LibraryProduct("libabsl_cordz_handle", :libabsl_cordz_handle),
    LibraryProduct("libabsl_cordz_info", :libabsl_cordz_info),
    LibraryProduct("libabsl_cordz_sample_token", :libabsl_cordz_sample_token),
    LibraryProduct("libabsl_crc32c", :libabsl_crc32c),
    LibraryProduct("libabsl_crc_cord_state", :libabsl_crc_cord_state),
    LibraryProduct("libabsl_crc_cpu_detect", :libabsl_crc_cpu_detect),
    LibraryProduct("libabsl_crc_internal", :libabsl_crc_internal),
    LibraryProduct("libabsl_debugging_internal", :libabsl_debugging_internal),
    LibraryProduct("libabsl_decode_rust_punycode", :libabsl_decode_rust_punycode),
    LibraryProduct("libabsl_demangle_internal", :libabsl_demangle_internal),
    LibraryProduct("libabsl_demangle_rust", :libabsl_demangle_rust),
    LibraryProduct("libabsl_die_if_null", :libabsl_die_if_null),
    LibraryProduct("libabsl_examine_stack", :libabsl_examine_stack),
    LibraryProduct("libabsl_exponential_biased", :libabsl_exponential_biased),
    LibraryProduct("libabsl_failure_signal_handler", :libabsl_failure_signal_handler),
    LibraryProduct("libabsl_flags_commandlineflag", :libabsl_flags_commandlineflag),
    LibraryProduct("libabsl_flags_commandlineflag_internal", :libabsl_flags_commandlineflag_internal),
    LibraryProduct("libabsl_flags_config", :libabsl_flags_config),
    LibraryProduct("libabsl_flags_internal", :libabsl_flags_internal),
    LibraryProduct("libabsl_flags_marshalling", :libabsl_flags_marshalling),
    LibraryProduct("libabsl_flags_parse", :libabsl_flags_parse),
    LibraryProduct("libabsl_flags_private_handle_accessor", :libabsl_flags_private_handle_accessor),
    LibraryProduct("libabsl_flags_program_name", :libabsl_flags_program_name),
    LibraryProduct("libabsl_flags_reflection", :libabsl_flags_reflection),
    LibraryProduct("libabsl_flags_usage", :libabsl_flags_usage),
    LibraryProduct("libabsl_flags_usage_internal", :libabsl_flags_usage_internal),
    LibraryProduct("libabsl_graphcycles_internal", :libabsl_graphcycles_internal),
    LibraryProduct("libabsl_hash", :libabsl_hash),
    LibraryProduct("libabsl_hashtable_profiler", :libabsl_hashtable_profiler),
    LibraryProduct("libabsl_hashtablez_sampler", :libabsl_hashtablez_sampler),
    LibraryProduct("libabsl_int128", :libabsl_int128),
    LibraryProduct("libabsl_kernel_timeout_internal", :libabsl_kernel_timeout_internal),
    LibraryProduct("libabsl_leak_check", :libabsl_leak_check),
    LibraryProduct("libabsl_log_entry", :libabsl_log_entry),
    LibraryProduct("libabsl_log_flags", :libabsl_log_flags),
    LibraryProduct("libabsl_log_globals", :libabsl_log_globals),
    LibraryProduct("libabsl_log_initialize", :libabsl_log_initialize),
    LibraryProduct("libabsl_log_internal_check_op", :libabsl_log_internal_check_op),
    LibraryProduct("libabsl_log_internal_conditions", :libabsl_log_internal_conditions),
    LibraryProduct("libabsl_log_internal_fnmatch", :libabsl_log_internal_fnmatch),
    LibraryProduct("libabsl_log_internal_format", :libabsl_log_internal_format),
    LibraryProduct("libabsl_log_internal_globals", :libabsl_log_internal_globals),
    LibraryProduct("libabsl_log_internal_log_sink_set", :libabsl_log_internal_log_sink_set),
    LibraryProduct("libabsl_log_internal_message", :libabsl_log_internal_message),
    LibraryProduct("libabsl_log_internal_nullguard", :libabsl_log_internal_nullguard),
    LibraryProduct("libabsl_log_internal_proto", :libabsl_log_internal_proto),
    LibraryProduct("libabsl_log_internal_structured_proto", :libabsl_log_internal_structured_proto),
    LibraryProduct("libabsl_log_severity", :libabsl_log_severity),
    LibraryProduct("libabsl_log_sink", :libabsl_log_sink),
    LibraryProduct("libabsl_malloc_internal", :libabsl_malloc_internal),
    LibraryProduct("libabsl_periodic_sampler", :libabsl_periodic_sampler),
    LibraryProduct("libabsl_poison", :libabsl_poison),
    LibraryProduct("libabsl_profile_builder", :libabsl_profile_builder),
    LibraryProduct("libabsl_random_distributions", :libabsl_random_distributions),
    LibraryProduct("libabsl_random_internal_distribution_test_util", :libabsl_random_internal_distribution_test_util),
    LibraryProduct("libabsl_random_internal_entropy_pool", :libabsl_random_internal_entropy_pool),
    LibraryProduct("libabsl_random_internal_platform", :libabsl_random_internal_platform),
    LibraryProduct("libabsl_random_internal_randen", :libabsl_random_internal_randen),
    LibraryProduct("libabsl_random_internal_randen_hwaes", :libabsl_random_internal_randen_hwaes),
    LibraryProduct("libabsl_random_internal_randen_hwaes_impl", :libabsl_random_internal_randen_hwaes_impl),
    LibraryProduct("libabsl_random_internal_randen_slow", :libabsl_random_internal_randen_slow),
    LibraryProduct("libabsl_random_internal_seed_material", :libabsl_random_internal_seed_material),
    LibraryProduct("libabsl_random_seed_gen_exception", :libabsl_random_seed_gen_exception),
    LibraryProduct("libabsl_random_seed_sequences", :libabsl_random_seed_sequences),
    LibraryProduct("libabsl_raw_hash_set", :libabsl_raw_hash_set),
    LibraryProduct("libabsl_raw_logging_internal", :libabsl_raw_logging_internal),
    LibraryProduct("libabsl_scoped_set_env", :libabsl_scoped_set_env),
    LibraryProduct("libabsl_spinlock_wait", :libabsl_spinlock_wait),
    LibraryProduct("libabsl_stacktrace", :libabsl_stacktrace),
    LibraryProduct("libabsl_status", :libabsl_status),
    LibraryProduct("libabsl_statusor", :libabsl_statusor),
    LibraryProduct("libabsl_strerror", :libabsl_strerror),
    LibraryProduct("libabsl_str_format_internal", :libabsl_str_format_internal),
    LibraryProduct("libabsl_strings", :libabsl_strings),
    LibraryProduct("libabsl_strings_internal", :libabsl_strings_internal),
    LibraryProduct("libabsl_string_view", :libabsl_string_view),
    LibraryProduct("libabsl_symbolize", :libabsl_symbolize),
    LibraryProduct("libabsl_synchronization", :libabsl_synchronization),
    LibraryProduct("libabsl_throw_delegate", :libabsl_throw_delegate),
    LibraryProduct("libabsl_time", :libabsl_time),
    LibraryProduct("libabsl_time_zone", :libabsl_time_zone),
    LibraryProduct("libabsl_tracing_internal", :libabsl_tracing_internal),
    LibraryProduct("libabsl_utf8_for_code_point", :libabsl_utf8_for_code_point),
    LibraryProduct("libabsl_vlog_config_internal", :libabsl_vlog_config_internal),
]

# Dependencies that must be installed before this package can be built
dependencies = [
    Dependency("CompilerSupportLibraries_jll"),
]

# Build the tarballs, and possibly a `build.jl` as well.
build_tarballs(ARGS, name, version, sources, script, platforms, products, dependencies;
               julia_compat="1.6",
               # abseil headers include <filesystem>, which GCC 7 and 8 do not ship
               preferred_gcc_version=v"9",
)
