load("@rules_proto//proto:defs.bzl", "proto_library")
load("//:java_grpc_library.bzl", "java_grpc_library")

java_library(
    name = "alts_internal",
    srcs = glob([
        "src/main/java/io/grpc/alts/internal/*.java",
    ]),
    deps = [
        ":handshaker_java_grpc",
        ":handshaker_java_proto",
        "//api",
        "//core:internal",
        "//grpclb",
        "//netty",
        "//stub",
        "@com_google_code_findbugs_jsr305//jar",
        "@com_google_guava_guava//jar",
        "@com_google_j2objc_j2objc_annotations//jar",
        "@com_google_protobuf//:protobuf_java",
        "@com_google_protobuf//:protobuf_java_util",
        "@io_netty_netty_buffer//jar",
        "@io_netty_netty_codec//jar",
        "@io_netty_netty_common//jar",
        "@io_netty_netty_handler//jar",
        "@io_netty_netty_transport//jar",
    ],
)

java_library(
    name = "alts",
    srcs = glob([
        "src/main/java/io/grpc/alts/*.java",
    ]),
    visibility = ["//visibility:public"],
    deps = [
        ":alts_internal",
        ":handshaker_java_proto",
        ":handshaker_java_grpc",
        "//api",
        "//auth",
        "//core:internal",
        "//netty",
        "@com_google_auth_google_auth_library_oauth2_http//jar",
        "@com_google_code_findbugs_jsr305//jar",
        "@com_google_guava_guava//jar",
        "@com_google_j2objc_j2objc_annotations//jar",
        "@io_netty_netty_common//jar",
        "@io_netty_netty_handler//jar",
        "@io_netty_netty_transport//jar",
    ],
)

# bazel only accepts proto import with absolute path.
genrule(
    name = "protobuf_imports",
    srcs = glob(["src/main/proto/grpc/gcp/*.proto"]),
    outs = [
        "protobuf_out/grpc/gcp/altscontext.proto",
        "protobuf_out/grpc/gcp/handshaker.proto",
        "protobuf_out/grpc/gcp/transport_security_common.proto",
    ],
    cmd = "for fname in $(SRCS); do " +
          "sed 's,import \",import \"alts/protobuf_out/,g' $$fname > " +
          "$(@D)/protobuf_out/grpc/gcp/$$(basename $$fname); done",
)

proto_library(
    name = "handshaker_proto",
    srcs = [
        "protobuf_out/grpc/gcp/altscontext.proto",
        "protobuf_out/grpc/gcp/handshaker.proto",
        "protobuf_out/grpc/gcp/transport_security_common.proto",
    ],
)

java_proto_library(
    name = "handshaker_java_proto",
    deps = [":handshaker_proto"],
)

java_grpc_library(
    name = "handshaker_java_grpc",
    srcs = [":handshaker_proto"],
    deps = [":handshaker_java_proto"],
)
