blob: 6e19e130ca716407c9581e8b86e5bdf1649a7b90 [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 {
28 name: "linux_bionic_supported",
29}
30
31toolchain_library {
32 name: "libclang_rt.builtins-x86_64-android",
33 defaults: ["linux_bionic_supported"],
34 vendor_available: true,
35 vendor_ramdisk_available: true,
36 product_available: true,
37 recovery_available: true,
38 native_bridge_supported: true,
39 src: "",
40}`
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)
50 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
51 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
52 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
53}
54
55func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040056 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020057 dir := "."
58 filesystem := make(map[string][]byte)
59 toParse := []string{
60 "Android.bp",
61 }
62 for f, content := range tc.filesystem {
63 if strings.HasSuffix(f, "Android.bp") {
64 toParse = append(toParse, f)
65 }
66 filesystem[f] = []byte(content)
67 }
68 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
69 ctx := android.NewTestContext(config)
70
71 registerModuleTypes(ctx)
72 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
73 ctx.RegisterBp2BuildConfig(bp2buildConfig)
74 for _, m := range tc.depsMutators {
75 ctx.DepsBp2BuildMutators(m)
76 }
77 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
78 ctx.RegisterForBazelConversion()
79
80 _, errs := ctx.ParseFileList(dir, toParse)
81 if errored(t, tc.description, errs) {
82 return
83 }
84 _, errs = ctx.ResolveDependencies(config)
85 if errored(t, tc.description, errs) {
86 return
87 }
88
89 checkDir := dir
90 if tc.dir != "" {
91 checkDir = tc.dir
92 }
93 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
94 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
95 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
96 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
97 } else {
98 for i, target := range bazelTargets {
99 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
100 t.Errorf(
101 "%s: Expected generated Bazel target to be '%s', got '%s'",
102 tc.description,
103 w,
104 g,
105 )
106 }
107 }
108 }
109}
110
111func TestCcLibrarySimple(t *testing.T) {
112 runCcLibraryTestCase(t, bp2buildTestCase{
113 description: "cc_library - simple example",
114 moduleTypeUnderTest: "cc_library",
115 moduleTypeUnderTestFactory: cc.LibraryFactory,
116 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
117 filesystem: map[string]string{
118 "android.cpp": "",
119 "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 },
165 },
166}
167`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200168 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400169 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000170 copts = [
171 "-Wall",
172 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400173 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000174 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400175 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000176 includes = ["foo-dir"],
177 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
178 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
179 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
180 "//conditions:default": [],
181 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000182 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000183 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
184 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400185 "//conditions:default": [],
186 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000187 "//build/bazel/platforms/os:android": ["android.cpp"],
188 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
189 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400190 "//conditions:default": [],
191 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200192)`}})
193}
194
195func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
196 runCcLibraryTestCase(t, bp2buildTestCase{
197 description: "cc_library - trimmed example of //bionic/linker:ld-android",
198 moduleTypeUnderTest: "cc_library",
199 moduleTypeUnderTestFactory: cc.LibraryFactory,
200 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
201 filesystem: map[string]string{
202 "ld-android.cpp": "",
203 "linked_list.h": "",
204 "linker.h": "",
205 "linker_block_allocator.h": "",
206 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400207 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200208 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400209cc_library_headers { name: "libc_headers" }
210cc_library {
211 name: "fake-ld-android",
212 srcs: ["ld_android.cpp"],
213 cflags: [
214 "-Wall",
215 "-Wextra",
216 "-Wunused",
217 "-Werror",
218 ],
219 header_libs: ["libc_headers"],
220 ldflags: [
221 "-Wl,--exclude-libs=libgcc.a",
222 "-Wl,--exclude-libs=libgcc_stripped.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
225 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
226 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
227 ],
228 arch: {
229 x86: {
230 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
231 },
232 x86_64: {
233 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
234 },
235 },
236}
237`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200238 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400239 name = "fake-ld-android",
240 copts = [
241 "-Wall",
242 "-Wextra",
243 "-Wunused",
244 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000245 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400246 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400247 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400248 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400249 linkopts = [
250 "-Wl,--exclude-libs=libgcc.a",
251 "-Wl,--exclude-libs=libgcc_stripped.a",
252 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
253 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
254 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
255 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
256 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000257 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
258 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400259 "//conditions:default": [],
260 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000261 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400262)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200263 })
264}
265
266func TestCcLibraryExcludeSrcs(t *testing.T) {
267 runCcLibraryTestCase(t, bp2buildTestCase{
268 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
269 moduleTypeUnderTest: "cc_library",
270 moduleTypeUnderTestFactory: cc.LibraryFactory,
271 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
272 dir: "external",
273 filesystem: map[string]string{
274 "external/math/cosf.c": "",
275 "external/math/erf.c": "",
276 "external/math/erf_data.c": "",
277 "external/math/erff.c": "",
278 "external/math/erff_data.c": "",
279 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000280cc_library {
281 name: "fake-libarm-optimized-routines-math",
282 exclude_srcs: [
283 // Provided by:
284 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
285 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
286 "math/erf.c",
287 "math/erf_data.c",
288 "math/erff.c",
289 "math/erff_data.c",
290 ],
291 srcs: [
292 "math/*.c",
293 ],
294 // arch-specific settings
295 arch: {
296 arm64: {
297 cflags: [
298 "-DHAVE_FAST_FMA=1",
299 ],
300 },
301 },
302 bazel_module: { bp2build_available: true },
303}
304`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200305 },
306 blueprint: soongCcLibraryPreamble,
307 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000308 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400309 copts = [
310 "-Iexternal",
311 "-I$(BINDIR)/external",
312 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000313 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
314 "//conditions:default": [],
315 }),
316 srcs = ["math/cosf.c"],
317)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200318 })
319}
320
321func TestCcLibrarySharedStaticProps(t *testing.T) {
322 runCcLibraryTestCase(t, bp2buildTestCase{
323 description: "cc_library shared/static props",
324 moduleTypeUnderTest: "cc_library",
325 moduleTypeUnderTestFactory: cc.LibraryFactory,
326 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
327 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
328 dir: "foo/bar",
329 filesystem: map[string]string{
330 "foo/bar/both.cpp": "",
331 "foo/bar/sharedonly.cpp": "",
332 "foo/bar/staticonly.cpp": "",
333 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000334cc_library {
335 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400336 srcs: ["both.cpp"],
337 cflags: ["bothflag"],
338 shared_libs: ["shared_dep_for_both"],
339 static_libs: ["static_dep_for_both"],
340 whole_static_libs: ["whole_static_lib_for_both"],
341 static: {
342 srcs: ["staticonly.cpp"],
343 cflags: ["staticflag"],
344 shared_libs: ["shared_dep_for_static"],
345 static_libs: ["static_dep_for_static"],
346 whole_static_libs: ["whole_static_lib_for_static"],
347 },
348 shared: {
349 srcs: ["sharedonly.cpp"],
350 cflags: ["sharedflag"],
351 shared_libs: ["shared_dep_for_shared"],
352 static_libs: ["static_dep_for_shared"],
353 whole_static_libs: ["whole_static_lib_for_shared"],
354 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000355 bazel_module: { bp2build_available: true },
356}
357
Chris Parsons08648312021-05-06 16:23:19 -0400358cc_library_static { name: "static_dep_for_shared" }
359
360cc_library_static { name: "static_dep_for_static" }
361
362cc_library_static { name: "static_dep_for_both" }
363
364cc_library_static { name: "whole_static_lib_for_shared" }
365
366cc_library_static { name: "whole_static_lib_for_static" }
367
368cc_library_static { name: "whole_static_lib_for_both" }
369
370cc_library { name: "shared_dep_for_shared" }
371
372cc_library { name: "shared_dep_for_static" }
373
374cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000375`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200376 },
377 blueprint: soongCcLibraryPreamble,
378 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000379 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400380 copts = [
381 "bothflag",
382 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400383 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400384 ],
Chris Parsons08648312021-05-06 16:23:19 -0400385 dynamic_deps = [":shared_dep_for_both"],
386 dynamic_deps_for_shared = [":shared_dep_for_shared"],
387 dynamic_deps_for_static = [":shared_dep_for_static"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400388 implementation_deps = [":static_dep_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400389 shared_copts = ["sharedflag"],
390 shared_srcs = ["sharedonly.cpp"],
391 srcs = ["both.cpp"],
392 static_copts = ["staticflag"],
393 static_deps_for_shared = [":static_dep_for_shared"],
394 static_deps_for_static = [":static_dep_for_static"],
395 static_srcs = ["staticonly.cpp"],
396 whole_archive_deps = [":whole_static_lib_for_both"],
397 whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
398 whole_archive_deps_for_static = [":whole_static_lib_for_static"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000399)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200400 })
401}
402
403func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
404 runCcLibraryTestCase(t, bp2buildTestCase{
405 description: "cc_library non-configured version script",
406 moduleTypeUnderTest: "cc_library",
407 moduleTypeUnderTestFactory: cc.LibraryFactory,
408 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
409 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
410 dir: "foo/bar",
411 filesystem: map[string]string{
412 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200413cc_library {
414 name: "a",
415 srcs: ["a.cpp"],
416 version_script: "v.map",
417 bazel_module: { bp2build_available: true },
418}
419`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200420 },
421 blueprint: soongCcLibraryPreamble,
422 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200423 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400424 copts = [
425 "-Ifoo/bar",
426 "-I$(BINDIR)/foo/bar",
427 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200428 srcs = ["a.cpp"],
429 version_script = "v.map",
430)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200431 })
432}
433
434func TestCcLibraryConfiguredVersionScript(t *testing.T) {
435 runCcLibraryTestCase(t, bp2buildTestCase{
436 description: "cc_library configured version script",
437 moduleTypeUnderTest: "cc_library",
438 moduleTypeUnderTestFactory: cc.LibraryFactory,
439 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
440 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
441 dir: "foo/bar",
442 filesystem: map[string]string{
443 "foo/bar/Android.bp": `
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200444 cc_library {
445 name: "a",
446 srcs: ["a.cpp"],
447 arch: {
448 arm: {
449 version_script: "arm.map",
450 },
451 arm64: {
452 version_script: "arm64.map",
453 },
454 },
455
456 bazel_module: { bp2build_available: true },
457 }
458 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200459 },
460 blueprint: soongCcLibraryPreamble,
461 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200462 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400463 copts = [
464 "-Ifoo/bar",
465 "-I$(BINDIR)/foo/bar",
466 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200467 srcs = ["a.cpp"],
468 version_script = select({
469 "//build/bazel/platforms/arch:arm": "arm.map",
470 "//build/bazel/platforms/arch:arm64": "arm64.map",
471 "//conditions:default": None,
472 }),
473)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200474 })
475}
476
477func TestCcLibrarySharedLibs(t *testing.T) {
478 runCcLibraryTestCase(t, bp2buildTestCase{
479 description: "cc_library shared_libs",
480 moduleTypeUnderTest: "cc_library",
481 moduleTypeUnderTestFactory: cc.LibraryFactory,
482 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
483 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
484 dir: "foo/bar",
485 filesystem: map[string]string{
486 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400487cc_library {
488 name: "mylib",
489 bazel_module: { bp2build_available: true },
490}
491
492cc_library {
493 name: "a",
494 shared_libs: ["mylib",],
495 bazel_module: { bp2build_available: true },
496}
497`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200498 },
499 blueprint: soongCcLibraryPreamble,
500 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400501 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400502 copts = [
503 "-Ifoo/bar",
504 "-I$(BINDIR)/foo/bar",
505 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400506 dynamic_deps = [":mylib"],
507)`, `cc_library(
508 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400509 copts = [
510 "-Ifoo/bar",
511 "-I$(BINDIR)/foo/bar",
512 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400513)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200514 })
515}
516
517func TestCcLibraryPackRelocations(t *testing.T) {
518 runCcLibraryTestCase(t, bp2buildTestCase{
519 description: "cc_library pack_relocations test",
520 moduleTypeUnderTest: "cc_library",
521 moduleTypeUnderTestFactory: cc.LibraryFactory,
522 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
523 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
524 dir: "foo/bar",
525 filesystem: map[string]string{
526 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400527cc_library {
528 name: "a",
529 srcs: ["a.cpp"],
530 pack_relocations: false,
531 bazel_module: { bp2build_available: true },
532}
533
534cc_library {
535 name: "b",
536 srcs: ["b.cpp"],
537 arch: {
538 x86_64: {
539 pack_relocations: false,
540 },
541 },
542 bazel_module: { bp2build_available: true },
543}
544
545cc_library {
546 name: "c",
547 srcs: ["c.cpp"],
548 target: {
549 darwin: {
550 pack_relocations: false,
551 },
552 },
553 bazel_module: { bp2build_available: true },
554}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200555 },
556 blueprint: soongCcLibraryPreamble,
557 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400558 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400559 copts = [
560 "-Ifoo/bar",
561 "-I$(BINDIR)/foo/bar",
562 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400563 linkopts = ["-Wl,--pack-dyn-relocs=none"],
564 srcs = ["a.cpp"],
565)`, `cc_library(
566 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400567 copts = [
568 "-Ifoo/bar",
569 "-I$(BINDIR)/foo/bar",
570 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400571 linkopts = select({
572 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
573 "//conditions:default": [],
574 }),
575 srcs = ["b.cpp"],
576)`, `cc_library(
577 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400578 copts = [
579 "-Ifoo/bar",
580 "-I$(BINDIR)/foo/bar",
581 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400582 linkopts = select({
583 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
584 "//conditions:default": [],
585 }),
586 srcs = ["c.cpp"],
587)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200588 })
589}
590
591func TestCcLibrarySpacesInCopts(t *testing.T) {
592 runCcLibraryTestCase(t, bp2buildTestCase{
593 description: "cc_library spaces in copts",
594 moduleTypeUnderTest: "cc_library",
595 moduleTypeUnderTestFactory: cc.LibraryFactory,
596 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
597 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
598 dir: "foo/bar",
599 filesystem: map[string]string{
600 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000601cc_library {
602 name: "a",
603 cflags: ["-include header.h",],
604 bazel_module: { bp2build_available: true },
605}
606`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200607 },
608 blueprint: soongCcLibraryPreamble,
609 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000610 name = "a",
611 copts = [
612 "-include",
613 "header.h",
614 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400615 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000616 ],
617)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200618 })
619}
620
621func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
622 runCcLibraryTestCase(t, bp2buildTestCase{
623 description: "cc_library cppflags goes into copts",
624 moduleTypeUnderTest: "cc_library",
625 moduleTypeUnderTestFactory: cc.LibraryFactory,
626 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
627 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
628 dir: "foo/bar",
629 filesystem: map[string]string{
630 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000631 name: "a",
632 srcs: ["a.cpp"],
633 cflags: [
634 "-Wall",
635 ],
636 cppflags: [
637 "-fsigned-char",
638 "-pedantic",
639 ],
640 arch: {
641 arm64: {
642 cppflags: ["-DARM64=1"],
643 },
644 },
645 target: {
646 android: {
647 cppflags: ["-DANDROID=1"],
648 },
649 },
650 bazel_module: { bp2build_available: true },
651}
652`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200653 },
654 blueprint: soongCcLibraryPreamble,
655 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000656 name = "a",
657 copts = [
658 "-Wall",
659 "-fsigned-char",
660 "-pedantic",
661 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400662 "-I$(BINDIR)/foo/bar",
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000663 ] + select({
664 "//build/bazel/platforms/arch:arm64": ["-DARM64=1"],
665 "//conditions:default": [],
666 }) + select({
667 "//build/bazel/platforms/os:android": ["-DANDROID=1"],
668 "//conditions:default": [],
669 }),
670 srcs = ["a.cpp"],
671)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200672 })
Jingwen Chen63930982021-03-24 10:04:33 -0400673}