blob: a6a002818e30c9e2e8ba2320bc6af50efb90c83b [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 ],
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000234 includes = [
235 ".",
236 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000237 linkstatic = True,
238 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400239 "static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000240 "implicit_include_1.h",
241 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000242 ],
243)`, `cc_library_static(
244 name = "static_lib_2",
Jingwen Chen63930982021-03-24 10:04:33 -0400245 hdrs = [
246 "implicit_include_1.h",
247 "implicit_include_2.h",
248 ],
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000249 includes = [
250 ".",
251 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000252 linkstatic = True,
253 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400254 "static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000255 "implicit_include_1.h",
256 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000257 ],
258)`, `cc_library_static(
259 name = "whole_static_lib_1",
Jingwen Chen63930982021-03-24 10:04:33 -0400260 hdrs = [
261 "implicit_include_1.h",
262 "implicit_include_2.h",
263 ],
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000264 includes = [
265 ".",
266 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000267 linkstatic = True,
268 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400269 "whole_static_lib_1.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000270 "implicit_include_1.h",
271 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000272 ],
273)`, `cc_library_static(
274 name = "whole_static_lib_2",
Jingwen Chen63930982021-03-24 10:04:33 -0400275 hdrs = [
276 "implicit_include_1.h",
277 "implicit_include_2.h",
278 ],
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000279 includes = [
280 ".",
281 ],
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000282 linkstatic = True,
283 srcs = [
Jingwen Chen63930982021-03-24 10:04:33 -0400284 "whole_static_lib_2.cc",
Rupert Shuttleworthc58d3d22021-04-06 16:37:15 +0000285 "implicit_include_1.h",
286 "implicit_include_2.h",
Rupert Shuttleworth095081c2021-03-25 09:06:03 +0000287 ],
288)`},
289 },
290 }
291
292 dir := "."
293 for _, testCase := range testCases {
294 filesystem := make(map[string][]byte)
295 toParse := []string{
296 "Android.bp",
297 }
298 for f, content := range testCase.filesystem {
299 if strings.HasSuffix(f, "Android.bp") {
300 toParse = append(toParse, f)
301 }
302 filesystem[f] = []byte(content)
303 }
304 config := android.TestConfig(buildDir, nil, testCase.bp, filesystem)
305 ctx := android.NewTestContext(config)
306
307 cc.RegisterCCBuildComponents(ctx)
308 ctx.RegisterModuleType("toolchain_library", cc.ToolchainLibraryFactory)
309 ctx.RegisterModuleType("cc_library_headers", cc.LibraryHeaderFactory)
310
311 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
312 for _, m := range testCase.depsMutators {
313 ctx.DepsBp2BuildMutators(m)
314 }
315 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
316 ctx.RegisterForBazelConversion()
317
318 _, errs := ctx.ParseFileList(dir, toParse)
319 if Errored(t, testCase.description, errs) {
320 continue
321 }
322 _, errs = ctx.ResolveDependencies(config)
323 if Errored(t, testCase.description, errs) {
324 continue
325 }
326
327 checkDir := dir
328 if testCase.dir != "" {
329 checkDir = testCase.dir
330 }
331 codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
332 bazelTargets := generateBazelTargetsForDir(codegenCtx, checkDir)
333 if actualCount, expectedCount := len(bazelTargets), len(testCase.expectedBazelTargets); actualCount != expectedCount {
334 t.Errorf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
335 } else {
336 for i, target := range bazelTargets {
337 if w, g := testCase.expectedBazelTargets[i], target.content; w != g {
338 t.Errorf(
339 "%s: Expected generated Bazel target to be '%s', got '%s'",
340 testCase.description,
341 w,
342 g,
343 )
344 }
345 }
346 }
347 }
348}