question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Provide a java_grpc_library() Skylark extension

See original GitHub issue

Bazel users can now build protos using the built-in proto_library and {cc,java,javalite}_proto_library rules.

These rules do not include gRPC support intentionally; this FR is about providing a Skylark extension to allow users to build gRPC bindings.

@ejona86

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
ejona86commented, Feb 28, 2017

Main hold-up is figuring out all the dependencies (and set up Bazel for the first time 😄). This is a modified dump of what I’ve got.

def _gensource_impl(ctx):
  if len(ctx.attr.srcs) > 1:
    fail("Only one src value supported", "srcs")
  for s in ctx.attr.srcs:
    if s.label.package != ctx.label.package:
      print(("in srcs attribute of {0}: Proto source with label {1} should be in "
             + "same package as consuming rule").format(ctx.label, s.label))
  # Use .jar since .srcjar makes protoc think output will be a directory
  srcdotjar = ctx.new_file(ctx.label.name + "_src.jar")

  srcs = [f for dep in ctx.attr.srcs for f in dep.proto.direct_sources]
  includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports]
  flavor = ctx.attr.flavor
  if flavor == "normal":
    flavor = ""
  ctx.action(
      inputs = [ctx.executable._java_plugin] + srcs + includes,
      outputs = [srcdotjar],
      executable = ctx.executable._protoc,
      arguments = [
          "--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path,
          "--grpc-java_out={0},enable_deprecated={1}:{2}"
            .format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path)]
          + ["-I{0}={1}".format(include.short_path, include.path) for include in includes]
          + [src.short_path for src in srcs])
  ctx.action(
      command = "cp $1 $2",
      inputs = [srcdotjar],
      outputs = [ctx.outputs.srcjar],
      arguments = [srcdotjar.path, ctx.outputs.srcjar.path])

_gensource = rule(
    attrs = {
        "srcs": attr.label_list(
            mandatory = True,
            non_empty = True,
            providers = ["proto"],
        ),
        "flavor": attr.string(
            values = [
                "normal",
                "lite",
            ],
            default = "normal",
        ),
        "enable_deprecated": attr.bool(
            default = False,
        ),
        "_protoc": attr.label(
            default = Label("@com_google_protobuf//:protoc"),
            executable = True,
            cfg = "host",
        ),
        "_java_plugin": attr.label(
            default = Label("@io_grpc_java//:TODO_plugin"),
            executable = True,
            cfg = "host",
        ),
    },
    outputs = {
        "srcjar": "%{name}.srcjar",
    },
    implementation = _gensource_impl,
)

def java_grpc_library(name, srcs, deps, flavor=None,
                      enable_deprecated=None, visibility=None,
                      **kwargs):
  """Generates and compiles gRPC Java sources for services defined in a proto
  file. This rule is compatible with java_proto_library and java_lite_proto_library.

  Do note that this rule only scans through the proto file for RPC services. It
  does not generate Java classes for proto messages. You will need a separate
  java_proto_library or java_lite_proto_library rule.

  Args:
    name: (str) A unique name for this rule. Required.
    srcs: (list) a single proto_library target that contains the schema of the
        service. Required.
    deps: (list) a single java_proto_library target for the proto_library in
        srcs.  Required.
    flavor: (str) "normal" (default) for normal proto runtime. "lite"
        for the lite runtime.
    visibility: (list) the visibility list
    **kwargs: Passed through to generated targets
  """
  if flavor == None:
    flavor = "normal"

  if len(deps) > 1:
    print("Multiple values in 'deps' is deprecated in " + name)

  gensource_name = name + "__do_not_reference__srcjar"
  _gensource(
      name = gensource_name,
      srcs = srcs,
      flavor = flavor,
      enable_deprecated = enable_deprecated,
      visibility = ["//visibility:private"],
      **kwargs
  )

  added_deps = [
      "@io_grpc_java//:TODO_core",
      "@io_grpc_java//:TODO_stub",
      "@TODO_guava//:util_concurrent",
  ]
  if flavor == "normal":
    added_deps += ["@com_google_protobuf_java//:java_toolchain"]
  elif flavor == "lite":
    added_deps += ["@com_google_protobuf_java_lite_TODO//:java_lite_toolchain"]
  else:
    fail("Unknown flavor type", "flavor")

  native.java_library(
      name = name,
      srcs = [gensource_name],
      visibility = visibility,
      deps = [
          "@TODO//:jsr305_annotations",
      ] + deps + added_deps,
      **kwargs
  )
0reactions
ejona86commented, Jun 22, 2017

I created #3125 for adding Bazel to CI

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basics tutorial | Java - gRPC
This tutorial provides a basic Java programmer's introduction to working with gRPC. By walking through this example you'll learn how to:.
Read more >
Getting Started with gRPC - Quarkus
Getting Started with gRPC · Solution · Configuring your project · Implementing a gRPC service · Consuming a gRPC service · Packaging the...
Read more >
Protocol Buffers in Bazel
Bazel currently provides rules for Java, JavaLite and C++. ... We will work with the gRPC team to create Skylark extensions to do...
Read more >
bazel attr
How can I avoid providing the build options at the command line ... deps attr on lang_{proto|grpc}_{compile|library} with the protos 2022. java at...
Read more >
Introduction to gRPC - Coherence - Oracle Help Center
Coherence gRPC for Java allows Java applications to access Coherence clustered services, including data, data events, and data processing from outside the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found