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