blob: 081a1ca2169e86d31ae4a88ac5216b8a13df8e05 [file] [log] [blame]
Dan Willemsenf33877b2015-06-23 23:34:49 -07001package main
2
3import (
Dan Willemsenead184a2015-06-23 22:47:38 -07004 "bytes"
Dan Willemsenf33877b2015-06-23 23:34:49 -07005 "strings"
6 "testing"
Dan Willemsenead184a2015-06-23 22:47:38 -07007 "unicode"
Dan Willemsenf33877b2015-06-23 23:34:49 -07008
9 bpparser "github.com/google/blueprint/parser"
10)
11
12var valueTestCases = []struct {
13 blueprint string
14 expected string
15}{
16 {
17 blueprint: `test = false`,
18 expected: `false`,
19 },
20 {
Dan Willemsenf33877b2015-06-23 23:34:49 -070021 blueprint: `test = "string"`,
22 expected: `string`,
23 },
24 {
25 blueprint: `test = ["a", "b"]`,
26 expected: `\
Colin Crossb3245e92015-06-30 16:27:57 -070027 a \
28 b
29 `,
Dan Willemsenf33877b2015-06-23 23:34:49 -070030 },
31}
32
33func TestValueToString(t *testing.T) {
34 for _, testCase := range valueTestCases {
35 blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil)
36 if len(errs) > 0 {
37 t.Errorf("Failed to read blueprint: %q", errs)
38 }
39
Colin Crossb0931242015-06-29 14:18:27 -070040 str, err := valueToString(blueprint.Defs[0].(*bpparser.Assignment).Value)
41 if err != nil {
42 t.Error(err.Error())
43 }
Dan Willemsenead184a2015-06-23 22:47:38 -070044 expect(t, testCase.blueprint, testCase.expected, str)
45 }
46}
47
48var moduleTestCases = []struct {
49 blueprint string
50 androidmk string
51}{
52 // Target-only
53 {
54 blueprint: `cc_library_shared { name: "test", }`,
55 androidmk: `include $(CLEAR_VARS)
56 LOCAL_MODULE := test
57 include $(BUILD_SHARED_LIBRARY)`,
58 },
59 // Host-only
60 {
61 blueprint: `cc_library_host_shared { name: "test", }`,
62 androidmk: `include $(CLEAR_VARS)
63 LOCAL_MODULE := test
64 include $(BUILD_HOST_SHARED_LIBRARY)`,
65 },
66 // Target and Host
67 {
68 blueprint: `cc_library_shared { name: "test", host_supported: true, }`,
69 androidmk: `include $(CLEAR_VARS)
70 LOCAL_MODULE := test
71 include $(BUILD_SHARED_LIBRARY)
72
73 include $(CLEAR_VARS)
74 LOCAL_MODULE := test
75 include $(BUILD_HOST_SHARED_LIBRARY)`,
76 },
Dan Willemsen49f50452015-06-24 14:56:00 -070077 // Static and Shared
78 {
Dan Willemsen26173162015-07-06 13:36:50 -070079 blueprint: `cc_library { name: "test", srcs: ["a"], }`,
Dan Willemsen49f50452015-06-24 14:56:00 -070080 androidmk: `include $(CLEAR_VARS)
81 LOCAL_MODULE := test
Dan Willemsen26173162015-07-06 13:36:50 -070082 LOCAL_WHOLE_STATIC_LIBRARIES := \
83 test
Dan Willemsen49f50452015-06-24 14:56:00 -070084 include $(BUILD_SHARED_LIBRARY)
85
86 include $(CLEAR_VARS)
87 LOCAL_MODULE := test
Dan Willemsen26173162015-07-06 13:36:50 -070088 LOCAL_SRC_FILES := \
89 a
Dan Willemsen49f50452015-06-24 14:56:00 -070090 include $(BUILD_STATIC_LIBRARY)`,
91 },
Dan Willemsen3a4045d2015-06-24 15:37:17 -070092 // Static and Shared / Target and Host
93 {
94 blueprint: `cc_library { name: "test", host_supported: true, }`,
95 androidmk: `include $(CLEAR_VARS)
96 LOCAL_MODULE := test
Dan Willemsen26173162015-07-06 13:36:50 -070097 LOCAL_WHOLE_STATIC_LIBRARIES := \
98 test
Dan Willemsen3a4045d2015-06-24 15:37:17 -070099 include $(BUILD_SHARED_LIBRARY)
100
101 include $(CLEAR_VARS)
102 LOCAL_MODULE := test
103 include $(BUILD_STATIC_LIBRARY)
104
105 include $(CLEAR_VARS)
106 LOCAL_MODULE := test
Dan Willemsen26173162015-07-06 13:36:50 -0700107 LOCAL_WHOLE_STATIC_LIBRARIES := \
108 test
Dan Willemsen3a4045d2015-06-24 15:37:17 -0700109 include $(BUILD_HOST_SHARED_LIBRARY)
110
111 include $(CLEAR_VARS)
112 LOCAL_MODULE := test
113 include $(BUILD_HOST_STATIC_LIBRARY)`,
114 },
Dan Willemsen27012122015-06-26 17:40:54 -0700115 // Static and Shared properties
116 {
117 blueprint: `cc_library {
118 name: "test",
119 srcs: ["a"],
120 static: { srcs: ["c"], static_libs: ["l"], },
121 shared: { srcs: ["b"], },
122 multilib: { lib32: { shared: { cflags: ["f"], }, }, },
123 }`,
124 androidmk: `include $(CLEAR_VARS)
125 LOCAL_MODULE := test
126 LOCAL_SRC_FILES := \
127 a \
128 b
129 LOCAL_CFLAGS_32 := \
130 f
131 include $(BUILD_SHARED_LIBRARY)
132
133 include $(CLEAR_VARS)
134 LOCAL_MODULE := test
135 LOCAL_SRC_FILES := \
136 a \
137 c
138 LOCAL_STATIC_LIBRARIES := \
139 l
140 include $(BUILD_STATIC_LIBRARY)`,
141 },
Dan Willemsen26173162015-07-06 13:36:50 -0700142 // Static and Shared properties, use whole static lib, but add extra shared srcs
143 {
144 blueprint: `cc_library {
145 name: "test",
146 srcs: ["a"],
147 shared: { srcs: ["b"], },
148 }`,
149 androidmk: `include $(CLEAR_VARS)
150 LOCAL_MODULE := test
151 LOCAL_WHOLE_STATIC_LIBRARIES := \
152 test
153 LOCAL_SRC_FILES := \
154 b
155 include $(BUILD_SHARED_LIBRARY)
156
157 include $(CLEAR_VARS)
158 LOCAL_MODULE := test
159 LOCAL_SRC_FILES := \
160 a
161 include $(BUILD_STATIC_LIBRARY)`,
162 },
Colin Crossb1a66c02015-06-29 16:24:57 -0700163 // Manual translation
164 {
165 blueprint: `/* Android.mk:start
166 # Manual translation
167 Android.mk:end */
168 cc_library { name: "test", host_supported: true, }`,
169 androidmk: `# Manual translation`,
170 },
171 // Ignored translation
172 {
173 blueprint: `/* Android.mk:ignore */
174 cc_library { name: "test", host_supported: true, }`,
175 androidmk: ``,
176 },
Dan Willemsenead184a2015-06-23 22:47:38 -0700177}
178
179func TestModules(t *testing.T) {
180 for _, testCase := range moduleTestCases {
181 blueprint, errs := bpparser.Parse("", strings.NewReader(testCase.blueprint), nil)
182 if len(errs) > 0 {
183 t.Errorf("Failed to read blueprint: %q", errs)
Dan Willemsenf33877b2015-06-23 23:34:49 -0700184 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700185
186 buf := &bytes.Buffer{}
187 writer := &androidMkWriter{
188 blueprint: blueprint,
189 path: "",
Colin Crossb0931242015-06-29 14:18:27 -0700190 Writer: buf,
Dan Willemsenead184a2015-06-23 22:47:38 -0700191 }
192
193 module := blueprint.Defs[0].(*bpparser.Module)
Colin Crossb0931242015-06-29 14:18:27 -0700194 err := writer.handleModule(module)
195 if err != nil {
196 t.Errorf("Unexpected error %s", err.Error())
197 }
Dan Willemsenead184a2015-06-23 22:47:38 -0700198
199 expect(t, testCase.blueprint, testCase.androidmk, buf.String())
200 }
201}
202
203// Trim left whitespace, and any trailing newlines. Leave inner blank lines and
204// right whitespace so that we can still check line continuations are correct
205func trim(str string) string {
206 var list []string
207 for _, s := range strings.Split(str, "\n") {
208 list = append(list, strings.TrimLeftFunc(s, unicode.IsSpace))
209 }
210 return strings.TrimRight(strings.Join(list, "\n"), "\n")
211}
212
213func expect(t *testing.T, testCase string, expected string, out string) {
214 expected = trim(expected)
215 out = trim(out)
216 if expected != out {
217 sep := " "
218 if strings.Index(expected, "\n") != -1 || strings.Index(out, "\n") != -1 {
219 sep = "\n"
220 }
221
222 t.Errorf("test case: %s", testCase)
223 t.Errorf("unexpected difference:")
224 t.Errorf(" expected:%s%s", sep, expected)
225 t.Errorf(" got:%s%s", sep, out)
Dan Willemsenf33877b2015-06-23 23:34:49 -0700226 }
227}