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 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 16 | func 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 Kammer | 9d2d410 | 2022-12-21 14:51:37 -0500 | [diff] [blame] | 31 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 32 | // if there is a select, use the base value as the conditions default value |
| 33 | if len(ret) > 0 { |
Liz Kammer | 9d2d410 | 2022-12-21 14:51:37 -0500 | [diff] [blame] | 34 | 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 Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | return value, []selects{ret} |
| 41 | } |
| 42 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 43 | func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects, bool) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 44 | value := reflect.ValueOf(list.Value) |
Zi Wang | 0f82844 | 2022-12-28 11:18:11 -0800 | [diff] [blame] | 45 | prepend := list.Prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 46 | if !list.HasConfigurableValues() { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 47 | return value, []selects{}, prepend |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 48 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 49 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 50 | 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 Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 57 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 58 | if len(archSelects) > 0 { |
| 59 | ret = append(ret, archSelects) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 63 | return value, ret, prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 66 | func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 67 | value := reflect.ValueOf(label.Value) |
| 68 | if !label.HasConfigurableValues() { |
| 69 | return value, []selects{} |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 72 | 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 Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 81 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 87 | return value, []selects{ret} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 90 | func 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 Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 112 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects, bool) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 113 | value := reflect.ValueOf(list.Value.Includes) |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 114 | prepend := list.Prepend |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 115 | var ret []selects |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 116 | for _, axis := range list.SortedConfigurationAxes() { |
| 117 | configToLabels := list.ConfigurableValues[axis] |
| 118 | if !configToLabels.HasConfigurableValues() { |
| 119 | continue |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 120 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 121 | archSelects := map[string]reflect.Value{} |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 122 | defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 123 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 125 | for config, labels := range configToLabels { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 126 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 130 | selectKey := axis.SelectKey(config) |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 131 | if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 132 | archSelects[selectKey] = value |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 133 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 134 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 135 | if len(archSelects) > 0 { |
| 136 | ret = append(ret, archSelects) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 137 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 140 | return value, ret, prepend |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 143 | func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) { |
| 144 | if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 145 | 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 154 | var ( |
| 155 | emptyBazelList = "[]" |
| 156 | bazelNone = "None" |
| 157 | ) |
| 158 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 159 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 160 | // select statements. |
| 161 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 162 | var value reflect.Value |
Chris Parsons | 7b3289b | 2023-01-26 17:30:44 -0500 | [diff] [blame] | 163 | // configurableAttrs is the list of individual select statements to be |
| 164 | // concatenated together. These select statements should be along different |
| 165 | // axes. For example, one element may be |
| 166 | // `select({"//color:red": "one", "//color:green": "two"})`, and the second |
| 167 | // element may be `select({"//animal:cat": "three", "//animal:dog": "four"}). |
| 168 | // These selects should be sorted by axis identifier. |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 169 | var configurableAttrs []selects |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 170 | var prepend bool |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 171 | var defaultSelectValue *string |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 172 | var emitZeroValues bool |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 173 | // If true, print the default attribute value, even if the attribute is zero. |
| 174 | shouldPrintDefault := false |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 175 | switch list := v.(type) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 3a019a6 | 2022-06-23 16:02:44 +0000 | [diff] [blame] | 176 | case bazel.StringAttribute: |
| 177 | if err := list.Collapse(); err != nil { |
| 178 | return "", err |
| 179 | } |
| 180 | value, configurableAttrs = getStringValue(list) |
| 181 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 182 | case bazel.StringListAttribute: |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 183 | value, configurableAttrs, prepend = getStringListValues(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 184 | defaultSelectValue = &emptyBazelList |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 185 | case bazel.LabelListAttribute: |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 186 | value, configurableAttrs, prepend = getLabelListValues(list) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 187 | emitZeroValues = list.EmitEmptyList |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 188 | defaultSelectValue = &emptyBazelList |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 189 | if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) { |
| 190 | shouldPrintDefault = true |
| 191 | } |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 192 | case bazel.LabelAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 193 | if err := list.Collapse(); err != nil { |
| 194 | return "", err |
| 195 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 196 | value, configurableAttrs = getLabelValue(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 197 | defaultSelectValue = &bazelNone |
| 198 | case bazel.BoolAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 199 | if err := list.Collapse(); err != nil { |
| 200 | return "", err |
| 201 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 202 | value, configurableAttrs = getBoolValue(list) |
| 203 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 204 | default: |
| 205 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 206 | } |
| 207 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 208 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 209 | ret := "" |
| 210 | if value.Kind() != reflect.Invalid { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 211 | 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] | 212 | if err != nil { |
| 213 | return ret, err |
| 214 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 215 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 216 | ret += s |
| 217 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 218 | // Convenience function to prepend/append selects components to an attribute value. |
| 219 | concatenateSelects := func(selectsData selects, defaultValue *string, s string, prepend bool) (string, error) { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 220 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 221 | if err != nil { |
| 222 | return "", err |
| 223 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 224 | var left, right string |
| 225 | if prepend { |
| 226 | left, right = selectMap, s |
| 227 | } else { |
| 228 | left, right = s, selectMap |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 229 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 230 | if left != "" && right != "" { |
| 231 | left += " + " |
| 232 | } |
| 233 | left += right |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 234 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 235 | return left, nil |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 236 | } |
| 237 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 238 | for _, configurableAttr := range configurableAttrs { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 239 | ret, err = concatenateSelects(configurableAttr, defaultSelectValue, ret, prepend) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 240 | if err != nil { |
| 241 | return "", err |
| 242 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 243 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 244 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 245 | if ret == "" && shouldPrintDefault { |
| 246 | return *defaultSelectValue, nil |
| 247 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 248 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 252 | // to construct a select map for any kind of attribute type. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 253 | 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] | 254 | if selectMap == nil { |
| 255 | return "", nil |
| 256 | } |
| 257 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 258 | var selects string |
Cole Faust | 18994c7 | 2023-02-28 16:02:16 -0800 | [diff] [blame] | 259 | for _, selectKey := range android.SortedKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 260 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 261 | // Handle default condition later. |
| 262 | continue |
| 263 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 264 | value := selectMap[selectKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 265 | if isZero(value) && !emitZeroValues && isZero(selectMap[bazel.ConditionsDefaultSelectKey]) { |
| 266 | // Ignore zero values to not generate empty lists. However, always note zero values if |
| 267 | // the default value is non-zero. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 268 | continue |
| 269 | } |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 270 | s, err := prettyPrintSelectEntry(value, selectKey, indent, true) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 271 | if err != nil { |
| 272 | return "", err |
| 273 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 274 | // s could still be an empty string, e.g. unset slices of structs with |
| 275 | // length of 0. |
| 276 | if s != "" { |
| 277 | selects += s + ",\n" |
| 278 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | if len(selects) == 0 { |
Spandan Das | 921af32 | 2023-04-26 02:56:37 +0000 | [diff] [blame] | 282 | // If there is a default value, and there are no selects for this axis, print that without any selects. |
| 283 | if val, exists := selectMap[bazel.ConditionsDefaultSelectKey]; exists { |
| 284 | return prettyPrint(val, indent, emitZeroValues) |
| 285 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 286 | // No conditions (or all values are empty lists), so no need for a map. |
| 287 | return "", nil |
| 288 | } |
| 289 | |
| 290 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 291 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 292 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 293 | |
| 294 | // Handle the default condition |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 295 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 296 | if err != nil { |
| 297 | return "", err |
| 298 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 299 | if s != "" { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 300 | // Print the custom default value. |
| 301 | ret += s |
| 302 | ret += ",\n" |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 303 | } else if defaultValue != nil { |
| 304 | // Print an explicit empty list (the default value) even if the value is |
| 305 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 306 | 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] | 307 | } |
| 308 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 309 | ret += starlark_fmt.Indention(indent) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 310 | ret += "})" |
| 311 | |
| 312 | return ret, nil |
| 313 | } |
| 314 | |
| 315 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 316 | // with a provided key. |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 317 | 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] | 318 | s := starlark_fmt.Indention(indent + 1) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 319 | v, err := prettyPrint(value, indent+1, emitZeroValues) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 320 | if err != nil { |
| 321 | return "", err |
| 322 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 323 | if v == "" { |
| 324 | return "", nil |
| 325 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 326 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 327 | return s, nil |
| 328 | } |