blob: 5976666942b7da193bd2cd0f8e9d9f66f78feace [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
31 (&tc).moduleTypeUnderTestBp2BuildMutator = genrule.GenruleBp2Build
32 runBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
33}
34
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040035func TestGenruleBp2Build(t *testing.T) {
36 otherGenruleBp := map[string]string{
37 "other/Android.bp": `genrule {
38 name: "foo.tool",
39 out: ["foo_tool.out"],
40 srcs: ["foo_tool.in"],
41 cmd: "cp $(in) $(out)",
42}
43genrule {
44 name: "other.tool",
45 out: ["other_tool.out"],
46 srcs: ["other_tool.in"],
47 cmd: "cp $(in) $(out)",
48}`,
49 }
50
51 testCases := []bp2buildTestCase{
52 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050053 description: "genrule with command line variable replacements",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040054 blueprint: `genrule {
55 name: "foo.tool",
56 out: ["foo_tool.out"],
57 srcs: ["foo_tool.in"],
58 cmd: "cp $(in) $(out)",
59 bazel_module: { bp2build_available: true },
60}
61
62genrule {
63 name: "foo",
64 out: ["foo.out"],
65 srcs: ["foo.in"],
66 tools: [":foo.tool"],
67 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
68 bazel_module: { bp2build_available: true },
69}`,
70 expectedBazelTargets: []string{
Liz Kammer78cfdaa2021-11-08 12:56:31 -050071 makeBazelTarget("genrule", "foo", attrNameToString{
72 "cmd": `"$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)"`,
73 "outs": `["foo.out"]`,
74 "srcs": `["foo.in"]`,
75 "tools": `[":foo.tool"]`,
76 }),
77 makeBazelTarget("genrule", "foo.tool", attrNameToString{
78 "cmd": `"cp $(SRCS) $(OUTS)"`,
79 "outs": `["foo_tool.out"]`,
80 "srcs": `["foo_tool.in"]`,
81 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040082 },
83 },
84 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050085 description: "genrule using $(locations :label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040086 blueprint: `genrule {
87 name: "foo.tools",
88 out: ["foo_tool.out", "foo_tool2.out"],
89 srcs: ["foo_tool.in"],
90 cmd: "cp $(in) $(out)",
91 bazel_module: { bp2build_available: true },
92}
93
94genrule {
95 name: "foo",
96 out: ["foo.out"],
97 srcs: ["foo.in"],
98 tools: [":foo.tools"],
99 cmd: "$(locations :foo.tools) -s $(out) $(in)",
100 bazel_module: { bp2build_available: true },
101}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500102 expectedBazelTargets: []string{
103 makeBazelTarget("genrule", "foo", attrNameToString{
104 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
105 "outs": `["foo.out"]`,
106 "srcs": `["foo.in"]`,
107 "tools": `[":foo.tools"]`,
108 }),
109 makeBazelTarget("genrule", "foo.tools", attrNameToString{
110 "cmd": `"cp $(SRCS) $(OUTS)"`,
111 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400112 "foo_tool.out",
113 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500114 ]`,
115 "srcs": `["foo_tool.in"]`,
116 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400117 },
118 },
119 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500120 description: "genrule using $(locations //absolute:label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400121 blueprint: `genrule {
122 name: "foo",
123 out: ["foo.out"],
124 srcs: ["foo.in"],
125 tool_files: [":foo.tool"],
126 cmd: "$(locations :foo.tool) -s $(out) $(in)",
127 bazel_module: { bp2build_available: true },
128}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500129 expectedBazelTargets: []string{
130 makeBazelTarget("genrule", "foo", attrNameToString{
131 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
132 "outs": `["foo.out"]`,
133 "srcs": `["foo.in"]`,
134 "tools": `["//other:foo.tool"]`,
135 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400136 },
137 filesystem: otherGenruleBp,
138 },
139 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500140 description: "genrule srcs using $(locations //absolute:label)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400141 blueprint: `genrule {
142 name: "foo",
143 out: ["foo.out"],
144 srcs: [":other.tool"],
145 tool_files: [":foo.tool"],
146 cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)",
147 bazel_module: { bp2build_available: true },
148}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500149 expectedBazelTargets: []string{
150 makeBazelTarget("genrule", "foo", attrNameToString{
151 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`,
152 "outs": `["foo.out"]`,
153 "srcs": `["//other:other.tool"]`,
154 "tools": `["//other:foo.tool"]`,
155 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400156 },
157 filesystem: otherGenruleBp,
158 },
159 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500160 description: "genrule using $(location) label should substitute first tool label automatically",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400161 blueprint: `genrule {
162 name: "foo",
163 out: ["foo.out"],
164 srcs: ["foo.in"],
165 tool_files: [":foo.tool", ":other.tool"],
166 cmd: "$(location) -s $(out) $(in)",
167 bazel_module: { bp2build_available: true },
168}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500169 expectedBazelTargets: []string{
170 makeBazelTarget("genrule", "foo", attrNameToString{
171 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
172 "outs": `["foo.out"]`,
173 "srcs": `["foo.in"]`,
174 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400175 "//other:foo.tool",
176 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500177 ]`,
178 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400179 },
180 filesystem: otherGenruleBp,
181 },
182 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500183 description: "genrule using $(locations) label should substitute first tool label automatically",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400184 blueprint: `genrule {
185 name: "foo",
186 out: ["foo.out"],
187 srcs: ["foo.in"],
188 tools: [":foo.tool", ":other.tool"],
189 cmd: "$(locations) -s $(out) $(in)",
190 bazel_module: { bp2build_available: true },
191}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500192 expectedBazelTargets: []string{
193 makeBazelTarget("genrule", "foo", attrNameToString{
194 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
195 "outs": `["foo.out"]`,
196 "srcs": `["foo.in"]`,
197 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400198 "//other:foo.tool",
199 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500200 ]`,
201 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400202 },
203 filesystem: otherGenruleBp,
204 },
205 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500206 description: "genrule without tools or tool_files can convert successfully",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400207 blueprint: `genrule {
208 name: "foo",
209 out: ["foo.out"],
210 srcs: ["foo.in"],
211 cmd: "cp $(in) $(out)",
212 bazel_module: { bp2build_available: true },
213}`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500214 expectedBazelTargets: []string{
215 makeBazelTarget("genrule", "foo", attrNameToString{
216 "cmd": `"cp $(SRCS) $(OUTS)"`,
217 "outs": `["foo.out"]`,
218 "srcs": `["foo.in"]`,
219 }),
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400220 },
221 },
222 }
223
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400224 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500225 t.Run(testCase.description, func(t *testing.T) {
226 runGenruleTestCase(t, testCase)
227 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400228 }
229}
230
231func TestBp2BuildInlinesDefaults(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500232 testCases := []bp2buildTestCase{
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400233 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500234 description: "genrule applies properties from a genrule_defaults dependency if not specified",
235 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400236 name: "gen_defaults",
237 cmd: "do-something $(in) $(out)",
238}
239genrule {
240 name: "gen",
241 out: ["out"],
242 srcs: ["in1"],
243 defaults: ["gen_defaults"],
244 bazel_module: { bp2build_available: true },
245}
246`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500247 expectedBazelTargets: []string{
248 makeBazelTarget("genrule", "gen", attrNameToString{
249 "cmd": `"do-something $(SRCS) $(OUTS)"`,
250 "outs": `["out"]`,
251 "srcs": `["in1"]`,
252 }),
253 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400254 },
255 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500256 description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
257 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400258 name: "gen_defaults",
259 out: ["out-from-defaults"],
260 srcs: ["in-from-defaults"],
261 cmd: "cmd-from-defaults",
262}
263genrule {
264 name: "gen",
265 out: ["out"],
266 srcs: ["in1"],
267 defaults: ["gen_defaults"],
268 cmd: "do-something $(in) $(out)",
269 bazel_module: { bp2build_available: true },
270}
271`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500272 expectedBazelTargets: []string{
273 makeBazelTarget("genrule", "gen", attrNameToString{
274 "cmd": `"do-something $(SRCS) $(OUTS)"`,
275 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400276 "out-from-defaults",
277 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500278 ]`,
279 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400280 "in-from-defaults",
281 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500282 ]`,
283 }),
284 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400285 },
286 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500287 description: "genrule applies properties from list of genrule_defaults",
288 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400289 name: "gen_defaults1",
290 cmd: "cp $(in) $(out)",
291}
292
293genrule_defaults {
294 name: "gen_defaults2",
295 srcs: ["in1"],
296}
297
298genrule {
299 name: "gen",
300 out: ["out"],
301 defaults: ["gen_defaults1", "gen_defaults2"],
302 bazel_module: { bp2build_available: true },
303}
304`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500305 expectedBazelTargets: []string{
306 makeBazelTarget("genrule", "gen", attrNameToString{
307 "cmd": `"cp $(SRCS) $(OUTS)"`,
308 "outs": `["out"]`,
309 "srcs": `["in1"]`,
310 }),
311 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400312 },
313 {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500314 description: "genrule applies properties from genrule_defaults transitively",
315 blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400316 name: "gen_defaults1",
317 defaults: ["gen_defaults2"],
318 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
319}
320
321genrule_defaults {
322 name: "gen_defaults2",
323 defaults: ["gen_defaults3"],
324 cmd: "cmd2 $(in) $(out)",
325 out: ["out-from-2"],
326 srcs: ["in1"],
327}
328
329genrule_defaults {
330 name: "gen_defaults3",
331 out: ["out-from-3"],
332 srcs: ["srcs-from-3"],
333}
334
335genrule {
336 name: "gen",
337 out: ["out"],
338 defaults: ["gen_defaults1"],
339 bazel_module: { bp2build_available: true },
340}
341`,
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500342 expectedBazelTargets: []string{
343 makeBazelTarget("genrule", "gen", attrNameToString{
344 "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
345 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400346 "out-from-3",
347 "out-from-2",
348 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500349 ]`,
350 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400351 "srcs-from-3",
352 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500353 ]`,
354 }),
355 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400356 },
357 }
358
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400359 for _, testCase := range testCases {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500360 t.Run(testCase.description, func(t *testing.T) {
361 runGenruleTestCase(t, testCase)
362 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400363 }
364}