• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""Java options and protobuf-specific java build rules with those options."""
2
3load("@rules_java//java:defs.bzl", "java_library")
4load("@rules_jvm_external//:defs.bzl", "java_export")
5load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
6load("//java/osgi:osgi.bzl", "osgi_java_library")
7
8JAVA_RELEASE_OPTS = [
9    "-source 8",
10    "-target 8",
11    "-Xep:Java8ApiChecker:ERROR",
12]
13
14BUNDLE_DOC_URL = "https://developers.google.com/protocol-buffers/"
15BUNDLE_LICENSE = "https://opensource.org/licenses/BSD-3-Clause"
16
17def protobuf_java_export(**kwargs):
18    java_export(
19        javacopts = JAVA_RELEASE_OPTS,
20        # https://github.com/bazelbuild/rules_jvm_external/issues/1245
21        javadocopts = [
22            "-notimestamp",
23            "-use",
24            "-quiet",
25            "-Xdoclint:-missing",
26            "-encoding",
27            "UTF8",
28        ],
29        **kwargs
30    )
31
32def protobuf_java_library(**kwargs):
33    java_library(
34        **kwargs
35    )
36
37def protobuf_versioned_java_library(
38        automatic_module_name,
39        bundle_description,
40        bundle_name,
41        bundle_symbolic_name,
42        bundle_additional_imports = [],
43        bundle_additional_exports = [],
44        **kwargs):
45    """Extends `java_library` to add OSGi headers to the MANIFEST.MF using bndlib
46
47    This macro should be usable as a drop-in replacement for java_library.
48
49    The additional arguments are given the bndlib tool to generate an OSGi-compliant manifest file.
50    See [bnd documentation](https://bnd.bndtools.org/chapters/110-introduction.html)
51
52    Takes all the args that are standard for a java_library target plus the following.
53    Args:
54        bundle_description: (required) The Bundle-Description header defines a short
55            description of this bundle.
56        automatic_module_name: (required) The Automatic-Module-Name header that represents
57            the name of the module when this bundle is used as an automatic
58            module.
59        bundle_name: (required) The Bundle-Name header defines a readable name for this
60            bundle. This should be a short, human-readable name that can
61            contain spaces.
62        bundle_symbolic_name: (required) The Bundle-SymbolicName header specifies a
63            non-localizable name for this bundle. The bundle symbolic name
64            together with a version must identify a unique bundle though it can
65            be installed multiple times in a framework. The bundle symbolic
66            name should be based on the reverse domain name convention.
67        bundle_additional_exports: The Export-Package header contains a
68            declaration of exported packages. These are additional export
69            package statements to be added before the default wildcard export
70            "*;version={$Bundle-Version}".
71        bundle_additional_imports: The Import-Package header declares the
72            imported packages for this bundle. These are additional import
73            package statements to be added before the default wildcard import
74            "*".
75        **kwargs: Additional key-word arguments that are passed to the internal
76            java_library target.
77    """
78    osgi_java_library(
79        javacopts = JAVA_RELEASE_OPTS,
80        automatic_module_name = automatic_module_name,
81        bundle_doc_url = BUNDLE_DOC_URL,
82        bundle_license = BUNDLE_LICENSE,
83        bundle_version = PROTOBUF_JAVA_VERSION,
84        bundle_description = bundle_description,
85        bundle_name = bundle_name,
86        bundle_symbolic_name = bundle_symbolic_name,
87        bundle_additional_exports = bundle_additional_exports,
88        bundle_additional_imports = bundle_additional_imports + ["sun.misc;resolution:=optional"],
89        **kwargs
90    )
91