blob: 367e13f7590d08ad77f897a51c4ae2e211a0032a [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
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500277 description string
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500278 }{
279 {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500280 description: "filegroup with no srcs",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500281 moduleTypeUnderTest: "filegroup",
282 moduleTypeUnderTestFactory: android.FileGroupFactory,
283 bp: `filegroup {
284 name: "foo",
285 srcs: [],
286}`,
287 expectedBazelTarget: `filegroup(
288 name = "foo",
289 srcs = [
290 ],
291)`,
292 },
293 {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500294 description: "filegroup with srcs",
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500295 moduleTypeUnderTest: "filegroup",
296 moduleTypeUnderTestFactory: android.FileGroupFactory,
297 bp: `filegroup {
298 name: "foo",
299 srcs: ["a", "b"],
300}`,
301 expectedBazelTarget: `filegroup(
302 name = "foo",
303 srcs = [
304 "a",
305 "b",
306 ],
307)`,
308 },
Jingwen Chen316e07c2020-12-14 09:09:52 -0500309 {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500310 description: "genrule with command line variable replacements",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500311 moduleTypeUnderTest: "genrule",
312 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
313 bp: `genrule {
314 name: "foo",
315 out: ["foo.out"],
316 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500317 tools: [":foo.tool"],
318 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500319}`,
320 expectedBazelTarget: `genrule(
321 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500322 cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500323 outs = [
324 "foo.out",
325 ],
326 srcs = [
327 "foo.in",
328 ],
329 tools = [
330 ":foo.tool",
331 ],
332)`,
333 },
334 {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500335 description: "genrule using $(locations :label)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500336 moduleTypeUnderTest: "genrule",
337 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
338 bp: `genrule {
339 name: "foo",
340 out: ["foo.out"],
341 srcs: ["foo.in"],
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500342 tools: [":foo.tools"],
343 cmd: "$(locations :foo.tools) -s $(out) $(in)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500344}`,
345 expectedBazelTarget: `genrule(
346 name = "foo",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500347 cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)",
348 outs = [
349 "foo.out",
350 ],
351 srcs = [
352 "foo.in",
353 ],
354 tools = [
355 ":foo.tools",
356 ],
357)`,
358 },
359 {
360 description: "genrule using $(location) label should substitute first tool label automatically",
361 moduleTypeUnderTest: "genrule",
362 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
363 bp: `genrule {
364 name: "foo",
365 out: ["foo.out"],
366 srcs: ["foo.in"],
367 tool_files: [":foo.tool", ":other.tool"],
368 cmd: "$(location) -s $(out) $(in)",
369}`,
370 expectedBazelTarget: `genrule(
371 name = "foo",
372 cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500373 outs = [
374 "foo.out",
375 ],
376 srcs = [
377 "foo.in",
378 ],
379 tools = [
380 ":foo.tool",
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500381 ":other.tool",
382 ],
383)`,
384 },
385 {
386 description: "genrule using $(locations) label should substitute first tool label automatically",
387 moduleTypeUnderTest: "genrule",
388 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
389 bp: `genrule {
390 name: "foo",
391 out: ["foo.out"],
392 srcs: ["foo.in"],
393 tools: [":foo.tool", ":other.tool"],
394 cmd: "$(locations) -s $(out) $(in)",
395}`,
396 expectedBazelTarget: `genrule(
397 name = "foo",
398 cmd = "$(locations :foo.tool) -s $(OUTS) $(SRCS)",
399 outs = [
400 "foo.out",
401 ],
402 srcs = [
403 "foo.in",
404 ],
405 tools = [
406 ":foo.tool",
407 ":other.tool",
408 ],
409)`,
410 },
411 {
412 description: "genrule without tools or tool_files can convert successfully",
413 moduleTypeUnderTest: "genrule",
414 moduleTypeUnderTestFactory: genrule.GenRuleFactory,
415 bp: `genrule {
416 name: "foo",
417 out: ["foo.out"],
418 srcs: ["foo.in"],
419 cmd: "cp $(in) $(out)",
420}`,
421 expectedBazelTarget: `genrule(
422 name = "foo",
423 cmd = "cp $(SRCS) $(OUTS)",
424 outs = [
425 "foo.out",
426 ],
427 srcs = [
428 "foo.in",
Jingwen Chen316e07c2020-12-14 09:09:52 -0500429 ],
430)`,
431 },
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500432 }
433
434 dir := "."
435 for _, testCase := range testCases {
436 config := android.TestConfig(buildDir, nil, testCase.bp, nil)
437 ctx := android.NewTestContext(config)
438 ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
439 ctx.RegisterForBazelConversion()
440
441 _, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
442 android.FailIfErrored(t, errs)
443 _, errs = ctx.ResolveDependencies(config)
444 android.FailIfErrored(t, errs)
445
Jingwen Chen33832f92021-01-24 22:55:54 -0500446 bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir]
Jingwen Chen4e4756d2021-01-24 21:13:13 -0500447 if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount {
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500448 t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount)
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500449 }
450
451 actualBazelTarget := bazelTargets[0]
452 if actualBazelTarget.content != testCase.expectedBazelTarget {
453 t.Errorf(
Jingwen Chen885ee7a2021-01-26 03:16:49 -0500454 "%s: Expected generated Bazel target to be '%s', got '%s'",
455 testCase.description,
Jingwen Chen32b4ece2021-01-21 03:20:18 -0500456 testCase.expectedBazelTarget,
457 actualBazelTarget.content,
458 )
459 }
460 }
461}