blob: 242d835e9fb55b77dc4deeaec388176e557ff8e7 [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 (
18 "testing"
19
20 "android/soong/android"
21
22 "github.com/google/blueprint"
23)
24
25func TestPrebuilt(t *testing.T) {
26 bp := `
27 cc_library {
28 name: "liba",
29 }
30
31 cc_prebuilt_library_shared {
32 name: "liba",
33 srcs: ["liba.so"],
34 }
35
36 cc_library {
37 name: "libb",
38 }
39
40 cc_prebuilt_library_static {
41 name: "libb",
42 srcs: ["libb.a"],
43 }
44
45 cc_library_shared {
46 name: "libd",
47 }
48
49 cc_prebuilt_library_shared {
50 name: "libd",
51 srcs: ["libd.so"],
52 }
53
54 cc_library_static {
55 name: "libe",
56 }
57
58 cc_prebuilt_library_static {
59 name: "libe",
60 srcs: ["libe.a"],
61 }
Paul Duffinbce90da2020-03-12 20:17:14 +000062
63 cc_library {
64 name: "libf",
65 }
66
67 cc_prebuilt_library {
68 name: "libf",
69 static: {
70 srcs: ["libf.a"],
71 },
72 shared: {
73 srcs: ["libf.so"],
74 },
75 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000076
77 cc_object {
78 name: "crtx",
79 }
80
81 cc_prebuilt_object {
82 name: "crtx",
83 srcs: ["crtx.o"],
84 }
Colin Cross33b2fb72019-05-14 14:07:01 -070085 `
86
Paul Duffinbce90da2020-03-12 20:17:14 +000087 ctx := testPrebuilt(t, bp)
Colin Cross33b2fb72019-05-14 14:07:01 -070088
89 // Verify that all the modules exist and that their dependencies were connected correctly
Colin Cross7113d202019-11-20 16:39:12 -080090 liba := ctx.ModuleForTests("liba", "android_arm64_armv8-a_shared").Module()
91 libb := ctx.ModuleForTests("libb", "android_arm64_armv8-a_static").Module()
92 libd := ctx.ModuleForTests("libd", "android_arm64_armv8-a_shared").Module()
93 libe := ctx.ModuleForTests("libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +000094 libfStatic := ctx.ModuleForTests("libf", "android_arm64_armv8-a_static").Module()
95 libfShared := ctx.ModuleForTests("libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000096 crtx := ctx.ModuleForTests("crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -070097
Colin Cross7113d202019-11-20 16:39:12 -080098 prebuiltLiba := ctx.ModuleForTests("prebuilt_liba", "android_arm64_armv8-a_shared").Module()
99 prebuiltLibb := ctx.ModuleForTests("prebuilt_libb", "android_arm64_armv8-a_static").Module()
100 prebuiltLibd := ctx.ModuleForTests("prebuilt_libd", "android_arm64_armv8-a_shared").Module()
101 prebuiltLibe := ctx.ModuleForTests("prebuilt_libe", "android_arm64_armv8-a_static").Module()
Paul Duffinbce90da2020-03-12 20:17:14 +0000102 prebuiltLibfStatic := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_static").Module()
103 prebuiltLibfShared := ctx.ModuleForTests("prebuilt_libf", "android_arm64_armv8-a_shared").Module()
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000104 prebuiltCrtx := ctx.ModuleForTests("prebuilt_crtx", "android_arm64_armv8-a").Module()
Colin Cross33b2fb72019-05-14 14:07:01 -0700105
106 hasDep := func(m android.Module, wantDep android.Module) bool {
107 t.Helper()
108 var found bool
109 ctx.VisitDirectDeps(m, func(dep blueprint.Module) {
110 if dep == wantDep {
111 found = true
112 }
113 })
114 return found
115 }
116
117 if !hasDep(liba, prebuiltLiba) {
118 t.Errorf("liba missing dependency on prebuilt_liba")
119 }
120
121 if !hasDep(libb, prebuiltLibb) {
122 t.Errorf("libb missing dependency on prebuilt_libb")
123 }
124
125 if !hasDep(libd, prebuiltLibd) {
126 t.Errorf("libd missing dependency on prebuilt_libd")
127 }
128
129 if !hasDep(libe, prebuiltLibe) {
130 t.Errorf("libe missing dependency on prebuilt_libe")
131 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000132
133 if !hasDep(libfStatic, prebuiltLibfStatic) {
134 t.Errorf("libf static missing dependency on prebuilt_libf")
135 }
136
137 if !hasDep(libfShared, prebuiltLibfShared) {
138 t.Errorf("libf shared missing dependency on prebuilt_libf")
139 }
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000140
141 if !hasDep(crtx, prebuiltCrtx) {
142 t.Errorf("crtx missing dependency on prebuilt_crtx")
143 }
Paul Duffinbce90da2020-03-12 20:17:14 +0000144}
145
146func testPrebuilt(t *testing.T, bp string) *android.TestContext {
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000147
Paul Duffinbce90da2020-03-12 20:17:14 +0000148 fs := map[string][]byte{
149 "liba.so": nil,
150 "libb.a": nil,
151 "libd.so": nil,
152 "libe.a": nil,
153 "libf.a": nil,
154 "libf.so": nil,
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000155 "crtx.o": nil,
Paul Duffinbce90da2020-03-12 20:17:14 +0000156 }
157 config := TestConfig(buildDir, android.Android, nil, bp, fs)
158 ctx := CreateTestContext()
159
160 // Enable androidmk support.
161 // * Register the singleton
162 // * Configure that we are inside make
163 // * Add CommonOS to ensure that androidmk processing works.
164 android.RegisterAndroidMkBuildComponents(ctx)
165 android.SetInMakeForTests(config)
166
167 ctx.Register(config)
168 _, errs := ctx.ParseFileList(".", []string{"Android.bp"})
169 android.FailIfErrored(t, errs)
170 _, errs = ctx.PrepareBuildActions(config)
171 android.FailIfErrored(t, errs)
172 return ctx
173}
174
175func TestPrebuiltLibraryShared(t *testing.T) {
176 ctx := testPrebuilt(t, `
177 cc_prebuilt_library_shared {
178 name: "libtest",
179 srcs: ["libf.so"],
180 strip: {
181 none: true,
182 },
183 }
184 `)
185
186 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
187 assertString(t, shared.OutputFile().String(), "libf.so")
188}
189
190func TestPrebuiltLibraryStatic(t *testing.T) {
191 ctx := testPrebuilt(t, `
192 cc_prebuilt_library_static {
193 name: "libtest",
194 srcs: ["libf.a"],
195 }
196 `)
197
198 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
199 assertString(t, static.OutputFile().String(), "libf.a")
200}
201
202func TestPrebuiltLibrary(t *testing.T) {
203 ctx := testPrebuilt(t, `
204 cc_prebuilt_library {
205 name: "libtest",
206 static: {
207 srcs: ["libf.a"],
208 },
209 shared: {
210 srcs: ["libf.so"],
211 },
212 strip: {
213 none: true,
214 },
215 }
216 `)
217
218 shared := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_shared").Module().(*Module)
219 assertString(t, shared.OutputFile().String(), "libf.so")
220
221 static := ctx.ModuleForTests("libtest", "android_arm64_armv8-a_static").Module().(*Module)
222 assertString(t, static.OutputFile().String(), "libf.a")
Colin Cross33b2fb72019-05-14 14:07:01 -0700223}