blob: ef528e96b8b95fee37dbecb50d89e7e3c0efe39e [file] [log] [blame]
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001// Copyright 2021 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 bp2build
16
17import (
18 "android/soong/android"
19 "android/soong/cc"
20 "strings"
21 "testing"
22)
23
24const (
25 // See cc/testing.go for more context
26 soongCcLibraryStaticPreamble = `
27cc_defaults {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 name: "libclang_rt.builtins-x86_64-android",
33 defaults: ["linux_bionic_supported"],
34 vendor_available: true,
35 vendor_ramdisk_available: true,
36 product_available: true,
37 recovery_available: true,
38 native_bridge_supported: true,
39 src: "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000040}`
41)
42
43func TestCcLibraryStaticLoadStatement(t *testing.T) {
44 testCases := []struct {
45 bazelTargets BazelTargets
46 expectedLoadStatements string
47 }{
48 {
49 bazelTargets: BazelTargets{
50 BazelTarget{
51 name: "cc_library_static_target",
52 ruleClass: "cc_library_static",
53 // NOTE: No bzlLoadLocation for native rules
54 },
55 },
56 expectedLoadStatements: ``,
57 },
58 }
59
60 for _, testCase := range testCases {
61 actual := testCase.bazelTargets.LoadStatements()
62 expected := testCase.expectedLoadStatements
63 if actual != expected {
64 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
65 }
66 }
67
68}
69
70func TestCcLibraryStaticBp2Build(t *testing.T) {
71 testCases := []struct {
72 description string
73 moduleTypeUnderTest string
74 moduleTypeUnderTestFactory android.ModuleFactory
75 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
76 preArchMutators []android.RegisterMutatorFunc
77 depsMutators []android.RegisterMutatorFunc
78 bp string
79 expectedBazelTargets []string
80 filesystem map[string]string
81 dir string
82 }{
83 {
84 description: "cc_library_static test",
85 moduleTypeUnderTest: "cc_library_static",
86 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
87 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
88 filesystem: map[string]string{
89 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
90 "include_dir_1/include_dir_1_a.h": "",
91 "include_dir_1/include_dir_1_b.h": "",
92 "include_dir_2/include_dir_2_a.h": "",
93 "include_dir_2/include_dir_2_b.h": "",
94 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
95 "local_include_dir_1/local_include_dir_1_a.h": "",
96 "local_include_dir_1/local_include_dir_1_b.h": "",
97 "local_include_dir_2/local_include_dir_2_a.h": "",
98 "local_include_dir_2/local_include_dir_2_b.h": "",
99 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
100 "export_include_dir_1/export_include_dir_1_a.h": "",
101 "export_include_dir_1/export_include_dir_1_b.h": "",
102 "export_include_dir_2/export_include_dir_2_a.h": "",
103 "export_include_dir_2/export_include_dir_2_b.h": "",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000104 // NOTE: Soong implicitly includes headers in the current directory
105 "implicit_include_1.h": "",
106 "implicit_include_2.h": "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000107 },
108 bp: soongCcLibraryStaticPreamble + `
109cc_library_headers {
110 name: "header_lib_1",
111 export_include_dirs: ["header_lib_1"],
112}
113
114cc_library_headers {
115 name: "header_lib_2",
116 export_include_dirs: ["header_lib_2"],
117}
118
119cc_library_static {
120 name: "static_lib_1",
121 srcs: ["static_lib_1.cc"],
122 bazel_module: { bp2build_available: true },
123}
124
125cc_library_static {
126 name: "static_lib_2",
127 srcs: ["static_lib_2.cc"],
128 bazel_module: { bp2build_available: true },
129}
130
131cc_library_static {
132 name: "whole_static_lib_1",
133 srcs: ["whole_static_lib_1.cc"],
134 bazel_module: { bp2build_available: true },
135}
136
137cc_library_static {
138 name: "whole_static_lib_2",
139 srcs: ["whole_static_lib_2.cc"],
140 bazel_module: { bp2build_available: true },
141}
142
143cc_library_static {
144 name: "foo_static",
145 srcs: [
146 "foo_static1.cc",
147 "foo_static2.cc",
148 ],
149 cflags: [
150 "-Dflag1",
151 "-Dflag2"
152 ],
153 static_libs: [
154 "static_lib_1",
155 "static_lib_2"
156 ],
157 whole_static_libs: [
158 "whole_static_lib_1",
159 "whole_static_lib_2"
160 ],
161 include_dirs: [
162 "include_dir_1",
163 "include_dir_2",
164 ],
165 local_include_dirs: [
166 "local_include_dir_1",
167 "local_include_dir_2",
168 ],
169 export_include_dirs: [
170 "export_include_dir_1",
171 "export_include_dir_2"
172 ],
173 header_libs: [
174 "header_lib_1",
175 "header_lib_2"
176 ],
177
178 // TODO: Also support export_header_lib_headers
179
180 bazel_module: { bp2build_available: true },
181}`,
182 expectedBazelTargets: []string{`cc_library_static(
183 name = "foo_static",
184 copts = [
185 "-Dflag1",
186 "-Dflag2",
187 ],
188 deps = [
189 ":header_lib_1",
190 ":header_lib_2",
191 ":static_lib_1",
192 ":static_lib_2",
193 ":whole_static_lib_1",
194 ":whole_static_lib_2",
195 ],
196 hdrs = [
197 "export_include_dir_1/export_include_dir_1_a.h",
198 "export_include_dir_1/export_include_dir_1_b.h",
199 "export_include_dir_2/export_include_dir_2_a.h",
200 "export_include_dir_2/export_include_dir_2_b.h",
201 ],
202 includes = [
203 "export_include_dir_1",
204 "export_include_dir_2",
205 "include_dir_1",
206 "include_dir_2",
207 "local_include_dir_1",
208 "local_include_dir_2",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000209 ".",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000210 ],
211 linkstatic = True,
212 srcs = [
213 "foo_static1.cc",
214 "foo_static2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000215 "implicit_include_1.h",
216 "implicit_include_2.h",
217 "include_dir_1/include_dir_1_a.h",
218 "include_dir_1/include_dir_1_b.h",
219 "include_dir_2/include_dir_2_a.h",
220 "include_dir_2/include_dir_2_b.h",
221 "local_include_dir_1/local_include_dir_1_a.h",
222 "local_include_dir_1/local_include_dir_1_b.h",
223 "local_include_dir_2/local_include_dir_2_a.h",
224 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000225 ],
226)`, `cc_library_static(
227 name = "static_lib_1",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000228 includes = [
229 ".",
230 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000231 linkstatic = True,
232 srcs = [
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000233 "implicit_include_1.h",
234 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000235 "static_lib_1.cc",
236 ],
237)`, `cc_library_static(
238 name = "static_lib_2",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000239 includes = [
240 ".",
241 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000242 linkstatic = True,
243 srcs = [
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000244 "implicit_include_1.h",
245 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000246 "static_lib_2.cc",
247 ],
248)`, `cc_library_static(
249 name = "whole_static_lib_1",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000250 includes = [
251 ".",
252 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000253 linkstatic = True,
254 srcs = [
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000255 "implicit_include_1.h",
256 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000257 "whole_static_lib_1.cc",
258 ],
259)`, `cc_library_static(
260 name = "whole_static_lib_2",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000261 includes = [
262 ".",
263 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000264 linkstatic = True,
265 srcs = [
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000266 "implicit_include_1.h",
267 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000268 "whole_static_lib_2.cc",
269 ],
270)`},
271 },
272 }
273
274 dir := "."
275 for _, testCase := range testCases {
276 filesystem := make(map[string][]byte)
277 toParse := []string{
278 "Android.bp",
279 }
280 for f, content := range testCase.filesystem {
281 if strings.HasSuffix(f, "Android.bp") {
282 toParse = append(toParse, f)
283 }
284 filesystem[f] = []byte(content)
285 }
286 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
287 ctx := android.NewTestContext(config)
288
289 cc.RegisterCCBuildComponents(ctx)
290 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
291 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
292
293 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
294 for _, m := range testCase.depsMutators {
295 ctx.DepsBp2BuildMutators(m)
296 }
297 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
298 ctx.RegisterForBazelConversion()
299
300 _, errs := ctx.ParseFileList(dir, toParse)
301 if Errored(t, testCase.description, errs) {
302 continue
303 }
304 _, errs = ctx.ResolveDependencies(config)
305 if Errored(t, testCase.description, errs) {
306 continue
307 }
308
309 checkDir := dir
310 if testCase.dir != "" {
311 checkDir = testCase.dir
312 }
313 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
314 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
315 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
316 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
317 } else {
318 for i, target := range bazelTargets {
319 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
320 t.Errorf(
321 "%s: Expected generated Bazel target to be '%s', got '%s'",
322 testCase.description,
323 w,
324 g,
325 )
326 }
327 }
328 }
329 }
330}