blob: c8105eb9b86b694620e10f4867ecaa8c33176af8 [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 Kammer9abd62d2021-05-21 08:37:59 -040051 return value, []selects{ret}
Lukacs T. Berki1353e592021-04-30 15:35:09 +020052}
53
Liz Kammerd366c902021-06-03 13:43:01 -040054func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) {
55 value := reflect.ValueOf(boolAttr.Value)
56 if !boolAttr.HasConfigurableValues() {
57 return value, []selects{}
58 }
59
60 ret := selects{}
61 for _, axis := range boolAttr.SortedConfigurationAxes() {
62 configToBools := boolAttr.ConfigurableValues[axis]
63 for config, bools := range configToBools {
64 selectKey := axis.SelectKey(config)
65 ret[selectKey] = reflect.ValueOf(bools)
66 }
67 }
68 // if there is a select, use the base value as the conditions default value
69 if len(ret) > 0 {
70 ret[bazel.ConditionsDefaultSelectKey] = value
71 value = reflect.Zero(value.Type())
72 }
73
74 return value, []selects{ret}
75}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -040076func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000077 value := reflect.ValueOf(list.Value.Includes)
Liz Kammer2b07ec72021-05-26 15:08:27 -040078 var ret []selects
Liz Kammer9abd62d2021-05-21 08:37:59 -040079 for _, axis := range list.SortedConfigurationAxes() {
80 configToLabels := list.ConfigurableValues[axis]
81 if !configToLabels.HasConfigurableValues() {
82 continue
Liz Kammer2b07ec72021-05-26 15:08:27 -040083 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040084 archSelects := map[string]reflect.Value{}
85 for config, labels := range configToLabels {
86 selectKey := axis.SelectKey(config)
87 if use, value := labelListSelectValue(selectKey, labels); use {
88 archSelects[selectKey] = value
Liz Kammer2b07ec72021-05-26 15:08:27 -040089 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040090 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040091 if len(archSelects) > 0 {
92 ret = append(ret, archSelects)
Liz Kammer2b07ec72021-05-26 15:08:27 -040093 }
Jingwen Chenc1c26502021-04-05 10:35:13 +000094 }
95
Liz Kammer2b07ec72021-05-26 15:08:27 -040096 return value, ret
97}
98
99func labelListSelectValue(selectKey string, list bazel.LabelList) (bool, reflect.Value) {
100 if selectKey == bazel.ConditionsDefaultSelectKey || len(list.Includes) > 0 {
101 return true, reflect.ValueOf(list.Includes)
102 } else if len(list.Excludes) > 0 {
103 // if there is still an excludes -- we need to have an empty list for this select & use the
104 // value in conditions default Includes
105 return true, reflect.ValueOf([]string{})
106 }
107 return false, reflect.Zero(reflect.TypeOf([]string{}))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000108}
109
Liz Kammerd366c902021-06-03 13:43:01 -0400110var (
111 emptyBazelList = "[]"
112 bazelNone = "None"
113)
114
Jingwen Chenc1c26502021-04-05 10:35:13 +0000115// prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain
116// select statements.
117func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
118 var value reflect.Value
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400119 var configurableAttrs []selects
Liz Kammerd366c902021-06-03 13:43:01 -0400120 var defaultSelectValue *string
Jingwen Chenc1c26502021-04-05 10:35:13 +0000121 switch list := v.(type) {
122 case bazel.StringListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400123 value, configurableAttrs = getStringListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400124 defaultSelectValue = &emptyBazelList
Jingwen Chenc1c26502021-04-05 10:35:13 +0000125 case bazel.LabelListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400126 value, configurableAttrs = getLabelListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400127 defaultSelectValue = &emptyBazelList
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200128 case bazel.LabelAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400129 value, configurableAttrs = getLabelValue(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400130 defaultSelectValue = &bazelNone
131 case bazel.BoolAttribute:
132 value, configurableAttrs = getBoolValue(list)
133 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000134 default:
135 return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v)
136 }
137
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400138 var err error
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200139 ret := ""
140 if value.Kind() != reflect.Invalid {
141 s, err := prettyPrint(value, indent)
142 if err != nil {
143 return ret, err
144 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000145
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200146 ret += s
147 }
Jingwen Chen63930982021-03-24 10:04:33 -0400148 // Convenience function to append selects components to an attribute value.
Liz Kammerd366c902021-06-03 13:43:01 -0400149 appendSelects := func(selectsData selects, defaultValue *string, s string) (string, error) {
Jingwen Chen63930982021-03-24 10:04:33 -0400150 selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent)
151 if err != nil {
152 return "", err
153 }
154 if s != "" && selectMap != "" {
155 s += " + "
156 }
157 s += selectMap
158
159 return s, nil
160 }
161
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400162 for _, configurableAttr := range configurableAttrs {
163 ret, err = appendSelects(configurableAttr, defaultSelectValue, ret)
164 if err != nil {
165 return "", err
166 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400167 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400168
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400169 return ret, nil
Jingwen Chen91220d72021-03-24 02:18:33 -0400170}
171
172// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
173// to construct a select map for any kind of attribute type.
Liz Kammerd366c902021-06-03 13:43:01 -0400174func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000175 if selectMap == nil {
176 return "", nil
177 }
178
Jingwen Chen91220d72021-03-24 02:18:33 -0400179 var selects string
180 for _, selectKey := range android.SortedStringKeys(selectMap) {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400181 if selectKey == bazel.ConditionsDefaultSelectKey {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000182 // Handle default condition later.
183 continue
184 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400185 value := selectMap[selectKey]
186 if isZero(value) {
187 // Ignore zero values to not generate empty lists.
188 continue
189 }
190 s, err := prettyPrintSelectEntry(value, selectKey, indent)
191 if err != nil {
192 return "", err
193 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000194 // s could still be an empty string, e.g. unset slices of structs with
195 // length of 0.
196 if s != "" {
197 selects += s + ",\n"
198 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400199 }
200
201 if len(selects) == 0 {
202 // No conditions (or all values are empty lists), so no need for a map.
203 return "", nil
204 }
205
206 // Create the map.
Jingwen Chen63930982021-03-24 10:04:33 -0400207 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400208 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000209
210 // Handle the default condition
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400211 s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000212 if err != nil {
213 return "", err
214 }
Liz Kammerd366c902021-06-03 13:43:01 -0400215 if s != "" {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000216 // Print the custom default value.
217 ret += s
218 ret += ",\n"
Liz Kammerd366c902021-06-03 13:43:01 -0400219 } else if defaultValue != nil {
220 // Print an explicit empty list (the default value) even if the value is
221 // empty, to avoid errors about not finding a configuration that matches.
222 ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000223 }
224
Jingwen Chen91220d72021-03-24 02:18:33 -0400225 ret += makeIndent(indent)
226 ret += "})"
227
228 return ret, nil
229}
230
231// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
232// with a provided key.
233func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) {
234 s := makeIndent(indent + 1)
235 v, err := prettyPrint(value, indent+1)
236 if err != nil {
237 return "", err
238 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000239 if v == "" {
240 return "", nil
241 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400242 s += fmt.Sprintf("\"%s\": %s", key, v)
243 return s, nil
244}