xref: /aosp_15_r20/build/bazel/product_config/config_settings/BUILD.bazel (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:selects.bzl", "selects")
16load("@soong_injection//product_config:product_variable_constants.bzl", "product_var_constant_info")
17load("//build/bazel/platforms/arch/variants:constants.bzl", "arch_variant_to_constraints")
18load(":soong_config_variable_config_settings.bzl", "soong_config_variable_config_settings")
19
20package(default_visibility = ["//visibility:public"])
21
22# Unlike product config variables below, these are dynamically generated from
23# Soong, since the list of config variables are dynamically defined in
24# Android.bp files and not hardcoded into Soong.
25soong_config_variable_config_settings()
26
27# Add config settings for eng/debuggable. User doesn't exist because it didn't exist in soong.
28config_setting(
29    name = "eng",
30    flag_values = {
31        "//build/bazel/product_config:target_build_variant": "eng",
32    },
33)
34
35config_setting(
36    name = "userdebug",
37    flag_values = {
38        "//build/bazel/product_config:target_build_variant": "userdebug",
39    },
40    visibility = ["//visibility:private"],
41)
42
43selects.config_setting_group(
44    name = "debuggable",
45    match_any = [
46        ":eng",
47        ":userdebug",
48    ],
49)
50
51# Generate one config_setting for each product_variable.
52# The config_setting for <var> can be within a select() to specify an
53# attribute value for the same conditions product_variable.<var>, for most
54# cases, that is when the value of <var> is true. For example,
55#
56# product_variables: {
57#     debuggable: {
58#         cflags: ["debug_flag1", "debug_flag2"],
59#     },
60# }
61#
62# translates into:
63#
64# cflags = select({
65#   "//build/bazel/product_config/config_settings:debuggable": ["debug_flag1", "debug_flag2"],
66#   "//conditions:default": [],
67# }),
68[
69    config_setting(
70        name = product_variable.lower(),
71        flag_values = {
72            "//build/bazel/product_config:" + product_variable.lower(): "true",
73        },
74    )
75    for product_variable, info in product_var_constant_info.items()
76    if product_variable != "Debuggable" and product_variable != "Eng" and info.selectable and info.soongType in [
77        "bool",
78        "*bool",
79    ]
80]
81
82[
83    (
84        # We want a config setting for when the product variable is set to any non-empty string.
85        # To do that, generate an _inverse config setting that's set when the value is an empty
86        # string, and then invert it.
87        config_setting(
88            name = product_variable.lower() + "_inverse",
89            flag_values = {
90                "//build/bazel/product_config:" + product_variable.lower(): "",
91            },
92        ),
93        alias(
94            name = product_variable.lower(),
95            actual = select({
96                product_variable.lower() + "_inverse": "//build/bazel/utils:always_off_config_setting",
97                "//conditions:default": "//build/bazel/utils:always_on_config_setting",
98            }),
99        ),
100    )
101    for product_variable, info in product_var_constant_info.items()
102    if info.selectable and info.soongType in [
103        "*string",
104        "*int",
105    ]
106]
107
108# Caution: do not use these arch-variant product variables directly.
109# If you have a complex combination of product variable and architecture/os/etc,
110# prefer instead to craft an appropriate configuration in your BUILD file.
111# See: https://docs.bazel.build/versions/master/configurable-attributes.html
112# Within bp2build, :safestack-android should be used when an attribute value is
113# conditional on both safestack:true and the os is android.
114#
115# e.g.
116# target: {
117#     android: {
118#         product_variables: {
119#             safestack: {
120#                 cflags: ["-Dsafestack-android"],
121#             },
122#         },
123#     },
124# },
125#
126# would translate to:
127#
128# cflags = select({
129#     "//build/bazel/product_config/config_settings:safestack-android": ["-Dsafestack-android"],
130#     "//conditions:default": [],
131# }),
132[
133    [selects.config_setting_group(
134        name = product_variable.lower() + "-" + arch,
135        match_all = [
136            ":" + product_variable.lower(),
137            archConstraint,
138        ],
139    ) for arch, archConstraint in arch_variant_to_constraints.items()]
140    for product_variable, info in product_var_constant_info.items()
141    if info.selectable and info.archVariant
142]
143