xref: /aosp_15_r20/build/bazel/rules/java/java_sysprop_library.bzl (revision 7594170e27e0732bc44b93d1440d87a54b6ffe7c)
1# Copyright (C) 2023 The Android Open Source Project
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"""
16Rules for generating java code from sysprop_library modules
17"""
18
19load("//build/bazel/rules/sysprop:sysprop_library.bzl", "SyspropGenInfo")
20load(":sdk_transition.bzl", "sdk_transition")
21
22# TODO: b/301122615 - Implement stubs rule and macro for both
23
24_java_sysprop_library_attrs = {
25    "dep": attr.label(mandatory = True),
26    "_sdk_version": attr.string(default = "core_current"),
27    # TODO: TBD - Add other possible stub libs
28    "_platform_stubs": attr.label(
29        default = "//system/tools/sysprop:sysprop-library-stub-platform",
30    ),
31    "_sysprop_java": attr.label(
32        default = "//system/tools/sysprop:sysprop_java",
33        executable = True,
34        cfg = "exec",
35    ),
36    "_soong_zip": attr.label(
37        default = "//build/soong/zip/cmd:soong_zip",
38        executable = True,
39        cfg = "exec",
40    ),
41    "_allowlist_function_transition": attr.label(
42        default = "@bazel_tools//tools/allowlists/function_transition_allowlist",
43    ),
44}
45
46def _gen_java(
47        ctx,
48        srcs,
49        scope):
50    outputs = []
51    all_srcs = []
52    for src in srcs:
53        all_srcs.extend(src.files.to_list())
54
55    for src_file in all_srcs:
56        output_subpath = src_file.short_path.replace(
57            ctx.label.package + "/",
58            "",
59            1,
60        )
61        output_srcjar_file = ctx.actions.declare_file(
62            "%s.srcjar" % output_subpath,
63        )
64        output_tmp_dir_path = "%s.tmp" % output_srcjar_file.path
65        ctx.actions.run_shell(
66            tools = [
67                ctx.executable._sysprop_java,
68                ctx.executable._soong_zip,
69            ],
70            inputs = [src_file],
71            outputs = [output_srcjar_file],
72            command = """
73            rm -rf {dir} && mkdir -p {dir} &&
74            {sysprop_java} --scope {scope} --java-output-dir {dir} {input} &&
75            {soong_zip} -jar -o {output_srcjar} -C {dir} -D {dir}
76            """.format(
77                dir = output_tmp_dir_path,
78                sysprop_java = ctx.executable._sysprop_java.path,
79                scope = scope,
80                input = src_file.path,
81                soong_zip = ctx.executable._soong_zip.path,
82                output_srcjar = output_srcjar_file.path,
83            ),
84            mnemonic = "SyspropJava",
85            progress_message = "Generating srcjar from {}".format(
86                src_file.basename,
87            ),
88        )
89        outputs.append(output_srcjar_file)
90    return outputs
91
92def _compile_java(
93        name,
94        ctx,
95        srcs,
96        deps):
97    out_jar = ctx.actions.declare_file("%s.jar" % name)
98    java_info = java_common.compile(
99        ctx,
100        source_jars = srcs,
101        deps = deps,
102        output = out_jar,
103        java_toolchain = ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].java,
104    )
105    return java_info, out_jar
106
107def _java_sysprop_library_impl(ctx):
108    gen_srcjars = _gen_java(
109        ctx,
110        ctx.attr.dep[SyspropGenInfo].srcs,
111        "internal",  # TODO: b/302677541 - Determine based on props
112    )
113
114    java_info, out_jar = _compile_java(
115        ctx.attr.name,
116        ctx,
117        gen_srcjars,
118        # TODO: b/302677539 - Determine based on props
119        [ctx.attr._platform_stubs[JavaInfo]],
120    )
121
122    return [
123        java_info,
124        DefaultInfo(
125            files = depset([out_jar]),
126            runfiles = ctx.runfiles(
127                transitive_files = depset(
128                    transitive = [java_info.transitive_runtime_jars],
129                ),
130            ),
131        ),
132        OutputGroupInfo(default = depset()),
133    ]
134
135java_sysprop_library = rule(
136    implementation = _java_sysprop_library_impl,
137    cfg = sdk_transition,
138    doc = """
139    Generates java sources from the sources in the supplied sysprop_library
140    target and compiles them into a jar.
141    """,
142    attrs = _java_sysprop_library_attrs,
143    toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
144    fragments = ["java"],
145    provides = [JavaInfo],
146)
147