blob: be325304cb67166a6ec9b12e66ec3ff4da2ae94a [file] [log] [blame]
Liz Kammer2dd9ca42020-11-25 16:06:39 -08001// Copyright 2020 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"
Jingwen Chen316e07c2020-12-14 09:09:52 -050019 "android/soong/genrule"
Liz Kammer2dd9ca42020-11-25 16:06:39 -080020 "testing"
21)
22
23func TestGenerateSoongModuleTargets(t *testing.T) {
24 testCases := []struct {
25 bp string
26 expectedBazelTarget string
27 }{
28 {
29 bp: `custom {
30 name: "foo",
31}
32 `,
33 expectedBazelTarget: `soong_module(
34 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050035 soong_module_name = "foo",
36 soong_module_type = "custom",
37 soong_module_variant = "",
38 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080039 ],
40)`,
41 },
42 {
43 bp: `custom {
44 name: "foo",
45 ramdisk: true,
46}
47 `,
48 expectedBazelTarget: `soong_module(
49 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050050 soong_module_name = "foo",
51 soong_module_type = "custom",
52 soong_module_variant = "",
53 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080054 ],
55 ramdisk = True,
56)`,
57 },
58 {
59 bp: `custom {
60 name: "foo",
61 owner: "a_string_with\"quotes\"_and_\\backslashes\\\\",
62}
63 `,
64 expectedBazelTarget: `soong_module(
65 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050066 soong_module_name = "foo",
67 soong_module_type = "custom",
68 soong_module_variant = "",
69 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080070 ],
71 owner = "a_string_with\"quotes\"_and_\\backslashes\\\\",
72)`,
73 },
74 {
75 bp: `custom {
76 name: "foo",
77 required: ["bar"],
78}
79 `,
80 expectedBazelTarget: `soong_module(
81 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -050082 soong_module_name = "foo",
83 soong_module_type = "custom",
84 soong_module_variant = "",
85 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -080086 ],
87 required = [
88 "bar",
89 ],
90)`,
91 },
92 {
93 bp: `custom {
94 name: "foo",
95 target_required: ["qux", "bazqux"],
96}
97 `,
98 expectedBazelTarget: `soong_module(
99 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500100 soong_module_name = "foo",
101 soong_module_type = "custom",
102 soong_module_variant = "",
103 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800104 ],
105 target_required = [
106 "qux",
107 "bazqux",
108 ],
109)`,
110 },
111 {
112 bp: `custom {
113 name: "foo",
114 dist: {
115 targets: ["goal_foo"],
116 tag: ".foo",
117 },
118 dists: [
119 {
120 targets: ["goal_bar"],
121 tag: ".bar",
122 },
123 ],
124}
125 `,
126 expectedBazelTarget: `soong_module(
127 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500128 soong_module_name = "foo",
129 soong_module_type = "custom",
130 soong_module_variant = "",
131 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800132 ],
133 dist = {
134 "tag": ".foo",
135 "targets": [
136 "goal_foo",
137 ],
138 },
139 dists = [
140 {
141 "tag": ".bar",
142 "targets": [
143 "goal_bar",
144 ],
145 },
146 ],
147)`,
148 },
149 {
150 bp: `custom {
151 name: "foo",
152 required: ["bar"],
153 target_required: ["qux", "bazqux"],
154 ramdisk: true,
155 owner: "custom_owner",
156 dists: [
157 {
158 tag: ".tag",
159 targets: ["my_goal"],
160 },
161 ],
162}
163 `,
164 expectedBazelTarget: `soong_module(
165 name = "foo",
Jingwen Chen288e2ba2021-01-25 04:36:04 -0500166 soong_module_name = "foo",
167 soong_module_type = "custom",
168 soong_module_variant = "",
169 soong_module_deps = [
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800170 ],
171 dists = [
172 {
173 "tag": ".tag",
174 "targets": [
175 "my_goal",
176 ],
177 },
178 ],
179 owner = "custom_owner",
180 ramdisk = True,
181 required = [
182 "bar",
183 ],
184 target_required = [
185 "qux",
186 "bazqux",
187 ],
188)`,
189 },
190 }
191
192 dir := "."
193 for _, testCase := range testCases {
194 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
195 ctx := android.NewTestContext(config)
196 ctx.RegisterModuleType("custom", customModuleFactory)
197 ctx.Register()
198
199 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
200 android.FailIfErrored(t, errs)
201 _, errs = ctx.PrepareBuildActions(config)
202 android.FailIfErrored(t, errs)
203
Jingwen Chen33832f92021-01-24 22:55:54 -0500204 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, QueryView)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500205 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
206 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800207 }
208
209 actualBazelTarget := bazelTargets[0]
210 if actualBazelTarget.content != testCase.expectedBazelTarget {
211 t.Errorf(
212 "Expected generated Bazel target to be '%s', got '%s'",
213 testCase.expectedBazelTarget,
Jingwen Chen73850672020-12-14 08:25:34 -0500214 actualBazelTarget.content,
215 )
216 }
217 }
218}
219
220func TestGenerateBazelTargetModules(t *testing.T) {
221 testCases := []struct {
222 bp string
223 expectedBazelTarget string
224 }{
225 {
226 bp: `custom {
227 name: "foo",
228 string_list_prop: ["a", "b"],
229 string_prop: "a",
230}`,
231 expectedBazelTarget: `custom(
232 name = "foo",
233 string_list_prop = [
234 "a",
235 "b",
236 ],
237 string_prop = "a",
238)`,
239 },
240 }
241
242 dir := "."
243 for _, testCase := range testCases {
244 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
245 ctx := android.NewTestContext(config)
246 ctx.RegisterModuleType("custom", customModuleFactory)
247 ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator)
248 ctx.RegisterForBazelConversion()
249
250 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
251 android.FailIfErrored(t, errs)
252 _, errs = ctx.ResolveDependencies(config)
253 android.FailIfErrored(t, errs)
254
Jingwen Chen33832f92021-01-24 22:55:54 -0500255 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500256 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
257 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Jingwen Chen73850672020-12-14 08:25:34 -0500258 }
259
260 actualBazelTarget := bazelTargets[0]
261 if actualBazelTarget.content != testCase.expectedBazelTarget {
262 t.Errorf(
263 "Expected generated Bazel target to be '%s', got '%s'",
264 testCase.expectedBazelTarget,
265 actualBazelTarget.content,
Liz Kammer2dd9ca42020-11-25 16:06:39 -0800266 )
267 }
268 }
269}
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500270
271func TestModuleTypeBp2Build(t *testing.T) {
272 testCases := []struct {
273 moduleTypeUnderTest string
274 moduleTypeUnderTestFactory android.ModuleFactory
275 bp string
276 expectedBazelTarget string
277 }{
278 {
279 moduleTypeUnderTest: "filegroup",
280 moduleTypeUnderTestFactory: android.FileGroupFactory,
281 bp: `filegroup {
282 name: "foo",
283 srcs: [],
284}`,
285 expectedBazelTarget: `filegroup(
286 name = "foo",
287 srcs = [
288 ],
289)`,
290 },
291 {
292 moduleTypeUnderTest: "filegroup",
293 moduleTypeUnderTestFactory: android.FileGroupFactory,
294 bp: `filegroup {
295 name: "foo",
296 srcs: ["a", "b"],
297}`,
298 expectedBazelTarget: `filegroup(
299 name = "foo",
300 srcs = [
301 "a",
302 "b",
303 ],
304)`,
305 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500306 {
307 moduleTypeUnderTest: "genrule",
308 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
309 bp: `genrule {
310 name: "foo",
311 out: ["foo.out"],
312 srcs: ["foo.in"],
313 tool_files: [":foo.tool"],
314 cmd: "$(location :foo.tool) arg $(in) $(out)",
315}`,
316 expectedBazelTarget: `genrule(
317 name = "foo",
318 cmd = "$(location :foo.tool) arg $(SRCS) $(OUTS)",
319 outs = [
320 "foo.out",
321 ],
322 srcs = [
323 "foo.in",
324 ],
325 tools = [
326 ":foo.tool",
327 ],
328)`,
329 },
330 {
331 moduleTypeUnderTest: "genrule",
332 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
333 bp: `genrule {
334 name: "foo",
335 out: ["foo.out"],
336 srcs: ["foo.in"],
337 tools: [":foo.tool"],
338 cmd: "$(location :foo.tool) --out-dir=$(genDir) $(in)",
339}`,
340 expectedBazelTarget: `genrule(
341 name = "foo",
342 cmd = "$(location :foo.tool) --out-dir=$(GENDIR) $(SRCS)",
343 outs = [
344 "foo.out",
345 ],
346 srcs = [
347 "foo.in",
348 ],
349 tools = [
350 ":foo.tool",
351 ],
352)`,
353 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500354 }
355
356 dir := "."
357 for _, testCase := range testCases {
358 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
359 ctx := android.NewTestContext(config)
360 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
361 ctx.RegisterForBazelConversion()
362
363 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
364 android.FailIfErrored(t, errs)
365 _, errs = ctx.ResolveDependencies(config)
366 android.FailIfErrored(t, errs)
367
Jingwen Chen33832f92021-01-24 22:55:54 -0500368 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500369 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
370 t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500371 }
372
373 actualBazelTarget := bazelTargets[0]
374 if actualBazelTarget.content != testCase.expectedBazelTarget {
375 t.Errorf(
376 "Expected generated Bazel target to be '%s', got '%s'",
377 testCase.expectedBazelTarget,
378 actualBazelTarget.content,
379 )
380 }
381 }
382}