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