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 | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 20 | selectValues := make([]selects, 0) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 21 | archSelects := map[string]reflect.Value{} |
| 22 | for arch, selectKey := range bazel.PlatformArchMap { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 23 | archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 24 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 25 | if len(archSelects) > 0 { |
| 26 | selectValues = append(selectValues, archSelects) |
| 27 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 28 | |
| 29 | osSelects := map[string]reflect.Value{} |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 30 | osArchSelects := make([]selects, 0) |
| 31 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 32 | selectKey := bazel.PlatformOsMap[os] |
| 33 | osSelects[selectKey] = reflect.ValueOf(list.GetOsValueForTarget(os)) |
| 34 | archSelects := make(map[string]reflect.Value) |
| 35 | // TODO(b/187530594): Should we also check arch=CONDITIONS_DEFAULT? (not in AllArches) |
| 36 | for _, arch := range bazel.AllArches { |
| 37 | target := os + "_" + arch |
| 38 | selectKey := bazel.PlatformTargetMap[target] |
| 39 | archSelects[selectKey] = reflect.ValueOf(list.GetOsArchValueForTarget(os, arch)) |
| 40 | } |
| 41 | osArchSelects = append(osArchSelects, archSelects) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 42 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 43 | if len(osSelects) > 0 { |
| 44 | selectValues = append(selectValues, osSelects) |
| 45 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 46 | if len(osArchSelects) > 0 { |
| 47 | selectValues = append(selectValues, osArchSelects...) |
| 48 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 49 | |
Liz Kammer | e3e4a5f | 2021-05-10 11:39:53 -0400 | [diff] [blame] | 50 | for _, pv := range list.SortedProductVariables() { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 51 | s := make(selects) |
| 52 | if len(pv.Values) > 0 { |
| 53 | s[pv.SelectKey()] = reflect.ValueOf(pv.Values) |
| 54 | s[bazel.ConditionsDefaultSelectKey] = reflect.ValueOf([]string{}) |
| 55 | selectValues = append(selectValues, s) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return value, selectValues |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 62 | func getLabelValue(label bazel.LabelAttribute) (reflect.Value, []selects) { |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame^] | 63 | value := reflect.ValueOf(label.Value) |
| 64 | if !label.HasConfigurableValues() { |
| 65 | return value, []selects{} |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame^] | 68 | // Keep track of which arches and oses have been used in case we need to raise a warning |
| 69 | usedArches := make(map[string]bool) |
| 70 | usedOses := make(map[string]bool) |
| 71 | |
| 72 | archSelects := map[string]reflect.Value{} |
| 73 | for arch, selectKey := range bazel.PlatformArchMap { |
| 74 | archSelects[selectKey] = reflect.ValueOf(label.GetValueForArch(arch)) |
| 75 | if archSelects[selectKey].IsValid() && !isZero(archSelects[selectKey]) { |
| 76 | usedArches[arch] = true |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | osSelects := map[string]reflect.Value{} |
| 81 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 82 | selectKey := bazel.PlatformOsMap[os] |
| 83 | osSelects[selectKey] = reflect.ValueOf(label.GetOsValueForTarget(os)) |
| 84 | if osSelects[selectKey].IsValid() && !isZero(osSelects[selectKey]) { |
| 85 | usedOses[os] = true |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | osArchSelects := make([]selects, 0) |
| 90 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 91 | archSelects := make(map[string]reflect.Value) |
| 92 | // TODO(b/187530594): Should we also check arch=CONDITIONS_DEFAULT? (not in AllArches) |
| 93 | for _, arch := range bazel.AllArches { |
| 94 | target := os + "_" + arch |
| 95 | selectKey := bazel.PlatformTargetMap[target] |
| 96 | archSelects[selectKey] = reflect.ValueOf(label.GetOsArchValueForTarget(os, arch)) |
| 97 | if archSelects[selectKey].IsValid() && !isZero(archSelects[selectKey]) { |
| 98 | if _, ok := usedArches[arch]; ok { |
| 99 | fmt.Printf("WARNING: Same arch used twice in LabelAttribute select: arch '%s'\n", arch) |
| 100 | } |
| 101 | if _, ok := usedOses[os]; ok { |
| 102 | fmt.Printf("WARNING: Same os used twice in LabelAttribute select: os '%s'\n", os) |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | osArchSelects = append(osArchSelects, archSelects) |
| 107 | } |
| 108 | |
| 109 | // Because we have to return a single Label, we can only use one select statement |
| 110 | combinedSelects := map[string]reflect.Value{} |
| 111 | for k, v := range archSelects { |
| 112 | combinedSelects[k] = v |
| 113 | } |
| 114 | for k, v := range osSelects { |
| 115 | combinedSelects[k] = v |
| 116 | } |
| 117 | for _, osArchSelect := range osArchSelects { |
| 118 | for k, v := range osArchSelect { |
| 119 | combinedSelects[k] = v |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return value, []selects{combinedSelects} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 126 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 127 | value := reflect.ValueOf(list.Value.Includes) |
| 128 | if !list.HasConfigurableValues() { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 129 | return value, []selects{} |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | archSelects := map[string]reflect.Value{} |
| 133 | for arch, selectKey := range bazel.PlatformArchMap { |
| 134 | archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch).Includes) |
| 135 | } |
| 136 | |
| 137 | osSelects := map[string]reflect.Value{} |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 138 | osArchSelects := make([]selects, 0) |
| 139 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 140 | selectKey := bazel.PlatformOsMap[os] |
| 141 | osSelects[selectKey] = reflect.ValueOf(list.GetOsValueForTarget(os).Includes) |
| 142 | archSelects := make(map[string]reflect.Value) |
| 143 | // TODO(b/187530594): Should we also check arch=CONDITIOSN_DEFAULT? (not in AllArches) |
| 144 | for _, arch := range bazel.AllArches { |
| 145 | target := os + "_" + arch |
| 146 | selectKey := bazel.PlatformTargetMap[target] |
| 147 | archSelects[selectKey] = reflect.ValueOf(list.GetOsArchValueForTarget(os, arch).Includes) |
| 148 | } |
| 149 | osArchSelects = append(osArchSelects, archSelects) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 152 | var selects []selects |
| 153 | selects = append(selects, archSelects) |
| 154 | selects = append(selects, osSelects) |
| 155 | selects = append(selects, osArchSelects...) |
| 156 | return value, selects |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 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 |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 163 | var configurableAttrs []selects |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 164 | var defaultSelectValue string |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 165 | switch list := v.(type) { |
| 166 | case bazel.StringListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 167 | value, configurableAttrs = getStringListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 168 | defaultSelectValue = "[]" |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 169 | case bazel.LabelListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 170 | value, configurableAttrs = getLabelListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 171 | defaultSelectValue = "[]" |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 172 | case bazel.LabelAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 173 | value, configurableAttrs = getLabelValue(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 174 | defaultSelectValue = "None" |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 175 | default: |
| 176 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 177 | } |
| 178 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 179 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 180 | ret := "" |
| 181 | if value.Kind() != reflect.Invalid { |
| 182 | s, err := prettyPrint(value, indent) |
| 183 | if err != nil { |
| 184 | return ret, err |
| 185 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 186 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 187 | ret += s |
| 188 | } |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 189 | // Convenience function to append selects components to an attribute value. |
| 190 | appendSelects := func(selectsData selects, defaultValue, s string) (string, error) { |
| 191 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent) |
| 192 | if err != nil { |
| 193 | return "", err |
| 194 | } |
| 195 | if s != "" && selectMap != "" { |
| 196 | s += " + " |
| 197 | } |
| 198 | s += selectMap |
| 199 | |
| 200 | return s, nil |
| 201 | } |
| 202 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 203 | for _, configurableAttr := range configurableAttrs { |
| 204 | ret, err = appendSelects(configurableAttr, defaultSelectValue, ret) |
| 205 | if err != nil { |
| 206 | return "", err |
| 207 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 208 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 209 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 210 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 214 | // to construct a select map for any kind of attribute type. |
| 215 | 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] | 216 | if selectMap == nil { |
| 217 | return "", nil |
| 218 | } |
| 219 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 220 | // addConditionsDefault := false |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 221 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 222 | var selects string |
| 223 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 224 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 225 | // Handle default condition later. |
| 226 | continue |
| 227 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 228 | value := selectMap[selectKey] |
| 229 | if isZero(value) { |
| 230 | // Ignore zero values to not generate empty lists. |
| 231 | continue |
| 232 | } |
| 233 | s, err := prettyPrintSelectEntry(value, selectKey, indent) |
| 234 | if err != nil { |
| 235 | return "", err |
| 236 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 237 | // s could still be an empty string, e.g. unset slices of structs with |
| 238 | // length of 0. |
| 239 | if s != "" { |
| 240 | selects += s + ",\n" |
| 241 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | if len(selects) == 0 { |
| 245 | // No conditions (or all values are empty lists), so no need for a map. |
| 246 | return "", nil |
| 247 | } |
| 248 | |
| 249 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 250 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 251 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 252 | |
| 253 | // Handle the default condition |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 254 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 255 | if err != nil { |
| 256 | return "", err |
| 257 | } |
| 258 | if s == "" { |
| 259 | // Print an explicit empty list (the default value) even if the value is |
| 260 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 261 | 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] | 262 | } else { |
| 263 | // Print the custom default value. |
| 264 | ret += s |
| 265 | ret += ",\n" |
| 266 | } |
| 267 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 268 | ret += makeIndent(indent) |
| 269 | ret += "})" |
| 270 | |
| 271 | return ret, nil |
| 272 | } |
| 273 | |
| 274 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 275 | // with a provided key. |
| 276 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) { |
| 277 | s := makeIndent(indent + 1) |
| 278 | v, err := prettyPrint(value, indent+1) |
| 279 | if err != nil { |
| 280 | return "", err |
| 281 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 282 | if v == "" { |
| 283 | return "", nil |
| 284 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 285 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 286 | return s, nil |
| 287 | } |