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) { |
Lukacs T. Berki | 56bb083 | 2021-05-12 12:36:45 +0200 | [diff] [blame] | 63 | var value reflect.Value |
| 64 | var archSelects selects |
| 65 | |
| 66 | if label.HasConfigurableValues() { |
| 67 | archSelects = map[string]reflect.Value{} |
| 68 | for arch, selectKey := range bazel.PlatformArchMap { |
| 69 | archSelects[selectKey] = reflect.ValueOf(label.GetValueForArch(arch)) |
| 70 | } |
| 71 | } else { |
| 72 | value = reflect.ValueOf(label.Value) |
| 73 | } |
| 74 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 75 | return value, []selects{archSelects} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 76 | } |
| 77 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 78 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, []selects) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 79 | value := reflect.ValueOf(list.Value.Includes) |
| 80 | if !list.HasConfigurableValues() { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 81 | return value, []selects{} |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | archSelects := map[string]reflect.Value{} |
| 85 | for arch, selectKey := range bazel.PlatformArchMap { |
| 86 | archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch).Includes) |
| 87 | } |
| 88 | |
| 89 | osSelects := map[string]reflect.Value{} |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 90 | osArchSelects := make([]selects, 0) |
| 91 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 92 | selectKey := bazel.PlatformOsMap[os] |
| 93 | osSelects[selectKey] = reflect.ValueOf(list.GetOsValueForTarget(os).Includes) |
| 94 | archSelects := make(map[string]reflect.Value) |
| 95 | // TODO(b/187530594): Should we also check arch=CONDITIOSN_DEFAULT? (not in AllArches) |
| 96 | for _, arch := range bazel.AllArches { |
| 97 | target := os + "_" + arch |
| 98 | selectKey := bazel.PlatformTargetMap[target] |
| 99 | archSelects[selectKey] = reflect.ValueOf(list.GetOsArchValueForTarget(os, arch).Includes) |
| 100 | } |
| 101 | osArchSelects = append(osArchSelects, archSelects) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 104 | var selects []selects |
| 105 | selects = append(selects, archSelects) |
| 106 | selects = append(selects, osSelects) |
| 107 | selects = append(selects, osArchSelects...) |
| 108 | return value, selects |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 112 | // select statements. |
| 113 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 114 | var value reflect.Value |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 115 | var configurableAttrs []selects |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 116 | var defaultSelectValue string |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 117 | switch list := v.(type) { |
| 118 | case bazel.StringListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 119 | value, configurableAttrs = getStringListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 120 | defaultSelectValue = "[]" |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 121 | case bazel.LabelListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 122 | value, configurableAttrs = getLabelListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 123 | defaultSelectValue = "[]" |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 124 | case bazel.LabelAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 125 | value, configurableAttrs = getLabelValue(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 126 | defaultSelectValue = "None" |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 127 | default: |
| 128 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 129 | } |
| 130 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 131 | var err error |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 132 | ret := "" |
| 133 | if value.Kind() != reflect.Invalid { |
| 134 | s, err := prettyPrint(value, indent) |
| 135 | if err != nil { |
| 136 | return ret, err |
| 137 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 138 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 139 | ret += s |
| 140 | } |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 141 | // Convenience function to append selects components to an attribute value. |
| 142 | appendSelects := func(selectsData selects, defaultValue, s string) (string, error) { |
| 143 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent) |
| 144 | if err != nil { |
| 145 | return "", err |
| 146 | } |
| 147 | if s != "" && selectMap != "" { |
| 148 | s += " + " |
| 149 | } |
| 150 | s += selectMap |
| 151 | |
| 152 | return s, nil |
| 153 | } |
| 154 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 155 | for _, configurableAttr := range configurableAttrs { |
| 156 | ret, err = appendSelects(configurableAttr, defaultSelectValue, ret) |
| 157 | if err != nil { |
| 158 | return "", err |
| 159 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 160 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 161 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 162 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 166 | // to construct a select map for any kind of attribute type. |
| 167 | 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] | 168 | if selectMap == nil { |
| 169 | return "", nil |
| 170 | } |
| 171 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 172 | // addConditionsDefault := false |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 173 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 174 | var selects string |
| 175 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 176 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 177 | // Handle default condition later. |
| 178 | continue |
| 179 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 180 | value := selectMap[selectKey] |
| 181 | if isZero(value) { |
| 182 | // Ignore zero values to not generate empty lists. |
| 183 | continue |
| 184 | } |
| 185 | s, err := prettyPrintSelectEntry(value, selectKey, indent) |
| 186 | if err != nil { |
| 187 | return "", err |
| 188 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 189 | // s could still be an empty string, e.g. unset slices of structs with |
| 190 | // length of 0. |
| 191 | if s != "" { |
| 192 | selects += s + ",\n" |
| 193 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | if len(selects) == 0 { |
| 197 | // No conditions (or all values are empty lists), so no need for a map. |
| 198 | return "", nil |
| 199 | } |
| 200 | |
| 201 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 202 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 203 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 204 | |
| 205 | // Handle the default condition |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 206 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 207 | if err != nil { |
| 208 | return "", err |
| 209 | } |
| 210 | if s == "" { |
| 211 | // Print an explicit empty list (the default value) even if the value is |
| 212 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 213 | 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] | 214 | } else { |
| 215 | // Print the custom default value. |
| 216 | ret += s |
| 217 | ret += ",\n" |
| 218 | } |
| 219 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 220 | ret += makeIndent(indent) |
| 221 | ret += "})" |
| 222 | |
| 223 | return ret, nil |
| 224 | } |
| 225 | |
| 226 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 227 | // with a provided key. |
| 228 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) { |
| 229 | s := makeIndent(indent + 1) |
| 230 | v, err := prettyPrint(value, indent+1) |
| 231 | if err != nil { |
| 232 | return "", err |
| 233 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 234 | if v == "" { |
| 235 | return "", nil |
| 236 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 237 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 238 | return s, nil |
| 239 | } |