• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2024 The Dagger Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Skylark rules for jarjar. See https://github.com/pantsbuild/jarjar
16"""
17
18load("@rules_java//java:defs.bzl", "java_common")
19
20def _jarjar_library(ctx):
21    ctx.actions.write(
22        output = ctx.outputs._rules_file,
23        content = "\n".join(ctx.attr.rules),
24    )
25
26    jar_files = depset(transitive = [jar.files for jar in ctx.attr.jars]).to_list()
27
28    command = """
29        export JAVA_HOME="{java_home}"
30        export MERGE_META_INF_FILES="{merge_meta_inf_files}"
31        export JARJAR="{jarjar}"
32        export RULES_FILE="{rules_file}"
33        export OUTFILE="{outfile}"
34        "{jarjar_runner}" {jars}
35    """.format(
36        java_home = str(ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home),
37        merge_meta_inf_files = " ".join(ctx.attr.merge_meta_inf_files),
38        jarjar = ctx.executable._jarjar.path,
39        rules_file = ctx.outputs._rules_file.path,
40        outfile = ctx.outputs.jar.path,
41        jarjar_runner = ctx.executable._jarjar_runner.path,
42        jars = " ".join([jar.path for jar in jar_files]),
43    )
44
45    ctx.actions.run_shell(
46        command = command,
47        inputs = [ctx.outputs._rules_file] + jar_files + ctx.files._jdk,
48        outputs = [ctx.outputs.jar],
49        tools = [ctx.executable._jarjar, ctx.executable._jarjar_runner],
50    )
51
52_jarjar_library_attrs = {
53    "rules": attr.string_list(),
54    "jars": attr.label_list(
55        allow_files = [".jar"],
56    ),
57    "merge_meta_inf_files": attr.string_list(
58        allow_empty = True,
59        default = [],
60        mandatory = False,
61        doc = """A list of regular expressions that match files relative to the
62        META-INF directory that will be merged into the output jar, in addition
63        to files in META-INF/services. To add all files in META-INF/foo, for
64        example, use "foo/.*".""",
65    ),
66}
67
68_jarjar_library_attrs.update({
69    "_jarjar": attr.label(
70        default = Label("//tools/jarjar"),
71        executable = True,
72        cfg = "exec",
73    ),
74    "_jarjar_runner": attr.label(
75        default = Label("//tools/jarjar:jarjar_runner"),
76        executable = True,
77        cfg = "exec",
78    ),
79    "_jdk": attr.label(
80        default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
81        providers = [java_common.JavaRuntimeInfo],
82    ),
83})
84
85jarjar_library = rule(
86    attrs = _jarjar_library_attrs,
87    outputs = {
88        "jar": "%{name}.jar",
89        "_rules_file": "%{name}.jarjar_rules",
90    },
91    implementation = _jarjar_library,
92)
93