• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc.  All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7"""Support for compiling protoc generated Java code."""
8
9load("@rules_java//java/private:proto_support.bzl", "compile", "merge")  # buildifier: disable=bzl-visibility
10
11# The provider is used to collect source and runtime jars in the `proto_library` dependency graph.
12JavaProtoAspectInfo = provider("JavaProtoAspectInfo", fields = ["jars"])
13
14java_info_merge_for_protos = merge
15
16def java_compile_for_protos(ctx, output_jar_suffix, source_jar = None, deps = [], exports = [], injecting_rule_kind = "java_proto_library"):
17    """Compiles Java source jar returned by proto compiler.
18
19    Use this call for java_xxx_proto_library. It uses java_common.compile with
20    some checks disabled (via javacopts) and jspecify disabled, so that the
21    generated code passes.
22
23    It also takes care that input source jar is not repackaged with a different
24    name.
25
26    When `source_jar` is `None`, the function only merges `deps` and `exports`.
27
28    Args:
29      ctx: (RuleContext) Used to call `java_common.compile`
30      output_jar_suffix: (str) How to name the output jar. For example: `-speed.jar`.
31      source_jar: (File) Input source jar (may be `None`).
32      deps: (list[JavaInfo]) `deps` of the `proto_library`.
33      exports: (list[JavaInfo]) `exports` of the `proto_library`.
34      injecting_rule_kind: (str) Rule kind requesting the compilation.
35        It's embedded into META-INF of the produced runtime jar, for debugging.
36    Returns:
37      ((JavaInfo, list[File])) JavaInfo of this target and list containing source
38      and runtime jar, when they are created.
39    """
40    if source_jar != None:
41        path, sep, filename = ctx.label.name.rpartition("/")
42        output_jar = ctx.actions.declare_file(path + sep + "lib" + filename + output_jar_suffix)
43        java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java
44        java_info = compile(
45            ctx = ctx,
46            output = output_jar,
47            java_toolchain = java_toolchain,
48            source_jars = [source_jar],
49            deps = deps,
50            exports = exports,
51            output_source_jar = source_jar,
52            injecting_rule_kind = injecting_rule_kind,
53            javac_opts = java_toolchain._compatible_javacopts.get("proto", depset()),
54            enable_jspecify = False,
55            include_compilation_info = False,
56        )
57        jars = [source_jar, output_jar]
58    else:
59        # If there are no proto sources just pass along the compilation dependencies.
60        java_info = merge(deps + exports, merge_java_outputs = False, merge_source_jars = False)
61        jars = []
62    return java_info, jars
63