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