Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 1 | // Copyright 2020 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" |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 19 | "android/soong/genrule" |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 20 | "testing" |
| 21 | ) |
| 22 | |
| 23 | func TestGenerateSoongModuleTargets(t *testing.T) { |
| 24 | testCases := []struct { |
| 25 | bp string |
| 26 | expectedBazelTarget string |
| 27 | }{ |
| 28 | { |
| 29 | bp: `custom { |
| 30 | name: "foo", |
| 31 | } |
| 32 | `, |
| 33 | expectedBazelTarget: `soong_module( |
| 34 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 35 | soong_module_name = "foo", |
| 36 | soong_module_type = "custom", |
| 37 | soong_module_variant = "", |
| 38 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 39 | ], |
| 40 | )`, |
| 41 | }, |
| 42 | { |
| 43 | bp: `custom { |
| 44 | name: "foo", |
| 45 | ramdisk: true, |
| 46 | } |
| 47 | `, |
| 48 | expectedBazelTarget: `soong_module( |
| 49 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 50 | soong_module_name = "foo", |
| 51 | soong_module_type = "custom", |
| 52 | soong_module_variant = "", |
| 53 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 54 | ], |
| 55 | ramdisk = True, |
| 56 | )`, |
| 57 | }, |
| 58 | { |
| 59 | bp: `custom { |
| 60 | name: "foo", |
| 61 | owner: "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 62 | } |
| 63 | `, |
| 64 | expectedBazelTarget: `soong_module( |
| 65 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 66 | soong_module_name = "foo", |
| 67 | soong_module_type = "custom", |
| 68 | soong_module_variant = "", |
| 69 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 70 | ], |
| 71 | owner = "a_string_with\"quotes\"_and_\\backslashes\\\\", |
| 72 | )`, |
| 73 | }, |
| 74 | { |
| 75 | bp: `custom { |
| 76 | name: "foo", |
| 77 | required: ["bar"], |
| 78 | } |
| 79 | `, |
| 80 | expectedBazelTarget: `soong_module( |
| 81 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 82 | soong_module_name = "foo", |
| 83 | soong_module_type = "custom", |
| 84 | soong_module_variant = "", |
| 85 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 86 | ], |
| 87 | required = [ |
| 88 | "bar", |
| 89 | ], |
| 90 | )`, |
| 91 | }, |
| 92 | { |
| 93 | bp: `custom { |
| 94 | name: "foo", |
| 95 | target_required: ["qux", "bazqux"], |
| 96 | } |
| 97 | `, |
| 98 | expectedBazelTarget: `soong_module( |
| 99 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 100 | soong_module_name = "foo", |
| 101 | soong_module_type = "custom", |
| 102 | soong_module_variant = "", |
| 103 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 104 | ], |
| 105 | target_required = [ |
| 106 | "qux", |
| 107 | "bazqux", |
| 108 | ], |
| 109 | )`, |
| 110 | }, |
| 111 | { |
| 112 | bp: `custom { |
| 113 | name: "foo", |
| 114 | dist: { |
| 115 | targets: ["goal_foo"], |
| 116 | tag: ".foo", |
| 117 | }, |
| 118 | dists: [ |
| 119 | { |
| 120 | targets: ["goal_bar"], |
| 121 | tag: ".bar", |
| 122 | }, |
| 123 | ], |
| 124 | } |
| 125 | `, |
| 126 | expectedBazelTarget: `soong_module( |
| 127 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 128 | soong_module_name = "foo", |
| 129 | soong_module_type = "custom", |
| 130 | soong_module_variant = "", |
| 131 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 132 | ], |
| 133 | dist = { |
| 134 | "tag": ".foo", |
| 135 | "targets": [ |
| 136 | "goal_foo", |
| 137 | ], |
| 138 | }, |
| 139 | dists = [ |
| 140 | { |
| 141 | "tag": ".bar", |
| 142 | "targets": [ |
| 143 | "goal_bar", |
| 144 | ], |
| 145 | }, |
| 146 | ], |
| 147 | )`, |
| 148 | }, |
| 149 | { |
| 150 | bp: `custom { |
| 151 | name: "foo", |
| 152 | required: ["bar"], |
| 153 | target_required: ["qux", "bazqux"], |
| 154 | ramdisk: true, |
| 155 | owner: "custom_owner", |
| 156 | dists: [ |
| 157 | { |
| 158 | tag: ".tag", |
| 159 | targets: ["my_goal"], |
| 160 | }, |
| 161 | ], |
| 162 | } |
| 163 | `, |
| 164 | expectedBazelTarget: `soong_module( |
| 165 | name = "foo", |
Jingwen Chen | 288e2ba | 2021-01-25 04:36:04 -0500 | [diff] [blame] | 166 | soong_module_name = "foo", |
| 167 | soong_module_type = "custom", |
| 168 | soong_module_variant = "", |
| 169 | soong_module_deps = [ |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 170 | ], |
| 171 | dists = [ |
| 172 | { |
| 173 | "tag": ".tag", |
| 174 | "targets": [ |
| 175 | "my_goal", |
| 176 | ], |
| 177 | }, |
| 178 | ], |
| 179 | owner = "custom_owner", |
| 180 | ramdisk = True, |
| 181 | required = [ |
| 182 | "bar", |
| 183 | ], |
| 184 | target_required = [ |
| 185 | "qux", |
| 186 | "bazqux", |
| 187 | ], |
| 188 | )`, |
| 189 | }, |
| 190 | } |
| 191 | |
| 192 | dir := "." |
| 193 | for _, testCase := range testCases { |
| 194 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 195 | ctx := android.NewTestContext(config) |
| 196 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 197 | ctx.Register() |
| 198 | |
| 199 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 200 | android.FailIfErrored(t, errs) |
| 201 | _, errs = ctx.PrepareBuildActions(config) |
| 202 | android.FailIfErrored(t, errs) |
| 203 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 204 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, QueryView)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 205 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 206 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | actualBazelTarget := bazelTargets[0] |
| 210 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 211 | t.Errorf( |
| 212 | "Expected generated Bazel target to be '%s', got '%s'", |
| 213 | testCase.expectedBazelTarget, |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 214 | actualBazelTarget.content, |
| 215 | ) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | func TestGenerateBazelTargetModules(t *testing.T) { |
| 221 | testCases := []struct { |
| 222 | bp string |
| 223 | expectedBazelTarget string |
| 224 | }{ |
| 225 | { |
| 226 | bp: `custom { |
| 227 | name: "foo", |
| 228 | string_list_prop: ["a", "b"], |
| 229 | string_prop: "a", |
| 230 | }`, |
| 231 | expectedBazelTarget: `custom( |
| 232 | name = "foo", |
| 233 | string_list_prop = [ |
| 234 | "a", |
| 235 | "b", |
| 236 | ], |
| 237 | string_prop = "a", |
| 238 | )`, |
| 239 | }, |
| 240 | } |
| 241 | |
| 242 | dir := "." |
| 243 | for _, testCase := range testCases { |
| 244 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 245 | ctx := android.NewTestContext(config) |
| 246 | ctx.RegisterModuleType("custom", customModuleFactory) |
| 247 | ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutator) |
| 248 | ctx.RegisterForBazelConversion() |
| 249 | |
| 250 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 251 | android.FailIfErrored(t, errs) |
| 252 | _, errs = ctx.ResolveDependencies(config) |
| 253 | android.FailIfErrored(t, errs) |
| 254 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 255 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 256 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
| 257 | t.Fatalf("Expected %d bazel target, got %d", expectedCount, actualCount) |
Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | actualBazelTarget := bazelTargets[0] |
| 261 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 262 | t.Errorf( |
| 263 | "Expected generated Bazel target to be '%s', got '%s'", |
| 264 | testCase.expectedBazelTarget, |
| 265 | actualBazelTarget.content, |
Liz Kammer | 2dd9ca4 | 2020-11-25 16:06:39 -0800 | [diff] [blame] | 266 | ) |
| 267 | } |
| 268 | } |
| 269 | } |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 270 | |
| 271 | func TestModuleTypeBp2Build(t *testing.T) { |
| 272 | testCases := []struct { |
| 273 | moduleTypeUnderTest string |
| 274 | moduleTypeUnderTestFactory android.ModuleFactory |
| 275 | bp string |
| 276 | expectedBazelTarget string |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 277 | description string |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 278 | }{ |
| 279 | { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 280 | description: "filegroup with no srcs", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 281 | moduleTypeUnderTest: "filegroup", |
| 282 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 283 | bp: `filegroup { |
| 284 | name: "foo", |
| 285 | srcs: [], |
| 286 | }`, |
| 287 | expectedBazelTarget: `filegroup( |
| 288 | name = "foo", |
| 289 | srcs = [ |
| 290 | ], |
| 291 | )`, |
| 292 | }, |
| 293 | { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 294 | description: "filegroup with srcs", |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 295 | moduleTypeUnderTest: "filegroup", |
| 296 | moduleTypeUnderTestFactory: android.FileGroupFactory, |
| 297 | bp: `filegroup { |
| 298 | name: "foo", |
| 299 | srcs: ["a", "b"], |
| 300 | }`, |
| 301 | expectedBazelTarget: `filegroup( |
| 302 | name = "foo", |
| 303 | srcs = [ |
| 304 | "a", |
| 305 | "b", |
| 306 | ], |
| 307 | )`, |
| 308 | }, |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 309 | { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 310 | description: "genrule with command line variable replacements", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 311 | moduleTypeUnderTest: "genrule", |
| 312 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 313 | bp: `genrule { |
| 314 | name: "foo", |
| 315 | out: ["foo.out"], |
| 316 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 317 | tools: [":foo.tool"], |
| 318 | cmd: "$(location :foo.tool) --genDir=$(genDir) arg $(in) $(out)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 319 | }`, |
| 320 | expectedBazelTarget: `genrule( |
| 321 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 322 | cmd = "$(location :foo.tool) --genDir=$(GENDIR) arg $(SRCS) $(OUTS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 323 | outs = [ |
| 324 | "foo.out", |
| 325 | ], |
| 326 | srcs = [ |
| 327 | "foo.in", |
| 328 | ], |
| 329 | tools = [ |
| 330 | ":foo.tool", |
| 331 | ], |
| 332 | )`, |
| 333 | }, |
| 334 | { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 335 | description: "genrule using $(locations :label)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 336 | moduleTypeUnderTest: "genrule", |
| 337 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 338 | bp: `genrule { |
| 339 | name: "foo", |
| 340 | out: ["foo.out"], |
| 341 | srcs: ["foo.in"], |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 342 | tools: [":foo.tools"], |
| 343 | cmd: "$(locations :foo.tools) -s $(out) $(in)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 344 | }`, |
| 345 | expectedBazelTarget: `genrule( |
| 346 | name = "foo", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 347 | cmd = "$(locations :foo.tools) -s $(OUTS) $(SRCS)", |
| 348 | outs = [ |
| 349 | "foo.out", |
| 350 | ], |
| 351 | srcs = [ |
| 352 | "foo.in", |
| 353 | ], |
| 354 | tools = [ |
| 355 | ":foo.tools", |
| 356 | ], |
| 357 | )`, |
| 358 | }, |
| 359 | { |
| 360 | description: "genrule using $(location) label should substitute first tool label automatically", |
| 361 | moduleTypeUnderTest: "genrule", |
| 362 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 363 | bp: `genrule { |
| 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 | }`, |
| 370 | expectedBazelTarget: `genrule( |
| 371 | name = "foo", |
| 372 | cmd = "$(location :foo.tool) -s $(OUTS) $(SRCS)", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 373 | outs = [ |
| 374 | "foo.out", |
| 375 | ], |
| 376 | srcs = [ |
| 377 | "foo.in", |
| 378 | ], |
| 379 | tools = [ |
| 380 | ":foo.tool", |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 381 | ":other.tool", |
| 382 | ], |
| 383 | )`, |
| 384 | }, |
| 385 | { |
| 386 | description: "genrule using $(locations) label should substitute first tool label automatically", |
| 387 | moduleTypeUnderTest: "genrule", |
| 388 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 389 | bp: `genrule { |
| 390 | name: "foo", |
| 391 | out: ["foo.out"], |
| 392 | srcs: ["foo.in"], |
| 393 | tools: [":foo.tool", ":other.tool"], |
| 394 | cmd: "$(locations) -s $(out) $(in)", |
| 395 | }`, |
| 396 | expectedBazelTarget: `genrule( |
| 397 | name = "foo", |
| 398 | cmd = "$(locations :foo.tool) -s $(OUTS) $(SRCS)", |
| 399 | outs = [ |
| 400 | "foo.out", |
| 401 | ], |
| 402 | srcs = [ |
| 403 | "foo.in", |
| 404 | ], |
| 405 | tools = [ |
| 406 | ":foo.tool", |
| 407 | ":other.tool", |
| 408 | ], |
| 409 | )`, |
| 410 | }, |
| 411 | { |
| 412 | description: "genrule without tools or tool_files can convert successfully", |
| 413 | moduleTypeUnderTest: "genrule", |
| 414 | moduleTypeUnderTestFactory: genrule.GenRuleFactory, |
| 415 | bp: `genrule { |
| 416 | name: "foo", |
| 417 | out: ["foo.out"], |
| 418 | srcs: ["foo.in"], |
| 419 | cmd: "cp $(in) $(out)", |
| 420 | }`, |
| 421 | expectedBazelTarget: `genrule( |
| 422 | name = "foo", |
| 423 | cmd = "cp $(SRCS) $(OUTS)", |
| 424 | outs = [ |
| 425 | "foo.out", |
| 426 | ], |
| 427 | srcs = [ |
| 428 | "foo.in", |
Jingwen Chen | 316e07c | 2020-12-14 09:09:52 -0500 | [diff] [blame] | 429 | ], |
| 430 | )`, |
| 431 | }, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | dir := "." |
| 435 | for _, testCase := range testCases { |
| 436 | config := android.TestConfig(buildDir, nil, testCase.bp, nil) |
| 437 | ctx := android.NewTestContext(config) |
| 438 | ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory) |
| 439 | ctx.RegisterForBazelConversion() |
| 440 | |
| 441 | _, errs := ctx.ParseFileList(dir, []string{"Android.bp"}) |
| 442 | android.FailIfErrored(t, errs) |
| 443 | _, errs = ctx.ResolveDependencies(config) |
| 444 | android.FailIfErrored(t, errs) |
| 445 | |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 446 | bazelTargets := GenerateSoongModuleTargets(ctx.Context.Context, Bp2Build)[dir] |
Jingwen Chen | 4e4756d | 2021-01-24 21:13:13 -0500 | [diff] [blame] | 447 | if actualCount, expectedCount := len(bazelTargets), 1; actualCount != expectedCount { |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 448 | t.Fatalf("%s: Expected %d bazel target, got %d", testCase.description, expectedCount, actualCount) |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | actualBazelTarget := bazelTargets[0] |
| 452 | if actualBazelTarget.content != testCase.expectedBazelTarget { |
| 453 | t.Errorf( |
Jingwen Chen | 885ee7a | 2021-01-26 03:16:49 -0500 | [diff] [blame^] | 454 | "%s: Expected generated Bazel target to be '%s', got '%s'", |
| 455 | testCase.description, |
Jingwen Chen | 32b4ece | 2021-01-21 03:20:18 -0500 | [diff] [blame] | 456 | testCase.expectedBazelTarget, |
| 457 | actualBazelTarget.content, |
| 458 | ) |
| 459 | } |
| 460 | } |
| 461 | } |