xref: /aosp_15_r20/build/bazel/rules/aconfig/aconfig_declarations.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
15load("@bazel_skylib//lib:paths.bzl", "paths")
16load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
17load(":aconfig_value_set.bzl", "AconfigValueSetInfo")
18load(":aconfig_value_sets.bzl", "AconfigValueSetsInfo")
19
20AconfigDeclarationsInfo = provider(fields = [
21    "package",
22    "intermediate_path",
23])
24
25def _aconfig_declarations_rule_impl(ctx):
26    value_files = []
27    transitive = []
28    for value_set in ctx.attr._value_sets[AconfigValueSetsInfo].value_sets:
29        value_set_info = value_set[AconfigValueSetInfo].available_packages.get(ctx.attr.package)
30        if value_set_info != None:
31            value_files.extend(value_set_info.to_list())
32            transitive.append(value_set_info)
33
34    output = ctx.actions.declare_file(paths.join(ctx.label.name, "intermediate.pb"))
35
36    args = ctx.actions.args()
37    args.add("create-cache")
38    args.add_all(["--package", ctx.attr.package])
39    for src in ctx.files.srcs:
40        args.add_all(["--declarations", src.path])
41    for value in value_files:
42        args.add_all(["--values", value])
43    args.add_all(["--default-permission", ctx.attr._default_permission[BuildSettingInfo].value])
44    args.add_all(["--cache", output.path])
45
46    inputs = depset(
47        direct = ctx.files.srcs,
48        transitive = transitive,
49    )
50
51    ctx.actions.run(
52        inputs = inputs,
53        executable = ctx.executable._aconfig,
54        outputs = [output],
55        arguments = [args],
56        mnemonic = "AconfigCreateCache",
57    )
58
59    return [
60        DefaultInfo(files = depset(direct = [output])),
61        AconfigDeclarationsInfo(
62            package = ctx.attr.package,
63            intermediate_path = output,
64        ),
65    ]
66
67aconfig_declarations = rule(
68    implementation = _aconfig_declarations_rule_impl,
69    attrs = {
70        "srcs": attr.label_list(
71            mandatory = True,
72            allow_files = True,
73        ),
74        "package": attr.string(mandatory = True),
75        "_aconfig": attr.label(
76            allow_single_file = True,
77            executable = True,
78            cfg = "exec",
79            default = Label("//build/make/tools/aconfig:aconfig"),
80        ),
81        "_value_sets": attr.label(
82            default = "//build/bazel/product_config:release_aconfig_value_sets",
83            providers = [AconfigValueSetsInfo],
84        ),
85        "_release_version": attr.label(
86            default = "//build/bazel/product_config:release_version",
87        ),
88        "_default_permission": attr.label(
89            default = "//build/bazel/product_config:release_aconfig_flag_default_permission",
90        ),
91    },
92    provides = [AconfigDeclarationsInfo],
93)
94