| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -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 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 15 | package android | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 16 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 17 | import ( | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 18 | "path/filepath" | 
|  | 19 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 20 | "android/soong/bazel" | 
| Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 21 | "android/soong/ui/metrics/bp2build_metrics_proto" | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 22 |  | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint" | 
|  | 24 | ) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 25 |  | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 26 | // Phases: | 
|  | 27 | //   run Pre-arch mutators | 
|  | 28 | //   run archMutator | 
|  | 29 | //   run Pre-deps mutators | 
|  | 30 | //   run depsMutator | 
|  | 31 | //   run PostDeps mutators | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 32 | //   run FinalDeps mutators (CreateVariations disallowed in this phase) | 
| Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 33 | //   continue on to GenerateAndroidBuildActions | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 34 |  | 
| Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 35 | // RegisterMutatorsForBazelConversion is a alternate registration pipeline for bp2build. Exported for testing. | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 36 | func RegisterMutatorsForBazelConversion(ctx *Context, preArchMutators []RegisterMutatorFunc) { | 
| Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 37 | bp2buildMutators := append(preArchMutators, registerBp2buildConversionMutator) | 
|  | 38 | registerMutatorsForBazelConversion(ctx, bp2buildMutators) | 
|  | 39 | } | 
|  | 40 |  | 
| Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 41 | func registerMutatorsForBazelConversion(ctx *Context, bp2buildMutators []RegisterMutatorFunc) { | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 42 | mctx := ®isterMutatorsContext{ | 
|  | 43 | bazelConversionMode: true, | 
| Jingwen Chen | 041b184 | 2021-02-01 00:23:25 -0500 | [diff] [blame] | 44 | } | 
|  | 45 |  | 
| Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 46 | allMutators := append([]RegisterMutatorFunc{ | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 47 | RegisterNamespaceMutator, | 
|  | 48 | RegisterDefaultsPreArchMutators, | 
|  | 49 | // TODO(b/165114590): this is required to resolve deps that are only prebuilts, but we should | 
|  | 50 | // evaluate the impact on conversion. | 
|  | 51 | RegisterPrebuiltsPreArchMutators, | 
| Liz Kammer | e1b39a5 | 2023-09-18 14:32:18 -0400 | [diff] [blame] | 52 | RegisterPrebuiltsPostDepsMutators, | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 53 | }, | 
| Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 54 | bp2buildMutators...) | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 55 |  | 
| Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 56 | // Register bp2build mutators | 
| Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 57 | for _, f := range allMutators { | 
| Jingwen Chen | 7385067 | 2020-12-14 08:25:34 -0500 | [diff] [blame] | 58 | f(mctx) | 
|  | 59 | } | 
|  | 60 |  | 
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 61 | mctx.mutators.registerAll(ctx) | 
| Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 64 | // collateGloballyRegisteredMutators constructs the list of mutators that have been registered | 
|  | 65 | // with the InitRegistrationContext and will be used at runtime. | 
|  | 66 | func collateGloballyRegisteredMutators() sortableComponents { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 67 | // ensure mixed builds mutator is the last mutator | 
|  | 68 | finalDeps = append(finalDeps, registerMixedBuildsMutator) | 
| Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 69 | return collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps) | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | // collateRegisteredMutators constructs a single list of mutators from the separate lists. | 
|  | 73 | func collateRegisteredMutators(preArch, preDeps, postDeps, finalDeps []RegisterMutatorFunc) sortableComponents { | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 74 | mctx := ®isterMutatorsContext{} | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 75 |  | 
|  | 76 | register := func(funcs []RegisterMutatorFunc) { | 
|  | 77 | for _, f := range funcs { | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 78 | f(mctx) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 82 | register(preArch) | 
| Nan Zhang | db0b9a3 | 2017-02-27 10:12:13 -0800 | [diff] [blame] | 83 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 84 | register(preDeps) | 
|  | 85 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 86 | register([]RegisterMutatorFunc{registerDepsMutator}) | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | register(postDeps) | 
|  | 89 |  | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 90 | mctx.finalPhase = true | 
|  | 91 | register(finalDeps) | 
|  | 92 |  | 
| Paul Duffin | c05b034 | 2021-03-06 13:28:13 +0000 | [diff] [blame] | 93 | return mctx.mutators | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 94 | } | 
|  | 95 |  | 
|  | 96 | type registerMutatorsContext struct { | 
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 97 | mutators            sortableComponents | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 98 | finalPhase          bool | 
|  | 99 | bazelConversionMode bool | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 100 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 101 |  | 
|  | 102 | type RegisterMutatorsContext interface { | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 103 | TopDown(name string, m TopDownMutator) MutatorHandle | 
|  | 104 | BottomUp(name string, m BottomUpMutator) MutatorHandle | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 105 | BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle | 
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 106 | Transition(name string, m TransitionMutator) | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
|  | 109 | type RegisterMutatorFunc func(RegisterMutatorsContext) | 
|  | 110 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 111 | var preArch = []RegisterMutatorFunc{ | 
| Dan Willemsen | 6e72ef7 | 2018-01-26 18:27:02 -0800 | [diff] [blame] | 112 | RegisterNamespaceMutator, | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 113 |  | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 114 | // Check the visibility rules are valid. | 
|  | 115 | // | 
|  | 116 | // This must run after the package renamer mutators so that any issues found during | 
|  | 117 | // validation of the package's default_visibility property are reported using the | 
|  | 118 | // correct package name and not the synthetic name. | 
|  | 119 | // | 
|  | 120 | // This must also be run before defaults mutators as the rules for validation are | 
|  | 121 | // different before checking the rules than they are afterwards. e.g. | 
|  | 122 | //    visibility: ["//visibility:private", "//visibility:public"] | 
|  | 123 | // would be invalid if specified in a module definition but is valid if it results | 
|  | 124 | // from something like this: | 
|  | 125 | // | 
|  | 126 | //    defaults { | 
|  | 127 | //        name: "defaults", | 
|  | 128 | //        // Be inaccessible outside a package by default. | 
|  | 129 | //        visibility: ["//visibility:private"] | 
|  | 130 | //    } | 
|  | 131 | // | 
|  | 132 | //    defaultable_module { | 
|  | 133 | //        name: "defaultable_module", | 
|  | 134 | //        defaults: ["defaults"], | 
|  | 135 | //        // Override the default. | 
|  | 136 | //        visibility: ["//visibility:public"] | 
|  | 137 | //    } | 
|  | 138 | // | 
| Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 139 | RegisterVisibilityRuleChecker, | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 140 |  | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 141 | // Record the default_applicable_licenses for each package. | 
|  | 142 | // | 
|  | 143 | // This must run before the defaults so that defaults modules can pick up the package default. | 
|  | 144 | RegisterLicensesPackageMapper, | 
|  | 145 |  | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 146 | // Apply properties from defaults modules to the referencing modules. | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 147 | // | 
|  | 148 | // Any mutators that are added before this will not see any modules created by | 
|  | 149 | // a DefaultableHook. | 
| Colin Cross | 89536d4 | 2017-07-07 14:35:50 -0700 | [diff] [blame] | 150 | RegisterDefaultsPreArchMutators, | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 151 |  | 
| Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 152 | // Add dependencies on any components so that any component references can be | 
|  | 153 | // resolved within the deps mutator. | 
|  | 154 | // | 
|  | 155 | // Must be run after defaults so it can be used to create dependencies on the | 
|  | 156 | // component modules that are creating in a DefaultableHook. | 
|  | 157 | // | 
|  | 158 | // Must be run before RegisterPrebuiltsPreArchMutators, i.e. before prebuilts are | 
|  | 159 | // renamed. That is so that if a module creates components using a prebuilt module | 
|  | 160 | // type that any dependencies (which must use prebuilt_ prefixes) are resolved to | 
|  | 161 | // the prebuilt module and not the source module. | 
|  | 162 | RegisterComponentsMutator, | 
|  | 163 |  | 
| Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 164 | // Create an association between prebuilt modules and their corresponding source | 
|  | 165 | // modules (if any). | 
| Paul Duffin | afa9fa1 | 2020-04-29 16:47:28 +0100 | [diff] [blame] | 166 | // | 
|  | 167 | // Must be run after defaults mutators to ensure that any modules created by | 
|  | 168 | // a DefaultableHook can be either a prebuilt or a source module with a matching | 
|  | 169 | // prebuilt. | 
| Paul Duffin | c988c8e | 2020-04-29 18:27:14 +0100 | [diff] [blame] | 170 | RegisterPrebuiltsPreArchMutators, | 
|  | 171 |  | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 172 | // Gather the licenses properties for all modules for use during expansion and enforcement. | 
|  | 173 | // | 
|  | 174 | // This must come after the defaults mutators to ensure that any licenses supplied | 
|  | 175 | // in a defaults module has been successfully applied before the rules are gathered. | 
|  | 176 | RegisterLicensesPropertyGatherer, | 
|  | 177 |  | 
| Paul Duffin | aa4162e | 2020-05-05 11:35:43 +0100 | [diff] [blame] | 178 | // Gather the visibility rules for all modules for us during visibility enforcement. | 
|  | 179 | // | 
|  | 180 | // This must come after the defaults mutators to ensure that any visibility supplied | 
|  | 181 | // in a defaults module has been successfully applied before the rules are gathered. | 
| Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 182 | RegisterVisibilityRuleGatherer, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 183 | } | 
|  | 184 |  | 
| Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 185 | func registerArchMutator(ctx RegisterMutatorsContext) { | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 186 | ctx.BottomUpBlueprint("os", osMutator).Parallel() | 
| Colin Cross | fb0c16e | 2019-11-20 17:12:35 -0800 | [diff] [blame] | 187 | ctx.BottomUp("image", imageMutator).Parallel() | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 188 | ctx.BottomUpBlueprint("arch", archMutator).Parallel() | 
| Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 189 | } | 
|  | 190 |  | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 191 | var preDeps = []RegisterMutatorFunc{ | 
| Colin Cross | ae4c618 | 2017-09-15 17:33:55 -0700 | [diff] [blame] | 192 | registerArchMutator, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 193 | } | 
|  | 194 |  | 
|  | 195 | var postDeps = []RegisterMutatorFunc{ | 
| Colin Cross | 1b48842 | 2019-03-04 22:33:56 -0800 | [diff] [blame] | 196 | registerPathDepsMutator, | 
| Colin Cross | 5ea9bcc | 2017-07-27 15:41:32 -0700 | [diff] [blame] | 197 | RegisterPrebuiltsPostDepsMutators, | 
| Paul Duffin | 593b3c9 | 2019-12-05 14:31:48 +0000 | [diff] [blame] | 198 | RegisterVisibilityRuleEnforcer, | 
| Bob Badour | 37af046 | 2021-01-07 03:34:31 +0000 | [diff] [blame] | 199 | RegisterLicensesDependencyChecker, | 
| Paul Duffin | 45338f0 | 2021-03-30 23:07:52 +0100 | [diff] [blame] | 200 | registerNeverallowMutator, | 
| Jaewoong Jung | b639a6a | 2019-05-10 15:16:29 -0700 | [diff] [blame] | 201 | RegisterOverridePostDepsMutators, | 
| Colin Cross | cec8171 | 2017-07-13 14:43:27 -0700 | [diff] [blame] | 202 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 203 |  | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 204 | var finalDeps = []RegisterMutatorFunc{} | 
|  | 205 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 206 | func PreArchMutators(f RegisterMutatorFunc) { | 
|  | 207 | preArch = append(preArch, f) | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | func PreDepsMutators(f RegisterMutatorFunc) { | 
|  | 211 | preDeps = append(preDeps, f) | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | func PostDepsMutators(f RegisterMutatorFunc) { | 
|  | 215 | postDeps = append(postDeps, f) | 
|  | 216 | } | 
|  | 217 |  | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 218 | func FinalDepsMutators(f RegisterMutatorFunc) { | 
|  | 219 | finalDeps = append(finalDeps, f) | 
|  | 220 | } | 
|  | 221 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 222 | var bp2buildPreArchMutators = []RegisterMutatorFunc{} | 
| Rupert Shuttleworth | a9d76dd | 2021-07-02 07:17:16 -0400 | [diff] [blame] | 223 |  | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 224 | // A minimal context for Bp2build conversion | 
|  | 225 | type Bp2buildMutatorContext interface { | 
|  | 226 | BazelConversionPathContext | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 227 | BaseMutatorContext | 
| Colin Cross | 3f68a13 | 2017-10-23 17:10:29 -0700 | [diff] [blame] | 228 |  | 
| Chris Parsons | 5f1b3c7 | 2023-09-28 20:41:03 +0000 | [diff] [blame] | 229 | // AddDependency adds a dependency to the given module.  It returns a slice of modules for each | 
|  | 230 | // dependency (some entries may be nil). | 
|  | 231 | // | 
|  | 232 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the | 
|  | 233 | // new dependencies have had the current mutator called on them.  If the mutator is not | 
|  | 234 | // parallel this method does not affect the ordering of the current mutator pass, but will | 
|  | 235 | // be ordered correctly for all future mutator passes. | 
|  | 236 | AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module | 
|  | 237 |  | 
| Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 238 | // CreateBazelTargetModule creates a BazelTargetModule by calling the | 
|  | 239 | // factory method, just like in CreateModule, but also requires | 
|  | 240 | // BazelTargetModuleProperties containing additional metadata for the | 
|  | 241 | // bp2build codegenerator. | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 242 | CreateBazelTargetModule(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}) | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 243 |  | 
|  | 244 | // CreateBazelTargetModuleWithRestrictions creates a BazelTargetModule by calling the | 
|  | 245 | // factory method, just like in CreateModule, but also requires | 
|  | 246 | // BazelTargetModuleProperties containing additional metadata for the | 
|  | 247 | // bp2build codegenerator. The generated target is restricted to only be buildable for certain | 
|  | 248 | // platforms, as dictated by a given bool attribute: the target will not be buildable in | 
|  | 249 | // any platform for which this bool attribute is false. | 
|  | 250 | CreateBazelTargetModuleWithRestrictions(bazel.BazelTargetModuleProperties, CommonAttributes, interface{}, bazel.BoolAttribute) | 
| Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 251 |  | 
| Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 252 | // MarkBp2buildUnconvertible registers the current module as "unconvertible to bp2build" for the | 
|  | 253 | // given reason. | 
|  | 254 | MarkBp2buildUnconvertible(reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) | 
|  | 255 |  | 
| Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 256 | // CreateBazelTargetAliasInDir creates an alias definition in `dir` directory. | 
|  | 257 | // This function can be used to create alias definitions in a directory that is different | 
|  | 258 | // from the directory of the visited Soong module. | 
|  | 259 | CreateBazelTargetAliasInDir(dir string, name string, actual bazel.Label) | 
| Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 260 |  | 
|  | 261 | // CreateBazelConfigSetting creates a config_setting in <dir>/BUILD.bazel | 
|  | 262 | // build/bazel has several static config_setting(s) that are used in Bazel builds. | 
|  | 263 | // This function can be used to createa additional config_setting(s) based on the build graph | 
|  | 264 | // (e.g. a config_setting specific to an apex variant) | 
|  | 265 | CreateBazelConfigSetting(csa bazel.ConfigSettingAttributes, ca CommonAttributes, dir string) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 266 | } | 
|  | 267 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 268 | // PreArchBp2BuildMutators adds mutators to be register for converting Android Blueprint modules | 
|  | 269 | // into Bazel BUILD targets that should run prior to deps and conversion. | 
|  | 270 | func PreArchBp2BuildMutators(f RegisterMutatorFunc) { | 
|  | 271 | bp2buildPreArchMutators = append(bp2buildPreArchMutators, f) | 
|  | 272 | } | 
|  | 273 |  | 
|  | 274 | type BaseMutatorContext interface { | 
|  | 275 | BaseModuleContext | 
|  | 276 |  | 
|  | 277 | // MutatorName returns the name that this mutator was registered with. | 
|  | 278 | MutatorName() string | 
|  | 279 |  | 
|  | 280 | // Rename all variants of a module.  The new name is not visible to calls to ModuleName, | 
|  | 281 | // AddDependency or OtherModuleName until after this mutator pass is complete. | 
|  | 282 | Rename(name string) | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | type TopDownMutator func(TopDownMutatorContext) | 
|  | 286 |  | 
|  | 287 | type TopDownMutatorContext interface { | 
|  | 288 | BaseMutatorContext | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 289 |  | 
|  | 290 | // CreateModule creates a new module by calling the factory method for the specified moduleType, and applies | 
|  | 291 | // the specified property structs to it as if the properties were set in a blueprint file. | 
|  | 292 | CreateModule(ModuleFactory, ...interface{}) Module | 
|  | 293 | } | 
|  | 294 |  | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 295 | type topDownMutatorContext struct { | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 296 | bp blueprint.TopDownMutatorContext | 
| Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 297 | baseModuleContext | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 300 | type BottomUpMutator func(BottomUpMutatorContext) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 301 |  | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 302 | type BottomUpMutatorContext interface { | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 303 | BaseMutatorContext | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 304 | Bp2buildMutatorContext | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 305 |  | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 306 | // AddReverseDependency adds a dependency from the destination to the given module. | 
|  | 307 | // Does not affect the ordering of the current mutator pass, but will be ordered | 
|  | 308 | // correctly for all future mutator passes.  All reverse dependencies for a destination module are | 
|  | 309 | // collected until the end of the mutator pass, sorted by name, and then appended to the destination | 
|  | 310 | // module's dependency list. | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 311 | AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 312 |  | 
|  | 313 | // CreateVariations splits  a module into multiple variants, one for each name in the variationNames | 
|  | 314 | // parameter.  It returns a list of new modules in the same order as the variationNames | 
|  | 315 | // list. | 
|  | 316 | // | 
|  | 317 | // If any of the dependencies of the module being operated on were already split | 
|  | 318 | // by calling CreateVariations with the same name, the dependency will automatically | 
|  | 319 | // be updated to point the matching variant. | 
|  | 320 | // | 
|  | 321 | // If a module is split, and then a module depending on the first module is not split | 
|  | 322 | // when the Mutator is later called on it, the dependency of the depending module will | 
|  | 323 | // automatically be updated to point to the first variant. | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 324 | CreateVariations(...string) []Module | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 325 |  | 
|  | 326 | // CreateLocationVariations splits a module into multiple variants, one for each name in the variantNames | 
|  | 327 | // parameter.  It returns a list of new modules in the same order as the variantNames | 
|  | 328 | // list. | 
|  | 329 | // | 
|  | 330 | // Local variations do not affect automatic dependency resolution - dependencies added | 
|  | 331 | // to the split module via deps or DynamicDependerModule must exactly match a variant | 
|  | 332 | // that contains all the non-local variations. | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 333 | CreateLocalVariations(...string) []Module | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 334 |  | 
|  | 335 | // SetDependencyVariation sets all dangling dependencies on the current module to point to the variation | 
|  | 336 | // with given name. This function ignores the default variation set by SetDefaultDependencyVariation. | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 337 | SetDependencyVariation(string) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 338 |  | 
|  | 339 | // SetDefaultDependencyVariation sets the default variation when a dangling reference is detected | 
|  | 340 | // during the subsequent calls on Create*Variations* functions. To reset, set it to nil. | 
| Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 341 | SetDefaultDependencyVariation(*string) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 342 |  | 
|  | 343 | // AddVariationDependencies adds deps as dependencies of the current module, but uses the variations | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 344 | // argument to select which variant of the dependency to use.  It returns a slice of modules for | 
|  | 345 | // each dependency (some entries may be nil).  A variant of the dependency must exist that matches | 
| Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 346 | // all the non-local variations of the current module, plus the variations argument. | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 347 | // | 
|  | 348 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the | 
|  | 349 | // new dependencies have had the current mutator called on them.  If the mutator is not | 
|  | 350 | // parallel this method does not affect the ordering of the current mutator pass, but will | 
|  | 351 | // be ordered correctly for all future mutator passes. | 
| Usta Shrestha | c725f47 | 2022-01-11 02:44:21 -0500 | [diff] [blame] | 352 | AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, names ...string) []blueprint.Module | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 353 |  | 
|  | 354 | // AddFarVariationDependencies adds deps as dependencies of the current module, but uses the | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 355 | // variations argument to select which variant of the dependency to use.  It returns a slice of | 
|  | 356 | // modules for each dependency (some entries may be nil).  A variant of the dependency must | 
|  | 357 | // exist that matches the variations argument, but may also have other variations. | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 358 | // For any unspecified variation the first variant will be used. | 
|  | 359 | // | 
|  | 360 | // Unlike AddVariationDependencies, the variations of the current module are ignored - the | 
|  | 361 | // dependency only needs to match the supplied variations. | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 362 | // | 
|  | 363 | // If the mutator is parallel (see MutatorHandle.Parallel), this method will pause until the | 
|  | 364 | // new dependencies have had the current mutator called on them.  If the mutator is not | 
|  | 365 | // parallel this method does not affect the ordering of the current mutator pass, but will | 
|  | 366 | // be ordered correctly for all future mutator passes. | 
|  | 367 | AddFarVariationDependencies([]blueprint.Variation, blueprint.DependencyTag, ...string) []blueprint.Module | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 368 |  | 
|  | 369 | // AddInterVariantDependency adds a dependency between two variants of the same module.  Variants are always | 
|  | 370 | // ordered in the same orderas they were listed in CreateVariations, and AddInterVariantDependency does not change | 
|  | 371 | // that ordering, but it associates a DependencyTag with the dependency and makes it visible to VisitDirectDeps, | 
|  | 372 | // WalkDeps, etc. | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 373 | AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 374 |  | 
|  | 375 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the | 
|  | 376 | // specified name with the current variant of this module.  Replacements don't take effect until | 
|  | 377 | // after the mutator pass is finished. | 
| Colin Cross | aabf679 | 2017-11-29 00:27:14 -0800 | [diff] [blame] | 378 | ReplaceDependencies(string) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 379 |  | 
|  | 380 | // ReplaceDependencies replaces all dependencies on the identical variant of the module with the | 
|  | 381 | // specified name with the current variant of this module as long as the supplied predicate returns | 
|  | 382 | // true. | 
|  | 383 | // | 
|  | 384 | // Replacements don't take effect until after the mutator pass is finished. | 
| Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 385 | ReplaceDependenciesIf(string, blueprint.ReplaceDependencyPredicate) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 386 |  | 
|  | 387 | // AliasVariation takes a variationName that was passed to CreateVariations for this module, | 
|  | 388 | // and creates an alias from the current variant (before the mutator has run) to the new | 
|  | 389 | // variant.  The alias will be valid until the next time a mutator calls CreateVariations or | 
|  | 390 | // CreateLocalVariations on this module without also calling AliasVariation.  The alias can | 
|  | 391 | // be used to add dependencies on the newly created variant using the variant map from | 
|  | 392 | // before CreateVariations was run. | 
| Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 393 | AliasVariation(variationName string) | 
| Colin Cross | 9f35c3d | 2020-09-16 19:04:41 -0700 | [diff] [blame] | 394 |  | 
|  | 395 | // CreateAliasVariation takes a toVariationName that was passed to CreateVariations for this | 
|  | 396 | // module, and creates an alias from a new fromVariationName variant the toVariationName | 
|  | 397 | // variant.  The alias will be valid until the next time a mutator calls CreateVariations or | 
|  | 398 | // CreateLocalVariations on this module without also calling AliasVariation.  The alias can | 
|  | 399 | // be used to add dependencies on the toVariationName variant using the fromVariationName | 
|  | 400 | // variant. | 
| Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 401 | CreateAliasVariation(fromVariationName, toVariationName string) | 
| Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 402 |  | 
|  | 403 | // SetVariationProvider sets the value for a provider for the given newly created variant of | 
|  | 404 | // the current module, i.e. one of the Modules returned by CreateVariations..  It panics if | 
|  | 405 | // not called during the appropriate mutator or GenerateBuildActions pass for the provider, | 
|  | 406 | // if the value is not of the appropriate type, or if the module is not a newly created | 
|  | 407 | // variant of the current module.  The value should not be modified after being passed to | 
|  | 408 | // SetVariationProvider. | 
|  | 409 | SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 410 | } | 
|  | 411 |  | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 412 | type bottomUpMutatorContext struct { | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 413 | bp blueprint.BottomUpMutatorContext | 
| Colin Cross | 0ea8ba8 | 2019-06-06 14:33:29 -0700 | [diff] [blame] | 414 | baseModuleContext | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 415 | finalPhase bool | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 418 | func bottomUpMutatorContextFactory(ctx blueprint.BottomUpMutatorContext, a Module, | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 419 | finalPhase, bazelConversionMode bool) BottomUpMutatorContext { | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 420 |  | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 421 | moduleContext := a.base().baseModuleContextFactory(ctx) | 
|  | 422 | moduleContext.bazelConversionMode = bazelConversionMode | 
|  | 423 |  | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 424 | return &bottomUpMutatorContext{ | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 425 | bp:                ctx, | 
| Cole Faust | 0abd4b4 | 2023-01-10 10:49:18 -0800 | [diff] [blame] | 426 | baseModuleContext: moduleContext, | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 427 | finalPhase:        finalPhase, | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 428 | } | 
|  | 429 | } | 
|  | 430 |  | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 431 | func (x *registerMutatorsContext) BottomUp(name string, m BottomUpMutator) MutatorHandle { | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 432 | finalPhase := x.finalPhase | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 433 | bazelConversionMode := x.bazelConversionMode | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 434 | f := func(ctx blueprint.BottomUpMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 435 | if a, ok := ctx.Module().(Module); ok { | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 436 | m(bottomUpMutatorContextFactory(ctx, a, finalPhase, bazelConversionMode)) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 437 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 438 | } | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 439 | mutator := &mutator{name: x.mutatorName(name), bottomUpMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 440 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 441 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 442 | } | 
|  | 443 |  | 
| Colin Cross | 617b88a | 2020-08-24 18:04:09 -0700 | [diff] [blame] | 444 | func (x *registerMutatorsContext) BottomUpBlueprint(name string, m blueprint.BottomUpMutator) MutatorHandle { | 
|  | 445 | mutator := &mutator{name: name, bottomUpMutator: m} | 
|  | 446 | x.mutators = append(x.mutators, mutator) | 
|  | 447 | return mutator | 
|  | 448 | } | 
|  | 449 |  | 
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 450 | type IncomingTransitionContext interface { | 
|  | 451 | // Module returns the target of the dependency edge for which the transition | 
|  | 452 | // is being computed | 
|  | 453 | Module() Module | 
|  | 454 |  | 
|  | 455 | // Config returns the configuration for the build. | 
|  | 456 | Config() Config | 
|  | 457 | } | 
|  | 458 |  | 
|  | 459 | type OutgoingTransitionContext interface { | 
|  | 460 | // Module returns the target of the dependency edge for which the transition | 
|  | 461 | // is being computed | 
|  | 462 | Module() Module | 
|  | 463 |  | 
|  | 464 | // DepTag() Returns the dependency tag through which this dependency is | 
|  | 465 | // reached | 
|  | 466 | DepTag() blueprint.DependencyTag | 
|  | 467 | } | 
| Lukacs T. Berki | 0e691c1 | 2022-06-24 10:15:55 +0200 | [diff] [blame] | 468 |  | 
|  | 469 | // Transition mutators implement a top-down mechanism where a module tells its | 
|  | 470 | // direct dependencies what variation they should be built in but the dependency | 
|  | 471 | // has the final say. | 
|  | 472 | // | 
|  | 473 | // When implementing a transition mutator, one needs to implement four methods: | 
|  | 474 | //   - Split() that tells what variations a module has by itself | 
|  | 475 | //   - OutgoingTransition() where a module tells what it wants from its | 
|  | 476 | //     dependency | 
|  | 477 | //   - IncomingTransition() where a module has the final say about its own | 
|  | 478 | //     variation | 
|  | 479 | //   - Mutate() that changes the state of a module depending on its variation | 
|  | 480 | // | 
|  | 481 | // That the effective variation of module B when depended on by module A is the | 
|  | 482 | // composition the outgoing transition of module A and the incoming transition | 
|  | 483 | // of module B. | 
|  | 484 | // | 
|  | 485 | // the outgoing transition should not take the properties of the dependency into | 
|  | 486 | // account, only those of the module that depends on it. For this reason, the | 
|  | 487 | // dependency is not even passed into it as an argument. Likewise, the incoming | 
|  | 488 | // transition should not take the properties of the depending module into | 
|  | 489 | // account and is thus not informed about it. This makes for a nice | 
|  | 490 | // decomposition of the decision logic. | 
|  | 491 | // | 
|  | 492 | // A given transition mutator only affects its own variation; other variations | 
|  | 493 | // stay unchanged along the dependency edges. | 
|  | 494 | // | 
|  | 495 | // Soong makes sure that all modules are created in the desired variations and | 
|  | 496 | // that dependency edges are set up correctly. This ensures that "missing | 
|  | 497 | // variation" errors do not happen and allows for more flexible changes in the | 
|  | 498 | // value of the variation among dependency edges (as oppposed to bottom-up | 
|  | 499 | // mutators where if module A in variation X depends on module B and module B | 
|  | 500 | // has that variation X, A must depend on variation X of B) | 
|  | 501 | // | 
|  | 502 | // The limited power of the context objects passed to individual mutators | 
|  | 503 | // methods also makes it more difficult to shoot oneself in the foot. Complete | 
|  | 504 | // safety is not guaranteed because no one prevents individual transition | 
|  | 505 | // mutators from mutating modules in illegal ways and for e.g. Split() or | 
|  | 506 | // Mutate() to run their own visitations of the transitive dependency of the | 
|  | 507 | // module and both of these are bad ideas, but it's better than no guardrails at | 
|  | 508 | // all. | 
|  | 509 | // | 
|  | 510 | // This model is pretty close to Bazel's configuration transitions. The mapping | 
|  | 511 | // between concepts in Soong and Bazel is as follows: | 
|  | 512 | //   - Module == configured target | 
|  | 513 | //   - Variant == configuration | 
|  | 514 | //   - Variation name == configuration flag | 
|  | 515 | //   - Variation == configuration flag value | 
|  | 516 | //   - Outgoing transition == attribute transition | 
|  | 517 | //   - Incoming transition == rule transition | 
|  | 518 | // | 
|  | 519 | // The Split() method does not have a Bazel equivalent and Bazel split | 
|  | 520 | // transitions do not have a Soong equivalent. | 
|  | 521 | // | 
|  | 522 | // Mutate() does not make sense in Bazel due to the different models of the | 
|  | 523 | // two systems: when creating new variations, Soong clones the old module and | 
|  | 524 | // thus some way is needed to change it state whereas Bazel creates each | 
|  | 525 | // configuration of a given configured target anew. | 
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 526 | type TransitionMutator interface { | 
|  | 527 | // Split returns the set of variations that should be created for a module no | 
|  | 528 | // matter who depends on it. Used when Make depends on a particular variation | 
|  | 529 | // or when the module knows its variations just based on information given to | 
|  | 530 | // it in the Blueprint file. This method should not mutate the module it is | 
|  | 531 | // called on. | 
|  | 532 | Split(ctx BaseModuleContext) []string | 
|  | 533 |  | 
| Lukacs T. Berki | 0e691c1 | 2022-06-24 10:15:55 +0200 | [diff] [blame] | 534 | // Called on a module to determine which variation it wants from its direct | 
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 535 | // dependencies. The dependency itself can override this decision. This method | 
|  | 536 | // should not mutate the module itself. | 
|  | 537 | OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string | 
|  | 538 |  | 
|  | 539 | // Called on a module to determine which variation it should be in based on | 
|  | 540 | // the variation modules that depend on it want. This gives the module a final | 
|  | 541 | // say about its own variations. This method should not mutate the module | 
|  | 542 | // itself. | 
|  | 543 | IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string | 
|  | 544 |  | 
|  | 545 | // Called after a module was split into multiple variations on each variation. | 
|  | 546 | // It should not split the module any further but adding new dependencies is | 
|  | 547 | // fine. Unlike all the other methods on TransitionMutator, this method is | 
|  | 548 | // allowed to mutate the module. | 
|  | 549 | Mutate(ctx BottomUpMutatorContext, variation string) | 
|  | 550 | } | 
|  | 551 |  | 
|  | 552 | type androidTransitionMutator struct { | 
|  | 553 | finalPhase          bool | 
|  | 554 | bazelConversionMode bool | 
|  | 555 | mutator             TransitionMutator | 
|  | 556 | } | 
|  | 557 |  | 
|  | 558 | func (a *androidTransitionMutator) Split(ctx blueprint.BaseModuleContext) []string { | 
|  | 559 | if m, ok := ctx.Module().(Module); ok { | 
|  | 560 | moduleContext := m.base().baseModuleContextFactory(ctx) | 
|  | 561 | moduleContext.bazelConversionMode = a.bazelConversionMode | 
|  | 562 | return a.mutator.Split(&moduleContext) | 
|  | 563 | } else { | 
|  | 564 | return []string{""} | 
|  | 565 | } | 
|  | 566 | } | 
|  | 567 |  | 
|  | 568 | type outgoingTransitionContextImpl struct { | 
|  | 569 | bp blueprint.OutgoingTransitionContext | 
|  | 570 | } | 
|  | 571 |  | 
|  | 572 | func (c *outgoingTransitionContextImpl) Module() Module { | 
|  | 573 | return c.bp.Module().(Module) | 
|  | 574 | } | 
|  | 575 |  | 
|  | 576 | func (c *outgoingTransitionContextImpl) DepTag() blueprint.DependencyTag { | 
|  | 577 | return c.bp.DepTag() | 
|  | 578 | } | 
|  | 579 |  | 
|  | 580 | func (a *androidTransitionMutator) OutgoingTransition(ctx blueprint.OutgoingTransitionContext, sourceVariation string) string { | 
|  | 581 | if _, ok := ctx.Module().(Module); ok { | 
|  | 582 | return a.mutator.OutgoingTransition(&outgoingTransitionContextImpl{bp: ctx}, sourceVariation) | 
|  | 583 | } else { | 
|  | 584 | return "" | 
|  | 585 | } | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | type incomingTransitionContextImpl struct { | 
|  | 589 | bp blueprint.IncomingTransitionContext | 
|  | 590 | } | 
|  | 591 |  | 
|  | 592 | func (c *incomingTransitionContextImpl) Module() Module { | 
|  | 593 | return c.bp.Module().(Module) | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | func (c *incomingTransitionContextImpl) Config() Config { | 
|  | 597 | return c.bp.Config().(Config) | 
|  | 598 | } | 
|  | 599 |  | 
|  | 600 | func (a *androidTransitionMutator) IncomingTransition(ctx blueprint.IncomingTransitionContext, incomingVariation string) string { | 
|  | 601 | if _, ok := ctx.Module().(Module); ok { | 
|  | 602 | return a.mutator.IncomingTransition(&incomingTransitionContextImpl{bp: ctx}, incomingVariation) | 
|  | 603 | } else { | 
|  | 604 | return "" | 
|  | 605 | } | 
|  | 606 | } | 
|  | 607 |  | 
|  | 608 | func (a *androidTransitionMutator) Mutate(ctx blueprint.BottomUpMutatorContext, variation string) { | 
|  | 609 | if am, ok := ctx.Module().(Module); ok { | 
|  | 610 | a.mutator.Mutate(bottomUpMutatorContextFactory(ctx, am, a.finalPhase, a.bazelConversionMode), variation) | 
|  | 611 | } | 
|  | 612 | } | 
|  | 613 |  | 
|  | 614 | func (x *registerMutatorsContext) Transition(name string, m TransitionMutator) { | 
|  | 615 | atm := &androidTransitionMutator{ | 
|  | 616 | finalPhase:          x.finalPhase, | 
|  | 617 | bazelConversionMode: x.bazelConversionMode, | 
|  | 618 | mutator:             m, | 
|  | 619 | } | 
|  | 620 | mutator := &mutator{ | 
|  | 621 | name:              name, | 
|  | 622 | transitionMutator: atm} | 
|  | 623 | x.mutators = append(x.mutators, mutator) | 
|  | 624 | } | 
|  | 625 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 626 | func (x *registerMutatorsContext) mutatorName(name string) string { | 
|  | 627 | if x.bazelConversionMode { | 
|  | 628 | return name + "_bp2build" | 
|  | 629 | } | 
|  | 630 | return name | 
|  | 631 | } | 
|  | 632 |  | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 633 | func (x *registerMutatorsContext) TopDown(name string, m TopDownMutator) MutatorHandle { | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 634 | f := func(ctx blueprint.TopDownMutatorContext) { | 
| Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 635 | if a, ok := ctx.Module().(Module); ok { | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 636 | moduleContext := a.base().baseModuleContextFactory(ctx) | 
|  | 637 | moduleContext.bazelConversionMode = x.bazelConversionMode | 
| Colin Cross | 25de6c3 | 2019-06-06 14:29:25 -0700 | [diff] [blame] | 638 | actx := &topDownMutatorContext{ | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 639 | bp:                ctx, | 
| Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 640 | baseModuleContext: moduleContext, | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 641 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 642 | m(actx) | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 643 | } | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 644 | } | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 645 | mutator := &mutator{name: x.mutatorName(name), topDownMutator: f} | 
| Colin Cross | 795c377 | 2017-03-16 16:50:10 -0700 | [diff] [blame] | 646 | x.mutators = append(x.mutators, mutator) | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 647 | return mutator | 
|  | 648 | } | 
|  | 649 |  | 
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 650 | func (mutator *mutator) componentName() string { | 
|  | 651 | return mutator.name | 
|  | 652 | } | 
|  | 653 |  | 
|  | 654 | func (mutator *mutator) register(ctx *Context) { | 
|  | 655 | blueprintCtx := ctx.Context | 
|  | 656 | var handle blueprint.MutatorHandle | 
|  | 657 | if mutator.bottomUpMutator != nil { | 
|  | 658 | handle = blueprintCtx.RegisterBottomUpMutator(mutator.name, mutator.bottomUpMutator) | 
|  | 659 | } else if mutator.topDownMutator != nil { | 
|  | 660 | handle = blueprintCtx.RegisterTopDownMutator(mutator.name, mutator.topDownMutator) | 
| Lukacs T. Berki | 6c71676 | 2022-06-13 20:50:39 +0200 | [diff] [blame] | 661 | } else if mutator.transitionMutator != nil { | 
|  | 662 | blueprintCtx.RegisterTransitionMutator(mutator.name, mutator.transitionMutator) | 
| Paul Duffin | 1d2d42f | 2021-03-06 20:08:12 +0000 | [diff] [blame] | 663 | } | 
|  | 664 | if mutator.parallel { | 
|  | 665 | handle.Parallel() | 
|  | 666 | } | 
|  | 667 | } | 
|  | 668 |  | 
| Colin Cross | 798bfce | 2016-10-12 14:28:16 -0700 | [diff] [blame] | 669 | type MutatorHandle interface { | 
|  | 670 | Parallel() MutatorHandle | 
|  | 671 | } | 
|  | 672 |  | 
|  | 673 | func (mutator *mutator) Parallel() MutatorHandle { | 
|  | 674 | mutator.parallel = true | 
|  | 675 | return mutator | 
| Colin Cross | 6362e27 | 2015-10-29 15:25:03 -0700 | [diff] [blame] | 676 | } | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 677 |  | 
| Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 678 | func RegisterComponentsMutator(ctx RegisterMutatorsContext) { | 
|  | 679 | ctx.BottomUp("component-deps", componentDepsMutator).Parallel() | 
|  | 680 | } | 
|  | 681 |  | 
|  | 682 | // A special mutator that runs just prior to the deps mutator to allow the dependencies | 
|  | 683 | // on component modules to be added so that they can depend directly on a prebuilt | 
|  | 684 | // module. | 
|  | 685 | func componentDepsMutator(ctx BottomUpMutatorContext) { | 
|  | 686 | if m := ctx.Module(); m.Enabled() { | 
|  | 687 | m.ComponentDepsMutator(ctx) | 
|  | 688 | } | 
|  | 689 | } | 
|  | 690 |  | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 691 | func depsMutator(ctx BottomUpMutatorContext) { | 
| Paul Duffin | 44f1d84 | 2020-06-26 20:17:02 +0100 | [diff] [blame] | 692 | if m := ctx.Module(); m.Enabled() { | 
| Colin Cross | 1e676be | 2016-10-12 14:38:15 -0700 | [diff] [blame] | 693 | m.DepsMutator(ctx) | 
|  | 694 | } | 
|  | 695 | } | 
| Colin Cross | d11fcda | 2017-10-23 17:59:01 -0700 | [diff] [blame] | 696 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 697 | func registerDepsMutator(ctx RegisterMutatorsContext) { | 
|  | 698 | ctx.BottomUp("deps", depsMutator).Parallel() | 
|  | 699 | } | 
|  | 700 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 701 | func (t *bottomUpMutatorContext) CreateBazelTargetModule( | 
| Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 702 | bazelProps bazel.BazelTargetModuleProperties, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 703 | commonAttrs CommonAttributes, | 
| Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 704 | attrs interface{}) { | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 705 | t.createBazelTargetModule(bazelProps, commonAttrs, attrs, bazel.BoolAttribute{}) | 
|  | 706 | } | 
|  | 707 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 708 | func (t *bottomUpMutatorContext) CreateBazelTargetModuleWithRestrictions( | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 709 | bazelProps bazel.BazelTargetModuleProperties, | 
|  | 710 | commonAttrs CommonAttributes, | 
|  | 711 | attrs interface{}, | 
|  | 712 | enabledProperty bazel.BoolAttribute) { | 
|  | 713 | t.createBazelTargetModule(bazelProps, commonAttrs, attrs, enabledProperty) | 
|  | 714 | } | 
|  | 715 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 716 | func (t *bottomUpMutatorContext) MarkBp2buildUnconvertible( | 
| Chris Parsons | 39a1697 | 2023-06-08 14:28:51 +0000 | [diff] [blame] | 717 | reasonType bp2build_metrics_proto.UnconvertedReasonType, detail string) { | 
|  | 718 | mod := t.Module() | 
|  | 719 | mod.base().setBp2buildUnconvertible(reasonType, detail) | 
|  | 720 | } | 
|  | 721 |  | 
| Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 722 | var ( | 
|  | 723 | bazelAliasModuleProperties = bazel.BazelTargetModuleProperties{ | 
|  | 724 | Rule_class: "alias", | 
|  | 725 | } | 
|  | 726 | ) | 
|  | 727 |  | 
|  | 728 | type bazelAliasAttributes struct { | 
|  | 729 | Actual *bazel.LabelAttribute | 
|  | 730 | } | 
|  | 731 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 732 | func (t *bottomUpMutatorContext) CreateBazelTargetAliasInDir( | 
| Spandan Das | abedff0 | 2023-03-07 19:24:34 +0000 | [diff] [blame] | 733 | dir string, | 
|  | 734 | name string, | 
|  | 735 | actual bazel.Label) { | 
|  | 736 | mod := t.Module() | 
|  | 737 | attrs := &bazelAliasAttributes{ | 
|  | 738 | Actual: bazel.MakeLabelAttribute(actual.Label), | 
|  | 739 | } | 
|  | 740 | info := bp2buildInfo{ | 
|  | 741 | Dir:             dir, | 
|  | 742 | BazelProps:      bazelAliasModuleProperties, | 
|  | 743 | CommonAttrs:     CommonAttributes{Name: name}, | 
|  | 744 | ConstraintAttrs: constraintAttributes{}, | 
|  | 745 | Attrs:           attrs, | 
|  | 746 | } | 
|  | 747 | mod.base().addBp2buildInfo(info) | 
|  | 748 | } | 
|  | 749 |  | 
| Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 750 | // Returns the directory in which the bazel target will be generated | 
|  | 751 | // If ca.Dir is not nil, use that | 
|  | 752 | // Otherwise default to the directory of the soong module | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 753 | func dirForBazelTargetGeneration(t *bottomUpMutatorContext, ca *CommonAttributes) string { | 
| Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 754 | dir := t.OtherModuleDir(t.Module()) | 
|  | 755 | if ca.Dir != nil { | 
|  | 756 | dir = *ca.Dir | 
|  | 757 | // Restrict its use to dirs that contain an Android.bp file. | 
|  | 758 | // There are several places in bp2build where we use the existence of Android.bp/BUILD on the filesystem | 
|  | 759 | // to curate a compatible label for src files (e.g. headers for cc). | 
|  | 760 | // If we arbritrarily create BUILD files, then it might render those curated labels incompatible. | 
|  | 761 | if exists, _, _ := t.Config().fs.Exists(filepath.Join(dir, "Android.bp")); !exists { | 
|  | 762 | t.ModuleErrorf("Cannot use ca.Dir to create a BazelTarget in dir: %v since it does not contain an Android.bp file", dir) | 
|  | 763 | } | 
|  | 764 |  | 
|  | 765 | // Set ca.Dir to nil so that it does not get emitted to the BUILD files | 
|  | 766 | ca.Dir = nil | 
|  | 767 | } | 
|  | 768 | return dir | 
|  | 769 | } | 
|  | 770 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 771 | func (t *bottomUpMutatorContext) CreateBazelConfigSetting( | 
| Spandan Das | 6a448ec | 2023-04-19 17:36:12 +0000 | [diff] [blame] | 772 | csa bazel.ConfigSettingAttributes, | 
|  | 773 | ca CommonAttributes, | 
|  | 774 | dir string) { | 
|  | 775 | mod := t.Module() | 
|  | 776 | info := bp2buildInfo{ | 
|  | 777 | Dir: dir, | 
|  | 778 | BazelProps: bazel.BazelTargetModuleProperties{ | 
|  | 779 | Rule_class: "config_setting", | 
|  | 780 | }, | 
|  | 781 | CommonAttrs:     ca, | 
|  | 782 | ConstraintAttrs: constraintAttributes{}, | 
|  | 783 | Attrs:           &csa, | 
|  | 784 | } | 
|  | 785 | mod.base().addBp2buildInfo(info) | 
|  | 786 | } | 
|  | 787 |  | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 788 | // ApexAvailableTags converts the apex_available property value of an ApexModule | 
|  | 789 | // module and returns it as a list of keyed tags. | 
|  | 790 | func ApexAvailableTags(mod Module) bazel.StringListAttribute { | 
|  | 791 | attr := bazel.StringListAttribute{} | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 792 | // Transform specific attributes into tags. | 
|  | 793 | if am, ok := mod.(ApexModule); ok { | 
|  | 794 | // TODO(b/218841706): hidl_interface has the apex_available prop, but it's | 
|  | 795 | // defined directly as a prop and not via ApexModule, so this doesn't | 
|  | 796 | // pick those props up. | 
| Spandan Das | 2dc6dfc | 2023-04-17 19:16:06 +0000 | [diff] [blame] | 797 | apexAvailable := am.apexModuleBase().ApexAvailable() | 
|  | 798 | // If a user does not specify apex_available in Android.bp, then soong provides a default. | 
|  | 799 | // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files. | 
|  | 800 | if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 { | 
|  | 801 | apexAvailable = []string{} | 
|  | 802 | } | 
|  | 803 | attr.Value = ConvertApexAvailableToTags(apexAvailable) | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 804 | } | 
|  | 805 | return attr | 
|  | 806 | } | 
|  | 807 |  | 
| Spandan Das | f57a966 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 808 | func ApexAvailableTagsWithoutTestApexes(ctx BaseModuleContext, mod Module) bazel.StringListAttribute { | 
|  | 809 | attr := bazel.StringListAttribute{} | 
|  | 810 | if am, ok := mod.(ApexModule); ok { | 
|  | 811 | apexAvailableWithoutTestApexes := removeTestApexes(ctx, am.apexModuleBase().ApexAvailable()) | 
|  | 812 | // If a user does not specify apex_available in Android.bp, then soong provides a default. | 
|  | 813 | // To avoid verbosity of BUILD files, remove this default from user-facing BUILD files. | 
|  | 814 | if len(am.apexModuleBase().ApexProperties.Apex_available) == 0 { | 
|  | 815 | apexAvailableWithoutTestApexes = []string{} | 
|  | 816 | } | 
|  | 817 | attr.Value = ConvertApexAvailableToTags(apexAvailableWithoutTestApexes) | 
|  | 818 | } | 
|  | 819 | return attr | 
|  | 820 | } | 
|  | 821 |  | 
|  | 822 | func removeTestApexes(ctx BaseModuleContext, apex_available []string) []string { | 
|  | 823 | testApexes := []string{} | 
|  | 824 | for _, aa := range apex_available { | 
|  | 825 | // ignore the wildcards | 
|  | 826 | if InList(aa, AvailableToRecognziedWildcards) { | 
|  | 827 | continue | 
|  | 828 | } | 
|  | 829 | mod, _ := ctx.ModuleFromName(aa) | 
|  | 830 | if apex, ok := mod.(ApexTestInterface); ok && apex.IsTestApex() { | 
|  | 831 | testApexes = append(testApexes, aa) | 
|  | 832 | } | 
|  | 833 | } | 
|  | 834 | return RemoveListFromList(CopyOf(apex_available), testApexes) | 
|  | 835 | } | 
|  | 836 |  | 
| Cole Faust | fb11c1c | 2023-02-10 11:27:46 -0800 | [diff] [blame] | 837 | func ConvertApexAvailableToTags(apexAvailable []string) []string { | 
|  | 838 | if len(apexAvailable) == 0 { | 
|  | 839 | // We need nil specifically to make bp2build not add the tags property at all, | 
|  | 840 | // instead of adding it with an empty list | 
|  | 841 | return nil | 
|  | 842 | } | 
|  | 843 | result := make([]string, 0, len(apexAvailable)) | 
|  | 844 | for _, a := range apexAvailable { | 
|  | 845 | result = append(result, "apex_available="+a) | 
|  | 846 | } | 
|  | 847 | return result | 
|  | 848 | } | 
|  | 849 |  | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 850 | // ConvertApexAvailableToTagsWithoutTestApexes converts a list of apex names to a list of bazel tags | 
|  | 851 | // This function drops any test apexes from the input. | 
|  | 852 | func ConvertApexAvailableToTagsWithoutTestApexes(ctx BaseModuleContext, apexAvailable []string) []string { | 
|  | 853 | noTestApexes := removeTestApexes(ctx, apexAvailable) | 
|  | 854 | return ConvertApexAvailableToTags(noTestApexes) | 
|  | 855 | } | 
|  | 856 |  | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 857 | func (t *bottomUpMutatorContext) createBazelTargetModule( | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 858 | bazelProps bazel.BazelTargetModuleProperties, | 
|  | 859 | commonAttrs CommonAttributes, | 
|  | 860 | attrs interface{}, | 
|  | 861 | enabledProperty bazel.BoolAttribute) { | 
|  | 862 | constraintAttributes := commonAttrs.fillCommonBp2BuildModuleAttrs(t, enabledProperty) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 863 | mod := t.Module() | 
| Liz Kammer | 2ada09a | 2021-08-11 00:17:36 -0400 | [diff] [blame] | 864 | info := bp2buildInfo{ | 
| Spandan Das | 3131d67 | 2023-08-03 22:33:47 +0000 | [diff] [blame] | 865 | Dir:             dirForBazelTargetGeneration(t, &commonAttrs), | 
| Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 866 | BazelProps:      bazelProps, | 
|  | 867 | CommonAttrs:     commonAttrs, | 
|  | 868 | ConstraintAttrs: constraintAttributes, | 
|  | 869 | Attrs:           attrs, | 
| Jingwen Chen | fb4692a | 2021-02-07 10:05:16 -0500 | [diff] [blame] | 870 | } | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 447f6c9 | 2021-08-31 20:30:36 +0000 | [diff] [blame] | 871 | mod.base().addBp2buildInfo(info) | 
| Jingwen Chen | 1fd1469 | 2021-02-05 03:01:50 -0500 | [diff] [blame] | 872 | } | 
|  | 873 |  | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 874 | // android.topDownMutatorContext either has to embed blueprint.TopDownMutatorContext, in which case every method that | 
|  | 875 | // has an overridden version in android.BaseModuleContext has to be manually forwarded to BaseModuleContext to avoid | 
|  | 876 | // ambiguous method errors, or it has to store a blueprint.TopDownMutatorContext non-embedded, in which case every | 
|  | 877 | // non-overridden method has to be forwarded.  There are fewer non-overridden methods, so use the latter.  The following | 
|  | 878 | // methods forward to the identical blueprint versions for topDownMutatorContext and bottomUpMutatorContext. | 
|  | 879 |  | 
| Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 880 | func (t *topDownMutatorContext) MutatorName() string { | 
|  | 881 | return t.bp.MutatorName() | 
|  | 882 | } | 
|  | 883 |  | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 884 | func (t *topDownMutatorContext) Rename(name string) { | 
|  | 885 | t.bp.Rename(name) | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 886 | t.Module().base().commonProperties.DebugName = name | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 887 | } | 
|  | 888 |  | 
| Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 889 | func (t *topDownMutatorContext) createModule(factory blueprint.ModuleFactory, name string, props ...interface{}) blueprint.Module { | 
|  | 890 | return t.bp.CreateModule(factory, name, props...) | 
|  | 891 | } | 
|  | 892 |  | 
| Colin Cross | e003c4a | 2019-09-25 12:58:36 -0700 | [diff] [blame] | 893 | func (t *topDownMutatorContext) CreateModule(factory ModuleFactory, props ...interface{}) Module { | 
| Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 894 | return createModule(t, factory, "_topDownMutatorModule", props...) | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 895 | } | 
|  | 896 |  | 
| Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 897 | func (t *topDownMutatorContext) createModuleWithoutInheritance(factory ModuleFactory, props ...interface{}) Module { | 
| Liz Kammer | f31c900 | 2022-04-26 09:08:55 -0400 | [diff] [blame] | 898 | module := t.bp.CreateModule(ModuleFactoryAdaptor(factory), "", props...).(Module) | 
| Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 899 | return module | 
|  | 900 | } | 
|  | 901 |  | 
| Colin Cross | cb55e08 | 2019-07-01 15:32:31 -0700 | [diff] [blame] | 902 | func (b *bottomUpMutatorContext) MutatorName() string { | 
|  | 903 | return b.bp.MutatorName() | 
|  | 904 | } | 
|  | 905 |  | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 906 | func (b *bottomUpMutatorContext) Rename(name string) { | 
|  | 907 | b.bp.Rename(name) | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 908 | b.Module().base().commonProperties.DebugName = name | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 909 | } | 
|  | 910 |  | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 911 | func (b *bottomUpMutatorContext) AddDependency(module blueprint.Module, tag blueprint.DependencyTag, name ...string) []blueprint.Module { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 912 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 913 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 914 | } | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 915 | return b.bp.AddDependency(module, tag, name...) | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 916 | } | 
|  | 917 |  | 
|  | 918 | func (b *bottomUpMutatorContext) AddReverseDependency(module blueprint.Module, tag blueprint.DependencyTag, name string) { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 919 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 920 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 921 | } | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 922 | b.bp.AddReverseDependency(module, tag, name) | 
|  | 923 | } | 
|  | 924 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 925 | func (b *bottomUpMutatorContext) CreateVariations(variations ...string) []Module { | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 926 | if b.finalPhase { | 
|  | 927 | panic("CreateVariations not allowed in FinalDepsMutators") | 
|  | 928 | } | 
|  | 929 |  | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 930 | modules := b.bp.CreateVariations(variations...) | 
|  | 931 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 932 | aModules := make([]Module, len(modules)) | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 933 | for i := range variations { | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 934 | aModules[i] = modules[i].(Module) | 
|  | 935 | base := aModules[i].base() | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 936 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) | 
|  | 937 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) | 
|  | 938 | } | 
|  | 939 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 940 | return aModules | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 941 | } | 
|  | 942 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 943 | func (b *bottomUpMutatorContext) CreateLocalVariations(variations ...string) []Module { | 
| Martin Stjernholm | 710ec3a | 2020-01-16 15:12:04 +0000 | [diff] [blame] | 944 | if b.finalPhase { | 
|  | 945 | panic("CreateLocalVariations not allowed in FinalDepsMutators") | 
|  | 946 | } | 
|  | 947 |  | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 948 | modules := b.bp.CreateLocalVariations(variations...) | 
|  | 949 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 950 | aModules := make([]Module, len(modules)) | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 951 | for i := range variations { | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 952 | aModules[i] = modules[i].(Module) | 
|  | 953 | base := aModules[i].base() | 
| Colin Cross | 9a36223 | 2019-07-01 15:32:45 -0700 | [diff] [blame] | 954 | base.commonProperties.DebugMutators = append(base.commonProperties.DebugMutators, b.MutatorName()) | 
|  | 955 | base.commonProperties.DebugVariations = append(base.commonProperties.DebugVariations, variations[i]) | 
|  | 956 | } | 
|  | 957 |  | 
| Colin Cross | 43b92e0 | 2019-11-18 15:28:57 -0800 | [diff] [blame] | 958 | return aModules | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 959 | } | 
|  | 960 |  | 
|  | 961 | func (b *bottomUpMutatorContext) SetDependencyVariation(variation string) { | 
|  | 962 | b.bp.SetDependencyVariation(variation) | 
|  | 963 | } | 
|  | 964 |  | 
| Jiyong Park | 1d1119f | 2019-07-29 21:27:18 +0900 | [diff] [blame] | 965 | func (b *bottomUpMutatorContext) SetDefaultDependencyVariation(variation *string) { | 
|  | 966 | b.bp.SetDefaultDependencyVariation(variation) | 
|  | 967 | } | 
|  | 968 |  | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 969 | func (b *bottomUpMutatorContext) AddVariationDependencies(variations []blueprint.Variation, tag blueprint.DependencyTag, | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 970 | names ...string) []blueprint.Module { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 971 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 972 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 973 | } | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 974 | return b.bp.AddVariationDependencies(variations, tag, names...) | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 975 | } | 
|  | 976 |  | 
|  | 977 | func (b *bottomUpMutatorContext) AddFarVariationDependencies(variations []blueprint.Variation, | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 978 | tag blueprint.DependencyTag, names ...string) []blueprint.Module { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 979 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 980 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 981 | } | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 982 |  | 
| Colin Cross | 4f1dcb0 | 2020-09-16 18:45:04 -0700 | [diff] [blame] | 983 | return b.bp.AddFarVariationDependencies(variations, tag, names...) | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 984 | } | 
|  | 985 |  | 
|  | 986 | func (b *bottomUpMutatorContext) AddInterVariantDependency(tag blueprint.DependencyTag, from, to blueprint.Module) { | 
|  | 987 | b.bp.AddInterVariantDependency(tag, from, to) | 
|  | 988 | } | 
|  | 989 |  | 
|  | 990 | func (b *bottomUpMutatorContext) ReplaceDependencies(name string) { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 991 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 992 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 993 | } | 
| Colin Cross | dc35e21 | 2019-06-06 16:13:11 -0700 | [diff] [blame] | 994 | b.bp.ReplaceDependencies(name) | 
|  | 995 | } | 
| Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 996 |  | 
| Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 997 | func (b *bottomUpMutatorContext) ReplaceDependenciesIf(name string, predicate blueprint.ReplaceDependencyPredicate) { | 
| Liz Kammer | c13f785 | 2023-05-17 13:01:48 -0400 | [diff] [blame] | 998 | if b.baseModuleContext.checkedMissingDeps() { | 
|  | 999 | panic("Adding deps not allowed after checking for missing deps") | 
|  | 1000 | } | 
| Paul Duffin | 80342d7 | 2020-06-26 22:08:43 +0100 | [diff] [blame] | 1001 | b.bp.ReplaceDependenciesIf(name, predicate) | 
|  | 1002 | } | 
|  | 1003 |  | 
| Jaewoong Jung | 9f88ce2 | 2019-11-15 10:57:34 -0800 | [diff] [blame] | 1004 | func (b *bottomUpMutatorContext) AliasVariation(variationName string) { | 
|  | 1005 | b.bp.AliasVariation(variationName) | 
|  | 1006 | } | 
| Colin Cross | 1b9604b | 2020-08-11 12:03:56 -0700 | [diff] [blame] | 1007 |  | 
|  | 1008 | func (b *bottomUpMutatorContext) CreateAliasVariation(fromVariationName, toVariationName string) { | 
|  | 1009 | b.bp.CreateAliasVariation(fromVariationName, toVariationName) | 
|  | 1010 | } | 
| Colin Cross | d27e7b8 | 2020-07-02 11:38:17 -0700 | [diff] [blame] | 1011 |  | 
|  | 1012 | func (b *bottomUpMutatorContext) SetVariationProvider(module blueprint.Module, provider blueprint.ProviderKey, value interface{}) { | 
|  | 1013 | b.bp.SetVariationProvider(module, provider, value) | 
|  | 1014 | } |