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" |
| 19 | "android/soong/genrule" |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 23 | func registerGenruleModuleTypes(ctx android.RegistrationContext) { |
| 24 | ctx.RegisterModuleType("genrule_defaults", func() android.Module { return genrule.DefaultsFactory() }) |
| 25 | } |
| 26 | |
| 27 | func runGenruleTestCase(t *testing.T, tc bp2buildTestCase) { |
| 28 | t.Helper() |
| 29 | (&tc).moduleTypeUnderTest = "genrule" |
| 30 | (&tc).moduleTypeUnderTestFactory = genrule.GenRuleFactory |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 31 | runBp2BuildTestCase(t, registerGenruleModuleTypes, tc) |
| 32 | } |
| 33 | |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 34 | func TestGenruleBp2Build(t *testing.T) { |
| 35 | otherGenruleBp := map[string]string{ |
| 36 | "other/Android.bp": `genrule { |
| 37 | name: "foo.tool", |
| 38 | out: ["foo_tool.out"], |
| 39 | srcs: ["foo_tool.in"], |
| 40 | cmd: "cp $(in) $(out)", |
| 41 | } |
| 42 | genrule { |
| 43 | name: "other.tool", |
| 44 | out: ["other_tool.out"], |
| 45 | srcs: ["other_tool.in"], |
| 46 | cmd: "cp $(in) $(out)", |
| 47 | }`, |
| 48 | } |
| 49 | |
| 50 | testCases := []bp2buildTestCase{ |
| 51 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 52 | description: "genrule with command line variable replacements", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 53 | blueprint: `genrule { |
| 54 | name: "foo.tool", |
| 55 | out: ["foo_tool.out"], |
| 56 | srcs: ["foo_tool.in"], |
| 57 | cmd: "cp $(in) $(out)", |
| 58 | bazel_module: { bp2build_available: true }, |
| 59 | } |
| 60 | |
| 61 | genrule { |
| 62 | name: "foo", |
| 63 | out: ["foo.out"], |
| 64 | srcs: ["foo.in"], |
| 65 | tools: [":foo.tool"], |
| 66 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
| 67 | bazel_module: { bp2build_available: true }, |
| 68 | }`, |
| 69 | expectedBazelTargets: []string{ |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 70 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 71 | "cmd": `"$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)"`, |
| 72 | "outs": `["foo.out"]`, |
| 73 | "srcs": `["foo.in"]`, |
| 74 | "tools": `[":foo.tool"]`, |
| 75 | }), |
| 76 | makeBazelTarget("genrule", "foo.tool", attrNameToString{ |
| 77 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 78 | "outs": `["foo_tool.out"]`, |
| 79 | "srcs": `["foo_tool.in"]`, |
| 80 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 81 | }, |
| 82 | }, |
| 83 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 84 | description: "genrule using $(locations :label)", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 85 | blueprint: `genrule { |
| 86 | name: "foo.tools", |
| 87 | out: ["foo_tool.out", "foo_tool2.out"], |
| 88 | srcs: ["foo_tool.in"], |
| 89 | cmd: "cp $(in) $(out)", |
| 90 | bazel_module: { bp2build_available: true }, |
| 91 | } |
| 92 | |
| 93 | genrule { |
| 94 | name: "foo", |
| 95 | out: ["foo.out"], |
| 96 | srcs: ["foo.in"], |
| 97 | tools: [":foo.tools"], |
| 98 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
| 99 | bazel_module: { bp2build_available: true }, |
| 100 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 101 | expectedBazelTargets: []string{ |
| 102 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 103 | "cmd": `"$(locations :foo.tools) -s $(OUTS) $(SRCS)"`, |
| 104 | "outs": `["foo.out"]`, |
| 105 | "srcs": `["foo.in"]`, |
| 106 | "tools": `[":foo.tools"]`, |
| 107 | }), |
| 108 | makeBazelTarget("genrule", "foo.tools", attrNameToString{ |
| 109 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 110 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 111 | "foo_tool.out", |
| 112 | "foo_tool2.out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 113 | ]`, |
| 114 | "srcs": `["foo_tool.in"]`, |
| 115 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 116 | }, |
| 117 | }, |
| 118 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 119 | description: "genrule using $(locations //absolute:label)", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 120 | blueprint: `genrule { |
| 121 | name: "foo", |
| 122 | out: ["foo.out"], |
| 123 | srcs: ["foo.in"], |
| 124 | tool_files: [":foo.tool"], |
| 125 | cmd: "$(locations :foo.tool) -s $(out) $(in)", |
| 126 | bazel_module: { bp2build_available: true }, |
| 127 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 128 | expectedBazelTargets: []string{ |
| 129 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 130 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 131 | "outs": `["foo.out"]`, |
| 132 | "srcs": `["foo.in"]`, |
| 133 | "tools": `["//other:foo.tool"]`, |
| 134 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 135 | }, |
| 136 | filesystem: otherGenruleBp, |
| 137 | }, |
| 138 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 139 | description: "genrule srcs using $(locations //absolute:label)", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 140 | blueprint: `genrule { |
| 141 | name: "foo", |
| 142 | out: ["foo.out"], |
| 143 | srcs: [":other.tool"], |
| 144 | tool_files: [":foo.tool"], |
| 145 | cmd: "$(locations :foo.tool) -s $(out) $(location :other.tool)", |
| 146 | bazel_module: { bp2build_available: true }, |
| 147 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 148 | expectedBazelTargets: []string{ |
| 149 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 150 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(location //other:other.tool)"`, |
| 151 | "outs": `["foo.out"]`, |
| 152 | "srcs": `["//other:other.tool"]`, |
| 153 | "tools": `["//other:foo.tool"]`, |
| 154 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 155 | }, |
| 156 | filesystem: otherGenruleBp, |
| 157 | }, |
| 158 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 159 | description: "genrule using $(location) label should substitute first tool label automatically", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 160 | blueprint: `genrule { |
| 161 | name: "foo", |
| 162 | out: ["foo.out"], |
| 163 | srcs: ["foo.in"], |
| 164 | tool_files: [":foo.tool", ":other.tool"], |
| 165 | cmd: "$(location) -s $(out) $(in)", |
| 166 | bazel_module: { bp2build_available: true }, |
| 167 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 168 | expectedBazelTargets: []string{ |
| 169 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 170 | "cmd": `"$(location //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 171 | "outs": `["foo.out"]`, |
| 172 | "srcs": `["foo.in"]`, |
| 173 | "tools": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 174 | "//other:foo.tool", |
| 175 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 176 | ]`, |
| 177 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 178 | }, |
| 179 | filesystem: otherGenruleBp, |
| 180 | }, |
| 181 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 182 | description: "genrule using $(locations) label should substitute first tool label automatically", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 183 | blueprint: `genrule { |
| 184 | name: "foo", |
| 185 | out: ["foo.out"], |
| 186 | srcs: ["foo.in"], |
| 187 | tools: [":foo.tool", ":other.tool"], |
| 188 | cmd: "$(locations) -s $(out) $(in)", |
| 189 | bazel_module: { bp2build_available: true }, |
| 190 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 191 | expectedBazelTargets: []string{ |
| 192 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 193 | "cmd": `"$(locations //other:foo.tool) -s $(OUTS) $(SRCS)"`, |
| 194 | "outs": `["foo.out"]`, |
| 195 | "srcs": `["foo.in"]`, |
| 196 | "tools": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 197 | "//other:foo.tool", |
| 198 | "//other:other.tool", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 199 | ]`, |
| 200 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 201 | }, |
| 202 | filesystem: otherGenruleBp, |
| 203 | }, |
| 204 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 205 | description: "genrule without tools or tool_files can convert successfully", |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 206 | blueprint: `genrule { |
| 207 | name: "foo", |
| 208 | out: ["foo.out"], |
| 209 | srcs: ["foo.in"], |
| 210 | cmd: "cp $(in) $(out)", |
| 211 | bazel_module: { bp2build_available: true }, |
| 212 | }`, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 213 | expectedBazelTargets: []string{ |
| 214 | makeBazelTarget("genrule", "foo", attrNameToString{ |
| 215 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 216 | "outs": `["foo.out"]`, |
| 217 | "srcs": `["foo.in"]`, |
| 218 | }), |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 219 | }, |
| 220 | }, |
| 221 | } |
| 222 | |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 223 | for _, testCase := range testCases { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 224 | t.Run(testCase.description, func(t *testing.T) { |
| 225 | runGenruleTestCase(t, testCase) |
| 226 | }) |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | func TestBp2BuildInlinesDefaults(t *testing.T) { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 231 | testCases := []bp2buildTestCase{ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 232 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 233 | description: "genrule applies properties from a genrule_defaults dependency if not specified", |
| 234 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 235 | name: "gen_defaults", |
| 236 | cmd: "do-something $(in) $(out)", |
| 237 | } |
| 238 | genrule { |
| 239 | name: "gen", |
| 240 | out: ["out"], |
| 241 | srcs: ["in1"], |
| 242 | defaults: ["gen_defaults"], |
| 243 | bazel_module: { bp2build_available: true }, |
| 244 | } |
| 245 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 246 | expectedBazelTargets: []string{ |
| 247 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 248 | "cmd": `"do-something $(SRCS) $(OUTS)"`, |
| 249 | "outs": `["out"]`, |
| 250 | "srcs": `["in1"]`, |
| 251 | }), |
| 252 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 253 | }, |
| 254 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 255 | description: "genrule does merges properties from a genrule_defaults dependency, latest-first", |
| 256 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 257 | name: "gen_defaults", |
| 258 | out: ["out-from-defaults"], |
| 259 | srcs: ["in-from-defaults"], |
| 260 | cmd: "cmd-from-defaults", |
| 261 | } |
| 262 | genrule { |
| 263 | name: "gen", |
| 264 | out: ["out"], |
| 265 | srcs: ["in1"], |
| 266 | defaults: ["gen_defaults"], |
| 267 | cmd: "do-something $(in) $(out)", |
| 268 | bazel_module: { bp2build_available: true }, |
| 269 | } |
| 270 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 271 | expectedBazelTargets: []string{ |
| 272 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 273 | "cmd": `"do-something $(SRCS) $(OUTS)"`, |
| 274 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 275 | "out-from-defaults", |
| 276 | "out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 277 | ]`, |
| 278 | "srcs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 279 | "in-from-defaults", |
| 280 | "in1", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 281 | ]`, |
| 282 | }), |
| 283 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 284 | }, |
| 285 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 286 | description: "genrule applies properties from list of genrule_defaults", |
| 287 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 288 | name: "gen_defaults1", |
| 289 | cmd: "cp $(in) $(out)", |
| 290 | } |
| 291 | |
| 292 | genrule_defaults { |
| 293 | name: "gen_defaults2", |
| 294 | srcs: ["in1"], |
| 295 | } |
| 296 | |
| 297 | genrule { |
| 298 | name: "gen", |
| 299 | out: ["out"], |
| 300 | defaults: ["gen_defaults1", "gen_defaults2"], |
| 301 | bazel_module: { bp2build_available: true }, |
| 302 | } |
| 303 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 304 | expectedBazelTargets: []string{ |
| 305 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 306 | "cmd": `"cp $(SRCS) $(OUTS)"`, |
| 307 | "outs": `["out"]`, |
| 308 | "srcs": `["in1"]`, |
| 309 | }), |
| 310 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 311 | }, |
| 312 | { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 313 | description: "genrule applies properties from genrule_defaults transitively", |
| 314 | blueprint: `genrule_defaults { |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 315 | name: "gen_defaults1", |
| 316 | defaults: ["gen_defaults2"], |
| 317 | cmd: "cmd1 $(in) $(out)", // overrides gen_defaults2's cmd property value. |
| 318 | } |
| 319 | |
| 320 | genrule_defaults { |
| 321 | name: "gen_defaults2", |
| 322 | defaults: ["gen_defaults3"], |
| 323 | cmd: "cmd2 $(in) $(out)", |
| 324 | out: ["out-from-2"], |
| 325 | srcs: ["in1"], |
| 326 | } |
| 327 | |
| 328 | genrule_defaults { |
| 329 | name: "gen_defaults3", |
| 330 | out: ["out-from-3"], |
| 331 | srcs: ["srcs-from-3"], |
| 332 | } |
| 333 | |
| 334 | genrule { |
| 335 | name: "gen", |
| 336 | out: ["out"], |
| 337 | defaults: ["gen_defaults1"], |
| 338 | bazel_module: { bp2build_available: true }, |
| 339 | } |
| 340 | `, |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 341 | expectedBazelTargets: []string{ |
| 342 | makeBazelTarget("genrule", "gen", attrNameToString{ |
| 343 | "cmd": `"cmd1 $(SRCS) $(OUTS)"`, |
| 344 | "outs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 345 | "out-from-3", |
| 346 | "out-from-2", |
| 347 | "out", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 348 | ]`, |
| 349 | "srcs": `[ |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 350 | "srcs-from-3", |
| 351 | "in1", |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 352 | ]`, |
| 353 | }), |
| 354 | }, |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 355 | }, |
| 356 | } |
| 357 | |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 358 | for _, testCase := range testCases { |
Liz Kammer | 78cfdaa | 2021-11-08 12:56:31 -0500 | [diff] [blame] | 359 | t.Run(testCase.description, func(t *testing.T) { |
| 360 | runGenruleTestCase(t, testCase) |
| 361 | }) |
Rupert Shuttleworth | c5fa306 | 2021-09-08 10:36:41 -0400 | [diff] [blame] | 362 | } |
| 363 | } |