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 | } |
| 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 Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 40 | func getStringListValues(list bazel.StringListAttribute) (reflect.Value, []selects, bool) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 41 | value := reflect.ValueOf(list.Value) |
Zi Wang | 0f82844 | 2022-12-28 11:18:11 -0800 | [diff] [blame^] | 42 | prepend := list.Prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 43 | if !list.HasConfigurableValues() { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 44 | return value, []selects{}, prepend |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 45 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 46 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 47 | 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 Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 54 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 55 | if len(archSelects) > 0 { |
| 56 | ret = append(ret, archSelects) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 60 | return value, ret, prepend |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 63 | func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 64 | value := reflect.ValueOf(label.Value) |
| 65 | if !label.HasConfigurableValues() { |
| 66 | return value, []selects{} |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 69 | 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 Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Liz Kammer | dff00ea | 2021-10-04 13:44:34 -0400 | [diff] [blame] | 78 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 84 | return value, []selects{ret} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 87 | func 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 Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 109 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 110 | value := reflect.ValueOf(list.Value.Includes) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 111 | var ret []selects |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 112 | for _, axis := range list.SortedConfigurationAxes() { |
| 113 | configToLabels := list.ConfigurableValues[axis] |
| 114 | if !configToLabels.HasConfigurableValues() { |
| 115 | continue |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 116 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 117 | archSelects := map[string]reflect.Value{} |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 118 | defaultVal := configToLabels[bazel.ConditionsDefaultConfigKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 119 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 121 | for config, labels := range configToLabels { |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 122 | // 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 Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 126 | selectKey := axis.SelectKey(config) |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 127 | if use, value := labelListSelectValue(selectKey, labels, emitEmptyList); use { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 128 | archSelects[selectKey] = value |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 129 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 130 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 131 | if len(archSelects) > 0 { |
| 132 | ret = append(ret, archSelects) |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 133 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 136 | return value, ret |
| 137 | } |
| 138 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 139 | func labelListSelectValue(selectKey string, list bazel.LabelList, emitEmptyList bool) (bool, reflect.Value) { |
| 140 | if selectKey == bazel.ConditionsDefaultSelectKey || emitEmptyList || len(list.Includes) > 0 { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame] | 141 | 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 Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 150 | var ( |
| 151 | emptyBazelList = "[]" |
| 152 | bazelNone = "None" |
| 153 | ) |
| 154 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 155 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 156 | // select statements. |
| 157 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 158 | var value reflect.Value |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 159 | var configurableAttrs []selects |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 160 | var prepend bool |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 161 | var defaultSelectValue *string |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 162 | var emitZeroValues bool |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 163 | // If true, print the default attribute value, even if the attribute is zero. |
| 164 | shouldPrintDefault := false |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 165 | 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] | 166 | case bazel.StringAttribute: |
| 167 | if err := list.Collapse(); err != nil { |
| 168 | return "", err |
| 169 | } |
| 170 | value, configurableAttrs = getStringValue(list) |
| 171 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 172 | case bazel.StringListAttribute: |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 173 | value, configurableAttrs, prepend = getStringListValues(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 174 | defaultSelectValue = &emptyBazelList |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 175 | case bazel.LabelListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 176 | value, configurableAttrs = getLabelListValues(list) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 177 | emitZeroValues = list.EmitEmptyList |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 178 | defaultSelectValue = &emptyBazelList |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 179 | if list.ForceSpecifyEmptyList && (!value.IsNil() || list.HasConfigurableValues()) { |
| 180 | shouldPrintDefault = true |
| 181 | } |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 182 | case bazel.LabelAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 183 | if err := list.Collapse(); err != nil { |
| 184 | return "", err |
| 185 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 186 | value, configurableAttrs = getLabelValue(list) |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 187 | defaultSelectValue = &bazelNone |
| 188 | case bazel.BoolAttribute: |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 189 | if err := list.Collapse(); err != nil { |
| 190 | return "", err |
| 191 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 192 | value, configurableAttrs = getBoolValue(list) |
| 193 | defaultSelectValue = &bazelNone |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 194 | default: |
| 195 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 196 | } |
| 197 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 198 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 199 | ret := "" |
| 200 | if value.Kind() != reflect.Invalid { |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 201 | 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] | 202 | if err != nil { |
| 203 | return ret, err |
| 204 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 205 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 206 | ret += s |
| 207 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 208 | // 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 Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 210 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent, emitZeroValues) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 211 | if err != nil { |
| 212 | return "", err |
| 213 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 214 | var left, right string |
| 215 | if prepend { |
| 216 | left, right = selectMap, s |
| 217 | } else { |
| 218 | left, right = s, selectMap |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 219 | } |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 220 | if left != "" && right != "" { |
| 221 | left += " + " |
| 222 | } |
| 223 | left += right |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 224 | |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 225 | return left, nil |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 226 | } |
| 227 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 228 | for _, configurableAttr := range configurableAttrs { |
Zi Wang | 1cb1180 | 2022-12-09 16:08:54 -0800 | [diff] [blame] | 229 | ret, err = concatenateSelects(configurableAttr, defaultSelectValue, ret, prepend) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 230 | if err != nil { |
| 231 | return "", err |
| 232 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 233 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 234 | |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 235 | if ret == "" && shouldPrintDefault { |
| 236 | return *defaultSelectValue, nil |
| 237 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 238 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 239 | } |
| 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 Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 243 | 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] | 244 | if selectMap == nil { |
| 245 | return "", nil |
| 246 | } |
| 247 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 248 | var selects string |
| 249 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 250 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 251 | // Handle default condition later. |
| 252 | continue |
| 253 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 254 | value := selectMap[selectKey] |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 255 | 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 Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 258 | continue |
| 259 | } |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 260 | s, err := prettyPrintSelectEntry(value, selectKey, indent, true) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 261 | if err != nil { |
| 262 | return "", err |
| 263 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 264 | // 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 Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 269 | } |
| 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 Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 277 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 278 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 279 | |
| 280 | // Handle the default condition |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 281 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent, emitZeroValues) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 282 | if err != nil { |
| 283 | return "", err |
| 284 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 285 | if s != "" { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 286 | // Print the custom default value. |
| 287 | ret += s |
| 288 | ret += ",\n" |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 289 | } 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 Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 292 | 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] | 293 | } |
| 294 | |
Liz Kammer | 72beb34 | 2022-02-03 08:42:10 -0500 | [diff] [blame] | 295 | ret += starlark_fmt.Indention(indent) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 296 | 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 Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 303 | 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] | 304 | s := starlark_fmt.Indention(indent + 1) |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 305 | v, err := prettyPrint(value, indent+1, emitZeroValues) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 306 | if err != nil { |
| 307 | return "", err |
| 308 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 309 | if v == "" { |
| 310 | return "", nil |
| 311 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 312 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 313 | return s, nil |
| 314 | } |