blob: 73ca92fa1803854496f8ca3df0db10d711965f8f [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 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500273 moduleTypeUnderTest string
274 moduleTypeUnderTestFactory android.ModuleFactory
275 moduleTypeUnderTestBp2BuildMutator func(android.TopDownMutatorContext)
276 bp string
277 expectedBazelTarget string
278 description string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500279 }{
280 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500281 description: "filegroup with no srcs",
282 moduleTypeUnderTest: "filegroup",
283 moduleTypeUnderTestFactory: android.FileGroupFactory,
284 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500285 bp: `filegroup {
286 name: "foo",
287 srcs: [],
288}`,
289 expectedBazelTarget: `filegroup(
290 name = "foo",
291 srcs = [
292 ],
293)`,
294 },
295 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500296 description: "filegroup with srcs",
297 moduleTypeUnderTest: "filegroup",
298 moduleTypeUnderTestFactory: android.FileGroupFactory,
299 moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500300 bp: `filegroup {
301 name: "foo",
302 srcs: ["a", "b"],
303}`,
304 expectedBazelTarget: `filegroup(
305 name = "foo",
306 srcs = [
307 "a",
308 "b",
309 ],
310)`,
311 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500312 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500313 description: "genrule with command line variable replacements",
314 moduleTypeUnderTest: "genrule",
315 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
316 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500317 bp: `genrule {
318 name: "foo",
319 out: ["foo.out"],
320 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500321 tools: [":foo.tool"],
322 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500323}`,
324 expectedBazelTarget: `genrule(
325 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500326 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500327 outs = [
328 "foo.out",
329 ],
330 srcs = [
331 "foo.in",
332 ],
333 tools = [
334 ":foo.tool",
335 ],
336)`,
337 },
338 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500339 description: "genrule using $(locations :label)",
340 moduleTypeUnderTest: "genrule",
341 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
342 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen316e07c2020-12-14 09:09:52 -0500343 bp: `genrule {
344 name: "foo",
345 out: ["foo.out"],
346 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500347 tools: [":foo.tools"],
348 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500349}`,
350 expectedBazelTarget: `genrule(
351 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500352 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
353 outs = [
354 "foo.out",
355 ],
356 srcs = [
357 "foo.in",
358 ],
359 tools = [
360 ":foo.tools",
361 ],
362)`,
363 },
364 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500365 description: "genrule using $(location) label should substitute first tool label automatically",
366 moduleTypeUnderTest: "genrule",
367 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
368 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500369 bp: `genrule {
370 name: "foo",
371 out: ["foo.out"],
372 srcs: ["foo.in"],
373 tool_files: [":foo.tool", ":other.tool"],
374 cmd: "$(location) -s $(out) $(in)",
375}`,
376 expectedBazelTarget: `genrule(
377 name = "foo",
378 cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500379 outs = [
380 "foo.out",
381 ],
382 srcs = [
383 "foo.in",
384 ],
385 tools = [
386 ":foo.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500387 ":other.tool",
388 ],
389)`,
390 },
391 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500392 description: "genrule using $(locations) label should substitute first tool label automatically",
393 moduleTypeUnderTest: "genrule",
394 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
395 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500396 bp: `genrule {
397 name: "foo",
398 out: ["foo.out"],
399 srcs: ["foo.in"],
400 tools: [":foo.tool", ":other.tool"],
401 cmd: "$(locations) -s $(out) $(in)",
402}`,
403 expectedBazelTarget: `genrule(
404 name = "foo",
405 cmd = "$(locations :foo.tool) -s $(OUTS) $(SRCS)",
406 outs = [
407 "foo.out",
408 ],
409 srcs = [
410 "foo.in",
411 ],
412 tools = [
413 ":foo.tool",
414 ":other.tool",
415 ],
416)`,
417 },
418 {
Jingwen Chena42d6412021-01-26 21:57:27 -0500419 description: "genrule without tools or tool_files can convert successfully",
420 moduleTypeUnderTest: "genrule",
421 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
422 moduleTypeUnderTestBp2BuildMutator: genrule.GenruleBp2Build,
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500423 bp: `genrule {
424 name: "foo",
425 out: ["foo.out"],
426 srcs: ["foo.in"],
427 cmd: "cp $(in) $(out)",
428}`,
429 expectedBazelTarget: `genrule(
430 name = "foo",
431 cmd = "cp $(SRCS) $(OUTS)",
432 outs = [
433 "foo.out",
434 ],
435 srcs = [
436 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500437 ],
438)`,
439 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500440 }
441
442 dir := "."
443 for _, testCase := range testCases {
444 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
445 ctx := android.NewTestContext(config)
446 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
Jingwen Chena42d6412021-01-26 21:57:27 -0500447 ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500448 ctx.RegisterForBazelConversion()
449
450 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
451 android.FailIfErrored(t, errs)
452 _, errs = ctx.ResolveDependencies(config)
453 android.FailIfErrored(t, errs)
454
Jingwen Chen33832f92021-01-24 22:55:54 -0500455 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500456 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500457 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500458 }
459
460 actualBazelTarget := bazelTargets[0]
461 if actualBazelTarget.content != testCase.expectedBazelTarget {
462 t.Errorf(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500463 "%s: Expected generated Bazel target to be '%s', got '%s'",
464 testCase.description,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500465 testCase.expectedBazelTarget,
466 actualBazelTarget.content,
467 )
468 }
469 }
470}