blob: 5ca8bd7bad1e7d47789373b4fc7770c9fa619218 [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"
Liz Kammer0db0e342023-07-18 11:39:30 -040019 "path/filepath"
Yu Liud6201012022-10-17 12:29:15 -070020 "testing"
21
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040022 "android/soong/android"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000023 "android/soong/cc"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040024 "android/soong/genrule"
Sam Delmericocd1b80f2022-01-11 21:55:46 +000025 "android/soong/java"
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040026)
27
Liz Kammer78cfdaa2021-11-08 12:56:31 -050028func registerGenruleModuleTypes(ctx android.RegistrationContext) {
29 ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() })
30}
31
Sam Delmerico3177a6e2022-06-21 19:28:33 +000032func runGenruleTestCase(t *testing.T, tc Bp2buildTestCase) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -050033 t.Helper()
Sam Delmerico3177a6e2022-06-21 19:28:33 +000034 (&tc).ModuleTypeUnderTest = "genrule"
35 (&tc).ModuleTypeUnderTestFactory = genrule.GenRuleFactory
36 RunBp2BuildTestCase(t, registerGenruleModuleTypes, tc)
Liz Kammer78cfdaa2021-11-08 12:56:31 -050037}
38
Sam Delmericocd1b80f2022-01-11 21:55:46 +000039func otherGenruleBp(genruleTarget string) map[string]string {
40 return map[string]string{
41 "other/Android.bp": fmt.Sprintf(`%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040042 name: "foo.tool",
43 out: ["foo_tool.out"],
44 srcs: ["foo_tool.in"],
45 cmd: "cp $(in) $(out)",
46}
Sam Delmericocd1b80f2022-01-11 21:55:46 +000047%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040048 name: "other.tool",
49 out: ["other_tool.out"],
50 srcs: ["other_tool.in"],
51 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000052}`, genruleTarget, genruleTarget),
Liz Kammer1e753242023-06-02 19:03:06 -040053 "other/file.txt": "",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000054 }
55}
56
57func TestGenruleCliVariableReplacement(t *testing.T) {
58 testCases := []struct {
59 moduleType string
60 factory android.ModuleFactory
61 genDir string
Liz Kammerdfeb1202022-05-13 17:20:20 -040062 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +000063 }{
64 {
65 moduleType: "genrule",
66 factory: genrule.GenRuleFactory,
Vinh Tranf19b6582022-09-21 17:01:49 -040067 genDir: "$(RULEDIR)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000068 },
69 {
70 moduleType: "cc_genrule",
71 factory: cc.GenRuleFactory,
72 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040073 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000074 },
75 {
76 moduleType: "java_genrule",
77 factory: java.GenRuleFactory,
78 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040079 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000080 },
81 {
82 moduleType: "java_genrule_host",
83 factory: java.GenRuleFactoryHost,
84 genDir: "$(RULEDIR)",
Liz Kammerdfeb1202022-05-13 17:20:20 -040085 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +000086 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040087 }
88
Sam Delmericocd1b80f2022-01-11 21:55:46 +000089 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040090 name: "foo.tool",
91 out: ["foo_tool.out"],
92 srcs: ["foo_tool.in"],
93 cmd: "cp $(in) $(out)",
Sam Delmericocd1b80f2022-01-11 21:55:46 +000094 bazel_module: { bp2build_available: false },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040095}
96
Sam Delmericocd1b80f2022-01-11 21:55:46 +000097%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -040098 name: "foo",
99 out: ["foo.out"],
100 srcs: ["foo.in"],
101 tools: [":foo.tool"],
102 cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)",
103 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000104}`
105
106 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000107 moduleAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000108 "cmd": fmt.Sprintf(`"$(location :foo.tool) --genDir=%s arg $(SRCS) $(OUTS)"`, tc.genDir),
109 "outs": `["foo.out"]`,
110 "srcs": `["foo.in"]`,
111 "tools": `[":foo.tool"]`,
112 }
113
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000114 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400115 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000116 }
117
118 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000119 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,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000125 })
126 })
127 }
128}
129
130func TestGenruleLocationsLabel(t *testing.T) {
131 testCases := []struct {
132 moduleType string
133 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400134 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000135 }{
136 {
137 moduleType: "genrule",
138 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400139 },
140 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000141 moduleType: "cc_genrule",
142 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400143 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000144 },
145 {
146 moduleType: "java_genrule",
147 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400148 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000149 },
150 {
151 moduleType: "java_genrule_host",
152 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400153 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000154 },
155 }
156
157 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400158 name: "foo.tools",
159 out: ["foo_tool.out", "foo_tool2.out"],
160 srcs: ["foo_tool.in"],
161 cmd: "cp $(in) $(out)",
162 bazel_module: { bp2build_available: true },
163}
164
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000165%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400166 name: "foo",
167 out: ["foo.out"],
168 srcs: ["foo.in"],
169 tools: [":foo.tools"],
170 cmd: "$(locations :foo.tools) -s $(out) $(in)",
171 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000172}`
173
Sam Delmerico75539d62022-01-31 14:37:29 +0000174 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000175 fooAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000176 "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`,
177 "outs": `["foo.out"]`,
178 "srcs": `["foo.in"]`,
179 "tools": `[":foo.tools"]`,
180 }
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000181 fooToolsAttrs := AttrNameToString{
Sam Delmerico75539d62022-01-31 14:37:29 +0000182 "cmd": `"cp $(SRCS) $(OUTS)"`,
183 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400184 "foo_tool.out",
185 "foo_tool2.out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500186 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000187 "srcs": `["foo_tool.in"]`,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000188 }
189
Sam Delmerico75539d62022-01-31 14:37:29 +0000190 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400191 makeBazelTargetHostOrDevice("genrule", "foo", fooAttrs, tc.hod),
192 makeBazelTargetHostOrDevice("genrule", "foo.tools", fooToolsAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000193 }
194
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000195 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000196 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
197 Bp2buildTestCase{
198 ModuleTypeUnderTest: tc.moduleType,
199 ModuleTypeUnderTestFactory: tc.factory,
200 Blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType),
201 ExpectedBazelTargets: expectedBazelTargets,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000202 })
203 })
204 }
205}
206
207func TestGenruleLocationsAbsoluteLabel(t *testing.T) {
208 testCases := []struct {
209 moduleType string
210 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400211 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000212 }{
213 {
214 moduleType: "genrule",
215 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400216 },
217 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000218 moduleType: "cc_genrule",
219 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400220 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000221 },
222 {
223 moduleType: "java_genrule",
224 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400225 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000226 },
227 {
228 moduleType: "java_genrule_host",
229 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400230 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000231 },
232 }
233
234 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400235 name: "foo",
236 out: ["foo.out"],
237 srcs: ["foo.in"],
238 tool_files: [":foo.tool"],
239 cmd: "$(locations :foo.tool) -s $(out) $(in)",
240 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000241}`
242
Sam Delmerico75539d62022-01-31 14:37:29 +0000243 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000244 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000245 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
246 "outs": `["foo.out"]`,
247 "srcs": `["foo.in"]`,
248 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000249 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000250
Sam Delmerico75539d62022-01-31 14:37:29 +0000251 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400252 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000253 }
254
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000255 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000256 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
257 Bp2buildTestCase{
258 ModuleTypeUnderTest: tc.moduleType,
259 ModuleTypeUnderTestFactory: tc.factory,
260 Blueprint: fmt.Sprintf(bp, tc.moduleType),
261 ExpectedBazelTargets: expectedBazelTargets,
262 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000263 })
264 })
265 }
266}
267
268func TestGenruleSrcsLocationsAbsoluteLabel(t *testing.T) {
269 testCases := []struct {
270 moduleType string
271 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400272 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000273 }{
274 {
275 moduleType: "genrule",
276 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400277 },
278 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000279 moduleType: "cc_genrule",
280 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400281 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000282 },
283 {
284 moduleType: "java_genrule",
285 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400286 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000287 },
288 {
289 moduleType: "java_genrule_host",
290 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400291 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000292 },
293 }
294
295 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400296 name: "foo",
297 out: ["foo.out"],
Liz Kammer1e753242023-06-02 19:03:06 -0400298 srcs: [":other.tool", "other/file.txt",],
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400299 tool_files: [":foo.tool"],
Liz Kammer1e753242023-06-02 19:03:06 -0400300 cmd: "$(locations :foo.tool) $(location other/file.txt) -s $(out) $(location :other.tool)",
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400301 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000302}`
303
Sam Delmerico75539d62022-01-31 14:37:29 +0000304 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000305 moduleAttrs := AttrNameToString{
Liz Kammer1e753242023-06-02 19:03:06 -0400306 "cmd": `"$(locations //other:foo.tool) $(location //other:file.txt) -s $(OUTS) $(location //other:other.tool)"`,
307 "outs": `["foo.out"]`,
308 "srcs": `[
309 "//other:other.tool",
310 "//other:file.txt",
311 ]`,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000312 "tools": `["//other:foo.tool"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000313 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000314
Sam Delmerico75539d62022-01-31 14:37:29 +0000315 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400316 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000317 }
318
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000319 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000320 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
321 Bp2buildTestCase{
322 ModuleTypeUnderTest: tc.moduleType,
323 ModuleTypeUnderTestFactory: tc.factory,
324 Blueprint: fmt.Sprintf(bp, tc.moduleType),
325 ExpectedBazelTargets: expectedBazelTargets,
326 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000327 })
328 })
329 }
330}
331
332func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) {
333 testCases := []struct {
334 moduleType string
335 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400336 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000337 }{
338 {
339 moduleType: "genrule",
340 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400341 },
342 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000343 moduleType: "cc_genrule",
344 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400345 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000346 },
347 {
348 moduleType: "java_genrule",
349 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400350 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000351 },
352 {
353 moduleType: "java_genrule_host",
354 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400355 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000356 },
357 }
358
359 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400360 name: "foo",
361 out: ["foo.out"],
362 srcs: ["foo.in"],
363 tool_files: [":foo.tool", ":other.tool"],
364 cmd: "$(location) -s $(out) $(in)",
365 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000366}`
367
Sam Delmerico75539d62022-01-31 14:37:29 +0000368 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000369 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000370 "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`,
371 "outs": `["foo.out"]`,
372 "srcs": `["foo.in"]`,
373 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400374 "//other:foo.tool",
375 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500376 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000377 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000378
Sam Delmerico75539d62022-01-31 14:37:29 +0000379 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400380 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000381 }
382
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000383 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000384 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
385 Bp2buildTestCase{
386 ModuleTypeUnderTest: tc.moduleType,
387 ModuleTypeUnderTestFactory: tc.factory,
388 Blueprint: fmt.Sprintf(bp, tc.moduleType),
389 ExpectedBazelTargets: expectedBazelTargets,
390 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000391 })
392 })
393 }
394}
395
396func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) {
397 testCases := []struct {
398 moduleType string
399 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400400 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000401 }{
402 {
403 moduleType: "genrule",
404 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400405 },
406 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000407 moduleType: "cc_genrule",
408 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400409 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000410 },
411 {
412 moduleType: "java_genrule",
413 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400414 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000415 },
416 {
417 moduleType: "java_genrule_host",
418 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400419 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000420 },
421 }
422
423 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400424 name: "foo",
425 out: ["foo.out"],
426 srcs: ["foo.in"],
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000427 tool_files: [":foo.tool", ":other.tool"],
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400428 cmd: "$(locations) -s $(out) $(in)",
429 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000430}`
431
Sam Delmerico75539d62022-01-31 14:37:29 +0000432 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000433 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000434 "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`,
435 "outs": `["foo.out"]`,
436 "srcs": `["foo.in"]`,
437 "tools": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400438 "//other:foo.tool",
439 "//other:other.tool",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500440 ]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000441 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000442
Sam Delmerico75539d62022-01-31 14:37:29 +0000443 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400444 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000445 }
446
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000447 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000448 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
449 Bp2buildTestCase{
450 ModuleTypeUnderTest: tc.moduleType,
451 ModuleTypeUnderTestFactory: tc.factory,
452 Blueprint: fmt.Sprintf(bp, tc.moduleType),
453 ExpectedBazelTargets: expectedBazelTargets,
454 Filesystem: otherGenruleBp(tc.moduleType),
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000455 })
456 })
457 }
458}
459
460func TestGenruleWithoutToolsOrToolFiles(t *testing.T) {
461 testCases := []struct {
462 moduleType string
463 factory android.ModuleFactory
Liz Kammerdfeb1202022-05-13 17:20:20 -0400464 hod android.HostOrDeviceSupported
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000465 }{
466 {
467 moduleType: "genrule",
468 factory: genrule.GenRuleFactory,
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400469 },
470 {
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000471 moduleType: "cc_genrule",
472 factory: cc.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400473 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000474 },
475 {
476 moduleType: "java_genrule",
477 factory: java.GenRuleFactory,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400478 hod: android.DeviceSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000479 },
480 {
481 moduleType: "java_genrule_host",
482 factory: java.GenRuleFactoryHost,
Liz Kammerdfeb1202022-05-13 17:20:20 -0400483 hod: android.HostSupported,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000484 },
485 }
486
487 bp := `%s {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400488 name: "foo",
489 out: ["foo.out"],
490 srcs: ["foo.in"],
491 cmd: "cp $(in) $(out)",
492 bazel_module: { bp2build_available: true },
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000493}`
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400494
Sam Delmerico75539d62022-01-31 14:37:29 +0000495 for _, tc := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000496 moduleAttrs := AttrNameToString{
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000497 "cmd": `"cp $(SRCS) $(OUTS)"`,
498 "outs": `["foo.out"]`,
499 "srcs": `["foo.in"]`,
Sam Delmerico75539d62022-01-31 14:37:29 +0000500 }
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000501
Sam Delmerico75539d62022-01-31 14:37:29 +0000502 expectedBazelTargets := []string{
Liz Kammerdfeb1202022-05-13 17:20:20 -0400503 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
Sam Delmerico75539d62022-01-31 14:37:29 +0000504 }
505
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000506 t.Run(tc.moduleType, func(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000507 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
508 Bp2buildTestCase{
509 ModuleTypeUnderTest: tc.moduleType,
510 ModuleTypeUnderTestFactory: tc.factory,
511 Blueprint: fmt.Sprintf(bp, tc.moduleType),
512 ExpectedBazelTargets: expectedBazelTargets,
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000513 })
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500514 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400515 }
516}
517
Sam Delmericocd1b80f2022-01-11 21:55:46 +0000518func TestGenruleBp2BuildInlinesDefaults(t *testing.T) {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000519 testCases := []Bp2buildTestCase{
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400520 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000521 Description: "genrule applies properties from a genrule_defaults dependency if not specified",
522 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400523 name: "gen_defaults",
524 cmd: "do-something $(in) $(out)",
525}
526genrule {
527 name: "gen",
528 out: ["out"],
529 srcs: ["in1"],
530 defaults: ["gen_defaults"],
531 bazel_module: { bp2build_available: true },
532}
533`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000534 ExpectedBazelTargets: []string{
535 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500536 "cmd": `"do-something $(SRCS) $(OUTS)"`,
537 "outs": `["out"]`,
538 "srcs": `["in1"]`,
539 }),
540 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400541 },
542 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000543 Description: "genrule does merges properties from a genrule_defaults dependency, latest-first",
544 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400545 name: "gen_defaults",
546 out: ["out-from-defaults"],
547 srcs: ["in-from-defaults"],
548 cmd: "cmd-from-defaults",
549}
550genrule {
551 name: "gen",
552 out: ["out"],
553 srcs: ["in1"],
554 defaults: ["gen_defaults"],
555 cmd: "do-something $(in) $(out)",
556 bazel_module: { bp2build_available: true },
557}
558`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000559 ExpectedBazelTargets: []string{
560 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500561 "cmd": `"do-something $(SRCS) $(OUTS)"`,
562 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400563 "out-from-defaults",
564 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500565 ]`,
566 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400567 "in-from-defaults",
568 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500569 ]`,
570 }),
571 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400572 },
573 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000574 Description: "genrule applies properties from list of genrule_defaults",
575 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400576 name: "gen_defaults1",
577 cmd: "cp $(in) $(out)",
578}
579
580genrule_defaults {
581 name: "gen_defaults2",
582 srcs: ["in1"],
583}
584
585genrule {
586 name: "gen",
587 out: ["out"],
588 defaults: ["gen_defaults1", "gen_defaults2"],
589 bazel_module: { bp2build_available: true },
590}
591`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000592 ExpectedBazelTargets: []string{
593 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500594 "cmd": `"cp $(SRCS) $(OUTS)"`,
595 "outs": `["out"]`,
596 "srcs": `["in1"]`,
597 }),
598 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400599 },
600 {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000601 Description: "genrule applies properties from genrule_defaults transitively",
602 Blueprint: `genrule_defaults {
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400603 name: "gen_defaults1",
604 defaults: ["gen_defaults2"],
605 cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value.
606}
607
608genrule_defaults {
609 name: "gen_defaults2",
610 defaults: ["gen_defaults3"],
611 cmd: "cmd2 $(in) $(out)",
612 out: ["out-from-2"],
613 srcs: ["in1"],
614}
615
616genrule_defaults {
617 name: "gen_defaults3",
618 out: ["out-from-3"],
619 srcs: ["srcs-from-3"],
620}
621
622genrule {
623 name: "gen",
624 out: ["out"],
625 defaults: ["gen_defaults1"],
626 bazel_module: { bp2build_available: true },
627}
628`,
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000629 ExpectedBazelTargets: []string{
630 MakeBazelTargetNoRestrictions("genrule", "gen", AttrNameToString{
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500631 "cmd": `"cmd1 $(SRCS) $(OUTS)"`,
632 "outs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400633 "out-from-3",
634 "out-from-2",
635 "out",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500636 ]`,
637 "srcs": `[
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400638 "srcs-from-3",
639 "in1",
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500640 ]`,
641 }),
642 },
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400643 },
644 }
645
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400646 for _, testCase := range testCases {
Sam Delmerico3177a6e2022-06-21 19:28:33 +0000647 t.Run(testCase.Description, func(t *testing.T) {
Liz Kammer78cfdaa2021-11-08 12:56:31 -0500648 runGenruleTestCase(t, testCase)
649 })
Rupert Shuttleworthc5fa3062021-09-08 10:36:41 -0400650 }
651}
Yu Liud6201012022-10-17 12:29:15 -0700652
653func TestCcGenruleArchAndExcludeSrcs(t *testing.T) {
654 name := "cc_genrule with arch"
655 bp := `
656 cc_genrule {
657 name: "foo",
658 srcs: [
659 "foo1.in",
660 "foo2.in",
661 ],
662 exclude_srcs: ["foo2.in"],
663 arch: {
664 arm: {
665 srcs: [
666 "foo1_arch.in",
667 "foo2_arch.in",
668 ],
669 exclude_srcs: ["foo2_arch.in"],
670 },
671 },
672 cmd: "cat $(in) > $(out)",
673 bazel_module: { bp2build_available: true },
674 }`
675
676 expectedBazelAttrs := AttrNameToString{
677 "srcs": `["foo1.in"] + select({
678 "//build/bazel/platforms/arch:arm": ["foo1_arch.in"],
679 "//conditions:default": [],
680 })`,
681 "cmd": `"cat $(SRCS) > $(OUTS)"`,
682 "target_compatible_with": `["//build/bazel/platforms/os:android"]`,
683 }
684
685 expectedBazelTargets := []string{
686 MakeBazelTargetNoRestrictions("genrule", "foo", expectedBazelAttrs),
687 }
688
689 t.Run(name, func(t *testing.T) {
690 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
691 Bp2buildTestCase{
692 ModuleTypeUnderTest: "cc_genrule",
693 ModuleTypeUnderTestFactory: cc.GenRuleFactory,
694 Blueprint: bp,
695 ExpectedBazelTargets: expectedBazelTargets,
696 })
697 })
698}
Liz Kammer0db0e342023-07-18 11:39:30 -0400699
700func TestGenruleWithExportIncludeDirs(t *testing.T) {
701 testCases := []struct {
702 moduleType string
703 factory android.ModuleFactory
704 hod android.HostOrDeviceSupported
705 }{
706 {
707 moduleType: "genrule",
708 factory: genrule.GenRuleFactory,
709 },
710 {
711 moduleType: "cc_genrule",
712 factory: cc.GenRuleFactory,
713 hod: android.DeviceSupported,
714 },
715 {
716 moduleType: "java_genrule",
717 factory: java.GenRuleFactory,
718 hod: android.DeviceSupported,
719 },
720 {
721 moduleType: "java_genrule_host",
722 factory: java.GenRuleFactoryHost,
723 hod: android.HostSupported,
724 },
725 }
726
727 dir := "baz"
728
729 bp := `%s {
730 name: "foo",
731 out: ["foo.out.h"],
732 srcs: ["foo.in"],
733 cmd: "cp $(in) $(out)",
734 export_include_dirs: ["foo", "bar", "."],
735 bazel_module: { bp2build_available: true },
736}`
737
738 for _, tc := range testCases {
739 moduleAttrs := AttrNameToString{
740 "cmd": `"cp $(SRCS) $(OUTS)"`,
741 "outs": `["foo.out.h"]`,
742 "srcs": `["foo.in"]`,
743 }
744
745 expectedBazelTargets := []string{
746 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
747 makeBazelTargetHostOrDevice("cc_library_headers", "foo__header_library", AttrNameToString{
748 "hdrs": `[":foo"]`,
749 "export_includes": `[
750 "foo",
751 "baz/foo",
752 "bar",
753 "baz/bar",
754 ".",
755 "baz",
756 ]`,
757 },
758 tc.hod),
759 }
760
761 t.Run(tc.moduleType, func(t *testing.T) {
762 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) {},
763 Bp2buildTestCase{
764 ModuleTypeUnderTest: tc.moduleType,
765 ModuleTypeUnderTestFactory: tc.factory,
766 Filesystem: map[string]string{
767 filepath.Join(dir, "Android.bp"): fmt.Sprintf(bp, tc.moduleType),
768 },
769 Dir: dir,
770 ExpectedBazelTargets: expectedBazelTargets,
771 })
772 })
773 }
774}
Cole Faustf0d4d4f2023-08-14 11:37:54 -0700775
Cole Fausted940002023-08-15 11:59:24 -0700776func TestGenruleWithSoongConfigVariableConfiguredCmd(t *testing.T) {
Cole Faustf0d4d4f2023-08-14 11:37:54 -0700777 testCases := []struct {
778 moduleType string
779 factory android.ModuleFactory
780 hod android.HostOrDeviceSupported
781 }{
782 {
783 moduleType: "genrule",
784 factory: genrule.GenRuleFactory,
785 },
786 {
787 moduleType: "cc_genrule",
788 factory: cc.GenRuleFactory,
789 hod: android.DeviceSupported,
790 },
791 {
792 moduleType: "java_genrule",
793 factory: java.GenRuleFactory,
794 hod: android.DeviceSupported,
795 },
796 {
797 moduleType: "java_genrule_host",
798 factory: java.GenRuleFactoryHost,
799 hod: android.HostSupported,
800 },
801 }
802
803 bp := `
804soong_config_module_type {
805 name: "my_genrule",
806 module_type: "%s",
807 config_namespace: "my_namespace",
808 bool_variables: ["my_variable"],
809 properties: ["cmd"],
810}
811
812my_genrule {
813 name: "foo",
814 out: ["foo.txt"],
815 cmd: "echo 'no variable' > $(out)",
816 soong_config_variables: {
817 my_variable: {
818 cmd: "echo 'with variable' > $(out)",
819 },
820 },
821 bazel_module: { bp2build_available: true },
822}
823`
824
825 for _, tc := range testCases {
826 moduleAttrs := AttrNameToString{
827 "cmd": `select({
828 "//build/bazel/product_config/config_settings:my_namespace__my_variable": "echo 'with variable' > $(OUTS)",
829 "//conditions:default": "echo 'no variable' > $(OUTS)",
830 })`,
831 "outs": `["foo.txt"]`,
832 }
833
834 expectedBazelTargets := []string{
835 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
836 }
837
838 t.Run(tc.moduleType, func(t *testing.T) {
839 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { android.RegisterSoongConfigModuleBuildComponents(ctx) },
840 Bp2buildTestCase{
841 Blueprint: fmt.Sprintf(bp, tc.moduleType),
842 ModuleTypeUnderTest: tc.moduleType,
843 ModuleTypeUnderTestFactory: tc.factory,
844 ExpectedBazelTargets: expectedBazelTargets,
845 })
846 })
847 }
848}
Cole Fausted940002023-08-15 11:59:24 -0700849
850func TestGenruleWithProductVariableConfiguredCmd(t *testing.T) {
851 testCases := []struct {
852 moduleType string
853 factory android.ModuleFactory
854 hod android.HostOrDeviceSupported
855 }{
856 {
857 moduleType: "genrule",
858 factory: genrule.GenRuleFactory,
859 },
860 {
861 moduleType: "cc_genrule",
862 factory: cc.GenRuleFactory,
863 hod: android.DeviceSupported,
864 },
865 {
866 moduleType: "java_genrule",
867 factory: java.GenRuleFactory,
868 hod: android.DeviceSupported,
869 },
870 {
871 moduleType: "java_genrule_host",
872 factory: java.GenRuleFactoryHost,
873 hod: android.HostSupported,
874 },
875 }
876
877 bp := `
878
879%s {
880 name: "foo",
881 out: ["foo.txt"],
882 cmd: "echo 'no variable' > $(out)",
883 product_variables: {
884 debuggable: {
885 cmd: "echo 'with variable' > $(out)",
886 },
887 },
888 bazel_module: { bp2build_available: true },
889}
890`
891
892 for _, tc := range testCases {
893 moduleAttrs := AttrNameToString{
894 "cmd": `select({
895 "//build/bazel/product_config/config_settings:debuggable": "echo 'with variable' > $(OUTS)",
896 "//conditions:default": "echo 'no variable' > $(OUTS)",
897 })`,
898 "outs": `["foo.txt"]`,
899 }
900
901 expectedBazelTargets := []string{
902 makeBazelTargetHostOrDevice("genrule", "foo", moduleAttrs, tc.hod),
903 }
904
905 t.Run(tc.moduleType, func(t *testing.T) {
906 RunBp2BuildTestCase(t, func(ctx android.RegistrationContext) { android.RegisterSoongConfigModuleBuildComponents(ctx) },
907 Bp2buildTestCase{
908 Blueprint: fmt.Sprintf(bp, tc.moduleType),
909 ModuleTypeUnderTest: tc.moduleType,
910 ModuleTypeUnderTestFactory: tc.factory,
911 ExpectedBazelTargets: expectedBazelTargets,
912 })
913 })
914 }
915}