blob: 20274b2baf6791f74980f17fa5d7435a55a4428c [file] [log] [blame]
Colin Cross33b2fb72019-05-14 14:07:01 -07001// Copyright 2019 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package cc
16
17import (
Martin Stjernholm837ee1a2020-08-20 02:54:52 +010018 "path/filepath"
Colin Cross33b2fb72019-05-14 14:07:01 -070019 "testing"
20
21 "android/soong/android"
22
23 "github.com/google/blueprint"
24)
25
Paul Duffin6a1160e2021-03-07 15:47:42 +000026var prebuiltFixtureFactory = ccFixtureFactory.Extend(
27 android.PrepareForTestWithAndroidMk,
28)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010029
Paul Duffin6a1160e2021-03-07 15:47:42 +000030func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext {
31 result := prebuiltFixtureFactory.Extend(
32 fs.AddToFixture(),
33 ).Extend(handlers...).RunTestWithBp(t, bp)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010034
Paul Duffin6a1160e2021-03-07 15:47:42 +000035 return result.TestContext
Martin Stjernholmadeb0882020-04-01 23:02:57 +010036}
37
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070038type configCustomizer func(config android.Config)
39
Colin Cross33b2fb72019-05-14 14:07:01 -070040func TestPrebuilt(t *testing.T) {
41 bp := `
42 cc_library {
43 name: "liba",
44 }
45
46 cc_prebuilt_library_shared {
47 name: "liba",
48 srcs: ["liba.so"],
49 }
50
51 cc_library {
52 name: "libb",
53 }
54
55 cc_prebuilt_library_static {
56 name: "libb",
57 srcs: ["libb.a"],
58 }
59
60 cc_library_shared {
61 name: "libd",
62 }
63
64 cc_prebuilt_library_shared {
65 name: "libd",
66 srcs: ["libd.so"],
67 }
68
69 cc_library_static {
70 name: "libe",
71 }
72
73 cc_prebuilt_library_static {
74 name: "libe",
75 srcs: ["libe.a"],
76 }
Paul Duffinbce90da2020-03-12 20:17:14 +000077
78 cc_library {
79 name: "libf",
80 }
81
82 cc_prebuilt_library {
83 name: "libf",
84 static: {
85 srcs: ["libf.a"],
86 },
87 shared: {
88 srcs: ["libf.so"],
89 },
90 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000091
92 cc_object {
93 name: "crtx",
94 }
95
96 cc_prebuilt_object {
97 name: "crtx",
98 srcs: ["crtx.o"],
99 }
Colin Cross33b2fb72019-05-14 14:07:01 -0700100 `
101
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100102 ctx := testPrebuilt(t, bp, map[string][]byte{
103 "liba.so": nil,
104 "libb.a": nil,
105 "libd.so": nil,
106 "libe.a": nil,
107 "libf.a": nil,
108 "libf.so": nil,
109 "crtx.o": nil,
110 })
Colin Cross33b2fb72019-05-14 14:07:01 -0700111
112 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -0800113 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
114 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
115 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
116 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000117 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
118 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000119 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700120
Colin Cross7113d202019-11-20 16:39:12 -0800121 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
122 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
123 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
124 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000125 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
126 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000127 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700128
129 hasDep := func(m android.Module, wantDep android.Module) bool {
130 t.Helper()
131 var found bool
132 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
133 if dep == wantDep {
134 found = true
135 }
136 })
137 return found
138 }
139
140 if !hasDep(liba, prebuiltLiba) {
141 t.Errorf("liba missing dependency on prebuilt_liba")
142 }
143
144 if !hasDep(libb, prebuiltLibb) {
145 t.Errorf("libb missing dependency on prebuilt_libb")
146 }
147
148 if !hasDep(libd, prebuiltLibd) {
149 t.Errorf("libd missing dependency on prebuilt_libd")
150 }
151
152 if !hasDep(libe, prebuiltLibe) {
153 t.Errorf("libe missing dependency on prebuilt_libe")
154 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000155
156 if !hasDep(libfStatic, prebuiltLibfStatic) {
157 t.Errorf("libf static missing dependency on prebuilt_libf")
158 }
159
160 if !hasDep(libfShared, prebuiltLibfShared) {
161 t.Errorf("libf shared missing dependency on prebuilt_libf")
162 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000163
164 if !hasDep(crtx, prebuiltCrtx) {
165 t.Errorf("crtx missing dependency on prebuilt_crtx")
166 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000167}
168
Paul Duffinbce90da2020-03-12 20:17:14 +0000169func TestPrebuiltLibraryShared(t *testing.T) {
170 ctx := testPrebuilt(t, `
171 cc_prebuilt_library_shared {
172 name: "libtest",
173 srcs: ["libf.so"],
174 strip: {
175 none: true,
176 },
177 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100178 `, map[string][]byte{
179 "libf.so": nil,
180 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000181
182 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800183 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000184}
185
186func TestPrebuiltLibraryStatic(t *testing.T) {
187 ctx := testPrebuilt(t, `
188 cc_prebuilt_library_static {
189 name: "libtest",
190 srcs: ["libf.a"],
191 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100192 `, map[string][]byte{
193 "libf.a": nil,
194 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000195
196 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800197 assertString(t, static.OutputFile().Path().Base(), "libf.a")
Paul Duffinbce90da2020-03-12 20:17:14 +0000198}
199
200func TestPrebuiltLibrary(t *testing.T) {
201 ctx := testPrebuilt(t, `
202 cc_prebuilt_library {
203 name: "libtest",
204 static: {
205 srcs: ["libf.a"],
206 },
207 shared: {
208 srcs: ["libf.so"],
209 },
210 strip: {
211 none: true,
212 },
213 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100214 `, map[string][]byte{
215 "libf.a": nil,
216 "libf.so": nil,
217 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000218
219 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800220 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000221
222 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800223 assertString(t, static.OutputFile().Path().Base(), "libf.a")
224}
225
226func TestPrebuiltLibraryStem(t *testing.T) {
227 ctx := testPrebuilt(t, `
228 cc_prebuilt_library {
229 name: "libfoo",
230 stem: "libbar",
231 static: {
232 srcs: ["libfoo.a"],
233 },
234 shared: {
235 srcs: ["libfoo.so"],
236 },
237 strip: {
238 none: true,
239 },
240 }
241 `, map[string][]byte{
242 "libfoo.a": nil,
243 "libfoo.so": nil,
244 })
245
246 static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
247 assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
248
249 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
250 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
251}
252
253func TestPrebuiltLibrarySharedStem(t *testing.T) {
254 ctx := testPrebuilt(t, `
255 cc_prebuilt_library_shared {
256 name: "libfoo",
257 stem: "libbar",
258 srcs: ["libfoo.so"],
259 strip: {
260 none: true,
261 },
262 }
263 `, map[string][]byte{
264 "libfoo.so": nil,
265 })
266
267 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
268 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
Colin Cross33b2fb72019-05-14 14:07:01 -0700269}
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100270
271func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
Martin Stjernholm6a9a1462020-09-15 02:56:19 +0100272 if android.BuildOs != android.Linux {
273 t.Skipf("Skipping host prebuilt testing that is only supported on %s not %s", android.Linux, android.BuildOs)
274 }
275
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100276 ctx := testPrebuilt(t, `
277 cc_prebuilt_library_shared {
278 name: "libfoo",
279 device_supported: false,
280 host_supported: true,
281 target: {
282 linux_glibc_x86_64: {
283 srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
284 },
285 },
286 }
287
288 cc_prebuilt_binary {
289 name: "foo",
290 device_supported: false,
291 host_supported: true,
292 shared_libs: ["libfoo"],
293 target: {
294 linux_glibc_x86_64: {
295 srcs: ["linux_glibc_x86_64/bin/foo"],
296 },
297 },
298 }
299 `, map[string][]byte{
300 "libfoo.so": nil,
301 "foo": nil,
302 })
303
304 fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
305 assertString(t, fooRule.Output.String(),
306 filepath.Join(buildDir, ".intermediates/foo/linux_glibc_x86_64/foo"))
307 assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
308
309 var libfooDep android.Path
310 for _, dep := range fooRule.Implicits {
311 if dep.Base() == "libfoo.so" {
312 libfooDep = dep
313 break
314 }
315 }
316 assertString(t, libfooDep.String(),
317 filepath.Join(buildDir, ".intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so"))
318}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700319
320func TestPrebuiltLibrarySanitized(t *testing.T) {
321 bp := `cc_prebuilt_library {
322 name: "libtest",
323 static: {
324 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
325 },
326 shared: {
327 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
328 },
329 }
330 cc_prebuilt_library_static {
331 name: "libtest_static",
332 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
333 }
334 cc_prebuilt_library_shared {
335 name: "libtest_shared",
336 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
337 }`
338
339 fs := map[string][]byte{
340 "libf.a": nil,
341 "libf.hwasan.a": nil,
342 "libf.so": nil,
343 "hwasan/libf.so": nil,
344 }
345
346 // Without SANITIZE_TARGET.
347 ctx := testPrebuilt(t, bp, fs)
348
349 shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
350 assertString(t, shared_rule.Input.String(), "libf.so")
351
352 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
353 assertString(t, static.OutputFile().Path().Base(), "libf.a")
354
355 shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
356 assertString(t, shared_rule2.Input.String(), "libf.so")
357
358 static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module)
359 assertString(t, static2.OutputFile().Path().Base(), "libf.a")
360
361 // With SANITIZE_TARGET=hwaddress
Paul Duffin6a1160e2021-03-07 15:47:42 +0000362 ctx = testPrebuilt(t, bp, fs,
363 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
364 variables.SanitizeDevice = []string{"hwaddress"}
365 }),
366 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700367
368 shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
369 assertString(t, shared_rule.Input.String(), "hwasan/libf.so")
370
371 static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
372 assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a")
373
374 shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
375 assertString(t, shared_rule2.Input.String(), "hwasan/libf.so")
376
377 static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
378 assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a")
379}