| Colin Cross | 95b3627 | 2024-04-12 14:48:34 -0700 | [diff] [blame^] | 1 | // Copyright 2024 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 blueprint |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "slices" |
| 20 | "sort" |
| 21 | ) |
| 22 | |
| 23 | // TransitionMutator implements a top-down mechanism where a module tells its |
| 24 | // direct dependencies what variation they should be built in but the dependency |
| 25 | // has the final say. |
| 26 | // |
| 27 | // When implementing a transition mutator, one needs to implement four methods: |
| 28 | // - Split() that tells what variations a module has by itself |
| 29 | // - OutgoingTransition() where a module tells what it wants from its |
| 30 | // dependency |
| 31 | // - IncomingTransition() where a module has the final say about its own |
| 32 | // variation |
| 33 | // - Mutate() that changes the state of a module depending on its variation |
| 34 | // |
| 35 | // That the effective variation of module B when depended on by module A is the |
| 36 | // composition the outgoing transition of module A and the incoming transition |
| 37 | // of module B. |
| 38 | // |
| 39 | // The outgoing transition should not take the properties of the dependency into |
| 40 | // account, only those of the module that depends on it. For this reason, the |
| 41 | // dependency is not even passed into it as an argument. Likewise, the incoming |
| 42 | // transition should not take the properties of the depending module into |
| 43 | // account and is thus not informed about it. This makes for a nice |
| 44 | // decomposition of the decision logic. |
| 45 | // |
| 46 | // A given transition mutator only affects its own variation; other variations |
| 47 | // stay unchanged along the dependency edges. |
| 48 | // |
| 49 | // Soong makes sure that all modules are created in the desired variations and |
| 50 | // that dependency edges are set up correctly. This ensures that "missing |
| 51 | // variation" errors do not happen and allows for more flexible changes in the |
| 52 | // value of the variation among dependency edges (as opposed to bottom-up |
| 53 | // mutators where if module A in variation X depends on module B and module B |
| 54 | // has that variation X, A must depend on variation X of B) |
| 55 | // |
| 56 | // The limited power of the context objects passed to individual mutators |
| 57 | // methods also makes it more difficult to shoot oneself in the foot. Complete |
| 58 | // safety is not guaranteed because no one prevents individual transition |
| 59 | // mutators from mutating modules in illegal ways and for e.g. Split() or |
| 60 | // Mutate() to run their own visitations of the transitive dependency of the |
| 61 | // module and both of these are bad ideas, but it's better than no guardrails at |
| 62 | // all. |
| 63 | // |
| 64 | // This model is pretty close to Bazel's configuration transitions. The mapping |
| 65 | // between concepts in Soong and Bazel is as follows: |
| 66 | // - Module == configured target |
| 67 | // - Variant == configuration |
| 68 | // - Variation name == configuration flag |
| 69 | // - Variation == configuration flag value |
| 70 | // - Outgoing transition == attribute transition |
| 71 | // - Incoming transition == rule transition |
| 72 | // |
| 73 | // The Split() method does not have a Bazel equivalent and Bazel split |
| 74 | // transitions do not have a Soong equivalent. |
| 75 | // |
| 76 | // Mutate() does not make sense in Bazel due to the different models of the |
| 77 | // two systems: when creating new variations, Soong clones the old module and |
| 78 | // thus some way is needed to change it state whereas Bazel creates each |
| 79 | // configuration of a given configured target anew. |
| 80 | type TransitionMutator interface { |
| 81 | // Split returns the set of variations that should be created for a module no matter |
| 82 | // who depends on it. Used when Make depends on a particular variation or when |
| 83 | // the module knows its variations just based on information given to it in |
| 84 | // the Blueprint file. This method should not mutate the module it is called |
| 85 | // on. |
| 86 | Split(ctx BaseModuleContext) []string |
| 87 | |
| 88 | // OutgoingTransition is called on a module to determine which variation it wants |
| 89 | // from its direct dependencies. The dependency itself can override this decision. |
| 90 | // This method should not mutate the module itself. |
| 91 | OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string |
| 92 | |
| 93 | // IncomingTransition is called on a module to determine which variation it should |
| 94 | // be in based on the variation modules that depend on it want. This gives the module |
| 95 | // a final say about its own variations. This method should not mutate the module |
| 96 | // itself. |
| 97 | IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string |
| 98 | |
| 99 | // Mutate is called after a module was split into multiple variations on each |
| 100 | // variation. It should not split the module any further but adding new dependencies |
| 101 | // is fine. Unlike all the other methods on TransitionMutator, this method is |
| 102 | // allowed to mutate the module. |
| 103 | Mutate(ctx BottomUpMutatorContext, variation string) |
| 104 | } |
| 105 | |
| 106 | type IncomingTransitionContext interface { |
| 107 | // Module returns the target of the dependency edge for which the transition |
| 108 | // is being computed |
| 109 | Module() Module |
| 110 | |
| 111 | // Config returns the config object that was passed to |
| 112 | // Context.PrepareBuildActions. |
| 113 | Config() interface{} |
| 114 | |
| 115 | // Provider returns the value for a provider for the target of the dependency edge for which the |
| 116 | // transition is being computed. If the value is not set it returns nil and false. It panics if |
| 117 | // called before the appropriate mutator or GenerateBuildActions pass for the provider. The value |
| 118 | // returned may be a deep copy of the value originally passed to SetProvider. |
| 119 | // |
| 120 | // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead. |
| 121 | Provider(provider AnyProviderKey) (any, bool) |
| 122 | } |
| 123 | |
| 124 | type OutgoingTransitionContext interface { |
| 125 | // Module returns the source of the dependency edge for which the transition |
| 126 | // is being computed |
| 127 | Module() Module |
| 128 | |
| 129 | // DepTag() Returns the dependency tag through which this dependency is |
| 130 | // reached |
| 131 | DepTag() DependencyTag |
| 132 | |
| 133 | // Config returns the config object that was passed to |
| 134 | // Context.PrepareBuildActions. |
| 135 | Config() interface{} |
| 136 | |
| 137 | // Provider returns the value for a provider for the source of the dependency edge for which the |
| 138 | // transition is being computed. If the value is not set it returns nil and false. It panics if |
| 139 | // called before the appropriate mutator or GenerateBuildActions pass for the provider. The value |
| 140 | // returned may be a deep copy of the value originally passed to SetProvider. |
| 141 | // |
| 142 | // This method shouldn't be used directly, prefer the type-safe android.ModuleProvider instead. |
| 143 | Provider(provider AnyProviderKey) (any, bool) |
| 144 | } |
| 145 | |
| 146 | type transitionMutatorImpl struct { |
| 147 | name string |
| 148 | mutator TransitionMutator |
| 149 | } |
| 150 | |
| 151 | // Adds each argument in items to l if it's not already there. |
| 152 | func addToStringListIfNotPresent(l []string, items ...string) []string { |
| 153 | for _, i := range items { |
| 154 | if !slices.Contains(l, i) { |
| 155 | l = append(l, i) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return l |
| 160 | } |
| 161 | |
| 162 | func (t *transitionMutatorImpl) addRequiredVariation(m *moduleInfo, variation string) { |
| 163 | m.requiredVariationsLock.Lock() |
| 164 | defer m.requiredVariationsLock.Unlock() |
| 165 | |
| 166 | // This is only a consistency check. Leaking the variations of a transition |
| 167 | // mutator to another one could well lead to issues that are difficult to |
| 168 | // track down. |
| 169 | if m.currentTransitionMutator != "" && m.currentTransitionMutator != t.name { |
| 170 | panic(fmt.Errorf("transition mutator is %s in mutator %s", m.currentTransitionMutator, t.name)) |
| 171 | } |
| 172 | |
| 173 | m.currentTransitionMutator = t.name |
| 174 | m.transitionVariations = addToStringListIfNotPresent(m.transitionVariations, variation) |
| 175 | } |
| 176 | |
| 177 | func (t *transitionMutatorImpl) topDownMutator(mctx TopDownMutatorContext) { |
| 178 | module := mctx.(*mutatorContext).module |
| 179 | mutatorSplits := t.mutator.Split(mctx) |
| 180 | if mutatorSplits == nil || len(mutatorSplits) == 0 { |
| 181 | panic(fmt.Errorf("transition mutator %s returned no splits for module %s", t.name, mctx.ModuleName())) |
| 182 | } |
| 183 | |
| 184 | // transitionVariations for given a module can be mutated by the module itself |
| 185 | // and modules that directly depend on it. Since this is a top-down mutator, |
| 186 | // all modules that directly depend on this module have already been processed |
| 187 | // so no locking is necessary. |
| 188 | module.transitionVariations = addToStringListIfNotPresent(module.transitionVariations, mutatorSplits...) |
| 189 | sort.Strings(module.transitionVariations) |
| 190 | |
| 191 | outgoingTransitionCache := make([][]string, len(module.transitionVariations)) |
| 192 | for srcVariationIndex, srcVariation := range module.transitionVariations { |
| 193 | srcVariationTransitionCache := make([]string, len(module.directDeps)) |
| 194 | for depIndex, dep := range module.directDeps { |
| 195 | finalVariation := t.transition(mctx)(mctx.moduleInfo(), srcVariation, dep.module, dep.tag) |
| 196 | srcVariationTransitionCache[depIndex] = finalVariation |
| 197 | t.addRequiredVariation(dep.module, finalVariation) |
| 198 | } |
| 199 | outgoingTransitionCache[srcVariationIndex] = srcVariationTransitionCache |
| 200 | } |
| 201 | module.outgoingTransitionCache = outgoingTransitionCache |
| 202 | } |
| 203 | |
| 204 | type transitionContextImpl struct { |
| 205 | context *Context |
| 206 | source *moduleInfo |
| 207 | dep *moduleInfo |
| 208 | depTag DependencyTag |
| 209 | config interface{} |
| 210 | } |
| 211 | |
| 212 | func (c *transitionContextImpl) DepTag() DependencyTag { |
| 213 | return c.depTag |
| 214 | } |
| 215 | |
| 216 | func (c *transitionContextImpl) Config() interface{} { |
| 217 | return c.config |
| 218 | } |
| 219 | |
| 220 | type outgoingTransitionContextImpl struct { |
| 221 | transitionContextImpl |
| 222 | } |
| 223 | |
| 224 | func (c *outgoingTransitionContextImpl) Module() Module { |
| 225 | return c.source.logicModule |
| 226 | } |
| 227 | |
| 228 | func (c *outgoingTransitionContextImpl) Provider(provider AnyProviderKey) (any, bool) { |
| 229 | return c.context.provider(c.source, provider.provider()) |
| 230 | } |
| 231 | |
| 232 | type incomingTransitionContextImpl struct { |
| 233 | transitionContextImpl |
| 234 | } |
| 235 | |
| 236 | func (c *incomingTransitionContextImpl) Module() Module { |
| 237 | return c.dep.logicModule |
| 238 | } |
| 239 | |
| 240 | func (c *incomingTransitionContextImpl) Provider(provider AnyProviderKey) (any, bool) { |
| 241 | return c.context.provider(c.dep, provider.provider()) |
| 242 | } |
| 243 | |
| 244 | func (t *transitionMutatorImpl) transition(mctx BaseModuleContext) Transition { |
| 245 | return func(source *moduleInfo, sourceVariation string, dep *moduleInfo, depTag DependencyTag) string { |
| 246 | tc := transitionContextImpl{ |
| 247 | context: mctx.base().context, |
| 248 | source: source, |
| 249 | dep: dep, |
| 250 | depTag: depTag, |
| 251 | config: mctx.Config(), |
| 252 | } |
| 253 | outgoingVariation := t.mutator.OutgoingTransition(&outgoingTransitionContextImpl{tc}, sourceVariation) |
| 254 | if mctx.Failed() { |
| 255 | return outgoingVariation |
| 256 | } |
| 257 | finalVariation := t.mutator.IncomingTransition(&incomingTransitionContextImpl{tc}, outgoingVariation) |
| 258 | return finalVariation |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | func (t *transitionMutatorImpl) bottomUpMutator(mctx BottomUpMutatorContext) { |
| 263 | mc := mctx.(*mutatorContext) |
| 264 | // Fetch and clean up transition mutator state. No locking needed since the |
| 265 | // only time interaction between multiple modules is required is during the |
| 266 | // computation of the variations required by a given module. |
| 267 | variations := mc.module.transitionVariations |
| 268 | outgoingTransitionCache := mc.module.outgoingTransitionCache |
| 269 | mc.module.transitionVariations = nil |
| 270 | mc.module.outgoingTransitionCache = nil |
| 271 | mc.module.currentTransitionMutator = "" |
| 272 | |
| 273 | if len(variations) < 1 { |
| 274 | panic(fmt.Errorf("no variations found for module %s by mutator %s", |
| 275 | mctx.ModuleName(), t.name)) |
| 276 | } |
| 277 | |
| 278 | if len(variations) == 1 && variations[0] == "" { |
| 279 | // Module is not split, just apply the transition |
| 280 | mc.context.convertDepsToVariation(mc.module, 0, |
| 281 | chooseDepByIndexes(mc.mutator.name, outgoingTransitionCache)) |
| 282 | } else { |
| 283 | mc.createVariationsWithTransition(variations, outgoingTransitionCache) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | func (t *transitionMutatorImpl) mutateMutator(mctx BottomUpMutatorContext) { |
| 288 | module := mctx.(*mutatorContext).module |
| 289 | currentVariation := module.variant.variations[t.name] |
| 290 | t.mutator.Mutate(mctx, currentVariation) |
| 291 | } |
| 292 | |
| 293 | func (c *Context) RegisterTransitionMutator(name string, mutator TransitionMutator) { |
| 294 | impl := &transitionMutatorImpl{name: name, mutator: mutator} |
| 295 | |
| 296 | c.RegisterTopDownMutator(name+"_deps", impl.topDownMutator).Parallel() |
| 297 | c.RegisterBottomUpMutator(name, impl.bottomUpMutator).Parallel() |
| 298 | c.RegisterBottomUpMutator(name+"_mutate", impl.mutateMutator).Parallel() |
| 299 | } |
| 300 | |
| 301 | // This function is called for every dependency edge to determine which |
| 302 | // variation of the dependency is needed. Its inputs are the depending module, |
| 303 | // its variation, the dependency and the dependency tag. |
| 304 | type Transition func(source *moduleInfo, sourceVariation string, dep *moduleInfo, depTag DependencyTag) string |