blob: b2b3379fcb37eafa7020d90f494a56adc9a33021 [file] [log] [blame]
Jingwen Chen5d864492021-02-24 07:20:12 -05001package bp2build
2
Jingwen Chen91220d72021-03-24 02:18:33 -04003import (
4 "android/soong/android"
5 "android/soong/bazel"
6 "fmt"
7 "reflect"
8)
Jingwen Chen5d864492021-02-24 07:20:12 -05009
10// Configurability support for bp2build.
11
Jingwen Chenc1c26502021-04-05 10:35:13 +000012type selects map[string]reflect.Value
13
14func getStringListValues(list bazel.StringListAttribute) (reflect.Value, selects, selects) {
15 value := reflect.ValueOf(list.Value)
16 if !list.HasConfigurableValues() {
17 return value, nil, nil
Jingwen Chen5d864492021-02-24 07:20:12 -050018 }
Jingwen Chen91220d72021-03-24 02:18:33 -040019
Jingwen Chen91220d72021-03-24 02:18:33 -040020 archSelects := map[string]reflect.Value{}
21 for arch, selectKey := range bazel.PlatformArchMap {
Jingwen Chenc1c26502021-04-05 10:35:13 +000022 archSelects[selectKey] = reflect.ValueOf(list.GetValueForArch(arch))
Jingwen Chen91220d72021-03-24 02:18:33 -040023 }
Jingwen Chenc1c26502021-04-05 10:35:13 +000024
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
33func 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.
54func 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 Chen91220d72021-03-24 02:18:33 -040073 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 Chen91220d72021-03-24 02:18:33 -040080 selectMap, err = prettyPrintSelectMap(osSelects, "[]", indent)
Jingwen Chenc1c26502021-04-05 10:35:13 +000081 if err != nil {
82 return "", err
83 }
84 ret += selectMap
85
86 return ret, err
Jingwen Chen91220d72021-03-24 02:18:33 -040087}
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.
91func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue string, indent int) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +000092 if selectMap == nil {
93 return "", nil
94 }
95
Jingwen Chen91220d72021-03-24 02:18:33 -040096 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.
128func 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}