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