blob: c6309655d0e96ac16b7f3179ddf190bb223dfbe3 [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 }
31 // if there is a select, use the base value as the conditions default value
32 if len(ret) > 0 {
33 ret[bazel.ConditionsDefaultSelectKey] = value
34 value = reflect.Zero(value.Type())
35 }
36
37 return value, []selects{ret}
38}
39
Zi Wang1cb11802022-12-09 16:08:54 -080040func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects, bool) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000041 value := reflect.ValueOf(list.Value)
Zi Wang0f828442022-12-28 11:18:11 -080042 prepend := list.Prepend
Jingwen Chenc1c26502021-04-05 10:35:13 +000043 if !list.HasConfigurableValues() {
Zi Wang1cb11802022-12-09 16:08:54 -080044 return value, []selects{}, prepend
Jingwen Chen5d864492021-02-24 07:20:12 -050045 }
Jingwen Chen91220d72021-03-24 02:18:33 -040046
Liz Kammer9abd62d2021-05-21 08:37:59 -040047 var ret []selects
48 for _, axis := range list.SortedConfigurationAxes() {
49 configToLists := list.ConfigurableValues[axis]
50 archSelects := map[string]reflect.Value{}
51 for config, labels := range configToLists {
52 selectKey := axis.SelectKey(config)
53 archSelects[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040054 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040055 if len(archSelects) > 0 {
56 ret = append(ret, archSelects)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040057 }
58 }
59
Zi Wang1cb11802022-12-09 16:08:54 -080060 return value, ret, prepend
Jingwen Chenc1c26502021-04-05 10:35:13 +000061}
62
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040063func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) {
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040064 value := reflect.ValueOf(label.Value)
65 if !label.HasConfigurableValues() {
66 return value, []selects{}
Lukacs T. Berki56bb0832021-05-12 12:36:45 +020067 }
68
Liz Kammer9abd62d2021-05-21 08:37:59 -040069 ret := selects{}
70 for _, axis := range label.SortedConfigurationAxes() {
71 configToLabels := label.ConfigurableValues[axis]
72 for config, labels := range configToLabels {
73 selectKey := axis.SelectKey(config)
74 ret[selectKey] = reflect.ValueOf(labels)
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -040075 }
76 }
77
Liz Kammerdff00ea2021-10-04 13:44:34 -040078 // if there is a select, use the base value as the conditions default value
79 if len(ret) > 0 {
80 ret[bazel.ConditionsDefaultSelectKey] = value
81 value = reflect.Zero(value.Type())
82 }
83
Liz Kammer9abd62d2021-05-21 08:37:59 -040084 return value, []selects{ret}
Lukacs T. Berki1353e592021-04-30 15:35:09 +020085}
86
Liz Kammerd366c902021-06-03 13:43:01 -040087func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) {
88 value := reflect.ValueOf(boolAttr.Value)
89 if !boolAttr.HasConfigurableValues() {
90 return value, []selects{}
91 }
92
93 ret := selects{}
94 for _, axis := range boolAttr.SortedConfigurationAxes() {
95 configToBools := boolAttr.ConfigurableValues[axis]
96 for config, bools := range configToBools {
97 selectKey := axis.SelectKey(config)
98 ret[selectKey] = reflect.ValueOf(bools)
99 }
100 }
101 // if there is a select, use the base value as the conditions default value
102 if len(ret) > 0 {
103 ret[bazel.ConditionsDefaultSelectKey] = value
104 value = reflect.Zero(value.Type())
105 }
106
107 return value, []selects{ret}
108}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400109func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000110 value := reflect.ValueOf(list.Value.Includes)
Liz Kammer2b07ec72021-05-26 15:08:27 -0400111 var ret []selects
Liz Kammer9abd62d2021-05-21 08:37:59 -0400112 for _, axis := range list.SortedConfigurationAxes() {
113 configToLabels := list.ConfigurableValues[axis]
114 if !configToLabels.HasConfigurableValues() {
115 continue
Liz Kammer2b07ec72021-05-26 15:08:27 -0400116 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400117 archSelects := map[string]reflect.Value{}
Chris Parsons51f8c392021-08-03 21:01:05 -0400118 defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey]
Chris Parsons58852a02021-12-09 18:10:18 -0500119 // Skip empty list values unless ether EmitEmptyList is true, or these values differ from the default.
120 emitEmptyList := list.EmitEmptyList || len(defaultVal.Includes) > 0
Liz Kammer9abd62d2021-05-21 08:37:59 -0400121 for config, labels := range configToLabels {
Chris Parsons51f8c392021-08-03 21:01:05 -0400122 // Omit any entries in the map which match the default value, for brevity.
123 if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) {
124 continue
125 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400126 selectKey := axis.SelectKey(config)
Chris Parsons58852a02021-12-09 18:10:18 -0500127 if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use {
Liz Kammer9abd62d2021-05-21 08:37:59 -0400128 archSelects[selectKey] = value
Liz Kammer2b07ec72021-05-26 15:08:27 -0400129 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -0400130 }
Liz Kammer9abd62d2021-05-21 08:37:59 -0400131 if len(archSelects) > 0 {
132 ret = append(ret, archSelects)
Liz Kammer2b07ec72021-05-26 15:08:27 -0400133 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000134 }
135
Liz Kammer2b07ec72021-05-26 15:08:27 -0400136 return value, ret
137}
138
Jingwen Chen58ff6802021-11-17 12:14:41 +0000139func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) {
140 if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 {
Liz Kammer2b07ec72021-05-26 15:08:27 -0400141 return true, reflect.ValueOf(list.Includes)
142 } else if len(list.Excludes) > 0 {
143 // if there is still an excludes -- we need to have an empty list for this select & use the
144 // value in conditions default Includes
145 return true, reflect.ValueOf([]string{})
146 }
147 return false, reflect.Zero(reflect.TypeOf([]string{}))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000148}
149
Liz Kammerd366c902021-06-03 13:43:01 -0400150var (
151 emptyBazelList = "[]"
152 bazelNone = "None"
153)
154
Jingwen Chenc1c26502021-04-05 10:35:13 +0000155// prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain
156// select statements.
157func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
158 var value reflect.Value
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400159 var configurableAttrs []selects
Zi Wang1cb11802022-12-09 16:08:54 -0800160 var prepend bool
Liz Kammerd366c902021-06-03 13:43:01 -0400161 var defaultSelectValue *string
Jingwen Chen58ff6802021-11-17 12:14:41 +0000162 var emitZeroValues bool
Chris Parsons51f8c392021-08-03 21:01:05 -0400163 // If true, print the default attribute value, even if the attribute is zero.
164 shouldPrintDefault := false
Jingwen Chenc1c26502021-04-05 10:35:13 +0000165 switch list := v.(type) {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux3a019a62022-06-23 16:02:44 +0000166 case bazel.StringAttribute:
167 if err := list.Collapse(); err != nil {
168 return "", err
169 }
170 value, configurableAttrs = getStringValue(list)
171 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000172 case bazel.StringListAttribute:
Zi Wang1cb11802022-12-09 16:08:54 -0800173 value, configurableAttrs, prepend = getStringListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400174 defaultSelectValue = &emptyBazelList
Jingwen Chenc1c26502021-04-05 10:35:13 +0000175 case bazel.LabelListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400176 value, configurableAttrs = getLabelListValues(list)
Jingwen Chen58ff6802021-11-17 12:14:41 +0000177 emitZeroValues = list.EmitEmptyList
Liz Kammerd366c902021-06-03 13:43:01 -0400178 defaultSelectValue = &emptyBazelList
Chris Parsons51f8c392021-08-03 21:01:05 -0400179 if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) {
180 shouldPrintDefault = true
181 }
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200182 case bazel.LabelAttribute:
Chris Parsons58852a02021-12-09 18:10:18 -0500183 if err := list.Collapse(); err != nil {
184 return "", err
185 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400186 value, configurableAttrs = getLabelValue(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400187 defaultSelectValue = &bazelNone
188 case bazel.BoolAttribute:
Chris Parsons58852a02021-12-09 18:10:18 -0500189 if err := list.Collapse(); err != nil {
190 return "", err
191 }
Liz Kammerd366c902021-06-03 13:43:01 -0400192 value, configurableAttrs = getBoolValue(list)
193 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000194 default:
195 return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v)
196 }
197
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400198 var err error
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200199 ret := ""
200 if value.Kind() != reflect.Invalid {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000201 s, err := prettyPrint(value, indent, false) // never emit zero values for the base value
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200202 if err != nil {
203 return ret, err
204 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000205
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200206 ret += s
207 }
Zi Wang1cb11802022-12-09 16:08:54 -0800208 // Convenience function to prepend/append selects components to an attribute value.
209 concatenateSelects := func(selectsData selects, defaultValue *string, s string, prepend bool) (string, error) {
Jingwen Chen58ff6802021-11-17 12:14:41 +0000210 selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues)
Jingwen Chen63930982021-03-24 10:04:33 -0400211 if err != nil {
212 return "", err
213 }
Zi Wang1cb11802022-12-09 16:08:54 -0800214 var left, right string
215 if prepend {
216 left, right = selectMap, s
217 } else {
218 left, right = s, selectMap
Jingwen Chen63930982021-03-24 10:04:33 -0400219 }
Zi Wang1cb11802022-12-09 16:08:54 -0800220 if left != "" && right != "" {
221 left += " + "
222 }
223 left += right
Jingwen Chen63930982021-03-24 10:04:33 -0400224
Zi Wang1cb11802022-12-09 16:08:54 -0800225 return left, nil
Jingwen Chen63930982021-03-24 10:04:33 -0400226 }
227
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400228 for _, configurableAttr := range configurableAttrs {
Zi Wang1cb11802022-12-09 16:08:54 -0800229 ret, err = concatenateSelects(configurableAttr, defaultSelectValue, ret, prepend)
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400230 if err != nil {
231 return "", err
232 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400233 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400234
Chris Parsons51f8c392021-08-03 21:01:05 -0400235 if ret == "" && shouldPrintDefault {
236 return *defaultSelectValue, nil
237 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400238 return ret, nil
Jingwen Chen91220d72021-03-24 02:18:33 -0400239}
240
241// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
242// to construct a select map for any kind of attribute type.
Jingwen Chen58ff6802021-11-17 12:14:41 +0000243func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int, emitZeroValues bool) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000244 if selectMap == nil {
245 return "", nil
246 }
247
Jingwen Chen91220d72021-03-24 02:18:33 -0400248 var selects string
249 for _, selectKey := range android.SortedStringKeys(selectMap) {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400250 if selectKey == bazel.ConditionsDefaultSelectKey {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000251 // Handle default condition later.
252 continue
253 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400254 value := selectMap[selectKey]
Chris Parsons58852a02021-12-09 18:10:18 -0500255 if isZero(value) && !emitZeroValues && isZero(selectMap[bazel.ConditionsDefaultSelectKey]) {
256 // Ignore zero values to not generate empty lists. However, always note zero values if
257 // the default value is non-zero.
Jingwen Chen91220d72021-03-24 02:18:33 -0400258 continue
259 }
Chris Parsons58852a02021-12-09 18:10:18 -0500260 s, err := prettyPrintSelectEntry(value, selectKey, indent, true)
Jingwen Chen91220d72021-03-24 02:18:33 -0400261 if err != nil {
262 return "", err
263 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000264 // s could still be an empty string, e.g. unset slices of structs with
265 // length of 0.
266 if s != "" {
267 selects += s + ",\n"
268 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400269 }
270
271 if len(selects) == 0 {
272 // No conditions (or all values are empty lists), so no need for a map.
273 return "", nil
274 }
275
276 // Create the map.
Jingwen Chen63930982021-03-24 10:04:33 -0400277 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400278 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000279
280 // Handle the default condition
Jingwen Chen58ff6802021-11-17 12:14:41 +0000281 s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000282 if err != nil {
283 return "", err
284 }
Liz Kammerd366c902021-06-03 13:43:01 -0400285 if s != "" {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000286 // Print the custom default value.
287 ret += s
288 ret += ",\n"
Liz Kammerd366c902021-06-03 13:43:01 -0400289 } else if defaultValue != nil {
290 // Print an explicit empty list (the default value) even if the value is
291 // empty, to avoid errors about not finding a configuration that matches.
Liz Kammer72beb342022-02-03 08:42:10 -0500292 ret += fmt.Sprintf("%s\"%s\": %s,\n", starlark_fmt.Indention(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000293 }
294
Liz Kammer72beb342022-02-03 08:42:10 -0500295 ret += starlark_fmt.Indention(indent)
Jingwen Chen91220d72021-03-24 02:18:33 -0400296 ret += "})"
297
298 return ret, nil
299}
300
301// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
302// with a provided key.
Jingwen Chen58ff6802021-11-17 12:14:41 +0000303func prettyPrintSelectEntry(value reflect.Value, key string, indent int, emitZeroValues bool) (string, error) {
Liz Kammer72beb342022-02-03 08:42:10 -0500304 s := starlark_fmt.Indention(indent + 1)
Jingwen Chen58ff6802021-11-17 12:14:41 +0000305 v, err := prettyPrint(value, indent+1, emitZeroValues)
Jingwen Chen91220d72021-03-24 02:18:33 -0400306 if err != nil {
307 return "", err
308 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000309 if v == "" {
310 return "", nil
311 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400312 s += fmt.Sprintf("\"%s\": %s", key, v)
313 return s, nil
314}