load("@bazel_skylib//rules:copy_file.bzl", "copy_file")
load("@rules_cc//cc:cc_static_library.bzl", "cc_static_library")
load("@rules_cc//cc:defs.bzl", "cc_library")

package(default_visibility = ["//visibility:public"])

config_setting(
    name = "platform_aarch64_unknown_linux_musl",
    constraint_values = [
        "@platforms//cpu:aarch64",
        "@platforms//os:linux",
        "@llvm//constraints/libc:musl",
    ],
)

config_setting(
    name = "platform_x86_64_unknown_linux_musl",
    constraint_values = [
        "@platforms//cpu:x86_64",
        "@platforms//os:linux",
        "@llvm//constraints/libc:musl",
    ],
)

alias(
    name = "v8_146_4_0_x86_64_apple_darwin",
    actual = "@rusty_v8_146_4_0_x86_64_apple_darwin_archive//file",
)

alias(
    name = "v8_146_4_0_aarch64_apple_darwin",
    actual = "@rusty_v8_146_4_0_aarch64_apple_darwin_archive//file",
)

alias(
    name = "v8_146_4_0_x86_64_unknown_linux_gnu",
    actual = "@rusty_v8_146_4_0_x86_64_unknown_linux_gnu_archive//file",
)

alias(
    name = "v8_146_4_0_aarch64_unknown_linux_gnu",
    actual = "@rusty_v8_146_4_0_aarch64_unknown_linux_gnu_archive//file",
)

alias(
    name = "v8_146_4_0_x86_64_unknown_linux_musl",
    actual = "@rusty_v8_146_4_0_x86_64_unknown_linux_musl_archive//file",
)

alias(
    name = "v8_146_4_0_aarch64_unknown_linux_musl",
    actual = "@rusty_v8_146_4_0_aarch64_unknown_linux_musl_archive//file",
)

alias(
    name = "v8_146_4_0_x86_64_pc_windows_msvc",
    actual = "@rusty_v8_146_4_0_x86_64_pc_windows_msvc_archive//file",
)

alias(
    name = "v8_146_4_0_aarch64_pc_windows_msvc",
    actual = "@rusty_v8_146_4_0_aarch64_pc_windows_msvc_archive//file",
)

alias(
    name = "v8_146_4_0_aarch64_pc_windows_gnullvm",
    # `rusty_v8` only ships prebuilt Windows archives for MSVC. Build the
    # GNU-flavored archive in-tree so windows-gnullvm consumers can link
    # against a matching ABI instead of trying to reuse the MSVC release.
    actual = ":v8_146_4_0_aarch64_pc_windows_gnullvm_bazel",
)

alias(
    name = "v8_146_4_0_x86_64_pc_windows_gnullvm",
    actual = ":v8_146_4_0_x86_64_pc_windows_gnullvm_bazel",
)

filegroup(
    name = "src_binding_release_x86_64_apple_darwin",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_x86_64_apple_darwin"],
)

filegroup(
    name = "src_binding_release_aarch64_apple_darwin",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_aarch64_apple_darwin"],
)

filegroup(
    name = "src_binding_release_aarch64_unknown_linux_gnu",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_aarch64_unknown_linux_gnu"],
)

filegroup(
    name = "src_binding_release_x86_64_unknown_linux_gnu",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_x86_64_unknown_linux_gnu"],
)

alias(
    name = "src_binding_release_x86_64_unknown_linux_musl",
    actual = "@rusty_v8_146_4_0_x86_64_unknown_linux_musl_binding//file",
)

alias(
    name = "src_binding_release_aarch64_unknown_linux_musl",
    actual = "@rusty_v8_146_4_0_aarch64_unknown_linux_musl_binding//file",
)

filegroup(
    name = "src_binding_release_x86_64_pc_windows_msvc",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_x86_64_pc_windows_msvc"],
)

filegroup(
    name = "src_binding_release_aarch64_pc_windows_msvc",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_aarch64_pc_windows_msvc"],
)

alias(
    name = "src_binding_release_x86_64_pc_windows_gnullvm",
    # `rusty_v8` does not publish a Windows GNU binding file. The generated
    # binding only describes this V8 release's C++ API surface, so reuse the
    # Linux release binding while the windows-gnullvm archive build is still
    # experimental.
    actual = ":src_binding_release_x86_64_unknown_linux_gnu",
)

alias(
    name = "src_binding_release_aarch64_pc_windows_gnullvm",
    actual = ":src_binding_release_aarch64_unknown_linux_gnu",
)

alias(
    name = "rusty_v8_archive_for_target",
    actual = select({
        "@rules_rs//rs/experimental/platforms/config:aarch64-apple-darwin": ":v8_146_4_0_aarch64_apple_darwin_bazel",
        "@rules_rs//rs/experimental/platforms/config:aarch64-pc-windows-gnullvm": ":v8_146_4_0_aarch64_pc_windows_gnullvm",
        "@rules_rs//rs/experimental/platforms/config:aarch64-pc-windows-msvc": ":v8_146_4_0_aarch64_pc_windows_msvc",
        "@rules_rs//rs/experimental/platforms/config:aarch64-unknown-linux-gnu": ":v8_146_4_0_aarch64_unknown_linux_gnu_bazel",
        ":platform_aarch64_unknown_linux_musl": ":v8_146_4_0_aarch64_unknown_linux_musl_release_base",
        "@rules_rs//rs/experimental/platforms/config:x86_64-apple-darwin": ":v8_146_4_0_x86_64_apple_darwin_bazel",
        "@rules_rs//rs/experimental/platforms/config:x86_64-pc-windows-gnullvm": ":v8_146_4_0_x86_64_pc_windows_gnullvm",
        "@rules_rs//rs/experimental/platforms/config:x86_64-pc-windows-msvc": ":v8_146_4_0_x86_64_pc_windows_msvc",
        "@rules_rs//rs/experimental/platforms/config:x86_64-unknown-linux-gnu": ":v8_146_4_0_x86_64_unknown_linux_gnu_bazel",
        ":platform_x86_64_unknown_linux_musl": ":v8_146_4_0_x86_64_unknown_linux_musl_release",
        "//conditions:default": ":v8_146_4_0_x86_64_unknown_linux_gnu_bazel",
    }),
)

alias(
    name = "rusty_v8_binding_for_target",
    actual = select({
        "@rules_rs//rs/experimental/platforms/config:aarch64-apple-darwin": ":src_binding_release_aarch64_apple_darwin",
        "@rules_rs//rs/experimental/platforms/config:aarch64-pc-windows-gnullvm": ":src_binding_release_aarch64_pc_windows_gnullvm",
        "@rules_rs//rs/experimental/platforms/config:aarch64-pc-windows-msvc": ":src_binding_release_aarch64_pc_windows_msvc",
        "@rules_rs//rs/experimental/platforms/config:aarch64-unknown-linux-gnu": ":src_binding_release_aarch64_unknown_linux_gnu",
        ":platform_aarch64_unknown_linux_musl": ":src_binding_release_aarch64_unknown_linux_musl",
        "@rules_rs//rs/experimental/platforms/config:x86_64-apple-darwin": ":src_binding_release_x86_64_apple_darwin",
        "@rules_rs//rs/experimental/platforms/config:x86_64-pc-windows-gnullvm": ":src_binding_release_x86_64_pc_windows_gnullvm",
        "@rules_rs//rs/experimental/platforms/config:x86_64-pc-windows-msvc": ":src_binding_release_x86_64_pc_windows_msvc",
        "@rules_rs//rs/experimental/platforms/config:x86_64-unknown-linux-gnu": ":src_binding_release_x86_64_unknown_linux_gnu",
        ":platform_x86_64_unknown_linux_musl": ":src_binding_release_x86_64_unknown_linux_musl",
        "//conditions:default": ":src_binding_release_x86_64_unknown_linux_gnu",
    }),
)

V8_COPTS = ["-std=c++20"]

V8_STATIC_LIBRARY_FEATURES = [
    "-symbol_check",
    "-validate-static-library",
]

genrule(
    name = "binding_cc",
    srcs = ["@v8_crate_146_4_0//:binding_cc"],
    outs = ["binding.cc"],
    # Keep this as one physical shell line. In Windows cross CI, this genrule
    # runs on Linux RBE from a Windows Bazel client; multiline command text can
    # carry CRLF into `/bin/bash` as a standalone `$'\r'` command. Use an
    # explicit argv-style join so separators stay visible without shell
    # newlines.
    cmd = " ".join([
        "sed",
        "-e '/#include \"v8\\/src\\/flags\\/flags.h\"/d'",
        "-e 's|\"v8/src/libplatform/default-platform.h\"|\"src/libplatform/default-platform.h\"|'",
        "-e 's|  namespace i = v8::internal;|  (void)usage;|'",
        "-e '/using HelpOptions = i::FlagList::HelpOptions;/d'",
        "-e '/HelpOptions help_options = HelpOptions(HelpOptions::kExit, usage);/d'",
        "-e 's|  i::FlagList::SetFlagsFromCommandLine(argc, argv, true, help_options);|  v8::V8::SetFlagsFromCommandLine(argc, argv, true);|'",
        "$(location @v8_crate_146_4_0//:binding_cc)",
        ">",
        '"$@"',
    ]),
)

copy_file(
    name = "support_h",
    src = "@v8_crate_146_4_0//:support_h",
    out = "support.h",
)

cc_library(
    name = "v8_146_4_0_binding",
    srcs = [":binding_cc"],
    hdrs = [":support_h"],
    copts = V8_COPTS,
    deps = [
        "@v8//:core_lib_icu",
        "@v8//:rusty_v8_internal_headers",
    ],
)

cc_static_library(
    name = "v8_146_4_0_aarch64_apple_darwin_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_aarch64_unknown_linux_gnu_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_aarch64_pc_windows_gnullvm_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_x86_64_apple_darwin_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_x86_64_pc_windows_gnullvm_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_x86_64_unknown_linux_gnu_bazel",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

cc_static_library(
    name = "v8_146_4_0_aarch64_unknown_linux_musl_release_base",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

genrule(
    name = "v8_146_4_0_aarch64_unknown_linux_musl_release",
    srcs = [
        ":v8_146_4_0_aarch64_unknown_linux_musl_release_base",
        "@llvm//runtimes/compiler-rt:clang_rt.builtins.static",
    ],
    tools = [
        "@llvm//tools:llvm-ar",
        "@llvm//tools:llvm-ranlib",
    ],
    outs = ["libv8_146_4_0_aarch64_unknown_linux_musl.a"],
    cmd = """
      cat > "$(@D)/merge.mri" <<'EOF'
create $@
addlib $(location :v8_146_4_0_aarch64_unknown_linux_musl_release_base)
addlib $(location @llvm//runtimes/compiler-rt:clang_rt.builtins.static)
save
end
EOF
      $(location @llvm//tools:llvm-ar) -M < "$(@D)/merge.mri"
      $(location @llvm//tools:llvm-ranlib) "$@"
    """,
)

cc_static_library(
    name = "v8_146_4_0_x86_64_unknown_linux_musl_release",
    deps = [":v8_146_4_0_binding"],
    features = V8_STATIC_LIBRARY_FEATURES,
)

filegroup(
    name = "src_binding_release_aarch64_unknown_linux_musl_release",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_aarch64_unknown_linux_gnu"],
)

filegroup(
    name = "src_binding_release_x86_64_unknown_linux_musl_release",
    srcs = ["@v8_crate_146_4_0//:src_binding_release_x86_64_unknown_linux_gnu"],
)

filegroup(
    name = "rusty_v8_release_pair_x86_64_apple_darwin",
    srcs = [
        ":v8_146_4_0_x86_64_apple_darwin",
        ":src_binding_release_x86_64_apple_darwin",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_aarch64_apple_darwin",
    srcs = [
        ":v8_146_4_0_aarch64_apple_darwin",
        ":src_binding_release_aarch64_apple_darwin",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_x86_64_unknown_linux_gnu",
    srcs = [
        ":v8_146_4_0_x86_64_unknown_linux_gnu",
        ":src_binding_release_x86_64_unknown_linux_gnu",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_aarch64_unknown_linux_gnu",
    srcs = [
        ":v8_146_4_0_aarch64_unknown_linux_gnu",
        ":src_binding_release_aarch64_unknown_linux_gnu",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_x86_64_unknown_linux_musl",
    srcs = [
        ":v8_146_4_0_x86_64_unknown_linux_musl_release",
        ":src_binding_release_x86_64_unknown_linux_musl_release",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_aarch64_unknown_linux_musl",
    srcs = [
        ":v8_146_4_0_aarch64_unknown_linux_musl_release",
        ":src_binding_release_aarch64_unknown_linux_musl_release",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_x86_64_pc_windows_msvc",
    srcs = [
        ":v8_146_4_0_x86_64_pc_windows_msvc",
        ":src_binding_release_x86_64_pc_windows_msvc",
    ],
)

filegroup(
    name = "rusty_v8_release_pair_aarch64_pc_windows_msvc",
    srcs = [
        ":v8_146_4_0_aarch64_pc_windows_msvc",
        ":src_binding_release_aarch64_pc_windows_msvc",
    ],
)
