Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 1 | // Copyright 2015 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 ( |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 18 | "fmt" |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 19 | "strings" |
Colin Cross | 2d2e68f | 2024-10-10 11:57:11 -0700 | [diff] [blame] | 20 | "sync" |
| 21 | "sync/atomic" |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | type mutatorTestModule struct { |
| 28 | ModuleBase |
| 29 | props struct { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 30 | Deps_missing_deps []string |
| 31 | Mutator_missing_deps []string |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | missingDeps []string |
| 35 | } |
| 36 | |
| 37 | func mutatorTestModuleFactory() Module { |
| 38 | module := &mutatorTestModule{} |
| 39 | module.AddProperties(&module.props) |
| 40 | InitAndroidModule(module) |
| 41 | return module |
| 42 | } |
| 43 | |
| 44 | func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 45 | ctx.Build(pctx, BuildParams{ |
| 46 | Rule: Touch, |
| 47 | Output: PathForModuleOut(ctx, "output"), |
| 48 | }) |
| 49 | |
| 50 | m.missingDeps = ctx.GetMissingDependencies() |
| 51 | } |
| 52 | |
| 53 | func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 54 | ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | func addMissingDependenciesMutator(ctx TopDownMutatorContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 58 | ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | func TestMutatorAddMissingDependencies(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 62 | bp := ` |
| 63 | test { |
| 64 | name: "foo", |
| 65 | deps_missing_deps: ["regular_missing_dep"], |
| 66 | mutator_missing_deps: ["added_missing_dep"], |
| 67 | } |
| 68 | ` |
| 69 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 70 | result := GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 71 | PrepareForTestWithAllowMissingDependencies, |
| 72 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 73 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 74 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 75 | ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator) |
| 76 | }) |
| 77 | }), |
| 78 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 79 | ).RunTest(t) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 80 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 81 | foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 82 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 83 | AssertDeepEquals(t, "foo missing deps", []string{"added_missing_dep", "regular_missing_dep"}, foo.missingDeps) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 84 | } |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 85 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 86 | func TestFinalDepsPhase(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 87 | bp := ` |
| 88 | test { |
| 89 | name: "common_dep_1", |
| 90 | } |
| 91 | test { |
| 92 | name: "common_dep_2", |
| 93 | } |
| 94 | test { |
| 95 | name: "foo", |
| 96 | } |
| 97 | ` |
| 98 | |
Colin Cross | 2d2e68f | 2024-10-10 11:57:11 -0700 | [diff] [blame] | 99 | finalGot := sync.Map{} |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 100 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 101 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 102 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 103 | dep1Tag := struct { |
| 104 | blueprint.BaseDependencyTag |
| 105 | }{} |
| 106 | dep2Tag := struct { |
| 107 | blueprint.BaseDependencyTag |
| 108 | }{} |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 109 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 110 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 111 | ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) { |
| 112 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 113 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") |
| 114 | } |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 115 | }) |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 116 | ctx.Transition("variant", &testTransitionMutator{ |
| 117 | split: func(ctx BaseModuleContext) []string { |
| 118 | return []string{"a", "b"} |
| 119 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 120 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 121 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 122 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 123 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
| 124 | ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) { |
| 125 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 126 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2") |
| 127 | } |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 128 | }) |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 129 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
Colin Cross | 2d2e68f | 2024-10-10 11:57:11 -0700 | [diff] [blame] | 130 | counter, _ := finalGot.LoadOrStore(ctx.Module().String(), &atomic.Int64{}) |
| 131 | counter.(*atomic.Int64).Add(1) |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 132 | ctx.VisitDirectDeps(func(mod Module) { |
Colin Cross | 2d2e68f | 2024-10-10 11:57:11 -0700 | [diff] [blame] | 133 | counter, _ := finalGot.LoadOrStore(fmt.Sprintf("%s -> %s", ctx.Module().String(), mod), &atomic.Int64{}) |
| 134 | counter.(*atomic.Int64).Add(1) |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 135 | }) |
Colin Cross | 8a96280 | 2024-10-09 15:29:27 -0700 | [diff] [blame] | 136 | }) |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 137 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 138 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 139 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 140 | }), |
| 141 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 142 | ).RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 143 | |
| 144 | finalWant := map[string]int{ |
| 145 | "common_dep_1{variant:a}": 1, |
| 146 | "common_dep_1{variant:b}": 1, |
| 147 | "common_dep_2{variant:a}": 1, |
| 148 | "common_dep_2{variant:b}": 1, |
| 149 | "foo{variant:a}": 1, |
| 150 | "foo{variant:a} -> common_dep_1{variant:a}": 1, |
| 151 | "foo{variant:a} -> common_dep_2{variant:a}": 1, |
| 152 | "foo{variant:b}": 1, |
| 153 | "foo{variant:b} -> common_dep_1{variant:b}": 1, |
| 154 | "foo{variant:b} -> common_dep_2{variant:a}": 1, |
| 155 | } |
| 156 | |
Colin Cross | 2d2e68f | 2024-10-10 11:57:11 -0700 | [diff] [blame] | 157 | finalGotMap := make(map[string]int) |
| 158 | finalGot.Range(func(k, v any) bool { |
| 159 | finalGotMap[k.(string)] = int(v.(*atomic.Int64).Load()) |
| 160 | return true |
| 161 | }) |
| 162 | |
| 163 | AssertDeepEquals(t, "final", finalWant, finalGotMap) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 164 | } |