blob: fd631a53b9da61a80c1b10cd9ee7f6fd724eaf69 [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"
19 "android/soong/genrule"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040020 "testing"
21)
22
Liz Kammer78cfdaa2021-11-08 12:56:31 -050023func registerGenruleModuleTypes(ctx android.RegistrationContext) {
24 ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
25}
26
27func runGenruleTestCase(t *testing.T, tc bp2buildTestCase) {
28 t.Helper()
29 (&tc).moduleTypeUnderTest = "genrule"
30 (&tc).moduleTypeUnderTestFactory = genrule.GenRuleFactory
Liz Kammer78cfdaa2021-11-08 12:56:31 -050031 runBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
32}
33
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040034func TestGenruleBp2Build(t *testing.T) {
35 otherGenruleBp := map[string]string{
36 "other/Android.bp": `genrule {
37 name: "foo.tool",
38 out: ["foo_tool.out"],
39 srcs: ["foo_tool.in"],
40 cmd: "cp $(in) $(out)",
41}
42genrule {
43 name: "other.tool",
44 out: ["other_tool.out"],
45 srcs: ["other_tool.in"],
46 cmd: "cp $(in) $(out)",
47}`,
48 }
49
50 testCases := []bp2buildTestCase{
51 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050052 description: "genrule with command line variable replacements",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040053 blueprint: `genrule {
54 name: "foo.tool",
55 out: ["foo_tool.out"],
56 srcs: ["foo_tool.in"],
57 cmd: "cp $(in) $(out)",
58 bazel_module: { bp2build_available: true },
59}
60
61genrule {
62 name: "foo",
63 out: ["foo.out"],
64 srcs: ["foo.in"],
65 tools: [":foo.tool"],
66 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
67 bazel_module: { bp2build_available: true },
68}`,
69 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050070 makeBazelTarget("genrule", "foo", attrNameToString{
71 "cmd": `"$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)"`,
72 "outs": `["foo.out"]`,
73 "srcs": `["foo.in"]`,
74 "tools": `[":foo.tool"]`,
75 }),
76 makeBazelTarget("genrule", "foo.tool", attrNameToString{
77 "cmd": `"cp $(SRCS) $(OUTS)"`,
78 "outs": `["foo_tool.out"]`,
79 "srcs": `["foo_tool.in"]`,
80 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040081 },
82 },
83 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050084 description: "genrule using $(locations :label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040085 blueprint: `genrule {
86 name: "foo.tools",
87 out: ["foo_tool.out", "foo_tool2.out"],
88 srcs: ["foo_tool.in"],
89 cmd: "cp $(in) $(out)",
90 bazel_module: { bp2build_available: true },
91}
92
93genrule {
94 name: "foo",
95 out: ["foo.out"],
96 srcs: ["foo.in"],
97 tools: [":foo.tools"],
98 cmd: "$(locations :foo.tools) -s $(out) $(in)",
99 bazel_module: { bp2build_available: true },
100}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500101 expectedBazelTargets: []string{
102 makeBazelTarget("genrule", "foo", attrNameToString{
103 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
104 "outs": `["foo.out"]`,
105 "srcs": `["foo.in"]`,
106 "tools": `[":foo.tools"]`,
107 }),
108 makeBazelTarget("genrule", "foo.tools", attrNameToString{
109 "cmd": `"cp $(SRCS) $(OUTS)"`,
110 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400111 "foo_tool.out",
112 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500113 ]`,
114 "srcs": `["foo_tool.in"]`,
115 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400116 },
117 },
118 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500119 description: "genrule using $(locations //absolute:label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400120 blueprint: `genrule {
121 name: "foo",
122 out: ["foo.out"],
123 srcs: ["foo.in"],
124 tool_files: [":foo.tool"],
125 cmd: "$(locations :foo.tool) -s $(out) $(in)",
126 bazel_module: { bp2build_available: true },
127}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500128 expectedBazelTargets: []string{
129 makeBazelTarget("genrule", "foo", attrNameToString{
130 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
131 "outs": `["foo.out"]`,
132 "srcs": `["foo.in"]`,
133 "tools": `["//other:foo.tool"]`,
134 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400135 },
136 filesystem: otherGenruleBp,
137 },
138 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500139 description: "genrule srcs using $(locations //absolute:label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400140 blueprint: `genrule {
141 name: "foo",
142 out: ["foo.out"],
143 srcs: [":other.tool"],
144 tool_files: [":foo.tool"],
145 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
146 bazel_module: { bp2build_available: true },
147}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500148 expectedBazelTargets: []string{
149 makeBazelTarget("genrule", "foo", attrNameToString{
150 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
151 "outs": `["foo.out"]`,
152 "srcs": `["//other:other.tool"]`,
153 "tools": `["//other:foo.tool"]`,
154 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400155 },
156 filesystem: otherGenruleBp,
157 },
158 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500159 description: "genrule using $(location) label should substitute first tool label automatically",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400160 blueprint: `genrule {
161 name: "foo",
162 out: ["foo.out"],
163 srcs: ["foo.in"],
164 tool_files: [":foo.tool", ":other.tool"],
165 cmd: "$(location) -s $(out) $(in)",
166 bazel_module: { bp2build_available: true },
167}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500168 expectedBazelTargets: []string{
169 makeBazelTarget("genrule", "foo", attrNameToString{
170 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
171 "outs": `["foo.out"]`,
172 "srcs": `["foo.in"]`,
173 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400174 "//other:foo.tool",
175 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500176 ]`,
177 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400178 },
179 filesystem: otherGenruleBp,
180 },
181 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500182 description: "genrule using $(locations) label should substitute first tool label automatically",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400183 blueprint: `genrule {
184 name: "foo",
185 out: ["foo.out"],
186 srcs: ["foo.in"],
187 tools: [":foo.tool", ":other.tool"],
188 cmd: "$(locations) -s $(out) $(in)",
189 bazel_module: { bp2build_available: true },
190}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500191 expectedBazelTargets: []string{
192 makeBazelTarget("genrule", "foo", attrNameToString{
193 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
194 "outs": `["foo.out"]`,
195 "srcs": `["foo.in"]`,
196 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400197 "//other:foo.tool",
198 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500199 ]`,
200 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400201 },
202 filesystem: otherGenruleBp,
203 },
204 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500205 description: "genrule without tools or tool_files can convert successfully",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400206 blueprint: `genrule {
207 name: "foo",
208 out: ["foo.out"],
209 srcs: ["foo.in"],
210 cmd: "cp $(in) $(out)",
211 bazel_module: { bp2build_available: true },
212}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500213 expectedBazelTargets: []string{
214 makeBazelTarget("genrule", "foo", attrNameToString{
215 "cmd": `"cp $(SRCS) $(OUTS)"`,
216 "outs": `["foo.out"]`,
217 "srcs": `["foo.in"]`,
218 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400219 },
220 },
221 }
222
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400223 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500224 t.Run(testCase.description, func(t *testing.T) {
225 runGenruleTestCase(t, testCase)
226 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400227 }
228}
229
230func TestBp2BuildInlinesDefaults(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500231 testCases := []bp2buildTestCase{
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400232 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500233 description: "genrule applies properties from a genrule_defaults dependency if not specified",
234 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400235 name: "gen_defaults",
236 cmd: "do-something $(in) $(out)",
237}
238genrule {
239 name: "gen",
240 out: ["out"],
241 srcs: ["in1"],
242 defaults: ["gen_defaults"],
243 bazel_module: { bp2build_available: true },
244}
245`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500246 expectedBazelTargets: []string{
247 makeBazelTarget("genrule", "gen", attrNameToString{
248 "cmd": `"do-something $(SRCS) $(OUTS)"`,
249 "outs": `["out"]`,
250 "srcs": `["in1"]`,
251 }),
252 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400253 },
254 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500255 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
256 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400257 name: "gen_defaults",
258 out: ["out-from-defaults"],
259 srcs: ["in-from-defaults"],
260 cmd: "cmd-from-defaults",
261}
262genrule {
263 name: "gen",
264 out: ["out"],
265 srcs: ["in1"],
266 defaults: ["gen_defaults"],
267 cmd: "do-something $(in) $(out)",
268 bazel_module: { bp2build_available: true },
269}
270`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500271 expectedBazelTargets: []string{
272 makeBazelTarget("genrule", "gen", attrNameToString{
273 "cmd": `"do-something $(SRCS) $(OUTS)"`,
274 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400275 "out-from-defaults",
276 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500277 ]`,
278 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400279 "in-from-defaults",
280 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500281 ]`,
282 }),
283 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400284 },
285 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500286 description: "genrule applies properties from list of genrule_defaults",
287 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400288 name: "gen_defaults1",
289 cmd: "cp $(in) $(out)",
290}
291
292genrule_defaults {
293 name: "gen_defaults2",
294 srcs: ["in1"],
295}
296
297genrule {
298 name: "gen",
299 out: ["out"],
300 defaults: ["gen_defaults1", "gen_defaults2"],
301 bazel_module: { bp2build_available: true },
302}
303`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500304 expectedBazelTargets: []string{
305 makeBazelTarget("genrule", "gen", attrNameToString{
306 "cmd": `"cp $(SRCS) $(OUTS)"`,
307 "outs": `["out"]`,
308 "srcs": `["in1"]`,
309 }),
310 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400311 },
312 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500313 description: "genrule applies properties from genrule_defaults transitively",
314 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400315 name: "gen_defaults1",
316 defaults: ["gen_defaults2"],
317 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
318}
319
320genrule_defaults {
321 name: "gen_defaults2",
322 defaults: ["gen_defaults3"],
323 cmd: "cmd2 $(in) $(out)",
324 out: ["out-from-2"],
325 srcs: ["in1"],
326}
327
328genrule_defaults {
329 name: "gen_defaults3",
330 out: ["out-from-3"],
331 srcs: ["srcs-from-3"],
332}
333
334genrule {
335 name: "gen",
336 out: ["out"],
337 defaults: ["gen_defaults1"],
338 bazel_module: { bp2build_available: true },
339}
340`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500341 expectedBazelTargets: []string{
342 makeBazelTarget("genrule", "gen", attrNameToString{
343 "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
344 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400345 "out-from-3",
346 "out-from-2",
347 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500348 ]`,
349 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400350 "srcs-from-3",
351 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500352 ]`,
353 }),
354 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400355 },
356 }
357
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400358 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500359 t.Run(testCase.description, func(t *testing.T) {
360 runGenruleTestCase(t, testCase)
361 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400362 }
363}