blob: 5cf4fb216a8062366ff19ba5e01617d203ab16f0 [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 (
Yu Liud6201012022-10-17 12:29:15 -070018 "fmt"
19 "testing"
20
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040021 "android/soong/android"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000022 "android/soong/cc"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040023 "android/soong/genrule"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000024 "android/soong/java"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040025)
26
Liz Kammer78cfdaa2021-11-08 12:56:31 -050027func registerGenruleModuleTypes(ctx android.RegistrationContext) {
28 ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
29}
30
Sam Delmerico3177a6e2022-06-21 19:28:33 +000031func runGenruleTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050032 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000033 (&tc).ModuleTypeUnderTest = "genrule"
34 (&tc).ModuleTypeUnderTestFactory = genrule.GenRuleFactory
35 RunBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -050036}
37
Sam Delmericocd1b80f2022-01-11 21:55:46 +000038func otherGenruleBp(genruleTarget string) map[string]string {
39 return map[string]string{
40 "other/Android.bp": fmt.Sprintf(`%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040041 name: "foo.tool",
42 out: ["foo_tool.out"],
43 srcs: ["foo_tool.in"],
44 cmd: "cp $(in) $(out)",
45}
Sam Delmericocd1b80f2022-01-11 21:55:46 +000046%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040047 name: "other.tool",
48 out: ["other_tool.out"],
49 srcs: ["other_tool.in"],
50 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000051}`, genruleTarget, genruleTarget),
Liz Kammer1e753242023-06-02 19:03:06 -040052 "other/file.txt": "",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000053 }
54}
55
56func TestGenruleCliVariableReplacement(t *testing.T) {
57 testCases := []struct {
58 moduleType string
59 factory android.ModuleFactory
60 genDir string
Liz Kammerdfeb1202022-05-13 17:20:20 -040061 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +000062 }{
63 {
64 moduleType: "genrule",
65 factory: genrule.GenRuleFactory,
Vinh Tranf19b6582022-09-21 17:01:49 -040066 genDir: "$(RULEDIR)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000067 },
68 {
69 moduleType: "cc_genrule",
70 factory: cc.GenRuleFactory,
71 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040072 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000073 },
74 {
75 moduleType: "java_genrule",
76 factory: java.GenRuleFactory,
77 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040078 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000079 },
80 {
81 moduleType: "java_genrule_host",
82 factory: java.GenRuleFactoryHost,
83 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040084 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000085 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040086 }
87
Sam Delmericocd1b80f2022-01-11 21:55:46 +000088 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040089 name: "foo.tool",
90 out: ["foo_tool.out"],
91 srcs: ["foo_tool.in"],
92 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000093 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040094}
95
Sam Delmericocd1b80f2022-01-11 21:55:46 +000096%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040097 name: "foo",
98 out: ["foo.out"],
99 srcs: ["foo.in"],
100 tools: [":foo.tool"],
101 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
102 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000103}`
104
105 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000106 moduleAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000107 "cmd": fmt.Sprintf(`"$(location :foo.tool) --genDir=%s arg $(SRCS) $(OUTS)"`, tc.genDir),
108 "outs": `["foo.out"]`,
109 "srcs": `["foo.in"]`,
110 "tools": `[":foo.tool"]`,
111 }
112
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000113 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400114 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000115 }
116
117 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000118 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
119 Bp2buildTestCase{
120 ModuleTypeUnderTest: tc.moduleType,
121 ModuleTypeUnderTestFactory: tc.factory,
122 Blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType),
123 ExpectedBazelTargets: expectedBazelTargets,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000124 })
125 })
126 }
127}
128
129func TestGenruleLocationsLabel(t *testing.T) {
130 testCases := []struct {
131 moduleType string
132 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400133 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000134 }{
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,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400142 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000143 },
144 {
145 moduleType: "java_genrule",
146 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400147 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000148 },
149 {
150 moduleType: "java_genrule_host",
151 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400152 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000153 },
154 }
155
156 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400157 name: "foo.tools",
158 out: ["foo_tool.out", "foo_tool2.out"],
159 srcs: ["foo_tool.in"],
160 cmd: "cp $(in) $(out)",
161 bazel_module: { bp2build_available: true },
162}
163
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000164%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400165 name: "foo",
166 out: ["foo.out"],
167 srcs: ["foo.in"],
168 tools: [":foo.tools"],
169 cmd: "$(locations :foo.tools) -s $(out) $(in)",
170 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000171}`
172
Sam Delmerico75539d62022-01-31 14:37:29 +0000173 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000174 fooAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000175 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
176 "outs": `["foo.out"]`,
177 "srcs": `["foo.in"]`,
178 "tools": `[":foo.tools"]`,
179 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000180 fooToolsAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000181 "cmd": `"cp $(SRCS) $(OUTS)"`,
182 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400183 "foo_tool.out",
184 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500185 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000186 "srcs": `["foo_tool.in"]`,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000187 }
188
Sam Delmerico75539d62022-01-31 14:37:29 +0000189 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400190 makeBazelTargetHostOrDevice("genrule", "foo", fooAttrs, tc.hod),
191 makeBazelTargetHostOrDevice("genrule", "foo.tools", fooToolsAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000192 }
193
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000194 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000195 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
196 Bp2buildTestCase{
197 ModuleTypeUnderTest: tc.moduleType,
198 ModuleTypeUnderTestFactory: tc.factory,
199 Blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType),
200 ExpectedBazelTargets: expectedBazelTargets,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000201 })
202 })
203 }
204}
205
206func TestGenruleLocationsAbsoluteLabel(t *testing.T) {
207 testCases := []struct {
208 moduleType string
209 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400210 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000211 }{
212 {
213 moduleType: "genrule",
214 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400215 },
216 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000217 moduleType: "cc_genrule",
218 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400219 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000220 },
221 {
222 moduleType: "java_genrule",
223 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400224 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000225 },
226 {
227 moduleType: "java_genrule_host",
228 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400229 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000230 },
231 }
232
233 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400234 name: "foo",
235 out: ["foo.out"],
236 srcs: ["foo.in"],
237 tool_files: [":foo.tool"],
238 cmd: "$(locations :foo.tool) -s $(out) $(in)",
239 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000240}`
241
Sam Delmerico75539d62022-01-31 14:37:29 +0000242 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000243 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000244 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
245 "outs": `["foo.out"]`,
246 "srcs": `["foo.in"]`,
247 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000248 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000249
Sam Delmerico75539d62022-01-31 14:37:29 +0000250 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400251 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000252 }
253
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000254 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000255 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
256 Bp2buildTestCase{
257 ModuleTypeUnderTest: tc.moduleType,
258 ModuleTypeUnderTestFactory: tc.factory,
259 Blueprint: fmt.Sprintf(bp, tc.moduleType),
260 ExpectedBazelTargets: expectedBazelTargets,
261 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000262 })
263 })
264 }
265}
266
267func TestGenruleSrcsLocationsAbsoluteLabel(t *testing.T) {
268 testCases := []struct {
269 moduleType string
270 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400271 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000272 }{
273 {
274 moduleType: "genrule",
275 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400276 },
277 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000278 moduleType: "cc_genrule",
279 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400280 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000281 },
282 {
283 moduleType: "java_genrule",
284 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400285 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000286 },
287 {
288 moduleType: "java_genrule_host",
289 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400290 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000291 },
292 }
293
294 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400295 name: "foo",
296 out: ["foo.out"],
Liz Kammer1e753242023-06-02 19:03:06 -0400297 srcs: [":other.tool", "other/file.txt",],
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400298 tool_files: [":foo.tool"],
Liz Kammer1e753242023-06-02 19:03:06 -0400299 cmd: "$(locations :foo.tool) $(location other/file.txt) -s $(out) $(location :other.tool)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400300 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000301}`
302
Sam Delmerico75539d62022-01-31 14:37:29 +0000303 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000304 moduleAttrs := AttrNameToString{
Liz Kammer1e753242023-06-02 19:03:06 -0400305 "cmd": `"$(locations //other:foo.tool) $(location //other:file.txt) -s $(OUTS) $(location //other:other.tool)"`,
306 "outs": `["foo.out"]`,
307 "srcs": `[
308 "//other:other.tool",
309 "//other:file.txt",
310 ]`,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000311 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000312 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000313
Sam Delmerico75539d62022-01-31 14:37:29 +0000314 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400315 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000316 }
317
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000318 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000319 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
320 Bp2buildTestCase{
321 ModuleTypeUnderTest: tc.moduleType,
322 ModuleTypeUnderTestFactory: tc.factory,
323 Blueprint: fmt.Sprintf(bp, tc.moduleType),
324 ExpectedBazelTargets: expectedBazelTargets,
325 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000326 })
327 })
328 }
329}
330
331func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) {
332 testCases := []struct {
333 moduleType string
334 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400335 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000336 }{
337 {
338 moduleType: "genrule",
339 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400340 },
341 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000342 moduleType: "cc_genrule",
343 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400344 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000345 },
346 {
347 moduleType: "java_genrule",
348 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400349 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000350 },
351 {
352 moduleType: "java_genrule_host",
353 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400354 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000355 },
356 }
357
358 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400359 name: "foo",
360 out: ["foo.out"],
361 srcs: ["foo.in"],
362 tool_files: [":foo.tool", ":other.tool"],
363 cmd: "$(location) -s $(out) $(in)",
364 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000365}`
366
Sam Delmerico75539d62022-01-31 14:37:29 +0000367 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000368 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000369 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
370 "outs": `["foo.out"]`,
371 "srcs": `["foo.in"]`,
372 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400373 "//other:foo.tool",
374 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500375 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000376 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000377
Sam Delmerico75539d62022-01-31 14:37:29 +0000378 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400379 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000380 }
381
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000382 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000383 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
384 Bp2buildTestCase{
385 ModuleTypeUnderTest: tc.moduleType,
386 ModuleTypeUnderTestFactory: tc.factory,
387 Blueprint: fmt.Sprintf(bp, tc.moduleType),
388 ExpectedBazelTargets: expectedBazelTargets,
389 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000390 })
391 })
392 }
393}
394
395func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) {
396 testCases := []struct {
397 moduleType string
398 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400399 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000400 }{
401 {
402 moduleType: "genrule",
403 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400404 },
405 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000406 moduleType: "cc_genrule",
407 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400408 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000409 },
410 {
411 moduleType: "java_genrule",
412 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400413 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000414 },
415 {
416 moduleType: "java_genrule_host",
417 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400418 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000419 },
420 }
421
422 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400423 name: "foo",
424 out: ["foo.out"],
425 srcs: ["foo.in"],
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000426 tool_files: [":foo.tool", ":other.tool"],
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400427 cmd: "$(locations) -s $(out) $(in)",
428 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000429}`
430
Sam Delmerico75539d62022-01-31 14:37:29 +0000431 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000432 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000433 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
434 "outs": `["foo.out"]`,
435 "srcs": `["foo.in"]`,
436 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400437 "//other:foo.tool",
438 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500439 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000440 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000441
Sam Delmerico75539d62022-01-31 14:37:29 +0000442 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400443 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000444 }
445
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000446 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000447 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
448 Bp2buildTestCase{
449 ModuleTypeUnderTest: tc.moduleType,
450 ModuleTypeUnderTestFactory: tc.factory,
451 Blueprint: fmt.Sprintf(bp, tc.moduleType),
452 ExpectedBazelTargets: expectedBazelTargets,
453 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000454 })
455 })
456 }
457}
458
459func TestGenruleWithoutToolsOrToolFiles(t *testing.T) {
460 testCases := []struct {
461 moduleType string
462 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400463 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000464 }{
465 {
466 moduleType: "genrule",
467 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400468 },
469 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000470 moduleType: "cc_genrule",
471 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400472 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000473 },
474 {
475 moduleType: "java_genrule",
476 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400477 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000478 },
479 {
480 moduleType: "java_genrule_host",
481 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400482 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000483 },
484 }
485
486 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400487 name: "foo",
488 out: ["foo.out"],
489 srcs: ["foo.in"],
490 cmd: "cp $(in) $(out)",
491 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000492}`
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400493
Sam Delmerico75539d62022-01-31 14:37:29 +0000494 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000495 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000496 "cmd": `"cp $(SRCS) $(OUTS)"`,
497 "outs": `["foo.out"]`,
498 "srcs": `["foo.in"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000499 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000500
Sam Delmerico75539d62022-01-31 14:37:29 +0000501 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400502 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000503 }
504
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000505 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000506 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
507 Bp2buildTestCase{
508 ModuleTypeUnderTest: tc.moduleType,
509 ModuleTypeUnderTestFactory: tc.factory,
510 Blueprint: fmt.Sprintf(bp, tc.moduleType),
511 ExpectedBazelTargets: expectedBazelTargets,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000512 })
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500513 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400514 }
515}
516
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000517func TestGenruleBp2BuildInlinesDefaults(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000518 testCases := []Bp2buildTestCase{
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400519 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000520 Description: "genrule applies properties from a genrule_defaults dependency if not specified",
521 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400522 name: "gen_defaults",
523 cmd: "do-something $(in) $(out)",
524}
525genrule {
526 name: "gen",
527 out: ["out"],
528 srcs: ["in1"],
529 defaults: ["gen_defaults"],
530 bazel_module: { bp2build_available: true },
531}
532`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000533 ExpectedBazelTargets: []string{
534 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500535 "cmd": `"do-something $(SRCS) $(OUTS)"`,
536 "outs": `["out"]`,
537 "srcs": `["in1"]`,
538 }),
539 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400540 },
541 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000542 Description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
543 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400544 name: "gen_defaults",
545 out: ["out-from-defaults"],
546 srcs: ["in-from-defaults"],
547 cmd: "cmd-from-defaults",
548}
549genrule {
550 name: "gen",
551 out: ["out"],
552 srcs: ["in1"],
553 defaults: ["gen_defaults"],
554 cmd: "do-something $(in) $(out)",
555 bazel_module: { bp2build_available: true },
556}
557`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000558 ExpectedBazelTargets: []string{
559 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500560 "cmd": `"do-something $(SRCS) $(OUTS)"`,
561 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400562 "out-from-defaults",
563 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500564 ]`,
565 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400566 "in-from-defaults",
567 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500568 ]`,
569 }),
570 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400571 },
572 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000573 Description: "genrule applies properties from list of genrule_defaults",
574 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400575 name: "gen_defaults1",
576 cmd: "cp $(in) $(out)",
577}
578
579genrule_defaults {
580 name: "gen_defaults2",
581 srcs: ["in1"],
582}
583
584genrule {
585 name: "gen",
586 out: ["out"],
587 defaults: ["gen_defaults1", "gen_defaults2"],
588 bazel_module: { bp2build_available: true },
589}
590`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000591 ExpectedBazelTargets: []string{
592 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500593 "cmd": `"cp $(SRCS) $(OUTS)"`,
594 "outs": `["out"]`,
595 "srcs": `["in1"]`,
596 }),
597 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400598 },
599 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000600 Description: "genrule applies properties from genrule_defaults transitively",
601 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400602 name: "gen_defaults1",
603 defaults: ["gen_defaults2"],
604 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
605}
606
607genrule_defaults {
608 name: "gen_defaults2",
609 defaults: ["gen_defaults3"],
610 cmd: "cmd2 $(in) $(out)",
611 out: ["out-from-2"],
612 srcs: ["in1"],
613}
614
615genrule_defaults {
616 name: "gen_defaults3",
617 out: ["out-from-3"],
618 srcs: ["srcs-from-3"],
619}
620
621genrule {
622 name: "gen",
623 out: ["out"],
624 defaults: ["gen_defaults1"],
625 bazel_module: { bp2build_available: true },
626}
627`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000628 ExpectedBazelTargets: []string{
629 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500630 "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
631 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400632 "out-from-3",
633 "out-from-2",
634 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500635 ]`,
636 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400637 "srcs-from-3",
638 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500639 ]`,
640 }),
641 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400642 },
643 }
644
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400645 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000646 t.Run(testCase.Description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500647 runGenruleTestCase(t, testCase)
648 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400649 }
650}
Yu Liud6201012022-10-17 12:29:15 -0700651
652func TestCcGenruleArchAndExcludeSrcs(t *testing.T) {
653 name := "cc_genrule with arch"
654 bp := `
655 cc_genrule {
656 name: "foo",
657 srcs: [
658 "foo1.in",
659 "foo2.in",
660 ],
661 exclude_srcs: ["foo2.in"],
662 arch: {
663 arm: {
664 srcs: [
665 "foo1_arch.in",
666 "foo2_arch.in",
667 ],
668 exclude_srcs: ["foo2_arch.in"],
669 },
670 },
671 cmd: "cat $(in) > $(out)",
672 bazel_module: { bp2build_available: true },
673 }`
674
675 expectedBazelAttrs := AttrNameToString{
676 "srcs": `["foo1.in"] + select({
677 "//build/bazel/platforms/arch:arm": ["foo1_arch.in"],
678 "//conditions:default": [],
679 })`,
680 "cmd": `"cat $(SRCS) > $(OUTS)"`,
681 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
682 }
683
684 expectedBazelTargets := []string{
685 MakeBazelTargetNoRestrictions("genrule", "foo", expectedBazelAttrs),
686 }
687
688 t.Run(name, func(t *testing.T) {
689 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
690 Bp2buildTestCase{
691 ModuleTypeUnderTest: "cc_genrule",
692 ModuleTypeUnderTestFactory: cc.GenRuleFactory,
693 Blueprint: bp,
694 ExpectedBazelTargets: expectedBazelTargets,
695 })
696 })
697}