• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// 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	"android/soong/genrule"
21
22	"testing"
23)
24
25const (
26	// See cc/testing.go for more context
27	soongCcLibraryStaticPreamble = `
28cc_defaults {
29    name: "linux_bionic_supported",
30}`
31)
32
33func TestCcLibraryStaticLoadStatement(t *testing.T) {
34	testCases := []struct {
35		bazelTargets           BazelTargets
36		expectedLoadStatements string
37	}{
38		{
39			bazelTargets: BazelTargets{
40				BazelTarget{
41					name:      "cc_library_static_target",
42					ruleClass: "cc_library_static",
43					// NOTE: No bzlLoadLocation for native rules
44				},
45			},
46			expectedLoadStatements: ``,
47		},
48	}
49
50	for _, testCase := range testCases {
51		actual := testCase.bazelTargets.LoadStatements()
52		expected := testCase.expectedLoadStatements
53		if actual != expected {
54			t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
55		}
56	}
57}
58
59func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) {
60	cc.RegisterCCBuildComponents(ctx)
61	ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
62	ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
63	// Required for system_shared_libs dependencies.
64	ctx.RegisterModuleType("cc_library", cc.LibraryFactory)
65}
66
67func runCcLibraryStaticTestCase(t *testing.T, tc bp2buildTestCase) {
68	t.Helper()
69
70	(&tc).moduleTypeUnderTest = "cc_library_static"
71	(&tc).moduleTypeUnderTestFactory = cc.LibraryStaticFactory
72	runBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc)
73}
74
75func TestCcLibraryStaticSimple(t *testing.T) {
76	runCcLibraryStaticTestCase(t, bp2buildTestCase{
77		description: "cc_library_static test",
78		filesystem: map[string]string{
79			// NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
80			"include_dir_1/include_dir_1_a.h": "",
81			"include_dir_1/include_dir_1_b.h": "",
82			"include_dir_2/include_dir_2_a.h": "",
83			"include_dir_2/include_dir_2_b.h": "",
84			// NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
85			"local_include_dir_1/local_include_dir_1_a.h": "",
86			"local_include_dir_1/local_include_dir_1_b.h": "",
87			"local_include_dir_2/local_include_dir_2_a.h": "",
88			"local_include_dir_2/local_include_dir_2_b.h": "",
89			// NOTE: export_include_dir headers *should* appear in Bazel hdrs later
90			"export_include_dir_1/export_include_dir_1_a.h": "",
91			"export_include_dir_1/export_include_dir_1_b.h": "",
92			"export_include_dir_2/export_include_dir_2_a.h": "",
93			"export_include_dir_2/export_include_dir_2_b.h": "",
94			// NOTE: Soong implicitly includes headers in the current directory
95			"implicit_include_1.h": "",
96			"implicit_include_2.h": "",
97		},
98		blueprint: soongCcLibraryStaticPreamble + `
99cc_library_headers {
100    name: "header_lib_1",
101    export_include_dirs: ["header_lib_1"],
102    bazel_module: { bp2build_available: false },
103}
104
105cc_library_headers {
106    name: "header_lib_2",
107    export_include_dirs: ["header_lib_2"],
108    bazel_module: { bp2build_available: false },
109}
110
111cc_library_static {
112    name: "static_lib_1",
113    srcs: ["static_lib_1.cc"],
114    bazel_module: { bp2build_available: false },
115}
116
117cc_library_static {
118    name: "static_lib_2",
119    srcs: ["static_lib_2.cc"],
120    bazel_module: { bp2build_available: false },
121}
122
123cc_library_static {
124    name: "whole_static_lib_1",
125    srcs: ["whole_static_lib_1.cc"],
126    bazel_module: { bp2build_available: false },
127}
128
129cc_library_static {
130    name: "whole_static_lib_2",
131    srcs: ["whole_static_lib_2.cc"],
132    bazel_module: { bp2build_available: false },
133}
134
135cc_library_static {
136    name: "foo_static",
137    srcs: [
138        "foo_static1.cc",
139        "foo_static2.cc",
140    ],
141    cflags: [
142        "-Dflag1",
143        "-Dflag2"
144    ],
145    static_libs: [
146        "static_lib_1",
147        "static_lib_2"
148    ],
149    whole_static_libs: [
150        "whole_static_lib_1",
151        "whole_static_lib_2"
152    ],
153    include_dirs: [
154        "include_dir_1",
155        "include_dir_2",
156    ],
157    local_include_dirs: [
158        "local_include_dir_1",
159        "local_include_dir_2",
160    ],
161    export_include_dirs: [
162        "export_include_dir_1",
163        "export_include_dir_2"
164    ],
165    header_libs: [
166        "header_lib_1",
167        "header_lib_2"
168    ],
169    sdk_version: "current",
170    min_sdk_version: "29",
171
172    // TODO: Also support export_header_lib_headers
173}`,
174		expectedBazelTargets: []string{
175			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
176				"absolute_includes": `[
177        "include_dir_1",
178        "include_dir_2",
179    ]`,
180				"copts": `[
181        "-Dflag1",
182        "-Dflag2",
183    ]`,
184				"export_includes": `[
185        "export_include_dir_1",
186        "export_include_dir_2",
187    ]`,
188				"implementation_deps": `[
189        ":header_lib_1",
190        ":header_lib_2",
191        ":static_lib_1",
192        ":static_lib_2",
193    ]`,
194				"local_includes": `[
195        "local_include_dir_1",
196        "local_include_dir_2",
197        ".",
198    ]`,
199				"srcs": `[
200        "foo_static1.cc",
201        "foo_static2.cc",
202    ]`,
203				"whole_archive_deps": `[
204        ":whole_static_lib_1",
205        ":whole_static_lib_2",
206    ]`,
207        "sdk_version": `"current"`,
208        "min_sdk_version": `"29"`,
209			}),
210		},
211	})
212}
213
214func TestCcLibraryStaticSubpackage(t *testing.T) {
215	runCcLibraryStaticTestCase(t, bp2buildTestCase{
216		description: "cc_library_static subpackage test",
217		filesystem: map[string]string{
218			// subpackage with subdirectory
219			"subpackage/Android.bp":                         "",
220			"subpackage/subpackage_header.h":                "",
221			"subpackage/subdirectory/subdirectory_header.h": "",
222			// subsubpackage with subdirectory
223			"subpackage/subsubpackage/Android.bp":                         "",
224			"subpackage/subsubpackage/subsubpackage_header.h":             "",
225			"subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
226			// subsubsubpackage with subdirectory
227			"subpackage/subsubpackage/subsubsubpackage/Android.bp":                         "",
228			"subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h":          "",
229			"subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
230		},
231		blueprint: soongCcLibraryStaticPreamble + `
232cc_library_static {
233    name: "foo_static",
234    srcs: [],
235    include_dirs: [
236        "subpackage",
237    ],
238}`,
239		expectedBazelTargets: []string{
240			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
241				"absolute_includes": `["subpackage"]`,
242				"local_includes":    `["."]`,
243			}),
244		},
245	})
246}
247
248func TestCcLibraryStaticExportIncludeDir(t *testing.T) {
249	runCcLibraryStaticTestCase(t, bp2buildTestCase{
250		description: "cc_library_static export include dir",
251		filesystem: map[string]string{
252			// subpackage with subdirectory
253			"subpackage/Android.bp":                         "",
254			"subpackage/subpackage_header.h":                "",
255			"subpackage/subdirectory/subdirectory_header.h": "",
256		},
257		blueprint: soongCcLibraryStaticPreamble + `
258cc_library_static {
259    name: "foo_static",
260    export_include_dirs: ["subpackage"],
261    include_build_directory: false,
262}`,
263		expectedBazelTargets: []string{
264			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
265				"export_includes": `["subpackage"]`,
266			}),
267		},
268	})
269}
270
271func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) {
272	runCcLibraryStaticTestCase(t, bp2buildTestCase{
273		description: "cc_library_static export system include dir",
274		filesystem: map[string]string{
275			// subpackage with subdirectory
276			"subpackage/Android.bp":                         "",
277			"subpackage/subpackage_header.h":                "",
278			"subpackage/subdirectory/subdirectory_header.h": "",
279		},
280		blueprint: soongCcLibraryStaticPreamble + `
281cc_library_static {
282    name: "foo_static",
283    export_system_include_dirs: ["subpackage"],
284    include_build_directory: false,
285}`,
286		expectedBazelTargets: []string{
287			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
288				"export_system_includes": `["subpackage"]`,
289			}),
290		},
291	})
292}
293
294func TestCcLibraryStaticManyIncludeDirs(t *testing.T) {
295	runCcLibraryStaticTestCase(t, bp2buildTestCase{
296		description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
297		dir:         "subpackage",
298		filesystem: map[string]string{
299			// subpackage with subdirectory
300			"subpackage/Android.bp": `
301cc_library_static {
302    name: "foo_static",
303    // include_dirs are workspace/root relative
304    include_dirs: [
305        "subpackage/subsubpackage",
306        "subpackage2",
307        "subpackage3/subsubpackage"
308    ],
309    local_include_dirs: ["subsubpackage2"], // module dir relative
310    export_include_dirs: ["./exported_subsubpackage"], // module dir relative
311    include_build_directory: true,
312    bazel_module: { bp2build_available: true },
313}`,
314			"subpackage/subsubpackage/header.h":          "",
315			"subpackage/subsubpackage2/header.h":         "",
316			"subpackage/exported_subsubpackage/header.h": "",
317			"subpackage2/header.h":                       "",
318			"subpackage3/subsubpackage/header.h":         "",
319		},
320		blueprint: soongCcLibraryStaticPreamble,
321		expectedBazelTargets: []string{
322			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
323				"absolute_includes": `[
324        "subpackage/subsubpackage",
325        "subpackage2",
326        "subpackage3/subsubpackage",
327    ]`,
328				"export_includes": `["./exported_subsubpackage"]`,
329				"local_includes": `[
330        "subsubpackage2",
331        ".",
332    ]`,
333			})},
334	})
335}
336
337func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) {
338	runCcLibraryStaticTestCase(t, bp2buildTestCase{
339		description: "cc_library_static include_build_directory disabled",
340		filesystem: map[string]string{
341			// subpackage with subdirectory
342			"subpackage/Android.bp":                         "",
343			"subpackage/subpackage_header.h":                "",
344			"subpackage/subdirectory/subdirectory_header.h": "",
345		},
346		blueprint: soongCcLibraryStaticPreamble + `
347cc_library_static {
348    name: "foo_static",
349    include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
350    local_include_dirs: ["subpackage2"],
351    include_build_directory: false,
352}`,
353		expectedBazelTargets: []string{
354			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
355				"absolute_includes": `["subpackage"]`,
356				"local_includes":    `["subpackage2"]`,
357			}),
358		},
359	})
360}
361
362func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) {
363	runCcLibraryStaticTestCase(t, bp2buildTestCase{
364		description: "cc_library_static include_build_directory enabled",
365		filesystem: map[string]string{
366			// subpackage with subdirectory
367			"subpackage/Android.bp":                         "",
368			"subpackage/subpackage_header.h":                "",
369			"subpackage2/Android.bp":                        "",
370			"subpackage2/subpackage2_header.h":              "",
371			"subpackage/subdirectory/subdirectory_header.h": "",
372		},
373		blueprint: soongCcLibraryStaticPreamble + `
374cc_library_static {
375    name: "foo_static",
376    include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
377    local_include_dirs: ["subpackage2"],
378    include_build_directory: true,
379}`,
380		expectedBazelTargets: []string{
381			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
382				"absolute_includes": `["subpackage"]`,
383				"local_includes": `[
384        "subpackage2",
385        ".",
386    ]`,
387			}),
388		},
389	})
390}
391
392func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) {
393	runCcLibraryStaticTestCase(t, bp2buildTestCase{
394		description: "cc_library_static arch-specific static_libs",
395		filesystem:  map[string]string{},
396		blueprint: soongCcLibraryStaticPreamble + `
397cc_library_static {
398    name: "static_dep",
399    bazel_module: { bp2build_available: false },
400}
401cc_library_static {
402    name: "static_dep2",
403    bazel_module: { bp2build_available: false },
404}
405cc_library_static {
406    name: "foo_static",
407    arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
408    include_build_directory: false,
409}`,
410		expectedBazelTargets: []string{
411			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
412				"implementation_deps": `select({
413        "//build/bazel/platforms/arch:arm64": [":static_dep"],
414        "//conditions:default": [],
415    })`,
416				"whole_archive_deps": `select({
417        "//build/bazel/platforms/arch:arm64": [":static_dep2"],
418        "//conditions:default": [],
419    })`,
420			}),
421		},
422	})
423}
424
425func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) {
426	runCcLibraryStaticTestCase(t, bp2buildTestCase{
427		description: "cc_library_static os-specific static_libs",
428		filesystem:  map[string]string{},
429		blueprint: soongCcLibraryStaticPreamble + `
430cc_library_static {
431    name: "static_dep",
432    bazel_module: { bp2build_available: false },
433}
434cc_library_static {
435    name: "static_dep2",
436    bazel_module: { bp2build_available: false },
437}
438cc_library_static {
439    name: "foo_static",
440    target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
441    include_build_directory: false,
442}`,
443		expectedBazelTargets: []string{
444			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
445				"implementation_deps": `select({
446        "//build/bazel/platforms/os:android": [":static_dep"],
447        "//conditions:default": [],
448    })`,
449				"whole_archive_deps": `select({
450        "//build/bazel/platforms/os:android": [":static_dep2"],
451        "//conditions:default": [],
452    })`,
453			}),
454		},
455	})
456}
457
458func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) {
459	runCcLibraryStaticTestCase(t, bp2buildTestCase{
460		description: "cc_library_static base, arch and os-specific static_libs",
461		filesystem:  map[string]string{},
462		blueprint: soongCcLibraryStaticPreamble + `
463cc_library_static {
464    name: "static_dep",
465    bazel_module: { bp2build_available: false },
466}
467cc_library_static {
468    name: "static_dep2",
469    bazel_module: { bp2build_available: false },
470}
471cc_library_static {
472    name: "static_dep3",
473    bazel_module: { bp2build_available: false },
474}
475cc_library_static {
476    name: "static_dep4",
477    bazel_module: { bp2build_available: false },
478}
479cc_library_static {
480    name: "foo_static",
481    static_libs: ["static_dep"],
482    whole_static_libs: ["static_dep2"],
483    target: { android: { static_libs: ["static_dep3"] } },
484    arch: { arm64: { static_libs: ["static_dep4"] } },
485    include_build_directory: false,
486}`,
487		expectedBazelTargets: []string{
488			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
489				"implementation_deps": `[":static_dep"] + select({
490        "//build/bazel/platforms/arch:arm64": [":static_dep4"],
491        "//conditions:default": [],
492    }) + select({
493        "//build/bazel/platforms/os:android": [":static_dep3"],
494        "//conditions:default": [],
495    })`,
496				"whole_archive_deps": `[":static_dep2"]`,
497			}),
498		},
499	})
500}
501
502func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) {
503	runCcLibraryStaticTestCase(t, bp2buildTestCase{
504		description: "cc_library_static simple exclude_srcs",
505		filesystem: map[string]string{
506			"common.c":       "",
507			"foo-a.c":        "",
508			"foo-excluded.c": "",
509		},
510		blueprint: soongCcLibraryStaticPreamble + `
511cc_library_static {
512    name: "foo_static",
513    srcs: ["common.c", "foo-*.c"],
514    exclude_srcs: ["foo-excluded.c"],
515    include_build_directory: false,
516}`,
517		expectedBazelTargets: []string{
518			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
519				"srcs_c": `[
520        "common.c",
521        "foo-a.c",
522    ]`,
523			}),
524		},
525	})
526}
527
528func TestCcLibraryStaticOneArchSrcs(t *testing.T) {
529	runCcLibraryStaticTestCase(t, bp2buildTestCase{
530		description: "cc_library_static one arch specific srcs",
531		filesystem: map[string]string{
532			"common.c":  "",
533			"foo-arm.c": "",
534		},
535		blueprint: soongCcLibraryStaticPreamble + `
536cc_library_static {
537    name: "foo_static",
538    srcs: ["common.c"],
539    arch: { arm: { srcs: ["foo-arm.c"] } },
540    include_build_directory: false,
541}`,
542		expectedBazelTargets: []string{
543			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
544				"srcs_c": `["common.c"] + select({
545        "//build/bazel/platforms/arch:arm": ["foo-arm.c"],
546        "//conditions:default": [],
547    })`,
548			}),
549		},
550	})
551}
552
553func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) {
554	runCcLibraryStaticTestCase(t, bp2buildTestCase{
555		description: "cc_library_static one arch specific srcs and exclude_srcs",
556		filesystem: map[string]string{
557			"common.c":           "",
558			"for-arm.c":          "",
559			"not-for-arm.c":      "",
560			"not-for-anything.c": "",
561		},
562		blueprint: soongCcLibraryStaticPreamble + `
563cc_library_static {
564    name: "foo_static",
565    srcs: ["common.c", "not-for-*.c"],
566    exclude_srcs: ["not-for-anything.c"],
567    arch: {
568        arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
569    },
570    include_build_directory: false,
571}`,
572		expectedBazelTargets: []string{
573			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
574				"srcs_c": `["common.c"] + select({
575        "//build/bazel/platforms/arch:arm": ["for-arm.c"],
576        "//conditions:default": ["not-for-arm.c"],
577    })`,
578			}),
579		},
580	})
581}
582
583func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) {
584	runCcLibraryStaticTestCase(t, bp2buildTestCase{
585		description: "cc_library_static arch specific exclude_srcs for 2 architectures",
586		filesystem: map[string]string{
587			"common.c":      "",
588			"for-arm.c":     "",
589			"for-x86.c":     "",
590			"not-for-arm.c": "",
591			"not-for-x86.c": "",
592		},
593		blueprint: soongCcLibraryStaticPreamble + `
594cc_library_static {
595    name: "foo_static",
596    srcs: ["common.c", "not-for-*.c"],
597    exclude_srcs: ["not-for-everything.c"],
598    arch: {
599        arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
600        x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
601    },
602    include_build_directory: false,
603} `,
604		expectedBazelTargets: []string{
605			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
606				"srcs_c": `["common.c"] + select({
607        "//build/bazel/platforms/arch:arm": [
608            "not-for-x86.c",
609            "for-arm.c",
610        ],
611        "//build/bazel/platforms/arch:x86": [
612            "not-for-arm.c",
613            "for-x86.c",
614        ],
615        "//conditions:default": [
616            "not-for-arm.c",
617            "not-for-x86.c",
618        ],
619    })`,
620			}),
621		},
622	})
623}
624
625func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) {
626	runCcLibraryStaticTestCase(t, bp2buildTestCase{
627		description: "cc_library_static arch specific exclude_srcs for 4 architectures",
628		filesystem: map[string]string{
629			"common.c":             "",
630			"for-arm.c":            "",
631			"for-arm64.c":          "",
632			"for-x86.c":            "",
633			"for-x86_64.c":         "",
634			"not-for-arm.c":        "",
635			"not-for-arm64.c":      "",
636			"not-for-x86.c":        "",
637			"not-for-x86_64.c":     "",
638			"not-for-everything.c": "",
639		},
640		blueprint: soongCcLibraryStaticPreamble + `
641cc_library_static {
642    name: "foo_static",
643    srcs: ["common.c", "not-for-*.c"],
644    exclude_srcs: ["not-for-everything.c"],
645    arch: {
646        arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
647        arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
648        x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
649        x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
650  },
651    include_build_directory: false,
652} `,
653		expectedBazelTargets: []string{
654			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
655				"srcs_c": `["common.c"] + select({
656        "//build/bazel/platforms/arch:arm": [
657            "not-for-arm64.c",
658            "not-for-x86.c",
659            "not-for-x86_64.c",
660            "for-arm.c",
661        ],
662        "//build/bazel/platforms/arch:arm64": [
663            "not-for-arm.c",
664            "not-for-x86.c",
665            "not-for-x86_64.c",
666            "for-arm64.c",
667        ],
668        "//build/bazel/platforms/arch:x86": [
669            "not-for-arm.c",
670            "not-for-arm64.c",
671            "not-for-x86_64.c",
672            "for-x86.c",
673        ],
674        "//build/bazel/platforms/arch:x86_64": [
675            "not-for-arm.c",
676            "not-for-arm64.c",
677            "not-for-x86.c",
678            "for-x86_64.c",
679        ],
680        "//conditions:default": [
681            "not-for-arm.c",
682            "not-for-arm64.c",
683            "not-for-x86.c",
684            "not-for-x86_64.c",
685        ],
686    })`,
687			}),
688		},
689	})
690}
691
692func TestCcLibraryStaticOneArchEmpty(t *testing.T) {
693	runCcLibraryStaticTestCase(t, bp2buildTestCase{
694		description: "cc_library_static one arch empty",
695		filesystem: map[string]string{
696			"common.cc":       "",
697			"foo-no-arm.cc":   "",
698			"foo-excluded.cc": "",
699		},
700		blueprint: soongCcLibraryStaticPreamble + `
701cc_library_static {
702    name: "foo_static",
703    srcs: ["common.cc", "foo-*.cc"],
704    exclude_srcs: ["foo-excluded.cc"],
705    arch: {
706        arm: { exclude_srcs: ["foo-no-arm.cc"] },
707    },
708    include_build_directory: false,
709}`,
710		expectedBazelTargets: []string{
711			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
712				"srcs": `["common.cc"] + select({
713        "//build/bazel/platforms/arch:arm": [],
714        "//conditions:default": ["foo-no-arm.cc"],
715    })`,
716			}),
717		},
718	})
719}
720
721func TestCcLibraryStaticOneArchEmptyOtherSet(t *testing.T) {
722	runCcLibraryStaticTestCase(t, bp2buildTestCase{
723		description: "cc_library_static one arch empty other set",
724		filesystem: map[string]string{
725			"common.cc":       "",
726			"foo-no-arm.cc":   "",
727			"x86-only.cc":     "",
728			"foo-excluded.cc": "",
729		},
730		blueprint: soongCcLibraryStaticPreamble + `
731cc_library_static {
732    name: "foo_static",
733    srcs: ["common.cc", "foo-*.cc"],
734    exclude_srcs: ["foo-excluded.cc"],
735    arch: {
736        arm: { exclude_srcs: ["foo-no-arm.cc"] },
737        x86: { srcs: ["x86-only.cc"] },
738    },
739    include_build_directory: false,
740}`,
741		expectedBazelTargets: []string{
742			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
743				"srcs": `["common.cc"] + select({
744        "//build/bazel/platforms/arch:arm": [],
745        "//build/bazel/platforms/arch:x86": [
746            "foo-no-arm.cc",
747            "x86-only.cc",
748        ],
749        "//conditions:default": ["foo-no-arm.cc"],
750    })`,
751			}),
752		},
753	})
754}
755
756func TestCcLibraryStaticMultipleDepSameName(t *testing.T) {
757	runCcLibraryStaticTestCase(t, bp2buildTestCase{
758		description: "cc_library_static multiple dep same name panic",
759		filesystem:  map[string]string{},
760		blueprint: soongCcLibraryStaticPreamble + `
761cc_library_static {
762    name: "static_dep",
763    bazel_module: { bp2build_available: false },
764}
765cc_library_static {
766    name: "foo_static",
767    static_libs: ["static_dep", "static_dep"],
768    include_build_directory: false,
769}`,
770		expectedBazelTargets: []string{
771			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
772				"implementation_deps": `[":static_dep"]`,
773			}),
774		},
775	})
776}
777
778func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) {
779	runCcLibraryStaticTestCase(t, bp2buildTestCase{
780		description: "cc_library_static 1 multilib srcs and exclude_srcs",
781		filesystem: map[string]string{
782			"common.c":        "",
783			"for-lib32.c":     "",
784			"not-for-lib32.c": "",
785		},
786		blueprint: soongCcLibraryStaticPreamble + `
787cc_library_static {
788    name: "foo_static",
789    srcs: ["common.c", "not-for-*.c"],
790    multilib: {
791        lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
792    },
793    include_build_directory: false,
794} `,
795		expectedBazelTargets: []string{
796			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
797				"srcs_c": `["common.c"] + select({
798        "//build/bazel/platforms/arch:arm": ["for-lib32.c"],
799        "//build/bazel/platforms/arch:x86": ["for-lib32.c"],
800        "//conditions:default": ["not-for-lib32.c"],
801    })`,
802			}),
803		},
804	})
805}
806
807func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) {
808	runCcLibraryStaticTestCase(t, bp2buildTestCase{
809		description: "cc_library_static 2 multilib srcs and exclude_srcs",
810		filesystem: map[string]string{
811			"common.c":        "",
812			"for-lib32.c":     "",
813			"for-lib64.c":     "",
814			"not-for-lib32.c": "",
815			"not-for-lib64.c": "",
816		},
817		blueprint: soongCcLibraryStaticPreamble + `
818cc_library_static {
819    name: "foo_static",
820    srcs: ["common.c", "not-for-*.c"],
821    multilib: {
822        lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
823        lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
824    },
825    include_build_directory: false,
826} `,
827		expectedBazelTargets: []string{
828			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
829				"srcs_c": `["common.c"] + select({
830        "//build/bazel/platforms/arch:arm": [
831            "not-for-lib64.c",
832            "for-lib32.c",
833        ],
834        "//build/bazel/platforms/arch:arm64": [
835            "not-for-lib32.c",
836            "for-lib64.c",
837        ],
838        "//build/bazel/platforms/arch:x86": [
839            "not-for-lib64.c",
840            "for-lib32.c",
841        ],
842        "//build/bazel/platforms/arch:x86_64": [
843            "not-for-lib32.c",
844            "for-lib64.c",
845        ],
846        "//conditions:default": [
847            "not-for-lib32.c",
848            "not-for-lib64.c",
849        ],
850    })`,
851			}),
852		},
853	})
854}
855
856func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) {
857	runCcLibraryStaticTestCase(t, bp2buildTestCase{
858		description: "cc_library_static arch and multilib srcs and exclude_srcs",
859		filesystem: map[string]string{
860			"common.c":             "",
861			"for-arm.c":            "",
862			"for-arm64.c":          "",
863			"for-x86.c":            "",
864			"for-x86_64.c":         "",
865			"for-lib32.c":          "",
866			"for-lib64.c":          "",
867			"not-for-arm.c":        "",
868			"not-for-arm64.c":      "",
869			"not-for-x86.c":        "",
870			"not-for-x86_64.c":     "",
871			"not-for-lib32.c":      "",
872			"not-for-lib64.c":      "",
873			"not-for-everything.c": "",
874		},
875		blueprint: soongCcLibraryStaticPreamble + `
876cc_library_static {
877   name: "foo_static",
878   srcs: ["common.c", "not-for-*.c"],
879   exclude_srcs: ["not-for-everything.c"],
880   arch: {
881       arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
882       arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
883       x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
884       x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
885   },
886   multilib: {
887       lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
888       lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
889   },
890    include_build_directory: false,
891}`,
892		expectedBazelTargets: []string{
893			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
894				"srcs_c": `["common.c"] + select({
895        "//build/bazel/platforms/arch:arm": [
896            "not-for-arm64.c",
897            "not-for-lib64.c",
898            "not-for-x86.c",
899            "not-for-x86_64.c",
900            "for-arm.c",
901            "for-lib32.c",
902        ],
903        "//build/bazel/platforms/arch:arm64": [
904            "not-for-arm.c",
905            "not-for-lib32.c",
906            "not-for-x86.c",
907            "not-for-x86_64.c",
908            "for-arm64.c",
909            "for-lib64.c",
910        ],
911        "//build/bazel/platforms/arch:x86": [
912            "not-for-arm.c",
913            "not-for-arm64.c",
914            "not-for-lib64.c",
915            "not-for-x86_64.c",
916            "for-x86.c",
917            "for-lib32.c",
918        ],
919        "//build/bazel/platforms/arch:x86_64": [
920            "not-for-arm.c",
921            "not-for-arm64.c",
922            "not-for-lib32.c",
923            "not-for-x86.c",
924            "for-x86_64.c",
925            "for-lib64.c",
926        ],
927        "//conditions:default": [
928            "not-for-arm.c",
929            "not-for-arm64.c",
930            "not-for-lib32.c",
931            "not-for-lib64.c",
932            "not-for-x86.c",
933            "not-for-x86_64.c",
934        ],
935    })`,
936			}),
937		},
938	})
939}
940
941func TestCcLibraryStaticGeneratedHeadersAllPartitions(t *testing.T) {
942	runCcLibraryStaticTestCase(t, bp2buildTestCase{
943		blueprint: soongCcLibraryStaticPreamble + `
944genrule {
945    name: "generated_hdr",
946    cmd: "nothing to see here",
947    bazel_module: { bp2build_available: false },
948}
949
950genrule {
951    name: "export_generated_hdr",
952    cmd: "nothing to see here",
953    bazel_module: { bp2build_available: false },
954}
955
956cc_library_static {
957    name: "foo_static",
958    srcs: ["cpp_src.cpp", "as_src.S", "c_src.c"],
959    generated_headers: ["generated_hdr", "export_generated_hdr"],
960    export_generated_headers: ["export_generated_hdr"],
961    include_build_directory: false,
962}`,
963		expectedBazelTargets: []string{
964			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
965				"export_includes": `["."]`,
966				"local_includes":  `["."]`,
967				"hdrs":            `[":export_generated_hdr"]`,
968				"srcs": `[
969        "cpp_src.cpp",
970        ":generated_hdr",
971    ]`,
972				"srcs_as": `[
973        "as_src.S",
974        ":generated_hdr",
975    ]`,
976				"srcs_c": `[
977        "c_src.c",
978        ":generated_hdr",
979    ]`,
980			}),
981		},
982	})
983}
984
985func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) {
986	runCcLibraryStaticTestCase(t, bp2buildTestCase{
987		description: "cc_library_static arch srcs/exclude_srcs with generated files",
988		filesystem: map[string]string{
989			"common.cpp":             "",
990			"for-x86.cpp":            "",
991			"not-for-x86.cpp":        "",
992			"not-for-everything.cpp": "",
993			"dep/Android.bp": simpleModuleDoNotConvertBp2build("genrule", "generated_src_other_pkg") +
994				simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg") +
995				simpleModuleDoNotConvertBp2build("genrule", "generated_src_other_pkg_x86") +
996				simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg_x86") +
997				simpleModuleDoNotConvertBp2build("genrule", "generated_hdr_other_pkg_android"),
998		},
999		blueprint: soongCcLibraryStaticPreamble +
1000			simpleModuleDoNotConvertBp2build("genrule", "generated_src") +
1001			simpleModuleDoNotConvertBp2build("genrule", "generated_src_not_x86") +
1002			simpleModuleDoNotConvertBp2build("genrule", "generated_src_android") +
1003			simpleModuleDoNotConvertBp2build("genrule", "generated_hdr") + `
1004cc_library_static {
1005    name: "foo_static",
1006    srcs: ["common.cpp", "not-for-*.cpp"],
1007    exclude_srcs: ["not-for-everything.cpp"],
1008    generated_sources: ["generated_src", "generated_src_other_pkg", "generated_src_not_x86"],
1009    generated_headers: ["generated_hdr", "generated_hdr_other_pkg"],
1010    export_generated_headers: ["generated_hdr_other_pkg"],
1011    arch: {
1012        x86: {
1013          srcs: ["for-x86.cpp"],
1014          exclude_srcs: ["not-for-x86.cpp"],
1015          generated_headers: ["generated_hdr_other_pkg_x86"],
1016          exclude_generated_sources: ["generated_src_not_x86"],
1017    export_generated_headers: ["generated_hdr_other_pkg_x86"],
1018        },
1019    },
1020    target: {
1021        android: {
1022            generated_sources: ["generated_src_android"],
1023            generated_headers: ["generated_hdr_other_pkg_android"],
1024    export_generated_headers: ["generated_hdr_other_pkg_android"],
1025        },
1026    },
1027
1028    include_build_directory: false,
1029}
1030`,
1031		expectedBazelTargets: []string{
1032			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
1033				"srcs": `[
1034        "common.cpp",
1035        ":generated_src",
1036        "//dep:generated_src_other_pkg",
1037        ":generated_hdr",
1038    ] + select({
1039        "//build/bazel/platforms/arch:x86": ["for-x86.cpp"],
1040        "//conditions:default": [
1041            "not-for-x86.cpp",
1042            ":generated_src_not_x86",
1043        ],
1044    }) + select({
1045        "//build/bazel/platforms/os:android": [":generated_src_android"],
1046        "//conditions:default": [],
1047    })`,
1048				"hdrs": `["//dep:generated_hdr_other_pkg"] + select({
1049        "//build/bazel/platforms/arch:x86": ["//dep:generated_hdr_other_pkg_x86"],
1050        "//conditions:default": [],
1051    }) + select({
1052        "//build/bazel/platforms/os:android": ["//dep:generated_hdr_other_pkg_android"],
1053        "//conditions:default": [],
1054    })`,
1055				"local_includes":           `["."]`,
1056				"export_absolute_includes": `["dep"]`,
1057			}),
1058		},
1059	})
1060}
1061
1062func TestCcLibraryStaticGetTargetProperties(t *testing.T) {
1063	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1064
1065		description: "cc_library_static complex GetTargetProperties",
1066		blueprint: soongCcLibraryStaticPreamble + `
1067cc_library_static {
1068    name: "foo_static",
1069    target: {
1070        android: {
1071            srcs: ["android_src.c"],
1072        },
1073        android_arm: {
1074            srcs: ["android_arm_src.c"],
1075        },
1076        android_arm64: {
1077            srcs: ["android_arm64_src.c"],
1078        },
1079        android_x86: {
1080            srcs: ["android_x86_src.c"],
1081        },
1082        android_x86_64: {
1083            srcs: ["android_x86_64_src.c"],
1084        },
1085        linux_bionic_arm64: {
1086            srcs: ["linux_bionic_arm64_src.c"],
1087        },
1088        linux_bionic_x86_64: {
1089            srcs: ["linux_bionic_x86_64_src.c"],
1090        },
1091    },
1092    include_build_directory: false,
1093}`,
1094		expectedBazelTargets: []string{
1095			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
1096				"srcs_c": `select({
1097        "//build/bazel/platforms/os:android": ["android_src.c"],
1098        "//conditions:default": [],
1099    }) + select({
1100        "//build/bazel/platforms/os_arch:android_arm": ["android_arm_src.c"],
1101        "//build/bazel/platforms/os_arch:android_arm64": ["android_arm64_src.c"],
1102        "//build/bazel/platforms/os_arch:android_x86": ["android_x86_src.c"],
1103        "//build/bazel/platforms/os_arch:android_x86_64": ["android_x86_64_src.c"],
1104        "//build/bazel/platforms/os_arch:linux_bionic_arm64": ["linux_bionic_arm64_src.c"],
1105        "//build/bazel/platforms/os_arch:linux_bionic_x86_64": ["linux_bionic_x86_64_src.c"],
1106        "//conditions:default": [],
1107    })`,
1108			}),
1109		},
1110	})
1111}
1112
1113func TestCcLibraryStaticProductVariableSelects(t *testing.T) {
1114	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1115		description: "cc_library_static product variable selects",
1116		blueprint: soongCcLibraryStaticPreamble + `
1117cc_library_static {
1118    name: "foo_static",
1119    srcs: ["common.c"],
1120    product_variables: {
1121      malloc_not_svelte: {
1122        cflags: ["-Wmalloc_not_svelte"],
1123      },
1124      malloc_zero_contents: {
1125        cflags: ["-Wmalloc_zero_contents"],
1126      },
1127      binder32bit: {
1128        cflags: ["-Wbinder32bit"],
1129      },
1130    },
1131    include_build_directory: false,
1132} `,
1133		expectedBazelTargets: []string{
1134			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
1135				"copts": `select({
1136        "//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
1137        "//conditions:default": [],
1138    }) + select({
1139        "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
1140        "//conditions:default": [],
1141    }) + select({
1142        "//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"],
1143        "//conditions:default": [],
1144    })`,
1145				"srcs_c": `["common.c"]`,
1146			}),
1147		},
1148	})
1149}
1150
1151func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) {
1152	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1153		description: "cc_library_static arch-specific product variable selects",
1154		filesystem:  map[string]string{},
1155		blueprint: soongCcLibraryStaticPreamble + `
1156cc_library_static {
1157    name: "foo_static",
1158    srcs: ["common.c"],
1159    product_variables: {
1160      malloc_not_svelte: {
1161        cflags: ["-Wmalloc_not_svelte"],
1162      },
1163    },
1164    arch: {
1165        arm64: {
1166            product_variables: {
1167                malloc_not_svelte: {
1168                    cflags: ["-Warm64_malloc_not_svelte"],
1169                },
1170            },
1171        },
1172    },
1173    multilib: {
1174        lib32: {
1175            product_variables: {
1176                malloc_not_svelte: {
1177                    cflags: ["-Wlib32_malloc_not_svelte"],
1178                },
1179            },
1180        },
1181    },
1182    target: {
1183        android: {
1184            product_variables: {
1185                malloc_not_svelte: {
1186                    cflags: ["-Wandroid_malloc_not_svelte"],
1187                },
1188            },
1189        }
1190    },
1191    include_build_directory: false,
1192} `,
1193		expectedBazelTargets: []string{
1194			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
1195				"copts": `select({
1196        "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
1197        "//conditions:default": [],
1198    }) + select({
1199        "//build/bazel/product_variables:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
1200        "//conditions:default": [],
1201    }) + select({
1202        "//build/bazel/product_variables:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
1203        "//conditions:default": [],
1204    }) + select({
1205        "//build/bazel/product_variables:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
1206        "//conditions:default": [],
1207    }) + select({
1208        "//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
1209        "//conditions:default": [],
1210    })`,
1211				"srcs_c": `["common.c"]`,
1212			}),
1213		},
1214	})
1215}
1216
1217func TestCcLibraryStaticProductVariableStringReplacement(t *testing.T) {
1218	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1219		description: "cc_library_static product variable string replacement",
1220		filesystem:  map[string]string{},
1221		blueprint: soongCcLibraryStaticPreamble + `
1222cc_library_static {
1223    name: "foo_static",
1224    srcs: ["common.S"],
1225    product_variables: {
1226      platform_sdk_version: {
1227          asflags: ["-DPLATFORM_SDK_VERSION=%d"],
1228      },
1229    },
1230    include_build_directory: false,
1231} `,
1232		expectedBazelTargets: []string{
1233			makeBazelTarget("cc_library_static", "foo_static", attrNameToString{
1234				"asflags": `select({
1235        "//build/bazel/product_variables:platform_sdk_version": ["-DPLATFORM_SDK_VERSION=$(Platform_sdk_version)"],
1236        "//conditions:default": [],
1237    })`,
1238				"srcs_as": `["common.S"]`,
1239			}),
1240		},
1241	})
1242}
1243
1244func TestStaticLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1245	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1246		description: "cc_library_static system_shared_lib empty root",
1247		blueprint: soongCcLibraryStaticPreamble + `
1248cc_library_static {
1249    name: "root_empty",
1250    system_shared_libs: [],
1251    include_build_directory: false,
1252}
1253`,
1254		expectedBazelTargets: []string{
1255			makeBazelTarget("cc_library_static", "root_empty", attrNameToString{
1256				"system_dynamic_deps": `[]`,
1257			}),
1258		},
1259	})
1260}
1261
1262func TestStaticLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1263	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1264		description: "cc_library_static system_shared_lib empty static default",
1265		blueprint: soongCcLibraryStaticPreamble + `
1266cc_defaults {
1267    name: "static_empty_defaults",
1268    static: {
1269        system_shared_libs: [],
1270    },
1271    include_build_directory: false,
1272}
1273cc_library_static {
1274    name: "static_empty",
1275    defaults: ["static_empty_defaults"],
1276}
1277`,
1278		expectedBazelTargets: []string{
1279			makeBazelTarget("cc_library_static", "static_empty", attrNameToString{
1280				"system_dynamic_deps": `[]`,
1281			}),
1282		},
1283	})
1284}
1285
1286func TestStaticLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1287	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1288		description: "cc_library_static system_shared_lib empty for bionic variant",
1289		blueprint: soongCcLibraryStaticPreamble + `
1290cc_library_static {
1291    name: "target_bionic_empty",
1292    target: {
1293        bionic: {
1294            system_shared_libs: [],
1295        },
1296    },
1297    include_build_directory: false,
1298}
1299`,
1300		expectedBazelTargets: []string{
1301			makeBazelTarget("cc_library_static", "target_bionic_empty", attrNameToString{
1302				"system_dynamic_deps": `[]`,
1303			}),
1304		},
1305	})
1306}
1307
1308func TestStaticLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1309	// Note that this behavior is technically incorrect (it's a simplification).
1310	// The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1311	// only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1312	// b/195791252 tracks the fix.
1313	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1314		description: "cc_library_static system_shared_lib empty for linux_bionic variant",
1315		blueprint: soongCcLibraryStaticPreamble + `
1316cc_library_static {
1317    name: "target_linux_bionic_empty",
1318    target: {
1319        linux_bionic: {
1320            system_shared_libs: [],
1321        },
1322    },
1323    include_build_directory: false,
1324}
1325`,
1326		expectedBazelTargets: []string{
1327			makeBazelTarget("cc_library_static", "target_linux_bionic_empty", attrNameToString{
1328				"system_dynamic_deps": `[]`,
1329			}),
1330		},
1331	})
1332}
1333
1334func TestStaticLibrary_SystemSharedLibsBionic(t *testing.T) {
1335	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1336		description: "cc_library_static system_shared_libs set for bionic variant",
1337		blueprint: soongCcLibraryStaticPreamble +
1338			simpleModuleDoNotConvertBp2build("cc_library", "libc") + `
1339cc_library_static {
1340    name: "target_bionic",
1341    target: {
1342        bionic: {
1343            system_shared_libs: ["libc"],
1344        },
1345    },
1346    include_build_directory: false,
1347}
1348`,
1349		expectedBazelTargets: []string{
1350			makeBazelTarget("cc_library_static", "target_bionic", attrNameToString{
1351				"system_dynamic_deps": `select({
1352        "//build/bazel/platforms/os:android": [":libc"],
1353        "//build/bazel/platforms/os:linux_bionic": [":libc"],
1354        "//conditions:default": [],
1355    })`,
1356			}),
1357		},
1358	})
1359}
1360
1361func TestStaticLibrary_SystemSharedLibsLinuxRootAndLinuxBionic(t *testing.T) {
1362	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1363		description: "cc_library_static system_shared_libs set for root and linux_bionic variant",
1364		blueprint: soongCcLibraryStaticPreamble +
1365			simpleModuleDoNotConvertBp2build("cc_library", "libc") +
1366			simpleModuleDoNotConvertBp2build("cc_library", "libm") + `
1367cc_library_static {
1368    name: "target_linux_bionic",
1369    system_shared_libs: ["libc"],
1370    target: {
1371        linux_bionic: {
1372            system_shared_libs: ["libm"],
1373        },
1374    },
1375    include_build_directory: false,
1376}
1377`,
1378		expectedBazelTargets: []string{
1379			makeBazelTarget("cc_library_static", "target_linux_bionic", attrNameToString{
1380				"system_dynamic_deps": `[":libc"] + select({
1381        "//build/bazel/platforms/os:linux_bionic": [":libm"],
1382        "//conditions:default": [],
1383    })`,
1384			}),
1385		},
1386	})
1387}
1388
1389func TestCcLibrarystatic_SystemSharedLibUsedAsDep(t *testing.T) {
1390	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1391		description: "cc_library_static system_shared_lib empty for linux_bionic variant",
1392		blueprint: soongCcLibraryStaticPreamble +
1393			simpleModuleDoNotConvertBp2build("cc_library", "libc") + `
1394cc_library_static {
1395    name: "used_in_bionic_oses",
1396    target: {
1397        android: {
1398            shared_libs: ["libc"],
1399        },
1400        linux_bionic: {
1401            shared_libs: ["libc"],
1402        },
1403    },
1404    include_build_directory: false,
1405}
1406
1407cc_library_static {
1408    name: "all",
1409    shared_libs: ["libc"],
1410    include_build_directory: false,
1411}
1412
1413cc_library_static {
1414    name: "keep_for_empty_system_shared_libs",
1415    shared_libs: ["libc"],
1416		system_shared_libs: [],
1417    include_build_directory: false,
1418}
1419`,
1420		expectedBazelTargets: []string{
1421			makeBazelTarget("cc_library_static", "all", attrNameToString{
1422				"implementation_dynamic_deps": `select({
1423        "//build/bazel/platforms/os:android": [],
1424        "//build/bazel/platforms/os:linux_bionic": [],
1425        "//conditions:default": [":libc"],
1426    })`,
1427			}),
1428			makeBazelTarget("cc_library_static", "keep_for_empty_system_shared_libs", attrNameToString{
1429				"implementation_dynamic_deps": `[":libc"]`,
1430				"system_dynamic_deps":         `[]`,
1431			}),
1432			makeBazelTarget("cc_library_static", "used_in_bionic_oses", attrNameToString{}),
1433		},
1434	})
1435}
1436
1437func TestCcLibraryStaticProto(t *testing.T) {
1438	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1439		blueprint: soongCcProtoPreamble + `cc_library_static {
1440	name: "foo",
1441	srcs: ["foo.proto"],
1442	proto: {
1443		export_proto_headers: true,
1444	},
1445	include_build_directory: false,
1446}`,
1447		expectedBazelTargets: []string{
1448			makeBazelTarget("proto_library", "foo_proto", attrNameToString{
1449				"srcs": `["foo.proto"]`,
1450			}), makeBazelTarget("cc_lite_proto_library", "foo_cc_proto_lite", attrNameToString{
1451				"deps": `[":foo_proto"]`,
1452			}), makeBazelTarget("cc_library_static", "foo", attrNameToString{
1453				"deps":               `[":libprotobuf-cpp-lite"]`,
1454				"whole_archive_deps": `[":foo_cc_proto_lite"]`,
1455			}),
1456		},
1457	})
1458}
1459
1460func TestCcLibraryStaticUseVersionLib(t *testing.T) {
1461	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1462		blueprint: soongCcProtoPreamble + `cc_library_static {
1463	name: "foo",
1464	use_version_lib: true,
1465	include_build_directory: false,
1466}`,
1467		expectedBazelTargets: []string{
1468			makeBazelTarget("cc_library_static", "foo", attrNameToString{
1469				"use_version_lib": "True",
1470			}),
1471		},
1472	})
1473}
1474
1475func TestCcLibraryStaticStdInFlags(t *testing.T) {
1476	runCcLibraryStaticTestCase(t, bp2buildTestCase{
1477		blueprint: soongCcProtoPreamble + `cc_library_static {
1478	name: "foo",
1479	cflags: ["-std=candcpp"],
1480	conlyflags: ["-std=conly"],
1481	cppflags: ["-std=cpp"],
1482	include_build_directory: false,
1483}`,
1484		expectedBazelTargets: []string{
1485			makeBazelTarget("cc_library_static", "foo", attrNameToString{
1486				"conlyflags": `["-std=conly"]`,
1487				"cppflags":   `["-std=cpp"]`,
1488			}),
1489		},
1490	})
1491}
1492