blob: bb12462102143b37cf7fa40441f31c616aea9df3 [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"
Chris Parsons484e50a2021-05-13 15:13:04 -040020 "android/soong/genrule"
21
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000022 "strings"
23 "testing"
24)
25
26const (
27 // See cc/testing.go for more context
28 soongCcLibraryStaticPreamble = `
29cc_defaults {
30 name: "linux_bionic_supported",
31}
32
33toolchain_library {
34 name: "libclang_rt.builtins-x86_64-android",
35 defaults: ["linux_bionic_supported"],
36 vendor_available: true,
37 vendor_ramdisk_available: true,
38 product_available: true,
39 recovery_available: true,
40 native_bridge_supported: true,
41 src: "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000042}`
43)
44
45func TestCcLibraryStaticLoadStatement(t *testing.T) {
46 testCases := []struct {
47 bazelTargets BazelTargets
48 expectedLoadStatements string
49 }{
50 {
51 bazelTargets: BazelTargets{
52 BazelTarget{
53 name: "cc_library_static_target",
54 ruleClass: "cc_library_static",
55 // NOTE: No bzlLoadLocation for native rules
56 },
57 },
58 expectedLoadStatements: ``,
59 },
60 }
61
62 for _, testCase := range testCases {
63 actual := testCase.bazelTargets.LoadStatements()
64 expected := testCase.expectedLoadStatements
65 if actual != expected {
66 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
67 }
68 }
69
70}
71
72func TestCcLibraryStaticBp2Build(t *testing.T) {
73 testCases := []struct {
74 description string
75 moduleTypeUnderTest string
76 moduleTypeUnderTestFactory android.ModuleFactory
77 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
78 preArchMutators []android.RegisterMutatorFunc
79 depsMutators []android.RegisterMutatorFunc
80 bp string
81 expectedBazelTargets []string
82 filesystem map[string]string
83 dir string
84 }{
85 {
86 description: "cc_library_static test",
87 moduleTypeUnderTest: "cc_library_static",
88 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
89 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
90 filesystem: map[string]string{
91 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
92 "include_dir_1/include_dir_1_a.h": "",
93 "include_dir_1/include_dir_1_b.h": "",
94 "include_dir_2/include_dir_2_a.h": "",
95 "include_dir_2/include_dir_2_b.h": "",
96 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
97 "local_include_dir_1/local_include_dir_1_a.h": "",
98 "local_include_dir_1/local_include_dir_1_b.h": "",
99 "local_include_dir_2/local_include_dir_2_a.h": "",
100 "local_include_dir_2/local_include_dir_2_b.h": "",
101 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
102 "export_include_dir_1/export_include_dir_1_a.h": "",
103 "export_include_dir_1/export_include_dir_1_b.h": "",
104 "export_include_dir_2/export_include_dir_2_a.h": "",
105 "export_include_dir_2/export_include_dir_2_b.h": "",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000106 // NOTE: Soong implicitly includes headers in the current directory
107 "implicit_include_1.h": "",
108 "implicit_include_2.h": "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000109 },
110 bp: soongCcLibraryStaticPreamble + `
111cc_library_headers {
112 name: "header_lib_1",
113 export_include_dirs: ["header_lib_1"],
114}
115
116cc_library_headers {
117 name: "header_lib_2",
118 export_include_dirs: ["header_lib_2"],
119}
120
121cc_library_static {
122 name: "static_lib_1",
123 srcs: ["static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000124}
125
126cc_library_static {
127 name: "static_lib_2",
128 srcs: ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000129}
130
131cc_library_static {
132 name: "whole_static_lib_1",
133 srcs: ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000134}
135
136cc_library_static {
137 name: "whole_static_lib_2",
138 srcs: ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000139}
140
141cc_library_static {
142 name: "foo_static",
143 srcs: [
144 "foo_static1.cc",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000145 "foo_static2.cc",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000146 ],
147 cflags: [
148 "-Dflag1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000149 "-Dflag2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000150 ],
151 static_libs: [
152 "static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000153 "static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000154 ],
155 whole_static_libs: [
156 "whole_static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000157 "whole_static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000158 ],
159 include_dirs: [
Jingwen Chened9c17d2021-04-13 07:14:55 +0000160 "include_dir_1",
161 "include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000162 ],
163 local_include_dirs: [
164 "local_include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000165 "local_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000166 ],
167 export_include_dirs: [
Jingwen Chened9c17d2021-04-13 07:14:55 +0000168 "export_include_dir_1",
169 "export_include_dir_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000170 ],
171 header_libs: [
172 "header_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000173 "header_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000174 ],
175
176 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000177}`,
178 expectedBazelTargets: []string{`cc_library_static(
179 name = "foo_static",
180 copts = [
181 "-Dflag1",
182 "-Dflag2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000183 "-Iinclude_dir_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400184 "-I$(BINDIR)/include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000185 "-Iinclude_dir_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400186 "-I$(BINDIR)/include_dir_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000187 "-Ilocal_include_dir_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400188 "-I$(BINDIR)/local_include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000189 "-Ilocal_include_dir_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400190 "-I$(BINDIR)/local_include_dir_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000191 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400192 "-I$(BINDIR)/.",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000193 ],
194 deps = [
195 ":header_lib_1",
196 ":header_lib_2",
197 ":static_lib_1",
198 ":static_lib_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000199 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000200 includes = [
201 "export_include_dir_1",
202 "export_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000203 ],
204 linkstatic = True,
205 srcs = [
206 "foo_static1.cc",
207 "foo_static2.cc",
208 ],
Chris Parsons08648312021-05-06 16:23:19 -0400209 whole_archive_deps = [
210 ":whole_static_lib_1",
211 ":whole_static_lib_2",
212 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000213)`, `cc_library_static(
214 name = "static_lib_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400215 copts = [
216 "-I.",
217 "-I$(BINDIR)/.",
218 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000219 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000220 srcs = ["static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000221)`, `cc_library_static(
222 name = "static_lib_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400223 copts = [
224 "-I.",
225 "-I$(BINDIR)/.",
226 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000227 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000228 srcs = ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000229)`, `cc_library_static(
230 name = "whole_static_lib_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400231 copts = [
232 "-I.",
233 "-I$(BINDIR)/.",
234 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000235 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000236 srcs = ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000237)`, `cc_library_static(
238 name = "whole_static_lib_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400239 copts = [
240 "-I.",
241 "-I$(BINDIR)/.",
242 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000243 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000244 srcs = ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000245)`},
246 },
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400247 {
248 description: "cc_library_static subpackage test",
249 moduleTypeUnderTest: "cc_library_static",
250 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
251 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
252 filesystem: map[string]string{
253 // subpackage with subdirectory
254 "subpackage/Android.bp": "",
255 "subpackage/subpackage_header.h": "",
256 "subpackage/subdirectory/subdirectory_header.h": "",
257 // subsubpackage with subdirectory
258 "subpackage/subsubpackage/Android.bp": "",
259 "subpackage/subsubpackage/subsubpackage_header.h": "",
260 "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
261 // subsubsubpackage with subdirectory
262 "subpackage/subsubpackage/subsubsubpackage/Android.bp": "",
263 "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
264 "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
265 },
266 bp: soongCcLibraryStaticPreamble + `
267cc_library_static {
268 name: "foo_static",
269 srcs: [
270 ],
271 include_dirs: [
272 "subpackage",
273 ],
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400274}`,
275 expectedBazelTargets: []string{`cc_library_static(
276 name = "foo_static",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000277 copts = [
278 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400279 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000280 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400281 "-I$(BINDIR)/.",
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400282 ],
283 linkstatic = True,
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400284)`},
285 },
Jingwen Chened9c17d2021-04-13 07:14:55 +0000286 {
287 description: "cc_library_static export 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_include_dirs: ["subpackage"],
301}`,
302 expectedBazelTargets: []string{`cc_library_static(
303 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400304 copts = [
305 "-I.",
306 "-I$(BINDIR)/.",
307 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000308 includes = ["subpackage"],
309 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000310)`},
311 },
312 {
313 description: "cc_library_static export system include dir",
314 moduleTypeUnderTest: "cc_library_static",
315 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
316 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
317 filesystem: map[string]string{
318 // subpackage with subdirectory
319 "subpackage/Android.bp": "",
320 "subpackage/subpackage_header.h": "",
321 "subpackage/subdirectory/subdirectory_header.h": "",
322 },
323 bp: soongCcLibraryStaticPreamble + `
324cc_library_static {
325 name: "foo_static",
326 export_system_include_dirs: ["subpackage"],
327}`,
328 expectedBazelTargets: []string{`cc_library_static(
329 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400330 copts = [
331 "-I.",
332 "-I$(BINDIR)/.",
333 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000334 includes = ["subpackage"],
335 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000336)`},
337 },
338 {
339 description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
340 moduleTypeUnderTest: "cc_library_static",
341 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
342 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
343 dir: "subpackage",
344 filesystem: map[string]string{
345 // subpackage with subdirectory
346 "subpackage/Android.bp": `
347cc_library_static {
348 name: "foo_static",
349 // include_dirs are workspace/root relative
350 include_dirs: [
351 "subpackage/subsubpackage",
352 "subpackage2",
353 "subpackage3/subsubpackage"
354 ],
355 local_include_dirs: ["subsubpackage2"], // module dir relative
356 export_include_dirs: ["./exported_subsubpackage"], // module dir relative
357 include_build_directory: true,
358 bazel_module: { bp2build_available: true },
359}`,
360 "subpackage/subsubpackage/header.h": "",
361 "subpackage/subsubpackage2/header.h": "",
362 "subpackage/exported_subsubpackage/header.h": "",
363 "subpackage2/header.h": "",
364 "subpackage3/subsubpackage/header.h": "",
365 },
366 bp: soongCcLibraryStaticPreamble,
367 expectedBazelTargets: []string{`cc_library_static(
368 name = "foo_static",
369 copts = [
370 "-Isubpackage/subsubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400371 "-I$(BINDIR)/subpackage/subsubpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000372 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400373 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000374 "-Isubpackage3/subsubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400375 "-I$(BINDIR)/subpackage3/subsubpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000376 "-Isubpackage/subsubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400377 "-I$(BINDIR)/subpackage/subsubpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000378 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400379 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000380 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000381 includes = ["./exported_subsubpackage"],
382 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000383)`},
384 },
385 {
386 description: "cc_library_static include_build_directory disabled",
387 moduleTypeUnderTest: "cc_library_static",
388 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
389 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
390 filesystem: map[string]string{
391 // subpackage with subdirectory
392 "subpackage/Android.bp": "",
393 "subpackage/subpackage_header.h": "",
394 "subpackage/subdirectory/subdirectory_header.h": "",
395 },
396 bp: soongCcLibraryStaticPreamble + `
397cc_library_static {
398 name: "foo_static",
399 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
400 local_include_dirs: ["subpackage2"],
401 include_build_directory: false,
402}`,
403 expectedBazelTargets: []string{`cc_library_static(
404 name = "foo_static",
405 copts = [
406 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400407 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000408 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400409 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000410 ],
411 linkstatic = True,
412)`},
413 },
414 {
415 description: "cc_library_static include_build_directory enabled",
416 moduleTypeUnderTest: "cc_library_static",
417 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
418 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
419 filesystem: map[string]string{
420 // subpackage with subdirectory
421 "subpackage/Android.bp": "",
422 "subpackage/subpackage_header.h": "",
423 "subpackage2/Android.bp": "",
424 "subpackage2/subpackage2_header.h": "",
425 "subpackage/subdirectory/subdirectory_header.h": "",
426 },
427 bp: soongCcLibraryStaticPreamble + `
428cc_library_static {
429 name: "foo_static",
430 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
431 local_include_dirs: ["subpackage2"],
432 include_build_directory: true,
433}`,
434 expectedBazelTargets: []string{`cc_library_static(
435 name = "foo_static",
436 copts = [
437 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400438 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000439 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400440 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000441 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400442 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000443 ],
444 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000445)`},
446 },
447 {
448 description: "cc_library_static arch-specific static_libs",
449 moduleTypeUnderTest: "cc_library_static",
450 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
451 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
452 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
453 filesystem: map[string]string{},
454 bp: soongCcLibraryStaticPreamble + `
455cc_library_static { name: "static_dep" }
456cc_library_static { name: "static_dep2" }
457cc_library_static {
458 name: "foo_static",
459 arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
460}`,
461 expectedBazelTargets: []string{`cc_library_static(
462 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400463 copts = [
464 "-I.",
465 "-I$(BINDIR)/.",
466 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000467 deps = select({
Chris Parsons08648312021-05-06 16:23:19 -0400468 "//build/bazel/platforms/arch:arm64": [":static_dep"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000469 "//conditions:default": [],
470 }),
471 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400472 whole_archive_deps = select({
473 "//build/bazel/platforms/arch:arm64": [":static_dep2"],
474 "//conditions:default": [],
475 }),
Jingwen Chened9c17d2021-04-13 07:14:55 +0000476)`, `cc_library_static(
477 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400478 copts = [
479 "-I.",
480 "-I$(BINDIR)/.",
481 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000482 linkstatic = True,
483)`, `cc_library_static(
484 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400485 copts = [
486 "-I.",
487 "-I$(BINDIR)/.",
488 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000489 linkstatic = True,
490)`},
491 },
492 {
493 description: "cc_library_static os-specific static_libs",
494 moduleTypeUnderTest: "cc_library_static",
495 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
496 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
497 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
498 filesystem: map[string]string{},
499 bp: soongCcLibraryStaticPreamble + `
500cc_library_static { name: "static_dep" }
501cc_library_static { name: "static_dep2" }
502cc_library_static {
503 name: "foo_static",
504 target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
505}`,
506 expectedBazelTargets: []string{`cc_library_static(
507 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400508 copts = [
509 "-I.",
510 "-I$(BINDIR)/.",
511 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000512 deps = select({
Chris Parsons08648312021-05-06 16:23:19 -0400513 "//build/bazel/platforms/os:android": [":static_dep"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000514 "//conditions:default": [],
515 }),
516 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400517 whole_archive_deps = select({
518 "//build/bazel/platforms/os:android": [":static_dep2"],
519 "//conditions:default": [],
520 }),
Jingwen Chened9c17d2021-04-13 07:14:55 +0000521)`, `cc_library_static(
522 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400523 copts = [
524 "-I.",
525 "-I$(BINDIR)/.",
526 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000527 linkstatic = True,
528)`, `cc_library_static(
529 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400530 copts = [
531 "-I.",
532 "-I$(BINDIR)/.",
533 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000534 linkstatic = True,
535)`},
536 },
537 {
538 description: "cc_library_static base, arch and os-specific static_libs",
539 moduleTypeUnderTest: "cc_library_static",
540 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
541 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
542 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
543 filesystem: map[string]string{},
544 bp: soongCcLibraryStaticPreamble + `
545cc_library_static { name: "static_dep" }
546cc_library_static { name: "static_dep2" }
547cc_library_static { name: "static_dep3" }
548cc_library_static { name: "static_dep4" }
549cc_library_static {
550 name: "foo_static",
551 static_libs: ["static_dep"],
552 whole_static_libs: ["static_dep2"],
553 target: { android: { static_libs: ["static_dep3"] } },
554 arch: { arm64: { static_libs: ["static_dep4"] } },
555}`,
556 expectedBazelTargets: []string{`cc_library_static(
557 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400558 copts = [
559 "-I.",
560 "-I$(BINDIR)/.",
561 ],
Chris Parsons08648312021-05-06 16:23:19 -0400562 deps = [":static_dep"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000563 "//build/bazel/platforms/arch:arm64": [":static_dep4"],
564 "//conditions:default": [],
565 }) + select({
566 "//build/bazel/platforms/os:android": [":static_dep3"],
567 "//conditions:default": [],
568 }),
569 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400570 whole_archive_deps = [":static_dep2"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000571)`, `cc_library_static(
572 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400573 copts = [
574 "-I.",
575 "-I$(BINDIR)/.",
576 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000577 linkstatic = True,
578)`, `cc_library_static(
579 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400580 copts = [
581 "-I.",
582 "-I$(BINDIR)/.",
583 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000584 linkstatic = True,
585)`, `cc_library_static(
586 name = "static_dep3",
Chris Parsons484e50a2021-05-13 15:13:04 -0400587 copts = [
588 "-I.",
589 "-I$(BINDIR)/.",
590 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000591 linkstatic = True,
592)`, `cc_library_static(
593 name = "static_dep4",
Chris Parsons484e50a2021-05-13 15:13:04 -0400594 copts = [
595 "-I.",
596 "-I$(BINDIR)/.",
597 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000598 linkstatic = True,
599)`},
600 },
Jingwen Chene32e9e02021-04-23 09:17:24 +0000601 {
602 description: "cc_library_static simple exclude_srcs",
603 moduleTypeUnderTest: "cc_library_static",
604 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
605 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
606 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
607 filesystem: map[string]string{
608 "common.c": "",
609 "foo-a.c": "",
610 "foo-excluded.c": "",
611 },
612 bp: soongCcLibraryStaticPreamble + `
613cc_library_static {
614 name: "foo_static",
615 srcs: ["common.c", "foo-*.c"],
616 exclude_srcs: ["foo-excluded.c"],
617}`,
618 expectedBazelTargets: []string{`cc_library_static(
619 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400620 copts = [
621 "-I.",
622 "-I$(BINDIR)/.",
623 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000624 linkstatic = True,
625 srcs = [
626 "common.c",
627 "foo-a.c",
628 ],
629)`},
630 },
631 {
632 description: "cc_library_static one arch specific srcs",
633 moduleTypeUnderTest: "cc_library_static",
634 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
635 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
636 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
637 filesystem: map[string]string{
638 "common.c": "",
639 "foo-arm.c": "",
640 },
641 bp: soongCcLibraryStaticPreamble + `
642cc_library_static {
643 name: "foo_static",
644 srcs: ["common.c"],
645 arch: { arm: { srcs: ["foo-arm.c"] } }
646}`,
647 expectedBazelTargets: []string{`cc_library_static(
648 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400649 copts = [
650 "-I.",
651 "-I$(BINDIR)/.",
652 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000653 linkstatic = True,
654 srcs = ["common.c"] + select({
655 "//build/bazel/platforms/arch:arm": ["foo-arm.c"],
656 "//conditions:default": [],
657 }),
658)`},
659 },
660 {
661 description: "cc_library_static one arch specific srcs and exclude_srcs",
662 moduleTypeUnderTest: "cc_library_static",
663 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
664 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
665 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
666 filesystem: map[string]string{
667 "common.c": "",
668 "for-arm.c": "",
669 "not-for-arm.c": "",
670 "not-for-anything.c": "",
671 },
672 bp: soongCcLibraryStaticPreamble + `
673cc_library_static {
674 name: "foo_static",
675 srcs: ["common.c", "not-for-*.c"],
676 exclude_srcs: ["not-for-anything.c"],
677 arch: {
678 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
679 },
680}`,
681 expectedBazelTargets: []string{`cc_library_static(
682 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400683 copts = [
684 "-I.",
685 "-I$(BINDIR)/.",
686 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000687 linkstatic = True,
688 srcs = ["common.c"] + select({
689 "//build/bazel/platforms/arch:arm": ["for-arm.c"],
690 "//conditions:default": ["not-for-arm.c"],
691 }),
692)`},
693 },
694 {
695 description: "cc_library_static arch specific exclude_srcs for 2 architectures",
696 moduleTypeUnderTest: "cc_library_static",
697 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
698 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
699 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
700 filesystem: map[string]string{
701 "common.c": "",
702 "for-arm.c": "",
703 "for-x86.c": "",
704 "not-for-arm.c": "",
705 "not-for-x86.c": "",
706 },
707 bp: soongCcLibraryStaticPreamble + `
708cc_library_static {
709 name: "foo_static",
710 srcs: ["common.c", "not-for-*.c"],
711 exclude_srcs: ["not-for-everything.c"],
712 arch: {
713 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
714 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
715 },
716} `,
717 expectedBazelTargets: []string{`cc_library_static(
718 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400719 copts = [
720 "-I.",
721 "-I$(BINDIR)/.",
722 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000723 linkstatic = True,
724 srcs = ["common.c"] + select({
725 "//build/bazel/platforms/arch:arm": [
726 "for-arm.c",
727 "not-for-x86.c",
728 ],
729 "//build/bazel/platforms/arch:x86": [
730 "for-x86.c",
731 "not-for-arm.c",
732 ],
733 "//conditions:default": [
734 "not-for-arm.c",
735 "not-for-x86.c",
736 ],
737 }),
738)`},
739 },
740 {
741 description: "cc_library_static arch specific exclude_srcs for 4 architectures",
742 moduleTypeUnderTest: "cc_library_static",
743 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
744 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
745 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
746 filesystem: map[string]string{
747 "common.c": "",
748 "for-arm.c": "",
749 "for-arm64.c": "",
750 "for-x86.c": "",
751 "for-x86_64.c": "",
752 "not-for-arm.c": "",
753 "not-for-arm64.c": "",
754 "not-for-x86.c": "",
755 "not-for-x86_64.c": "",
756 "not-for-everything.c": "",
757 },
758 bp: soongCcLibraryStaticPreamble + `
759cc_library_static {
760 name: "foo_static",
761 srcs: ["common.c", "not-for-*.c"],
762 exclude_srcs: ["not-for-everything.c"],
763 arch: {
764 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
765 arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
766 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
767 x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
768 },
769} `,
770 expectedBazelTargets: []string{`cc_library_static(
771 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400772 copts = [
773 "-I.",
774 "-I$(BINDIR)/.",
775 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000776 linkstatic = True,
777 srcs = ["common.c"] + select({
778 "//build/bazel/platforms/arch:arm": [
779 "for-arm.c",
780 "not-for-arm64.c",
781 "not-for-x86.c",
782 "not-for-x86_64.c",
783 ],
784 "//build/bazel/platforms/arch:arm64": [
785 "for-arm64.c",
786 "not-for-arm.c",
787 "not-for-x86.c",
788 "not-for-x86_64.c",
789 ],
790 "//build/bazel/platforms/arch:x86": [
791 "for-x86.c",
792 "not-for-arm.c",
793 "not-for-arm64.c",
794 "not-for-x86_64.c",
795 ],
796 "//build/bazel/platforms/arch:x86_64": [
797 "for-x86_64.c",
798 "not-for-arm.c",
799 "not-for-arm64.c",
800 "not-for-x86.c",
801 ],
802 "//conditions:default": [
803 "not-for-arm.c",
804 "not-for-arm64.c",
805 "not-for-x86.c",
806 "not-for-x86_64.c",
807 ],
808 }),
809)`},
810 },
Liz Kammer2b50ce62021-04-26 15:47:28 -0400811 {
812 description: "cc_library_static multiple dep same name panic",
813 moduleTypeUnderTest: "cc_library_static",
814 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
815 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
816 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
817 filesystem: map[string]string{},
818 bp: soongCcLibraryStaticPreamble + `
819cc_library_static { name: "static_dep" }
820cc_library_static {
821 name: "foo_static",
Chris Parsons08648312021-05-06 16:23:19 -0400822 static_libs: ["static_dep", "static_dep"],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400823}`,
824 expectedBazelTargets: []string{`cc_library_static(
825 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400826 copts = [
827 "-I.",
828 "-I$(BINDIR)/.",
829 ],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400830 deps = [":static_dep"],
831 linkstatic = True,
832)`, `cc_library_static(
833 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400834 copts = [
835 "-I.",
836 "-I$(BINDIR)/.",
837 ],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400838 linkstatic = True,
839)`},
840 },
Chris Parsonsc424b762021-04-29 18:06:50 -0400841 {
842 description: "cc_library_static 1 multilib srcs and exclude_srcs",
843 moduleTypeUnderTest: "cc_library_static",
844 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
845 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
846 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
847 filesystem: map[string]string{
848 "common.c": "",
849 "for-lib32.c": "",
850 "not-for-lib32.c": "",
851 },
852 bp: soongCcLibraryStaticPreamble + `
853cc_library_static {
854 name: "foo_static",
855 srcs: ["common.c", "not-for-*.c"],
856 multilib: {
857 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
858 },
859} `,
860 expectedBazelTargets: []string{`cc_library_static(
861 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400862 copts = [
863 "-I.",
864 "-I$(BINDIR)/.",
865 ],
Chris Parsonsc424b762021-04-29 18:06:50 -0400866 linkstatic = True,
867 srcs = ["common.c"] + select({
868 "//build/bazel/platforms/arch:arm": ["for-lib32.c"],
869 "//build/bazel/platforms/arch:x86": ["for-lib32.c"],
870 "//conditions:default": ["not-for-lib32.c"],
871 }),
872)`},
873 },
874 {
875 description: "cc_library_static 2 multilib srcs and exclude_srcs",
876 moduleTypeUnderTest: "cc_library_static",
877 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
878 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
879 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
880 filesystem: map[string]string{
881 "common.c": "",
882 "for-lib32.c": "",
883 "for-lib64.c": "",
884 "not-for-lib32.c": "",
885 "not-for-lib64.c": "",
886 },
887 bp: soongCcLibraryStaticPreamble + `
888cc_library_static {
889 name: "foo_static2",
890 srcs: ["common.c", "not-for-*.c"],
891 multilib: {
892 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
893 lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
894 },
895} `,
896 expectedBazelTargets: []string{`cc_library_static(
897 name = "foo_static2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400898 copts = [
899 "-I.",
900 "-I$(BINDIR)/.",
901 ],
Chris Parsonsc424b762021-04-29 18:06:50 -0400902 linkstatic = True,
903 srcs = ["common.c"] + select({
904 "//build/bazel/platforms/arch:arm": [
905 "for-lib32.c",
906 "not-for-lib64.c",
907 ],
908 "//build/bazel/platforms/arch:arm64": [
909 "for-lib64.c",
910 "not-for-lib32.c",
911 ],
912 "//build/bazel/platforms/arch:x86": [
913 "for-lib32.c",
914 "not-for-lib64.c",
915 ],
916 "//build/bazel/platforms/arch:x86_64": [
917 "for-lib64.c",
918 "not-for-lib32.c",
919 ],
920 "//conditions:default": [
921 "not-for-lib32.c",
922 "not-for-lib64.c",
923 ],
924 }),
925)`},
926 },
927 {
928 description: "cc_library_static arch and multilib srcs and exclude_srcs",
929 moduleTypeUnderTest: "cc_library_static",
930 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
931 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
932 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
933 filesystem: map[string]string{
934 "common.c": "",
935 "for-arm.c": "",
936 "for-arm64.c": "",
937 "for-x86.c": "",
938 "for-x86_64.c": "",
939 "for-lib32.c": "",
940 "for-lib64.c": "",
941 "not-for-arm.c": "",
942 "not-for-arm64.c": "",
943 "not-for-x86.c": "",
944 "not-for-x86_64.c": "",
945 "not-for-lib32.c": "",
946 "not-for-lib64.c": "",
947 "not-for-everything.c": "",
948 },
949 bp: soongCcLibraryStaticPreamble + `
950cc_library_static {
951 name: "foo_static3",
952 srcs: ["common.c", "not-for-*.c"],
953 exclude_srcs: ["not-for-everything.c"],
954 arch: {
955 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
956 arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
957 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
958 x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
959 },
960 multilib: {
961 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
962 lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
963 },
964}`,
965 expectedBazelTargets: []string{`cc_library_static(
966 name = "foo_static3",
Chris Parsons484e50a2021-05-13 15:13:04 -0400967 copts = [
968 "-I.",
969 "-I$(BINDIR)/.",
970 ],
Chris Parsonsc424b762021-04-29 18:06:50 -0400971 linkstatic = True,
972 srcs = ["common.c"] + select({
973 "//build/bazel/platforms/arch:arm": [
974 "for-arm.c",
975 "for-lib32.c",
976 "not-for-arm64.c",
977 "not-for-lib64.c",
978 "not-for-x86.c",
979 "not-for-x86_64.c",
980 ],
981 "//build/bazel/platforms/arch:arm64": [
982 "for-arm64.c",
983 "for-lib64.c",
984 "not-for-arm.c",
985 "not-for-lib32.c",
986 "not-for-x86.c",
987 "not-for-x86_64.c",
988 ],
989 "//build/bazel/platforms/arch:x86": [
990 "for-lib32.c",
991 "for-x86.c",
992 "not-for-arm.c",
993 "not-for-arm64.c",
994 "not-for-lib64.c",
995 "not-for-x86_64.c",
996 ],
997 "//build/bazel/platforms/arch:x86_64": [
998 "for-lib64.c",
999 "for-x86_64.c",
1000 "not-for-arm.c",
1001 "not-for-arm64.c",
1002 "not-for-lib32.c",
1003 "not-for-x86.c",
1004 ],
1005 "//conditions:default": [
1006 "not-for-arm.c",
1007 "not-for-arm64.c",
1008 "not-for-lib32.c",
1009 "not-for-lib64.c",
1010 "not-for-x86.c",
1011 "not-for-x86_64.c",
1012 ],
1013 }),
1014)`},
1015 },
Chris Parsons484e50a2021-05-13 15:13:04 -04001016 {
1017 description: "cc_library_static arch srcs/exclude_srcs with generated files",
1018 moduleTypeUnderTest: "cc_library_static",
1019 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1020 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1021 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1022 filesystem: map[string]string{
1023 "common.c": "",
1024 "for-x86.c": "",
1025 "not-for-x86.c": "",
1026 "not-for-everything.c": "",
1027 "dep/Android.bp": `
1028genrule {
1029 name: "generated_src_other_pkg",
1030 out: ["generated_src_other_pkg.cpp"],
1031 cmd: "nothing to see here",
1032}
1033
1034genrule {
1035 name: "generated_hdr_other_pkg",
1036 out: ["generated_hdr_other_pkg.cpp"],
1037 cmd: "nothing to see here",
1038}
1039
1040genrule {
1041 name: "generated_hdr_other_pkg_x86",
1042 out: ["generated_hdr_other_pkg_x86.cpp"],
1043 cmd: "nothing to see here",
1044}`,
1045 },
1046 bp: soongCcLibraryStaticPreamble + `
1047genrule {
1048 name: "generated_src",
1049 out: ["generated_src.cpp"],
1050 cmd: "nothing to see here",
1051}
1052
1053genrule {
1054 name: "generated_src_x86",
1055 out: ["generated_src_x86.cpp"],
1056 cmd: "nothing to see here",
1057}
1058
1059genrule {
1060 name: "generated_hdr",
1061 out: ["generated_hdr.h"],
1062 cmd: "nothing to see here",
1063}
1064
1065cc_library_static {
1066 name: "foo_static3",
1067 srcs: ["common.c", "not-for-*.c"],
1068 exclude_srcs: ["not-for-everything.c"],
1069 generated_sources: ["generated_src", "generated_src_other_pkg"],
1070 generated_headers: ["generated_hdr", "generated_hdr_other_pkg"],
1071 arch: {
1072 x86: {
1073 srcs: ["for-x86.c"],
1074 exclude_srcs: ["not-for-x86.c"],
1075 generated_sources: ["generated_src_x86"],
1076 generated_headers: ["generated_hdr_other_pkg_x86"],
1077 },
1078 },
1079}
1080`,
1081 expectedBazelTargets: []string{`cc_library_static(
1082 name = "foo_static3",
1083 copts = [
1084 "-I.",
1085 "-I$(BINDIR)/.",
1086 ],
1087 linkstatic = True,
1088 srcs = [
1089 "//dep:generated_hdr_other_pkg",
1090 "//dep:generated_src_other_pkg",
1091 ":generated_hdr",
1092 ":generated_src",
1093 "common.c",
1094 ] + select({
1095 "//build/bazel/platforms/arch:x86": [
1096 "//dep:generated_hdr_other_pkg_x86",
1097 ":generated_src_x86",
1098 "for-x86.c",
1099 ],
1100 "//conditions:default": ["not-for-x86.c"],
1101 }),
1102)`},
1103 },
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001104 }
1105
1106 dir := "."
1107 for _, testCase := range testCases {
1108 filesystem := make(map[string][]byte)
1109 toParse := []string{
1110 "Android.bp",
1111 }
1112 for f, content := range testCase.filesystem {
1113 if strings.HasSuffix(f, "Android.bp") {
1114 toParse = append(toParse, f)
1115 }
1116 filesystem[f] = []byte(content)
1117 }
1118 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
1119 ctx := android.NewTestContext(config)
1120
1121 cc.RegisterCCBuildComponents(ctx)
1122 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
1123 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
Chris Parsons484e50a2021-05-13 15:13:04 -04001124 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001125
1126 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
1127 for _, m := range testCase.depsMutators {
1128 ctx.DepsBp2BuildMutators(m)
1129 }
1130 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chened9c17d2021-04-13 07:14:55 +00001131 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001132 ctx.RegisterForBazelConversion()
1133
1134 _, errs := ctx.ParseFileList(dir, toParse)
1135 if Errored(t, testCase.description, errs) {
1136 continue
1137 }
1138 _, errs = ctx.ResolveDependencies(config)
1139 if Errored(t, testCase.description, errs) {
1140 continue
1141 }
1142
1143 checkDir := dir
1144 if testCase.dir != "" {
1145 checkDir = testCase.dir
1146 }
1147 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
1148 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
1149 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
1150 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
1151 } else {
1152 for i, target := range bazelTargets {
1153 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
1154 t.Errorf(
1155 "%s: Expected generated Bazel target to be '%s', got '%s'",
1156 testCase.description,
1157 w,
1158 g,
1159 )
1160 }
1161 }
1162 }
1163 }
1164}