blob: 427aed35569e9b3e512cd5a793cb86fab8c4dd8f [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"
20 "strings"
21 "testing"
22)
23
24const (
25 // See cc/testing.go for more context
26 soongCcLibraryStaticPreamble = `
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: "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +000040}`
41)
42
43func TestCcLibraryStaticLoadStatement(t *testing.T) {
44 testCases := []struct {
45 bazelTargets BazelTargets
46 expectedLoadStatements string
47 }{
48 {
49 bazelTargets: BazelTargets{
50 BazelTarget{
51 name: "cc_library_static_target",
52 ruleClass: "cc_library_static",
53 // NOTE: No bzlLoadLocation for native rules
54 },
55 },
56 expectedLoadStatements: ``,
57 },
58 }
59
60 for _, testCase := range testCases {
61 actual := testCase.bazelTargets.LoadStatements()
62 expected := testCase.expectedLoadStatements
63 if actual != expected {
64 t.Fatalf("Expected load statements to be %s, got %s", expected, actual)
65 }
66 }
67
68}
69
70func TestCcLibraryStaticBp2Build(t *testing.T) {
71 testCases := []struct {
72 description string
73 moduleTypeUnderTest string
74 moduleTypeUnderTestFactory android.ModuleFactory
75 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
76 preArchMutators []android.RegisterMutatorFunc
77 depsMutators []android.RegisterMutatorFunc
78 bp string
79 expectedBazelTargets []string
80 filesystem map[string]string
81 dir string
82 }{
83 {
84 description: "cc_library_static test",
85 moduleTypeUnderTest: "cc_library_static",
86 moduleTypeUnderTestFactory: cc.LibraryStaticFactory,
87 moduleTypeUnderTestBp2BuildMutator: cc.CcLibraryStaticBp2Build,
88 filesystem: map[string]string{
89 // NOTE: include_dir headers *should not* appear in Bazel hdrs later (?)
90 "include_dir_1/include_dir_1_a.h": "",
91 "include_dir_1/include_dir_1_b.h": "",
92 "include_dir_2/include_dir_2_a.h": "",
93 "include_dir_2/include_dir_2_b.h": "",
94 // NOTE: local_include_dir headers *should not* appear in Bazel hdrs later (?)
95 "local_include_dir_1/local_include_dir_1_a.h": "",
96 "local_include_dir_1/local_include_dir_1_b.h": "",
97 "local_include_dir_2/local_include_dir_2_a.h": "",
98 "local_include_dir_2/local_include_dir_2_b.h": "",
99 // NOTE: export_include_dir headers *should* appear in Bazel hdrs later
100 "export_include_dir_1/export_include_dir_1_a.h": "",
101 "export_include_dir_1/export_include_dir_1_b.h": "",
102 "export_include_dir_2/export_include_dir_2_a.h": "",
103 "export_include_dir_2/export_include_dir_2_b.h": "",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000104 // NOTE: Soong implicitly includes headers in the current directory
105 "implicit_include_1.h": "",
106 "implicit_include_2.h": "",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000107 },
108 bp: soongCcLibraryStaticPreamble + `
109cc_library_headers {
110 name: "header_lib_1",
111 export_include_dirs: ["header_lib_1"],
112}
113
114cc_library_headers {
115 name: "header_lib_2",
116 export_include_dirs: ["header_lib_2"],
117}
118
119cc_library_static {
120 name: "static_lib_1",
121 srcs: ["static_lib_1.cc"],
122 bazel_module: { bp2build_available: true },
123}
124
125cc_library_static {
126 name: "static_lib_2",
127 srcs: ["static_lib_2.cc"],
128 bazel_module: { bp2build_available: true },
129}
130
131cc_library_static {
132 name: "whole_static_lib_1",
133 srcs: ["whole_static_lib_1.cc"],
134 bazel_module: { bp2build_available: true },
135}
136
137cc_library_static {
138 name: "whole_static_lib_2",
139 srcs: ["whole_static_lib_2.cc"],
140 bazel_module: { bp2build_available: true },
141}
142
143cc_library_static {
144 name: "foo_static",
145 srcs: [
146 "foo_static1.cc",
147 "foo_static2.cc",
148 ],
149 cflags: [
150 "-Dflag1",
151 "-Dflag2"
152 ],
153 static_libs: [
154 "static_lib_1",
155 "static_lib_2"
156 ],
157 whole_static_libs: [
158 "whole_static_lib_1",
159 "whole_static_lib_2"
160 ],
161 include_dirs: [
162 "include_dir_1",
163 "include_dir_2",
164 ],
165 local_include_dirs: [
166 "local_include_dir_1",
167 "local_include_dir_2",
168 ],
169 export_include_dirs: [
170 "export_include_dir_1",
171 "export_include_dir_2"
172 ],
173 header_libs: [
174 "header_lib_1",
175 "header_lib_2"
176 ],
177
178 // TODO: Also support export_header_lib_headers
179
180 bazel_module: { bp2build_available: true },
181}`,
182 expectedBazelTargets: []string{`cc_library_static(
183 name = "foo_static",
184 copts = [
185 "-Dflag1",
186 "-Dflag2",
187 ],
188 deps = [
189 ":header_lib_1",
190 ":header_lib_2",
191 ":static_lib_1",
192 ":static_lib_2",
193 ":whole_static_lib_1",
194 ":whole_static_lib_2",
195 ],
196 hdrs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400197 "implicit_include_1.h",
198 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000199 "export_include_dir_1/export_include_dir_1_a.h",
200 "export_include_dir_1/export_include_dir_1_b.h",
201 "export_include_dir_2/export_include_dir_2_a.h",
202 "export_include_dir_2/export_include_dir_2_b.h",
203 ],
204 includes = [
205 "export_include_dir_1",
206 "export_include_dir_2",
207 "include_dir_1",
208 "include_dir_2",
209 "local_include_dir_1",
210 "local_include_dir_2",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000211 ".",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000212 ],
213 linkstatic = True,
214 srcs = [
215 "foo_static1.cc",
216 "foo_static2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000217 "include_dir_1/include_dir_1_a.h",
218 "include_dir_1/include_dir_1_b.h",
219 "include_dir_2/include_dir_2_a.h",
220 "include_dir_2/include_dir_2_b.h",
221 "local_include_dir_1/local_include_dir_1_a.h",
222 "local_include_dir_1/local_include_dir_1_b.h",
223 "local_include_dir_2/local_include_dir_2_a.h",
224 "local_include_dir_2/local_include_dir_2_b.h",
Jingwen Chen63930982021-03-24 10:04:33 -0400225 "implicit_include_1.h",
226 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000227 ],
228)`, `cc_library_static(
229 name = "static_lib_1",
Jingwen Chen63930982021-03-24 10:04:33 -0400230 hdrs = [
231 "implicit_include_1.h",
232 "implicit_include_2.h",
233 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000234 includes = ["."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000235 linkstatic = True,
236 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400237 "static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000238 "implicit_include_1.h",
239 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000240 ],
241)`, `cc_library_static(
242 name = "static_lib_2",
Jingwen Chen63930982021-03-24 10:04:33 -0400243 hdrs = [
244 "implicit_include_1.h",
245 "implicit_include_2.h",
246 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000247 includes = ["."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000248 linkstatic = True,
249 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400250 "static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000251 "implicit_include_1.h",
252 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000253 ],
254)`, `cc_library_static(
255 name = "whole_static_lib_1",
Jingwen Chen63930982021-03-24 10:04:33 -0400256 hdrs = [
257 "implicit_include_1.h",
258 "implicit_include_2.h",
259 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000260 includes = ["."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000261 linkstatic = True,
262 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400263 "whole_static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000264 "implicit_include_1.h",
265 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000266 ],
267)`, `cc_library_static(
268 name = "whole_static_lib_2",
Jingwen Chen63930982021-03-24 10:04:33 -0400269 hdrs = [
270 "implicit_include_1.h",
271 "implicit_include_2.h",
272 ],
Jingwen Chenb4628eb2021-04-08 14:40:57 +0000273 includes = ["."],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000274 linkstatic = True,
275 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400276 "whole_static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000277 "implicit_include_1.h",
278 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000279 ],
280)`},
281 },
282 }
283
284 dir := "."
285 for _, testCase := range testCases {
286 filesystem := make(map[string][]byte)
287 toParse := []string{
288 "Android.bp",
289 }
290 for f, content := range testCase.filesystem {
291 if strings.HasSuffix(f, "Android.bp") {
292 toParse = append(toParse, f)
293 }
294 filesystem[f] = []byte(content)
295 }
296 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
297 ctx := android.NewTestContext(config)
298
299 cc.RegisterCCBuildComponents(ctx)
300 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
301 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
302
303 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
304 for _, m := range testCase.depsMutators {
305 ctx.DepsBp2BuildMutators(m)
306 }
307 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
308 ctx.RegisterForBazelConversion()
309
310 _, errs := ctx.ParseFileList(dir, toParse)
311 if Errored(t, testCase.description, errs) {
312 continue
313 }
314 _, errs = ctx.ResolveDependencies(config)
315 if Errored(t, testCase.description, errs) {
316 continue
317 }
318
319 checkDir := dir
320 if testCase.dir != "" {
321 checkDir = testCase.dir
322 }
323 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
324 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
325 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
326 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
327 } else {
328 for i, target := range bazelTargets {
329 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
330 t.Errorf(
331 "%s: Expected generated Bazel target to be '%s', got '%s'",
332 testCase.description,
333 w,
334 g,
335 )
336 }
337 }
338 }
339 }
340}