xref: /aosp_15_r20/build/soong/docs/selects.md (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1# Select statements
2
3## Introduction
4
5Soong currently has the arch, target, product_variables, and soong config variable properties that all support changing the values of soong properties based on some condition. These are confusing for users, and particularly the soong config variable properties require a lot of boilerplate that is annoying to write.
6
7In addition, these properties are all implemented using reflection on property structs, and can combine in ways that the original authors did not expect. For example, soong config variables can be combined with arch/target by saying that they operate on "arch.arm.enabled" instead of just "enabled". But product variables do not have the same abilities.
8
9The goal here is to combine all these different configuration mechanisms into one, to reduce complexity and boilerplate both in Android.bp files and in soong code.
10
11## Usage
12
13The soong select statements take their name and inspiration from [bazel select statements](https://bazel.build/docs/configurable-attributes).
14
15### Syntax
16
17#### Basic
18
19The basic syntax for select statements looks like:
20
21```
22my_module_type {
23  name: "my_module",
24  some_string_property: select(arch(), {
25    "arm": "foo",
26    "x86": "bar",
27    default: "baz",
28  }),
29}
30```
31
32That is, `select(` followed by a variable function, then a map of values of the variable to values to set the property to.
33
34Arguments can be passed to the "functions" that look up axes:
35
36```
37select(soong_config_variable("my_namespace", "my_variable"), {
38  "value1": "foo",
39  default: "bar",
40})
41```
42
43
44The list of functions that can currently be selected on:
45 - `arch()`
46 - `os()`
47 - `soong_config_variable(namespace, variable)`
48 - `release_flag(flag)`
49
50The functions are [defined here](https://cs.android.com/android/platform/superproject/main/+/main:build/soong/android/module.go;l=2144;drc=3f01580c04bfe37c920e247015cce93cff2451c0), and it should be easy to add more.
51
52#### Multivariable
53
54To handle multivariable selects, multiple axes can be specified within parenthesis, to look like tuple destructuring. All of the variables being selected must match the corresponding value from the branch in order for the branch to be chosen.
55
56```
57select((arch(), os()), {
58  ("arm", "linux"): "foo",
59  (default, "windows"): "bar",
60  (default, default): "baz",
61})
62```
63
64#### Unset
65
66You can have unset branches of selects using the "unset" keyword, which will act as if the property was not assigned to. This is only really useful if you’re using defaults modules.
67
68```
69cc_binary {
70  name: "my_binary",
71  enabled: select(os(), {
72    "darwin": false,
73    default: unset,
74  }),
75}
76```
77
78#### Appending
79
80You can append select statements to both scalar values and other select statements:
81
82```
83my_module_type {
84  name: "my_module",
85  // string_property will be set to something like penguin-four, apple-two, etc.
86  string_property: select(os(), {
87    "linux_glibc": "penguin",
88    "darwin": "apple",
89    default: "unknown",
90  }) + "-" + select(soong_config_variable("ANDROID", "favorite_vehicle"), {
91    "car": "four",
92    "tricycle": "three",
93    "bike": "two",
94     default: "unknown",
95  })
96}
97```
98
99
100You can also append a select with a value with another select that may not have a value, because some of its branches are "unset". If an unset branch was selected it will not append anything to the other select.
101
102#### Binding the selected value to a Blueprint variable and the "any" keyword
103
104In case you want to allow a selected value to have an unbounded number of possible values, you can bind its value to a blueprint variable and use it within the expression for that select branch.
105
106```
107my_module_type {
108  name: "my_module",
109  my_string_property: select(soong_config_variable("my_namespace", "my_variable"), {
110    "some_value": "baz",
111    any @ my_var: "foo" + my_var,
112    default: "bar",
113  }),
114}
115```
116
117The syntax is `any @ my_variable_name: <expression using my_variable_name>`. `any` is currently the only pattern that can be bound to a variable, but we may add more in the future. `any` is equivalent to `default` except it will not match undefined select conditions.
118
119#### Errors
120
121If a select statement does not have a "default" branch, and none of the other branches match the variable being selected on, it’s a compile-time error. This may be useful for enforcing a variable is 1 of only a few values.
122
123```
124# in product config:
125$(call soong_config_set,ANDROID,my_variable,foo)
126
127// in an Android.bp:
128my_module_type {
129  name: "my_module",
130  // Will error out with: soong_config_variable("ANDROID", "my_variable") had value "foo", which was not handled by the select
131  enabled: select(soong_config_variable("ANDROID", "my_variable"), {
132    "bar": true,
133    "baz": false,
134  }),
135}
136```
137
138### Changes to property structs to support selects
139
140Currently, the way configurable properties work is that there is secretly another property struct that has the `target`, `arch`, etc. properties, and then when the arch mutator (or other relevant mutator) runs, it copies the values of these properties onto the regular property structs. There’s nothing stopping you from accessing your properties from a mutator that runs before the one that updates the properties based on configurable values. This is a potential source of bugs, and we want to make sure that select statements don’t have the same pitfall. For that reason, you have to read property’s values through a getter which can do this check. This requires changing the code on a property-by-property basis to support selects.
141
142To make a property support selects, it must be of type [proptools.Configurable[T]](https://cs.android.com/android/platform/superproject/main/+/main:build/blueprint/proptools/configurable.go;l=341;drc=a52b058cccd2caa778d0f97077adcd4ef7ffb68a). T is the old type of the property. Currently we support bool, string, and []string. Configurable has a `Get(evaluator)` method to get the value of the property. The evaluator can be a ModuleContext, or if you’re in a situation where you only have a very limited context and a module, (such as in a singleton) you can use [ModuleBase.ConfigurableEvaluator](https://cs.android.com/android/platform/superproject/main/+/main:build/soong/android/module.go;l=2133;drc=e19f741052cce097da940d9083d3f29e668de5cb).
143
144`proptools.Configurable[T]` will handle unset properties for you, so you don’t need to make it a pointer type. However, there is a not-widely-known feature of property structs, where normally, properties are appended when squashing defaults. But if the property was a pointer property, later defaults replace earlier values instead of appending. With selects, to maintain this behavior, add the `android:"replace_instead_of_append"` struct tag. The "append" behavior for boolean values is to boolean OR them together, which is rarely what you want, so most boolean properties are pointers today.
145
146Old:
147```
148type commonProperties struct {
149  Enabled *bool `android:"arch_variant"`
150}
151
152func (m *ModuleBase) Enabled() *bool {
153  return m.commonProperties.Enabled
154}
155```
156
157New:
158```
159type commonProperties struct {
160  Enabled proptools.Configurable[bool] `android:"arch_variant,replace_instead_of_append"`
161}
162
163func (m *ModuleBase) Enabled(ctx ConfigAndErrorContext) *bool {
164  return m.commonProperties.Enabled.Get(m.ConfigurableEvaluator(ctx))
165}
166```
167
168The `android:"arch_variant"` tag is kept to support the old `target:` and `arch:` properties with this property, but if all their usages in bp files were replaced by selects, then that tag could be removed.
169
170The enabled property underwent this migration in https://r.android.com/3066188
171