Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 android |
| 16 | |
| 17 | import ( |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 18 | "fmt" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 19 | "testing" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 24 | func TestPrebuilts(t *testing.T) { |
| 25 | buildOS := TestArchConfig(t.TempDir(), nil, "", nil).BuildOS |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 26 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 27 | var prebuiltsTests = []struct { |
| 28 | name string |
| 29 | replaceBp bool // modules is added to default bp boilerplate if false. |
| 30 | modules string |
| 31 | prebuilt []OsType |
| 32 | preparer FixturePreparer |
| 33 | }{ |
| 34 | { |
| 35 | name: "no prebuilt", |
| 36 | modules: ` |
| 37 | source { |
| 38 | name: "bar", |
| 39 | }`, |
| 40 | prebuilt: nil, |
| 41 | }, |
| 42 | { |
| 43 | name: "no source prebuilt not preferred", |
| 44 | modules: ` |
| 45 | prebuilt { |
| 46 | name: "bar", |
| 47 | prefer: false, |
| 48 | srcs: ["prebuilt_file"], |
| 49 | }`, |
| 50 | prebuilt: []OsType{Android, buildOS}, |
| 51 | }, |
| 52 | { |
| 53 | name: "no source prebuilt preferred", |
| 54 | modules: ` |
| 55 | prebuilt { |
| 56 | name: "bar", |
| 57 | prefer: true, |
| 58 | srcs: ["prebuilt_file"], |
| 59 | }`, |
| 60 | prebuilt: []OsType{Android, buildOS}, |
| 61 | }, |
| 62 | { |
| 63 | name: "prebuilt not preferred", |
| 64 | modules: ` |
| 65 | source { |
| 66 | name: "bar", |
| 67 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 68 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 69 | prebuilt { |
| 70 | name: "bar", |
| 71 | prefer: false, |
| 72 | srcs: ["prebuilt_file"], |
| 73 | }`, |
| 74 | prebuilt: nil, |
| 75 | }, |
| 76 | { |
| 77 | name: "prebuilt preferred", |
| 78 | modules: ` |
| 79 | source { |
| 80 | name: "bar", |
| 81 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 82 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 83 | prebuilt { |
| 84 | name: "bar", |
| 85 | prefer: true, |
| 86 | srcs: ["prebuilt_file"], |
| 87 | }`, |
| 88 | prebuilt: []OsType{Android, buildOS}, |
| 89 | }, |
| 90 | { |
| 91 | name: "prebuilt no file not preferred", |
| 92 | modules: ` |
| 93 | source { |
| 94 | name: "bar", |
| 95 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 96 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 97 | prebuilt { |
| 98 | name: "bar", |
| 99 | prefer: false, |
| 100 | }`, |
| 101 | prebuilt: nil, |
| 102 | }, |
| 103 | { |
| 104 | name: "prebuilt no file preferred", |
| 105 | modules: ` |
| 106 | source { |
| 107 | name: "bar", |
| 108 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 109 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 110 | prebuilt { |
| 111 | name: "bar", |
| 112 | prefer: true, |
| 113 | }`, |
| 114 | prebuilt: nil, |
| 115 | }, |
| 116 | { |
| 117 | name: "prebuilt file from filegroup preferred", |
| 118 | modules: ` |
| 119 | filegroup { |
| 120 | name: "fg", |
| 121 | srcs: ["prebuilt_file"], |
| 122 | } |
| 123 | prebuilt { |
| 124 | name: "bar", |
| 125 | prefer: true, |
| 126 | srcs: [":fg"], |
| 127 | }`, |
| 128 | prebuilt: []OsType{Android, buildOS}, |
| 129 | }, |
| 130 | { |
| 131 | name: "prebuilt module for device only", |
| 132 | modules: ` |
| 133 | source { |
| 134 | name: "bar", |
| 135 | } |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 136 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 137 | prebuilt { |
| 138 | name: "bar", |
| 139 | host_supported: false, |
| 140 | prefer: true, |
| 141 | srcs: ["prebuilt_file"], |
| 142 | }`, |
| 143 | prebuilt: []OsType{Android}, |
| 144 | }, |
| 145 | { |
| 146 | name: "prebuilt file for host only", |
| 147 | modules: ` |
| 148 | source { |
| 149 | name: "bar", |
| 150 | } |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 151 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 152 | prebuilt { |
| 153 | name: "bar", |
| 154 | prefer: true, |
| 155 | target: { |
| 156 | host: { |
| 157 | srcs: ["prebuilt_file"], |
Liz Kammer | 69d6413 | 2021-06-24 13:03:06 -0400 | [diff] [blame] | 158 | }, |
| 159 | }, |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 160 | }`, |
| 161 | prebuilt: []OsType{buildOS}, |
| 162 | }, |
| 163 | { |
| 164 | name: "prebuilt override not preferred", |
| 165 | modules: ` |
| 166 | source { |
| 167 | name: "baz", |
| 168 | } |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 169 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 170 | override_source { |
| 171 | name: "bar", |
| 172 | base: "baz", |
| 173 | } |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 174 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 175 | prebuilt { |
| 176 | name: "bar", |
| 177 | prefer: false, |
| 178 | srcs: ["prebuilt_file"], |
| 179 | }`, |
| 180 | prebuilt: nil, |
| 181 | }, |
| 182 | { |
| 183 | name: "prebuilt override preferred", |
| 184 | modules: ` |
| 185 | source { |
| 186 | name: "baz", |
| 187 | } |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 188 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 189 | override_source { |
| 190 | name: "bar", |
| 191 | base: "baz", |
| 192 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 193 | |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 194 | prebuilt { |
| 195 | name: "bar", |
| 196 | prefer: true, |
| 197 | srcs: ["prebuilt_file"], |
| 198 | }`, |
| 199 | prebuilt: []OsType{Android, buildOS}, |
| 200 | }, |
| 201 | { |
| 202 | name: "prebuilt including default-disabled OS", |
| 203 | replaceBp: true, |
| 204 | modules: ` |
| 205 | source { |
| 206 | name: "foo", |
| 207 | deps: [":bar"], |
| 208 | target: { |
| 209 | windows: { |
| 210 | enabled: true, |
| 211 | }, |
| 212 | }, |
| 213 | } |
| 214 | |
| 215 | source { |
| 216 | name: "bar", |
| 217 | target: { |
| 218 | windows: { |
| 219 | enabled: true, |
| 220 | }, |
| 221 | }, |
| 222 | } |
| 223 | |
| 224 | prebuilt { |
| 225 | name: "bar", |
| 226 | prefer: true, |
| 227 | srcs: ["prebuilt_file"], |
| 228 | target: { |
| 229 | windows: { |
| 230 | enabled: true, |
| 231 | }, |
| 232 | }, |
| 233 | }`, |
| 234 | prebuilt: []OsType{Android, buildOS, Windows}, |
| 235 | }, |
| 236 | { |
| 237 | name: "fall back to source for default-disabled OS", |
| 238 | replaceBp: true, |
| 239 | modules: ` |
| 240 | source { |
| 241 | name: "foo", |
| 242 | deps: [":bar"], |
| 243 | target: { |
| 244 | windows: { |
| 245 | enabled: true, |
| 246 | }, |
| 247 | }, |
| 248 | } |
| 249 | |
| 250 | source { |
| 251 | name: "bar", |
| 252 | target: { |
| 253 | windows: { |
| 254 | enabled: true, |
| 255 | }, |
| 256 | }, |
| 257 | } |
| 258 | |
| 259 | prebuilt { |
| 260 | name: "bar", |
| 261 | prefer: true, |
| 262 | srcs: ["prebuilt_file"], |
| 263 | }`, |
| 264 | prebuilt: []OsType{Android, buildOS}, |
| 265 | }, |
| 266 | { |
| 267 | name: "prebuilt properties customizable", |
| 268 | replaceBp: true, |
| 269 | modules: ` |
| 270 | source { |
| 271 | name: "foo", |
| 272 | deps: [":bar"], |
| 273 | } |
| 274 | |
| 275 | soong_config_module_type { |
| 276 | name: "prebuilt_with_config", |
| 277 | module_type: "prebuilt", |
| 278 | config_namespace: "any_namespace", |
| 279 | bool_variables: ["bool_var"], |
| 280 | properties: ["prefer"], |
| 281 | } |
| 282 | |
| 283 | prebuilt_with_config { |
| 284 | name: "bar", |
| 285 | prefer: true, |
| 286 | srcs: ["prebuilt_file"], |
| 287 | soong_config_variables: { |
| 288 | bool_var: { |
| 289 | prefer: false, |
| 290 | conditions_default: { |
| 291 | prefer: true, |
| 292 | }, |
| 293 | }, |
| 294 | }, |
| 295 | }`, |
| 296 | prebuilt: []OsType{Android, buildOS}, |
| 297 | }, |
| 298 | { |
| 299 | name: "prebuilt use_source_config_var={acme, use_source} - no var specified", |
| 300 | modules: ` |
| 301 | source { |
| 302 | name: "bar", |
| 303 | } |
| 304 | |
| 305 | prebuilt { |
| 306 | name: "bar", |
| 307 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 308 | srcs: ["prebuilt_file"], |
| 309 | }`, |
| 310 | // When use_source_env is specified then it will use the prebuilt by default if the environment |
| 311 | // variable is not set. |
| 312 | prebuilt: []OsType{Android, buildOS}, |
| 313 | }, |
| 314 | { |
| 315 | name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=false", |
| 316 | modules: ` |
| 317 | source { |
| 318 | name: "bar", |
| 319 | } |
| 320 | |
| 321 | prebuilt { |
| 322 | name: "bar", |
| 323 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 324 | srcs: ["prebuilt_file"], |
| 325 | }`, |
| 326 | preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 327 | variables.VendorVars = map[string]map[string]string{ |
| 328 | "acme": { |
| 329 | "use_source": "false", |
| 330 | }, |
| 331 | } |
| 332 | }), |
| 333 | // Setting the environment variable named in use_source_env to false will cause the prebuilt to |
| 334 | // be used. |
| 335 | prebuilt: []OsType{Android, buildOS}, |
| 336 | }, |
| 337 | { |
Spandan Das | 1c4d94d | 2023-10-26 22:38:34 +0000 | [diff] [blame] | 338 | name: "apex_contributions supersedes any source preferred via use_source_config_var", |
| 339 | modules: ` |
| 340 | source { |
| 341 | name: "bar", |
| 342 | } |
| 343 | |
| 344 | prebuilt { |
| 345 | name: "bar", |
| 346 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 347 | srcs: ["prebuilt_file"], |
| 348 | } |
| 349 | apex_contributions { |
| 350 | name: "my_mainline_module_contribution", |
| 351 | api_domain: "apexfoo", |
| 352 | // this metadata module contains prebuilt |
| 353 | contents: ["prebuilt_bar"], |
| 354 | } |
| 355 | all_apex_contributions { |
| 356 | name: "all_apex_contributions", |
| 357 | } |
| 358 | `, |
| 359 | preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 360 | variables.VendorVars = map[string]map[string]string{ |
| 361 | "acme": { |
| 362 | "use_source": "true", |
| 363 | }, |
| 364 | } |
| 365 | variables.BuildFlags = map[string]string{ |
| 366 | "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contribution", |
| 367 | } |
| 368 | }), |
| 369 | // use_source_config_var indicates that source should be used |
| 370 | // but this is superseded by `my_mainline_module_contribution` |
| 371 | prebuilt: []OsType{Android, buildOS}, |
| 372 | }, |
| 373 | { |
| 374 | name: "apex_contributions supersedes any prebuilt preferred via use_source_config_var", |
| 375 | modules: ` |
| 376 | source { |
| 377 | name: "bar", |
| 378 | } |
| 379 | |
| 380 | prebuilt { |
| 381 | name: "bar", |
| 382 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 383 | srcs: ["prebuilt_file"], |
| 384 | } |
| 385 | apex_contributions { |
| 386 | name: "my_mainline_module_contribution", |
| 387 | api_domain: "apexfoo", |
| 388 | // this metadata module contains source |
| 389 | contents: ["bar"], |
| 390 | } |
| 391 | all_apex_contributions { |
| 392 | name: "all_apex_contributions", |
| 393 | } |
| 394 | `, |
| 395 | preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 396 | variables.VendorVars = map[string]map[string]string{ |
| 397 | "acme": { |
| 398 | "use_source": "false", |
| 399 | }, |
| 400 | } |
| 401 | variables.BuildFlags = map[string]string{ |
| 402 | "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_mainline_module_contribution", |
| 403 | } |
| 404 | }), |
| 405 | // use_source_config_var indicates that prebuilt should be used |
| 406 | // but this is superseded by `my_mainline_module_contribution` |
| 407 | prebuilt: nil, |
| 408 | }, |
| 409 | { |
Colin Cross | 0c66bc6 | 2021-07-20 09:47:41 -0700 | [diff] [blame] | 410 | name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true", |
| 411 | modules: ` |
| 412 | source { |
| 413 | name: "bar", |
| 414 | } |
| 415 | |
| 416 | prebuilt { |
| 417 | name: "bar", |
| 418 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 419 | srcs: ["prebuilt_file"], |
| 420 | }`, |
| 421 | preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 422 | variables.VendorVars = map[string]map[string]string{ |
| 423 | "acme": { |
| 424 | "use_source": "true", |
| 425 | }, |
| 426 | } |
| 427 | }), |
| 428 | // Setting the environment variable named in use_source_env to true will cause the source to be |
| 429 | // used. |
| 430 | prebuilt: nil, |
| 431 | }, |
| 432 | { |
| 433 | name: "prebuilt use_source_config_var={acme, use_source} - acme_use_source=true, no source", |
| 434 | modules: ` |
| 435 | prebuilt { |
| 436 | name: "bar", |
| 437 | use_source_config_var: {config_namespace: "acme", var_name: "use_source"}, |
| 438 | srcs: ["prebuilt_file"], |
| 439 | }`, |
| 440 | preparer: FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 441 | variables.VendorVars = map[string]map[string]string{ |
| 442 | "acme": { |
| 443 | "use_source": "true", |
| 444 | }, |
| 445 | } |
| 446 | }), |
| 447 | // Although the environment variable says to use source there is no source available. |
| 448 | prebuilt: []OsType{Android, buildOS}, |
| 449 | }, |
| 450 | } |
| 451 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 452 | fs := MockFS{ |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 453 | "prebuilt_file": nil, |
| 454 | "source_file": nil, |
| 455 | } |
Colin Cross | 0d614dd | 2016-10-14 15:38:43 -0700 | [diff] [blame] | 456 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 457 | for _, test := range prebuiltsTests { |
| 458 | t.Run(test.name, func(t *testing.T) { |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 459 | bp := test.modules |
| 460 | if !test.replaceBp { |
| 461 | bp = bp + ` |
| 462 | source { |
| 463 | name: "foo", |
| 464 | deps: [":bar"], |
| 465 | }` |
| 466 | } |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 467 | |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 468 | // Add windows to the target list to test the logic when a variant is |
| 469 | // disabled by default. |
| 470 | if !Windows.DefaultDisabled { |
| 471 | t.Errorf("windows is assumed to be disabled by default") |
| 472 | } |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 473 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 474 | result := GroupFixturePreparers( |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 475 | PrepareForTestWithArchMutator, |
| 476 | PrepareForTestWithPrebuilts, |
| 477 | PrepareForTestWithOverrides, |
| 478 | PrepareForTestWithFilegroup, |
| 479 | // Add a Windows target to the configuration. |
| 480 | FixtureModifyConfig(func(config Config) { |
| 481 | config.Targets[Windows] = []Target{ |
| 482 | {Windows, Arch{ArchType: X86_64}, NativeBridgeDisabled, "", "", true}, |
| 483 | } |
| 484 | }), |
| 485 | fs.AddToFixture(), |
| 486 | FixtureRegisterWithContext(registerTestPrebuiltModules), |
Paul Duffin | 0c52c7b | 2021-07-06 17:15:25 +0100 | [diff] [blame] | 487 | OptionalFixturePreparer(test.preparer), |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 488 | ).RunTestWithBp(t, bp) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 489 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 490 | for _, variant := range result.ModuleVariantsForTests("foo") { |
| 491 | foo := result.ModuleForTests("foo", variant) |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 492 | t.Run(foo.Module().Target().Os.String(), func(t *testing.T) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 493 | var dependsOnSourceModule, dependsOnPrebuiltModule bool |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 494 | result.VisitDirectDeps(foo.Module(), func(m blueprint.Module) { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 495 | if _, ok := m.(*sourceModule); ok { |
| 496 | dependsOnSourceModule = true |
| 497 | } |
| 498 | if p, ok := m.(*prebuiltModule); ok { |
| 499 | dependsOnPrebuiltModule = true |
| 500 | if !p.Prebuilt().properties.UsePrebuilt { |
| 501 | t.Errorf("dependency on prebuilt module not marked used") |
| 502 | } |
| 503 | } |
| 504 | }) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 505 | |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 506 | moduleIsDisabled := !foo.Module().Enabled() |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 507 | deps := foo.Module().(*sourceModule).deps |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 508 | if moduleIsDisabled { |
| 509 | if len(deps) > 0 { |
| 510 | t.Errorf("disabled module got deps: %v", deps) |
| 511 | } |
| 512 | } else { |
| 513 | if len(deps) != 1 { |
| 514 | t.Errorf("deps does not have single path, but is %v", deps) |
| 515 | } |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 516 | } |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 517 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 518 | var usingSourceFile, usingPrebuiltFile bool |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 519 | if len(deps) > 0 && deps[0].String() == "source_file" { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 520 | usingSourceFile = true |
| 521 | } |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 522 | if len(deps) > 0 && deps[0].String() == "prebuilt_file" { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 523 | usingPrebuiltFile = true |
| 524 | } |
Colin Cross | c3e7fa6 | 2017-03-17 13:14:32 -0700 | [diff] [blame] | 525 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 526 | prebuilt := false |
| 527 | for _, os := range test.prebuilt { |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 528 | if os == foo.Module().Target().Os { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 529 | prebuilt = true |
| 530 | } |
| 531 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 532 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 533 | if prebuilt { |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 534 | if moduleIsDisabled { |
| 535 | t.Errorf("dependent module for prebuilt is disabled") |
| 536 | } |
| 537 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 538 | if !dependsOnPrebuiltModule { |
| 539 | t.Errorf("doesn't depend on prebuilt module") |
| 540 | } |
| 541 | if !usingPrebuiltFile { |
| 542 | t.Errorf("doesn't use prebuilt_file") |
| 543 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 544 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 545 | if dependsOnSourceModule { |
| 546 | t.Errorf("depends on source module") |
| 547 | } |
| 548 | if usingSourceFile { |
| 549 | t.Errorf("using source_file") |
| 550 | } |
Martin Stjernholm | f467732 | 2020-07-07 02:20:40 +0100 | [diff] [blame] | 551 | } else if !moduleIsDisabled { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 552 | if dependsOnPrebuiltModule { |
| 553 | t.Errorf("depends on prebuilt module") |
| 554 | } |
| 555 | if usingPrebuiltFile { |
| 556 | t.Errorf("using prebuilt_file") |
| 557 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 558 | |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 559 | if !dependsOnSourceModule { |
| 560 | t.Errorf("doesn't depend on source module") |
| 561 | } |
| 562 | if !usingSourceFile { |
| 563 | t.Errorf("doesn't use source_file") |
| 564 | } |
| 565 | } |
| 566 | }) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 567 | } |
| 568 | }) |
| 569 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 572 | func testPrebuiltErrorWithFixture(t *testing.T, expectedError, bp string, fixture FixturePreparer) { |
Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 573 | t.Helper() |
| 574 | fs := MockFS{ |
| 575 | "prebuilt_file": nil, |
| 576 | } |
| 577 | GroupFixturePreparers( |
| 578 | PrepareForTestWithArchMutator, |
| 579 | PrepareForTestWithPrebuilts, |
| 580 | PrepareForTestWithOverrides, |
| 581 | fs.AddToFixture(), |
| 582 | FixtureRegisterWithContext(registerTestPrebuiltModules), |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 583 | OptionalFixturePreparer(fixture), |
Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 584 | ). |
| 585 | ExtendWithErrorHandler(FixtureExpectsAtLeastOneErrorMatchingPattern(expectedError)). |
| 586 | RunTestWithBp(t, bp) |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 587 | |
| 588 | } |
| 589 | |
| 590 | func testPrebuiltError(t *testing.T, expectedError, bp string) { |
| 591 | testPrebuiltErrorWithFixture(t, expectedError, bp, nil) |
Jooyung Han | ebaa573 | 2023-05-02 11:43:14 +0900 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | func TestPrebuiltShouldNotChangePartition(t *testing.T) { |
| 595 | testPrebuiltError(t, `partition is different`, ` |
| 596 | source { |
| 597 | name: "foo", |
| 598 | vendor: true, |
| 599 | } |
| 600 | prebuilt { |
| 601 | name: "foo", |
| 602 | prefer: true, |
| 603 | srcs: ["prebuilt_file"], |
| 604 | }`) |
| 605 | } |
| 606 | |
| 607 | func TestPrebuiltShouldNotChangePartition_WithOverride(t *testing.T) { |
| 608 | testPrebuiltError(t, `partition is different`, ` |
| 609 | source { |
| 610 | name: "foo", |
| 611 | vendor: true, |
| 612 | } |
| 613 | override_source { |
| 614 | name: "bar", |
| 615 | base: "foo", |
| 616 | } |
| 617 | prebuilt { |
| 618 | name: "bar", |
| 619 | prefer: true, |
| 620 | srcs: ["prebuilt_file"], |
| 621 | }`) |
| 622 | } |
| 623 | |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 624 | func registerTestPrebuiltBuildComponents(ctx RegistrationContext) { |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 625 | registerTestPrebuiltModules(ctx) |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 626 | |
| 627 | RegisterPrebuiltMutators(ctx) |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 628 | ctx.PostDepsMutators(RegisterOverridePostDepsMutators) |
Paul Duffin | 78ac5b9 | 2020-01-14 12:42:08 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Paul Duffin | cfd3374 | 2021-02-27 11:59:02 +0000 | [diff] [blame] | 631 | var prepareForTestWithFakePrebuiltModules = FixtureRegisterWithContext(registerTestPrebuiltModules) |
| 632 | |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 633 | func registerTestPrebuiltModules(ctx RegistrationContext) { |
| 634 | ctx.RegisterModuleType("prebuilt", newPrebuiltModule) |
| 635 | ctx.RegisterModuleType("source", newSourceModule) |
| 636 | ctx.RegisterModuleType("override_source", newOverrideSourceModule) |
Jingwen Chen | a47f28d | 2021-11-02 16:43:57 +0000 | [diff] [blame] | 637 | ctx.RegisterModuleType("soong_config_module_type", SoongConfigModuleTypeFactory) |
| 638 | ctx.RegisterModuleType("soong_config_string_variable", SoongConfigStringVariableDummyFactory) |
| 639 | ctx.RegisterModuleType("soong_config_bool_variable", SoongConfigBoolVariableDummyFactory) |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 640 | RegisterApexContributionsBuildComponents(ctx) |
Paul Duffin | 3581612 | 2021-02-24 01:49:52 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 643 | type prebuiltModule struct { |
| 644 | ModuleBase |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 645 | prebuilt Prebuilt |
| 646 | properties struct { |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 647 | Srcs []string `android:"path,arch_variant"` |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 648 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 649 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 650 | } |
| 651 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 652 | func newPrebuiltModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 653 | m := &prebuiltModule{} |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 654 | m.AddProperties(&m.properties) |
| 655 | InitPrebuiltModule(m, &m.properties.Srcs) |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 656 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 657 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | func (p *prebuiltModule) Name() string { |
| 661 | return p.prebuilt.Name(p.ModuleBase.Name()) |
| 662 | } |
| 663 | |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 664 | func (p *prebuiltModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 665 | if len(p.properties.Srcs) >= 1 { |
| 666 | p.src = p.prebuilt.SingleSourcePath(ctx) |
| 667 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | func (p *prebuiltModule) Prebuilt() *Prebuilt { |
| 671 | return &p.prebuilt |
| 672 | } |
| 673 | |
Colin Cross | 41955e8 | 2019-05-29 14:40:35 -0700 | [diff] [blame] | 674 | func (p *prebuiltModule) OutputFiles(tag string) (Paths, error) { |
| 675 | switch tag { |
| 676 | case "": |
| 677 | return Paths{p.src}, nil |
| 678 | default: |
| 679 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 680 | } |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 681 | } |
| 682 | |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 683 | type sourceModuleProperties struct { |
| 684 | Deps []string `android:"path,arch_variant"` |
| 685 | } |
| 686 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 687 | type sourceModule struct { |
| 688 | ModuleBase |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 689 | OverridableModuleBase |
| 690 | |
| 691 | properties sourceModuleProperties |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 692 | dependsOnSourceModule, dependsOnPrebuiltModule bool |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 693 | deps Paths |
| 694 | src Path |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 697 | func newSourceModule() Module { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 698 | m := &sourceModule{} |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 699 | m.AddProperties(&m.properties) |
Martin Stjernholm | 009a9dc | 2020-03-05 17:34:13 +0000 | [diff] [blame] | 700 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 701 | InitOverridableModule(m, nil) |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 702 | return m |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 705 | func (s *sourceModule) OverridablePropertiesDepsMutator(ctx BottomUpMutatorContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 706 | // s.properties.Deps are annotated with android:path, so they are |
| 707 | // automatically added to the dependency by pathDeps mutator |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | func (s *sourceModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
Jiyong Park | d467111 | 2019-04-19 17:23:04 +0900 | [diff] [blame] | 711 | s.deps = PathsForModuleSrc(ctx, s.properties.Deps) |
| 712 | s.src = PathForModuleSrc(ctx, "source_file") |
| 713 | } |
| 714 | |
| 715 | func (s *sourceModule) Srcs() Paths { |
| 716 | return Paths{s.src} |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 717 | } |
Jaewoong Jung | fb25a64 | 2020-06-11 10:37:51 -0700 | [diff] [blame] | 718 | |
| 719 | type overrideSourceModule struct { |
| 720 | ModuleBase |
| 721 | OverrideModuleBase |
| 722 | } |
| 723 | |
| 724 | func (o *overrideSourceModule) GenerateAndroidBuildActions(_ ModuleContext) { |
| 725 | } |
| 726 | |
| 727 | func newOverrideSourceModule() Module { |
| 728 | m := &overrideSourceModule{} |
| 729 | m.AddProperties(&sourceModuleProperties{}) |
| 730 | |
| 731 | InitAndroidArchModule(m, HostAndDeviceDefault, MultilibCommon) |
| 732 | InitOverrideModule(m) |
| 733 | return m |
| 734 | } |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 735 | |
| 736 | func TestPrebuiltErrorCannotListBothSourceAndPrebuiltInContributions(t *testing.T) { |
| 737 | selectMainlineModuleContritbutions := GroupFixturePreparers( |
| 738 | FixtureModifyProductVariables(func(variables FixtureProductVariables) { |
| 739 | variables.BuildFlags = map[string]string{ |
| 740 | "RELEASE_APEX_CONTRIBUTIONS_ADSERVICES": "my_apex_contributions", |
| 741 | } |
| 742 | }), |
| 743 | ) |
Spandan Das | 3576e76 | 2024-01-03 18:57:03 +0000 | [diff] [blame] | 744 | testPrebuiltErrorWithFixture(t, `Found duplicate variations of the same module in apex_contributions: foo and prebuilt_foo. Please remove one of these`, ` |
Spandan Das | e3fcb41 | 2023-10-26 20:48:02 +0000 | [diff] [blame] | 745 | source { |
| 746 | name: "foo", |
| 747 | } |
| 748 | prebuilt { |
| 749 | name: "foo", |
| 750 | srcs: ["prebuilt_file"], |
| 751 | } |
| 752 | apex_contributions { |
| 753 | name: "my_apex_contributions", |
| 754 | api_domain: "my_mainline_module", |
| 755 | contents: [ |
| 756 | "foo", |
| 757 | "prebuilt_foo", |
| 758 | ], |
| 759 | } |
| 760 | all_apex_contributions { |
| 761 | name: "all_apex_contributions", |
| 762 | } |
| 763 | `, selectMainlineModuleContritbutions) |
| 764 | } |