Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 17 | type ImageInterfaceContext interface { |
| 18 | ArchModuleContext |
| 19 | |
| 20 | Module() Module |
| 21 | |
| 22 | ModuleErrorf(fmt string, args ...interface{}) |
| 23 | PropertyErrorf(property, fmt string, args ...interface{}) |
| 24 | |
| 25 | DeviceSpecific() bool |
| 26 | SocSpecific() bool |
| 27 | ProductSpecific() bool |
| 28 | SystemExtSpecific() bool |
| 29 | Platform() bool |
| 30 | |
| 31 | Config() Config |
| 32 | } |
| 33 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 34 | // ImageInterface is implemented by modules that need to be split by the imageTransitionMutator. |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 35 | type ImageInterface interface { |
| 36 | // ImageMutatorBegin is called before any other method in the ImageInterface. |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 37 | ImageMutatorBegin(ctx ImageInterfaceContext) |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 38 | |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 39 | // VendorVariantNeeded should return true if the module needs a vendor variant (installed on the vendor image). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 40 | VendorVariantNeeded(ctx ImageInterfaceContext) bool |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 41 | |
Jihoon Kang | c3d4e11 | 2024-06-24 22:16:27 +0000 | [diff] [blame] | 42 | // ProductVariantNeeded should return true if the module needs a product variant (installed on the product image). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 43 | ProductVariantNeeded(ctx ImageInterfaceContext) bool |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 44 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 45 | // CoreVariantNeeded should return true if the module needs a core variant (installed on the system image). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 46 | CoreVariantNeeded(ctx ImageInterfaceContext) bool |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 47 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 48 | // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the |
| 49 | // ramdisk partition). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 50 | RamdiskVariantNeeded(ctx ImageInterfaceContext) bool |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 51 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 52 | // VendorRamdiskVariantNeeded should return true if the module needs a vendor ramdisk variant (installed on the |
| 53 | // vendor ramdisk partition). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 54 | VendorRamdiskVariantNeeded(ctx ImageInterfaceContext) bool |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 55 | |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 56 | // DebugRamdiskVariantNeeded should return true if the module needs a debug ramdisk variant (installed on the |
| 57 | // debug ramdisk partition: $(PRODUCT_OUT)/debug_ramdisk). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 58 | DebugRamdiskVariantNeeded(ctx ImageInterfaceContext) bool |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 59 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 60 | // RecoveryVariantNeeded should return true if the module needs a recovery variant (installed on the |
| 61 | // recovery partition). |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 62 | RecoveryVariantNeeded(ctx ImageInterfaceContext) bool |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 63 | |
| 64 | // ExtraImageVariations should return a list of the additional variations needed for the module. After the |
| 65 | // variants are created the SetImageVariation method will be called on each newly created variant with the |
| 66 | // its variation. |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 67 | ExtraImageVariations(ctx ImageInterfaceContext) []string |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 68 | |
Lukacs T. Berki | 2f5c340 | 2021-06-15 11:27:56 +0200 | [diff] [blame] | 69 | // SetImageVariation is called for each newly created image variant. The receiver is the original |
Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 70 | // module, "variation" is the name of the newly created variant. "variation" is set on the receiver. |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame] | 71 | SetImageVariation(ctx ImageInterfaceContext, variation string) |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | const ( |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 75 | // VendorVariation is the variant name used for /vendor code that does not |
| 76 | // compile against the VNDK. |
| 77 | VendorVariation string = "vendor" |
| 78 | |
| 79 | // ProductVariation is the variant name used for /product code that does not |
| 80 | // compile against the VNDK. |
| 81 | ProductVariation string = "product" |
| 82 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 83 | // CoreVariation is the variant used for framework-private libraries, or |
| 84 | // SDK libraries. (which framework-private libraries can use), which |
| 85 | // will be installed to the system image. |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 86 | CoreVariation string = "" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 87 | |
| 88 | // RecoveryVariation means a module to be installed to recovery image. |
| 89 | RecoveryVariation string = "recovery" |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 90 | |
| 91 | // RamdiskVariation means a module to be installed to ramdisk image. |
| 92 | RamdiskVariation string = "ramdisk" |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 93 | |
| 94 | // VendorRamdiskVariation means a module to be installed to vendor ramdisk image. |
| 95 | VendorRamdiskVariation string = "vendor_ramdisk" |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 96 | |
| 97 | // DebugRamdiskVariation means a module to be installed to debug ramdisk image. |
| 98 | DebugRamdiskVariation string = "debug_ramdisk" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 99 | ) |
| 100 | |
Cole Faust | 5b7635d | 2024-10-28 13:01:12 -0700 | [diff] [blame] | 101 | type imageInterfaceContextAdapter struct { |
| 102 | IncomingTransitionContext |
| 103 | kind moduleKind |
| 104 | } |
| 105 | |
| 106 | var _ ImageInterfaceContext = (*imageInterfaceContextAdapter)(nil) |
| 107 | |
| 108 | func (e *imageInterfaceContextAdapter) Platform() bool { |
| 109 | return e.kind == platformModule |
| 110 | } |
| 111 | |
| 112 | func (e *imageInterfaceContextAdapter) DeviceSpecific() bool { |
| 113 | return e.kind == deviceSpecificModule |
| 114 | } |
| 115 | |
| 116 | func (e *imageInterfaceContextAdapter) SocSpecific() bool { |
| 117 | return e.kind == socSpecificModule |
| 118 | } |
| 119 | |
| 120 | func (e *imageInterfaceContextAdapter) ProductSpecific() bool { |
| 121 | return e.kind == productSpecificModule |
| 122 | } |
| 123 | |
| 124 | func (e *imageInterfaceContextAdapter) SystemExtSpecific() bool { |
| 125 | return e.kind == systemExtSpecificModule |
| 126 | } |
| 127 | |
Cole Faust | 6ec8d4a | 2024-10-29 11:01:19 -0700 | [diff] [blame] | 128 | // imageMutatorBeginMutator calls ImageMutatorBegin on all modules that may have image variants. |
| 129 | // This happens right before the imageTransitionMutator runs. It's needed to initialize these |
| 130 | // modules so that they return the correct results for all the other ImageInterface methods, |
| 131 | // which the imageTransitionMutator will call. Transition mutators should also not mutate modules |
| 132 | // (except in their Mutate() function), which this method does, so we run it in a separate mutator |
| 133 | // first. |
Cole Faust | 5b7635d | 2024-10-28 13:01:12 -0700 | [diff] [blame] | 134 | func imageMutatorBeginMutator(ctx BottomUpMutatorContext) { |
| 135 | if m, ok := ctx.Module().(ImageInterface); ok && ctx.Os() == Android { |
| 136 | m.ImageMutatorBegin(ctx) |
| 137 | } |
| 138 | } |
| 139 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 140 | // imageTransitionMutator creates variants for modules that implement the ImageInterface that |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 141 | // allow them to build differently for each partition (recovery, core, vendor, etc.). |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 142 | type imageTransitionMutator struct{} |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 143 | |
Cole Faust | 5b7635d | 2024-10-28 13:01:12 -0700 | [diff] [blame] | 144 | func getImageVariations(ctx ImageInterfaceContext) []string { |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 145 | var variations []string |
| 146 | |
| 147 | if m, ok := ctx.Module().(ImageInterface); ctx.Os() == Android && ok { |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 148 | if m.CoreVariantNeeded(ctx) { |
| 149 | variations = append(variations, CoreVariation) |
| 150 | } |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 151 | if m.RamdiskVariantNeeded(ctx) { |
| 152 | variations = append(variations, RamdiskVariation) |
| 153 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 154 | if m.VendorRamdiskVariantNeeded(ctx) { |
| 155 | variations = append(variations, VendorRamdiskVariation) |
| 156 | } |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 157 | if m.DebugRamdiskVariantNeeded(ctx) { |
| 158 | variations = append(variations, DebugRamdiskVariation) |
| 159 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 160 | if m.RecoveryVariantNeeded(ctx) { |
| 161 | variations = append(variations, RecoveryVariation) |
| 162 | } |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 163 | if m.VendorVariantNeeded(ctx) { |
| 164 | variations = append(variations, VendorVariation) |
| 165 | } |
| 166 | if m.ProductVariantNeeded(ctx) { |
| 167 | variations = append(variations, ProductVariation) |
| 168 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 169 | |
| 170 | extraVariations := m.ExtraImageVariations(ctx) |
| 171 | variations = append(variations, extraVariations...) |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 172 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 173 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 174 | if len(variations) == 0 { |
| 175 | variations = append(variations, "") |
| 176 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 177 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 178 | return variations |
| 179 | } |
| 180 | |
Cole Faust | 5b7635d | 2024-10-28 13:01:12 -0700 | [diff] [blame] | 181 | func (imageTransitionMutator) Split(ctx BaseModuleContext) []string { |
| 182 | return getImageVariations(ctx) |
| 183 | } |
| 184 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 185 | func (imageTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string { |
| 186 | return sourceVariation |
| 187 | } |
| 188 | |
| 189 | func (imageTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string { |
| 190 | if _, ok := ctx.Module().(ImageInterface); ctx.Os() != Android || !ok { |
| 191 | return CoreVariation |
| 192 | } |
Cole Faust | 5b7635d | 2024-10-28 13:01:12 -0700 | [diff] [blame] | 193 | variations := getImageVariations(&imageInterfaceContextAdapter{ |
| 194 | IncomingTransitionContext: ctx, |
| 195 | kind: determineModuleKind(ctx.Module().base(), ctx), |
| 196 | }) |
| 197 | // If there's only 1 possible variation, use that. This is a holdover from when blueprint, |
| 198 | // when adding dependencies, would use the only variant of a module regardless of its variations |
| 199 | // if only 1 variant existed. |
| 200 | if len(variations) == 1 { |
| 201 | return variations[0] |
| 202 | } |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 203 | return incomingVariation |
| 204 | } |
| 205 | |
| 206 | func (imageTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) { |
| 207 | ctx.Module().base().setImageVariation(variation) |
| 208 | if m, ok := ctx.Module().(ImageInterface); ok { |
| 209 | m.SetImageVariation(ctx, variation) |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 210 | } |
| 211 | } |