blob: 050679b53cb5d5cd7f0bb23bdf2443e0df88875b [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
Lukacs T. Berki1353e592021-04-30 15:35:09 +020033func getLabelValue(label bazel.LabelAttribute) (reflect.Value, selects, selects) {
34 value := reflect.ValueOf(label.Value)
35 return value, nil, nil
36}
37
Jingwen Chenc1c26502021-04-05 10:35:13 +000038func 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.
59func prettyPrintAttribute(v bazel.Attribute, indent int) (string, error) {
60 var value reflect.Value
61 var archSelects, osSelects selects
Jingwen Chenc1c26502021-04-05 10:35:13 +000062 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. Berki1353e592021-04-30 15:35:09 +020067 case bazel.LabelAttribute:
68 value, archSelects, osSelects = getLabelValue(list)
Jingwen Chenc1c26502021-04-05 10:35:13 +000069 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 Chen63930982021-03-24 10:04:33 -040078 // 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 Chen91220d72021-03-24 02:18:33 -040093 if err != nil {
94 return "", err
95 }
Jingwen Chen91220d72021-03-24 02:18:33 -040096
Jingwen Chen63930982021-03-24 10:04:33 -040097 ret, err = appendSelects(osSelects, "[]", ret)
Jingwen Chenc1c26502021-04-05 10:35:13 +000098 return ret, err
Jingwen Chen91220d72021-03-24 02:18:33 -040099}
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.
103func prettyPrintSelectMap(selectMap map[string]reflect.Value, defaultValue string, indent int) (string, error) {
Jingwen Chenc1c26502021-04-05 10:35:13 +0000104 if selectMap == nil {
105 return "", nil
106 }
107
Jingwen Chene32e9e02021-04-23 09:17:24 +0000108 // addConditionsDefault := false
109 conditionsDefaultKey := bazel.PlatformArchMap[bazel.CONDITIONS_DEFAULT]
110
Jingwen Chen91220d72021-03-24 02:18:33 -0400111 var selects string
112 for _, selectKey := range android.SortedStringKeys(selectMap) {
Jingwen Chene32e9e02021-04-23 09:17:24 +0000113 if selectKey == conditionsDefaultKey {
114 // Handle default condition later.
115 continue
116 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400117 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 Chened9c17d2021-04-13 07:14:55 +0000126 // 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 Chen91220d72021-03-24 02:18:33 -0400131 }
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 Chen63930982021-03-24 10:04:33 -0400139 ret := "select({\n"
Jingwen Chen91220d72021-03-24 02:18:33 -0400140 ret += selects
Jingwen Chene32e9e02021-04-23 09:17:24 +0000141
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 Chen91220d72021-03-24 02:18:33 -0400157 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.
165func 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 Chened9c17d2021-04-13 07:14:55 +0000171 if v == "" {
172 return "", nil
173 }
Jingwen Chen91220d72021-03-24 02:18:33 -0400174 s += fmt.Sprintf("\"%s\": %s", key, v)
175 return s, nil
176}