blob: 9244b9922881cece57cd556c65afabe5e2653956 [file] [log] [blame]
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -04001// 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"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000019 "android/soong/cc"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040020 "android/soong/genrule"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000021 "android/soong/java"
22 "fmt"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040023 "testing"
24)
25
Liz Kammer78cfdaa2021-11-08 12:56:31 -050026func registerGenruleModuleTypes(ctx android.RegistrationContext) {
27 ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
28}
29
30func runGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
31 t.Helper()
32 (&tc).moduleTypeUnderTest = "genrule"
33 (&tc).moduleTypeUnderTestFactory = genrule.GenRuleFactory
Liz Kammer78cfdaa2021-11-08 12:56:31 -050034 runBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
35}
36
Sam Delmericocd1b80f2022-01-11 21:55:46 +000037func otherGenruleBp(genruleTarget string) map[string]string {
38 return map[string]string{
39 "other/Android.bp": fmt.Sprintf(`%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040040 name: "foo.tool",
41 out: ["foo_tool.out"],
42 srcs: ["foo_tool.in"],
43 cmd: "cp $(in) $(out)",
44}
Sam Delmericocd1b80f2022-01-11 21:55:46 +000045%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040046 name: "other.tool",
47 out: ["other_tool.out"],
48 srcs: ["other_tool.in"],
49 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000050}`, genruleTarget, genruleTarget),
51 }
52}
53
54func TestGenruleCliVariableReplacement(t *testing.T) {
55 testCases := []struct {
56 moduleType string
57 factory android.ModuleFactory
58 genDir string
59 }{
60 {
61 moduleType: "genrule",
62 factory: genrule.GenRuleFactory,
63 genDir: "$(GENDIR)",
64 },
65 {
66 moduleType: "cc_genrule",
67 factory: cc.GenRuleFactory,
68 genDir: "$(RULEDIR)",
69 },
70 {
71 moduleType: "java_genrule",
72 factory: java.GenRuleFactory,
73 genDir: "$(RULEDIR)",
74 },
75 {
76 moduleType: "java_genrule_host",
77 factory: java.GenRuleFactoryHost,
78 genDir: "$(RULEDIR)",
79 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040080 }
81
Sam Delmericocd1b80f2022-01-11 21:55:46 +000082 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040083 name: "foo.tool",
84 out: ["foo_tool.out"],
85 srcs: ["foo_tool.in"],
86 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000087 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040088}
89
Sam Delmericocd1b80f2022-01-11 21:55:46 +000090%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040091 name: "foo",
92 out: ["foo.out"],
93 srcs: ["foo.in"],
94 tools: [":foo.tool"],
95 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
96 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +000097}`
98
99 for _, tc := range testCases {
Sam Delmerico75539d62022-01-31 14:37:29 +0000100 moduleAttrs := attrNameToString{
101 "cmd": fmt.Sprintf(`"$(location :foo.tool) --genDir=%s arg $(SRCS) $(OUTS)"`, tc.genDir),
102 "outs": `["foo.out"]`,
103 "srcs": `["foo.in"]`,
104 "tools": `[":foo.tool"]`,
105 }
106
107 if tc.moduleType == "java_genrule_host" {
108 moduleAttrs["target_compatible_with"] = `select({
109 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
110 "//conditions:default": [],
111 })`
112 }
113
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000114 expectedBazelTargets := []string{
Sam Delmerico75539d62022-01-31 14:37:29 +0000115 makeBazelTarget("genrule", "foo", moduleAttrs),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000116 }
117
118 t.Run(tc.moduleType, func(t *testing.T) {
119 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
120 bp2buildTestCase{
121 moduleTypeUnderTest: tc.moduleType,
122 moduleTypeUnderTestFactory: tc.factory,
123 blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType),
124 expectedBazelTargets: expectedBazelTargets,
125 })
126 })
127 }
128}
129
130func TestGenruleLocationsLabel(t *testing.T) {
131 testCases := []struct {
132 moduleType string
133 factory android.ModuleFactory
134 }{
135 {
136 moduleType: "genrule",
137 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400138 },
139 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000140 moduleType: "cc_genrule",
141 factory: cc.GenRuleFactory,
142 },
143 {
144 moduleType: "java_genrule",
145 factory: java.GenRuleFactory,
146 },
147 {
148 moduleType: "java_genrule_host",
149 factory: java.GenRuleFactoryHost,
150 },
151 }
152
153 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400154 name: "foo.tools",
155 out: ["foo_tool.out", "foo_tool2.out"],
156 srcs: ["foo_tool.in"],
157 cmd: "cp $(in) $(out)",
158 bazel_module: { bp2build_available: true },
159}
160
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000161%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400162 name: "foo",
163 out: ["foo.out"],
164 srcs: ["foo.in"],
165 tools: [":foo.tools"],
166 cmd: "$(locations :foo.tools) -s $(out) $(in)",
167 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000168}`
169
Sam Delmerico75539d62022-01-31 14:37:29 +0000170 for _, tc := range testCases {
171 fooAttrs := attrNameToString{
172 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
173 "outs": `["foo.out"]`,
174 "srcs": `["foo.in"]`,
175 "tools": `[":foo.tools"]`,
176 }
177 fooToolsAttrs := attrNameToString{
178 "cmd": `"cp $(SRCS) $(OUTS)"`,
179 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400180 "foo_tool.out",
181 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500182 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000183 "srcs": `["foo_tool.in"]`,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000184 }
185
Sam Delmerico75539d62022-01-31 14:37:29 +0000186 if tc.moduleType == "java_genrule_host" {
187 compatibilityAttrs := `select({
188 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
189 "//conditions:default": [],
190 })`
191 fooAttrs["target_compatible_with"] = compatibilityAttrs
192 fooToolsAttrs["target_compatible_with"] = compatibilityAttrs
193 }
194
195 expectedBazelTargets := []string{
196 makeBazelTarget("genrule", "foo", fooAttrs),
197 makeBazelTarget("genrule", "foo.tools", fooToolsAttrs),
198 }
199
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000200 t.Run(tc.moduleType, func(t *testing.T) {
201 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
202 bp2buildTestCase{
203 moduleTypeUnderTest: tc.moduleType,
204 moduleTypeUnderTestFactory: tc.factory,
205 blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType),
206 expectedBazelTargets: expectedBazelTargets,
207 })
208 })
209 }
210}
211
212func TestGenruleLocationsAbsoluteLabel(t *testing.T) {
213 testCases := []struct {
214 moduleType string
215 factory android.ModuleFactory
216 }{
217 {
218 moduleType: "genrule",
219 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400220 },
221 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000222 moduleType: "cc_genrule",
223 factory: cc.GenRuleFactory,
224 },
225 {
226 moduleType: "java_genrule",
227 factory: java.GenRuleFactory,
228 },
229 {
230 moduleType: "java_genrule_host",
231 factory: java.GenRuleFactoryHost,
232 },
233 }
234
235 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400236 name: "foo",
237 out: ["foo.out"],
238 srcs: ["foo.in"],
239 tool_files: [":foo.tool"],
240 cmd: "$(locations :foo.tool) -s $(out) $(in)",
241 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000242}`
243
Sam Delmerico75539d62022-01-31 14:37:29 +0000244 for _, tc := range testCases {
245 moduleAttrs := attrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000246 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
247 "outs": `["foo.out"]`,
248 "srcs": `["foo.in"]`,
249 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000250 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000251
Sam Delmerico75539d62022-01-31 14:37:29 +0000252 if tc.moduleType == "java_genrule_host" {
253 moduleAttrs["target_compatible_with"] = `select({
254 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
255 "//conditions:default": [],
256 })`
257 }
258
259 expectedBazelTargets := []string{
260 makeBazelTarget("genrule", "foo", moduleAttrs),
261 }
262
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000263 t.Run(tc.moduleType, func(t *testing.T) {
264 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
265 bp2buildTestCase{
266 moduleTypeUnderTest: tc.moduleType,
267 moduleTypeUnderTestFactory: tc.factory,
268 blueprint: fmt.Sprintf(bp, tc.moduleType),
269 expectedBazelTargets: expectedBazelTargets,
270 filesystem: otherGenruleBp(tc.moduleType),
271 })
272 })
273 }
274}
275
276func TestGenruleSrcsLocationsAbsoluteLabel(t *testing.T) {
277 testCases := []struct {
278 moduleType string
279 factory android.ModuleFactory
280 }{
281 {
282 moduleType: "genrule",
283 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400284 },
285 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000286 moduleType: "cc_genrule",
287 factory: cc.GenRuleFactory,
288 },
289 {
290 moduleType: "java_genrule",
291 factory: java.GenRuleFactory,
292 },
293 {
294 moduleType: "java_genrule_host",
295 factory: java.GenRuleFactoryHost,
296 },
297 }
298
299 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400300 name: "foo",
301 out: ["foo.out"],
302 srcs: [":other.tool"],
303 tool_files: [":foo.tool"],
304 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
305 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000306}`
307
Sam Delmerico75539d62022-01-31 14:37:29 +0000308 for _, tc := range testCases {
309 moduleAttrs := attrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000310 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
311 "outs": `["foo.out"]`,
312 "srcs": `["//other:other.tool"]`,
313 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000314 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000315
Sam Delmerico75539d62022-01-31 14:37:29 +0000316 if tc.moduleType == "java_genrule_host" {
317 moduleAttrs["target_compatible_with"] = `select({
318 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
319 "//conditions:default": [],
320 })`
321 }
322
323 expectedBazelTargets := []string{
324 makeBazelTarget("genrule", "foo", moduleAttrs),
325 }
326
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000327 t.Run(tc.moduleType, func(t *testing.T) {
328 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
329 bp2buildTestCase{
330 moduleTypeUnderTest: tc.moduleType,
331 moduleTypeUnderTestFactory: tc.factory,
332 blueprint: fmt.Sprintf(bp, tc.moduleType),
333 expectedBazelTargets: expectedBazelTargets,
334 filesystem: otherGenruleBp(tc.moduleType),
335 })
336 })
337 }
338}
339
340func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) {
341 testCases := []struct {
342 moduleType string
343 factory android.ModuleFactory
344 }{
345 {
346 moduleType: "genrule",
347 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400348 },
349 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000350 moduleType: "cc_genrule",
351 factory: cc.GenRuleFactory,
352 },
353 {
354 moduleType: "java_genrule",
355 factory: java.GenRuleFactory,
356 },
357 {
358 moduleType: "java_genrule_host",
359 factory: java.GenRuleFactoryHost,
360 },
361 }
362
363 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400364 name: "foo",
365 out: ["foo.out"],
366 srcs: ["foo.in"],
367 tool_files: [":foo.tool", ":other.tool"],
368 cmd: "$(location) -s $(out) $(in)",
369 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000370}`
371
Sam Delmerico75539d62022-01-31 14:37:29 +0000372 for _, tc := range testCases {
373 moduleAttrs := attrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000374 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
375 "outs": `["foo.out"]`,
376 "srcs": `["foo.in"]`,
377 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400378 "//other:foo.tool",
379 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500380 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000381 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000382
Sam Delmerico75539d62022-01-31 14:37:29 +0000383 if tc.moduleType == "java_genrule_host" {
384 moduleAttrs["target_compatible_with"] = `select({
385 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
386 "//conditions:default": [],
387 })`
388 }
389
390 expectedBazelTargets := []string{
391 makeBazelTarget("genrule", "foo", moduleAttrs),
392 }
393
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000394 t.Run(tc.moduleType, func(t *testing.T) {
395 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
396 bp2buildTestCase{
397 moduleTypeUnderTest: tc.moduleType,
398 moduleTypeUnderTestFactory: tc.factory,
399 blueprint: fmt.Sprintf(bp, tc.moduleType),
400 expectedBazelTargets: expectedBazelTargets,
401 filesystem: otherGenruleBp(tc.moduleType),
402 })
403 })
404 }
405}
406
407func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) {
408 testCases := []struct {
409 moduleType string
410 factory android.ModuleFactory
411 }{
412 {
413 moduleType: "genrule",
414 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400415 },
416 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000417 moduleType: "cc_genrule",
418 factory: cc.GenRuleFactory,
419 },
420 {
421 moduleType: "java_genrule",
422 factory: java.GenRuleFactory,
423 },
424 {
425 moduleType: "java_genrule_host",
426 factory: java.GenRuleFactoryHost,
427 },
428 }
429
430 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400431 name: "foo",
432 out: ["foo.out"],
433 srcs: ["foo.in"],
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000434 tool_files: [":foo.tool", ":other.tool"],
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400435 cmd: "$(locations) -s $(out) $(in)",
436 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000437}`
438
Sam Delmerico75539d62022-01-31 14:37:29 +0000439 for _, tc := range testCases {
440 moduleAttrs := attrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000441 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
442 "outs": `["foo.out"]`,
443 "srcs": `["foo.in"]`,
444 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400445 "//other:foo.tool",
446 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500447 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000448 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000449
Sam Delmerico75539d62022-01-31 14:37:29 +0000450 if tc.moduleType == "java_genrule_host" {
451 moduleAttrs["target_compatible_with"] = `select({
452 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
453 "//conditions:default": [],
454 })`
455 }
456
457 expectedBazelTargets := []string{
458 makeBazelTarget("genrule", "foo", moduleAttrs),
459 }
460
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000461 t.Run(tc.moduleType, func(t *testing.T) {
462 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
463 bp2buildTestCase{
464 moduleTypeUnderTest: tc.moduleType,
465 moduleTypeUnderTestFactory: tc.factory,
466 blueprint: fmt.Sprintf(bp, tc.moduleType),
467 expectedBazelTargets: expectedBazelTargets,
468 filesystem: otherGenruleBp(tc.moduleType),
469 })
470 })
471 }
472}
473
474func TestGenruleWithoutToolsOrToolFiles(t *testing.T) {
475 testCases := []struct {
476 moduleType string
477 factory android.ModuleFactory
478 }{
479 {
480 moduleType: "genrule",
481 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400482 },
483 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000484 moduleType: "cc_genrule",
485 factory: cc.GenRuleFactory,
486 },
487 {
488 moduleType: "java_genrule",
489 factory: java.GenRuleFactory,
490 },
491 {
492 moduleType: "java_genrule_host",
493 factory: java.GenRuleFactoryHost,
494 },
495 }
496
497 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400498 name: "foo",
499 out: ["foo.out"],
500 srcs: ["foo.in"],
501 cmd: "cp $(in) $(out)",
502 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000503}`
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400504
Sam Delmerico75539d62022-01-31 14:37:29 +0000505 for _, tc := range testCases {
506 moduleAttrs := attrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000507 "cmd": `"cp $(SRCS) $(OUTS)"`,
508 "outs": `["foo.out"]`,
509 "srcs": `["foo.in"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000510 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000511
Sam Delmerico75539d62022-01-31 14:37:29 +0000512 if tc.moduleType == "java_genrule_host" {
513 moduleAttrs["target_compatible_with"] = `select({
514 "//build/bazel/platforms/os:android": ["@platforms//:incompatible"],
515 "//conditions:default": [],
516 })`
517 }
518
519 expectedBazelTargets := []string{
520 makeBazelTarget("genrule", "foo", moduleAttrs),
521 }
522
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000523 t.Run(tc.moduleType, func(t *testing.T) {
524 runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
525 bp2buildTestCase{
526 moduleTypeUnderTest: tc.moduleType,
527 moduleTypeUnderTestFactory: tc.factory,
528 blueprint: fmt.Sprintf(bp, tc.moduleType),
529 expectedBazelTargets: expectedBazelTargets,
530 })
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500531 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400532 }
533}
534
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000535func TestGenruleBp2BuildInlinesDefaults(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500536 testCases := []bp2buildTestCase{
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400537 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500538 description: "genrule applies properties from a genrule_defaults dependency if not specified",
539 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400540 name: "gen_defaults",
541 cmd: "do-something $(in) $(out)",
542}
543genrule {
544 name: "gen",
545 out: ["out"],
546 srcs: ["in1"],
547 defaults: ["gen_defaults"],
548 bazel_module: { bp2build_available: true },
549}
550`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500551 expectedBazelTargets: []string{
552 makeBazelTarget("genrule", "gen", attrNameToString{
553 "cmd": `"do-something $(SRCS) $(OUTS)"`,
554 "outs": `["out"]`,
555 "srcs": `["in1"]`,
556 }),
557 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400558 },
559 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500560 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
561 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400562 name: "gen_defaults",
563 out: ["out-from-defaults"],
564 srcs: ["in-from-defaults"],
565 cmd: "cmd-from-defaults",
566}
567genrule {
568 name: "gen",
569 out: ["out"],
570 srcs: ["in1"],
571 defaults: ["gen_defaults"],
572 cmd: "do-something $(in) $(out)",
573 bazel_module: { bp2build_available: true },
574}
575`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500576 expectedBazelTargets: []string{
577 makeBazelTarget("genrule", "gen", attrNameToString{
578 "cmd": `"do-something $(SRCS) $(OUTS)"`,
579 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400580 "out-from-defaults",
581 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500582 ]`,
583 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400584 "in-from-defaults",
585 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500586 ]`,
587 }),
588 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400589 },
590 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500591 description: "genrule applies properties from list of genrule_defaults",
592 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400593 name: "gen_defaults1",
594 cmd: "cp $(in) $(out)",
595}
596
597genrule_defaults {
598 name: "gen_defaults2",
599 srcs: ["in1"],
600}
601
602genrule {
603 name: "gen",
604 out: ["out"],
605 defaults: ["gen_defaults1", "gen_defaults2"],
606 bazel_module: { bp2build_available: true },
607}
608`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500609 expectedBazelTargets: []string{
610 makeBazelTarget("genrule", "gen", attrNameToString{
611 "cmd": `"cp $(SRCS) $(OUTS)"`,
612 "outs": `["out"]`,
613 "srcs": `["in1"]`,
614 }),
615 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400616 },
617 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500618 description: "genrule applies properties from genrule_defaults transitively",
619 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400620 name: "gen_defaults1",
621 defaults: ["gen_defaults2"],
622 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
623}
624
625genrule_defaults {
626 name: "gen_defaults2",
627 defaults: ["gen_defaults3"],
628 cmd: "cmd2 $(in) $(out)",
629 out: ["out-from-2"],
630 srcs: ["in1"],
631}
632
633genrule_defaults {
634 name: "gen_defaults3",
635 out: ["out-from-3"],
636 srcs: ["srcs-from-3"],
637}
638
639genrule {
640 name: "gen",
641 out: ["out"],
642 defaults: ["gen_defaults1"],
643 bazel_module: { bp2build_available: true },
644}
645`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500646 expectedBazelTargets: []string{
647 makeBazelTarget("genrule", "gen", attrNameToString{
648 "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
649 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400650 "out-from-3",
651 "out-from-2",
652 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500653 ]`,
654 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400655 "srcs-from-3",
656 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500657 ]`,
658 }),
659 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400660 },
661 }
662
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400663 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500664 t.Run(testCase.description, func(t *testing.T) {
665 runGenruleTestCase(t, testCase)
666 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400667 }
668}