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 | }) |
| 137 | ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) { |
| 138 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 139 | ctx.Rename(ctx.Module().base().Name() + "_renamed1") |
| 140 | }) |
| 141 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 142 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 143 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 144 | ctx.Transition("pre_deps", &testTransitionMutator{ |
| 145 | split: func(ctx BaseModuleContext) []string { |
| 146 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 147 | return []string{"c", "d"} |
| 148 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 149 | }) |
| 150 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 151 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 152 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 153 | ctx.Transition("post_deps", &testTransitionMutator{ |
| 154 | split: func(ctx BaseModuleContext) []string { |
| 155 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 156 | return []string{"e", "f"} |
| 157 | }, |
| 158 | outgoingTransition: func(ctx OutgoingTransitionContext, sourceVariation string) string { |
| 159 | return "" |
| 160 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 161 | }) |
| 162 | ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) { |
| 163 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 164 | ctx.Rename(ctx.Module().base().Name() + "_renamed2") |
| 165 | }) |
| 166 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 167 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 168 | }) |
| 169 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 170 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 171 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 172 | }), |
| 173 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 174 | ).RunTest(t) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 175 | |
| 176 | want := []string{ |
| 177 | // Initial name. |
| 178 | "foo{}", |
| 179 | |
| 180 | // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order). |
| 181 | "foo{pre_arch:b}", |
| 182 | "foo{pre_arch:a}", |
| 183 | |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 184 | // After rename_top_down (reversed because pre_deps TransitionMutator.Split is TopDown). |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 185 | "foo_renamed1{pre_arch:b}", |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 186 | "foo_renamed1{pre_arch:a}", |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 187 | |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 188 | // After pre_deps (reversed because post_deps TransitionMutator.Split is TopDown). |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 189 | "foo_renamed1{pre_arch:b,pre_deps:d}", |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 190 | "foo_renamed1{pre_arch:b,pre_deps:c}", |
| 191 | "foo_renamed1{pre_arch:a,pre_deps:d}", |
| 192 | "foo_renamed1{pre_arch:a,pre_deps:c}", |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 193 | |
| 194 | // After post_deps. |
| 195 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}", |
| 196 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}", |
| 197 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}", |
| 198 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}", |
| 199 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}", |
| 200 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}", |
| 201 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}", |
| 202 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}", |
| 203 | |
| 204 | // After rename_bottom_up. |
| 205 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}", |
| 206 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}", |
| 207 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}", |
| 208 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}", |
| 209 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}", |
| 210 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}", |
| 211 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}", |
| 212 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}", |
| 213 | } |
| 214 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 215 | AssertDeepEquals(t, "module String() values", want, moduleStrings) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 216 | } |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 217 | |
| 218 | func TestFinalDepsPhase(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 219 | bp := ` |
| 220 | test { |
| 221 | name: "common_dep_1", |
| 222 | } |
| 223 | test { |
| 224 | name: "common_dep_2", |
| 225 | } |
| 226 | test { |
| 227 | name: "foo", |
| 228 | } |
| 229 | ` |
| 230 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 231 | finalGot := map[string]int{} |
| 232 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 233 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 234 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 235 | dep1Tag := struct { |
| 236 | blueprint.BaseDependencyTag |
| 237 | }{} |
| 238 | dep2Tag := struct { |
| 239 | blueprint.BaseDependencyTag |
| 240 | }{} |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 241 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 242 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 243 | ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) { |
| 244 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 245 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") |
| 246 | } |
| 247 | }) |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 248 | ctx.Transition("variant", &testTransitionMutator{ |
| 249 | split: func(ctx BaseModuleContext) []string { |
| 250 | return []string{"a", "b"} |
| 251 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 252 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 253 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 254 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 255 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
| 256 | ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) { |
| 257 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 258 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2") |
| 259 | } |
| 260 | }) |
| 261 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 262 | finalGot[ctx.Module().String()] += 1 |
| 263 | ctx.VisitDirectDeps(func(mod Module) { |
| 264 | finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1 |
| 265 | }) |
| 266 | }) |
| 267 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 268 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 269 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 270 | }), |
| 271 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 272 | ).RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 273 | |
| 274 | finalWant := map[string]int{ |
| 275 | "common_dep_1{variant:a}": 1, |
| 276 | "common_dep_1{variant:b}": 1, |
| 277 | "common_dep_2{variant:a}": 1, |
| 278 | "common_dep_2{variant:b}": 1, |
| 279 | "foo{variant:a}": 1, |
| 280 | "foo{variant:a} -> common_dep_1{variant:a}": 1, |
| 281 | "foo{variant:a} -> common_dep_2{variant:a}": 1, |
| 282 | "foo{variant:b}": 1, |
| 283 | "foo{variant:b} -> common_dep_1{variant:b}": 1, |
| 284 | "foo{variant:b} -> common_dep_2{variant:a}": 1, |
| 285 | } |
| 286 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 287 | AssertDeepEquals(t, "final", finalWant, finalGot) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | func TestNoCreateVariationsInFinalDeps(t *testing.T) { |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 291 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 292 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 293 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 294 | ctx.Transition("vars", &testTransitionMutator{ |
| 295 | split: func(ctx BaseModuleContext) []string { |
| 296 | return []string{"a", "b"} |
| 297 | }, |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 298 | }) |
| 299 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 300 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 301 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 302 | }), |
| 303 | FixtureWithRootAndroidBp(`test {name: "foo"}`), |
Colin Cross | d27205e | 2024-09-12 22:41:37 -0700 | [diff] [blame^] | 304 | ). |
| 305 | ExtendWithErrorHandler(FixtureExpectsOneErrorPattern("not allowed in FinalDepsMutators")). |
| 306 | RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 307 | } |