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 | |
| 72 | // Create the selects for arch specific values. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 73 | selectMap, err := prettyPrintSelectMap(archSelects, "[]", indent) |
| 74 | if err != nil { |
| 75 | return "", err |
| 76 | } |
| 77 | ret += selectMap |
| 78 | |
| 79 | // Create the selects for target os specific values. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 80 | selectMap, err = prettyPrintSelectMap(osSelects, "[]", indent) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame^] | 81 | if err != nil { |
| 82 | return "", err |
| 83 | } |
| 84 | ret += selectMap |
| 85 | |
| 86 | return ret, err |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // prettyPrintSelectMap converts a map of select keys to reflected Values as a generic way |
| 90 | // to construct a select map for any kind of attribute type. |
| 91 | 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^] | 92 | if selectMap == nil { |
| 93 | return "", nil |
| 94 | } |
| 95 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 96 | var selects string |
| 97 | for _, selectKey := range android.SortedStringKeys(selectMap) { |
| 98 | value := selectMap[selectKey] |
| 99 | if isZero(value) { |
| 100 | // Ignore zero values to not generate empty lists. |
| 101 | continue |
| 102 | } |
| 103 | s, err := prettyPrintSelectEntry(value, selectKey, indent) |
| 104 | if err != nil { |
| 105 | return "", err |
| 106 | } |
| 107 | selects += s + ",\n" |
| 108 | } |
| 109 | |
| 110 | if len(selects) == 0 { |
| 111 | // No conditions (or all values are empty lists), so no need for a map. |
| 112 | return "", nil |
| 113 | } |
| 114 | |
| 115 | // Create the map. |
| 116 | ret := " + select({\n" |
| 117 | ret += selects |
| 118 | // default condition comes last. |
| 119 | ret += fmt.Sprintf("%s\"%s\": %s,\n", makeIndent(indent+1), "//conditions:default", defaultValue) |
| 120 | ret += makeIndent(indent) |
| 121 | ret += "})" |
| 122 | |
| 123 | return ret, nil |
| 124 | } |
| 125 | |
| 126 | // prettyPrintSelectEntry converts a reflect.Value into an entry in a select map |
| 127 | // with a provided key. |
| 128 | func prettyPrintSelectEntry(value reflect.Value, key string, indent int) (string, error) { |
| 129 | s := makeIndent(indent + 1) |
| 130 | v, err := prettyPrint(value, indent+1) |
| 131 | if err != nil { |
| 132 | return "", err |
| 133 | } |
| 134 | s += fmt.Sprintf("\"%s\": %s", key, v) |
| 135 | return s, nil |
| 136 | } |