blob: 00325fb6199f0d702734e81ad42bca1ff02a569f [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"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000122}
123
124cc_library_static {
125 name: "static_lib_2",
126 srcs: ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000127}
128
129cc_library_static {
130 name: "whole_static_lib_1",
131 srcs: ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000132}
133
134cc_library_static {
135 name: "whole_static_lib_2",
136 srcs: ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000137}
138
139cc_library_static {
140 name: "foo_static",
141 srcs: [
142 "foo_static1.cc",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000143 "foo_static2.cc",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000144 ],
145 cflags: [
146 "-Dflag1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000147 "-Dflag2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000148 ],
149 static_libs: [
150 "static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000151 "static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000152 ],
153 whole_static_libs: [
154 "whole_static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000155 "whole_static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000156 ],
157 include_dirs: [
Jingwen Chened9c17d2021-04-13 07:14:55 +0000158 "include_dir_1",
159 "include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000160 ],
161 local_include_dirs: [
162 "local_include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000163 "local_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000164 ],
165 export_include_dirs: [
Jingwen Chened9c17d2021-04-13 07:14:55 +0000166 "export_include_dir_1",
167 "export_include_dir_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000168 ],
169 header_libs: [
170 "header_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000171 "header_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000172 ],
173
174 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000175}`,
176 expectedBazelTargets: []string{`cc_library_static(
177 name = "foo_static",
178 copts = [
179 "-Dflag1",
180 "-Dflag2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000181 "-Iinclude_dir_1",
182 "-Iinclude_dir_2",
183 "-Ilocal_include_dir_1",
184 "-Ilocal_include_dir_2",
185 "-I.",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000186 ],
187 deps = [
188 ":header_lib_1",
189 ":header_lib_2",
190 ":static_lib_1",
191 ":static_lib_2",
192 ":whole_static_lib_1",
193 ":whole_static_lib_2",
194 ],
195 hdrs = [
196 "export_include_dir_1/export_include_dir_1_a.h",
197 "export_include_dir_1/export_include_dir_1_b.h",
198 "export_include_dir_2/export_include_dir_2_a.h",
199 "export_include_dir_2/export_include_dir_2_b.h",
200 ],
201 includes = [
202 "export_include_dir_1",
203 "export_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000204 ],
205 linkstatic = True,
206 srcs = [
207 "foo_static1.cc",
208 "foo_static2.cc",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000209 "implicit_include_1.h",
210 "implicit_include_2.h",
211 "export_include_dir_1/export_include_dir_1_a.h",
212 "export_include_dir_1/export_include_dir_1_b.h",
213 "export_include_dir_2/export_include_dir_2_a.h",
214 "export_include_dir_2/export_include_dir_2_b.h",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000215 "include_dir_1/include_dir_1_a.h",
216 "include_dir_1/include_dir_1_b.h",
217 "include_dir_2/include_dir_2_a.h",
218 "include_dir_2/include_dir_2_b.h",
219 "local_include_dir_1/local_include_dir_1_a.h",
220 "local_include_dir_1/local_include_dir_1_b.h",
221 "local_include_dir_2/local_include_dir_2_a.h",
222 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000223 ],
224)`, `cc_library_static(
225 name = "static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000226 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000227 linkstatic = True,
228 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400229 "static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000230 "implicit_include_1.h",
231 "implicit_include_2.h",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000232 "export_include_dir_1/export_include_dir_1_a.h",
233 "export_include_dir_1/export_include_dir_1_b.h",
234 "export_include_dir_2/export_include_dir_2_a.h",
235 "export_include_dir_2/export_include_dir_2_b.h",
236 "include_dir_1/include_dir_1_a.h",
237 "include_dir_1/include_dir_1_b.h",
238 "include_dir_2/include_dir_2_a.h",
239 "include_dir_2/include_dir_2_b.h",
240 "local_include_dir_1/local_include_dir_1_a.h",
241 "local_include_dir_1/local_include_dir_1_b.h",
242 "local_include_dir_2/local_include_dir_2_a.h",
243 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000244 ],
245)`, `cc_library_static(
246 name = "static_lib_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000247 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000248 linkstatic = True,
249 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400250 "static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000251 "implicit_include_1.h",
252 "implicit_include_2.h",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000253 "export_include_dir_1/export_include_dir_1_a.h",
254 "export_include_dir_1/export_include_dir_1_b.h",
255 "export_include_dir_2/export_include_dir_2_a.h",
256 "export_include_dir_2/export_include_dir_2_b.h",
257 "include_dir_1/include_dir_1_a.h",
258 "include_dir_1/include_dir_1_b.h",
259 "include_dir_2/include_dir_2_a.h",
260 "include_dir_2/include_dir_2_b.h",
261 "local_include_dir_1/local_include_dir_1_a.h",
262 "local_include_dir_1/local_include_dir_1_b.h",
263 "local_include_dir_2/local_include_dir_2_a.h",
264 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000265 ],
266)`, `cc_library_static(
267 name = "whole_static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000268 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000269 linkstatic = True,
270 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400271 "whole_static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000272 "implicit_include_1.h",
273 "implicit_include_2.h",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000274 "export_include_dir_1/export_include_dir_1_a.h",
275 "export_include_dir_1/export_include_dir_1_b.h",
276 "export_include_dir_2/export_include_dir_2_a.h",
277 "export_include_dir_2/export_include_dir_2_b.h",
278 "include_dir_1/include_dir_1_a.h",
279 "include_dir_1/include_dir_1_b.h",
280 "include_dir_2/include_dir_2_a.h",
281 "include_dir_2/include_dir_2_b.h",
282 "local_include_dir_1/local_include_dir_1_a.h",
283 "local_include_dir_1/local_include_dir_1_b.h",
284 "local_include_dir_2/local_include_dir_2_a.h",
285 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000286 ],
287)`, `cc_library_static(
288 name = "whole_static_lib_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000289 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000290 linkstatic = True,
291 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400292 "whole_static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000293 "implicit_include_1.h",
294 "implicit_include_2.h",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000295 "export_include_dir_1/export_include_dir_1_a.h",
296 "export_include_dir_1/export_include_dir_1_b.h",
297 "export_include_dir_2/export_include_dir_2_a.h",
298 "export_include_dir_2/export_include_dir_2_b.h",
299 "include_dir_1/include_dir_1_a.h",
300 "include_dir_1/include_dir_1_b.h",
301 "include_dir_2/include_dir_2_a.h",
302 "include_dir_2/include_dir_2_b.h",
303 "local_include_dir_1/local_include_dir_1_a.h",
304 "local_include_dir_1/local_include_dir_1_b.h",
305 "local_include_dir_2/local_include_dir_2_a.h",
306 "local_include_dir_2/local_include_dir_2_b.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000307 ],
308)`},
309 },
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400310 {
311 description: "cc_library_static subpackage test",
312 moduleTypeUnderTest: "cc_library_static",
313 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
314 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
315 filesystem: map[string]string{
316 // subpackage with subdirectory
317 "subpackage/Android.bp": "",
318 "subpackage/subpackage_header.h": "",
319 "subpackage/subdirectory/subdirectory_header.h": "",
320 // subsubpackage with subdirectory
321 "subpackage/subsubpackage/Android.bp": "",
322 "subpackage/subsubpackage/subsubpackage_header.h": "",
323 "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
324 // subsubsubpackage with subdirectory
325 "subpackage/subsubpackage/subsubsubpackage/Android.bp": "",
326 "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
327 "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
328 },
329 bp: soongCcLibraryStaticPreamble + `
330cc_library_static {
331 name: "foo_static",
332 srcs: [
333 ],
334 include_dirs: [
335 "subpackage",
336 ],
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400337}`,
338 expectedBazelTargets: []string{`cc_library_static(
339 name = "foo_static",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000340 copts = [
341 "-Isubpackage",
342 "-I.",
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400343 ],
344 linkstatic = True,
345 srcs = [
346 "//subpackage:subpackage_header.h",
347 "//subpackage:subdirectory/subdirectory_header.h",
348 "//subpackage/subsubpackage:subsubpackage_header.h",
349 "//subpackage/subsubpackage:subdirectory/subdirectory_header.h",
350 "//subpackage/subsubpackage/subsubsubpackage:subsubsubpackage_header.h",
351 "//subpackage/subsubpackage/subsubsubpackage:subdirectory/subdirectory_header.h",
352 ],
353)`},
354 },
Jingwen Chened9c17d2021-04-13 07:14:55 +0000355 {
356 description: "cc_library_static export include dir",
357 moduleTypeUnderTest: "cc_library_static",
358 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
359 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
360 filesystem: map[string]string{
361 // subpackage with subdirectory
362 "subpackage/Android.bp": "",
363 "subpackage/subpackage_header.h": "",
364 "subpackage/subdirectory/subdirectory_header.h": "",
365 },
366 bp: soongCcLibraryStaticPreamble + `
367cc_library_static {
368 name: "foo_static",
369 export_include_dirs: ["subpackage"],
370}`,
371 expectedBazelTargets: []string{`cc_library_static(
372 name = "foo_static",
373 copts = ["-I."],
374 hdrs = [
375 "//subpackage:subdirectory/subdirectory_header.h",
376 "//subpackage:subpackage_header.h",
377 ],
378 includes = ["subpackage"],
379 linkstatic = True,
380 srcs = [
381 "//subpackage:subpackage_header.h",
382 "//subpackage:subdirectory/subdirectory_header.h",
383 ],
384)`},
385 },
386 {
387 description: "cc_library_static export system include dir",
388 moduleTypeUnderTest: "cc_library_static",
389 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
390 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
391 filesystem: map[string]string{
392 // subpackage with subdirectory
393 "subpackage/Android.bp": "",
394 "subpackage/subpackage_header.h": "",
395 "subpackage/subdirectory/subdirectory_header.h": "",
396 },
397 bp: soongCcLibraryStaticPreamble + `
398cc_library_static {
399 name: "foo_static",
400 export_system_include_dirs: ["subpackage"],
401}`,
402 expectedBazelTargets: []string{`cc_library_static(
403 name = "foo_static",
404 copts = ["-I."],
405 hdrs = [
406 "//subpackage:subdirectory/subdirectory_header.h",
407 "//subpackage:subpackage_header.h",
408 ],
409 includes = ["subpackage"],
410 linkstatic = True,
411 srcs = [
412 "//subpackage:subpackage_header.h",
413 "//subpackage:subdirectory/subdirectory_header.h",
414 ],
415)`},
416 },
417 {
418 description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
419 moduleTypeUnderTest: "cc_library_static",
420 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
421 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
422 dir: "subpackage",
423 filesystem: map[string]string{
424 // subpackage with subdirectory
425 "subpackage/Android.bp": `
426cc_library_static {
427 name: "foo_static",
428 // include_dirs are workspace/root relative
429 include_dirs: [
430 "subpackage/subsubpackage",
431 "subpackage2",
432 "subpackage3/subsubpackage"
433 ],
434 local_include_dirs: ["subsubpackage2"], // module dir relative
435 export_include_dirs: ["./exported_subsubpackage"], // module dir relative
436 include_build_directory: true,
437 bazel_module: { bp2build_available: true },
438}`,
439 "subpackage/subsubpackage/header.h": "",
440 "subpackage/subsubpackage2/header.h": "",
441 "subpackage/exported_subsubpackage/header.h": "",
442 "subpackage2/header.h": "",
443 "subpackage3/subsubpackage/header.h": "",
444 },
445 bp: soongCcLibraryStaticPreamble,
446 expectedBazelTargets: []string{`cc_library_static(
447 name = "foo_static",
448 copts = [
449 "-Isubpackage/subsubpackage",
450 "-Isubpackage2",
451 "-Isubpackage3/subsubpackage",
452 "-Isubpackage/subsubpackage2",
453 "-Isubpackage",
454 ],
455 hdrs = ["exported_subsubpackage/header.h"],
456 includes = ["./exported_subsubpackage"],
457 linkstatic = True,
458 srcs = [
459 "exported_subsubpackage/header.h",
460 "subsubpackage/header.h",
461 "subsubpackage2/header.h",
462 ],
463)`},
464 },
465 {
466 description: "cc_library_static include_build_directory disabled",
467 moduleTypeUnderTest: "cc_library_static",
468 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
469 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
470 filesystem: map[string]string{
471 // subpackage with subdirectory
472 "subpackage/Android.bp": "",
473 "subpackage/subpackage_header.h": "",
474 "subpackage/subdirectory/subdirectory_header.h": "",
475 },
476 bp: soongCcLibraryStaticPreamble + `
477cc_library_static {
478 name: "foo_static",
479 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
480 local_include_dirs: ["subpackage2"],
481 include_build_directory: false,
482}`,
483 expectedBazelTargets: []string{`cc_library_static(
484 name = "foo_static",
485 copts = [
486 "-Isubpackage",
487 "-Isubpackage2",
488 ],
489 linkstatic = True,
490)`},
491 },
492 {
493 description: "cc_library_static include_build_directory enabled",
494 moduleTypeUnderTest: "cc_library_static",
495 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
496 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
497 filesystem: map[string]string{
498 // subpackage with subdirectory
499 "subpackage/Android.bp": "",
500 "subpackage/subpackage_header.h": "",
501 "subpackage2/Android.bp": "",
502 "subpackage2/subpackage2_header.h": "",
503 "subpackage/subdirectory/subdirectory_header.h": "",
504 },
505 bp: soongCcLibraryStaticPreamble + `
506cc_library_static {
507 name: "foo_static",
508 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
509 local_include_dirs: ["subpackage2"],
510 include_build_directory: true,
511}`,
512 expectedBazelTargets: []string{`cc_library_static(
513 name = "foo_static",
514 copts = [
515 "-Isubpackage",
516 "-Isubpackage2",
517 "-I.",
518 ],
519 linkstatic = True,
520 srcs = [
521 "//subpackage:subpackage_header.h",
522 "//subpackage:subdirectory/subdirectory_header.h",
523 "//subpackage2:subpackage2_header.h",
524 ],
525)`},
526 },
527 {
528 description: "cc_library_static arch-specific static_libs",
529 moduleTypeUnderTest: "cc_library_static",
530 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
531 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
532 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
533 filesystem: map[string]string{},
534 bp: soongCcLibraryStaticPreamble + `
535cc_library_static { name: "static_dep" }
536cc_library_static { name: "static_dep2" }
537cc_library_static {
538 name: "foo_static",
539 arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
540}`,
541 expectedBazelTargets: []string{`cc_library_static(
542 name = "foo_static",
543 copts = ["-I."],
544 deps = select({
545 "//build/bazel/platforms/arch:arm64": [
546 ":static_dep",
547 ":static_dep2",
548 ],
549 "//conditions:default": [],
550 }),
551 linkstatic = True,
552)`, `cc_library_static(
553 name = "static_dep",
554 copts = ["-I."],
555 linkstatic = True,
556)`, `cc_library_static(
557 name = "static_dep2",
558 copts = ["-I."],
559 linkstatic = True,
560)`},
561 },
562 {
563 description: "cc_library_static os-specific static_libs",
564 moduleTypeUnderTest: "cc_library_static",
565 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
566 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
567 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
568 filesystem: map[string]string{},
569 bp: soongCcLibraryStaticPreamble + `
570cc_library_static { name: "static_dep" }
571cc_library_static { name: "static_dep2" }
572cc_library_static {
573 name: "foo_static",
574 target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
575}`,
576 expectedBazelTargets: []string{`cc_library_static(
577 name = "foo_static",
578 copts = ["-I."],
579 deps = select({
580 "//build/bazel/platforms/os:android": [
581 ":static_dep",
582 ":static_dep2",
583 ],
584 "//conditions:default": [],
585 }),
586 linkstatic = True,
587)`, `cc_library_static(
588 name = "static_dep",
589 copts = ["-I."],
590 linkstatic = True,
591)`, `cc_library_static(
592 name = "static_dep2",
593 copts = ["-I."],
594 linkstatic = True,
595)`},
596 },
597 {
598 description: "cc_library_static base, arch and os-specific static_libs",
599 moduleTypeUnderTest: "cc_library_static",
600 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
601 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
602 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
603 filesystem: map[string]string{},
604 bp: soongCcLibraryStaticPreamble + `
605cc_library_static { name: "static_dep" }
606cc_library_static { name: "static_dep2" }
607cc_library_static { name: "static_dep3" }
608cc_library_static { name: "static_dep4" }
609cc_library_static {
610 name: "foo_static",
611 static_libs: ["static_dep"],
612 whole_static_libs: ["static_dep2"],
613 target: { android: { static_libs: ["static_dep3"] } },
614 arch: { arm64: { static_libs: ["static_dep4"] } },
615}`,
616 expectedBazelTargets: []string{`cc_library_static(
617 name = "foo_static",
618 copts = ["-I."],
619 deps = [
620 ":static_dep",
621 ":static_dep2",
622 ] + select({
623 "//build/bazel/platforms/arch:arm64": [":static_dep4"],
624 "//conditions:default": [],
625 }) + select({
626 "//build/bazel/platforms/os:android": [":static_dep3"],
627 "//conditions:default": [],
628 }),
629 linkstatic = True,
630)`, `cc_library_static(
631 name = "static_dep",
632 copts = ["-I."],
633 linkstatic = True,
634)`, `cc_library_static(
635 name = "static_dep2",
636 copts = ["-I."],
637 linkstatic = True,
638)`, `cc_library_static(
639 name = "static_dep3",
640 copts = ["-I."],
641 linkstatic = True,
642)`, `cc_library_static(
643 name = "static_dep4",
644 copts = ["-I."],
645 linkstatic = True,
646)`},
647 },
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000648 }
649
650 dir := "."
651 for _, testCase := range testCases {
652 filesystem := make(map[string][]byte)
653 toParse := []string{
654 "Android.bp",
655 }
656 for f, content := range testCase.filesystem {
657 if strings.HasSuffix(f, "Android.bp") {
658 toParse = append(toParse, f)
659 }
660 filesystem[f] = []byte(content)
661 }
662 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
663 ctx := android.NewTestContext(config)
664
665 cc.RegisterCCBuildComponents(ctx)
666 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
667 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
668
669 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
670 for _, m := range testCase.depsMutators {
671 ctx.DepsBp2BuildMutators(m)
672 }
673 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000674 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000675 ctx.RegisterForBazelConversion()
676
677 _, errs := ctx.ParseFileList(dir, toParse)
678 if Errored(t, testCase.description, errs) {
679 continue
680 }
681 _, errs = ctx.ResolveDependencies(config)
682 if Errored(t, testCase.description, errs) {
683 continue
684 }
685
686 checkDir := dir
687 if testCase.dir != "" {
688 checkDir = testCase.dir
689 }
690 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
691 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
692 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
693 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
694 } else {
695 for i, target := range bazelTargets {
696 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
697 t.Errorf(
698 "%s: Expected generated Bazel target to be '%s', got '%s'",
699 testCase.description,
700 w,
701 g,
702 )
703 }
704 }
705 }
706 }
707}