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