Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 1 | // 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 | |
| 15 | package bp2build |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 19 | "android/soong/cc" |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 20 | "android/soong/genrule" |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 21 | "android/soong/java" |
| 22 | "fmt" |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 23 | "testing" |
| 24 | ) |
| 25 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 26 | func registerGenruleModuleTypes(ctx android.RegistrationContext) { |
| 27 | ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() }) |
| 28 | } |
| 29 | |
| 30 | func runGenruleTestCase(t *testing.T, tc bp2buildTestCase) { |
| 31 | t.Helper() |
| 32 | (&tc).moduleTypeUnderTest = "genrule" |
| 33 | (&tc).moduleTypeUnderTestFactory = genrule.GenRuleFactory |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 34 | runBp2BuildTestCase(t, registerGenruleModuleTypes, tc) |
| 35 | } |
| 36 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 37 | func otherGenruleBp(genruleTarget string) map[string]string { |
| 38 | return map[string]string{ |
| 39 | "other/Android.bp": fmt.Sprintf(`%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 40 | name: "foo.tool", |
| 41 | out: ["foo_tool.out"], |
| 42 | srcs: ["foo_tool.in"], |
| 43 | cmd: "cp $(in) $(out)", |
| 44 | } |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 45 | %s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 46 | name: "other.tool", |
| 47 | out: ["other_tool.out"], |
| 48 | srcs: ["other_tool.in"], |
| 49 | cmd: "cp $(in) $(out)", |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 50 | }`, genruleTarget, genruleTarget), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | func 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 Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 80 | } |
| 81 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 82 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 83 | name: "foo.tool", |
| 84 | out: ["foo_tool.out"], |
| 85 | srcs: ["foo_tool.in"], |
| 86 | cmd: "cp $(in) $(out)", |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 87 | bazel_module: { bp2build_available: false }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 88 | } |
| 89 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 90 | %s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 91 | 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 Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 97 | }` |
| 98 | |
| 99 | for _, tc := range testCases { |
| 100 | expectedBazelTargets := []string{ |
| 101 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 102 | "cmd": fmt.Sprintf(`"$(location :foo.tool) --genDir=%s arg $(SRCS) $(OUTS)"`, tc.genDir), |
| 103 | "outs": `["foo.out"]`, |
| 104 | "srcs": `["foo.in"]`, |
| 105 | "tools": `[":foo.tool"]`, |
| 106 | }), |
| 107 | } |
| 108 | |
| 109 | t.Run(tc.moduleType, func(t *testing.T) { |
| 110 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 111 | bp2buildTestCase{ |
| 112 | moduleTypeUnderTest: tc.moduleType, |
| 113 | moduleTypeUnderTestFactory: tc.factory, |
| 114 | blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType), |
| 115 | expectedBazelTargets: expectedBazelTargets, |
| 116 | }) |
| 117 | }) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestGenruleLocationsLabel(t *testing.T) { |
| 122 | testCases := []struct { |
| 123 | moduleType string |
| 124 | factory android.ModuleFactory |
| 125 | }{ |
| 126 | { |
| 127 | moduleType: "genrule", |
| 128 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 129 | }, |
| 130 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 131 | moduleType: "cc_genrule", |
| 132 | factory: cc.GenRuleFactory, |
| 133 | }, |
| 134 | { |
| 135 | moduleType: "java_genrule", |
| 136 | factory: java.GenRuleFactory, |
| 137 | }, |
| 138 | { |
| 139 | moduleType: "java_genrule_host", |
| 140 | factory: java.GenRuleFactoryHost, |
| 141 | }, |
| 142 | } |
| 143 | |
| 144 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 145 | name: "foo.tools", |
| 146 | out: ["foo_tool.out", "foo_tool2.out"], |
| 147 | srcs: ["foo_tool.in"], |
| 148 | cmd: "cp $(in) $(out)", |
| 149 | bazel_module: { bp2build_available: true }, |
| 150 | } |
| 151 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 152 | %s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 153 | name: "foo", |
| 154 | out: ["foo.out"], |
| 155 | srcs: ["foo.in"], |
| 156 | tools: [":foo.tools"], |
| 157 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
| 158 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 159 | }` |
| 160 | |
| 161 | expectedBazelTargets := |
| 162 | []string{ |
| 163 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 164 | "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`, |
| 165 | "outs": `["foo.out"]`, |
| 166 | "srcs": `["foo.in"]`, |
| 167 | "tools": `[":foo.tools"]`, |
| 168 | }), |
| 169 | makeBazelTarget("genrule", "foo.tools", attrNameToString{ |
| 170 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 171 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 172 | "foo_tool.out", |
| 173 | "foo_tool2.out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 174 | ]`, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 175 | "srcs": `["foo_tool.in"]`, |
| 176 | }), |
| 177 | } |
| 178 | |
| 179 | for _, tc := range testCases { |
| 180 | t.Run(tc.moduleType, func(t *testing.T) { |
| 181 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 182 | bp2buildTestCase{ |
| 183 | moduleTypeUnderTest: tc.moduleType, |
| 184 | moduleTypeUnderTestFactory: tc.factory, |
| 185 | blueprint: fmt.Sprintf(bp, tc.moduleType, tc.moduleType), |
| 186 | expectedBazelTargets: expectedBazelTargets, |
| 187 | }) |
| 188 | }) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | func TestGenruleLocationsAbsoluteLabel(t *testing.T) { |
| 193 | testCases := []struct { |
| 194 | moduleType string |
| 195 | factory android.ModuleFactory |
| 196 | }{ |
| 197 | { |
| 198 | moduleType: "genrule", |
| 199 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 200 | }, |
| 201 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 202 | moduleType: "cc_genrule", |
| 203 | factory: cc.GenRuleFactory, |
| 204 | }, |
| 205 | { |
| 206 | moduleType: "java_genrule", |
| 207 | factory: java.GenRuleFactory, |
| 208 | }, |
| 209 | { |
| 210 | moduleType: "java_genrule_host", |
| 211 | factory: java.GenRuleFactoryHost, |
| 212 | }, |
| 213 | } |
| 214 | |
| 215 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 216 | name: "foo", |
| 217 | out: ["foo.out"], |
| 218 | srcs: ["foo.in"], |
| 219 | tool_files: [":foo.tool"], |
| 220 | cmd: "$(locations :foo.tool) -s $(out) $(in)", |
| 221 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 222 | }` |
| 223 | |
| 224 | expectedBazelTargets := []string{ |
| 225 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 226 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 227 | "outs": `["foo.out"]`, |
| 228 | "srcs": `["foo.in"]`, |
| 229 | "tools": `["//other:foo.tool"]`, |
| 230 | }), |
| 231 | } |
| 232 | |
| 233 | for _, tc := range testCases { |
| 234 | t.Run(tc.moduleType, func(t *testing.T) { |
| 235 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 236 | bp2buildTestCase{ |
| 237 | moduleTypeUnderTest: tc.moduleType, |
| 238 | moduleTypeUnderTestFactory: tc.factory, |
| 239 | blueprint: fmt.Sprintf(bp, tc.moduleType), |
| 240 | expectedBazelTargets: expectedBazelTargets, |
| 241 | filesystem: otherGenruleBp(tc.moduleType), |
| 242 | }) |
| 243 | }) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | func TestGenruleSrcsLocationsAbsoluteLabel(t *testing.T) { |
| 248 | testCases := []struct { |
| 249 | moduleType string |
| 250 | factory android.ModuleFactory |
| 251 | }{ |
| 252 | { |
| 253 | moduleType: "genrule", |
| 254 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 255 | }, |
| 256 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 257 | moduleType: "cc_genrule", |
| 258 | factory: cc.GenRuleFactory, |
| 259 | }, |
| 260 | { |
| 261 | moduleType: "java_genrule", |
| 262 | factory: java.GenRuleFactory, |
| 263 | }, |
| 264 | { |
| 265 | moduleType: "java_genrule_host", |
| 266 | factory: java.GenRuleFactoryHost, |
| 267 | }, |
| 268 | } |
| 269 | |
| 270 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 271 | name: "foo", |
| 272 | out: ["foo.out"], |
| 273 | srcs: [":other.tool"], |
| 274 | tool_files: [":foo.tool"], |
| 275 | cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", |
| 276 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 277 | }` |
| 278 | |
| 279 | expectedBazelTargets := []string{ |
| 280 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 281 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`, |
| 282 | "outs": `["foo.out"]`, |
| 283 | "srcs": `["//other:other.tool"]`, |
| 284 | "tools": `["//other:foo.tool"]`, |
| 285 | }), |
| 286 | } |
| 287 | |
| 288 | for _, tc := range testCases { |
| 289 | t.Run(tc.moduleType, func(t *testing.T) { |
| 290 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 291 | bp2buildTestCase{ |
| 292 | moduleTypeUnderTest: tc.moduleType, |
| 293 | moduleTypeUnderTestFactory: tc.factory, |
| 294 | blueprint: fmt.Sprintf(bp, tc.moduleType), |
| 295 | expectedBazelTargets: expectedBazelTargets, |
| 296 | filesystem: otherGenruleBp(tc.moduleType), |
| 297 | }) |
| 298 | }) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | func TestGenruleLocationLabelShouldSubstituteFirstToolLabel(t *testing.T) { |
| 303 | testCases := []struct { |
| 304 | moduleType string |
| 305 | factory android.ModuleFactory |
| 306 | }{ |
| 307 | { |
| 308 | moduleType: "genrule", |
| 309 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 310 | }, |
| 311 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 312 | moduleType: "cc_genrule", |
| 313 | factory: cc.GenRuleFactory, |
| 314 | }, |
| 315 | { |
| 316 | moduleType: "java_genrule", |
| 317 | factory: java.GenRuleFactory, |
| 318 | }, |
| 319 | { |
| 320 | moduleType: "java_genrule_host", |
| 321 | factory: java.GenRuleFactoryHost, |
| 322 | }, |
| 323 | } |
| 324 | |
| 325 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 326 | name: "foo", |
| 327 | out: ["foo.out"], |
| 328 | srcs: ["foo.in"], |
| 329 | tool_files: [":foo.tool", ":other.tool"], |
| 330 | cmd: "$(location) -s $(out) $(in)", |
| 331 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 332 | }` |
| 333 | |
| 334 | expectedBazelTargets := []string{ |
| 335 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 336 | "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 337 | "outs": `["foo.out"]`, |
| 338 | "srcs": `["foo.in"]`, |
| 339 | "tools": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 340 | "//other:foo.tool", |
| 341 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 342 | ]`, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 343 | })} |
| 344 | |
| 345 | for _, tc := range testCases { |
| 346 | t.Run(tc.moduleType, func(t *testing.T) { |
| 347 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 348 | bp2buildTestCase{ |
| 349 | moduleTypeUnderTest: tc.moduleType, |
| 350 | moduleTypeUnderTestFactory: tc.factory, |
| 351 | blueprint: fmt.Sprintf(bp, tc.moduleType), |
| 352 | expectedBazelTargets: expectedBazelTargets, |
| 353 | filesystem: otherGenruleBp(tc.moduleType), |
| 354 | }) |
| 355 | }) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | func TestGenruleLocationsLabelShouldSubstituteFirstToolLabel(t *testing.T) { |
| 360 | testCases := []struct { |
| 361 | moduleType string |
| 362 | factory android.ModuleFactory |
| 363 | }{ |
| 364 | { |
| 365 | moduleType: "genrule", |
| 366 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 367 | }, |
| 368 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 369 | moduleType: "cc_genrule", |
| 370 | factory: cc.GenRuleFactory, |
| 371 | }, |
| 372 | { |
| 373 | moduleType: "java_genrule", |
| 374 | factory: java.GenRuleFactory, |
| 375 | }, |
| 376 | { |
| 377 | moduleType: "java_genrule_host", |
| 378 | factory: java.GenRuleFactoryHost, |
| 379 | }, |
| 380 | } |
| 381 | |
| 382 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 383 | name: "foo", |
| 384 | out: ["foo.out"], |
| 385 | srcs: ["foo.in"], |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 386 | tool_files: [":foo.tool", ":other.tool"], |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 387 | cmd: "$(locations) -s $(out) $(in)", |
| 388 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 389 | }` |
| 390 | |
| 391 | expectedBazelTargets := []string{ |
| 392 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 393 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 394 | "outs": `["foo.out"]`, |
| 395 | "srcs": `["foo.in"]`, |
| 396 | "tools": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 397 | "//other:foo.tool", |
| 398 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 399 | ]`, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 400 | })} |
| 401 | |
| 402 | for _, tc := range testCases { |
| 403 | t.Run(tc.moduleType, func(t *testing.T) { |
| 404 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 405 | bp2buildTestCase{ |
| 406 | moduleTypeUnderTest: tc.moduleType, |
| 407 | moduleTypeUnderTestFactory: tc.factory, |
| 408 | blueprint: fmt.Sprintf(bp, tc.moduleType), |
| 409 | expectedBazelTargets: expectedBazelTargets, |
| 410 | filesystem: otherGenruleBp(tc.moduleType), |
| 411 | }) |
| 412 | }) |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | func TestGenruleWithoutToolsOrToolFiles(t *testing.T) { |
| 417 | testCases := []struct { |
| 418 | moduleType string |
| 419 | factory android.ModuleFactory |
| 420 | }{ |
| 421 | { |
| 422 | moduleType: "genrule", |
| 423 | factory: genrule.GenRuleFactory, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 424 | }, |
| 425 | { |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 426 | moduleType: "cc_genrule", |
| 427 | factory: cc.GenRuleFactory, |
| 428 | }, |
| 429 | { |
| 430 | moduleType: "java_genrule", |
| 431 | factory: java.GenRuleFactory, |
| 432 | }, |
| 433 | { |
| 434 | moduleType: "java_genrule_host", |
| 435 | factory: java.GenRuleFactoryHost, |
| 436 | }, |
| 437 | } |
| 438 | |
| 439 | bp := `%s { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 440 | name: "foo", |
| 441 | out: ["foo.out"], |
| 442 | srcs: ["foo.in"], |
| 443 | cmd: "cp $(in) $(out)", |
| 444 | bazel_module: { bp2build_available: true }, |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 445 | }` |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 446 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 447 | expectedBazelTargets := []string{ |
| 448 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 449 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 450 | "outs": `["foo.out"]`, |
| 451 | "srcs": `["foo.in"]`, |
| 452 | })} |
| 453 | |
| 454 | for _, tc := range testCases { |
| 455 | t.Run(tc.moduleType, func(t *testing.T) { |
| 456 | runBp2BuildTestCase(t, func(ctx android.RegistrationContext) {}, |
| 457 | bp2buildTestCase{ |
| 458 | moduleTypeUnderTest: tc.moduleType, |
| 459 | moduleTypeUnderTestFactory: tc.factory, |
| 460 | blueprint: fmt.Sprintf(bp, tc.moduleType), |
| 461 | expectedBazelTargets: expectedBazelTargets, |
| 462 | }) |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 463 | }) |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
Sam Delmerico | cd1b80f | 2022-01-11 21:55:46 +0000 | [diff] [blame] | 467 | func TestGenruleBp2BuildInlinesDefaults(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 468 | testCases := []bp2buildTestCase{ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 469 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 470 | description: "genrule applies properties from a genrule_defaults dependency if not specified", |
| 471 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 472 | name: "gen_defaults", |
| 473 | cmd: "do-something $(in) $(out)", |
| 474 | } |
| 475 | genrule { |
| 476 | name: "gen", |
| 477 | out: ["out"], |
| 478 | srcs: ["in1"], |
| 479 | defaults: ["gen_defaults"], |
| 480 | bazel_module: { bp2build_available: true }, |
| 481 | } |
| 482 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 483 | expectedBazelTargets: []string{ |
| 484 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 485 | "cmd": `"do-something $(SRCS) $(OUTS)"`, |
| 486 | "outs": `["out"]`, |
| 487 | "srcs": `["in1"]`, |
| 488 | }), |
| 489 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 490 | }, |
| 491 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 492 | description: "genrule does merges properties from a genrule_defaults dependency, latest-first", |
| 493 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 494 | name: "gen_defaults", |
| 495 | out: ["out-from-defaults"], |
| 496 | srcs: ["in-from-defaults"], |
| 497 | cmd: "cmd-from-defaults", |
| 498 | } |
| 499 | genrule { |
| 500 | name: "gen", |
| 501 | out: ["out"], |
| 502 | srcs: ["in1"], |
| 503 | defaults: ["gen_defaults"], |
| 504 | cmd: "do-something $(in) $(out)", |
| 505 | bazel_module: { bp2build_available: true }, |
| 506 | } |
| 507 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 508 | expectedBazelTargets: []string{ |
| 509 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 510 | "cmd": `"do-something $(SRCS) $(OUTS)"`, |
| 511 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 512 | "out-from-defaults", |
| 513 | "out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 514 | ]`, |
| 515 | "srcs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 516 | "in-from-defaults", |
| 517 | "in1", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 518 | ]`, |
| 519 | }), |
| 520 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 521 | }, |
| 522 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 523 | description: "genrule applies properties from list of genrule_defaults", |
| 524 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 525 | name: "gen_defaults1", |
| 526 | cmd: "cp $(in) $(out)", |
| 527 | } |
| 528 | |
| 529 | genrule_defaults { |
| 530 | name: "gen_defaults2", |
| 531 | srcs: ["in1"], |
| 532 | } |
| 533 | |
| 534 | genrule { |
| 535 | name: "gen", |
| 536 | out: ["out"], |
| 537 | defaults: ["gen_defaults1", "gen_defaults2"], |
| 538 | bazel_module: { bp2build_available: true }, |
| 539 | } |
| 540 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 541 | expectedBazelTargets: []string{ |
| 542 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 543 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 544 | "outs": `["out"]`, |
| 545 | "srcs": `["in1"]`, |
| 546 | }), |
| 547 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 548 | }, |
| 549 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 550 | description: "genrule applies properties from genrule_defaults transitively", |
| 551 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 552 | name: "gen_defaults1", |
| 553 | defaults: ["gen_defaults2"], |
| 554 | cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. |
| 555 | } |
| 556 | |
| 557 | genrule_defaults { |
| 558 | name: "gen_defaults2", |
| 559 | defaults: ["gen_defaults3"], |
| 560 | cmd: "cmd2 $(in) $(out)", |
| 561 | out: ["out-from-2"], |
| 562 | srcs: ["in1"], |
| 563 | } |
| 564 | |
| 565 | genrule_defaults { |
| 566 | name: "gen_defaults3", |
| 567 | out: ["out-from-3"], |
| 568 | srcs: ["srcs-from-3"], |
| 569 | } |
| 570 | |
| 571 | genrule { |
| 572 | name: "gen", |
| 573 | out: ["out"], |
| 574 | defaults: ["gen_defaults1"], |
| 575 | bazel_module: { bp2build_available: true }, |
| 576 | } |
| 577 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 578 | expectedBazelTargets: []string{ |
| 579 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 580 | "cmd": `"cmd1 $(SRCS) $(OUTS)"`, |
| 581 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 582 | "out-from-3", |
| 583 | "out-from-2", |
| 584 | "out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 585 | ]`, |
| 586 | "srcs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 587 | "srcs-from-3", |
| 588 | "in1", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 589 | ]`, |
| 590 | }), |
| 591 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 592 | }, |
| 593 | } |
| 594 | |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 595 | for _, testCase := range testCases { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 596 | t.Run(testCase.description, func(t *testing.T) { |
| 597 | runGenruleTestCase(t, testCase) |
| 598 | }) |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 599 | } |
| 600 | } |