blob: 454b15320549d1030f886501b95cf22204e261ef [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) {
44 runBp2BuildTestCase(t, registerCcLibraryModuleTypes, tc)
45}
46
47func registerCcLibraryModuleTypes(ctx android.RegistrationContext) {
48 cc.RegisterCCBuildComponents(ctx)
49 ctx.RegisterModuleType("cc_library_static", cc.LibraryStaticFactory)
50 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
51 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
52}
53
54func runBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc bp2buildTestCase) {
55 dir := "."
56 filesystem := make(map[string][]byte)
57 toParse := []string{
58 "Android.bp",
59 }
60 for f, content := range tc.filesystem {
61 if strings.HasSuffix(f, "Android.bp") {
62 toParse = append(toParse, f)
63 }
64 filesystem[f] = []byte(content)
65 }
66 config := android.TestConfig(buildDir, nil, tc.blueprint, filesystem)
67 ctx := android.NewTestContext(config)
68
69 registerModuleTypes(ctx)
70 ctx.RegisterModuleType(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestFactory)
71 ctx.RegisterBp2BuildConfig(bp2buildConfig)
72 for _, m := range tc.depsMutators {
73 ctx.DepsBp2BuildMutators(m)
74 }
75 ctx.RegisterBp2BuildMutator(tc.moduleTypeUnderTest, tc.moduleTypeUnderTestBp2BuildMutator)
76 ctx.RegisterForBazelConversion()
77
78 _, errs := ctx.ParseFileList(dir, toParse)
79 if errored(t, tc.description, errs) {
80 return
81 }
82 _, errs = ctx.ResolveDependencies(config)
83 if errored(t, tc.description, errs) {
84 return
85 }
86
87 checkDir := dir
88 if tc.dir != "" {
89 checkDir = tc.dir
90 }
91 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
92 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
93 if actualCount, expectedCount := len(bazelTargets), len(tc.expectedBazelTargets); actualCount != expectedCount {
94 t.Errorf("%s: Expected %d bazel target, got %d", tc.description, expectedCount, actualCount)
95 } else {
96 for i, target := range bazelTargets {
97 if w, g := tc.expectedBazelTargets[i], target.content; w != g {
98 t.Errorf(
99 "%s: Expected generated Bazel target to be '%s', got '%s'",
100 tc.description,
101 w,
102 g,
103 )
104 }
105 }
106 }
107}
108
109func TestCcLibrarySimple(t *testing.T) {
110 runCcLibraryTestCase(t, bp2buildTestCase{
111 description: "cc_library - simple example",
112 moduleTypeUnderTest: "cc_library",
113 moduleTypeUnderTestFactory: cc.LibraryFactory,
114 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
115 filesystem: map[string]string{
116 "android.cpp": "",
117 "darwin.cpp": "",
118 // Refer to cc.headerExts for the supported header extensions in Soong.
119 "header.h": "",
120 "header.hh": "",
121 "header.hpp": "",
122 "header.hxx": "",
123 "header.h++": "",
124 "header.inl": "",
125 "header.inc": "",
126 "header.ipp": "",
127 "header.h.generic": "",
128 "impl.cpp": "",
129 "linux.cpp": "",
130 "x86.cpp": "",
131 "x86_64.cpp": "",
132 "foo-dir/a.h": "",
133 },
134 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400135cc_library_headers { name: "some-headers" }
136cc_library {
137 name: "foo-lib",
138 srcs: ["impl.cpp"],
139 cflags: ["-Wall"],
140 header_libs: ["some-headers"],
141 export_include_dirs: ["foo-dir"],
142 ldflags: ["-Wl,--exclude-libs=bar.a"],
143 arch: {
144 x86: {
145 ldflags: ["-Wl,--exclude-libs=baz.a"],
146 srcs: ["x86.cpp"],
147 },
148 x86_64: {
149 ldflags: ["-Wl,--exclude-libs=qux.a"],
150 srcs: ["x86_64.cpp"],
151 },
152 },
153 target: {
154 android: {
155 srcs: ["android.cpp"],
156 },
157 linux_glibc: {
158 srcs: ["linux.cpp"],
159 },
160 darwin: {
161 srcs: ["darwin.cpp"],
162 },
163 },
164}
165`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200166 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400167 name = "foo-lib",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000168 copts = [
169 "-Wall",
170 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400171 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000172 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400173 implementation_deps = [":some-headers"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000174 includes = ["foo-dir"],
175 linkopts = ["-Wl,--exclude-libs=bar.a"] + select({
176 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=baz.a"],
177 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=qux.a"],
178 "//conditions:default": [],
179 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000180 srcs = ["impl.cpp"] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000181 "//build/bazel/platforms/arch:x86": ["x86.cpp"],
182 "//build/bazel/platforms/arch:x86_64": ["x86_64.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400183 "//conditions:default": [],
184 }) + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000185 "//build/bazel/platforms/os:android": ["android.cpp"],
186 "//build/bazel/platforms/os:darwin": ["darwin.cpp"],
187 "//build/bazel/platforms/os:linux": ["linux.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400188 "//conditions:default": [],
189 }),
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200190)`}})
191}
192
193func TestCcLibraryTrimmedLdAndroid(t *testing.T) {
194 runCcLibraryTestCase(t, bp2buildTestCase{
195 description: "cc_library - trimmed example of //bionic/linker:ld-android",
196 moduleTypeUnderTest: "cc_library",
197 moduleTypeUnderTestFactory: cc.LibraryFactory,
198 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
199 filesystem: map[string]string{
200 "ld-android.cpp": "",
201 "linked_list.h": "",
202 "linker.h": "",
203 "linker_block_allocator.h": "",
204 "linker_cfi.h": "",
Jingwen Chen63930982021-03-24 10:04:33 -0400205 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200206 blueprint: soongCcLibraryPreamble + `
Jingwen Chen63930982021-03-24 10:04:33 -0400207cc_library_headers { name: "libc_headers" }
208cc_library {
209 name: "fake-ld-android",
210 srcs: ["ld_android.cpp"],
211 cflags: [
212 "-Wall",
213 "-Wextra",
214 "-Wunused",
215 "-Werror",
216 ],
217 header_libs: ["libc_headers"],
218 ldflags: [
219 "-Wl,--exclude-libs=libgcc.a",
220 "-Wl,--exclude-libs=libgcc_stripped.a",
221 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
222 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
223 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
224 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
225 ],
226 arch: {
227 x86: {
228 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
229 },
230 x86_64: {
231 ldflags: ["-Wl,--exclude-libs=libgcc_eh.a"],
232 },
233 },
234}
235`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200236 expectedBazelTargets: []string{`cc_library(
Jingwen Chen63930982021-03-24 10:04:33 -0400237 name = "fake-ld-android",
238 copts = [
239 "-Wall",
240 "-Wextra",
241 "-Wunused",
242 "-Werror",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000243 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400244 "-I$(BINDIR)/.",
Jingwen Chen63930982021-03-24 10:04:33 -0400245 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400246 implementation_deps = [":libc_headers"],
Jingwen Chen63930982021-03-24 10:04:33 -0400247 linkopts = [
248 "-Wl,--exclude-libs=libgcc.a",
249 "-Wl,--exclude-libs=libgcc_stripped.a",
250 "-Wl,--exclude-libs=libclang_rt.builtins-arm-android.a",
251 "-Wl,--exclude-libs=libclang_rt.builtins-aarch64-android.a",
252 "-Wl,--exclude-libs=libclang_rt.builtins-i686-android.a",
253 "-Wl,--exclude-libs=libclang_rt.builtins-x86_64-android.a",
254 ] + select({
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000255 "//build/bazel/platforms/arch:x86": ["-Wl,--exclude-libs=libgcc_eh.a"],
256 "//build/bazel/platforms/arch:x86_64": ["-Wl,--exclude-libs=libgcc_eh.a"],
Jingwen Chen63930982021-03-24 10:04:33 -0400257 "//conditions:default": [],
258 }),
Jingwen Chen882bcc12021-04-27 05:54:20 +0000259 srcs = ["ld_android.cpp"],
Jingwen Chen63930982021-03-24 10:04:33 -0400260)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200261 })
262}
263
264func TestCcLibraryExcludeSrcs(t *testing.T) {
265 runCcLibraryTestCase(t, bp2buildTestCase{
266 description: "cc_library exclude_srcs - trimmed example of //external/arm-optimized-routines:libarm-optimized-routines-math",
267 moduleTypeUnderTest: "cc_library",
268 moduleTypeUnderTestFactory: cc.LibraryFactory,
269 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
270 dir: "external",
271 filesystem: map[string]string{
272 "external/math/cosf.c": "",
273 "external/math/erf.c": "",
274 "external/math/erf_data.c": "",
275 "external/math/erff.c": "",
276 "external/math/erff_data.c": "",
277 "external/Android.bp": `
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000278cc_library {
279 name: "fake-libarm-optimized-routines-math",
280 exclude_srcs: [
281 // Provided by:
282 // bionic/libm/upstream-freebsd/lib/msun/src/s_erf.c
283 // bionic/libm/upstream-freebsd/lib/msun/src/s_erff.c
284 "math/erf.c",
285 "math/erf_data.c",
286 "math/erff.c",
287 "math/erff_data.c",
288 ],
289 srcs: [
290 "math/*.c",
291 ],
292 // arch-specific settings
293 arch: {
294 arm64: {
295 cflags: [
296 "-DHAVE_FAST_FMA=1",
297 ],
298 },
299 },
300 bazel_module: { bp2build_available: true },
301}
302`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200303 },
304 blueprint: soongCcLibraryPreamble,
305 expectedBazelTargets: []string{`cc_library(
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000306 name = "fake-libarm-optimized-routines-math",
Chris Parsons484e50a2021-05-13 15:13:04 -0400307 copts = [
308 "-Iexternal",
309 "-I$(BINDIR)/external",
310 ] + select({
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000311 "//build/bazel/platforms/arch:arm64": ["-DHAVE_FAST_FMA=1"],
312 "//conditions:default": [],
313 }),
Chris Parsons990c4f42021-05-25 12:10:58 -0400314 srcs_c = ["math/cosf.c"],
Jingwen Chen4ecc67d2021-04-27 09:47:02 +0000315)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200316 })
317}
318
319func TestCcLibrarySharedStaticProps(t *testing.T) {
320 runCcLibraryTestCase(t, bp2buildTestCase{
321 description: "cc_library shared/static props",
322 moduleTypeUnderTest: "cc_library",
323 moduleTypeUnderTestFactory: cc.LibraryFactory,
324 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
325 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
326 dir: "foo/bar",
327 filesystem: map[string]string{
328 "foo/bar/both.cpp": "",
329 "foo/bar/sharedonly.cpp": "",
330 "foo/bar/staticonly.cpp": "",
331 "foo/bar/Android.bp": `
Jingwen Chen53681ef2021-04-29 08:15:13 +0000332cc_library {
333 name: "a",
Chris Parsons08648312021-05-06 16:23:19 -0400334 srcs: ["both.cpp"],
335 cflags: ["bothflag"],
336 shared_libs: ["shared_dep_for_both"],
337 static_libs: ["static_dep_for_both"],
338 whole_static_libs: ["whole_static_lib_for_both"],
339 static: {
340 srcs: ["staticonly.cpp"],
341 cflags: ["staticflag"],
342 shared_libs: ["shared_dep_for_static"],
343 static_libs: ["static_dep_for_static"],
344 whole_static_libs: ["whole_static_lib_for_static"],
345 },
346 shared: {
347 srcs: ["sharedonly.cpp"],
348 cflags: ["sharedflag"],
349 shared_libs: ["shared_dep_for_shared"],
350 static_libs: ["static_dep_for_shared"],
351 whole_static_libs: ["whole_static_lib_for_shared"],
352 },
Jingwen Chen53681ef2021-04-29 08:15:13 +0000353 bazel_module: { bp2build_available: true },
354}
355
Chris Parsons08648312021-05-06 16:23:19 -0400356cc_library_static { name: "static_dep_for_shared" }
357
358cc_library_static { name: "static_dep_for_static" }
359
360cc_library_static { name: "static_dep_for_both" }
361
362cc_library_static { name: "whole_static_lib_for_shared" }
363
364cc_library_static { name: "whole_static_lib_for_static" }
365
366cc_library_static { name: "whole_static_lib_for_both" }
367
368cc_library { name: "shared_dep_for_shared" }
369
370cc_library { name: "shared_dep_for_static" }
371
372cc_library { name: "shared_dep_for_both" }
Jingwen Chen53681ef2021-04-29 08:15:13 +0000373`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200374 },
375 blueprint: soongCcLibraryPreamble,
376 expectedBazelTargets: []string{`cc_library(
Jingwen Chen53681ef2021-04-29 08:15:13 +0000377 name = "a",
Chris Parsons08648312021-05-06 16:23:19 -0400378 copts = [
379 "bothflag",
380 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400381 "-I$(BINDIR)/foo/bar",
Chris Parsons08648312021-05-06 16:23:19 -0400382 ],
Chris Parsons08648312021-05-06 16:23:19 -0400383 dynamic_deps = [":shared_dep_for_both"],
384 dynamic_deps_for_shared = [":shared_dep_for_shared"],
385 dynamic_deps_for_static = [":shared_dep_for_static"],
Chris Parsonsd6358772021-05-18 18:35:24 -0400386 implementation_deps = [":static_dep_for_both"],
Chris Parsons08648312021-05-06 16:23:19 -0400387 shared_copts = ["sharedflag"],
388 shared_srcs = ["sharedonly.cpp"],
389 srcs = ["both.cpp"],
390 static_copts = ["staticflag"],
391 static_deps_for_shared = [":static_dep_for_shared"],
392 static_deps_for_static = [":static_dep_for_static"],
393 static_srcs = ["staticonly.cpp"],
394 whole_archive_deps = [":whole_static_lib_for_both"],
395 whole_archive_deps_for_shared = [":whole_static_lib_for_shared"],
396 whole_archive_deps_for_static = [":whole_static_lib_for_static"],
Jingwen Chen53681ef2021-04-29 08:15:13 +0000397)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200398 })
399}
400
401func TestCcLibraryNonConfiguredVersionScript(t *testing.T) {
402 runCcLibraryTestCase(t, bp2buildTestCase{
403 description: "cc_library non-configured version script",
404 moduleTypeUnderTest: "cc_library",
405 moduleTypeUnderTestFactory: cc.LibraryFactory,
406 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
407 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
408 dir: "foo/bar",
409 filesystem: map[string]string{
410 "foo/bar/Android.bp": `
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200411cc_library {
412 name: "a",
413 srcs: ["a.cpp"],
414 version_script: "v.map",
415 bazel_module: { bp2build_available: true },
416}
417`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200418 },
419 blueprint: soongCcLibraryPreamble,
420 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200421 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400422 copts = [
423 "-Ifoo/bar",
424 "-I$(BINDIR)/foo/bar",
425 ],
Lukacs T. Berki1353e592021-04-30 15:35:09 +0200426 srcs = ["a.cpp"],
427 version_script = "v.map",
428)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200429 })
430}
431
432func TestCcLibraryConfiguredVersionScript(t *testing.T) {
433 runCcLibraryTestCase(t, bp2buildTestCase{
434 description: "cc_library configured version script",
435 moduleTypeUnderTest: "cc_library",
436 moduleTypeUnderTestFactory: cc.LibraryFactory,
437 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
438 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
439 dir: "foo/bar",
440 filesystem: map[string]string{
441 "foo/bar/Android.bp": `
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200442 cc_library {
443 name: "a",
444 srcs: ["a.cpp"],
445 arch: {
446 arm: {
447 version_script: "arm.map",
448 },
449 arm64: {
450 version_script: "arm64.map",
451 },
452 },
453
454 bazel_module: { bp2build_available: true },
455 }
456 `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200457 },
458 blueprint: soongCcLibraryPreamble,
459 expectedBazelTargets: []string{`cc_library(
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200460 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400461 copts = [
462 "-Ifoo/bar",
463 "-I$(BINDIR)/foo/bar",
464 ],
Lukacs T. Berki56bb0832021-05-12 12:36:45 +0200465 srcs = ["a.cpp"],
466 version_script = select({
467 "//build/bazel/platforms/arch:arm": "arm.map",
468 "//build/bazel/platforms/arch:arm64": "arm64.map",
469 "//conditions:default": None,
470 }),
471)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200472 })
473}
474
475func TestCcLibrarySharedLibs(t *testing.T) {
476 runCcLibraryTestCase(t, bp2buildTestCase{
477 description: "cc_library shared_libs",
478 moduleTypeUnderTest: "cc_library",
479 moduleTypeUnderTestFactory: cc.LibraryFactory,
480 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
481 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
482 dir: "foo/bar",
483 filesystem: map[string]string{
484 "foo/bar/Android.bp": `
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400485cc_library {
486 name: "mylib",
487 bazel_module: { bp2build_available: true },
488}
489
490cc_library {
491 name: "a",
492 shared_libs: ["mylib",],
493 bazel_module: { bp2build_available: true },
494}
495`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200496 },
497 blueprint: soongCcLibraryPreamble,
498 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400499 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400500 copts = [
501 "-Ifoo/bar",
502 "-I$(BINDIR)/foo/bar",
503 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400504 dynamic_deps = [":mylib"],
505)`, `cc_library(
506 name = "mylib",
Chris Parsons484e50a2021-05-13 15:13:04 -0400507 copts = [
508 "-Ifoo/bar",
509 "-I$(BINDIR)/foo/bar",
510 ],
Rupert Shuttleworthc50fa8d2021-05-06 02:40:33 -0400511)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200512 })
513}
514
515func TestCcLibraryPackRelocations(t *testing.T) {
516 runCcLibraryTestCase(t, bp2buildTestCase{
517 description: "cc_library pack_relocations test",
518 moduleTypeUnderTest: "cc_library",
519 moduleTypeUnderTestFactory: cc.LibraryFactory,
520 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
521 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
522 dir: "foo/bar",
523 filesystem: map[string]string{
524 "foo/bar/Android.bp": `
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400525cc_library {
526 name: "a",
527 srcs: ["a.cpp"],
528 pack_relocations: false,
529 bazel_module: { bp2build_available: true },
530}
531
532cc_library {
533 name: "b",
534 srcs: ["b.cpp"],
535 arch: {
536 x86_64: {
537 pack_relocations: false,
538 },
539 },
540 bazel_module: { bp2build_available: true },
541}
542
543cc_library {
544 name: "c",
545 srcs: ["c.cpp"],
546 target: {
547 darwin: {
548 pack_relocations: false,
549 },
550 },
551 bazel_module: { bp2build_available: true },
552}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200553 },
554 blueprint: soongCcLibraryPreamble,
555 expectedBazelTargets: []string{`cc_library(
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400556 name = "a",
Chris Parsons484e50a2021-05-13 15:13:04 -0400557 copts = [
558 "-Ifoo/bar",
559 "-I$(BINDIR)/foo/bar",
560 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400561 linkopts = ["-Wl,--pack-dyn-relocs=none"],
562 srcs = ["a.cpp"],
563)`, `cc_library(
564 name = "b",
Chris Parsons484e50a2021-05-13 15:13:04 -0400565 copts = [
566 "-Ifoo/bar",
567 "-I$(BINDIR)/foo/bar",
568 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400569 linkopts = select({
570 "//build/bazel/platforms/arch:x86_64": ["-Wl,--pack-dyn-relocs=none"],
571 "//conditions:default": [],
572 }),
573 srcs = ["b.cpp"],
574)`, `cc_library(
575 name = "c",
Chris Parsons484e50a2021-05-13 15:13:04 -0400576 copts = [
577 "-Ifoo/bar",
578 "-I$(BINDIR)/foo/bar",
579 ],
Rupert Shuttleworth143be942021-05-09 23:55:51 -0400580 linkopts = select({
581 "//build/bazel/platforms/os:darwin": ["-Wl,--pack-dyn-relocs=none"],
582 "//conditions:default": [],
583 }),
584 srcs = ["c.cpp"],
585)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200586 })
587}
588
589func TestCcLibrarySpacesInCopts(t *testing.T) {
590 runCcLibraryTestCase(t, bp2buildTestCase{
591 description: "cc_library spaces in copts",
592 moduleTypeUnderTest: "cc_library",
593 moduleTypeUnderTestFactory: cc.LibraryFactory,
594 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
595 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
596 dir: "foo/bar",
597 filesystem: map[string]string{
598 "foo/bar/Android.bp": `
Jingwen Chen3950cd62021-05-12 04:33:00 +0000599cc_library {
600 name: "a",
601 cflags: ["-include header.h",],
602 bazel_module: { bp2build_available: true },
603}
604`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200605 },
606 blueprint: soongCcLibraryPreamble,
607 expectedBazelTargets: []string{`cc_library(
Jingwen Chen3950cd62021-05-12 04:33:00 +0000608 name = "a",
609 copts = [
610 "-include",
611 "header.h",
612 "-Ifoo/bar",
Chris Parsons484e50a2021-05-13 15:13:04 -0400613 "-I$(BINDIR)/foo/bar",
Jingwen Chen3950cd62021-05-12 04:33:00 +0000614 ],
615)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200616 })
617}
618
619func TestCcLibraryCppFlagsGoesIntoCopts(t *testing.T) {
620 runCcLibraryTestCase(t, bp2buildTestCase{
Chris Parsons990c4f42021-05-25 12:10:58 -0400621 description: "cc_library cppflags usage",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200622 moduleTypeUnderTest: "cc_library",
623 moduleTypeUnderTestFactory: cc.LibraryFactory,
624 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryBp2Build,
625 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
626 dir: "foo/bar",
627 filesystem: map[string]string{
628 "foo/bar/Android.bp": `cc_library {
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000629 name: "a",
630 srcs: ["a.cpp"],
631 cflags: [
632 "-Wall",
633 ],
634 cppflags: [
635 "-fsigned-char",
636 "-pedantic",
637 ],
638 arch: {
639 arm64: {
640 cppflags: ["-DARM64=1"],
641 },
642 },
643 target: {
644 android: {
645 cppflags: ["-DANDROID=1"],
646 },
647 },
648 bazel_module: { bp2build_available: true },
649}
650`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200651 },
652 blueprint: soongCcLibraryPreamble,
653 expectedBazelTargets: []string{`cc_library(
Jingwen Chen75be1ca2021-05-12 05:04:58 +0000654 name = "a",
655 copts = [
656 "-Wall",
Colin Cross52aa4e12021-05-25 15:20:39 +0000657 "-Ifoo/bar",
658 "-I$(BINDIR)/foo/bar",
Chris Parsons990c4f42021-05-25 12:10:58 -0400659 ],
660 cppflags = [
661 "-fsigned-char",
662 "-pedantic",
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}