blob: 0bcf91d58692bff96b3ce9368b749112fae3c9f6 [file] [log] [blame]
Jingwen Chen5d864492021-02-24 07:20:12 -05001package bp2build
2
Jingwen Chen91220d72021-03-24 02:18:33 -04003import (
4 "android/soong/android"
5 "android/soong/bazel"
6 "fmt"
7 "reflect"
8)
Jingwen Chen5d864492021-02-24 07:20:12 -05009
10// Configurability support for bp2build.
11
Jingwen Chenc1c26502021-04-05 10:35:13 +000012type selects map[string]reflect.Value
13
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040014func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000015 value := reflect.ValueOf(list.Value)
16 if !list.HasConfigurableValues() {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040017 return value, []selects{}
Jingwen Chen5d864492021-02-24 07:20:12 -050018 }
Jingwen Chen91220d72021-03-24 02:18:33 -040019
Liz Kammer9abd62d2021-05-21 08:37:59 -040020 var ret []selects
21 for _, axis := range list.SortedConfigurationAxes() {
22 configToLists := list.ConfigurableValues[axis]
23 archSelects := map[string]reflect.Value{}
24 for config, labels := range configToLists {
25 selectKey := axis.SelectKey(config)
26 archSelects[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040027 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040028 if len(archSelects) > 0 {
29 ret = append(ret, archSelects)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040030 }
31 }
32
Liz Kammer9abd62d2021-05-21 08:37:59 -040033 return value, ret
Jingwen Chenc1c26502021-04-05 10:35:13 +000034}
35
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040036func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040037 value := reflect.ValueOf(label.Value)
38 if !label.HasConfigurableValues() {
39 return value, []selects{}
Lukacs T. Berki56bb0832021-05-12 12:36:45 +020040 }
41
Liz Kammer9abd62d2021-05-21 08:37:59 -040042 ret := selects{}
43 for _, axis := range label.SortedConfigurationAxes() {
44 configToLabels := label.ConfigurableValues[axis]
45 for config, labels := range configToLabels {
46 selectKey := axis.SelectKey(config)
47 ret[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040048 }
49 }
50
Liz Kammerdff00ea2021-10-04 13:44:34 -040051 // if there is a select, use the base value as the conditions default value
52 if len(ret) > 0 {
53 ret[bazel.ConditionsDefaultSelectKey] = value
54 value = reflect.Zero(value.Type())
55 }
56
Liz Kammer9abd62d2021-05-21 08:37:59 -040057 return value, []selects{ret}
Lukacs T. Berki1353e592021-04-30 15:35:09 +020058}
59
Liz Kammerd366c902021-06-03 13:43:01 -040060func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) {
61 value := reflect.ValueOf(boolAttr.Value)
62 if !boolAttr.HasConfigurableValues() {
63 return value, []selects{}
64 }
65
66 ret := selects{}
67 for _, axis := range boolAttr.SortedConfigurationAxes() {
68 configToBools := boolAttr.ConfigurableValues[axis]
69 for config, bools := range configToBools {
70 selectKey := axis.SelectKey(config)
71 ret[selectKey] = reflect.ValueOf(bools)
72 }
73 }
74 // if there is a select, use the base value as the conditions default value
75 if len(ret) > 0 {
76 ret[bazel.ConditionsDefaultSelectKey] = value
77 value = reflect.Zero(value.Type())
78 }
79
80 return value, []selects{ret}
81}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040082func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000083 value := reflect.ValueOf(list.Value.Includes)
Liz Kammer2b07ec72021-05-26 15:08:27 -040084 var ret []selects
Liz Kammer9abd62d2021-05-21 08:37:59 -040085 for _, axis := range list.SortedConfigurationAxes() {
86 configToLabels := list.ConfigurableValues[axis]
87 if !configToLabels.HasConfigurableValues() {
88 continue
Liz Kammer2b07ec72021-05-26 15:08:27 -040089 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040090 archSelects := map[string]reflect.Value{}
Chris Parsons51f8c392021-08-03 21:01:05 -040091 defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey]
Liz Kammer9abd62d2021-05-21 08:37:59 -040092 for config, labels := range configToLabels {
Chris Parsons51f8c392021-08-03 21:01:05 -040093 // Omit any entries in the map which match the default value, for brevity.
94 if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) {
95 continue
96 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040097 selectKey := axis.SelectKey(config)
98 if use, value := labelListSelectValue(selectKey, labels); use {
99 archSelects[selectKey] = value
Liz Kammer2b07ec72021-05-26 15:08:27 -0400100 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400101 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400102 if len(archSelects) > 0 {
103 ret = append(ret, archSelects)
Liz Kammer2b07ec72021-05-26 15:08:27 -0400104 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000105 }
106
Liz Kammer2b07ec72021-05-26 15:08:27 -0400107 return value, ret
108}
109
110func labelListSelectValue(selectKey string, list bazel.LabelList) (bool, reflect.Value) {
111 if selectKey == bazel.ConditionsDefaultSelectKey || len(list.Includes) > 0 {
112 return true, reflect.ValueOf(list.Includes)
113 } else if len(list.Excludes) > 0 {
114 // if there is still an excludes -- we need to have an empty list for this select & use the
115 // value in conditions default Includes
116 return true, reflect.ValueOf([]string{})
117 }
118 return false, reflect.Zero(reflect.TypeOf([]string{}))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000119}
120
Liz Kammerd366c902021-06-03 13:43:01 -0400121var (
122 emptyBazelList = "[]"
123 bazelNone = "None"
124)
125
Jingwen Chenc1c26502021-04-05 10:35:13 +0000126// prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain
127// select statements.
128func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
129 var value reflect.Value
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400130 var configurableAttrs []selects
Liz Kammerd366c902021-06-03 13:43:01 -0400131 var defaultSelectValue *string
Chris Parsons51f8c392021-08-03 21:01:05 -0400132 // If true, print the default attribute value, even if the attribute is zero.
133 shouldPrintDefault := false
Jingwen Chenc1c26502021-04-05 10:35:13 +0000134 switch list := v.(type) {
135 case bazel.StringListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400136 value, configurableAttrs = getStringListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400137 defaultSelectValue = &emptyBazelList
Jingwen Chenc1c26502021-04-05 10:35:13 +0000138 case bazel.LabelListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400139 value, configurableAttrs = getLabelListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400140 defaultSelectValue = &emptyBazelList
Chris Parsons51f8c392021-08-03 21:01:05 -0400141 if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) {
142 shouldPrintDefault = true
143 }
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200144 case bazel.LabelAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400145 value, configurableAttrs = getLabelValue(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400146 defaultSelectValue = &bazelNone
147 case bazel.BoolAttribute:
148 value, configurableAttrs = getBoolValue(list)
149 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000150 default:
151 return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v)
152 }
153
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400154 var err error
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200155 ret := ""
156 if value.Kind() != reflect.Invalid {
157 s, err := prettyPrint(value, indent)
158 if err != nil {
159 return ret, err
160 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000161
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200162 ret += s
163 }
Jingwen Chen63930982021-03-24 10:04:33 -0400164 // Convenience function to append selects components to an attribute value.
Liz Kammerd366c902021-06-03 13:43:01 -0400165 appendSelects := func(selectsData selects, defaultValue *string, s string) (string, error) {
Jingwen Chen63930982021-03-24 10:04:33 -0400166 selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent)
167 if err != nil {
168 return "", err
169 }
170 if s != "" && selectMap != "" {
171 s += " + "
172 }
173 s += selectMap
174
175 return s, nil
176 }
177
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400178 for _, configurableAttr := range configurableAttrs {
179 ret, err = appendSelects(configurableAttr, defaultSelectValue, ret)
180 if err != nil {
181 return "", err
182 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400183 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400184
Chris Parsons51f8c392021-08-03 21:01:05 -0400185 if ret == "" && shouldPrintDefault {
186 return *defaultSelectValue, nil
187 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400188 return ret, nil
Jingwen Chen91220d72021-03-24 02:18:33 -0400189}
190
191// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
192// to construct a select map for any kind of attribute type.
Liz Kammerd366c902021-06-03 13:43:01 -0400193func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000194 if selectMap == nil {
195 return "", nil
196 }
197
Jingwen Chen91220d72021-03-24 02:18:33 -0400198 var selects string
199 for _, selectKey := range android.SortedStringKeys(selectMap) {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400200 if selectKey == bazel.ConditionsDefaultSelectKey {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000201 // Handle default condition later.
202 continue
203 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400204 value := selectMap[selectKey]
205 if isZero(value) {
206 // Ignore zero values to not generate empty lists.
207 continue
208 }
209 s, err := prettyPrintSelectEntry(value, selectKey, indent)
210 if err != nil {
211 return "", err
212 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000213 // s could still be an empty string, e.g. unset slices of structs with
214 // length of 0.
215 if s != "" {
216 selects += s + ",\n"
217 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400218 }
219
220 if len(selects) == 0 {
221 // No conditions (or all values are empty lists), so no need for a map.
222 return "", nil
223 }
224
225 // Create the map.
Jingwen Chen63930982021-03-24 10:04:33 -0400226 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400227 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000228
229 // Handle the default condition
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400230 s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000231 if err != nil {
232 return "", err
233 }
Liz Kammerd366c902021-06-03 13:43:01 -0400234 if s != "" {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000235 // Print the custom default value.
236 ret += s
237 ret += ",\n"
Liz Kammerd366c902021-06-03 13:43:01 -0400238 } else if defaultValue != nil {
239 // Print an explicit empty list (the default value) even if the value is
240 // empty, to avoid errors about not finding a configuration that matches.
241 ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000242 }
243
Jingwen Chen91220d72021-03-24 02:18:33 -0400244 ret += makeIndent(indent)
245 ret += "})"
246
247 return ret, nil
248}
249
250// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
251// with a provided key.
252func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) {
253 s := makeIndent(indent + 1)
254 v, err := prettyPrint(value, indent+1)
255 if err != nil {
256 return "", err
257 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000258 if v == "" {
259 return "", nil
260 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400261 s += fmt.Sprintf("\"%s\": %s", key, v)
262 return s, nil
263}