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"""Bazel rules for platform_compat_config"""
16
17PlatformCompatConfigInfo = provider(
18    "platform_compat_config extracts and installs compat_config",
19    fields = {
20        "config_file": "File containing config for embedding on the device",
21        "metadata_file": "File containing metadata about merged config",
22    },
23)
24
25def _platform_compat_config_impl(ctx):
26    config_file = ctx.actions.declare_file(ctx.attr.name + ".xml")
27    metadata_file = ctx.actions.declare_file(ctx.attr.name + "_meta.xml")
28
29    input_jar_files = ctx.attr.src[JavaInfo].compile_jars.to_list()
30
31    args = ctx.actions.args()
32    args.add_all(["--jar"] + input_jar_files)
33    args.add_all(["--device-config", config_file])
34    args.add_all(["--merged-config", metadata_file])
35
36    ctx.actions.run(
37        outputs = [config_file, metadata_file],
38        inputs = input_jar_files,
39        executable = ctx.executable._tool_name,
40        arguments = [args],
41        mnemonic = "ExtractCompatConfig",
42    )
43
44    return PlatformCompatConfigInfo(
45        config_file = config_file,
46        metadata_file = metadata_file,
47    )
48
49platform_compat_config = rule(
50    implementation = _platform_compat_config_impl,
51    attrs = {
52        "src": attr.label(
53            mandatory = True,
54            providers = [JavaInfo],
55        ),
56        "_tool_name": attr.label(
57            default = "//tools/platform-compat/build:process-compat-config",
58            executable = True,
59            cfg = "exec",
60        ),
61    },
62    provides = [PlatformCompatConfigInfo],
63)
64