blob: 42449569270df4233d4edead1947a8a08fcb96ba [file] [log] [blame]
Jingwen Chen5d864492021-02-24 07:20:12 -05001package bp2build
2
Jingwen Chen91220d72021-03-24 02:18:33 -04003import (
Jingwen Chen91220d72021-03-24 02:18:33 -04004 "fmt"
5 "reflect"
Chris Parsons58852a02021-12-09 18:10:18 -05006
7 "android/soong/android"
8 "android/soong/bazel"
Liz Kammer72beb342022-02-03 08:42:10 -05009 "android/soong/starlark_fmt"
Jingwen Chen91220d72021-03-24 02:18:33 -040010)
Jingwen Chen5d864492021-02-24 07:20:12 -050011
12// Configurability support for bp2build.
13
Jingwen Chenc1c26502021-04-05 10:35:13 +000014type selects map[string]reflect.Value
15
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +000016func getStringValue(str bazel.StringAttribute) (reflect.Value, []selects) {
17 value := reflect.ValueOf(str.Value)
18
19 if !str.HasConfigurableValues() {
20 return value, []selects{}
21 }
22
23 ret := selects{}
24 for _, axis := range str.SortedConfigurationAxes() {
25 configToStrs := str.ConfigurableValues[axis]
26 for config, strs := range configToStrs {
27 selectKey := axis.SelectKey(config)
28 ret[selectKey] = reflect.ValueOf(strs)
29 }
30 }
Liz Kammer9d2d4102022-12-21 14:51:37 -050031
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +000032 // if there is a select, use the base value as the conditions default value
33 if len(ret) > 0 {
Liz Kammer9d2d4102022-12-21 14:51:37 -050034 if _, ok := ret[bazel.ConditionsDefaultSelectKey]; !ok {
35 ret[bazel.ConditionsDefaultSelectKey] = value
36 value = reflect.Zero(value.Type())
37 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +000038 }
39
40 return value, []selects{ret}
41}
42
Zi Wang1cb11802022-12-09 16:08:54 -080043func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects, bool) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000044 value := reflect.ValueOf(list.Value)
Zi Wang0f828442022-12-28 11:18:11 -080045 prepend := list.Prepend
Jingwen Chenc1c26502021-04-05 10:35:13 +000046 if !list.HasConfigurableValues() {
Zi Wang1cb11802022-12-09 16:08:54 -080047 return value, []selects{}, prepend
Jingwen Chen5d864492021-02-24 07:20:12 -050048 }
Jingwen Chen91220d72021-03-24 02:18:33 -040049
Liz Kammer9abd62d2021-05-21 08:37:59 -040050 var ret []selects
51 for _, axis := range list.SortedConfigurationAxes() {
52 configToLists := list.ConfigurableValues[axis]
53 archSelects := map[string]reflect.Value{}
54 for config, labels := range configToLists {
55 selectKey := axis.SelectKey(config)
56 archSelects[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040057 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040058 if len(archSelects) > 0 {
59 ret = append(ret, archSelects)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040060 }
61 }
62
Zi Wang1cb11802022-12-09 16:08:54 -080063 return value, ret, prepend
Jingwen Chenc1c26502021-04-05 10:35:13 +000064}
65
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040066func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040067 value := reflect.ValueOf(label.Value)
68 if !label.HasConfigurableValues() {
69 return value, []selects{}
Lukacs T. Berki56bb0832021-05-12 12:36:45 +020070 }
71
Liz Kammer9abd62d2021-05-21 08:37:59 -040072 ret := selects{}
73 for _, axis := range label.SortedConfigurationAxes() {
74 configToLabels := label.ConfigurableValues[axis]
75 for config, labels := range configToLabels {
76 selectKey := axis.SelectKey(config)
77 ret[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040078 }
79 }
80
Liz Kammerdff00ea2021-10-04 13:44:34 -040081 // if there is a select, use the base value as the conditions default value
82 if len(ret) > 0 {
83 ret[bazel.ConditionsDefaultSelectKey] = value
84 value = reflect.Zero(value.Type())
85 }
86
Liz Kammer9abd62d2021-05-21 08:37:59 -040087 return value, []selects{ret}
Lukacs T. Berki1353e592021-04-30 15:35:09 +020088}
89
Liz Kammerd366c902021-06-03 13:43:01 -040090func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) {
91 value := reflect.ValueOf(boolAttr.Value)
92 if !boolAttr.HasConfigurableValues() {
93 return value, []selects{}
94 }
95
96 ret := selects{}
97 for _, axis := range boolAttr.SortedConfigurationAxes() {
98 configToBools := boolAttr.ConfigurableValues[axis]
99 for config, bools := range configToBools {
100 selectKey := axis.SelectKey(config)
101 ret[selectKey] = reflect.ValueOf(bools)
102 }
103 }
104 // if there is a select, use the base value as the conditions default value
105 if len(ret) > 0 {
106 ret[bazel.ConditionsDefaultSelectKey] = value
107 value = reflect.Zero(value.Type())
108 }
109
110 return value, []selects{ret}
111}
Zi Wang9f609db2023-01-04 11:06:54 -0800112func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects, bool) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000113 value := reflect.ValueOf(list.Value.Includes)
Zi Wang9f609db2023-01-04 11:06:54 -0800114 prepend := list.Prepend
Liz Kammer2b07ec72021-05-26 15:08:27 -0400115 var ret []selects
Liz Kammer9abd62d2021-05-21 08:37:59 -0400116 for _, axis := range list.SortedConfigurationAxes() {
117 configToLabels := list.ConfigurableValues[axis]
118 if !configToLabels.HasConfigurableValues() {
119 continue
Liz Kammer2b07ec72021-05-26 15:08:27 -0400120 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400121 archSelects := map[string]reflect.Value{}
Chris Parsons51f8c392021-08-03 21:01:05 -0400122 defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey]
Chris Parsons58852a02021-12-09 18:10:18 -0500123 // Skip empty list values unless ether EmitEmptyList is true, or these values differ from the default.
124 emitEmptyList := list.EmitEmptyList || len(defaultVal.Includes) > 0
Liz Kammer9abd62d2021-05-21 08:37:59 -0400125 for config, labels := range configToLabels {
Chris Parsons51f8c392021-08-03 21:01:05 -0400126 // Omit any entries in the map which match the default value, for brevity.
127 if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) {
128 continue
129 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400130 selectKey := axis.SelectKey(config)
Chris Parsons58852a02021-12-09 18:10:18 -0500131 if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400132 archSelects[selectKey] = value
Liz Kammer2b07ec72021-05-26 15:08:27 -0400133 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400134 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400135 if len(archSelects) > 0 {
136 ret = append(ret, archSelects)
Liz Kammer2b07ec72021-05-26 15:08:27 -0400137 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000138 }
139
Zi Wang9f609db2023-01-04 11:06:54 -0800140 return value, ret, prepend
Liz Kammer2b07ec72021-05-26 15:08:27 -0400141}
142
Jingwen Chen58ff6802021-11-17 12:14:41 +0000143func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) {
144 if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 {
Liz Kammer2b07ec72021-05-26 15:08:27 -0400145 return true, reflect.ValueOf(list.Includes)
146 } else if len(list.Excludes) > 0 {
147 // if there is still an excludes -- we need to have an empty list for this select & use the
148 // value in conditions default Includes
149 return true, reflect.ValueOf([]string{})
150 }
151 return false, reflect.Zero(reflect.TypeOf([]string{}))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000152}
153
Liz Kammerd366c902021-06-03 13:43:01 -0400154var (
155 emptyBazelList = "[]"
156 bazelNone = "None"
157)
158
Jingwen Chenc1c26502021-04-05 10:35:13 +0000159// prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain
160// select statements.
161func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
162 var value reflect.Value
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400163 var configurableAttrs []selects
Zi Wang1cb11802022-12-09 16:08:54 -0800164 var prepend bool
Liz Kammerd366c902021-06-03 13:43:01 -0400165 var defaultSelectValue *string
Jingwen Chen58ff6802021-11-17 12:14:41 +0000166 var emitZeroValues bool
Chris Parsons51f8c392021-08-03 21:01:05 -0400167 // If true, print the default attribute value, even if the attribute is zero.
168 shouldPrintDefault := false
Jingwen Chenc1c26502021-04-05 10:35:13 +0000169 switch list := v.(type) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000170 case bazel.StringAttribute:
171 if err := list.Collapse(); err != nil {
172 return "", err
173 }
174 value, configurableAttrs = getStringValue(list)
175 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000176 case bazel.StringListAttribute:
Zi Wang1cb11802022-12-09 16:08:54 -0800177 value, configurableAttrs, prepend = getStringListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400178 defaultSelectValue = &emptyBazelList
Jingwen Chenc1c26502021-04-05 10:35:13 +0000179 case bazel.LabelListAttribute:
Zi Wang9f609db2023-01-04 11:06:54 -0800180 value, configurableAttrs, prepend = getLabelListValues(list)
Jingwen Chen58ff6802021-11-17 12:14:41 +0000181 emitZeroValues = list.EmitEmptyList
Liz Kammerd366c902021-06-03 13:43:01 -0400182 defaultSelectValue = &emptyBazelList
Chris Parsons51f8c392021-08-03 21:01:05 -0400183 if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) {
184 shouldPrintDefault = true
185 }
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200186 case bazel.LabelAttribute:
Chris Parsons58852a02021-12-09 18:10:18 -0500187 if err := list.Collapse(); err != nil {
188 return "", err
189 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400190 value, configurableAttrs = getLabelValue(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400191 defaultSelectValue = &bazelNone
192 case bazel.BoolAttribute:
Chris Parsons58852a02021-12-09 18:10:18 -0500193 if err := list.Collapse(); err != nil {
194 return "", err
195 }
Liz Kammerd366c902021-06-03 13:43:01 -0400196 value, configurableAttrs = getBoolValue(list)
197 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000198 default:
199 return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v)
200 }
201
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400202 var err error
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200203 ret := ""
204 if value.Kind() != reflect.Invalid {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000205 s, err := prettyPrint(value, indent, false) // never emit zero values for the base value
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200206 if err != nil {
207 return ret, err
208 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000209
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200210 ret += s
211 }
Zi Wang1cb11802022-12-09 16:08:54 -0800212 // Convenience function to prepend/append selects components to an attribute value.
213 concatenateSelects := func(selectsData selects, defaultValue *string, s string, prepend bool) (string, error) {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000214 selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues)
Jingwen Chen63930982021-03-24 10:04:33 -0400215 if err != nil {
216 return "", err
217 }
Zi Wang1cb11802022-12-09 16:08:54 -0800218 var left, right string
219 if prepend {
220 left, right = selectMap, s
221 } else {
222 left, right = s, selectMap
Jingwen Chen63930982021-03-24 10:04:33 -0400223 }
Zi Wang1cb11802022-12-09 16:08:54 -0800224 if left != "" && right != "" {
225 left += " + "
226 }
227 left += right
Jingwen Chen63930982021-03-24 10:04:33 -0400228
Zi Wang1cb11802022-12-09 16:08:54 -0800229 return left, nil
Jingwen Chen63930982021-03-24 10:04:33 -0400230 }
231
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400232 for _, configurableAttr := range configurableAttrs {
Zi Wang1cb11802022-12-09 16:08:54 -0800233 ret, err = concatenateSelects(configurableAttr, defaultSelectValue, ret, prepend)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400234 if err != nil {
235 return "", err
236 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400237 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400238
Chris Parsons51f8c392021-08-03 21:01:05 -0400239 if ret == "" && shouldPrintDefault {
240 return *defaultSelectValue, nil
241 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400242 return ret, nil
Jingwen Chen91220d72021-03-24 02:18:33 -0400243}
244
245// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
246// to construct a select map for any kind of attribute type.
Jingwen Chen58ff6802021-11-17 12:14:41 +0000247func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int, emitZeroValues bool) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000248 if selectMap == nil {
249 return "", nil
250 }
251
Jingwen Chen91220d72021-03-24 02:18:33 -0400252 var selects string
253 for _, selectKey := range android.SortedStringKeys(selectMap) {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400254 if selectKey == bazel.ConditionsDefaultSelectKey {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000255 // Handle default condition later.
256 continue
257 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400258 value := selectMap[selectKey]
Chris Parsons58852a02021-12-09 18:10:18 -0500259 if isZero(value) && !emitZeroValues && isZero(selectMap[bazel.ConditionsDefaultSelectKey]) {
260 // Ignore zero values to not generate empty lists. However, always note zero values if
261 // the default value is non-zero.
Jingwen Chen91220d72021-03-24 02:18:33 -0400262 continue
263 }
Chris Parsons58852a02021-12-09 18:10:18 -0500264 s, err := prettyPrintSelectEntry(value, selectKey, indent, true)
Jingwen Chen91220d72021-03-24 02:18:33 -0400265 if err != nil {
266 return "", err
267 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000268 // s could still be an empty string, e.g. unset slices of structs with
269 // length of 0.
270 if s != "" {
271 selects += s + ",\n"
272 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400273 }
274
275 if len(selects) == 0 {
276 // No conditions (or all values are empty lists), so no need for a map.
277 return "", nil
278 }
279
280 // Create the map.
Jingwen Chen63930982021-03-24 10:04:33 -0400281 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400282 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000283
284 // Handle the default condition
Jingwen Chen58ff6802021-11-17 12:14:41 +0000285 s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000286 if err != nil {
287 return "", err
288 }
Liz Kammerd366c902021-06-03 13:43:01 -0400289 if s != "" {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000290 // Print the custom default value.
291 ret += s
292 ret += ",\n"
Liz Kammerd366c902021-06-03 13:43:01 -0400293 } else if defaultValue != nil {
294 // Print an explicit empty list (the default value) even if the value is
295 // empty, to avoid errors about not finding a configuration that matches.
Liz Kammer72beb342022-02-03 08:42:10 -0500296 ret += fmt.Sprintf("%s\"%s\": %s,\n", starlark_fmt.Indention(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000297 }
298
Liz Kammer72beb342022-02-03 08:42:10 -0500299 ret += starlark_fmt.Indention(indent)
Jingwen Chen91220d72021-03-24 02:18:33 -0400300 ret += "})"
301
302 return ret, nil
303}
304
305// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
306// with a provided key.
Jingwen Chen58ff6802021-11-17 12:14:41 +0000307func prettyPrintSelectEntry(value reflect.Value, key string, indent int, emitZeroValues bool) (string, error) {
Liz Kammer72beb342022-02-03 08:42:10 -0500308 s := starlark_fmt.Indention(indent + 1)
Jingwen Chen58ff6802021-11-17 12:14:41 +0000309 v, err := prettyPrint(value, indent+1, emitZeroValues)
Jingwen Chen91220d72021-03-24 02:18:33 -0400310 if err != nil {
311 return "", err
312 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000313 if v == "" {
314 return "", nil
315 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400316 s += fmt.Sprintf("\"%s\": %s", key, v)
317 return s, nil
318}