blob: 52416ac3b03c0f4a066485d08f4ed455d1f496d8 [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
Martin Stjernholmadeb0882020-04-01 23:02:57 +010026func testPrebuilt(t *testing.T, bp string, fs map[string][]byte) *android.TestContext {
27 config := TestConfig(buildDir, android.Android, nil, bp, fs)
28 ctx := CreateTestContext()
29
30 // Enable androidmk support.
31 // * Register the singleton
32 // * Configure that we are inside make
33 // * Add CommonOS to ensure that androidmk processing works.
34 android.RegisterAndroidMkBuildComponents(ctx)
35 android.SetInMakeForTests(config)
36
37 ctx.Register(config)
38 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
39 android.FailIfErrored(t, errs)
40 _, errs = ctx.PrepareBuildActions(config)
41 android.FailIfErrored(t, errs)
42 return ctx
43}
44
Colin Cross33b2fb72019-05-14 14:07:01 -070045func TestPrebuilt(t *testing.T) {
46 bp := `
47 cc_library {
48 name: "liba",
49 }
50
51 cc_prebuilt_library_shared {
52 name: "liba",
53 srcs: ["liba.so"],
54 }
55
56 cc_library {
57 name: "libb",
58 }
59
60 cc_prebuilt_library_static {
61 name: "libb",
62 srcs: ["libb.a"],
63 }
64
65 cc_library_shared {
66 name: "libd",
67 }
68
69 cc_prebuilt_library_shared {
70 name: "libd",
71 srcs: ["libd.so"],
72 }
73
74 cc_library_static {
75 name: "libe",
76 }
77
78 cc_prebuilt_library_static {
79 name: "libe",
80 srcs: ["libe.a"],
81 }
Paul Duffinbce90da2020-03-12 20:17:14 +000082
83 cc_library {
84 name: "libf",
85 }
86
87 cc_prebuilt_library {
88 name: "libf",
89 static: {
90 srcs: ["libf.a"],
91 },
92 shared: {
93 srcs: ["libf.so"],
94 },
95 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000096
97 cc_object {
98 name: "crtx",
99 }
100
101 cc_prebuilt_object {
102 name: "crtx",
103 srcs: ["crtx.o"],
104 }
Colin Cross33b2fb72019-05-14 14:07:01 -0700105 `
106
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100107 ctx := testPrebuilt(t, bp, map[string][]byte{
108 "liba.so": nil,
109 "libb.a": nil,
110 "libd.so": nil,
111 "libe.a": nil,
112 "libf.a": nil,
113 "libf.so": nil,
114 "crtx.o": nil,
115 })
Colin Cross33b2fb72019-05-14 14:07:01 -0700116
117 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -0800118 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
119 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
120 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
121 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000122 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
123 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000124 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700125
Colin Cross7113d202019-11-20 16:39:12 -0800126 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
127 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
128 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
129 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000130 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
131 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000132 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700133
134 hasDep := func(m android.Module, wantDep android.Module) bool {
135 t.Helper()
136 var found bool
137 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
138 if dep == wantDep {
139 found = true
140 }
141 })
142 return found
143 }
144
145 if !hasDep(liba, prebuiltLiba) {
146 t.Errorf("liba missing dependency on prebuilt_liba")
147 }
148
149 if !hasDep(libb, prebuiltLibb) {
150 t.Errorf("libb missing dependency on prebuilt_libb")
151 }
152
153 if !hasDep(libd, prebuiltLibd) {
154 t.Errorf("libd missing dependency on prebuilt_libd")
155 }
156
157 if !hasDep(libe, prebuiltLibe) {
158 t.Errorf("libe missing dependency on prebuilt_libe")
159 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000160
161 if !hasDep(libfStatic, prebuiltLibfStatic) {
162 t.Errorf("libf static missing dependency on prebuilt_libf")
163 }
164
165 if !hasDep(libfShared, prebuiltLibfShared) {
166 t.Errorf("libf shared missing dependency on prebuilt_libf")
167 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000168
169 if !hasDep(crtx, prebuiltCrtx) {
170 t.Errorf("crtx missing dependency on prebuilt_crtx")
171 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000172}
173
Paul Duffinbce90da2020-03-12 20:17:14 +0000174func TestPrebuiltLibraryShared(t *testing.T) {
175 ctx := testPrebuilt(t, `
176 cc_prebuilt_library_shared {
177 name: "libtest",
178 srcs: ["libf.so"],
179 strip: {
180 none: true,
181 },
182 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100183 `, map[string][]byte{
184 "libf.so": nil,
185 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000186
187 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800188 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000189}
190
191func TestPrebuiltLibraryStatic(t *testing.T) {
192 ctx := testPrebuilt(t, `
193 cc_prebuilt_library_static {
194 name: "libtest",
195 srcs: ["libf.a"],
196 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100197 `, map[string][]byte{
198 "libf.a": nil,
199 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000200
201 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800202 assertString(t, static.OutputFile().Path().Base(), "libf.a")
Paul Duffinbce90da2020-03-12 20:17:14 +0000203}
204
205func TestPrebuiltLibrary(t *testing.T) {
206 ctx := testPrebuilt(t, `
207 cc_prebuilt_library {
208 name: "libtest",
209 static: {
210 srcs: ["libf.a"],
211 },
212 shared: {
213 srcs: ["libf.so"],
214 },
215 strip: {
216 none: true,
217 },
218 }
Martin Stjernholmadeb0882020-04-01 23:02:57 +0100219 `, map[string][]byte{
220 "libf.a": nil,
221 "libf.so": nil,
222 })
Paul Duffinbce90da2020-03-12 20:17:14 +0000223
224 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800225 assertString(t, shared.OutputFile().Path().Base(), "libtest.so")
Paul Duffinbce90da2020-03-12 20:17:14 +0000226
227 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
Yo Chianga3ad9b22020-03-18 14:19:07 +0800228 assertString(t, static.OutputFile().Path().Base(), "libf.a")
229}
230
231func TestPrebuiltLibraryStem(t *testing.T) {
232 ctx := testPrebuilt(t, `
233 cc_prebuilt_library {
234 name: "libfoo",
235 stem: "libbar",
236 static: {
237 srcs: ["libfoo.a"],
238 },
239 shared: {
240 srcs: ["libfoo.so"],
241 },
242 strip: {
243 none: true,
244 },
245 }
246 `, map[string][]byte{
247 "libfoo.a": nil,
248 "libfoo.so": nil,
249 })
250
251 static := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_static").Module().(*Module)
252 assertString(t, static.OutputFile().Path().Base(), "libfoo.a")
253
254 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
255 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
256}
257
258func TestPrebuiltLibrarySharedStem(t *testing.T) {
259 ctx := testPrebuilt(t, `
260 cc_prebuilt_library_shared {
261 name: "libfoo",
262 stem: "libbar",
263 srcs: ["libfoo.so"],
264 strip: {
265 none: true,
266 },
267 }
268 `, map[string][]byte{
269 "libfoo.so": nil,
270 })
271
272 shared := ctx.ModuleForTests("libfoo", "android_arm64_armv8-a_shared").Module().(*Module)
273 assertString(t, shared.OutputFile().Path().Base(), "libbar.so")
Colin Cross33b2fb72019-05-14 14:07:01 -0700274}
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100275
276func TestPrebuiltSymlinkedHostBinary(t *testing.T) {
Martin Stjernholm6a9a1462020-09-15 02:56:19 +0100277 if android.BuildOs != android.Linux {
278 t.Skipf("Skipping host prebuilt testing that is only supported on %s not %s", android.Linux, android.BuildOs)
279 }
280
Martin Stjernholm837ee1a2020-08-20 02:54:52 +0100281 ctx := testPrebuilt(t, `
282 cc_prebuilt_library_shared {
283 name: "libfoo",
284 device_supported: false,
285 host_supported: true,
286 target: {
287 linux_glibc_x86_64: {
288 srcs: ["linux_glibc_x86_64/lib64/libfoo.so"],
289 },
290 },
291 }
292
293 cc_prebuilt_binary {
294 name: "foo",
295 device_supported: false,
296 host_supported: true,
297 shared_libs: ["libfoo"],
298 target: {
299 linux_glibc_x86_64: {
300 srcs: ["linux_glibc_x86_64/bin/foo"],
301 },
302 },
303 }
304 `, map[string][]byte{
305 "libfoo.so": nil,
306 "foo": nil,
307 })
308
309 fooRule := ctx.ModuleForTests("foo", "linux_glibc_x86_64").Rule("Symlink")
310 assertString(t, fooRule.Output.String(),
311 filepath.Join(buildDir, ".intermediates/foo/linux_glibc_x86_64/foo"))
312 assertString(t, fooRule.Args["fromPath"], "$$PWD/linux_glibc_x86_64/bin/foo")
313
314 var libfooDep android.Path
315 for _, dep := range fooRule.Implicits {
316 if dep.Base() == "libfoo.so" {
317 libfooDep = dep
318 break
319 }
320 }
321 assertString(t, libfooDep.String(),
322 filepath.Join(buildDir, ".intermediates/libfoo/linux_glibc_x86_64_shared/libfoo.so"))
323}