blob: bff192f539fd7a0783fe0616aaa6af52634bae51 [file] [log] [blame]
Jingwen Chen63930982021-03-24 10:04:33 -04001// 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 soongCcLibraryPreamble = `
27cc_defaults {
Liz Kammerd366c902021-06-03 13:43:01 -040028 name: "linux_bionic_supported",
Jingwen Chen63930982021-03-24 10:04:33 -040029}
30
31toolchain_library {
Liz Kammerd366c902021-06-03 13:43:01 -040032 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: "",
Jingwen Chen63930982021-03-24 10:04:33 -040040}`
41)
42
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020043func runCcLibraryTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040044 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020045 runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
46}
47
48func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
49 cc.RegisterCCBuildComponents(ctx)
Jingwen Chen14a8bda2021-06-02 11:10:02 +000050 ctx.RegisterModuleType("filegroup", android.FileGroupFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020051 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
Liz Kammer2d7bbe32021-06-10 18:20:06 -040052 ctx.RegisterModuleType("cc_prebuilt_library_static", cc.PrebuiltStaticLibraryFactory)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020053 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
54 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
55}
56
57func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040058 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020059 dir := "."
60 filesystem := make(map[string][]byte)
61 toParse := []string{
62 "Android.bp",
63 }
64 for f, content := range tc.filesystem {
65 if strings.HasSuffix(f, "Android.bp") {
66 toParse = append(toParse, f)
67 }
68 filesystem[f] = []byte(content)
69 }
70 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
71 ctx := android.NewTestContext(config)
72
73 registerModuleTypes(ctx)
74 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
75 ctx.RegisterBp2BuildConfig(bp2buildConfig)
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020076 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
77 ctx.RegisterForBazelConversion()
78
79 _, errs := ctx.ParseFileList(dir, toParse)
80 if errored(t, tc.description, errs) {
81 return
82 }
83 _, errs = ctx.ResolveDependencies(config)
84 if errored(t, tc.description, errs) {
85 return
86 }
87
88 checkDir := dir
89 if tc.dir != "" {
90 checkDir = tc.dir
91 }
92 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
93 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
94 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
95 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
96 } else {
97 for i, target := range bazelTargets {
98 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
99 t.Errorf(
100 "%s: Expected generated Bazel target to be '%s', got '%s'",
101 tc.description,
102 w,
103 g,
104 )
105 }
106 }
107 }
108}
109
110func TestCcLibrarySimple(t *testing.T) {
111 runCcLibraryTestCase(t, bp2buildTestCase{
112 description: "cc_library - simple example",
113 moduleTypeUnderTest: "cc_library",
114 moduleTypeUnderTestFactory: cc.LibraryFactory,
115 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
116 filesystem: map[string]string{
117 "android.cpp": "",
Liz Kammer01a16e82021-07-16 16:33:47 -0400118 "bionic.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200119 "darwin.cpp": "",
120 // Refer to cc.headerExts for the supported header extensions in Soong.
121 "header.h": "",
122 "header.hh": "",
123 "header.hpp": "",
124 "header.hxx": "",
125 "header.h++": "",
126 "header.inl": "",
127 "header.inc": "",
128 "header.ipp": "",
129 "header.h.generic": "",
130 "impl.cpp": "",
131 "linux.cpp": "",
132 "x86.cpp": "",
133 "x86_64.cpp": "",
134 "foo-dir/a.h": "",
135 },
136 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400137cc_library_headers { name: "some-headers" }
138cc_library {
139 name: "foo-lib",
140 srcs: ["impl.cpp"],
141 cflags: ["-Wall"],
142 header_libs: ["some-headers"],
143 export_include_dirs: ["foo-dir"],
144 ldflags: ["-Wl,--exclude-libs=bar.a"],
145 arch: {
146 x86: {
147 ldflags: ["-Wl,--exclude-libs=baz.a"],
148 srcs: ["x86.cpp"],
149 },
150 x86_64: {
151 ldflags: ["-Wl,--exclude-libs=qux.a"],
152 srcs: ["x86_64.cpp"],
153 },
154 },
155 target: {
156 android: {
157 srcs: ["android.cpp"],
158 },
159 linux_glibc: {
160 srcs: ["linux.cpp"],
161 },
162 darwin: {
163 srcs: ["darwin.cpp"],
164 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400165 bionic: {
166 srcs: ["bionic.cpp"]
167 },
Jingwen Chen63930982021-03-24 10:04:33 -0400168 },
169}
170`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200171 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400172 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000173 copts = [
174 "-Wall",
175 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400176 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000177 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400178 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000179 includes = ["foo-dir"],
180 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
181 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
182 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
183 "//conditions:default": [],
184 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000185 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000186 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
187 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400188 "//conditions:default": [],
189 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000190 "//build/bazel/platforms/os:android": ["android.cpp"],
191 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
192 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400193 "//conditions:default": [],
Liz Kammer01a16e82021-07-16 16:33:47 -0400194 }) + select({
195 "//build/bazel/platforms/os:bionic": ["bionic.cpp"],
196 "//conditions:default": [],
Jingwen Chen63930982021-03-24 10:04:33 -0400197 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200198)`}})
199}
200
201func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
202 runCcLibraryTestCase(t, bp2buildTestCase{
203 description: "cc_library - trimmed example of //bionic/linker:ld-android",
204 moduleTypeUnderTest: "cc_library",
205 moduleTypeUnderTestFactory: cc.LibraryFactory,
206 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
207 filesystem: map[string]string{
208 "ld-android.cpp": "",
209 "linked_list.h": "",
210 "linker.h": "",
211 "linker_block_allocator.h": "",
212 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400213 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200214 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400215cc_library_headers { name: "libc_headers" }
216cc_library {
217 name: "fake-ld-android",
218 srcs: ["ld_android.cpp"],
219 cflags: [
220 "-Wall",
221 "-Wextra",
222 "-Wunused",
223 "-Werror",
224 ],
225 header_libs: ["libc_headers"],
226 ldflags: [
227 "-Wl,--exclude-libs=libgcc.a",
228 "-Wl,--exclude-libs=libgcc_stripped.a",
229 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
230 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
231 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
232 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
233 ],
234 arch: {
235 x86: {
236 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
237 },
238 x86_64: {
239 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
240 },
241 },
242}
243`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200244 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400245 name = "fake-ld-android",
246 copts = [
247 "-Wall",
248 "-Wextra",
249 "-Wunused",
250 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000251 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400252 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400253 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400254 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400255 linkopts = [
256 "-Wl,--exclude-libs=libgcc.a",
257 "-Wl,--exclude-libs=libgcc_stripped.a",
258 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
259 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
260 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
261 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
262 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000263 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
264 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400265 "//conditions:default": [],
266 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000267 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400268)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200269 })
270}
271
272func TestCcLibraryExcludeSrcs(t *testing.T) {
273 runCcLibraryTestCase(t, bp2buildTestCase{
274 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
275 moduleTypeUnderTest: "cc_library",
276 moduleTypeUnderTestFactory: cc.LibraryFactory,
277 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
278 dir: "external",
279 filesystem: map[string]string{
280 "external/math/cosf.c": "",
281 "external/math/erf.c": "",
282 "external/math/erf_data.c": "",
283 "external/math/erff.c": "",
284 "external/math/erff_data.c": "",
285 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000286cc_library {
287 name: "fake-libarm-optimized-routines-math",
288 exclude_srcs: [
289 // Provided by:
290 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
291 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
292 "math/erf.c",
293 "math/erf_data.c",
294 "math/erff.c",
295 "math/erff_data.c",
296 ],
297 srcs: [
298 "math/*.c",
299 ],
300 // arch-specific settings
301 arch: {
302 arm64: {
303 cflags: [
304 "-DHAVE_FAST_FMA=1",
305 ],
306 },
307 },
308 bazel_module: { bp2build_available: true },
309}
310`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200311 },
312 blueprint: soongCcLibraryPreamble,
313 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000314 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400315 copts = [
316 "-Iexternal",
317 "-I$(BINDIR)/external",
318 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000319 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
320 "//conditions:default": [],
321 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400322 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000323)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200324 })
325}
326
327func TestCcLibrarySharedStaticProps(t *testing.T) {
328 runCcLibraryTestCase(t, bp2buildTestCase{
329 description: "cc_library shared/static props",
330 moduleTypeUnderTest: "cc_library",
331 moduleTypeUnderTestFactory: cc.LibraryFactory,
332 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200333 dir: "foo/bar",
334 filesystem: map[string]string{
335 "foo/bar/both.cpp": "",
336 "foo/bar/sharedonly.cpp": "",
337 "foo/bar/staticonly.cpp": "",
338 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000339cc_library {
340 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400341 srcs: ["both.cpp"],
342 cflags: ["bothflag"],
343 shared_libs: ["shared_dep_for_both"],
344 static_libs: ["static_dep_for_both"],
345 whole_static_libs: ["whole_static_lib_for_both"],
346 static: {
347 srcs: ["staticonly.cpp"],
348 cflags: ["staticflag"],
349 shared_libs: ["shared_dep_for_static"],
350 static_libs: ["static_dep_for_static"],
351 whole_static_libs: ["whole_static_lib_for_static"],
352 },
353 shared: {
354 srcs: ["sharedonly.cpp"],
355 cflags: ["sharedflag"],
356 shared_libs: ["shared_dep_for_shared"],
357 static_libs: ["static_dep_for_shared"],
358 whole_static_libs: ["whole_static_lib_for_shared"],
359 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000360 bazel_module: { bp2build_available: true },
361}
362
Chris Parsons08648312021-05-06 16:23:19 -0400363cc_library_static { name: "static_dep_for_shared" }
364
365cc_library_static { name: "static_dep_for_static" }
366
367cc_library_static { name: "static_dep_for_both" }
368
369cc_library_static { name: "whole_static_lib_for_shared" }
370
371cc_library_static { name: "whole_static_lib_for_static" }
372
373cc_library_static { name: "whole_static_lib_for_both" }
374
375cc_library { name: "shared_dep_for_shared" }
376
377cc_library { name: "shared_dep_for_static" }
378
379cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000380`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200381 },
382 blueprint: soongCcLibraryPreamble,
383 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000384 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400385 copts = [
386 "bothflag",
387 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400388 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400389 ],
Chris Parsons08648312021-05-06 16:23:19 -0400390 dynamic_deps = [":shared_dep_for_both"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400391 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000392 shared = {
393 "copts": ["sharedflag"],
394 "dynamic_deps": [":shared_dep_for_shared"],
395 "srcs": ["sharedonly.cpp"],
396 "static_deps": [":static_dep_for_shared"],
397 "whole_archive_deps": [":whole_static_lib_for_shared"],
398 },
Chris Parsons08648312021-05-06 16:23:19 -0400399 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000400 static = {
401 "copts": ["staticflag"],
402 "dynamic_deps": [":shared_dep_for_static"],
403 "srcs": ["staticonly.cpp"],
404 "static_deps": [":static_dep_for_static"],
405 "whole_archive_deps": [":whole_static_lib_for_static"],
406 },
Chris Parsons08648312021-05-06 16:23:19 -0400407 whole_archive_deps = [":whole_static_lib_for_both"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000408)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200409 })
410}
411
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400412func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
413 runCcLibraryTestCase(t, bp2buildTestCase{
414 moduleTypeUnderTest: "cc_library",
415 moduleTypeUnderTestFactory: cc.LibraryFactory,
416 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400417 dir: "foo/bar",
418 filesystem: map[string]string{
419 "foo/bar/Android.bp": `
420cc_library {
421 name: "a",
422 whole_static_libs: ["whole_static_lib_for_both"],
423 static: {
424 whole_static_libs: ["whole_static_lib_for_static"],
425 },
426 shared: {
427 whole_static_libs: ["whole_static_lib_for_shared"],
428 },
429 bazel_module: { bp2build_available: true },
430}
431
432cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
433
434cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
435
436cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
437`,
438 },
439 blueprint: soongCcLibraryPreamble,
440 expectedBazelTargets: []string{`cc_library(
441 name = "a",
442 copts = [
443 "-Ifoo/bar",
444 "-I$(BINDIR)/foo/bar",
445 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000446 shared = {
447 "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
448 },
449 static = {
450 "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
451 },
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400452 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400453)`},
454 })
455}
456
Jingwen Chenbcf53042021-05-26 04:42:42 +0000457func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
458 runCcLibraryTestCase(t, bp2buildTestCase{
459 description: "cc_library shared/static props in arch",
460 moduleTypeUnderTest: "cc_library",
461 moduleTypeUnderTestFactory: cc.LibraryFactory,
462 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chenbcf53042021-05-26 04:42:42 +0000463 dir: "foo/bar",
464 filesystem: map[string]string{
465 "foo/bar/arm.cpp": "",
466 "foo/bar/x86.cpp": "",
467 "foo/bar/sharedonly.cpp": "",
468 "foo/bar/staticonly.cpp": "",
469 "foo/bar/Android.bp": `
470cc_library {
471 name: "a",
472 arch: {
473 arm: {
474 shared: {
475 srcs: ["arm_shared.cpp"],
476 cflags: ["-DARM_SHARED"],
477 static_libs: ["arm_static_dep_for_shared"],
478 whole_static_libs: ["arm_whole_static_dep_for_shared"],
479 shared_libs: ["arm_shared_dep_for_shared"],
480 },
481 },
482 x86: {
483 static: {
484 srcs: ["x86_static.cpp"],
485 cflags: ["-DX86_STATIC"],
486 static_libs: ["x86_dep_for_static"],
487 },
488 },
489 },
490 target: {
491 android: {
492 shared: {
493 srcs: ["android_shared.cpp"],
494 cflags: ["-DANDROID_SHARED"],
495 static_libs: ["android_dep_for_shared"],
496 },
497 },
498 android_arm: {
499 shared: {
500 cflags: ["-DANDROID_ARM_SHARED"],
501 },
502 },
503 },
504 srcs: ["both.cpp"],
505 cflags: ["bothflag"],
506 static_libs: ["static_dep_for_both"],
507 static: {
508 srcs: ["staticonly.cpp"],
509 cflags: ["staticflag"],
510 static_libs: ["static_dep_for_static"],
511 },
512 shared: {
513 srcs: ["sharedonly.cpp"],
514 cflags: ["sharedflag"],
515 static_libs: ["static_dep_for_shared"],
516 },
517 bazel_module: { bp2build_available: true },
518}
519
520cc_library_static { name: "static_dep_for_shared" }
521cc_library_static { name: "static_dep_for_static" }
522cc_library_static { name: "static_dep_for_both" }
523
524cc_library_static { name: "arm_static_dep_for_shared" }
525cc_library_static { name: "arm_whole_static_dep_for_shared" }
526cc_library_static { name: "arm_shared_dep_for_shared" }
527
528cc_library_static { name: "x86_dep_for_static" }
529
530cc_library_static { name: "android_dep_for_shared" }
531`,
532 },
533 blueprint: soongCcLibraryPreamble,
534 expectedBazelTargets: []string{`cc_library(
535 name = "a",
536 copts = [
537 "bothflag",
538 "-Ifoo/bar",
539 "-I$(BINDIR)/foo/bar",
540 ],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000541 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000542 shared = {
543 "copts": ["sharedflag"] + select({
544 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
545 "//conditions:default": [],
546 }) + select({
547 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
548 "//conditions:default": [],
549 }) + select({
550 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
551 "//conditions:default": [],
552 }),
553 "dynamic_deps": select({
554 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
555 "//conditions:default": [],
556 }),
557 "srcs": ["sharedonly.cpp"] + select({
558 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
559 "//conditions:default": [],
560 }) + select({
561 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
562 "//conditions:default": [],
563 }),
564 "static_deps": [":static_dep_for_shared"] + select({
565 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
566 "//conditions:default": [],
567 }) + select({
568 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
569 "//conditions:default": [],
570 }),
571 "whole_archive_deps": select({
572 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
573 "//conditions:default": [],
574 }),
575 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000576 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000577 static = {
578 "copts": ["staticflag"] + select({
579 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
580 "//conditions:default": [],
581 }),
582 "srcs": ["staticonly.cpp"] + select({
583 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
584 "//conditions:default": [],
585 }),
586 "static_deps": [":static_dep_for_static"] + select({
587 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
588 "//conditions:default": [],
589 }),
590 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000591)`},
592 })
593}
594
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000595func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
596 runCcLibraryTestCase(t, bp2buildTestCase{
597 description: "cc_library shared/static props with c/cpp/s mixed sources",
598 moduleTypeUnderTest: "cc_library",
599 moduleTypeUnderTestFactory: cc.LibraryFactory,
600 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000601 dir: "foo/bar",
602 filesystem: map[string]string{
603 "foo/bar/both_source.cpp": "",
604 "foo/bar/both_source.cc": "",
605 "foo/bar/both_source.c": "",
606 "foo/bar/both_source.s": "",
607 "foo/bar/both_source.S": "",
608 "foo/bar/shared_source.cpp": "",
609 "foo/bar/shared_source.cc": "",
610 "foo/bar/shared_source.c": "",
611 "foo/bar/shared_source.s": "",
612 "foo/bar/shared_source.S": "",
613 "foo/bar/static_source.cpp": "",
614 "foo/bar/static_source.cc": "",
615 "foo/bar/static_source.c": "",
616 "foo/bar/static_source.s": "",
617 "foo/bar/static_source.S": "",
618 "foo/bar/Android.bp": `
619cc_library {
620 name: "a",
621 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400622 "both_source.cpp",
623 "both_source.cc",
624 "both_source.c",
625 "both_source.s",
626 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000627 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400628 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000629 static: {
Liz Kammerd366c902021-06-03 13:43:01 -0400630 srcs: [
631 "static_source.cpp",
632 "static_source.cc",
633 "static_source.c",
634 "static_source.s",
635 "static_source.S",
636 ":static_filegroup",
637 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000638 },
639 shared: {
Liz Kammerd366c902021-06-03 13:43:01 -0400640 srcs: [
641 "shared_source.cpp",
642 "shared_source.cc",
643 "shared_source.c",
644 "shared_source.s",
645 "shared_source.S",
646 ":shared_filegroup",
647 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000648 },
649 bazel_module: { bp2build_available: true },
650}
651
652filegroup {
653 name: "both_filegroup",
654 srcs: [
655 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400656 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000657}
658
659filegroup {
660 name: "shared_filegroup",
661 srcs: [
662 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400663 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000664}
665
666filegroup {
667 name: "static_filegroup",
668 srcs: [
669 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400670 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000671}
672`,
673 },
674 blueprint: soongCcLibraryPreamble,
675 expectedBazelTargets: []string{`cc_library(
676 name = "a",
Chris Parsons69fa9f92021-07-13 11:47:44 -0400677 asflags = [
678 "-Ifoo/bar",
679 "-I$(BINDIR)/foo/bar",
680 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000681 copts = [
682 "-Ifoo/bar",
683 "-I$(BINDIR)/foo/bar",
684 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000685 shared = {
686 "srcs": [
687 ":shared_filegroup_cpp_srcs",
688 "shared_source.cc",
689 "shared_source.cpp",
690 ],
691 "srcs_as": [
692 "shared_source.s",
693 "shared_source.S",
694 ":shared_filegroup_as_srcs",
695 ],
696 "srcs_c": [
697 "shared_source.c",
698 ":shared_filegroup_c_srcs",
699 ],
700 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000701 srcs = [
702 ":both_filegroup_cpp_srcs",
703 "both_source.cc",
704 "both_source.cpp",
705 ],
706 srcs_as = [
707 "both_source.s",
708 "both_source.S",
709 ":both_filegroup_as_srcs",
710 ],
711 srcs_c = [
712 "both_source.c",
713 ":both_filegroup_c_srcs",
714 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000715 static = {
716 "srcs": [
717 ":static_filegroup_cpp_srcs",
718 "static_source.cc",
719 "static_source.cpp",
720 ],
721 "srcs_as": [
722 "static_source.s",
723 "static_source.S",
724 ":static_filegroup_as_srcs",
725 ],
726 "srcs_c": [
727 "static_source.c",
728 ":static_filegroup_c_srcs",
729 ],
730 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000731)`},
732 })
733}
734
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200735func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
736 runCcLibraryTestCase(t, bp2buildTestCase{
737 description: "cc_library non-configured version script",
738 moduleTypeUnderTest: "cc_library",
739 moduleTypeUnderTestFactory: cc.LibraryFactory,
740 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200741 dir: "foo/bar",
742 filesystem: map[string]string{
743 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200744cc_library {
745 name: "a",
746 srcs: ["a.cpp"],
747 version_script: "v.map",
748 bazel_module: { bp2build_available: true },
749}
750`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200751 },
752 blueprint: soongCcLibraryPreamble,
753 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200754 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400755 copts = [
756 "-Ifoo/bar",
757 "-I$(BINDIR)/foo/bar",
758 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200759 srcs = ["a.cpp"],
760 version_script = "v.map",
761)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200762 })
763}
764
765func TestCcLibraryConfiguredVersionScript(t *testing.T) {
766 runCcLibraryTestCase(t, bp2buildTestCase{
767 description: "cc_library configured version script",
768 moduleTypeUnderTest: "cc_library",
769 moduleTypeUnderTestFactory: cc.LibraryFactory,
770 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200771 dir: "foo/bar",
772 filesystem: map[string]string{
773 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400774 cc_library {
775 name: "a",
776 srcs: ["a.cpp"],
777 arch: {
778 arm: {
779 version_script: "arm.map",
780 },
781 arm64: {
782 version_script: "arm64.map",
783 },
784 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200785
Liz Kammerd366c902021-06-03 13:43:01 -0400786 bazel_module: { bp2build_available: true },
787 }
788 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200789 },
790 blueprint: soongCcLibraryPreamble,
791 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200792 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400793 copts = [
794 "-Ifoo/bar",
795 "-I$(BINDIR)/foo/bar",
796 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200797 srcs = ["a.cpp"],
798 version_script = select({
799 "//build/bazel/platforms/arch:arm": "arm.map",
800 "//build/bazel/platforms/arch:arm64": "arm64.map",
801 "//conditions:default": None,
802 }),
803)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200804 })
805}
806
807func TestCcLibrarySharedLibs(t *testing.T) {
808 runCcLibraryTestCase(t, bp2buildTestCase{
809 description: "cc_library shared_libs",
810 moduleTypeUnderTest: "cc_library",
811 moduleTypeUnderTestFactory: cc.LibraryFactory,
812 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200813 dir: "foo/bar",
814 filesystem: map[string]string{
815 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400816cc_library {
817 name: "mylib",
818 bazel_module: { bp2build_available: true },
819}
820
821cc_library {
822 name: "a",
823 shared_libs: ["mylib",],
824 bazel_module: { bp2build_available: true },
825}
826`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200827 },
828 blueprint: soongCcLibraryPreamble,
829 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400830 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400831 copts = [
832 "-Ifoo/bar",
833 "-I$(BINDIR)/foo/bar",
834 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400835 dynamic_deps = [":mylib"],
836)`, `cc_library(
837 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400838 copts = [
839 "-Ifoo/bar",
840 "-I$(BINDIR)/foo/bar",
841 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400842)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200843 })
844}
845
846func TestCcLibraryPackRelocations(t *testing.T) {
847 runCcLibraryTestCase(t, bp2buildTestCase{
848 description: "cc_library pack_relocations test",
849 moduleTypeUnderTest: "cc_library",
850 moduleTypeUnderTestFactory: cc.LibraryFactory,
851 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200852 dir: "foo/bar",
853 filesystem: map[string]string{
854 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400855cc_library {
856 name: "a",
857 srcs: ["a.cpp"],
858 pack_relocations: false,
859 bazel_module: { bp2build_available: true },
860}
861
862cc_library {
863 name: "b",
864 srcs: ["b.cpp"],
865 arch: {
866 x86_64: {
Liz Kammerd366c902021-06-03 13:43:01 -0400867 pack_relocations: false,
868 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400869 },
870 bazel_module: { bp2build_available: true },
871}
872
873cc_library {
874 name: "c",
875 srcs: ["c.cpp"],
876 target: {
877 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400878 pack_relocations: false,
879 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400880 },
881 bazel_module: { bp2build_available: true },
882}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200883 },
884 blueprint: soongCcLibraryPreamble,
885 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400886 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400887 copts = [
888 "-Ifoo/bar",
889 "-I$(BINDIR)/foo/bar",
890 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400891 linkopts = ["-Wl,--pack-dyn-relocs=none"],
892 srcs = ["a.cpp"],
893)`, `cc_library(
894 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400895 copts = [
896 "-Ifoo/bar",
897 "-I$(BINDIR)/foo/bar",
898 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400899 linkopts = select({
900 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
901 "//conditions:default": [],
902 }),
903 srcs = ["b.cpp"],
904)`, `cc_library(
905 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400906 copts = [
907 "-Ifoo/bar",
908 "-I$(BINDIR)/foo/bar",
909 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400910 linkopts = select({
911 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
912 "//conditions:default": [],
913 }),
914 srcs = ["c.cpp"],
915)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200916 })
917}
918
919func TestCcLibrarySpacesInCopts(t *testing.T) {
920 runCcLibraryTestCase(t, bp2buildTestCase{
921 description: "cc_library spaces in copts",
922 moduleTypeUnderTest: "cc_library",
923 moduleTypeUnderTestFactory: cc.LibraryFactory,
924 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200925 dir: "foo/bar",
926 filesystem: map[string]string{
927 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000928cc_library {
929 name: "a",
930 cflags: ["-include header.h",],
931 bazel_module: { bp2build_available: true },
932}
933`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200934 },
935 blueprint: soongCcLibraryPreamble,
936 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000937 name = "a",
938 copts = [
939 "-include",
940 "header.h",
941 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400942 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000943 ],
944)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200945 })
946}
947
948func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
949 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400950 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200951 moduleTypeUnderTest: "cc_library",
952 moduleTypeUnderTestFactory: cc.LibraryFactory,
953 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200954 dir: "foo/bar",
955 filesystem: map[string]string{
956 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000957 name: "a",
958 srcs: ["a.cpp"],
959 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400960 "-Wall",
961 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000962 cppflags: [
963 "-fsigned-char",
964 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400965 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000966 arch: {
967 arm64: {
968 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400969 },
970 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000971 target: {
972 android: {
973 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400974 },
975 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000976 bazel_module: { bp2build_available: true },
977}
978`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200979 },
980 blueprint: soongCcLibraryPreamble,
981 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000982 name = "a",
983 copts = [
984 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000985 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400986 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400987 ],
988 cppflags = [
989 "-fsigned-char",
990 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000991 ] + select({
992 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
993 "//conditions:default": [],
994 }) + select({
995 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
996 "//conditions:default": [],
997 }),
998 srcs = ["a.cpp"],
999)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001000 })
Jingwen Chen63930982021-03-24 10:04:33 -04001001}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001002
1003func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
1004 runCcLibraryTestCase(t, bp2buildTestCase{
1005 description: "cc_library GetTargetProperties on a LabelAttribute",
1006 moduleTypeUnderTest: "cc_library",
1007 moduleTypeUnderTestFactory: cc.LibraryFactory,
1008 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001009 dir: "foo/bar",
1010 filesystem: map[string]string{
1011 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -04001012 cc_library {
1013 name: "a",
1014 srcs: ["a.cpp"],
1015 target: {
1016 android_arm: {
1017 version_script: "android_arm.map",
1018 },
1019 linux_bionic_arm64: {
1020 version_script: "linux_bionic_arm64.map",
1021 },
1022 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001023
Liz Kammerd366c902021-06-03 13:43:01 -04001024 bazel_module: { bp2build_available: true },
1025 }
1026 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001027 },
1028 blueprint: soongCcLibraryPreamble,
1029 expectedBazelTargets: []string{`cc_library(
1030 name = "a",
1031 copts = [
1032 "-Ifoo/bar",
1033 "-I$(BINDIR)/foo/bar",
1034 ],
1035 srcs = ["a.cpp"],
1036 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -04001037 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
1038 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001039 "//conditions:default": None,
1040 }),
1041)`},
1042 })
1043}
Liz Kammer47535c52021-06-02 16:02:22 -04001044
1045func TestCcLibraryExcludeLibs(t *testing.T) {
1046 runCcLibraryTestCase(t, bp2buildTestCase{
1047 moduleTypeUnderTest: "cc_library",
1048 moduleTypeUnderTestFactory: cc.LibraryFactory,
1049 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Liz Kammer47535c52021-06-02 16:02:22 -04001050 filesystem: map[string]string{},
1051 blueprint: soongCcLibraryStaticPreamble + `
1052cc_library {
1053 name: "foo_static",
1054 srcs: ["common.c"],
1055 whole_static_libs: [
1056 "arm_whole_static_lib_excludes",
1057 "malloc_not_svelte_whole_static_lib_excludes"
1058 ],
1059 static_libs: [
1060 "arm_static_lib_excludes",
1061 "malloc_not_svelte_static_lib_excludes"
1062 ],
1063 shared_libs: [
1064 "arm_shared_lib_excludes",
1065 ],
1066 arch: {
1067 arm: {
1068 exclude_shared_libs: [
1069 "arm_shared_lib_excludes",
1070 ],
1071 exclude_static_libs: [
1072 "arm_static_lib_excludes",
1073 "arm_whole_static_lib_excludes",
1074 ],
1075 },
1076 },
1077 product_variables: {
1078 malloc_not_svelte: {
1079 shared_libs: ["malloc_not_svelte_shared_lib"],
1080 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1081 exclude_static_libs: [
1082 "malloc_not_svelte_static_lib_excludes",
1083 "malloc_not_svelte_whole_static_lib_excludes",
1084 ],
1085 },
1086 },
1087}
1088
1089cc_library {
1090 name: "arm_whole_static_lib_excludes",
1091 bazel_module: { bp2build_available: false },
1092}
1093
1094cc_library {
1095 name: "malloc_not_svelte_whole_static_lib",
1096 bazel_module: { bp2build_available: false },
1097}
1098
1099cc_library {
1100 name: "malloc_not_svelte_whole_static_lib_excludes",
1101 bazel_module: { bp2build_available: false },
1102}
1103
1104cc_library {
1105 name: "arm_static_lib_excludes",
1106 bazel_module: { bp2build_available: false },
1107}
1108
1109cc_library {
1110 name: "malloc_not_svelte_static_lib_excludes",
1111 bazel_module: { bp2build_available: false },
1112}
1113
1114cc_library {
1115 name: "arm_shared_lib_excludes",
1116 bazel_module: { bp2build_available: false },
1117}
1118
1119cc_library {
1120 name: "malloc_not_svelte_shared_lib",
1121 bazel_module: { bp2build_available: false },
1122}
1123`,
1124 expectedBazelTargets: []string{
1125 `cc_library(
1126 name = "foo_static",
1127 copts = [
1128 "-I.",
1129 "-I$(BINDIR)/.",
1130 ],
1131 dynamic_deps = select({
1132 "//build/bazel/platforms/arch:arm": [],
1133 "//conditions:default": [":arm_shared_lib_excludes"],
1134 }) + select({
1135 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1136 "//conditions:default": [],
1137 }),
1138 implementation_deps = select({
1139 "//build/bazel/platforms/arch:arm": [],
1140 "//conditions:default": [":arm_static_lib_excludes"],
1141 }) + select({
1142 "//build/bazel/product_variables:malloc_not_svelte": [],
1143 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1144 }),
1145 srcs_c = ["common.c"],
1146 whole_archive_deps = select({
1147 "//build/bazel/platforms/arch:arm": [],
1148 "//conditions:default": [":arm_whole_static_lib_excludes"],
1149 }) + select({
1150 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1151 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1152 }),
1153)`,
1154 },
1155 })
1156}
Liz Kammerd366c902021-06-03 13:43:01 -04001157
1158func TestCCLibraryNoCrtTrue(t *testing.T) {
1159 runCcLibraryTestCase(t, bp2buildTestCase{
1160 description: "cc_library - simple example",
1161 moduleTypeUnderTest: "cc_library",
1162 moduleTypeUnderTestFactory: cc.LibraryFactory,
1163 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1164 filesystem: map[string]string{
1165 "impl.cpp": "",
1166 },
1167 blueprint: soongCcLibraryPreamble + `
1168cc_library_headers { name: "some-headers" }
1169cc_library {
1170 name: "foo-lib",
1171 srcs: ["impl.cpp"],
1172 no_libcrt: true,
1173}
1174`,
1175 expectedBazelTargets: []string{`cc_library(
1176 name = "foo-lib",
1177 copts = [
1178 "-I.",
1179 "-I$(BINDIR)/.",
1180 ],
1181 srcs = ["impl.cpp"],
1182 use_libcrt = False,
1183)`}})
1184}
1185
1186func TestCCLibraryNoCrtFalse(t *testing.T) {
1187 runCcLibraryTestCase(t, bp2buildTestCase{
1188 moduleTypeUnderTest: "cc_library",
1189 moduleTypeUnderTestFactory: cc.LibraryFactory,
1190 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1191 filesystem: map[string]string{
1192 "impl.cpp": "",
1193 },
1194 blueprint: soongCcLibraryPreamble + `
1195cc_library_headers { name: "some-headers" }
1196cc_library {
1197 name: "foo-lib",
1198 srcs: ["impl.cpp"],
1199 no_libcrt: false,
1200}
1201`,
1202 expectedBazelTargets: []string{`cc_library(
1203 name = "foo-lib",
1204 copts = [
1205 "-I.",
1206 "-I$(BINDIR)/.",
1207 ],
1208 srcs = ["impl.cpp"],
1209 use_libcrt = True,
1210)`}})
1211}
1212
1213func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1214 runCcLibraryTestCase(t, bp2buildTestCase{
1215 moduleTypeUnderTest: "cc_library",
1216 moduleTypeUnderTestFactory: cc.LibraryFactory,
1217 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1218 filesystem: map[string]string{
1219 "impl.cpp": "",
1220 },
1221 blueprint: soongCcLibraryPreamble + `
1222cc_library_headers { name: "some-headers" }
1223cc_library {
1224 name: "foo-lib",
1225 srcs: ["impl.cpp"],
1226 arch: {
1227 arm: {
1228 no_libcrt: true,
1229 },
1230 x86: {
1231 no_libcrt: true,
1232 },
1233 },
1234}
1235`,
1236 expectedBazelTargets: []string{`cc_library(
1237 name = "foo-lib",
1238 copts = [
1239 "-I.",
1240 "-I$(BINDIR)/.",
1241 ],
1242 srcs = ["impl.cpp"],
1243 use_libcrt = select({
1244 "//build/bazel/platforms/arch:arm": False,
1245 "//build/bazel/platforms/arch:x86": False,
1246 "//conditions:default": None,
1247 }),
1248)`}})
1249}
1250
1251func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1252 runCcLibraryTestCase(t, bp2buildTestCase{
1253 moduleTypeUnderTest: "cc_library",
1254 moduleTypeUnderTestFactory: cc.LibraryFactory,
1255 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1256 filesystem: map[string]string{
1257 "impl.cpp": "",
1258 },
1259 blueprint: soongCcLibraryPreamble + `
1260cc_library_headers { name: "some-headers" }
1261cc_library {
1262 name: "foo-lib",
1263 srcs: ["impl.cpp"],
1264 no_libcrt: false,
1265 arch: {
1266 arm: {
1267 no_libcrt: true,
1268 },
1269 x86: {
1270 no_libcrt: true,
1271 },
1272 },
1273}
1274`,
1275 expectedBazelTargets: []string{`cc_library(
1276 name = "foo-lib",
1277 copts = [
1278 "-I.",
1279 "-I$(BINDIR)/.",
1280 ],
1281 srcs = ["impl.cpp"],
1282 use_libcrt = select({
1283 "//build/bazel/platforms/arch:arm": False,
1284 "//build/bazel/platforms/arch:x86": False,
1285 "//conditions:default": True,
1286 }),
1287)`}})
1288}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001289
1290func TestCcLibraryStrip(t *testing.T) {
1291 runCcLibraryTestCase(t, bp2buildTestCase{
1292 description: "cc_library strip args",
1293 moduleTypeUnderTest: "cc_library",
1294 moduleTypeUnderTestFactory: cc.LibraryFactory,
1295 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001296 dir: "foo/bar",
1297 filesystem: map[string]string{
1298 "foo/bar/Android.bp": `
1299cc_library {
1300 name: "nothing",
1301 bazel_module: { bp2build_available: true },
1302}
1303cc_library {
1304 name: "keep_symbols",
1305 bazel_module: { bp2build_available: true },
1306 strip: {
1307 keep_symbols: true,
1308 }
1309}
1310cc_library {
1311 name: "keep_symbols_and_debug_frame",
1312 bazel_module: { bp2build_available: true },
1313 strip: {
1314 keep_symbols_and_debug_frame: true,
1315 }
1316}
1317cc_library {
1318 name: "none",
1319 bazel_module: { bp2build_available: true },
1320 strip: {
1321 none: true,
1322 }
1323}
1324cc_library {
1325 name: "keep_symbols_list",
1326 bazel_module: { bp2build_available: true },
1327 strip: {
1328 keep_symbols_list: ["symbol"],
1329 }
1330}
1331cc_library {
1332 name: "all",
1333 bazel_module: { bp2build_available: true },
1334 strip: {
1335 all: true,
1336 }
1337}
1338`,
1339 },
1340 blueprint: soongCcLibraryPreamble,
1341 expectedBazelTargets: []string{`cc_library(
1342 name = "all",
1343 copts = [
1344 "-Ifoo/bar",
1345 "-I$(BINDIR)/foo/bar",
1346 ],
1347 strip = {
1348 "all": True,
1349 },
1350)`, `cc_library(
1351 name = "keep_symbols",
1352 copts = [
1353 "-Ifoo/bar",
1354 "-I$(BINDIR)/foo/bar",
1355 ],
1356 strip = {
1357 "keep_symbols": True,
1358 },
1359)`, `cc_library(
1360 name = "keep_symbols_and_debug_frame",
1361 copts = [
1362 "-Ifoo/bar",
1363 "-I$(BINDIR)/foo/bar",
1364 ],
1365 strip = {
1366 "keep_symbols_and_debug_frame": True,
1367 },
1368)`, `cc_library(
1369 name = "keep_symbols_list",
1370 copts = [
1371 "-Ifoo/bar",
1372 "-I$(BINDIR)/foo/bar",
1373 ],
1374 strip = {
1375 "keep_symbols_list": ["symbol"],
1376 },
1377)`, `cc_library(
1378 name = "none",
1379 copts = [
1380 "-Ifoo/bar",
1381 "-I$(BINDIR)/foo/bar",
1382 ],
1383 strip = {
1384 "none": True,
1385 },
1386)`, `cc_library(
1387 name = "nothing",
1388 copts = [
1389 "-Ifoo/bar",
1390 "-I$(BINDIR)/foo/bar",
1391 ],
1392)`},
1393 })
1394}
1395
1396func TestCcLibraryStripWithArch(t *testing.T) {
1397 runCcLibraryTestCase(t, bp2buildTestCase{
1398 description: "cc_library strip args",
1399 moduleTypeUnderTest: "cc_library",
1400 moduleTypeUnderTestFactory: cc.LibraryFactory,
1401 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001402 dir: "foo/bar",
1403 filesystem: map[string]string{
1404 "foo/bar/Android.bp": `
1405cc_library {
1406 name: "multi-arch",
1407 bazel_module: { bp2build_available: true },
1408 target: {
1409 darwin: {
1410 strip: {
1411 keep_symbols_list: ["foo", "bar"]
1412 }
1413 },
1414 },
1415 arch: {
1416 arm: {
1417 strip: {
1418 keep_symbols_and_debug_frame: true,
1419 },
1420 },
1421 arm64: {
1422 strip: {
1423 keep_symbols: true,
1424 },
1425 },
1426 }
1427}
1428`,
1429 },
1430 blueprint: soongCcLibraryPreamble,
1431 expectedBazelTargets: []string{`cc_library(
1432 name = "multi-arch",
1433 copts = [
1434 "-Ifoo/bar",
1435 "-I$(BINDIR)/foo/bar",
1436 ],
1437 strip = {
1438 "keep_symbols": select({
1439 "//build/bazel/platforms/arch:arm64": True,
1440 "//conditions:default": None,
1441 }),
1442 "keep_symbols_and_debug_frame": select({
1443 "//build/bazel/platforms/arch:arm": True,
1444 "//conditions:default": None,
1445 }),
1446 "keep_symbols_list": select({
1447 "//build/bazel/platforms/os:darwin": [
1448 "foo",
1449 "bar",
1450 ],
1451 "//conditions:default": [],
1452 }),
1453 },
1454)`},
1455 })
1456}
Chris Parsons51f8c392021-08-03 21:01:05 -04001457
1458func TestCcLibrary_SystemSharedLibsRootEmpty(t *testing.T) {
1459 runCcLibraryTestCase(t, bp2buildTestCase{
1460 description: "cc_library system_shared_libs empty at root",
1461 moduleTypeUnderTest: "cc_library",
1462 moduleTypeUnderTestFactory: cc.LibraryFactory,
1463 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1464 blueprint: soongCcLibraryPreamble + `
1465cc_library {
1466 name: "root_empty",
1467 system_shared_libs: [],
1468}
1469`,
1470 expectedBazelTargets: []string{`cc_library(
1471 name = "root_empty",
1472 copts = [
1473 "-I.",
1474 "-I$(BINDIR)/.",
1475 ],
1476 system_dynamic_deps = [],
1477)`},
1478 })
1479}
1480
1481func TestCcLibrary_SystemSharedLibsStaticEmpty(t *testing.T) {
1482 runCcLibraryTestCase(t, bp2buildTestCase{
1483 description: "cc_library system_shared_libs empty for static variant",
1484 moduleTypeUnderTest: "cc_library",
1485 moduleTypeUnderTestFactory: cc.LibraryFactory,
1486 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1487 blueprint: soongCcLibraryPreamble + `
1488cc_library {
1489 name: "static_empty",
1490 static: {
1491 system_shared_libs: [],
1492 },
1493}
1494`,
1495 expectedBazelTargets: []string{`cc_library(
1496 name = "static_empty",
1497 copts = [
1498 "-I.",
1499 "-I$(BINDIR)/.",
1500 ],
1501 static = {
1502 "system_dynamic_deps": [],
1503 },
1504)`},
1505 })
1506}
1507
1508func TestCcLibrary_SystemSharedLibsSharedEmpty(t *testing.T) {
1509 runCcLibraryTestCase(t, bp2buildTestCase{
1510 description: "cc_library system_shared_libs empty for shared variant",
1511 moduleTypeUnderTest: "cc_library",
1512 moduleTypeUnderTestFactory: cc.LibraryFactory,
1513 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1514 blueprint: soongCcLibraryPreamble + `
1515cc_library {
1516 name: "shared_empty",
1517 shared: {
1518 system_shared_libs: [],
1519 },
1520}
1521`,
1522 expectedBazelTargets: []string{`cc_library(
1523 name = "shared_empty",
1524 copts = [
1525 "-I.",
1526 "-I$(BINDIR)/.",
1527 ],
1528 shared = {
1529 "system_dynamic_deps": [],
1530 },
1531)`},
1532 })
1533}
1534
1535func TestCcLibrary_SystemSharedLibsSharedBionicEmpty(t *testing.T) {
1536 runCcLibraryTestCase(t, bp2buildTestCase{
1537 description: "cc_library system_shared_libs empty for shared, bionic variant",
1538 moduleTypeUnderTest: "cc_library",
1539 moduleTypeUnderTestFactory: cc.LibraryFactory,
1540 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1541 blueprint: soongCcLibraryPreamble + `
1542cc_library {
1543 name: "shared_empty",
1544 target: {
1545 bionic: {
1546 shared: {
1547 system_shared_libs: [],
1548 }
1549 }
1550 },
1551}
1552`,
1553 expectedBazelTargets: []string{`cc_library(
1554 name = "shared_empty",
1555 copts = [
1556 "-I.",
1557 "-I$(BINDIR)/.",
1558 ],
1559 shared = {
1560 "system_dynamic_deps": [],
1561 },
1562)`},
1563 })
1564}
1565
1566func TestCcLibrary_SystemSharedLibsLinuxBionicEmpty(t *testing.T) {
1567 // Note that this behavior is technically incorrect (it's a simplification).
1568 // The correct behavior would be if bp2build wrote `system_dynamic_deps = []`
1569 // only for linux_bionic, but `android` had `["libc", "libdl", "libm"].
1570 // b/195791252 tracks the fix.
1571 runCcLibraryTestCase(t, bp2buildTestCase{
1572 description: "cc_library system_shared_libs empty for linux_bionic variant",
1573 moduleTypeUnderTest: "cc_library",
1574 moduleTypeUnderTestFactory: cc.LibraryFactory,
1575 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1576 blueprint: soongCcLibraryPreamble + `
1577cc_library {
1578 name: "target_linux_bionic_empty",
1579 target: {
1580 linux_bionic: {
1581 system_shared_libs: [],
1582 },
1583 },
1584}
1585`,
1586 expectedBazelTargets: []string{`cc_library(
1587 name = "target_linux_bionic_empty",
1588 copts = [
1589 "-I.",
1590 "-I$(BINDIR)/.",
1591 ],
1592 system_dynamic_deps = [],
1593)`},
1594 })
1595}
1596
1597func TestCcLibrary_SystemSharedLibsBionicEmpty(t *testing.T) {
1598 runCcLibraryTestCase(t, bp2buildTestCase{
1599 description: "cc_library system_shared_libs empty for bionic variant",
1600 moduleTypeUnderTest: "cc_library",
1601 moduleTypeUnderTestFactory: cc.LibraryFactory,
1602 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1603 blueprint: soongCcLibraryPreamble + `
1604cc_library {
1605 name: "target_bionic_empty",
1606 target: {
1607 bionic: {
1608 system_shared_libs: [],
1609 },
1610 },
1611}
1612`,
1613 expectedBazelTargets: []string{`cc_library(
1614 name = "target_bionic_empty",
1615 copts = [
1616 "-I.",
1617 "-I$(BINDIR)/.",
1618 ],
1619 system_dynamic_deps = [],
1620)`},
1621 })
1622}
1623
1624func TestCcLibrary_SystemSharedLibsSharedAndRoot(t *testing.T) {
1625 runCcLibraryTestCase(t, bp2buildTestCase{
1626 description: "cc_library system_shared_libs set for shared and root",
1627 moduleTypeUnderTest: "cc_library",
1628 moduleTypeUnderTestFactory: cc.LibraryFactory,
1629 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1630 blueprint: soongCcLibraryPreamble + `
1631cc_library {name: "libc"}
1632cc_library {name: "libm"}
1633
1634cc_library {
1635 name: "foo",
1636 system_shared_libs: ["libc"],
1637 shared: {
1638 system_shared_libs: ["libm"],
1639 },
1640}
1641`,
1642 expectedBazelTargets: []string{`cc_library(
1643 name = "foo",
1644 copts = [
1645 "-I.",
1646 "-I$(BINDIR)/.",
1647 ],
1648 shared = {
1649 "system_dynamic_deps": [":libm"],
1650 },
1651 system_dynamic_deps = [":libc"],
1652)`, `cc_library(
1653 name = "libc",
1654 copts = [
1655 "-I.",
1656 "-I$(BINDIR)/.",
1657 ],
1658)`, `cc_library(
1659 name = "libm",
1660 copts = [
1661 "-I.",
1662 "-I$(BINDIR)/.",
1663 ],
1664)`},
1665 })
1666}