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 | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 20 | "testing" |
| 21 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 23 | ) |
| 24 | |
| 25 | type mutatorTestModule struct { |
| 26 | ModuleBase |
| 27 | props struct { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 28 | Deps_missing_deps []string |
| 29 | Mutator_missing_deps []string |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | missingDeps []string |
| 33 | } |
| 34 | |
| 35 | func mutatorTestModuleFactory() Module { |
| 36 | module := &mutatorTestModule{} |
| 37 | module.AddProperties(&module.props) |
| 38 | InitAndroidModule(module) |
| 39 | return module |
| 40 | } |
| 41 | |
| 42 | func (m *mutatorTestModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 43 | ctx.Build(pctx, BuildParams{ |
| 44 | Rule: Touch, |
| 45 | Output: PathForModuleOut(ctx, "output"), |
| 46 | }) |
| 47 | |
| 48 | m.missingDeps = ctx.GetMissingDependencies() |
| 49 | } |
| 50 | |
| 51 | func (m *mutatorTestModule) DepsMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 52 | ctx.AddDependency(ctx.Module(), nil, m.props.Deps_missing_deps...) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func addMissingDependenciesMutator(ctx TopDownMutatorContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 56 | ctx.AddMissingDependencies(ctx.Module().(*mutatorTestModule).props.Mutator_missing_deps) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func TestMutatorAddMissingDependencies(t *testing.T) { |
Colin Cross | 98be1bb | 2019-12-13 20:41:13 -0800 | [diff] [blame] | 60 | bp := ` |
| 61 | test { |
| 62 | name: "foo", |
| 63 | deps_missing_deps: ["regular_missing_dep"], |
| 64 | mutator_missing_deps: ["added_missing_dep"], |
| 65 | } |
| 66 | ` |
| 67 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 68 | result := GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 69 | PrepareForTestWithAllowMissingDependencies, |
| 70 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 71 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 72 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 73 | ctx.TopDown("add_missing_dependencies", addMissingDependenciesMutator) |
| 74 | }) |
| 75 | }), |
| 76 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 77 | ).RunTest(t) |
Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 78 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 79 | foo := result.ModuleForTests("foo", "").Module().(*mutatorTestModule) |
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 | 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] | 82 | } |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 83 | |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 84 | type testTransitionMutator struct { |
| 85 | split func(ctx BaseModuleContext) []string |
| 86 | outgoingTransition func(ctx OutgoingTransitionContext, sourceVariation string) string |
| 87 | incomingTransition func(ctx IncomingTransitionContext, incomingVariation string) string |
| 88 | mutate func(ctx BottomUpMutatorContext, variation string) |
| 89 | } |
| 90 | |
| 91 | func (t *testTransitionMutator) Split(ctx BaseModuleContext) []string { |
| 92 | if t.split != nil { |
| 93 | return t.split(ctx) |
| 94 | } |
| 95 | return []string{""} |
| 96 | } |
| 97 | |
| 98 | func (t *testTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string { |
| 99 | if t.outgoingTransition != nil { |
| 100 | return t.outgoingTransition(ctx, sourceVariation) |
| 101 | } |
| 102 | return sourceVariation |
| 103 | } |
| 104 | |
| 105 | func (t *testTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string { |
| 106 | if t.incomingTransition != nil { |
| 107 | return t.incomingTransition(ctx, incomingVariation) |
| 108 | } |
| 109 | return incomingVariation |
| 110 | } |
| 111 | |
| 112 | func (t *testTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) { |
| 113 | if t.mutate != nil { |
| 114 | t.mutate(ctx, variation) |
| 115 | } |
| 116 | } |
| 117 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 118 | func TestModuleString(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 119 | bp := ` |
| 120 | test { |
| 121 | name: "foo", |
| 122 | } |
| 123 | ` |
| 124 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 125 | var moduleStrings []string |
| 126 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 127 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 128 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 129 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 130 | ctx.PreArchMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 131 | ctx.Transition("pre_arch", &testTransitionMutator{ |
| 132 | split: func(ctx BaseModuleContext) []string { |
| 133 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 134 | return []string{"a", "b"} |
| 135 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 136 | }) |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 137 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 138 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 139 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 140 | ctx.Transition("pre_deps", &testTransitionMutator{ |
| 141 | split: func(ctx BaseModuleContext) []string { |
| 142 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 143 | return []string{"c", "d"} |
| 144 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 145 | }) |
| 146 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 147 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 148 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 149 | ctx.Transition("post_deps", &testTransitionMutator{ |
| 150 | split: func(ctx BaseModuleContext) []string { |
| 151 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 152 | return []string{"e", "f"} |
| 153 | }, |
| 154 | outgoingTransition: func(ctx OutgoingTransitionContext, sourceVariation string) string { |
| 155 | return "" |
| 156 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 157 | }) |
| 158 | ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) { |
| 159 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 160 | ctx.Rename(ctx.Module().base().Name() + "_renamed1") |
Colin Cross | b8533a8 | 2024-10-05 15:25:09 -0700 | [diff] [blame^] | 161 | }).UsesRename() |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 162 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 163 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 164 | }) |
| 165 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 166 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 167 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 168 | }), |
| 169 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 170 | ).RunTest(t) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 171 | |
| 172 | want := []string{ |
| 173 | // Initial name. |
| 174 | "foo{}", |
| 175 | |
| 176 | // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order). |
| 177 | "foo{pre_arch:b}", |
| 178 | "foo{pre_arch:a}", |
| 179 | |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 180 | // After pre_deps (reversed because post_deps TransitionMutator.Split is TopDown). |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 181 | "foo{pre_arch:b,pre_deps:d}", |
| 182 | "foo{pre_arch:b,pre_deps:c}", |
| 183 | "foo{pre_arch:a,pre_deps:d}", |
| 184 | "foo{pre_arch:a,pre_deps:c}", |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 185 | |
| 186 | // After post_deps. |
Colin Cross | b2388e3 | 2024-10-07 15:05:23 -0700 | [diff] [blame] | 187 | "foo{pre_arch:a,pre_deps:c,post_deps:e}", |
| 188 | "foo{pre_arch:a,pre_deps:c,post_deps:f}", |
| 189 | "foo{pre_arch:a,pre_deps:d,post_deps:e}", |
| 190 | "foo{pre_arch:a,pre_deps:d,post_deps:f}", |
| 191 | "foo{pre_arch:b,pre_deps:c,post_deps:e}", |
| 192 | "foo{pre_arch:b,pre_deps:c,post_deps:f}", |
| 193 | "foo{pre_arch:b,pre_deps:d,post_deps:e}", |
| 194 | "foo{pre_arch:b,pre_deps:d,post_deps:f}", |
| 195 | |
| 196 | // After rename_bottom_up. |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 197 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}", |
| 198 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}", |
| 199 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}", |
| 200 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}", |
| 201 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}", |
| 202 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}", |
| 203 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}", |
| 204 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}", |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 207 | AssertDeepEquals(t, "module String() values", want, moduleStrings) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 208 | } |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 209 | |
| 210 | func TestFinalDepsPhase(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 211 | bp := ` |
| 212 | test { |
| 213 | name: "common_dep_1", |
| 214 | } |
| 215 | test { |
| 216 | name: "common_dep_2", |
| 217 | } |
| 218 | test { |
| 219 | name: "foo", |
| 220 | } |
| 221 | ` |
| 222 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 223 | finalGot := map[string]int{} |
| 224 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 225 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 226 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 227 | dep1Tag := struct { |
| 228 | blueprint.BaseDependencyTag |
| 229 | }{} |
| 230 | dep2Tag := struct { |
| 231 | blueprint.BaseDependencyTag |
| 232 | }{} |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 233 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 234 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 235 | ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) { |
| 236 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 237 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") |
| 238 | } |
| 239 | }) |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 240 | ctx.Transition("variant", &testTransitionMutator{ |
| 241 | split: func(ctx BaseModuleContext) []string { |
| 242 | return []string{"a", "b"} |
| 243 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 244 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 245 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 246 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 247 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
| 248 | ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) { |
| 249 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 250 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2") |
| 251 | } |
| 252 | }) |
| 253 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 254 | finalGot[ctx.Module().String()] += 1 |
| 255 | ctx.VisitDirectDeps(func(mod Module) { |
| 256 | finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1 |
| 257 | }) |
| 258 | }) |
| 259 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 260 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 261 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 262 | }), |
| 263 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 264 | ).RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 265 | |
| 266 | finalWant := map[string]int{ |
| 267 | "common_dep_1{variant:a}": 1, |
| 268 | "common_dep_1{variant:b}": 1, |
| 269 | "common_dep_2{variant:a}": 1, |
| 270 | "common_dep_2{variant:b}": 1, |
| 271 | "foo{variant:a}": 1, |
| 272 | "foo{variant:a} -> common_dep_1{variant:a}": 1, |
| 273 | "foo{variant:a} -> common_dep_2{variant:a}": 1, |
| 274 | "foo{variant:b}": 1, |
| 275 | "foo{variant:b} -> common_dep_1{variant:b}": 1, |
| 276 | "foo{variant:b} -> common_dep_2{variant:a}": 1, |
| 277 | } |
| 278 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 279 | AssertDeepEquals(t, "final", finalWant, finalGot) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Colin Cross | 1e954b6 | 2024-09-13 13:50:00 -0700 | [diff] [blame] | 282 | func TestTransitionMutatorInFinalDeps(t *testing.T) { |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 283 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 284 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 285 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 286 | ctx.Transition("vars", &testTransitionMutator{ |
| 287 | split: func(ctx BaseModuleContext) []string { |
| 288 | return []string{"a", "b"} |
| 289 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 290 | }) |
| 291 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 292 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 293 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 294 | }), |
| 295 | FixtureWithRootAndroidBp(`test {name: "foo"}`), |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame] | 296 | ). |
| 297 | ExtendWithErrorHandler(FixtureExpectsOneErrorPattern("not allowed in FinalDepsMutators")). |
| 298 | RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 299 | } |