blob: 662d9d11a93747bf8575592ccd6b7d1bc5e970f6 [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)
76 for _, m := range tc.depsMutators {
77 ctx.DepsBp2BuildMutators(m)
78 }
79 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
80 ctx.RegisterForBazelConversion()
81
82 _, errs := ctx.ParseFileList(dir, toParse)
83 if errored(t, tc.description, errs) {
84 return
85 }
86 _, errs = ctx.ResolveDependencies(config)
87 if errored(t, tc.description, errs) {
88 return
89 }
90
91 checkDir := dir
92 if tc.dir != "" {
93 checkDir = tc.dir
94 }
95 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
96 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
97 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
98 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
99 } else {
100 for i, target := range bazelTargets {
101 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
102 t.Errorf(
103 "%s: Expected generated Bazel target to be '%s', got '%s'",
104 tc.description,
105 w,
106 g,
107 )
108 }
109 }
110 }
111}
112
113func TestCcLibrarySimple(t *testing.T) {
114 runCcLibraryTestCase(t, bp2buildTestCase{
115 description: "cc_library - simple example",
116 moduleTypeUnderTest: "cc_library",
117 moduleTypeUnderTestFactory: cc.LibraryFactory,
118 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
119 filesystem: map[string]string{
120 "android.cpp": "",
Liz Kammer01a16e82021-07-16 16:33:47 -0400121 "bionic.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200122 "darwin.cpp": "",
123 // Refer to cc.headerExts for the supported header extensions in Soong.
124 "header.h": "",
125 "header.hh": "",
126 "header.hpp": "",
127 "header.hxx": "",
128 "header.h++": "",
129 "header.inl": "",
130 "header.inc": "",
131 "header.ipp": "",
132 "header.h.generic": "",
133 "impl.cpp": "",
134 "linux.cpp": "",
135 "x86.cpp": "",
136 "x86_64.cpp": "",
137 "foo-dir/a.h": "",
138 },
139 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400140cc_library_headers { name: "some-headers" }
141cc_library {
142 name: "foo-lib",
143 srcs: ["impl.cpp"],
144 cflags: ["-Wall"],
145 header_libs: ["some-headers"],
146 export_include_dirs: ["foo-dir"],
147 ldflags: ["-Wl,--exclude-libs=bar.a"],
148 arch: {
149 x86: {
150 ldflags: ["-Wl,--exclude-libs=baz.a"],
151 srcs: ["x86.cpp"],
152 },
153 x86_64: {
154 ldflags: ["-Wl,--exclude-libs=qux.a"],
155 srcs: ["x86_64.cpp"],
156 },
157 },
158 target: {
159 android: {
160 srcs: ["android.cpp"],
161 },
162 linux_glibc: {
163 srcs: ["linux.cpp"],
164 },
165 darwin: {
166 srcs: ["darwin.cpp"],
167 },
Liz Kammer01a16e82021-07-16 16:33:47 -0400168 bionic: {
169 srcs: ["bionic.cpp"]
170 },
Jingwen Chen63930982021-03-24 10:04:33 -0400171 },
172}
173`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200174 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400175 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000176 copts = [
177 "-Wall",
178 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400179 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000180 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400181 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000182 includes = ["foo-dir"],
183 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
184 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
185 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
186 "//conditions:default": [],
187 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000188 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000189 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
190 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400191 "//conditions:default": [],
192 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000193 "//build/bazel/platforms/os:android": ["android.cpp"],
194 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
195 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400196 "//conditions:default": [],
Liz Kammer01a16e82021-07-16 16:33:47 -0400197 }) + select({
198 "//build/bazel/platforms/os:bionic": ["bionic.cpp"],
199 "//conditions:default": [],
Jingwen Chen63930982021-03-24 10:04:33 -0400200 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200201)`}})
202}
203
204func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
205 runCcLibraryTestCase(t, bp2buildTestCase{
206 description: "cc_library - trimmed example of //bionic/linker:ld-android",
207 moduleTypeUnderTest: "cc_library",
208 moduleTypeUnderTestFactory: cc.LibraryFactory,
209 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
210 filesystem: map[string]string{
211 "ld-android.cpp": "",
212 "linked_list.h": "",
213 "linker.h": "",
214 "linker_block_allocator.h": "",
215 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400216 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200217 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400218cc_library_headers { name: "libc_headers" }
219cc_library {
220 name: "fake-ld-android",
221 srcs: ["ld_android.cpp"],
222 cflags: [
223 "-Wall",
224 "-Wextra",
225 "-Wunused",
226 "-Werror",
227 ],
228 header_libs: ["libc_headers"],
229 ldflags: [
230 "-Wl,--exclude-libs=libgcc.a",
231 "-Wl,--exclude-libs=libgcc_stripped.a",
232 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
233 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
234 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
235 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
236 ],
237 arch: {
238 x86: {
239 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
240 },
241 x86_64: {
242 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
243 },
244 },
245}
246`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200247 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400248 name = "fake-ld-android",
249 copts = [
250 "-Wall",
251 "-Wextra",
252 "-Wunused",
253 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000254 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400255 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400256 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400257 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400258 linkopts = [
259 "-Wl,--exclude-libs=libgcc.a",
260 "-Wl,--exclude-libs=libgcc_stripped.a",
261 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
262 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
263 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
264 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
265 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000266 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
267 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400268 "//conditions:default": [],
269 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000270 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400271)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200272 })
273}
274
275func TestCcLibraryExcludeSrcs(t *testing.T) {
276 runCcLibraryTestCase(t, bp2buildTestCase{
277 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
278 moduleTypeUnderTest: "cc_library",
279 moduleTypeUnderTestFactory: cc.LibraryFactory,
280 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
281 dir: "external",
282 filesystem: map[string]string{
283 "external/math/cosf.c": "",
284 "external/math/erf.c": "",
285 "external/math/erf_data.c": "",
286 "external/math/erff.c": "",
287 "external/math/erff_data.c": "",
288 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000289cc_library {
290 name: "fake-libarm-optimized-routines-math",
291 exclude_srcs: [
292 // Provided by:
293 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
294 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
295 "math/erf.c",
296 "math/erf_data.c",
297 "math/erff.c",
298 "math/erff_data.c",
299 ],
300 srcs: [
301 "math/*.c",
302 ],
303 // arch-specific settings
304 arch: {
305 arm64: {
306 cflags: [
307 "-DHAVE_FAST_FMA=1",
308 ],
309 },
310 },
311 bazel_module: { bp2build_available: true },
312}
313`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200314 },
315 blueprint: soongCcLibraryPreamble,
316 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000317 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400318 copts = [
319 "-Iexternal",
320 "-I$(BINDIR)/external",
321 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000322 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
323 "//conditions:default": [],
324 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400325 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000326)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200327 })
328}
329
330func TestCcLibrarySharedStaticProps(t *testing.T) {
331 runCcLibraryTestCase(t, bp2buildTestCase{
332 description: "cc_library shared/static props",
333 moduleTypeUnderTest: "cc_library",
334 moduleTypeUnderTestFactory: cc.LibraryFactory,
335 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
336 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
337 dir: "foo/bar",
338 filesystem: map[string]string{
339 "foo/bar/both.cpp": "",
340 "foo/bar/sharedonly.cpp": "",
341 "foo/bar/staticonly.cpp": "",
342 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000343cc_library {
344 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400345 srcs: ["both.cpp"],
346 cflags: ["bothflag"],
347 shared_libs: ["shared_dep_for_both"],
348 static_libs: ["static_dep_for_both"],
349 whole_static_libs: ["whole_static_lib_for_both"],
350 static: {
351 srcs: ["staticonly.cpp"],
352 cflags: ["staticflag"],
353 shared_libs: ["shared_dep_for_static"],
354 static_libs: ["static_dep_for_static"],
355 whole_static_libs: ["whole_static_lib_for_static"],
356 },
357 shared: {
358 srcs: ["sharedonly.cpp"],
359 cflags: ["sharedflag"],
360 shared_libs: ["shared_dep_for_shared"],
361 static_libs: ["static_dep_for_shared"],
362 whole_static_libs: ["whole_static_lib_for_shared"],
363 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000364 bazel_module: { bp2build_available: true },
365}
366
Chris Parsons08648312021-05-06 16:23:19 -0400367cc_library_static { name: "static_dep_for_shared" }
368
369cc_library_static { name: "static_dep_for_static" }
370
371cc_library_static { name: "static_dep_for_both" }
372
373cc_library_static { name: "whole_static_lib_for_shared" }
374
375cc_library_static { name: "whole_static_lib_for_static" }
376
377cc_library_static { name: "whole_static_lib_for_both" }
378
379cc_library { name: "shared_dep_for_shared" }
380
381cc_library { name: "shared_dep_for_static" }
382
383cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000384`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200385 },
386 blueprint: soongCcLibraryPreamble,
387 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000388 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400389 copts = [
390 "bothflag",
391 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400392 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400393 ],
Chris Parsons08648312021-05-06 16:23:19 -0400394 dynamic_deps = [":shared_dep_for_both"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400395 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000396 shared = {
397 "copts": ["sharedflag"],
398 "dynamic_deps": [":shared_dep_for_shared"],
399 "srcs": ["sharedonly.cpp"],
400 "static_deps": [":static_dep_for_shared"],
401 "whole_archive_deps": [":whole_static_lib_for_shared"],
402 },
Chris Parsons08648312021-05-06 16:23:19 -0400403 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000404 static = {
405 "copts": ["staticflag"],
406 "dynamic_deps": [":shared_dep_for_static"],
407 "srcs": ["staticonly.cpp"],
408 "static_deps": [":static_dep_for_static"],
409 "whole_archive_deps": [":whole_static_lib_for_static"],
410 },
Chris Parsons08648312021-05-06 16:23:19 -0400411 whole_archive_deps = [":whole_static_lib_for_both"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000412)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200413 })
414}
415
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400416func TestCcLibraryWholeStaticLibsAlwaysLink(t *testing.T) {
417 runCcLibraryTestCase(t, bp2buildTestCase{
418 moduleTypeUnderTest: "cc_library",
419 moduleTypeUnderTestFactory: cc.LibraryFactory,
420 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
421 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
422 dir: "foo/bar",
423 filesystem: map[string]string{
424 "foo/bar/Android.bp": `
425cc_library {
426 name: "a",
427 whole_static_libs: ["whole_static_lib_for_both"],
428 static: {
429 whole_static_libs: ["whole_static_lib_for_static"],
430 },
431 shared: {
432 whole_static_libs: ["whole_static_lib_for_shared"],
433 },
434 bazel_module: { bp2build_available: true },
435}
436
437cc_prebuilt_library_static { name: "whole_static_lib_for_shared" }
438
439cc_prebuilt_library_static { name: "whole_static_lib_for_static" }
440
441cc_prebuilt_library_static { name: "whole_static_lib_for_both" }
442`,
443 },
444 blueprint: soongCcLibraryPreamble,
445 expectedBazelTargets: []string{`cc_library(
446 name = "a",
447 copts = [
448 "-Ifoo/bar",
449 "-I$(BINDIR)/foo/bar",
450 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000451 shared = {
452 "whole_archive_deps": [":whole_static_lib_for_shared_alwayslink"],
453 },
454 static = {
455 "whole_archive_deps": [":whole_static_lib_for_static_alwayslink"],
456 },
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400457 whole_archive_deps = [":whole_static_lib_for_both_alwayslink"],
Liz Kammer2d7bbe32021-06-10 18:20:06 -0400458)`},
459 })
460}
461
Jingwen Chenbcf53042021-05-26 04:42:42 +0000462func TestCcLibrarySharedStaticPropsInArch(t *testing.T) {
463 runCcLibraryTestCase(t, bp2buildTestCase{
464 description: "cc_library shared/static props in arch",
465 moduleTypeUnderTest: "cc_library",
466 moduleTypeUnderTestFactory: cc.LibraryFactory,
467 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
468 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
469 dir: "foo/bar",
470 filesystem: map[string]string{
471 "foo/bar/arm.cpp": "",
472 "foo/bar/x86.cpp": "",
473 "foo/bar/sharedonly.cpp": "",
474 "foo/bar/staticonly.cpp": "",
475 "foo/bar/Android.bp": `
476cc_library {
477 name: "a",
478 arch: {
479 arm: {
480 shared: {
481 srcs: ["arm_shared.cpp"],
482 cflags: ["-DARM_SHARED"],
483 static_libs: ["arm_static_dep_for_shared"],
484 whole_static_libs: ["arm_whole_static_dep_for_shared"],
485 shared_libs: ["arm_shared_dep_for_shared"],
486 },
487 },
488 x86: {
489 static: {
490 srcs: ["x86_static.cpp"],
491 cflags: ["-DX86_STATIC"],
492 static_libs: ["x86_dep_for_static"],
493 },
494 },
495 },
496 target: {
497 android: {
498 shared: {
499 srcs: ["android_shared.cpp"],
500 cflags: ["-DANDROID_SHARED"],
501 static_libs: ["android_dep_for_shared"],
502 },
503 },
504 android_arm: {
505 shared: {
506 cflags: ["-DANDROID_ARM_SHARED"],
507 },
508 },
509 },
510 srcs: ["both.cpp"],
511 cflags: ["bothflag"],
512 static_libs: ["static_dep_for_both"],
513 static: {
514 srcs: ["staticonly.cpp"],
515 cflags: ["staticflag"],
516 static_libs: ["static_dep_for_static"],
517 },
518 shared: {
519 srcs: ["sharedonly.cpp"],
520 cflags: ["sharedflag"],
521 static_libs: ["static_dep_for_shared"],
522 },
523 bazel_module: { bp2build_available: true },
524}
525
526cc_library_static { name: "static_dep_for_shared" }
527cc_library_static { name: "static_dep_for_static" }
528cc_library_static { name: "static_dep_for_both" }
529
530cc_library_static { name: "arm_static_dep_for_shared" }
531cc_library_static { name: "arm_whole_static_dep_for_shared" }
532cc_library_static { name: "arm_shared_dep_for_shared" }
533
534cc_library_static { name: "x86_dep_for_static" }
535
536cc_library_static { name: "android_dep_for_shared" }
537`,
538 },
539 blueprint: soongCcLibraryPreamble,
540 expectedBazelTargets: []string{`cc_library(
541 name = "a",
542 copts = [
543 "bothflag",
544 "-Ifoo/bar",
545 "-I$(BINDIR)/foo/bar",
546 ],
Jingwen Chenbcf53042021-05-26 04:42:42 +0000547 implementation_deps = [":static_dep_for_both"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000548 shared = {
549 "copts": ["sharedflag"] + select({
550 "//build/bazel/platforms/arch:arm": ["-DARM_SHARED"],
551 "//conditions:default": [],
552 }) + select({
553 "//build/bazel/platforms/os:android": ["-DANDROID_SHARED"],
554 "//conditions:default": [],
555 }) + select({
556 "//build/bazel/platforms/os_arch:android_arm": ["-DANDROID_ARM_SHARED"],
557 "//conditions:default": [],
558 }),
559 "dynamic_deps": select({
560 "//build/bazel/platforms/arch:arm": [":arm_shared_dep_for_shared"],
561 "//conditions:default": [],
562 }),
563 "srcs": ["sharedonly.cpp"] + select({
564 "//build/bazel/platforms/arch:arm": ["arm_shared.cpp"],
565 "//conditions:default": [],
566 }) + select({
567 "//build/bazel/platforms/os:android": ["android_shared.cpp"],
568 "//conditions:default": [],
569 }),
570 "static_deps": [":static_dep_for_shared"] + select({
571 "//build/bazel/platforms/arch:arm": [":arm_static_dep_for_shared"],
572 "//conditions:default": [],
573 }) + select({
574 "//build/bazel/platforms/os:android": [":android_dep_for_shared"],
575 "//conditions:default": [],
576 }),
577 "whole_archive_deps": select({
578 "//build/bazel/platforms/arch:arm": [":arm_whole_static_dep_for_shared"],
579 "//conditions:default": [],
580 }),
581 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000582 srcs = ["both.cpp"],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000583 static = {
584 "copts": ["staticflag"] + select({
585 "//build/bazel/platforms/arch:x86": ["-DX86_STATIC"],
586 "//conditions:default": [],
587 }),
588 "srcs": ["staticonly.cpp"] + select({
589 "//build/bazel/platforms/arch:x86": ["x86_static.cpp"],
590 "//conditions:default": [],
591 }),
592 "static_deps": [":static_dep_for_static"] + select({
593 "//build/bazel/platforms/arch:x86": [":x86_dep_for_static"],
594 "//conditions:default": [],
595 }),
596 },
Jingwen Chenbcf53042021-05-26 04:42:42 +0000597)`},
598 })
599}
600
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000601func TestCcLibrarySharedStaticPropsWithMixedSources(t *testing.T) {
602 runCcLibraryTestCase(t, bp2buildTestCase{
603 description: "cc_library shared/static props with c/cpp/s mixed sources",
604 moduleTypeUnderTest: "cc_library",
605 moduleTypeUnderTestFactory: cc.LibraryFactory,
606 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
607 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
608 dir: "foo/bar",
609 filesystem: map[string]string{
610 "foo/bar/both_source.cpp": "",
611 "foo/bar/both_source.cc": "",
612 "foo/bar/both_source.c": "",
613 "foo/bar/both_source.s": "",
614 "foo/bar/both_source.S": "",
615 "foo/bar/shared_source.cpp": "",
616 "foo/bar/shared_source.cc": "",
617 "foo/bar/shared_source.c": "",
618 "foo/bar/shared_source.s": "",
619 "foo/bar/shared_source.S": "",
620 "foo/bar/static_source.cpp": "",
621 "foo/bar/static_source.cc": "",
622 "foo/bar/static_source.c": "",
623 "foo/bar/static_source.s": "",
624 "foo/bar/static_source.S": "",
625 "foo/bar/Android.bp": `
626cc_library {
627 name: "a",
628 srcs: [
Liz Kammerd366c902021-06-03 13:43:01 -0400629 "both_source.cpp",
630 "both_source.cc",
631 "both_source.c",
632 "both_source.s",
633 "both_source.S",
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000634 ":both_filegroup",
Liz Kammerd366c902021-06-03 13:43:01 -0400635 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000636 static: {
Liz Kammerd366c902021-06-03 13:43:01 -0400637 srcs: [
638 "static_source.cpp",
639 "static_source.cc",
640 "static_source.c",
641 "static_source.s",
642 "static_source.S",
643 ":static_filegroup",
644 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000645 },
646 shared: {
Liz Kammerd366c902021-06-03 13:43:01 -0400647 srcs: [
648 "shared_source.cpp",
649 "shared_source.cc",
650 "shared_source.c",
651 "shared_source.s",
652 "shared_source.S",
653 ":shared_filegroup",
654 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000655 },
656 bazel_module: { bp2build_available: true },
657}
658
659filegroup {
660 name: "both_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: "shared_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
673filegroup {
674 name: "static_filegroup",
675 srcs: [
676 // Not relevant, handled by filegroup macro
Liz Kammerd366c902021-06-03 13:43:01 -0400677 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000678}
679`,
680 },
681 blueprint: soongCcLibraryPreamble,
682 expectedBazelTargets: []string{`cc_library(
683 name = "a",
Chris Parsons69fa9f92021-07-13 11:47:44 -0400684 asflags = [
685 "-Ifoo/bar",
686 "-I$(BINDIR)/foo/bar",
687 ],
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000688 copts = [
689 "-Ifoo/bar",
690 "-I$(BINDIR)/foo/bar",
691 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000692 shared = {
693 "srcs": [
694 ":shared_filegroup_cpp_srcs",
695 "shared_source.cc",
696 "shared_source.cpp",
697 ],
698 "srcs_as": [
699 "shared_source.s",
700 "shared_source.S",
701 ":shared_filegroup_as_srcs",
702 ],
703 "srcs_c": [
704 "shared_source.c",
705 ":shared_filegroup_c_srcs",
706 ],
707 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000708 srcs = [
709 ":both_filegroup_cpp_srcs",
710 "both_source.cc",
711 "both_source.cpp",
712 ],
713 srcs_as = [
714 "both_source.s",
715 "both_source.S",
716 ":both_filegroup_as_srcs",
717 ],
718 srcs_c = [
719 "both_source.c",
720 ":both_filegroup_c_srcs",
721 ],
Jingwen Chenc4dc9b42021-06-11 12:51:48 +0000722 static = {
723 "srcs": [
724 ":static_filegroup_cpp_srcs",
725 "static_source.cc",
726 "static_source.cpp",
727 ],
728 "srcs_as": [
729 "static_source.s",
730 "static_source.S",
731 ":static_filegroup_as_srcs",
732 ],
733 "srcs_c": [
734 "static_source.c",
735 ":static_filegroup_c_srcs",
736 ],
737 },
Jingwen Chen14a8bda2021-06-02 11:10:02 +0000738)`},
739 })
740}
741
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200742func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
743 runCcLibraryTestCase(t, bp2buildTestCase{
744 description: "cc_library non-configured version script",
745 moduleTypeUnderTest: "cc_library",
746 moduleTypeUnderTestFactory: cc.LibraryFactory,
747 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
748 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
749 dir: "foo/bar",
750 filesystem: map[string]string{
751 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200752cc_library {
753 name: "a",
754 srcs: ["a.cpp"],
755 version_script: "v.map",
756 bazel_module: { bp2build_available: true },
757}
758`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200759 },
760 blueprint: soongCcLibraryPreamble,
761 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200762 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400763 copts = [
764 "-Ifoo/bar",
765 "-I$(BINDIR)/foo/bar",
766 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200767 srcs = ["a.cpp"],
768 version_script = "v.map",
769)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200770 })
771}
772
773func TestCcLibraryConfiguredVersionScript(t *testing.T) {
774 runCcLibraryTestCase(t, bp2buildTestCase{
775 description: "cc_library configured version script",
776 moduleTypeUnderTest: "cc_library",
777 moduleTypeUnderTestFactory: cc.LibraryFactory,
778 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
779 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
780 dir: "foo/bar",
781 filesystem: map[string]string{
782 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -0400783 cc_library {
784 name: "a",
785 srcs: ["a.cpp"],
786 arch: {
787 arm: {
788 version_script: "arm.map",
789 },
790 arm64: {
791 version_script: "arm64.map",
792 },
793 },
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200794
Liz Kammerd366c902021-06-03 13:43:01 -0400795 bazel_module: { bp2build_available: true },
796 }
797 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200798 },
799 blueprint: soongCcLibraryPreamble,
800 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200801 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400802 copts = [
803 "-Ifoo/bar",
804 "-I$(BINDIR)/foo/bar",
805 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200806 srcs = ["a.cpp"],
807 version_script = select({
808 "//build/bazel/platforms/arch:arm": "arm.map",
809 "//build/bazel/platforms/arch:arm64": "arm64.map",
810 "//conditions:default": None,
811 }),
812)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200813 })
814}
815
816func TestCcLibrarySharedLibs(t *testing.T) {
817 runCcLibraryTestCase(t, bp2buildTestCase{
818 description: "cc_library shared_libs",
819 moduleTypeUnderTest: "cc_library",
820 moduleTypeUnderTestFactory: cc.LibraryFactory,
821 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
822 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
823 dir: "foo/bar",
824 filesystem: map[string]string{
825 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400826cc_library {
827 name: "mylib",
828 bazel_module: { bp2build_available: true },
829}
830
831cc_library {
832 name: "a",
833 shared_libs: ["mylib",],
834 bazel_module: { bp2build_available: true },
835}
836`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200837 },
838 blueprint: soongCcLibraryPreamble,
839 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400840 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400841 copts = [
842 "-Ifoo/bar",
843 "-I$(BINDIR)/foo/bar",
844 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400845 dynamic_deps = [":mylib"],
846)`, `cc_library(
847 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400848 copts = [
849 "-Ifoo/bar",
850 "-I$(BINDIR)/foo/bar",
851 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400852)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200853 })
854}
855
856func TestCcLibraryPackRelocations(t *testing.T) {
857 runCcLibraryTestCase(t, bp2buildTestCase{
858 description: "cc_library pack_relocations test",
859 moduleTypeUnderTest: "cc_library",
860 moduleTypeUnderTestFactory: cc.LibraryFactory,
861 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
862 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
863 dir: "foo/bar",
864 filesystem: map[string]string{
865 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400866cc_library {
867 name: "a",
868 srcs: ["a.cpp"],
869 pack_relocations: false,
870 bazel_module: { bp2build_available: true },
871}
872
873cc_library {
874 name: "b",
875 srcs: ["b.cpp"],
876 arch: {
877 x86_64: {
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}
883
884cc_library {
885 name: "c",
886 srcs: ["c.cpp"],
887 target: {
888 darwin: {
Liz Kammerd366c902021-06-03 13:43:01 -0400889 pack_relocations: false,
890 },
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400891 },
892 bazel_module: { bp2build_available: true },
893}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200894 },
895 blueprint: soongCcLibraryPreamble,
896 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400897 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400898 copts = [
899 "-Ifoo/bar",
900 "-I$(BINDIR)/foo/bar",
901 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400902 linkopts = ["-Wl,--pack-dyn-relocs=none"],
903 srcs = ["a.cpp"],
904)`, `cc_library(
905 name = "b",
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/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
912 "//conditions:default": [],
913 }),
914 srcs = ["b.cpp"],
915)`, `cc_library(
916 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400917 copts = [
918 "-Ifoo/bar",
919 "-I$(BINDIR)/foo/bar",
920 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400921 linkopts = select({
922 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
923 "//conditions:default": [],
924 }),
925 srcs = ["c.cpp"],
926)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200927 })
928}
929
930func TestCcLibrarySpacesInCopts(t *testing.T) {
931 runCcLibraryTestCase(t, bp2buildTestCase{
932 description: "cc_library spaces in copts",
933 moduleTypeUnderTest: "cc_library",
934 moduleTypeUnderTestFactory: cc.LibraryFactory,
935 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
936 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
937 dir: "foo/bar",
938 filesystem: map[string]string{
939 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000940cc_library {
941 name: "a",
942 cflags: ["-include header.h",],
943 bazel_module: { bp2build_available: true },
944}
945`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200946 },
947 blueprint: soongCcLibraryPreamble,
948 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000949 name = "a",
950 copts = [
951 "-include",
952 "header.h",
953 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400954 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000955 ],
956)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200957 })
958}
959
960func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
961 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400962 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200963 moduleTypeUnderTest: "cc_library",
964 moduleTypeUnderTestFactory: cc.LibraryFactory,
965 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
966 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
967 dir: "foo/bar",
968 filesystem: map[string]string{
969 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000970 name: "a",
971 srcs: ["a.cpp"],
972 cflags: [
Liz Kammerd366c902021-06-03 13:43:01 -0400973 "-Wall",
974 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000975 cppflags: [
976 "-fsigned-char",
977 "-pedantic",
Liz Kammerd366c902021-06-03 13:43:01 -0400978 ],
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000979 arch: {
980 arm64: {
981 cppflags: ["-DARM64=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400982 },
983 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000984 target: {
985 android: {
986 cppflags: ["-DANDROID=1"],
Liz Kammerd366c902021-06-03 13:43:01 -0400987 },
988 },
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000989 bazel_module: { bp2build_available: true },
990}
991`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200992 },
993 blueprint: soongCcLibraryPreamble,
994 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000995 name = "a",
996 copts = [
997 "-Wall",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000998 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400999 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -04001000 ],
1001 cppflags = [
1002 "-fsigned-char",
1003 "-pedantic",
Jingwen Chen75be1ca2021-05-12 05:04:58 +00001004 ] + select({
1005 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
1006 "//conditions:default": [],
1007 }) + select({
1008 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
1009 "//conditions:default": [],
1010 }),
1011 srcs = ["a.cpp"],
1012)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001013 })
Jingwen Chen63930982021-03-24 10:04:33 -04001014}
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001015
1016func TestCcLibraryLabelAttributeGetTargetProperties(t *testing.T) {
1017 runCcLibraryTestCase(t, bp2buildTestCase{
1018 description: "cc_library GetTargetProperties on a LabelAttribute",
1019 moduleTypeUnderTest: "cc_library",
1020 moduleTypeUnderTestFactory: cc.LibraryFactory,
1021 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1022 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1023 dir: "foo/bar",
1024 filesystem: map[string]string{
1025 "foo/bar/Android.bp": `
Liz Kammerd366c902021-06-03 13:43:01 -04001026 cc_library {
1027 name: "a",
1028 srcs: ["a.cpp"],
1029 target: {
1030 android_arm: {
1031 version_script: "android_arm.map",
1032 },
1033 linux_bionic_arm64: {
1034 version_script: "linux_bionic_arm64.map",
1035 },
1036 },
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001037
Liz Kammerd366c902021-06-03 13:43:01 -04001038 bazel_module: { bp2build_available: true },
1039 }
1040 `,
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001041 },
1042 blueprint: soongCcLibraryPreamble,
1043 expectedBazelTargets: []string{`cc_library(
1044 name = "a",
1045 copts = [
1046 "-Ifoo/bar",
1047 "-I$(BINDIR)/foo/bar",
1048 ],
1049 srcs = ["a.cpp"],
1050 version_script = select({
Rupert Shuttleworthffd45822021-05-14 03:02:34 -04001051 "//build/bazel/platforms/os_arch:android_arm": "android_arm.map",
1052 "//build/bazel/platforms/os_arch:linux_bionic_arm64": "linux_bionic_arm64.map",
Rupert Shuttleworth22cd2eb2021-05-27 02:15:54 -04001053 "//conditions:default": None,
1054 }),
1055)`},
1056 })
1057}
Liz Kammer47535c52021-06-02 16:02:22 -04001058
1059func TestCcLibraryExcludeLibs(t *testing.T) {
1060 runCcLibraryTestCase(t, bp2buildTestCase{
1061 moduleTypeUnderTest: "cc_library",
1062 moduleTypeUnderTestFactory: cc.LibraryFactory,
1063 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1064 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1065 filesystem: map[string]string{},
1066 blueprint: soongCcLibraryStaticPreamble + `
1067cc_library {
1068 name: "foo_static",
1069 srcs: ["common.c"],
1070 whole_static_libs: [
1071 "arm_whole_static_lib_excludes",
1072 "malloc_not_svelte_whole_static_lib_excludes"
1073 ],
1074 static_libs: [
1075 "arm_static_lib_excludes",
1076 "malloc_not_svelte_static_lib_excludes"
1077 ],
1078 shared_libs: [
1079 "arm_shared_lib_excludes",
1080 ],
1081 arch: {
1082 arm: {
1083 exclude_shared_libs: [
1084 "arm_shared_lib_excludes",
1085 ],
1086 exclude_static_libs: [
1087 "arm_static_lib_excludes",
1088 "arm_whole_static_lib_excludes",
1089 ],
1090 },
1091 },
1092 product_variables: {
1093 malloc_not_svelte: {
1094 shared_libs: ["malloc_not_svelte_shared_lib"],
1095 whole_static_libs: ["malloc_not_svelte_whole_static_lib"],
1096 exclude_static_libs: [
1097 "malloc_not_svelte_static_lib_excludes",
1098 "malloc_not_svelte_whole_static_lib_excludes",
1099 ],
1100 },
1101 },
1102}
1103
1104cc_library {
1105 name: "arm_whole_static_lib_excludes",
1106 bazel_module: { bp2build_available: false },
1107}
1108
1109cc_library {
1110 name: "malloc_not_svelte_whole_static_lib",
1111 bazel_module: { bp2build_available: false },
1112}
1113
1114cc_library {
1115 name: "malloc_not_svelte_whole_static_lib_excludes",
1116 bazel_module: { bp2build_available: false },
1117}
1118
1119cc_library {
1120 name: "arm_static_lib_excludes",
1121 bazel_module: { bp2build_available: false },
1122}
1123
1124cc_library {
1125 name: "malloc_not_svelte_static_lib_excludes",
1126 bazel_module: { bp2build_available: false },
1127}
1128
1129cc_library {
1130 name: "arm_shared_lib_excludes",
1131 bazel_module: { bp2build_available: false },
1132}
1133
1134cc_library {
1135 name: "malloc_not_svelte_shared_lib",
1136 bazel_module: { bp2build_available: false },
1137}
1138`,
1139 expectedBazelTargets: []string{
1140 `cc_library(
1141 name = "foo_static",
1142 copts = [
1143 "-I.",
1144 "-I$(BINDIR)/.",
1145 ],
1146 dynamic_deps = select({
1147 "//build/bazel/platforms/arch:arm": [],
1148 "//conditions:default": [":arm_shared_lib_excludes"],
1149 }) + select({
1150 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_shared_lib"],
1151 "//conditions:default": [],
1152 }),
1153 implementation_deps = select({
1154 "//build/bazel/platforms/arch:arm": [],
1155 "//conditions:default": [":arm_static_lib_excludes"],
1156 }) + select({
1157 "//build/bazel/product_variables:malloc_not_svelte": [],
1158 "//conditions:default": [":malloc_not_svelte_static_lib_excludes"],
1159 }),
1160 srcs_c = ["common.c"],
1161 whole_archive_deps = select({
1162 "//build/bazel/platforms/arch:arm": [],
1163 "//conditions:default": [":arm_whole_static_lib_excludes"],
1164 }) + select({
1165 "//build/bazel/product_variables:malloc_not_svelte": [":malloc_not_svelte_whole_static_lib"],
1166 "//conditions:default": [":malloc_not_svelte_whole_static_lib_excludes"],
1167 }),
1168)`,
1169 },
1170 })
1171}
Liz Kammerd366c902021-06-03 13:43:01 -04001172
1173func TestCCLibraryNoCrtTrue(t *testing.T) {
1174 runCcLibraryTestCase(t, bp2buildTestCase{
1175 description: "cc_library - simple example",
1176 moduleTypeUnderTest: "cc_library",
1177 moduleTypeUnderTestFactory: cc.LibraryFactory,
1178 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1179 filesystem: map[string]string{
1180 "impl.cpp": "",
1181 },
1182 blueprint: soongCcLibraryPreamble + `
1183cc_library_headers { name: "some-headers" }
1184cc_library {
1185 name: "foo-lib",
1186 srcs: ["impl.cpp"],
1187 no_libcrt: true,
1188}
1189`,
1190 expectedBazelTargets: []string{`cc_library(
1191 name = "foo-lib",
1192 copts = [
1193 "-I.",
1194 "-I$(BINDIR)/.",
1195 ],
1196 srcs = ["impl.cpp"],
1197 use_libcrt = False,
1198)`}})
1199}
1200
1201func TestCCLibraryNoCrtFalse(t *testing.T) {
1202 runCcLibraryTestCase(t, bp2buildTestCase{
1203 moduleTypeUnderTest: "cc_library",
1204 moduleTypeUnderTestFactory: cc.LibraryFactory,
1205 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1206 filesystem: map[string]string{
1207 "impl.cpp": "",
1208 },
1209 blueprint: soongCcLibraryPreamble + `
1210cc_library_headers { name: "some-headers" }
1211cc_library {
1212 name: "foo-lib",
1213 srcs: ["impl.cpp"],
1214 no_libcrt: false,
1215}
1216`,
1217 expectedBazelTargets: []string{`cc_library(
1218 name = "foo-lib",
1219 copts = [
1220 "-I.",
1221 "-I$(BINDIR)/.",
1222 ],
1223 srcs = ["impl.cpp"],
1224 use_libcrt = True,
1225)`}})
1226}
1227
1228func TestCCLibraryNoCrtArchVariant(t *testing.T) {
1229 runCcLibraryTestCase(t, bp2buildTestCase{
1230 moduleTypeUnderTest: "cc_library",
1231 moduleTypeUnderTestFactory: cc.LibraryFactory,
1232 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1233 filesystem: map[string]string{
1234 "impl.cpp": "",
1235 },
1236 blueprint: soongCcLibraryPreamble + `
1237cc_library_headers { name: "some-headers" }
1238cc_library {
1239 name: "foo-lib",
1240 srcs: ["impl.cpp"],
1241 arch: {
1242 arm: {
1243 no_libcrt: true,
1244 },
1245 x86: {
1246 no_libcrt: true,
1247 },
1248 },
1249}
1250`,
1251 expectedBazelTargets: []string{`cc_library(
1252 name = "foo-lib",
1253 copts = [
1254 "-I.",
1255 "-I$(BINDIR)/.",
1256 ],
1257 srcs = ["impl.cpp"],
1258 use_libcrt = select({
1259 "//build/bazel/platforms/arch:arm": False,
1260 "//build/bazel/platforms/arch:x86": False,
1261 "//conditions:default": None,
1262 }),
1263)`}})
1264}
1265
1266func TestCCLibraryNoCrtArchVariantWithDefault(t *testing.T) {
1267 runCcLibraryTestCase(t, bp2buildTestCase{
1268 moduleTypeUnderTest: "cc_library",
1269 moduleTypeUnderTestFactory: cc.LibraryFactory,
1270 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1271 filesystem: map[string]string{
1272 "impl.cpp": "",
1273 },
1274 blueprint: soongCcLibraryPreamble + `
1275cc_library_headers { name: "some-headers" }
1276cc_library {
1277 name: "foo-lib",
1278 srcs: ["impl.cpp"],
1279 no_libcrt: false,
1280 arch: {
1281 arm: {
1282 no_libcrt: true,
1283 },
1284 x86: {
1285 no_libcrt: true,
1286 },
1287 },
1288}
1289`,
1290 expectedBazelTargets: []string{`cc_library(
1291 name = "foo-lib",
1292 copts = [
1293 "-I.",
1294 "-I$(BINDIR)/.",
1295 ],
1296 srcs = ["impl.cpp"],
1297 use_libcrt = select({
1298 "//build/bazel/platforms/arch:arm": False,
1299 "//build/bazel/platforms/arch:x86": False,
1300 "//conditions:default": True,
1301 }),
1302)`}})
1303}
Jingwen Chen3d383bb2021-06-09 07:18:37 +00001304
1305func TestCcLibraryStrip(t *testing.T) {
1306 runCcLibraryTestCase(t, bp2buildTestCase{
1307 description: "cc_library strip args",
1308 moduleTypeUnderTest: "cc_library",
1309 moduleTypeUnderTestFactory: cc.LibraryFactory,
1310 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1311 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1312 dir: "foo/bar",
1313 filesystem: map[string]string{
1314 "foo/bar/Android.bp": `
1315cc_library {
1316 name: "nothing",
1317 bazel_module: { bp2build_available: true },
1318}
1319cc_library {
1320 name: "keep_symbols",
1321 bazel_module: { bp2build_available: true },
1322 strip: {
1323 keep_symbols: true,
1324 }
1325}
1326cc_library {
1327 name: "keep_symbols_and_debug_frame",
1328 bazel_module: { bp2build_available: true },
1329 strip: {
1330 keep_symbols_and_debug_frame: true,
1331 }
1332}
1333cc_library {
1334 name: "none",
1335 bazel_module: { bp2build_available: true },
1336 strip: {
1337 none: true,
1338 }
1339}
1340cc_library {
1341 name: "keep_symbols_list",
1342 bazel_module: { bp2build_available: true },
1343 strip: {
1344 keep_symbols_list: ["symbol"],
1345 }
1346}
1347cc_library {
1348 name: "all",
1349 bazel_module: { bp2build_available: true },
1350 strip: {
1351 all: true,
1352 }
1353}
1354`,
1355 },
1356 blueprint: soongCcLibraryPreamble,
1357 expectedBazelTargets: []string{`cc_library(
1358 name = "all",
1359 copts = [
1360 "-Ifoo/bar",
1361 "-I$(BINDIR)/foo/bar",
1362 ],
1363 strip = {
1364 "all": True,
1365 },
1366)`, `cc_library(
1367 name = "keep_symbols",
1368 copts = [
1369 "-Ifoo/bar",
1370 "-I$(BINDIR)/foo/bar",
1371 ],
1372 strip = {
1373 "keep_symbols": True,
1374 },
1375)`, `cc_library(
1376 name = "keep_symbols_and_debug_frame",
1377 copts = [
1378 "-Ifoo/bar",
1379 "-I$(BINDIR)/foo/bar",
1380 ],
1381 strip = {
1382 "keep_symbols_and_debug_frame": True,
1383 },
1384)`, `cc_library(
1385 name = "keep_symbols_list",
1386 copts = [
1387 "-Ifoo/bar",
1388 "-I$(BINDIR)/foo/bar",
1389 ],
1390 strip = {
1391 "keep_symbols_list": ["symbol"],
1392 },
1393)`, `cc_library(
1394 name = "none",
1395 copts = [
1396 "-Ifoo/bar",
1397 "-I$(BINDIR)/foo/bar",
1398 ],
1399 strip = {
1400 "none": True,
1401 },
1402)`, `cc_library(
1403 name = "nothing",
1404 copts = [
1405 "-Ifoo/bar",
1406 "-I$(BINDIR)/foo/bar",
1407 ],
1408)`},
1409 })
1410}
1411
1412func TestCcLibraryStripWithArch(t *testing.T) {
1413 runCcLibraryTestCase(t, bp2buildTestCase{
1414 description: "cc_library strip args",
1415 moduleTypeUnderTest: "cc_library",
1416 moduleTypeUnderTestFactory: cc.LibraryFactory,
1417 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
1418 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1419 dir: "foo/bar",
1420 filesystem: map[string]string{
1421 "foo/bar/Android.bp": `
1422cc_library {
1423 name: "multi-arch",
1424 bazel_module: { bp2build_available: true },
1425 target: {
1426 darwin: {
1427 strip: {
1428 keep_symbols_list: ["foo", "bar"]
1429 }
1430 },
1431 },
1432 arch: {
1433 arm: {
1434 strip: {
1435 keep_symbols_and_debug_frame: true,
1436 },
1437 },
1438 arm64: {
1439 strip: {
1440 keep_symbols: true,
1441 },
1442 },
1443 }
1444}
1445`,
1446 },
1447 blueprint: soongCcLibraryPreamble,
1448 expectedBazelTargets: []string{`cc_library(
1449 name = "multi-arch",
1450 copts = [
1451 "-Ifoo/bar",
1452 "-I$(BINDIR)/foo/bar",
1453 ],
1454 strip = {
1455 "keep_symbols": select({
1456 "//build/bazel/platforms/arch:arm64": True,
1457 "//conditions:default": None,
1458 }),
1459 "keep_symbols_and_debug_frame": select({
1460 "//build/bazel/platforms/arch:arm": True,
1461 "//conditions:default": None,
1462 }),
1463 "keep_symbols_list": select({
1464 "//build/bazel/platforms/os:darwin": [
1465 "foo",
1466 "bar",
1467 ],
1468 "//conditions:default": [],
1469 }),
1470 },
1471)`},
1472 })
1473}