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 | } |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 83 | var ret []selects |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 84 | |
| 85 | archSelects := map[string]reflect.Value{} |
| 86 | for arch, selectKey := range bazel.PlatformArchMap { |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 87 | if use, value := labelListSelectValue(selectKey, list.GetValueForArch(arch)); use { |
| 88 | archSelects[selectKey] = value |
| 89 | } |
| 90 | } |
| 91 | if len(archSelects) > 0 { |
| 92 | ret = append(ret, archSelects) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | osSelects := map[string]reflect.Value{} |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 96 | osArchSelects := []selects{} |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 97 | for _, os := range android.SortedStringKeys(bazel.PlatformOsMap) { |
| 98 | selectKey := bazel.PlatformOsMap[os] |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 99 | if use, value := labelListSelectValue(selectKey, list.GetOsValueForTarget(os)); use { |
| 100 | osSelects[selectKey] = value |
| 101 | } |
| 102 | selects := make(map[string]reflect.Value) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 103 | // TODO(b/187530594): Should we also check arch=CONDITIOSN_DEFAULT? (not in AllArches) |
| 104 | for _, arch := range bazel.AllArches { |
| 105 | target := os + "_" + arch |
| 106 | selectKey := bazel.PlatformTargetMap[target] |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 107 | if use, value := labelListSelectValue(selectKey, list.GetOsArchValueForTarget(os, arch)); use { |
| 108 | selects[selectKey] = value |
| 109 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 110 | } |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 111 | if len(selects) > 0 { |
| 112 | osArchSelects = append(osArchSelects, selects) |
| 113 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 114 | } |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 115 | if len(osSelects) > 0 { |
| 116 | ret = append(ret, osSelects) |
| 117 | } |
| 118 | ret = append(ret, osArchSelects...) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 119 | |
Liz Kammer | 2b07ec7 | 2021-05-26 15:08:27 -0400 | [diff] [blame^] | 120 | return value, ret |
| 121 | } |
| 122 | |
| 123 | func labelListSelectValue(selectKey string, list bazel.LabelList) (bool, reflect.Value) { |
| 124 | if selectKey == bazel.ConditionsDefaultSelectKey || len(list.Includes) > 0 { |
| 125 | return true, reflect.ValueOf(list.Includes) |
| 126 | } else if len(list.Excludes) > 0 { |
| 127 | // if there is still an excludes -- we need to have an empty list for this select & use the |
| 128 | // value in conditions default Includes |
| 129 | return true, reflect.ValueOf([]string{}) |
| 130 | } |
| 131 | return false, reflect.Zero(reflect.TypeOf([]string{})) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 135 | // select statements. |
| 136 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 137 | var value reflect.Value |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 138 | var configurableAttrs []selects |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 139 | var defaultSelectValue string |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 140 | switch list := v.(type) { |
| 141 | case bazel.StringListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 142 | value, configurableAttrs = getStringListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 143 | defaultSelectValue = "[]" |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 144 | case bazel.LabelListAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 145 | value, configurableAttrs = getLabelListValues(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 146 | defaultSelectValue = "[]" |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 147 | case bazel.LabelAttribute: |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 148 | value, configurableAttrs = getLabelValue(list) |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 149 | defaultSelectValue = "None" |
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. |
| 165 | appendSelects := func(selectsData selects, defaultValue, s string) (string, error) { |
| 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 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 185 | return ret, nil |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 189 | // to construct a select map for any kind of attribute type. |
| 190 | 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] | 191 | if selectMap == nil { |
| 192 | return "", nil |
| 193 | } |
| 194 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 195 | // addConditionsDefault := false |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 196 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 197 | var selects string |
| 198 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 199 | if selectKey == bazel.ConditionsDefaultSelectKey { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 200 | // Handle default condition later. |
| 201 | continue |
| 202 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 203 | value := selectMap[selectKey] |
| 204 | if isZero(value) { |
| 205 | // Ignore zero values to not generate empty lists. |
| 206 | continue |
| 207 | } |
| 208 | s, err := prettyPrintSelectEntry(value, selectKey, indent) |
| 209 | if err != nil { |
| 210 | return "", err |
| 211 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 212 | // s could still be an empty string, e.g. unset slices of structs with |
| 213 | // length of 0. |
| 214 | if s != "" { |
| 215 | selects += s + ",\n" |
| 216 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if len(selects) == 0 { |
| 220 | // No conditions (or all values are empty lists), so no need for a map. |
| 221 | return "", nil |
| 222 | } |
| 223 | |
| 224 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 225 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 226 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 227 | |
| 228 | // Handle the default condition |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 229 | s, err := prettyPrintSelectEntry(selectMap[bazel.ConditionsDefaultSelectKey], bazel.ConditionsDefaultSelectKey, indent) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 230 | if err != nil { |
| 231 | return "", err |
| 232 | } |
| 233 | if s == "" { |
| 234 | // Print an explicit empty list (the default value) even if the value is |
| 235 | // empty, to avoid errors about not finding a configuration that matches. |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 236 | 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] | 237 | } else { |
| 238 | // Print the custom default value. |
| 239 | ret += s |
| 240 | ret += ",\n" |
| 241 | } |
| 242 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 243 | ret += makeIndent(indent) |
| 244 | ret += "})" |
| 245 | |
| 246 | return ret, nil |
| 247 | } |
| 248 | |
| 249 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 250 | // with a provided key. |
| 251 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) { |
| 252 | s := makeIndent(indent + 1) |
| 253 | v, err := prettyPrint(value, indent+1) |
| 254 | if err != nil { |
| 255 | return "", err |
| 256 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 257 | if v == "" { |
| 258 | return "", nil |
| 259 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 260 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 261 | return s, nil |
| 262 | } |