Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 1 | package bp2build |
| 2 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 3 | import ( |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 4 | "fmt" |
| 5 | "reflect" |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 6 | |
| 7 | "android/soong/android" |
| 8 | "android/soong/bazel" |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 9 | "android/soong/starlark_fmt" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 10 | ) |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 11 | |
| 12 | // Configurability support for bp2build. |
| 13 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 14 | type selects map[string]reflect.Value |
| 15 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 16 | func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 17 | value := reflect.ValueOf(list.Value) |
| 18 | if !list.HasConfigurableValues() { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 19 | return value, []selects{} |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 20 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 21 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 22 | var ret []selects |
| 23 | for _, axis := range list.SortedConfigurationAxes() { |
| 24 | configToLists := list.ConfigurableValues[axis] |
| 25 | archSelects := map[string]reflect.Value{} |
| 26 | for config, labels := range configToLists { |
| 27 | selectKey := axis.SelectKey(config) |
| 28 | archSelects[selectKey] = reflect.ValueOf(labels) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 29 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 30 | if len(archSelects) > 0 { |
| 31 | ret = append(ret, archSelects) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 35 | return value, ret |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 38 | func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 39 | value := reflect.ValueOf(label.Value) |
| 40 | if !label.HasConfigurableValues() { |
| 41 | return value, []selects{} |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 44 | ret := selects{} |
| 45 | for _, axis := range label.SortedConfigurationAxes() { |
| 46 | configToLabels := label.ConfigurableValues[axis] |
| 47 | for config, labels := range configToLabels { |
| 48 | selectKey := axis.SelectKey(config) |
| 49 | ret[selectKey] = reflect.ValueOf(labels) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 53 | // if there is a select, use the base value as the conditions default value |
| 54 | if len(ret) > 0 { |
| 55 | ret[bazel.ConditionsDefaultSelectKey] = value |
| 56 | value = reflect.Zero(value.Type()) |
| 57 | } |
| 58 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 59 | return value, []selects{ret} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 62 | func getBoolValue(boolAttr bazel.BoolAttribute) (reflect.Value, []selects) { |
| 63 | value := reflect.ValueOf(boolAttr.Value) |
| 64 | if !boolAttr.HasConfigurableValues() { |
| 65 | return value, []selects{} |
| 66 | } |
| 67 | |
| 68 | ret := selects{} |
| 69 | for _, axis := range boolAttr.SortedConfigurationAxes() { |
| 70 | configToBools := boolAttr.ConfigurableValues[axis] |
| 71 | for config, bools := range configToBools { |
| 72 | selectKey := axis.SelectKey(config) |
| 73 | ret[selectKey] = reflect.ValueOf(bools) |
| 74 | } |
| 75 | } |
| 76 | // if there is a select, use the base value as the conditions default value |
| 77 | if len(ret) > 0 { |
| 78 | ret[bazel.ConditionsDefaultSelectKey] = value |
| 79 | value = reflect.Zero(value.Type()) |
| 80 | } |
| 81 | |
| 82 | return value, []selects{ret} |
| 83 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 84 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 85 | value := reflect.ValueOf(list.Value.Includes) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 86 | var ret []selects |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 87 | for _, axis := range list.SortedConfigurationAxes() { |
| 88 | configToLabels := list.ConfigurableValues[axis] |
| 89 | if !configToLabels.HasConfigurableValues() { |
| 90 | continue |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 91 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 92 | archSelects := map[string]reflect.Value{} |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 93 | defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 94 | // Skip empty list values unless ether EmitEmptyList is true, or these values differ from the default. |
| 95 | emitEmptyList := list.EmitEmptyList || len(defaultVal.Includes) > 0 |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 96 | for config, labels := range configToLabels { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 97 | // Omit any entries in the map which match the default value, for brevity. |
| 98 | if config != bazel.ConditionsDefaultConfigKey && labels.Equals(defaultVal) { |
| 99 | continue |
| 100 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 101 | selectKey := axis.SelectKey(config) |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 102 | if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 103 | archSelects[selectKey] = value |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 104 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 105 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 106 | if len(archSelects) > 0 { |
| 107 | ret = append(ret, archSelects) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 108 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 111 | return value, ret |
| 112 | } |
| 113 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 114 | func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) { |
| 115 | if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 116 | return true, reflect.ValueOf(list.Includes) |
| 117 | } else if len(list.Excludes) > 0 { |
| 118 | // if there is still an excludes -- we need to have an empty list for this select & use the |
| 119 | // value in conditions default Includes |
| 120 | return true, reflect.ValueOf([]string{}) |
| 121 | } |
| 122 | return false, reflect.Zero(reflect.TypeOf([]string{})) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 125 | var ( |
| 126 | emptyBazelList = "[]" |
| 127 | bazelNone = "None" |
| 128 | ) |
| 129 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 130 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 131 | // select statements. |
| 132 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 133 | var value reflect.Value |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 134 | var configurableAttrs []selects |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 135 | var defaultSelectValue *string |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 136 | var emitZeroValues bool |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 137 | // If true, print the default attribute value, even if the attribute is zero. |
| 138 | shouldPrintDefault := false |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 139 | switch list := v.(type) { |
| 140 | case bazel.StringListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 141 | value, configurableAttrs = getStringListValues(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 142 | defaultSelectValue = &emptyBazelList |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 143 | case bazel.LabelListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 144 | value, configurableAttrs = getLabelListValues(list) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 145 | emitZeroValues = list.EmitEmptyList |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 146 | defaultSelectValue = &emptyBazelList |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 147 | if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) { |
| 148 | shouldPrintDefault = true |
| 149 | } |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 150 | case bazel.LabelAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 151 | if err := list.Collapse(); err != nil { |
| 152 | return "", err |
| 153 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 154 | value, configurableAttrs = getLabelValue(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 155 | defaultSelectValue = &bazelNone |
| 156 | case bazel.BoolAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 157 | if err := list.Collapse(); err != nil { |
| 158 | return "", err |
| 159 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 160 | value, configurableAttrs = getBoolValue(list) |
| 161 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 162 | default: |
| 163 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 164 | } |
| 165 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 166 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 167 | ret := "" |
| 168 | if value.Kind() != reflect.Invalid { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 169 | s, err := prettyPrint(value, indent, false) // never emit zero values for the base value |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 170 | if err != nil { |
| 171 | return ret, err |
| 172 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 173 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 174 | ret += s |
| 175 | } |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 176 | // Convenience function to append selects components to an attribute value. |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 177 | appendSelects := func(selectsData selects, defaultValue *string, s string) (string, error) { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 178 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 179 | if err != nil { |
| 180 | return "", err |
| 181 | } |
| 182 | if s != "" && selectMap != "" { |
| 183 | s += " + " |
| 184 | } |
| 185 | s += selectMap |
| 186 | |
| 187 | return s, nil |
| 188 | } |
| 189 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 190 | for _, configurableAttr := range configurableAttrs { |
| 191 | ret, err = appendSelects(configurableAttr, defaultSelectValue, ret) |
| 192 | if err != nil { |
| 193 | return "", err |
| 194 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 195 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 196 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 197 | if ret == "" && shouldPrintDefault { |
| 198 | return *defaultSelectValue, nil |
| 199 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 200 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 204 | // to construct a select map for any kind of attribute type. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 205 | func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue *string, indent int, emitZeroValues bool) (string, error) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 206 | if selectMap == nil { |
| 207 | return "", nil |
| 208 | } |
| 209 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 210 | var selects string |
| 211 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 212 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 213 | // Handle default condition later. |
| 214 | continue |
| 215 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 216 | value := selectMap[selectKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 217 | if isZero(value) && !emitZeroValues && isZero(selectMap[bazel.ConditionsDefaultSelectKey]) { |
| 218 | // Ignore zero values to not generate empty lists. However, always note zero values if |
| 219 | // the default value is non-zero. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 220 | continue |
| 221 | } |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 222 | s, err := prettyPrintSelectEntry(value, selectKey, indent, true) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 223 | if err != nil { |
| 224 | return "", err |
| 225 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 226 | // s could still be an empty string, e.g. unset slices of structs with |
| 227 | // length of 0. |
| 228 | if s != "" { |
| 229 | selects += s + ",\n" |
| 230 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | if len(selects) == 0 { |
| 234 | // No conditions (or all values are empty lists), so no need for a map. |
| 235 | return "", nil |
| 236 | } |
| 237 | |
| 238 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 239 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 240 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 241 | |
| 242 | // Handle the default condition |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 243 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 244 | if err != nil { |
| 245 | return "", err |
| 246 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 247 | if s != "" { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 248 | // Print the custom default value. |
| 249 | ret += s |
| 250 | ret += ",\n" |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 251 | } else if defaultValue != nil { |
| 252 | // Print an explicit empty list (the default value) even if the value is |
| 253 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 254 | ret += fmt.Sprintf("%s\"%s\": %s,\n", starlark_fmt.Indention(indent+1), bazel.ConditionsDefaultSelectKey, *defaultValue) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 257 | ret += starlark_fmt.Indention(indent) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 258 | ret += "})" |
| 259 | |
| 260 | return ret, nil |
| 261 | } |
| 262 | |
| 263 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 264 | // with a provided key. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 265 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int, emitZeroValues bool) (string, error) { |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 266 | s := starlark_fmt.Indention(indent + 1) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 267 | v, err := prettyPrint(value, indent+1, emitZeroValues) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 268 | if err != nil { |
| 269 | return "", err |
| 270 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 271 | if v == "" { |
| 272 | return "", nil |
| 273 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 274 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 275 | return s, nil |
| 276 | } |