blob: e8235d588e26cd8deeff2e5636a7b10cbe09cfae [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 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000195 includes = [
196 "export_include_dir_1",
197 "export_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000198 ],
199 linkstatic = True,
200 srcs = [
201 "foo_static1.cc",
202 "foo_static2.cc",
203 ],
204)`, `cc_library_static(
205 name = "static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000206 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000207 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000208 srcs = ["static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000209)`, `cc_library_static(
210 name = "static_lib_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000211 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000212 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000213 srcs = ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000214)`, `cc_library_static(
215 name = "whole_static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000216 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000217 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000218 srcs = ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000219)`, `cc_library_static(
220 name = "whole_static_lib_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000221 copts = ["-I."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000222 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000223 srcs = ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000224)`},
225 },
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400226 {
227 description: "cc_library_static subpackage test",
228 moduleTypeUnderTest: "cc_library_static",
229 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
230 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
231 filesystem: map[string]string{
232 // subpackage with subdirectory
233 "subpackage/Android.bp": "",
234 "subpackage/subpackage_header.h": "",
235 "subpackage/subdirectory/subdirectory_header.h": "",
236 // subsubpackage with subdirectory
237 "subpackage/subsubpackage/Android.bp": "",
238 "subpackage/subsubpackage/subsubpackage_header.h": "",
239 "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
240 // subsubsubpackage with subdirectory
241 "subpackage/subsubpackage/subsubsubpackage/Android.bp": "",
242 "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
243 "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
244 },
245 bp: soongCcLibraryStaticPreamble + `
246cc_library_static {
247 name: "foo_static",
248 srcs: [
249 ],
250 include_dirs: [
251 "subpackage",
252 ],
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400253}`,
254 expectedBazelTargets: []string{`cc_library_static(
255 name = "foo_static",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000256 copts = [
257 "-Isubpackage",
258 "-I.",
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400259 ],
260 linkstatic = True,
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400261)`},
262 },
Jingwen Chened9c17d2021-04-13 07:14:55 +0000263 {
264 description: "cc_library_static export include dir",
265 moduleTypeUnderTest: "cc_library_static",
266 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
267 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
268 filesystem: map[string]string{
269 // subpackage with subdirectory
270 "subpackage/Android.bp": "",
271 "subpackage/subpackage_header.h": "",
272 "subpackage/subdirectory/subdirectory_header.h": "",
273 },
274 bp: soongCcLibraryStaticPreamble + `
275cc_library_static {
276 name: "foo_static",
277 export_include_dirs: ["subpackage"],
278}`,
279 expectedBazelTargets: []string{`cc_library_static(
280 name = "foo_static",
281 copts = ["-I."],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000282 includes = ["subpackage"],
283 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000284)`},
285 },
286 {
287 description: "cc_library_static export system include dir",
288 moduleTypeUnderTest: "cc_library_static",
289 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
290 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
291 filesystem: map[string]string{
292 // subpackage with subdirectory
293 "subpackage/Android.bp": "",
294 "subpackage/subpackage_header.h": "",
295 "subpackage/subdirectory/subdirectory_header.h": "",
296 },
297 bp: soongCcLibraryStaticPreamble + `
298cc_library_static {
299 name: "foo_static",
300 export_system_include_dirs: ["subpackage"],
301}`,
302 expectedBazelTargets: []string{`cc_library_static(
303 name = "foo_static",
304 copts = ["-I."],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000305 includes = ["subpackage"],
306 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000307)`},
308 },
309 {
310 description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
311 moduleTypeUnderTest: "cc_library_static",
312 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
313 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
314 dir: "subpackage",
315 filesystem: map[string]string{
316 // subpackage with subdirectory
317 "subpackage/Android.bp": `
318cc_library_static {
319 name: "foo_static",
320 // include_dirs are workspace/root relative
321 include_dirs: [
322 "subpackage/subsubpackage",
323 "subpackage2",
324 "subpackage3/subsubpackage"
325 ],
326 local_include_dirs: ["subsubpackage2"], // module dir relative
327 export_include_dirs: ["./exported_subsubpackage"], // module dir relative
328 include_build_directory: true,
329 bazel_module: { bp2build_available: true },
330}`,
331 "subpackage/subsubpackage/header.h": "",
332 "subpackage/subsubpackage2/header.h": "",
333 "subpackage/exported_subsubpackage/header.h": "",
334 "subpackage2/header.h": "",
335 "subpackage3/subsubpackage/header.h": "",
336 },
337 bp: soongCcLibraryStaticPreamble,
338 expectedBazelTargets: []string{`cc_library_static(
339 name = "foo_static",
340 copts = [
341 "-Isubpackage/subsubpackage",
342 "-Isubpackage2",
343 "-Isubpackage3/subsubpackage",
344 "-Isubpackage/subsubpackage2",
345 "-Isubpackage",
346 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000347 includes = ["./exported_subsubpackage"],
348 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000349)`},
350 },
351 {
352 description: "cc_library_static include_build_directory disabled",
353 moduleTypeUnderTest: "cc_library_static",
354 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
355 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
356 filesystem: map[string]string{
357 // subpackage with subdirectory
358 "subpackage/Android.bp": "",
359 "subpackage/subpackage_header.h": "",
360 "subpackage/subdirectory/subdirectory_header.h": "",
361 },
362 bp: soongCcLibraryStaticPreamble + `
363cc_library_static {
364 name: "foo_static",
365 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
366 local_include_dirs: ["subpackage2"],
367 include_build_directory: false,
368}`,
369 expectedBazelTargets: []string{`cc_library_static(
370 name = "foo_static",
371 copts = [
372 "-Isubpackage",
373 "-Isubpackage2",
374 ],
375 linkstatic = True,
376)`},
377 },
378 {
379 description: "cc_library_static include_build_directory enabled",
380 moduleTypeUnderTest: "cc_library_static",
381 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
382 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
383 filesystem: map[string]string{
384 // subpackage with subdirectory
385 "subpackage/Android.bp": "",
386 "subpackage/subpackage_header.h": "",
387 "subpackage2/Android.bp": "",
388 "subpackage2/subpackage2_header.h": "",
389 "subpackage/subdirectory/subdirectory_header.h": "",
390 },
391 bp: soongCcLibraryStaticPreamble + `
392cc_library_static {
393 name: "foo_static",
394 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
395 local_include_dirs: ["subpackage2"],
396 include_build_directory: true,
397}`,
398 expectedBazelTargets: []string{`cc_library_static(
399 name = "foo_static",
400 copts = [
401 "-Isubpackage",
402 "-Isubpackage2",
403 "-I.",
404 ],
405 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000406)`},
407 },
408 {
409 description: "cc_library_static arch-specific static_libs",
410 moduleTypeUnderTest: "cc_library_static",
411 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
412 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
413 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
414 filesystem: map[string]string{},
415 bp: soongCcLibraryStaticPreamble + `
416cc_library_static { name: "static_dep" }
417cc_library_static { name: "static_dep2" }
418cc_library_static {
419 name: "foo_static",
420 arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
421}`,
422 expectedBazelTargets: []string{`cc_library_static(
423 name = "foo_static",
424 copts = ["-I."],
425 deps = select({
426 "//build/bazel/platforms/arch:arm64": [
427 ":static_dep",
428 ":static_dep2",
429 ],
430 "//conditions:default": [],
431 }),
432 linkstatic = True,
433)`, `cc_library_static(
434 name = "static_dep",
435 copts = ["-I."],
436 linkstatic = True,
437)`, `cc_library_static(
438 name = "static_dep2",
439 copts = ["-I."],
440 linkstatic = True,
441)`},
442 },
443 {
444 description: "cc_library_static os-specific static_libs",
445 moduleTypeUnderTest: "cc_library_static",
446 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
447 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
448 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
449 filesystem: map[string]string{},
450 bp: soongCcLibraryStaticPreamble + `
451cc_library_static { name: "static_dep" }
452cc_library_static { name: "static_dep2" }
453cc_library_static {
454 name: "foo_static",
455 target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
456}`,
457 expectedBazelTargets: []string{`cc_library_static(
458 name = "foo_static",
459 copts = ["-I."],
460 deps = select({
461 "//build/bazel/platforms/os:android": [
462 ":static_dep",
463 ":static_dep2",
464 ],
465 "//conditions:default": [],
466 }),
467 linkstatic = True,
468)`, `cc_library_static(
469 name = "static_dep",
470 copts = ["-I."],
471 linkstatic = True,
472)`, `cc_library_static(
473 name = "static_dep2",
474 copts = ["-I."],
475 linkstatic = True,
476)`},
477 },
478 {
479 description: "cc_library_static base, arch and os-specific static_libs",
480 moduleTypeUnderTest: "cc_library_static",
481 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
482 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
483 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
484 filesystem: map[string]string{},
485 bp: soongCcLibraryStaticPreamble + `
486cc_library_static { name: "static_dep" }
487cc_library_static { name: "static_dep2" }
488cc_library_static { name: "static_dep3" }
489cc_library_static { name: "static_dep4" }
490cc_library_static {
491 name: "foo_static",
492 static_libs: ["static_dep"],
493 whole_static_libs: ["static_dep2"],
494 target: { android: { static_libs: ["static_dep3"] } },
495 arch: { arm64: { static_libs: ["static_dep4"] } },
496}`,
497 expectedBazelTargets: []string{`cc_library_static(
498 name = "foo_static",
499 copts = ["-I."],
500 deps = [
501 ":static_dep",
502 ":static_dep2",
503 ] + select({
504 "//build/bazel/platforms/arch:arm64": [":static_dep4"],
505 "//conditions:default": [],
506 }) + select({
507 "//build/bazel/platforms/os:android": [":static_dep3"],
508 "//conditions:default": [],
509 }),
510 linkstatic = True,
511)`, `cc_library_static(
512 name = "static_dep",
513 copts = ["-I."],
514 linkstatic = True,
515)`, `cc_library_static(
516 name = "static_dep2",
517 copts = ["-I."],
518 linkstatic = True,
519)`, `cc_library_static(
520 name = "static_dep3",
521 copts = ["-I."],
522 linkstatic = True,
523)`, `cc_library_static(
524 name = "static_dep4",
525 copts = ["-I."],
526 linkstatic = True,
527)`},
528 },
Jingwen Chene32e9e02021-04-23 09:17:24 +0000529 {
530 description: "cc_library_static simple exclude_srcs",
531 moduleTypeUnderTest: "cc_library_static",
532 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
533 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
534 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
535 filesystem: map[string]string{
536 "common.c": "",
537 "foo-a.c": "",
538 "foo-excluded.c": "",
539 },
540 bp: soongCcLibraryStaticPreamble + `
541cc_library_static {
542 name: "foo_static",
543 srcs: ["common.c", "foo-*.c"],
544 exclude_srcs: ["foo-excluded.c"],
545}`,
546 expectedBazelTargets: []string{`cc_library_static(
547 name = "foo_static",
548 copts = ["-I."],
549 linkstatic = True,
550 srcs = [
551 "common.c",
552 "foo-a.c",
553 ],
554)`},
555 },
556 {
557 description: "cc_library_static one arch specific srcs",
558 moduleTypeUnderTest: "cc_library_static",
559 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
560 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
561 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
562 filesystem: map[string]string{
563 "common.c": "",
564 "foo-arm.c": "",
565 },
566 bp: soongCcLibraryStaticPreamble + `
567cc_library_static {
568 name: "foo_static",
569 srcs: ["common.c"],
570 arch: { arm: { srcs: ["foo-arm.c"] } }
571}`,
572 expectedBazelTargets: []string{`cc_library_static(
573 name = "foo_static",
574 copts = ["-I."],
575 linkstatic = True,
576 srcs = ["common.c"] + select({
577 "//build/bazel/platforms/arch:arm": ["foo-arm.c"],
578 "//conditions:default": [],
579 }),
580)`},
581 },
582 {
583 description: "cc_library_static one arch specific srcs and exclude_srcs",
584 moduleTypeUnderTest: "cc_library_static",
585 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
586 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
587 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
588 filesystem: map[string]string{
589 "common.c": "",
590 "for-arm.c": "",
591 "not-for-arm.c": "",
592 "not-for-anything.c": "",
593 },
594 bp: soongCcLibraryStaticPreamble + `
595cc_library_static {
596 name: "foo_static",
597 srcs: ["common.c", "not-for-*.c"],
598 exclude_srcs: ["not-for-anything.c"],
599 arch: {
600 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
601 },
602}`,
603 expectedBazelTargets: []string{`cc_library_static(
604 name = "foo_static",
605 copts = ["-I."],
606 linkstatic = True,
607 srcs = ["common.c"] + select({
608 "//build/bazel/platforms/arch:arm": ["for-arm.c"],
609 "//conditions:default": ["not-for-arm.c"],
610 }),
611)`},
612 },
613 {
614 description: "cc_library_static arch specific exclude_srcs for 2 architectures",
615 moduleTypeUnderTest: "cc_library_static",
616 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
617 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
618 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
619 filesystem: map[string]string{
620 "common.c": "",
621 "for-arm.c": "",
622 "for-x86.c": "",
623 "not-for-arm.c": "",
624 "not-for-x86.c": "",
625 },
626 bp: soongCcLibraryStaticPreamble + `
627cc_library_static {
628 name: "foo_static",
629 srcs: ["common.c", "not-for-*.c"],
630 exclude_srcs: ["not-for-everything.c"],
631 arch: {
632 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
633 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
634 },
635} `,
636 expectedBazelTargets: []string{`cc_library_static(
637 name = "foo_static",
638 copts = ["-I."],
639 linkstatic = True,
640 srcs = ["common.c"] + select({
641 "//build/bazel/platforms/arch:arm": [
642 "for-arm.c",
643 "not-for-x86.c",
644 ],
645 "//build/bazel/platforms/arch:x86": [
646 "for-x86.c",
647 "not-for-arm.c",
648 ],
649 "//conditions:default": [
650 "not-for-arm.c",
651 "not-for-x86.c",
652 ],
653 }),
654)`},
655 },
656 {
657 description: "cc_library_static arch specific exclude_srcs for 4 architectures",
658 moduleTypeUnderTest: "cc_library_static",
659 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
660 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
661 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
662 filesystem: map[string]string{
663 "common.c": "",
664 "for-arm.c": "",
665 "for-arm64.c": "",
666 "for-x86.c": "",
667 "for-x86_64.c": "",
668 "not-for-arm.c": "",
669 "not-for-arm64.c": "",
670 "not-for-x86.c": "",
671 "not-for-x86_64.c": "",
672 "not-for-everything.c": "",
673 },
674 bp: soongCcLibraryStaticPreamble + `
675cc_library_static {
676 name: "foo_static",
677 srcs: ["common.c", "not-for-*.c"],
678 exclude_srcs: ["not-for-everything.c"],
679 arch: {
680 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
681 arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
682 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
683 x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
684 },
685} `,
686 expectedBazelTargets: []string{`cc_library_static(
687 name = "foo_static",
688 copts = ["-I."],
689 linkstatic = True,
690 srcs = ["common.c"] + select({
691 "//build/bazel/platforms/arch:arm": [
692 "for-arm.c",
693 "not-for-arm64.c",
694 "not-for-x86.c",
695 "not-for-x86_64.c",
696 ],
697 "//build/bazel/platforms/arch:arm64": [
698 "for-arm64.c",
699 "not-for-arm.c",
700 "not-for-x86.c",
701 "not-for-x86_64.c",
702 ],
703 "//build/bazel/platforms/arch:x86": [
704 "for-x86.c",
705 "not-for-arm.c",
706 "not-for-arm64.c",
707 "not-for-x86_64.c",
708 ],
709 "//build/bazel/platforms/arch:x86_64": [
710 "for-x86_64.c",
711 "not-for-arm.c",
712 "not-for-arm64.c",
713 "not-for-x86.c",
714 ],
715 "//conditions:default": [
716 "not-for-arm.c",
717 "not-for-arm64.c",
718 "not-for-x86.c",
719 "not-for-x86_64.c",
720 ],
721 }),
722)`},
723 },
Liz Kammer2b50ce62021-04-26 15:47:28 -0400724 {
725 description: "cc_library_static multiple dep same name panic",
726 moduleTypeUnderTest: "cc_library_static",
727 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
728 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
729 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
730 filesystem: map[string]string{},
731 bp: soongCcLibraryStaticPreamble + `
732cc_library_static { name: "static_dep" }
733cc_library_static {
734 name: "foo_static",
735 static_libs: ["static_dep"],
736 whole_static_libs: ["static_dep"],
737}`,
738 expectedBazelTargets: []string{`cc_library_static(
739 name = "foo_static",
740 copts = ["-I."],
741 deps = [":static_dep"],
742 linkstatic = True,
743)`, `cc_library_static(
744 name = "static_dep",
745 copts = ["-I."],
746 linkstatic = True,
747)`},
748 },
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000749 }
750
751 dir := "."
752 for _, testCase := range testCases {
753 filesystem := make(map[string][]byte)
754 toParse := []string{
755 "Android.bp",
756 }
757 for f, content := range testCase.filesystem {
758 if strings.HasSuffix(f, "Android.bp") {
759 toParse = append(toParse, f)
760 }
761 filesystem[f] = []byte(content)
762 }
763 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
764 ctx := android.NewTestContext(config)
765
766 cc.RegisterCCBuildComponents(ctx)
767 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
768 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
769
770 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
771 for _, m := range testCase.depsMutators {
772 ctx.DepsBp2BuildMutators(m)
773 }
774 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chened9c17d2021-04-13 07:14:55 +0000775 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000776 ctx.RegisterForBazelConversion()
777
778 _, errs := ctx.ParseFileList(dir, toParse)
779 if Errored(t, testCase.description, errs) {
780 continue
781 }
782 _, errs = ctx.ResolveDependencies(config)
783 if Errored(t, testCase.description, errs) {
784 continue
785 }
786
787 checkDir := dir
788 if testCase.dir != "" {
789 checkDir = testCase.dir
790 }
791 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
792 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
793 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
794 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
795 } else {
796 for i, target := range bazelTargets {
797 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
798 t.Errorf(
799 "%s: Expected generated Bazel target to be '%s', got '%s'",
800 testCase.description,
801 w,
802 g,
803 )
804 }
805 }
806 }
807 }
808}