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 | |
| 84 | func TestModuleString(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 85 | bp := ` |
| 86 | test { |
| 87 | name: "foo", |
| 88 | } |
| 89 | ` |
| 90 | |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 91 | var moduleStrings []string |
| 92 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 93 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 94 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 95 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 96 | ctx.PreArchMutators(func(ctx RegisterMutatorsContext) { |
| 97 | ctx.BottomUp("pre_arch", func(ctx BottomUpMutatorContext) { |
| 98 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 99 | ctx.CreateVariations("a", "b") |
| 100 | }) |
| 101 | ctx.TopDown("rename_top_down", func(ctx TopDownMutatorContext) { |
| 102 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 103 | ctx.Rename(ctx.Module().base().Name() + "_renamed1") |
| 104 | }) |
| 105 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 106 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 107 | ctx.PreDepsMutators(func(ctx RegisterMutatorsContext) { |
| 108 | ctx.BottomUp("pre_deps", func(ctx BottomUpMutatorContext) { |
| 109 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 110 | ctx.CreateVariations("c", "d") |
| 111 | }) |
| 112 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 113 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 114 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 115 | ctx.BottomUp("post_deps", func(ctx BottomUpMutatorContext) { |
| 116 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 117 | ctx.CreateLocalVariations("e", "f") |
| 118 | }) |
| 119 | ctx.BottomUp("rename_bottom_up", func(ctx BottomUpMutatorContext) { |
| 120 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 121 | ctx.Rename(ctx.Module().base().Name() + "_renamed2") |
| 122 | }) |
| 123 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 124 | moduleStrings = append(moduleStrings, ctx.Module().String()) |
| 125 | }) |
| 126 | }) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 127 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 128 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 129 | }), |
| 130 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 131 | ).RunTest(t) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 132 | |
| 133 | want := []string{ |
| 134 | // Initial name. |
| 135 | "foo{}", |
| 136 | |
| 137 | // After pre_arch (reversed because rename_top_down is TopDown so it visits in reverse order). |
| 138 | "foo{pre_arch:b}", |
| 139 | "foo{pre_arch:a}", |
| 140 | |
| 141 | // After rename_top_down. |
| 142 | "foo_renamed1{pre_arch:a}", |
| 143 | "foo_renamed1{pre_arch:b}", |
| 144 | |
| 145 | // After pre_deps. |
| 146 | "foo_renamed1{pre_arch:a,pre_deps:c}", |
| 147 | "foo_renamed1{pre_arch:a,pre_deps:d}", |
| 148 | "foo_renamed1{pre_arch:b,pre_deps:c}", |
| 149 | "foo_renamed1{pre_arch:b,pre_deps:d}", |
| 150 | |
| 151 | // After post_deps. |
| 152 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:e}", |
| 153 | "foo_renamed1{pre_arch:a,pre_deps:c,post_deps:f}", |
| 154 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:e}", |
| 155 | "foo_renamed1{pre_arch:a,pre_deps:d,post_deps:f}", |
| 156 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:e}", |
| 157 | "foo_renamed1{pre_arch:b,pre_deps:c,post_deps:f}", |
| 158 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:e}", |
| 159 | "foo_renamed1{pre_arch:b,pre_deps:d,post_deps:f}", |
| 160 | |
| 161 | // After rename_bottom_up. |
| 162 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:e}", |
| 163 | "foo_renamed2{pre_arch:a,pre_deps:c,post_deps:f}", |
| 164 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:e}", |
| 165 | "foo_renamed2{pre_arch:a,pre_deps:d,post_deps:f}", |
| 166 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:e}", |
| 167 | "foo_renamed2{pre_arch:b,pre_deps:c,post_deps:f}", |
| 168 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:e}", |
| 169 | "foo_renamed2{pre_arch:b,pre_deps:d,post_deps:f}", |
| 170 | } |
| 171 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 172 | AssertDeepEquals(t, "module String() values", want, moduleStrings) |
Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 173 | } |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 174 | |
| 175 | func TestFinalDepsPhase(t *testing.T) { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 176 | bp := ` |
| 177 | test { |
| 178 | name: "common_dep_1", |
| 179 | } |
| 180 | test { |
| 181 | name: "common_dep_2", |
| 182 | } |
| 183 | test { |
| 184 | name: "foo", |
| 185 | } |
| 186 | ` |
| 187 | |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 188 | finalGot := map[string]int{} |
| 189 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 190 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 191 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 192 | dep1Tag := struct { |
| 193 | blueprint.BaseDependencyTag |
| 194 | }{} |
| 195 | dep2Tag := struct { |
| 196 | blueprint.BaseDependencyTag |
| 197 | }{} |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 198 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 199 | ctx.PostDepsMutators(func(ctx RegisterMutatorsContext) { |
| 200 | ctx.BottomUp("far_deps_1", func(ctx BottomUpMutatorContext) { |
| 201 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 202 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep1Tag, "common_dep_1") |
| 203 | } |
| 204 | }) |
| 205 | ctx.BottomUp("variant", func(ctx BottomUpMutatorContext) { |
| 206 | ctx.CreateLocalVariations("a", "b") |
| 207 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 208 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 209 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 210 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
| 211 | ctx.BottomUp("far_deps_2", func(ctx BottomUpMutatorContext) { |
| 212 | if !strings.HasPrefix(ctx.ModuleName(), "common_dep") { |
| 213 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, dep2Tag, "common_dep_2") |
| 214 | } |
| 215 | }) |
| 216 | ctx.BottomUp("final", func(ctx BottomUpMutatorContext) { |
| 217 | finalGot[ctx.Module().String()] += 1 |
| 218 | ctx.VisitDirectDeps(func(mod Module) { |
| 219 | finalGot[fmt.Sprintf("%s -> %s", ctx.Module().String(), mod)] += 1 |
| 220 | }) |
| 221 | }) |
| 222 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 223 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 224 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 225 | }), |
| 226 | FixtureWithRootAndroidBp(bp), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 227 | ).RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 228 | |
| 229 | finalWant := map[string]int{ |
| 230 | "common_dep_1{variant:a}": 1, |
| 231 | "common_dep_1{variant:b}": 1, |
| 232 | "common_dep_2{variant:a}": 1, |
| 233 | "common_dep_2{variant:b}": 1, |
| 234 | "foo{variant:a}": 1, |
| 235 | "foo{variant:a} -> common_dep_1{variant:a}": 1, |
| 236 | "foo{variant:a} -> common_dep_2{variant:a}": 1, |
| 237 | "foo{variant:b}": 1, |
| 238 | "foo{variant:b} -> common_dep_1{variant:b}": 1, |
| 239 | "foo{variant:b} -> common_dep_2{variant:a}": 1, |
| 240 | } |
| 241 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 242 | AssertDeepEquals(t, "final", finalWant, finalGot) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | func TestNoCreateVariationsInFinalDeps(t *testing.T) { |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 246 | checkErr := func() { |
| 247 | if err := recover(); err == nil || !strings.Contains(fmt.Sprintf("%s", err), "not allowed in FinalDepsMutators") { |
| 248 | panic("Expected FinalDepsMutators consistency check to fail") |
| 249 | } |
| 250 | } |
| 251 | |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 252 | GroupFixturePreparers( |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 253 | FixtureRegisterWithContext(func(ctx RegistrationContext) { |
| 254 | ctx.FinalDepsMutators(func(ctx RegisterMutatorsContext) { |
| 255 | ctx.BottomUp("vars", func(ctx BottomUpMutatorContext) { |
| 256 | defer checkErr() |
| 257 | ctx.CreateVariations("a", "b") |
| 258 | }) |
| 259 | ctx.BottomUp("local_vars", func(ctx BottomUpMutatorContext) { |
| 260 | defer checkErr() |
| 261 | ctx.CreateLocalVariations("a", "b") |
| 262 | }) |
| 263 | }) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 264 | |
Paul Duffin | e8a4ac4 | 2021-03-16 22:35:28 +0000 | [diff] [blame] | 265 | ctx.RegisterModuleType("test", mutatorTestModuleFactory) |
| 266 | }), |
| 267 | FixtureWithRootAndroidBp(`test {name: "foo"}`), |
Paul Duffin | 30ac3e7 | 2021-03-20 00:36:14 +0000 | [diff] [blame] | 268 | ).RunTest(t) |
Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 269 | } |