xref: /aosp_15_r20/external/sdk-platform-java/gax-java/gax_java.bzl (revision 882aa7c72c3cd3b66e72a261bdd69b93f7de7670)
1# Copyright 2019 Google LLC
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google LLC nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29def java_tests(name, srcs, runtime_deps, size):
30    classNames = []
31    for src in srcs:
32        # convert .java file path to fully qualified class name
33        className = src[(src.index("/com/") + 1):-5].replace("/", ".")
34        classNames.append(className)
35        native.java_test(
36            name = className,
37            test_class = className,
38            runtime_deps = runtime_deps,
39            size = size,
40        )
41    if classNames:
42        native.test_suite(
43            name = name,
44            tests = classNames,
45        )
46
47def google_java_format(name, srcs, formatter):
48    native.genrule(
49        name = name,
50        outs = ["%s.sh" % name],
51        srcs = srcs,
52        # TODO: this may fail if list of files is too long (exceeds max command line limit in shell).
53        #       Split the command into multiple executions if this ever fails (good enough for now)
54        cmd = "echo ' $(location %s) --replace $(SRCS)' > $@" % formatter,
55        executable = True,
56        tools = [formatter],
57        local = 1,
58    )
59
60def _google_java_format_verification_impl(ctx):
61    src_files = [src.path for src in ctx.files.srcs]
62    output_file = ctx.outputs.output_file
63    formatter = ctx.executable.formatter
64
65    ctx.actions.run_shell(
66        inputs = ctx.files.srcs,
67        arguments = ["--dry-run", "--set-exit-if-changed"] + src_files,
68        tools = [formatter],
69        command = "%s $@ > %s" % (formatter.path, output_file.path),
70        outputs = [output_file],
71        progress_message =
72            "If this target fails check the list of files that must be formatted in %s" % output_file.path,
73    )
74
75google_java_format_verification = rule(
76    attrs = {
77        "srcs": attr.label_list(allow_files = True),
78        "formatter": attr.label(
79            executable = True,
80            cfg = "host",
81        ),
82    },
83    outputs = {"output_file": "%{name}.txt"},
84    implementation = _google_java_format_verification_impl,
85)
86