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 | |
| 14 | func getStringListValues(list bazel.StringListAttribute) (reflect.Value, selects, selects) { |
| 15 | value := reflect.ValueOf(list.Value) |
| 16 | if !list.HasConfigurableValues() { |
| 17 | return value, nil, nil |
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 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 20 | archSelects := map[string]reflect.Value{} |
| 21 | for arch, selectKey := range bazel.PlatformArchMap { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 22 | archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 23 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 24 | |
| 25 | osSelects := map[string]reflect.Value{} |
| 26 | for os, selectKey := range bazel.PlatformOsMap { |
| 27 | osSelects[selectKey] = reflect.ValueOf(list.GetValueForOS(os)) |
| 28 | } |
| 29 | |
| 30 | return value, archSelects, osSelects |
| 31 | } |
| 32 | |
| 33 | func getLabelListValues(list bazel.LabelListAttribute) (reflect.Value, selects, selects) { |
| 34 | value := reflect.ValueOf(list.Value.Includes) |
| 35 | if !list.HasConfigurableValues() { |
| 36 | return value, nil, nil |
| 37 | } |
| 38 | |
| 39 | archSelects := map[string]reflect.Value{} |
| 40 | for arch, selectKey := range bazel.PlatformArchMap { |
| 41 | archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch).Includes) |
| 42 | } |
| 43 | |
| 44 | osSelects := map[string]reflect.Value{} |
| 45 | for os, selectKey := range bazel.PlatformOsMap { |
| 46 | osSelects[selectKey] = reflect.ValueOf(list.GetValueForOS(os).Includes) |
| 47 | } |
| 48 | |
| 49 | return value, archSelects, osSelects |
| 50 | } |
| 51 | |
| 52 | // prettyPrintAttribute converts an Attribute to its Bazel syntax. May contain |
| 53 | // select statements. |
| 54 | func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) { |
| 55 | var value reflect.Value |
| 56 | var archSelects, osSelects selects |
| 57 | |
| 58 | switch list := v.(type) { |
| 59 | case bazel.StringListAttribute: |
| 60 | value, archSelects, osSelects = getStringListValues(list) |
| 61 | case bazel.LabelListAttribute: |
| 62 | value, archSelects, osSelects = getLabelListValues(list) |
| 63 | default: |
| 64 | return "", fmt.Errorf("Not a supported Bazel attribute type: %s", v) |
| 65 | } |
| 66 | |
| 67 | ret, err := prettyPrint(value, indent) |
| 68 | if err != nil { |
| 69 | return ret, err |
| 70 | } |
| 71 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 72 | // Convenience function to append selects components to an attribute value. |
| 73 | appendSelects := func(selectsData selects, defaultValue, s string) (string, error) { |
| 74 | selectMap, err := prettyPrintSelectMap(selectsData, defaultValue, indent) |
| 75 | if err != nil { |
| 76 | return "", err |
| 77 | } |
| 78 | if s != "" && selectMap != "" { |
| 79 | s += " + " |
| 80 | } |
| 81 | s += selectMap |
| 82 | |
| 83 | return s, nil |
| 84 | } |
| 85 | |
| 86 | ret, err = appendSelects(archSelects, "[]", ret) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 87 | if err != nil { |
| 88 | return "", err |
| 89 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 90 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 91 | ret, err = appendSelects(osSelects, "[]", ret) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 92 | return ret, err |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 96 | // to construct a select map for any kind of attribute type. |
| 97 | 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] | 98 | if selectMap == nil { |
| 99 | return "", nil |
| 100 | } |
| 101 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 102 | // addConditionsDefault := false |
| 103 | conditionsDefaultKey := bazel.PlatformArchMap[bazel.CONDITIONS_DEFAULT] |
| 104 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 105 | var selects string |
| 106 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 107 | if selectKey == conditionsDefaultKey { |
| 108 | // Handle default condition later. |
| 109 | continue |
| 110 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 111 | value := selectMap[selectKey] |
| 112 | if isZero(value) { |
| 113 | // Ignore zero values to not generate empty lists. |
| 114 | continue |
| 115 | } |
| 116 | s, err := prettyPrintSelectEntry(value, selectKey, indent) |
| 117 | if err != nil { |
| 118 | return "", err |
| 119 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 120 | // s could still be an empty string, e.g. unset slices of structs with |
| 121 | // length of 0. |
| 122 | if s != "" { |
| 123 | selects += s + ",\n" |
| 124 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | if len(selects) == 0 { |
| 128 | // No conditions (or all values are empty lists), so no need for a map. |
| 129 | return "", nil |
| 130 | } |
| 131 | |
| 132 | // Create the map. |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 133 | ret := "select({\n" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 134 | ret += selects |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 135 | |
| 136 | // Handle the default condition |
| 137 | s, err := prettyPrintSelectEntry(selectMap[conditionsDefaultKey], conditionsDefaultKey, indent) |
| 138 | if err != nil { |
| 139 | return "", err |
| 140 | } |
| 141 | if s == "" { |
| 142 | // Print an explicit empty list (the default value) even if the value is |
| 143 | // empty, to avoid errors about not finding a configuration that matches. |
| 144 | ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), "//conditions:default", defaultValue) |
| 145 | } else { |
| 146 | // Print the custom default value. |
| 147 | ret += s |
| 148 | ret += ",\n" |
| 149 | } |
| 150 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 151 | ret += makeIndent(indent) |
| 152 | ret += "})" |
| 153 | |
| 154 | return ret, nil |
| 155 | } |
| 156 | |
| 157 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 158 | // with a provided key. |
| 159 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) { |
| 160 | s := makeIndent(indent + 1) |
| 161 | v, err := prettyPrint(value, indent+1) |
| 162 | if err != nil { |
| 163 | return "", err |
| 164 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 165 | if v == "" { |
| 166 | return "", nil |
| 167 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 168 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 169 | return s, nil |
| 170 | } |