xref: /aosp_15_r20/external/jazzer-api/bazel/jar.bzl (revision 33edd6723662ea34453766bfdca85dbfdd5342b8)
1# Copyright 2022 Code Intelligence GmbH
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
15def _strip_jar(ctx):
16    out_jar = ctx.outputs.out
17    if out_jar == None:
18        out_jar = ctx.actions.declare_file(ctx.attr.name + ".jar")
19
20    args = ctx.actions.args()
21    args.add(ctx.file.jar)
22    args.add(out_jar)
23    args.add_all(ctx.attr.paths_to_strip)
24    args.add_all(ctx.attr.paths_to_keep, format_each = "+%s")
25    ctx.actions.run(
26        outputs = [out_jar],
27        inputs = [ctx.file.jar],
28        arguments = [args],
29        executable = ctx.executable._jar_stripper,
30    )
31
32    return [
33        DefaultInfo(
34            files = depset([out_jar]),
35        ),
36        coverage_common.instrumented_files_info(
37            ctx,
38            dependency_attributes = ["jar"],
39        ),
40    ]
41
42strip_jar = rule(
43    implementation = _strip_jar,
44    attrs = {
45        "out": attr.output(),
46        "jar": attr.label(
47            mandatory = True,
48            allow_single_file = [".jar"],
49        ),
50        "paths_to_strip": attr.string_list(),
51        "paths_to_keep": attr.string_list(),
52        "_jar_stripper": attr.label(
53            default = "//bazel/tools/java:JarStripper",
54            cfg = "exec",
55            executable = True,
56        ),
57    },
58)
59