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