blob: 833ceba09691a6a8fe6cbc5f18b279c94946d565 [file] [log] [blame]
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001// 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"
Chris Parsons484e50a2021-05-13 15:13:04 -040020 "android/soong/genrule"
21
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000022 "testing"
23)
24
25const (
26 // See cc/testing.go for more context
27 soongCcLibraryStaticPreamble = `
28cc_defaults {
29 name: "linux_bionic_supported",
30}
31
32toolchain_library {
33 name: "libclang_rt.builtins-x86_64-android",
34 defaults: ["linux_bionic_supported"],
35 vendor_available: true,
36 vendor_ramdisk_available: true,
37 product_available: true,
38 recovery_available: true,
39 native_bridge_supported: true,
40 src: "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000041}`
42)
43
44func TestCcLibraryStaticLoadStatement(t *testing.T) {
45 testCases := []struct {
46 bazelTargets BazelTargets
47 expectedLoadStatements string
48 }{
49 {
50 bazelTargets: BazelTargets{
51 BazelTarget{
52 name: "cc_library_static_target",
53 ruleClass: "cc_library_static",
54 // NOTE: No bzlLoadLocation for native rules
55 },
56 },
57 expectedLoadStatements: ``,
58 },
59 }
60
61 for _, testCase := range testCases {
62 actual := testCase.bazelTargets.LoadStatements()
63 expected := testCase.expectedLoadStatements
64 if actual != expected {
65 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
66 }
67 }
68
69}
70
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020071func registerCcLibraryStaticModuleTypes(ctx android.RegistrationContext) {
72 cc.RegisterCCBuildComponents(ctx)
73 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
74 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
75 ctx.RegisterModuleType("genrule", genrule.GenRuleFactory)
76}
77
78func runCcLibraryStaticTestCase(t *testing.T, tc bp2buildTestCase) {
Liz Kammere4982e82021-05-25 10:39:35 -040079 t.Helper()
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +020080 runBp2BuildTestCase(t, registerCcLibraryStaticModuleTypes, tc)
81}
82
83func TestCcLibraryStaticSimple(t *testing.T) {
84 runCcLibraryStaticTestCase(t, bp2buildTestCase{
85 description: "cc_library_static test",
86 moduleTypeUnderTest: "cc_library_static",
87 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
88 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
89 filesystem: map[string]string{
90 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
91 "include_dir_1/include_dir_1_a.h": "",
92 "include_dir_1/include_dir_1_b.h": "",
93 "include_dir_2/include_dir_2_a.h": "",
94 "include_dir_2/include_dir_2_b.h": "",
95 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
96 "local_include_dir_1/local_include_dir_1_a.h": "",
97 "local_include_dir_1/local_include_dir_1_b.h": "",
98 "local_include_dir_2/local_include_dir_2_a.h": "",
99 "local_include_dir_2/local_include_dir_2_b.h": "",
100 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
101 "export_include_dir_1/export_include_dir_1_a.h": "",
102 "export_include_dir_1/export_include_dir_1_b.h": "",
103 "export_include_dir_2/export_include_dir_2_a.h": "",
104 "export_include_dir_2/export_include_dir_2_b.h": "",
105 // NOTE: Soong implicitly includes headers in the current directory
106 "implicit_include_1.h": "",
107 "implicit_include_2.h": "",
108 },
109 blueprint: soongCcLibraryStaticPreamble + `
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000110cc_library_headers {
111 name: "header_lib_1",
112 export_include_dirs: ["header_lib_1"],
113}
114
115cc_library_headers {
116 name: "header_lib_2",
117 export_include_dirs: ["header_lib_2"],
118}
119
120cc_library_static {
121 name: "static_lib_1",
122 srcs: ["static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000123}
124
125cc_library_static {
126 name: "static_lib_2",
127 srcs: ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000128}
129
130cc_library_static {
131 name: "whole_static_lib_1",
132 srcs: ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000133}
134
135cc_library_static {
136 name: "whole_static_lib_2",
137 srcs: ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000138}
139
140cc_library_static {
141 name: "foo_static",
142 srcs: [
143 "foo_static1.cc",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000144 "foo_static2.cc",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000145 ],
146 cflags: [
147 "-Dflag1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000148 "-Dflag2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000149 ],
150 static_libs: [
151 "static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000152 "static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000153 ],
154 whole_static_libs: [
155 "whole_static_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000156 "whole_static_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000157 ],
158 include_dirs: [
Jingwen Chened9c17d2021-04-13 07:14:55 +0000159 "include_dir_1",
160 "include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000161 ],
162 local_include_dirs: [
163 "local_include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000164 "local_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000165 ],
166 export_include_dirs: [
Chris Parsonsd6358772021-05-18 18:35:24 -0400167 "export_include_dir_1",
168 "export_include_dir_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000169 ],
170 header_libs: [
171 "header_lib_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000172 "header_lib_2"
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000173 ],
174
175 // TODO: Also support export_header_lib_headers
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000176}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200177 expectedBazelTargets: []string{`cc_library_static(
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000178 name = "foo_static",
179 copts = [
180 "-Dflag1",
181 "-Dflag2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000182 "-Iinclude_dir_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400183 "-I$(BINDIR)/include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000184 "-Iinclude_dir_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400185 "-I$(BINDIR)/include_dir_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000186 "-Ilocal_include_dir_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400187 "-I$(BINDIR)/local_include_dir_1",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000188 "-Ilocal_include_dir_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400189 "-I$(BINDIR)/local_include_dir_2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000190 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400191 "-I$(BINDIR)/.",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000192 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400193 implementation_deps = [
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000194 ":header_lib_1",
195 ":header_lib_2",
196 ":static_lib_1",
197 ":static_lib_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000198 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000199 includes = [
200 "export_include_dir_1",
201 "export_include_dir_2",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000202 ],
203 linkstatic = True,
204 srcs = [
205 "foo_static1.cc",
206 "foo_static2.cc",
207 ],
Chris Parsons08648312021-05-06 16:23:19 -0400208 whole_archive_deps = [
209 ":whole_static_lib_1",
210 ":whole_static_lib_2",
211 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000212)`, `cc_library_static(
213 name = "static_lib_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400214 copts = [
215 "-I.",
216 "-I$(BINDIR)/.",
217 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000218 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000219 srcs = ["static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000220)`, `cc_library_static(
221 name = "static_lib_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400222 copts = [
223 "-I.",
224 "-I$(BINDIR)/.",
225 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000226 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000227 srcs = ["static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000228)`, `cc_library_static(
229 name = "whole_static_lib_1",
Chris Parsons484e50a2021-05-13 15:13:04 -0400230 copts = [
231 "-I.",
232 "-I$(BINDIR)/.",
233 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000234 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000235 srcs = ["whole_static_lib_1.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000236)`, `cc_library_static(
237 name = "whole_static_lib_2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400238 copts = [
239 "-I.",
240 "-I$(BINDIR)/.",
241 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000242 linkstatic = True,
Jingwen Chen882bcc12021-04-27 05:54:20 +0000243 srcs = ["whole_static_lib_2.cc"],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000244)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200245 })
246}
247
248func TestCcLibraryStaticSubpackage(t *testing.T) {
249 runCcLibraryStaticTestCase(t, bp2buildTestCase{
250 description: "cc_library_static subpackage test",
251 moduleTypeUnderTest: "cc_library_static",
252 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
253 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
254 filesystem: map[string]string{
255 // subpackage with subdirectory
256 "subpackage/Android.bp": "",
257 "subpackage/subpackage_header.h": "",
258 "subpackage/subdirectory/subdirectory_header.h": "",
259 // subsubpackage with subdirectory
260 "subpackage/subsubpackage/Android.bp": "",
261 "subpackage/subsubpackage/subsubpackage_header.h": "",
262 "subpackage/subsubpackage/subdirectory/subdirectory_header.h": "",
263 // subsubsubpackage with subdirectory
264 "subpackage/subsubpackage/subsubsubpackage/Android.bp": "",
265 "subpackage/subsubpackage/subsubsubpackage/subsubsubpackage_header.h": "",
266 "subpackage/subsubpackage/subsubsubpackage/subdirectory/subdirectory_header.h": "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000267 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200268 blueprint: soongCcLibraryStaticPreamble + `
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400269cc_library_static {
270 name: "foo_static",
271 srcs: [
272 ],
273 include_dirs: [
274 "subpackage",
275 ],
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400276}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200277 expectedBazelTargets: []string{`cc_library_static(
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400278 name = "foo_static",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000279 copts = [
280 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400281 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000282 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400283 "-I$(BINDIR)/.",
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400284 ],
285 linkstatic = True,
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400286)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200287 })
288}
289
290func TestCcLibraryStaticExportIncludeDir(t *testing.T) {
291 runCcLibraryStaticTestCase(t, bp2buildTestCase{
292 description: "cc_library_static export include dir",
293 moduleTypeUnderTest: "cc_library_static",
294 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
295 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
296 filesystem: map[string]string{
297 // subpackage with subdirectory
298 "subpackage/Android.bp": "",
299 "subpackage/subpackage_header.h": "",
300 "subpackage/subdirectory/subdirectory_header.h": "",
Rupert Shuttleworthc143cc52021-04-13 13:08:04 -0400301 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200302 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000303cc_library_static {
304 name: "foo_static",
305 export_include_dirs: ["subpackage"],
306}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200307 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000308 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400309 copts = [
310 "-I.",
311 "-I$(BINDIR)/.",
312 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000313 includes = ["subpackage"],
314 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000315)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200316 })
317}
318
319func TestCcLibraryStaticExportSystemIncludeDir(t *testing.T) {
320 runCcLibraryStaticTestCase(t, bp2buildTestCase{
321 description: "cc_library_static export system include dir",
322 moduleTypeUnderTest: "cc_library_static",
323 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
324 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
325 filesystem: map[string]string{
326 // subpackage with subdirectory
327 "subpackage/Android.bp": "",
328 "subpackage/subpackage_header.h": "",
329 "subpackage/subdirectory/subdirectory_header.h": "",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000330 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200331 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000332cc_library_static {
333 name: "foo_static",
334 export_system_include_dirs: ["subpackage"],
335}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200336 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000337 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400338 copts = [
339 "-I.",
340 "-I$(BINDIR)/.",
341 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000342 includes = ["subpackage"],
343 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000344)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200345 })
346}
347
348func TestCcLibraryStaticManyIncludeDirs(t *testing.T) {
349 runCcLibraryStaticTestCase(t, bp2buildTestCase{
350 description: "cc_library_static include_dirs, local_include_dirs, export_include_dirs (b/183742505)",
351 moduleTypeUnderTest: "cc_library_static",
352 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
353 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
354 dir: "subpackage",
355 filesystem: map[string]string{
356 // subpackage with subdirectory
357 "subpackage/Android.bp": `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000358cc_library_static {
359 name: "foo_static",
360 // include_dirs are workspace/root relative
361 include_dirs: [
362 "subpackage/subsubpackage",
363 "subpackage2",
364 "subpackage3/subsubpackage"
365 ],
366 local_include_dirs: ["subsubpackage2"], // module dir relative
367 export_include_dirs: ["./exported_subsubpackage"], // module dir relative
368 include_build_directory: true,
369 bazel_module: { bp2build_available: true },
370}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200371 "subpackage/subsubpackage/header.h": "",
372 "subpackage/subsubpackage2/header.h": "",
373 "subpackage/exported_subsubpackage/header.h": "",
374 "subpackage2/header.h": "",
375 "subpackage3/subsubpackage/header.h": "",
376 },
377 blueprint: soongCcLibraryStaticPreamble,
378 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000379 name = "foo_static",
380 copts = [
381 "-Isubpackage/subsubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400382 "-I$(BINDIR)/subpackage/subsubpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000383 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400384 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000385 "-Isubpackage3/subsubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400386 "-I$(BINDIR)/subpackage3/subsubpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000387 "-Isubpackage/subsubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400388 "-I$(BINDIR)/subpackage/subsubpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000389 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400390 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000391 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000392 includes = ["./exported_subsubpackage"],
393 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000394)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200395 })
396}
397
398func TestCcLibraryStaticIncludeBuildDirectoryDisabled(t *testing.T) {
399 runCcLibraryStaticTestCase(t, bp2buildTestCase{
400 description: "cc_library_static include_build_directory disabled",
401 moduleTypeUnderTest: "cc_library_static",
402 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
403 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
404 filesystem: map[string]string{
405 // subpackage with subdirectory
406 "subpackage/Android.bp": "",
407 "subpackage/subpackage_header.h": "",
408 "subpackage/subdirectory/subdirectory_header.h": "",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000409 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200410 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000411cc_library_static {
412 name: "foo_static",
413 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
414 local_include_dirs: ["subpackage2"],
415 include_build_directory: false,
416}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200417 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000418 name = "foo_static",
419 copts = [
420 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400421 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000422 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400423 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000424 ],
425 linkstatic = True,
426)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200427 })
428}
429
430func TestCcLibraryStaticIncludeBuildDirectoryEnabled(t *testing.T) {
431 runCcLibraryStaticTestCase(t, bp2buildTestCase{
432 description: "cc_library_static include_build_directory enabled",
433 moduleTypeUnderTest: "cc_library_static",
434 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
435 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
436 filesystem: map[string]string{
437 // subpackage with subdirectory
438 "subpackage/Android.bp": "",
439 "subpackage/subpackage_header.h": "",
440 "subpackage2/Android.bp": "",
441 "subpackage2/subpackage2_header.h": "",
442 "subpackage/subdirectory/subdirectory_header.h": "",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000443 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200444 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000445cc_library_static {
446 name: "foo_static",
447 include_dirs: ["subpackage"], // still used, but local_include_dirs is recommended
448 local_include_dirs: ["subpackage2"],
449 include_build_directory: true,
450}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200451 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000452 name = "foo_static",
453 copts = [
454 "-Isubpackage",
Chris Parsons484e50a2021-05-13 15:13:04 -0400455 "-I$(BINDIR)/subpackage",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000456 "-Isubpackage2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400457 "-I$(BINDIR)/subpackage2",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000458 "-I.",
Chris Parsons484e50a2021-05-13 15:13:04 -0400459 "-I$(BINDIR)/.",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000460 ],
461 linkstatic = True,
Jingwen Chened9c17d2021-04-13 07:14:55 +0000462)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200463 })
464}
465
466func TestCcLibraryStaticArchSpecificStaticLib(t *testing.T) {
467 runCcLibraryStaticTestCase(t, bp2buildTestCase{
468 description: "cc_library_static arch-specific static_libs",
469 moduleTypeUnderTest: "cc_library_static",
470 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
471 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
472 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
473 filesystem: map[string]string{},
474 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000475cc_library_static { name: "static_dep" }
476cc_library_static { name: "static_dep2" }
477cc_library_static {
478 name: "foo_static",
479 arch: { arm64: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
480}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200481 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000482 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400483 copts = [
484 "-I.",
485 "-I$(BINDIR)/.",
486 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400487 implementation_deps = select({
Chris Parsons08648312021-05-06 16:23:19 -0400488 "//build/bazel/platforms/arch:arm64": [":static_dep"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000489 "//conditions:default": [],
490 }),
491 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400492 whole_archive_deps = select({
493 "//build/bazel/platforms/arch:arm64": [":static_dep2"],
494 "//conditions:default": [],
495 }),
Jingwen Chened9c17d2021-04-13 07:14:55 +0000496)`, `cc_library_static(
497 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400498 copts = [
499 "-I.",
500 "-I$(BINDIR)/.",
501 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000502 linkstatic = True,
503)`, `cc_library_static(
504 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400505 copts = [
506 "-I.",
507 "-I$(BINDIR)/.",
508 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000509 linkstatic = True,
510)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200511 })
512}
513
514func TestCcLibraryStaticOsSpecificStaticLib(t *testing.T) {
515 runCcLibraryStaticTestCase(t, bp2buildTestCase{
516 description: "cc_library_static os-specific static_libs",
517 moduleTypeUnderTest: "cc_library_static",
518 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
519 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
520 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
521 filesystem: map[string]string{},
522 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000523cc_library_static { name: "static_dep" }
524cc_library_static { name: "static_dep2" }
525cc_library_static {
526 name: "foo_static",
527 target: { android: { static_libs: ["static_dep"], whole_static_libs: ["static_dep2"] } },
528}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200529 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000530 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400531 copts = [
532 "-I.",
533 "-I$(BINDIR)/.",
534 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400535 implementation_deps = select({
Chris Parsons08648312021-05-06 16:23:19 -0400536 "//build/bazel/platforms/os:android": [":static_dep"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000537 "//conditions:default": [],
538 }),
539 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400540 whole_archive_deps = select({
541 "//build/bazel/platforms/os:android": [":static_dep2"],
542 "//conditions:default": [],
543 }),
Jingwen Chened9c17d2021-04-13 07:14:55 +0000544)`, `cc_library_static(
545 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400546 copts = [
547 "-I.",
548 "-I$(BINDIR)/.",
549 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000550 linkstatic = True,
551)`, `cc_library_static(
552 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400553 copts = [
554 "-I.",
555 "-I$(BINDIR)/.",
556 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000557 linkstatic = True,
558)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200559 })
560}
561
562func TestCcLibraryStaticBaseArchOsSpecificStaticLib(t *testing.T) {
563 runCcLibraryStaticTestCase(t, bp2buildTestCase{
564 description: "cc_library_static base, arch and os-specific static_libs",
565 moduleTypeUnderTest: "cc_library_static",
566 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
567 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
568 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
569 filesystem: map[string]string{},
570 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chened9c17d2021-04-13 07:14:55 +0000571cc_library_static { name: "static_dep" }
572cc_library_static { name: "static_dep2" }
573cc_library_static { name: "static_dep3" }
574cc_library_static { name: "static_dep4" }
575cc_library_static {
576 name: "foo_static",
577 static_libs: ["static_dep"],
578 whole_static_libs: ["static_dep2"],
579 target: { android: { static_libs: ["static_dep3"] } },
580 arch: { arm64: { static_libs: ["static_dep4"] } },
581}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200582 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chened9c17d2021-04-13 07:14:55 +0000583 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400584 copts = [
585 "-I.",
586 "-I$(BINDIR)/.",
587 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400588 implementation_deps = [":static_dep"] + select({
Jingwen Chened9c17d2021-04-13 07:14:55 +0000589 "//build/bazel/platforms/arch:arm64": [":static_dep4"],
590 "//conditions:default": [],
591 }) + select({
592 "//build/bazel/platforms/os:android": [":static_dep3"],
593 "//conditions:default": [],
594 }),
595 linkstatic = True,
Chris Parsons08648312021-05-06 16:23:19 -0400596 whole_archive_deps = [":static_dep2"],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000597)`, `cc_library_static(
598 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400599 copts = [
600 "-I.",
601 "-I$(BINDIR)/.",
602 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000603 linkstatic = True,
604)`, `cc_library_static(
605 name = "static_dep2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400606 copts = [
607 "-I.",
608 "-I$(BINDIR)/.",
609 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000610 linkstatic = True,
611)`, `cc_library_static(
612 name = "static_dep3",
Chris Parsons484e50a2021-05-13 15:13:04 -0400613 copts = [
614 "-I.",
615 "-I$(BINDIR)/.",
616 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000617 linkstatic = True,
618)`, `cc_library_static(
619 name = "static_dep4",
Chris Parsons484e50a2021-05-13 15:13:04 -0400620 copts = [
621 "-I.",
622 "-I$(BINDIR)/.",
623 ],
Jingwen Chened9c17d2021-04-13 07:14:55 +0000624 linkstatic = True,
625)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200626 })
627}
628
629func TestCcLibraryStaticSimpleExcludeSrcs(t *testing.T) {
630 runCcLibraryStaticTestCase(t, bp2buildTestCase{
631 description: "cc_library_static simple exclude_srcs",
632 moduleTypeUnderTest: "cc_library_static",
633 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
634 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
635 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
636 filesystem: map[string]string{
637 "common.c": "",
638 "foo-a.c": "",
639 "foo-excluded.c": "",
Jingwen Chened9c17d2021-04-13 07:14:55 +0000640 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200641 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chene32e9e02021-04-23 09:17:24 +0000642cc_library_static {
643 name: "foo_static",
644 srcs: ["common.c", "foo-*.c"],
645 exclude_srcs: ["foo-excluded.c"],
646}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200647 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chene32e9e02021-04-23 09:17:24 +0000648 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400649 copts = [
650 "-I.",
651 "-I$(BINDIR)/.",
652 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000653 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400654 srcs_c = [
Jingwen Chene32e9e02021-04-23 09:17:24 +0000655 "common.c",
656 "foo-a.c",
657 ],
658)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200659 })
660}
661
662func TestCcLibraryStaticOneArchSrcs(t *testing.T) {
663 runCcLibraryStaticTestCase(t, bp2buildTestCase{
664 description: "cc_library_static one arch specific srcs",
665 moduleTypeUnderTest: "cc_library_static",
666 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
667 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
668 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
669 filesystem: map[string]string{
670 "common.c": "",
671 "foo-arm.c": "",
Jingwen Chene32e9e02021-04-23 09:17:24 +0000672 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200673 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chene32e9e02021-04-23 09:17:24 +0000674cc_library_static {
675 name: "foo_static",
676 srcs: ["common.c"],
677 arch: { arm: { srcs: ["foo-arm.c"] } }
678}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200679 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chene32e9e02021-04-23 09:17:24 +0000680 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400681 copts = [
682 "-I.",
683 "-I$(BINDIR)/.",
684 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000685 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400686 srcs_c = ["common.c"] + select({
Jingwen Chene32e9e02021-04-23 09:17:24 +0000687 "//build/bazel/platforms/arch:arm": ["foo-arm.c"],
688 "//conditions:default": [],
689 }),
690)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200691 })
692}
693
694func TestCcLibraryStaticOneArchSrcsExcludeSrcs(t *testing.T) {
695 runCcLibraryStaticTestCase(t, bp2buildTestCase{
696 description: "cc_library_static one arch specific srcs and exclude_srcs",
697 moduleTypeUnderTest: "cc_library_static",
698 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
699 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
700 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
701 filesystem: map[string]string{
702 "common.c": "",
703 "for-arm.c": "",
704 "not-for-arm.c": "",
705 "not-for-anything.c": "",
Jingwen Chene32e9e02021-04-23 09:17:24 +0000706 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200707 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chene32e9e02021-04-23 09:17:24 +0000708cc_library_static {
709 name: "foo_static",
710 srcs: ["common.c", "not-for-*.c"],
711 exclude_srcs: ["not-for-anything.c"],
712 arch: {
713 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
714 },
715}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200716 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chene32e9e02021-04-23 09:17:24 +0000717 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400718 copts = [
719 "-I.",
720 "-I$(BINDIR)/.",
721 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000722 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400723 srcs_c = ["common.c"] + select({
Jingwen Chene32e9e02021-04-23 09:17:24 +0000724 "//build/bazel/platforms/arch:arm": ["for-arm.c"],
725 "//conditions:default": ["not-for-arm.c"],
726 }),
727)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200728 })
729}
730
731func TestCcLibraryStaticTwoArchExcludeSrcs(t *testing.T) {
732 runCcLibraryStaticTestCase(t, bp2buildTestCase{
733 description: "cc_library_static arch specific exclude_srcs for 2 architectures",
734 moduleTypeUnderTest: "cc_library_static",
735 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
736 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
737 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
738 filesystem: map[string]string{
739 "common.c": "",
740 "for-arm.c": "",
741 "for-x86.c": "",
742 "not-for-arm.c": "",
743 "not-for-x86.c": "",
Jingwen Chene32e9e02021-04-23 09:17:24 +0000744 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200745 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chene32e9e02021-04-23 09:17:24 +0000746cc_library_static {
747 name: "foo_static",
748 srcs: ["common.c", "not-for-*.c"],
749 exclude_srcs: ["not-for-everything.c"],
750 arch: {
751 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
752 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
753 },
754} `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200755 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chene32e9e02021-04-23 09:17:24 +0000756 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400757 copts = [
758 "-I.",
759 "-I$(BINDIR)/.",
760 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000761 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400762 srcs_c = ["common.c"] + select({
Jingwen Chene32e9e02021-04-23 09:17:24 +0000763 "//build/bazel/platforms/arch:arm": [
764 "for-arm.c",
765 "not-for-x86.c",
766 ],
767 "//build/bazel/platforms/arch:x86": [
768 "for-x86.c",
769 "not-for-arm.c",
770 ],
771 "//conditions:default": [
772 "not-for-arm.c",
773 "not-for-x86.c",
774 ],
775 }),
776)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200777 })
778}
779func TestCcLibraryStaticFourArchExcludeSrcs(t *testing.T) {
780 runCcLibraryStaticTestCase(t, bp2buildTestCase{
781 description: "cc_library_static arch specific exclude_srcs for 4 architectures",
782 moduleTypeUnderTest: "cc_library_static",
783 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
784 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
785 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
786 filesystem: map[string]string{
787 "common.c": "",
788 "for-arm.c": "",
789 "for-arm64.c": "",
790 "for-x86.c": "",
791 "for-x86_64.c": "",
792 "not-for-arm.c": "",
793 "not-for-arm64.c": "",
794 "not-for-x86.c": "",
795 "not-for-x86_64.c": "",
796 "not-for-everything.c": "",
Jingwen Chene32e9e02021-04-23 09:17:24 +0000797 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200798 blueprint: soongCcLibraryStaticPreamble + `
Jingwen Chene32e9e02021-04-23 09:17:24 +0000799cc_library_static {
800 name: "foo_static",
801 srcs: ["common.c", "not-for-*.c"],
802 exclude_srcs: ["not-for-everything.c"],
803 arch: {
804 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
805 arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
806 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
807 x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
808 },
809} `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200810 expectedBazelTargets: []string{`cc_library_static(
Jingwen Chene32e9e02021-04-23 09:17:24 +0000811 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400812 copts = [
813 "-I.",
814 "-I$(BINDIR)/.",
815 ],
Jingwen Chene32e9e02021-04-23 09:17:24 +0000816 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400817 srcs_c = ["common.c"] + select({
Jingwen Chene32e9e02021-04-23 09:17:24 +0000818 "//build/bazel/platforms/arch:arm": [
819 "for-arm.c",
820 "not-for-arm64.c",
821 "not-for-x86.c",
822 "not-for-x86_64.c",
823 ],
824 "//build/bazel/platforms/arch:arm64": [
825 "for-arm64.c",
826 "not-for-arm.c",
827 "not-for-x86.c",
828 "not-for-x86_64.c",
829 ],
830 "//build/bazel/platforms/arch:x86": [
831 "for-x86.c",
832 "not-for-arm.c",
833 "not-for-arm64.c",
834 "not-for-x86_64.c",
835 ],
836 "//build/bazel/platforms/arch:x86_64": [
837 "for-x86_64.c",
838 "not-for-arm.c",
839 "not-for-arm64.c",
840 "not-for-x86.c",
841 ],
842 "//conditions:default": [
843 "not-for-arm.c",
844 "not-for-arm64.c",
845 "not-for-x86.c",
846 "not-for-x86_64.c",
847 ],
848 }),
849)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200850 })
851}
852
853func TestCcLibraryStaticMultipleDepSameName(t *testing.T) {
854 runCcLibraryStaticTestCase(t, bp2buildTestCase{
855 description: "cc_library_static multiple dep same name panic",
856 moduleTypeUnderTest: "cc_library_static",
857 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
858 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
859 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
860 filesystem: map[string]string{},
861 blueprint: soongCcLibraryStaticPreamble + `
Liz Kammer2b50ce62021-04-26 15:47:28 -0400862cc_library_static { name: "static_dep" }
863cc_library_static {
864 name: "foo_static",
Chris Parsons08648312021-05-06 16:23:19 -0400865 static_libs: ["static_dep", "static_dep"],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400866}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200867 expectedBazelTargets: []string{`cc_library_static(
Liz Kammer2b50ce62021-04-26 15:47:28 -0400868 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400869 copts = [
870 "-I.",
871 "-I$(BINDIR)/.",
872 ],
Chris Parsonsd6358772021-05-18 18:35:24 -0400873 implementation_deps = [":static_dep"],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400874 linkstatic = True,
875)`, `cc_library_static(
876 name = "static_dep",
Chris Parsons484e50a2021-05-13 15:13:04 -0400877 copts = [
878 "-I.",
879 "-I$(BINDIR)/.",
880 ],
Liz Kammer2b50ce62021-04-26 15:47:28 -0400881 linkstatic = True,
882)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200883 })
884}
885
886func TestCcLibraryStaticOneMultilibSrcsExcludeSrcs(t *testing.T) {
887 runCcLibraryStaticTestCase(t, bp2buildTestCase{
888 description: "cc_library_static 1 multilib srcs and exclude_srcs",
889 moduleTypeUnderTest: "cc_library_static",
890 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
891 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
892 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
893 filesystem: map[string]string{
894 "common.c": "",
895 "for-lib32.c": "",
896 "not-for-lib32.c": "",
Liz Kammer2b50ce62021-04-26 15:47:28 -0400897 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200898 blueprint: soongCcLibraryStaticPreamble + `
Chris Parsonsc424b762021-04-29 18:06:50 -0400899cc_library_static {
900 name: "foo_static",
901 srcs: ["common.c", "not-for-*.c"],
902 multilib: {
903 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
904 },
905} `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200906 expectedBazelTargets: []string{`cc_library_static(
Chris Parsonsc424b762021-04-29 18:06:50 -0400907 name = "foo_static",
Chris Parsons484e50a2021-05-13 15:13:04 -0400908 copts = [
909 "-I.",
910 "-I$(BINDIR)/.",
911 ],
Chris Parsonsc424b762021-04-29 18:06:50 -0400912 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400913 srcs_c = ["common.c"] + select({
Chris Parsonsc424b762021-04-29 18:06:50 -0400914 "//build/bazel/platforms/arch:arm": ["for-lib32.c"],
915 "//build/bazel/platforms/arch:x86": ["for-lib32.c"],
916 "//conditions:default": ["not-for-lib32.c"],
917 }),
918)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200919 })
920}
921
922func TestCcLibraryStaticTwoMultilibSrcsExcludeSrcs(t *testing.T) {
923 runCcLibraryStaticTestCase(t, bp2buildTestCase{
924 description: "cc_library_static 2 multilib srcs and exclude_srcs",
925 moduleTypeUnderTest: "cc_library_static",
926 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
927 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
928 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
929 filesystem: map[string]string{
930 "common.c": "",
931 "for-lib32.c": "",
932 "for-lib64.c": "",
933 "not-for-lib32.c": "",
934 "not-for-lib64.c": "",
Chris Parsonsc424b762021-04-29 18:06:50 -0400935 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200936 blueprint: soongCcLibraryStaticPreamble + `
Chris Parsonsc424b762021-04-29 18:06:50 -0400937cc_library_static {
938 name: "foo_static2",
939 srcs: ["common.c", "not-for-*.c"],
940 multilib: {
941 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
942 lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
943 },
944} `,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200945 expectedBazelTargets: []string{`cc_library_static(
Chris Parsonsc424b762021-04-29 18:06:50 -0400946 name = "foo_static2",
Chris Parsons484e50a2021-05-13 15:13:04 -0400947 copts = [
948 "-I.",
949 "-I$(BINDIR)/.",
950 ],
Chris Parsonsc424b762021-04-29 18:06:50 -0400951 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -0400952 srcs_c = ["common.c"] + select({
Chris Parsonsc424b762021-04-29 18:06:50 -0400953 "//build/bazel/platforms/arch:arm": [
954 "for-lib32.c",
955 "not-for-lib64.c",
956 ],
957 "//build/bazel/platforms/arch:arm64": [
958 "for-lib64.c",
959 "not-for-lib32.c",
960 ],
961 "//build/bazel/platforms/arch:x86": [
962 "for-lib32.c",
963 "not-for-lib64.c",
964 ],
965 "//build/bazel/platforms/arch:x86_64": [
966 "for-lib64.c",
967 "not-for-lib32.c",
968 ],
969 "//conditions:default": [
970 "not-for-lib32.c",
971 "not-for-lib64.c",
972 ],
973 }),
974)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +0200975 })
976}
977
978func TestCcLibrarySTaticArchMultilibSrcsExcludeSrcs(t *testing.T) {
979 runCcLibraryStaticTestCase(t, bp2buildTestCase{
980 description: "cc_library_static arch and multilib srcs and exclude_srcs",
981 moduleTypeUnderTest: "cc_library_static",
982 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
983 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
984 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
985 filesystem: map[string]string{
986 "common.c": "",
987 "for-arm.c": "",
988 "for-arm64.c": "",
989 "for-x86.c": "",
990 "for-x86_64.c": "",
991 "for-lib32.c": "",
992 "for-lib64.c": "",
993 "not-for-arm.c": "",
994 "not-for-arm64.c": "",
995 "not-for-x86.c": "",
996 "not-for-x86_64.c": "",
997 "not-for-lib32.c": "",
998 "not-for-lib64.c": "",
999 "not-for-everything.c": "",
Chris Parsonsc424b762021-04-29 18:06:50 -04001000 },
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001001 blueprint: soongCcLibraryStaticPreamble + `
Chris Parsonsc424b762021-04-29 18:06:50 -04001002cc_library_static {
1003 name: "foo_static3",
1004 srcs: ["common.c", "not-for-*.c"],
1005 exclude_srcs: ["not-for-everything.c"],
1006 arch: {
1007 arm: { srcs: ["for-arm.c"], exclude_srcs: ["not-for-arm.c"] },
1008 arm64: { srcs: ["for-arm64.c"], exclude_srcs: ["not-for-arm64.c"] },
1009 x86: { srcs: ["for-x86.c"], exclude_srcs: ["not-for-x86.c"] },
1010 x86_64: { srcs: ["for-x86_64.c"], exclude_srcs: ["not-for-x86_64.c"] },
1011 },
1012 multilib: {
1013 lib32: { srcs: ["for-lib32.c"], exclude_srcs: ["not-for-lib32.c"] },
1014 lib64: { srcs: ["for-lib64.c"], exclude_srcs: ["not-for-lib64.c"] },
1015 },
1016}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001017 expectedBazelTargets: []string{`cc_library_static(
Chris Parsonsc424b762021-04-29 18:06:50 -04001018 name = "foo_static3",
Chris Parsons484e50a2021-05-13 15:13:04 -04001019 copts = [
1020 "-I.",
1021 "-I$(BINDIR)/.",
1022 ],
Chris Parsonsc424b762021-04-29 18:06:50 -04001023 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -04001024 srcs_c = ["common.c"] + select({
Chris Parsonsc424b762021-04-29 18:06:50 -04001025 "//build/bazel/platforms/arch:arm": [
1026 "for-arm.c",
1027 "for-lib32.c",
1028 "not-for-arm64.c",
1029 "not-for-lib64.c",
1030 "not-for-x86.c",
1031 "not-for-x86_64.c",
1032 ],
1033 "//build/bazel/platforms/arch:arm64": [
1034 "for-arm64.c",
1035 "for-lib64.c",
1036 "not-for-arm.c",
1037 "not-for-lib32.c",
1038 "not-for-x86.c",
1039 "not-for-x86_64.c",
1040 ],
1041 "//build/bazel/platforms/arch:x86": [
1042 "for-lib32.c",
1043 "for-x86.c",
1044 "not-for-arm.c",
1045 "not-for-arm64.c",
1046 "not-for-lib64.c",
1047 "not-for-x86_64.c",
1048 ],
1049 "//build/bazel/platforms/arch:x86_64": [
1050 "for-lib64.c",
1051 "for-x86_64.c",
1052 "not-for-arm.c",
1053 "not-for-arm64.c",
1054 "not-for-lib32.c",
1055 "not-for-x86.c",
1056 ],
1057 "//conditions:default": [
1058 "not-for-arm.c",
1059 "not-for-arm64.c",
1060 "not-for-lib32.c",
1061 "not-for-lib64.c",
1062 "not-for-x86.c",
1063 "not-for-x86_64.c",
1064 ],
1065 }),
1066)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001067 })
1068}
1069
1070func TestCcLibraryStaticArchSrcsExcludeSrcsGeneratedFiles(t *testing.T) {
1071 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1072 description: "cc_library_static arch srcs/exclude_srcs with generated files",
1073 moduleTypeUnderTest: "cc_library_static",
1074 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1075 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1076 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1077 filesystem: map[string]string{
Chris Parsons990c4f42021-05-25 12:10:58 -04001078 "common.cpp": "",
1079 "for-x86.cpp": "",
1080 "not-for-x86.cpp": "",
1081 "not-for-everything.cpp": "",
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001082 "dep/Android.bp": `
Chris Parsons484e50a2021-05-13 15:13:04 -04001083genrule {
1084 name: "generated_src_other_pkg",
1085 out: ["generated_src_other_pkg.cpp"],
1086 cmd: "nothing to see here",
1087}
1088
1089genrule {
1090 name: "generated_hdr_other_pkg",
1091 out: ["generated_hdr_other_pkg.cpp"],
1092 cmd: "nothing to see here",
1093}
1094
1095genrule {
1096 name: "generated_hdr_other_pkg_x86",
1097 out: ["generated_hdr_other_pkg_x86.cpp"],
1098 cmd: "nothing to see here",
1099}`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001100 },
1101 blueprint: soongCcLibraryStaticPreamble + `
Chris Parsons484e50a2021-05-13 15:13:04 -04001102genrule {
1103 name: "generated_src",
1104 out: ["generated_src.cpp"],
1105 cmd: "nothing to see here",
1106}
1107
1108genrule {
1109 name: "generated_src_x86",
1110 out: ["generated_src_x86.cpp"],
1111 cmd: "nothing to see here",
1112}
1113
1114genrule {
1115 name: "generated_hdr",
1116 out: ["generated_hdr.h"],
1117 cmd: "nothing to see here",
1118}
1119
1120cc_library_static {
1121 name: "foo_static3",
Chris Parsons990c4f42021-05-25 12:10:58 -04001122 srcs: ["common.cpp", "not-for-*.cpp"],
1123 exclude_srcs: ["not-for-everything.cpp"],
Chris Parsons484e50a2021-05-13 15:13:04 -04001124 generated_sources: ["generated_src", "generated_src_other_pkg"],
1125 generated_headers: ["generated_hdr", "generated_hdr_other_pkg"],
1126 arch: {
1127 x86: {
Chris Parsons990c4f42021-05-25 12:10:58 -04001128 srcs: ["for-x86.cpp"],
1129 exclude_srcs: ["not-for-x86.cpp"],
Chris Parsons484e50a2021-05-13 15:13:04 -04001130 generated_sources: ["generated_src_x86"],
1131 generated_headers: ["generated_hdr_other_pkg_x86"],
1132 },
1133 },
1134}
1135`,
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001136 expectedBazelTargets: []string{`cc_library_static(
Chris Parsons484e50a2021-05-13 15:13:04 -04001137 name = "foo_static3",
1138 copts = [
1139 "-I.",
1140 "-I$(BINDIR)/.",
1141 ],
1142 linkstatic = True,
1143 srcs = [
1144 "//dep:generated_hdr_other_pkg",
1145 "//dep:generated_src_other_pkg",
1146 ":generated_hdr",
1147 ":generated_src",
Chris Parsons990c4f42021-05-25 12:10:58 -04001148 "common.cpp",
Chris Parsons484e50a2021-05-13 15:13:04 -04001149 ] + select({
1150 "//build/bazel/platforms/arch:x86": [
1151 "//dep:generated_hdr_other_pkg_x86",
1152 ":generated_src_x86",
Chris Parsons990c4f42021-05-25 12:10:58 -04001153 "for-x86.cpp",
Chris Parsons484e50a2021-05-13 15:13:04 -04001154 ],
Chris Parsons990c4f42021-05-25 12:10:58 -04001155 "//conditions:default": ["not-for-x86.cpp"],
Chris Parsons484e50a2021-05-13 15:13:04 -04001156 }),
1157)`},
Lukacs T. Berkic1cc3b92021-05-21 09:37:00 +02001158 })
Rupert Shuttleworth095081c2021-03-25 09:06:03 +00001159}
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001160
Rupert Shuttleworthc194ffb2021-05-19 06:49:02 -04001161func TestCcLibraryStaticGetTargetProperties(t *testing.T) {
1162 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1163
1164 description: "cc_library_static complex GetTargetProperties",
1165 moduleTypeUnderTest: "cc_library_static",
1166 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1167 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1168 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1169 blueprint: soongCcLibraryStaticPreamble + `
1170cc_library_static {
1171 name: "foo_static",
1172 target: {
1173 android: {
1174 srcs: ["android_src.c"],
1175 },
1176 android_arm: {
1177 srcs: ["android_arm_src.c"],
1178 },
1179 android_arm64: {
1180 srcs: ["android_arm64_src.c"],
1181 },
1182 android_x86: {
1183 srcs: ["android_x86_src.c"],
1184 },
1185 android_x86_64: {
1186 srcs: ["android_x86_64_src.c"],
1187 },
1188 linux_bionic_arm64: {
1189 srcs: ["linux_bionic_arm64_src.c"],
1190 },
1191 linux_bionic_x86_64: {
1192 srcs: ["linux_bionic_x86_64_src.c"],
1193 },
1194 },
1195}`,
1196 expectedBazelTargets: []string{`cc_library_static(
1197 name = "foo_static",
1198 copts = [
1199 "-I.",
1200 "-I$(BINDIR)/.",
1201 ],
1202 linkstatic = True,
1203 srcs_c = select({
1204 "//build/bazel/platforms/os:android": ["android_src.c"],
1205 "//conditions:default": [],
1206 }) + select({
1207 "//build/bazel/platforms:android_arm": ["android_arm_src.c"],
1208 "//build/bazel/platforms:android_arm64": ["android_arm64_src.c"],
1209 "//build/bazel/platforms:android_x86": ["android_x86_src.c"],
1210 "//build/bazel/platforms:android_x86_64": ["android_x86_64_src.c"],
1211 "//conditions:default": [],
1212 }) + select({
1213 "//build/bazel/platforms:linux_bionic_arm64": ["linux_bionic_arm64_src.c"],
1214 "//build/bazel/platforms:linux_bionic_x86_64": ["linux_bionic_x86_64_src.c"],
1215 "//conditions:default": [],
1216 }),
1217)`},
1218 })
1219}
1220
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001221func TestCcLibraryStaticProductVariableSelects(t *testing.T) {
1222 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1223 description: "cc_library_static product variable selects",
1224 moduleTypeUnderTest: "cc_library_static",
1225 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1226 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1227 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1228 filesystem: map[string]string{},
1229 blueprint: soongCcLibraryStaticPreamble + `
1230cc_library_static {
1231 name: "foo_static",
1232 srcs: ["common.c"],
1233 product_variables: {
1234 malloc_not_svelte: {
1235 cflags: ["-Wmalloc_not_svelte"],
1236 },
1237 malloc_zero_contents: {
1238 cflags: ["-Wmalloc_zero_contents"],
1239 },
1240 binder32bit: {
1241 cflags: ["-Wbinder32bit"],
1242 },
1243 },
1244} `,
1245 expectedBazelTargets: []string{`cc_library_static(
1246 name = "foo_static",
1247 copts = [
1248 "-I.",
1249 "-I$(BINDIR)/.",
1250 ] + select({
Liz Kammere3e4a5f2021-05-10 11:39:53 -04001251 "//build/bazel/product_variables:binder32bit": ["-Wbinder32bit"],
1252 "//conditions:default": [],
1253 }) + select({
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001254 "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
1255 "//conditions:default": [],
1256 }) + select({
1257 "//build/bazel/product_variables:malloc_zero_contents": ["-Wmalloc_zero_contents"],
1258 "//conditions:default": [],
Liz Kammere3e4a5f2021-05-10 11:39:53 -04001259 }),
1260 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -04001261 srcs_c = ["common.c"],
Liz Kammere3e4a5f2021-05-10 11:39:53 -04001262)`},
1263 })
1264}
1265
1266func TestCcLibraryStaticProductVariableArchSpecificSelects(t *testing.T) {
1267 runCcLibraryStaticTestCase(t, bp2buildTestCase{
1268 description: "cc_library_static arch-specific product variable selects",
1269 moduleTypeUnderTest: "cc_library_static",
1270 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
1271 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
1272 depsMutators: []android.RegisterMutatorFunc{cc.RegisterDepsBp2Build},
1273 filesystem: map[string]string{},
1274 blueprint: soongCcLibraryStaticPreamble + `
1275cc_library_static {
1276 name: "foo_static",
1277 srcs: ["common.c"],
1278 product_variables: {
1279 malloc_not_svelte: {
1280 cflags: ["-Wmalloc_not_svelte"],
1281 },
1282 },
1283 arch: {
1284 arm64: {
1285 product_variables: {
1286 malloc_not_svelte: {
1287 cflags: ["-Warm64_malloc_not_svelte"],
1288 },
1289 },
1290 },
1291 },
1292 multilib: {
1293 lib32: {
1294 product_variables: {
1295 malloc_not_svelte: {
1296 cflags: ["-Wlib32_malloc_not_svelte"],
1297 },
1298 },
1299 },
1300 },
1301 target: {
1302 android: {
1303 product_variables: {
1304 malloc_not_svelte: {
1305 cflags: ["-Wandroid_malloc_not_svelte"],
1306 },
1307 },
1308 }
1309 },
1310} `,
1311 expectedBazelTargets: []string{`cc_library_static(
1312 name = "foo_static",
1313 copts = [
1314 "-I.",
1315 "-I$(BINDIR)/.",
1316 ] + select({
1317 "//build/bazel/product_variables:malloc_not_svelte": ["-Wmalloc_not_svelte"],
1318 "//conditions:default": [],
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001319 }) + select({
Liz Kammere3e4a5f2021-05-10 11:39:53 -04001320 "//build/bazel/product_variables:malloc_not_svelte-android": ["-Wandroid_malloc_not_svelte"],
1321 "//conditions:default": [],
1322 }) + select({
1323 "//build/bazel/product_variables:malloc_not_svelte-arm": ["-Wlib32_malloc_not_svelte"],
1324 "//conditions:default": [],
1325 }) + select({
1326 "//build/bazel/product_variables:malloc_not_svelte-arm64": ["-Warm64_malloc_not_svelte"],
1327 "//conditions:default": [],
1328 }) + select({
1329 "//build/bazel/product_variables:malloc_not_svelte-x86": ["-Wlib32_malloc_not_svelte"],
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001330 "//conditions:default": [],
1331 }),
1332 linkstatic = True,
Chris Parsons990c4f42021-05-25 12:10:58 -04001333 srcs_c = ["common.c"],
Liz Kammer6fd7b3f2021-05-06 13:54:29 -04001334)`},
1335 })
1336}