Revision 46b0cddcf8374f11aef9ea21f49f488f432dc988 authored by Quan Nguyen on 19 July 2018, 13:25:24 UTC, committed by Tink Team on 20 July 2018, 15:08:31 UTC
PiperOrigin-RevId: 205230447
GitOrigin-RevId: 1dbfa6a5013fd35f356bd6f795801713de0dce61
1 parent 8a48e96
Raw File
java_single_jar.bzl
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

""" Definition of java_single_jar. """

def _check_non_empty(value, name):
    if not value:
        fail("%s must be non-empty" % name)

def _java_single_jar(ctx):
    _check_non_empty(ctx.attr.root_packages, "root_packages")

    inputs = depset()
    source_jars = depset()
    for dep in ctx.attr.deps:
        inputs = depset(transitive = [inputs, dep.java.transitive_runtime_deps])
        source_jars = depset(transitive = [source_jars, dep.java.source_jars])
        for td in dep.java.transitive_runtime_deps:
            if hasattr(td, "java"):
                source_jars = depset(transitive = [source_jars, td.java.source_jars])

    compress = ""
    if ctx.attr.compress == "preserve":
        compress = "--dont_change_compression"
    elif ctx.attr.compress == "yes":
        compress = "--compression"
    elif ctx.attr.compress == "no":
        pass
    else:
        fail("\"compress\" attribute (%s) must be: yes, no, preserve." % ctx.attr.compress)

    if ctx.attr.source_jar:
        inputs = source_jars
        compress = ""

    args = ctx.actions.args()
    args.add("--sources")
    args.add(inputs)
    args.use_param_file(
        "@%s",
        use_always = True,
    )
    args.set_param_file_format("multiline")

    include_prefixes = " ".join([x.replace(".", "/") for x in ctx.attr.root_packages])

    ctx.actions.run(
        inputs = inputs,
        outputs = [ctx.outputs.jar],
        arguments = [
                        args,
                        "--output",
                        ctx.outputs.jar.path,
                        "--include_prefixes",
                        include_prefixes,
                        "--normalize",
                    ] +
                    # Deal with limitation of singlejar flags: tool's default behavior is
                    # "no", but you get that behavior only by absence of compression flags.
                    ([] if not compress else [compress]),
        progress_message = "Merging into %s" % ctx.outputs.jar.short_path,
        mnemonic = "JavaSingleJar",
        executable = ctx.executable._singlejar,
    )

java_single_jar = rule(
    attrs = {
        "deps": attr.label_list(providers = ["java"]),
        "_singlejar": attr.label(
            default = Label("@bazel_tools//tools/jdk:singlejar"),
            cfg = "host",
            allow_single_file = True,
            executable = True,
        ),
        "source_jar": attr.bool(default = False),
        "compress": attr.string(default = "preserve"),
        "root_packages": attr.string_list(),
    },
    outputs = {
        "jar": "%{name}.jar",
    },
    implementation = _java_single_jar,
)
"""
Collects Java dependencies and jar files into a single jar

Args:
  deps: The Java targets (including java_import and java_library) to collect
      transitive dependencies from. Both compile-time dependencies (deps,
      exports) and runtime dependencies (runtime_deps) are collected.
      Resources are also collected. Native cc_library or java_wrap_cc
      dependencies are not.
  compress: Whether to always deflate ("yes"), always store ("no"), or pass
      through unmodified ("preserve"). The default is "preserve", and is the
      most efficient option -- no extra work is done to inflate or deflate.
  source_jar: Whether to combine the source jars of input to create a single
      output source jar.
  root_packages: Java packages to include in generated jar.

Outputs:
  {name}.jar: A single jar containing all of the input.
"""
back to top