blob: c8f8103b04264ecb64dfc84da57055d351d8c829 [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 (
Colin Cross0c66bc62021-07-20 09:47:41 -070018 "runtime"
Colin Cross33b2fb72019-05-14 14:07:01 -070019 "testing"
20
21 "android/soong/android"
22
23 "github.com/google/blueprint"
24)
25
Paul Duffin8567f222021-03-23 00:02:06 +000026var prepareForPrebuiltTest = android.GroupFixturePreparers(
27 prepareForCcTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000028 android.PrepareForTestWithAndroidMk,
29)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010030
Paul Duffin6a1160e2021-03-07 15:47:42 +000031func testPrebuilt(t *testing.T, bp string, fs android.MockFS, handlers ...android.FixturePreparer) *android.TestContext {
Paul Duffin8567f222021-03-23 00:02:06 +000032 result := android.GroupFixturePreparers(
33 prepareForPrebuiltTest,
Paul Duffin6a1160e2021-03-07 15:47:42 +000034 fs.AddToFixture(),
Paul Duffin8567f222021-03-23 00:02:06 +000035 android.GroupFixturePreparers(handlers...),
36 ).RunTestWithBp(t, bp)
Martin Stjernholmadeb0882020-04-01 23:02:57 +010037
Paul Duffin6a1160e2021-03-07 15:47:42 +000038 return result.TestContext
Martin Stjernholmadeb0882020-04-01 23:02:57 +010039}
40
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070041type configCustomizer func(config android.Config)
42
Colin Cross33b2fb72019-05-14 14:07:01 -070043func TestPrebuilt(t *testing.T) {
44 bp := `
45 cc_library {
46 name: "liba",
47 }
48
49 cc_prebuilt_library_shared {
50 name: "liba",
51 srcs: ["liba.so"],
52 }
53
54 cc_library {
55 name: "libb",
56 }
57
58 cc_prebuilt_library_static {
59 name: "libb",
60 srcs: ["libb.a"],
61 }
62
63 cc_library_shared {
64 name: "libd",
65 }
66
67 cc_prebuilt_library_shared {
68 name: "libd",
69 srcs: ["libd.so"],
70 }
71
72 cc_library_static {
73 name: "libe",
74 }
75
76 cc_prebuilt_library_static {
77 name: "libe",
78 srcs: ["libe.a"],
79 }
Paul Duffinbce90da2020-03-12 20:17:14 +000080
81 cc_library {
82 name: "libf",
83 }
84
85 cc_prebuilt_library {
86 name: "libf",
87 static: {
88 srcs: ["libf.a"],
89 },
90 shared: {
91 srcs: ["libf.so"],
92 },
93 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000094
95 cc_object {
96 name: "crtx",
97 }
98
99 cc_prebuilt_object {
100 name: "crtx",
101 srcs: ["crtx.o"],
102 }
Colin Cross33b2fb72019-05-14 14:07:01 -0700103 `
104
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100105 ctx := testPrebuilt(t, bp, map[string][]byte{
106 "liba.so": nil,
107 "libb.a": nil,
108 "libd.so": nil,
109 "libe.a": nil,
110 "libf.a": nil,
111 "libf.so": nil,
112 "crtx.o": nil,
113 })
Colin Cross33b2fb72019-05-14 14:07:01 -0700114
115 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -0800116 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
117 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
118 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
119 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000120 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
121 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000122 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700123
Colin Cross7113d202019-11-20 16:39:12 -0800124 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
125 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
126 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
127 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000128 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
129 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000130 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700131
132 hasDep := func(m android.Module, wantDep android.Module) bool {
133 t.Helper()
134 var found bool
135 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
136 if dep == wantDep {
137 found = true
138 }
139 })
140 return found
141 }
142
143 if !hasDep(liba, prebuiltLiba) {
144 t.Errorf("liba missing dependency on prebuilt_liba")
145 }
146
147 if !hasDep(libb, prebuiltLibb) {
148 t.Errorf("libb missing dependency on prebuilt_libb")
149 }
150
151 if !hasDep(libd, prebuiltLibd) {
152 t.Errorf("libd missing dependency on prebuilt_libd")
153 }
154
155 if !hasDep(libe, prebuiltLibe) {
156 t.Errorf("libe missing dependency on prebuilt_libe")
157 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000158
159 if !hasDep(libfStatic, prebuiltLibfStatic) {
160 t.Errorf("libf static missing dependency on prebuilt_libf")
161 }
162
163 if !hasDep(libfShared, prebuiltLibfShared) {
164 t.Errorf("libf shared missing dependency on prebuilt_libf")
165 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000166
167 if !hasDep(crtx, prebuiltCrtx) {
168 t.Errorf("crtx missing dependency on prebuilt_crtx")
169 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000170}
171
Paul Duffinbce90da2020-03-12 20:17:14 +0000172func TestPrebuiltLibraryShared(t *testing.T) {
173 ctx := testPrebuilt(t, `
174 cc_prebuilt_library_shared {
175 name: "libtest",
176 srcs: ["libf.so"],
177 strip: {
178 none: true,
179 },
180 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100181 `, map[string][]byte{
182 "libf.so": nil,
183 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000184
185 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800186 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000187}
188
189func TestPrebuiltLibraryStatic(t *testing.T) {
190 ctx := testPrebuilt(t, `
191 cc_prebuilt_library_static {
192 name: "libtest",
193 srcs: ["libf.a"],
194 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100195 `, map[string][]byte{
196 "libf.a": nil,
197 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000198
199 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800200 assertString(t, static.OutputFile().Path().Base(), "libf.a")
Paul Duffinbce90da2020-03-12 20:17:14 +0000201}
202
203func TestPrebuiltLibrary(t *testing.T) {
204 ctx := testPrebuilt(t, `
205 cc_prebuilt_library {
206 name: "libtest",
207 static: {
208 srcs: ["libf.a"],
209 },
210 shared: {
211 srcs: ["libf.so"],
212 },
213 strip: {
214 none: true,
215 },
216 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100217 `, map[string][]byte{
218 "libf.a": nil,
219 "libf.so": nil,
220 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000221
222 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800223 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000224
225 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800226 assertString(t, static.OutputFile().Path().Base(), "libf.a")
227}
228
229func TestPrebuiltLibraryStem(t *testing.T) {
230 ctx := testPrebuilt(t, `
231 cc_prebuilt_library {
232 name: "libfoo",
233 stem: "libbar",
234 static: {
235 srcs: ["libfoo.a"],
236 },
237 shared: {
238 srcs: ["libfoo.so"],
239 },
240 strip: {
241 none: true,
242 },
243 }
244 `, map[string][]byte{
245 "libfoo.a": nil,
246 "libfoo.so": nil,
247 })
248
249 static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
250 assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
251
252 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
253 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
254}
255
256func TestPrebuiltLibrarySharedStem(t *testing.T) {
257 ctx := testPrebuilt(t, `
258 cc_prebuilt_library_shared {
259 name: "libfoo",
260 stem: "libbar",
261 srcs: ["libfoo.so"],
262 strip: {
263 none: true,
264 },
265 }
266 `, map[string][]byte{
267 "libfoo.so": nil,
268 })
269
270 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
271 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
Colin Cross33b2fb72019-05-14 14:07:01 -0700272}
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100273
274func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
Colin Cross0c66bc62021-07-20 09:47:41 -0700275 if runtime.GOOS != "linux" {
276 t.Skipf("Skipping host prebuilt testing that is only supported on linux not %s", runtime.GOOS)
Martin Stjernholm6a9a1462020-09-15 02:56:19 +0100277 }
278
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100279 ctx := testPrebuilt(t, `
280 cc_prebuilt_library_shared {
281 name: "libfoo",
282 device_supported: false,
283 host_supported: true,
284 target: {
285 linux_glibc_x86_64: {
286 srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
287 },
288 },
289 }
290
291 cc_prebuilt_binary {
292 name: "foo",
293 device_supported: false,
294 host_supported: true,
295 shared_libs: ["libfoo"],
296 target: {
297 linux_glibc_x86_64: {
298 srcs: ["linux_glibc_x86_64/bin/foo"],
299 },
300 },
301 }
302 `, map[string][]byte{
303 "libfoo.so": nil,
304 "foo": nil,
305 })
306
307 fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
Paul Duffine8366da2021-03-24 10:40:38 +0000308 assertString(t, fooRule.Output.String(), "out/soong/.intermediates/foo/linux_glibc_x86_64/foo")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100309 assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
310
311 var libfooDep android.Path
312 for _, dep := range fooRule.Implicits {
313 if dep.Base() == "libfoo.so" {
314 libfooDep = dep
315 break
316 }
317 }
Paul Duffine8366da2021-03-24 10:40:38 +0000318 assertString(t, libfooDep.String(), "out/soong/.intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so")
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100319}
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700320
321func TestPrebuiltLibrarySanitized(t *testing.T) {
322 bp := `cc_prebuilt_library {
323 name: "libtest",
324 static: {
325 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
326 },
327 shared: {
328 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
329 },
330 }
331 cc_prebuilt_library_static {
332 name: "libtest_static",
333 sanitized: { none: { srcs: ["libf.a"], }, hwaddress: { srcs: ["libf.hwasan.a"], }, },
334 }
335 cc_prebuilt_library_shared {
336 name: "libtest_shared",
337 sanitized: { none: { srcs: ["libf.so"], }, hwaddress: { srcs: ["hwasan/libf.so"], }, },
338 }`
339
340 fs := map[string][]byte{
341 "libf.a": nil,
342 "libf.hwasan.a": nil,
343 "libf.so": nil,
344 "hwasan/libf.so": nil,
345 }
346
347 // Without SANITIZE_TARGET.
348 ctx := testPrebuilt(t, bp, fs)
349
350 shared_rule := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
351 assertString(t, shared_rule.Input.String(), "libf.so")
352
353 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
354 assertString(t, static.OutputFile().Path().Base(), "libf.a")
355
356 shared_rule2 := ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared").Rule("android/soong/cc.strip")
357 assertString(t, shared_rule2.Input.String(), "libf.so")
358
359 static2 := ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static").Module().(*Module)
360 assertString(t, static2.OutputFile().Path().Base(), "libf.a")
361
362 // With SANITIZE_TARGET=hwaddress
Paul Duffin6a1160e2021-03-07 15:47:42 +0000363 ctx = testPrebuilt(t, bp, fs,
364 android.FixtureModifyProductVariables(func(variables android.FixtureProductVariables) {
365 variables.SanitizeDevice = []string{"hwaddress"}
366 }),
367 )
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700368
369 shared_rule = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
370 assertString(t, shared_rule.Input.String(), "hwasan/libf.so")
371
372 static = ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
373 assertString(t, static.OutputFile().Path().Base(), "libf.hwasan.a")
374
375 shared_rule2 = ctx.ModuleForTests("libtest_shared", "android_arm64_armv8-a_shared_hwasan").Rule("android/soong/cc.strip")
376 assertString(t, shared_rule2.Input.String(), "hwasan/libf.so")
377
378 static2 = ctx.ModuleForTests("libtest_static", "android_arm64_armv8-a_static_hwasan").Module().(*Module)
379 assertString(t, static2.OutputFile().Path().Base(), "libf.hwasan.a")
380}