blob: 005f13dd25d57bb9981321e36ca3cdbc3e3b1b0b [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{}
Chris Parsons51f8c392021-08-03 21:01:05 -040085 defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey]
Liz Kammer9abd62d2021-05-21 08:37:59 -040086 for config, labels := range configToLabels {
Chris Parsons51f8c392021-08-03 21:01:05 -040087 // Omit any entries in the map which match the default value, for brevity.
88 if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) {
89 continue
90 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040091 selectKey := axis.SelectKey(config)
92 if use, value := labelListSelectValue(selectKey, labels); use {
93 archSelects[selectKey] = value
Liz Kammer2b07ec72021-05-26 15:08:27 -040094 }
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -040095 }
Liz Kammer9abd62d2021-05-21 08:37:59 -040096 if len(archSelects) > 0 {
97 ret = append(ret, archSelects)
Liz Kammer2b07ec72021-05-26 15:08:27 -040098 }
Jingwen Chenc1c26502021-04-05 10:35:13 +000099 }
100
Liz Kammer2b07ec72021-05-26 15:08:27 -0400101 return value, ret
102}
103
104func labelListSelectValue(selectKey string, list bazel.LabelList) (bool, reflect.Value) {
105 if selectKey == bazel.ConditionsDefaultSelectKey || len(list.Includes) > 0 {
106 return true, reflect.ValueOf(list.Includes)
107 } else if len(list.Excludes) > 0 {
108 // if there is still an excludes -- we need to have an empty list for this select & use the
109 // value in conditions default Includes
110 return true, reflect.ValueOf([]string{})
111 }
112 return false, reflect.Zero(reflect.TypeOf([]string{}))
Jingwen Chenc1c26502021-04-05 10:35:13 +0000113}
114
Liz Kammerd366c902021-06-03 13:43:01 -0400115var (
116 emptyBazelList = "[]"
117 bazelNone = "None"
118)
119
Jingwen Chenc1c26502021-04-05 10:35:13 +0000120// prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain
121// select statements.
122func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
123 var value reflect.Value
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400124 var configurableAttrs []selects
Liz Kammerd366c902021-06-03 13:43:01 -0400125 var defaultSelectValue *string
Chris Parsons51f8c392021-08-03 21:01:05 -0400126 // If true, print the default attribute value, even if the attribute is zero.
127 shouldPrintDefault := false
Jingwen Chenc1c26502021-04-05 10:35:13 +0000128 switch list := v.(type) {
129 case bazel.StringListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400130 value, configurableAttrs = getStringListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400131 defaultSelectValue = &emptyBazelList
Jingwen Chenc1c26502021-04-05 10:35:13 +0000132 case bazel.LabelListAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400133 value, configurableAttrs = getLabelListValues(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400134 defaultSelectValue = &emptyBazelList
Chris Parsons51f8c392021-08-03 21:01:05 -0400135 if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) {
136 shouldPrintDefault = true
137 }
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200138 case bazel.LabelAttribute:
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400139 value, configurableAttrs = getLabelValue(list)
Liz Kammerd366c902021-06-03 13:43:01 -0400140 defaultSelectValue = &bazelNone
141 case bazel.BoolAttribute:
142 value, configurableAttrs = getBoolValue(list)
143 defaultSelectValue = &bazelNone
Jingwen Chenc1c26502021-04-05 10:35:13 +0000144 default:
145 return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v)
146 }
147
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400148 var err error
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200149 ret := ""
150 if value.Kind() != reflect.Invalid {
151 s, err := prettyPrint(value, indent)
152 if err != nil {
153 return ret, err
154 }
Jingwen Chenc1c26502021-04-05 10:35:13 +0000155
Lukacs T. Berki598dd002021-05-05 09:00:01 +0200156 ret += s
157 }
Jingwen Chen63930982021-03-24 10:04:33 -0400158 // Convenience function to append selects components to an attribute value.
Liz Kammerd366c902021-06-03 13:43:01 -0400159 appendSelects := func(selectsData selects, defaultValue *string, s string) (string, error) {
Jingwen Chen63930982021-03-24 10:04:33 -0400160 selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent)
161 if err != nil {
162 return "", err
163 }
164 if s != "" && selectMap != "" {
165 s += " + "
166 }
167 s += selectMap
168
169 return s, nil
170 }
171
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400172 for _, configurableAttr := range configurableAttrs {
173 ret, err = appendSelects(configurableAttr, defaultSelectValue, ret)
174 if err != nil {
175 return "", err
176 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400177 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400178
Chris Parsons51f8c392021-08-03 21:01:05 -0400179 if ret == "" && shouldPrintDefault {
180 return *defaultSelectValue, nil
181 }
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400182 return ret, nil
Jingwen Chen91220d72021-03-24 02:18:33 -0400183}
184
185// prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way
186// to construct a select map for any kind of attribute type.
Liz Kammerd366c902021-06-03 13:43:01 -0400187func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000188 if selectMap == nil {
189 return "", nil
190 }
191
Jingwen Chen91220d72021-03-24 02:18:33 -0400192 var selects string
193 for _, selectKey := range android.SortedStringKeys(selectMap) {
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400194 if selectKey == bazel.ConditionsDefaultSelectKey {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000195 // Handle default condition later.
196 continue
197 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400198 value := selectMap[selectKey]
199 if isZero(value) {
200 // Ignore zero values to not generate empty lists.
201 continue
202 }
203 s, err := prettyPrintSelectEntry(value, selectKey, indent)
204 if err != nil {
205 return "", err
206 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000207 // s could still be an empty string, e.g. unset slices of structs with
208 // length of 0.
209 if s != "" {
210 selects += s + ",\n"
211 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400212 }
213
214 if len(selects) == 0 {
215 // No conditions (or all values are empty lists), so no need for a map.
216 return "", nil
217 }
218
219 // Create the map.
Jingwen Chen63930982021-03-24 10:04:33 -0400220 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400221 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000222
223 // Handle the default condition
Liz Kammer6fd7b3f2021-05-06 13:54:29 -0400224 s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000225 if err != nil {
226 return "", err
227 }
Liz Kammerd366c902021-06-03 13:43:01 -0400228 if s != "" {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000229 // Print the custom default value.
230 ret += s
231 ret += ",\n"
Liz Kammerd366c902021-06-03 13:43:01 -0400232 } else if defaultValue != nil {
233 // Print an explicit empty list (the default value) even if the value is
234 // empty, to avoid errors about not finding a configuration that matches.
235 ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue)
Jingwen Chene32e9e02021-04-23 09:17:24 +0000236 }
237
Jingwen Chen91220d72021-03-24 02:18:33 -0400238 ret += makeIndent(indent)
239 ret += "})"
240
241 return ret, nil
242}
243
244// prettyPrintSelectEntry converts a reflect.Value into an entry in a select map
245// with a provided key.
246func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) {
247 s := makeIndent(indent + 1)
248 v, err := prettyPrint(value, indent+1)
249 if err != nil {
250 return "", err
251 }
Jingwen Chened9c17d2021-04-13 07:14:55 +0000252 if v == "" {
253 return "", nil
254 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400255 s += fmt.Sprintf("\"%s\": %s", key, v)
256 return s, nil
257}