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