blob: 94e3e66bc4c59673b7560f3cd48de75a8224a836 [file] [log] [blame]
Colin Cross74d1ec02015-04-28 13:30:13 -07001package cc
2
3import (
Colin Cross5b529592017-05-09 13:34:34 -07004 "android/soong/android"
Jiyong Park6a43f042017-10-12 23:05:00 +09005 "io/ioutil"
6 "os"
Colin Cross74d1ec02015-04-28 13:30:13 -07007 "reflect"
8 "testing"
Jiyong Park6a43f042017-10-12 23:05:00 +09009
10 "github.com/google/blueprint/proptools"
Colin Cross74d1ec02015-04-28 13:30:13 -070011)
12
Jiyong Park6a43f042017-10-12 23:05:00 +090013var buildDir string
14
15func setUp() {
16 var err error
17 buildDir, err = ioutil.TempDir("", "soong_cc_test")
18 if err != nil {
19 panic(err)
20 }
21}
22
23func tearDown() {
24 os.RemoveAll(buildDir)
25}
26
27func TestMain(m *testing.M) {
28 run := func() int {
29 setUp()
30 defer tearDown()
31
32 return m.Run()
33 }
34
35 os.Exit(run())
36}
37
38func testCc(t *testing.T, bp string) *android.TestContext {
39 config := android.TestArchConfig(buildDir, nil)
40 config.ProductVariables.DeviceVndkVersion = proptools.StringPtr("current")
41
42 ctx := android.NewTestArchContext()
43 ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(libraryFactory))
44 ctx.RegisterModuleType("toolchain_library", android.ModuleFactoryAdaptor(toolchainLibraryFactory))
45 ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(llndkLibraryFactory))
46 ctx.PreDepsMutators(func(ctx android.RegisterMutatorsContext) {
47 ctx.BottomUp("image", vendorMutator).Parallel()
48 ctx.BottomUp("link", linkageMutator).Parallel()
49 ctx.BottomUp("vndk", vndkMutator).Parallel()
50 })
51 ctx.Register()
52
53 ctx.MockFileSystem(map[string][]byte{
54 "Android.bp": []byte(bp),
55 "foo.c": nil,
56 "bar.c": nil,
57 })
58
59 _, errs := ctx.ParseBlueprintsFiles("Android.bp")
60 fail(t, errs)
61 _, errs = ctx.PrepareBuildActions(config)
62 fail(t, errs)
63
64 return ctx
65}
66
67func TestVendorSrc(t *testing.T) {
68 ctx := testCc(t, `
69 cc_library {
70 name: "libTest",
71 srcs: ["foo.c"],
72 no_libgcc : true,
73 nocrt : true,
74 system_shared_libs : [],
75 vendor_available: true,
76 target: {
77 vendor: {
78 srcs: ["bar.c"],
79 },
80 },
81 }
82 toolchain_library {
83 name: "libatomic",
84 vendor_available: true,
85 }
86 toolchain_library {
87 name: "libcompiler_rt-extras",
88 vendor_available: true,
89 }
90 cc_library {
91 name: "libc",
92 no_libgcc : true,
93 nocrt : true,
94 system_shared_libs: [],
95 }
96 llndk_library {
97 name: "libc",
98 symbol_file: "",
99 }
100 cc_library {
101 name: "libm",
102 no_libgcc : true,
103 nocrt : true,
104 system_shared_libs: [],
105 }
106 llndk_library {
107 name: "libm",
108 symbol_file: "",
109 }
110 cc_library {
111 name: "libdl",
112 no_libgcc : true,
113 nocrt : true,
114 system_shared_libs: [],
115 }
116 llndk_library {
117 name: "libdl",
118 symbol_file: "",
119 }
120 `)
121
122 ld := ctx.ModuleForTests("libTest", "android_arm_armv7-a-neon_vendor_shared").Rule("ld")
123 var objs []string
124 for _, o := range ld.Inputs {
125 objs = append(objs, o.Base())
126 }
127 if len(objs) != 2 {
128 t.Errorf("inputs of libTest is expected to 2, but was %d.", len(objs))
129 }
130 if objs[0] != "foo.o" || objs[1] != "bar.o" {
131 t.Errorf("inputs of libTest must be []string{\"foo.o\", \"bar.o\"}, but was %#v.", objs)
132 }
133}
134
Colin Crossdd84e052017-05-17 13:44:16 -0700135var firstUniqueElementsTestCases = []struct {
136 in []string
137 out []string
138}{
139 {
140 in: []string{"a"},
141 out: []string{"a"},
142 },
143 {
144 in: []string{"a", "b"},
145 out: []string{"a", "b"},
146 },
147 {
148 in: []string{"a", "a"},
149 out: []string{"a"},
150 },
151 {
152 in: []string{"a", "b", "a"},
153 out: []string{"a", "b"},
154 },
155 {
156 in: []string{"b", "a", "a"},
157 out: []string{"b", "a"},
158 },
159 {
160 in: []string{"a", "a", "b"},
161 out: []string{"a", "b"},
162 },
163 {
164 in: []string{"a", "b", "a", "b"},
165 out: []string{"a", "b"},
166 },
167 {
168 in: []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
169 out: []string{"liblog", "libdl", "libc++", "libc", "libm"},
170 },
171}
172
173func TestFirstUniqueElements(t *testing.T) {
174 for _, testCase := range firstUniqueElementsTestCases {
175 out := firstUniqueElements(testCase.in)
176 if !reflect.DeepEqual(out, testCase.out) {
177 t.Errorf("incorrect output:")
178 t.Errorf(" input: %#v", testCase.in)
179 t.Errorf(" expected: %#v", testCase.out)
180 t.Errorf(" got: %#v", out)
181 }
182 }
183}
184
Colin Cross74d1ec02015-04-28 13:30:13 -0700185var lastUniqueElementsTestCases = []struct {
186 in []string
187 out []string
188}{
189 {
190 in: []string{"a"},
191 out: []string{"a"},
192 },
193 {
194 in: []string{"a", "b"},
195 out: []string{"a", "b"},
196 },
197 {
198 in: []string{"a", "a"},
199 out: []string{"a"},
200 },
201 {
202 in: []string{"a", "b", "a"},
203 out: []string{"b", "a"},
204 },
205 {
206 in: []string{"b", "a", "a"},
207 out: []string{"b", "a"},
208 },
209 {
210 in: []string{"a", "a", "b"},
211 out: []string{"a", "b"},
212 },
213 {
214 in: []string{"a", "b", "a", "b"},
215 out: []string{"a", "b"},
216 },
217 {
218 in: []string{"liblog", "libdl", "libc++", "libdl", "libc", "libm"},
219 out: []string{"liblog", "libc++", "libdl", "libc", "libm"},
220 },
221}
222
223func TestLastUniqueElements(t *testing.T) {
224 for _, testCase := range lastUniqueElementsTestCases {
225 out := lastUniqueElements(testCase.in)
226 if !reflect.DeepEqual(out, testCase.out) {
227 t.Errorf("incorrect output:")
228 t.Errorf(" input: %#v", testCase.in)
229 t.Errorf(" expected: %#v", testCase.out)
230 t.Errorf(" got: %#v", out)
231 }
232 }
233}
Colin Cross0af4b842015-04-30 16:36:18 -0700234
235var (
236 str11 = "01234567891"
237 str10 = str11[:10]
238 str9 = str11[:9]
239 str5 = str11[:5]
240 str4 = str11[:4]
241)
242
243var splitListForSizeTestCases = []struct {
244 in []string
245 out [][]string
246 size int
247}{
248 {
249 in: []string{str10},
250 out: [][]string{{str10}},
251 size: 10,
252 },
253 {
254 in: []string{str9},
255 out: [][]string{{str9}},
256 size: 10,
257 },
258 {
259 in: []string{str5},
260 out: [][]string{{str5}},
261 size: 10,
262 },
263 {
264 in: []string{str11},
265 out: nil,
266 size: 10,
267 },
268 {
269 in: []string{str10, str10},
270 out: [][]string{{str10}, {str10}},
271 size: 10,
272 },
273 {
274 in: []string{str9, str10},
275 out: [][]string{{str9}, {str10}},
276 size: 10,
277 },
278 {
279 in: []string{str10, str9},
280 out: [][]string{{str10}, {str9}},
281 size: 10,
282 },
283 {
284 in: []string{str5, str4},
285 out: [][]string{{str5, str4}},
286 size: 10,
287 },
288 {
289 in: []string{str5, str4, str5},
290 out: [][]string{{str5, str4}, {str5}},
291 size: 10,
292 },
293 {
294 in: []string{str5, str4, str5, str4},
295 out: [][]string{{str5, str4}, {str5, str4}},
296 size: 10,
297 },
298 {
299 in: []string{str5, str4, str5, str5},
300 out: [][]string{{str5, str4}, {str5}, {str5}},
301 size: 10,
302 },
303 {
304 in: []string{str5, str5, str5, str4},
305 out: [][]string{{str5}, {str5}, {str5, str4}},
306 size: 10,
307 },
308 {
309 in: []string{str9, str11},
310 out: nil,
311 size: 10,
312 },
313 {
314 in: []string{str11, str9},
315 out: nil,
316 size: 10,
317 },
318}
319
320func TestSplitListForSize(t *testing.T) {
321 for _, testCase := range splitListForSizeTestCases {
Colin Cross5b529592017-05-09 13:34:34 -0700322 out, _ := splitListForSize(android.PathsForTesting(testCase.in), testCase.size)
323
324 var outStrings [][]string
325
326 if len(out) > 0 {
327 outStrings = make([][]string, len(out))
328 for i, o := range out {
329 outStrings[i] = o.Strings()
330 }
331 }
332
333 if !reflect.DeepEqual(outStrings, testCase.out) {
Colin Cross0af4b842015-04-30 16:36:18 -0700334 t.Errorf("incorrect output:")
335 t.Errorf(" input: %#v", testCase.in)
336 t.Errorf(" size: %d", testCase.size)
337 t.Errorf(" expected: %#v", testCase.out)
Colin Cross5b529592017-05-09 13:34:34 -0700338 t.Errorf(" got: %#v", outStrings)
Colin Cross0af4b842015-04-30 16:36:18 -0700339 }
340 }
341}