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 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 17 | // 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] | 18 | type ImageInterface interface { |
| 19 | // ImageMutatorBegin is called before any other method in the ImageInterface. |
| 20 | ImageMutatorBegin(ctx BaseModuleContext) |
| 21 | |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 22 | // VendorVariantNeeded should return true if the module needs a vendor variant (installed on the vendor image). |
| 23 | VendorVariantNeeded(ctx BaseModuleContext) bool |
| 24 | |
Jihoon Kang | c3d4e11 | 2024-06-24 22:16:27 +0000 | [diff] [blame] | 25 | // ProductVariantNeeded should return true if the module needs a product variant (installed on the product image). |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 26 | ProductVariantNeeded(ctx BaseModuleContext) bool |
| 27 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 28 | // CoreVariantNeeded should return true if the module needs a core variant (installed on the system image). |
| 29 | CoreVariantNeeded(ctx BaseModuleContext) bool |
| 30 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 31 | // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the |
| 32 | // ramdisk partition). |
| 33 | RamdiskVariantNeeded(ctx BaseModuleContext) bool |
| 34 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 35 | // VendorRamdiskVariantNeeded should return true if the module needs a vendor ramdisk variant (installed on the |
| 36 | // vendor ramdisk partition). |
| 37 | VendorRamdiskVariantNeeded(ctx BaseModuleContext) bool |
| 38 | |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 39 | // DebugRamdiskVariantNeeded should return true if the module needs a debug ramdisk variant (installed on the |
| 40 | // debug ramdisk partition: $(PRODUCT_OUT)/debug_ramdisk). |
| 41 | DebugRamdiskVariantNeeded(ctx BaseModuleContext) bool |
| 42 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 43 | // RecoveryVariantNeeded should return true if the module needs a recovery variant (installed on the |
| 44 | // recovery partition). |
| 45 | RecoveryVariantNeeded(ctx BaseModuleContext) bool |
| 46 | |
| 47 | // ExtraImageVariations should return a list of the additional variations needed for the module. After the |
| 48 | // variants are created the SetImageVariation method will be called on each newly created variant with the |
| 49 | // its variation. |
| 50 | ExtraImageVariations(ctx BaseModuleContext) []string |
| 51 | |
Lukacs T. Berki | 2f5c340 | 2021-06-15 11:27:56 +0200 | [diff] [blame] | 52 | // 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] | 53 | // module, "variation" is the name of the newly created variant. "variation" is set on the receiver. |
| 54 | SetImageVariation(ctx BaseModuleContext, variation string) |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | const ( |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 58 | // VendorVariation is the variant name used for /vendor code that does not |
| 59 | // compile against the VNDK. |
| 60 | VendorVariation string = "vendor" |
| 61 | |
| 62 | // ProductVariation is the variant name used for /product code that does not |
| 63 | // compile against the VNDK. |
| 64 | ProductVariation string = "product" |
| 65 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 66 | // CoreVariation is the variant used for framework-private libraries, or |
| 67 | // SDK libraries. (which framework-private libraries can use), which |
| 68 | // will be installed to the system image. |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 69 | CoreVariation string = "" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 70 | |
| 71 | // RecoveryVariation means a module to be installed to recovery image. |
| 72 | RecoveryVariation string = "recovery" |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 73 | |
| 74 | // RamdiskVariation means a module to be installed to ramdisk image. |
| 75 | RamdiskVariation string = "ramdisk" |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 76 | |
| 77 | // VendorRamdiskVariation means a module to be installed to vendor ramdisk image. |
| 78 | VendorRamdiskVariation string = "vendor_ramdisk" |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 79 | |
| 80 | // DebugRamdiskVariation means a module to be installed to debug ramdisk image. |
| 81 | DebugRamdiskVariation string = "debug_ramdisk" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 82 | ) |
| 83 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 84 | // imageTransitionMutator creates variants for modules that implement the ImageInterface that |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 85 | // allow them to build differently for each partition (recovery, core, vendor, etc.). |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 86 | type imageTransitionMutator struct{} |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 87 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 88 | func (imageTransitionMutator) Split(ctx BaseModuleContext) []string { |
| 89 | var variations []string |
| 90 | |
| 91 | if m, ok := ctx.Module().(ImageInterface); ctx.Os() == Android && ok { |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 92 | m.ImageMutatorBegin(ctx) |
| 93 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 94 | if m.CoreVariantNeeded(ctx) { |
| 95 | variations = append(variations, CoreVariation) |
| 96 | } |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 97 | if m.RamdiskVariantNeeded(ctx) { |
| 98 | variations = append(variations, RamdiskVariation) |
| 99 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 100 | if m.VendorRamdiskVariantNeeded(ctx) { |
| 101 | variations = append(variations, VendorRamdiskVariation) |
| 102 | } |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 103 | if m.DebugRamdiskVariantNeeded(ctx) { |
| 104 | variations = append(variations, DebugRamdiskVariation) |
| 105 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 106 | if m.RecoveryVariantNeeded(ctx) { |
| 107 | variations = append(variations, RecoveryVariation) |
| 108 | } |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 109 | if m.VendorVariantNeeded(ctx) { |
| 110 | variations = append(variations, VendorVariation) |
| 111 | } |
| 112 | if m.ProductVariantNeeded(ctx) { |
| 113 | variations = append(variations, ProductVariation) |
| 114 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 115 | |
| 116 | extraVariations := m.ExtraImageVariations(ctx) |
| 117 | variations = append(variations, extraVariations...) |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 118 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 119 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 120 | if len(variations) == 0 { |
| 121 | variations = append(variations, "") |
| 122 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 123 | |
Jihoon Kang | 5402bbd | 2024-07-31 18:37:49 +0000 | [diff] [blame] | 124 | return variations |
| 125 | } |
| 126 | |
| 127 | func (imageTransitionMutator) OutgoingTransition(ctx OutgoingTransitionContext, sourceVariation string) string { |
| 128 | return sourceVariation |
| 129 | } |
| 130 | |
| 131 | func (imageTransitionMutator) IncomingTransition(ctx IncomingTransitionContext, incomingVariation string) string { |
| 132 | if _, ok := ctx.Module().(ImageInterface); ctx.Os() != Android || !ok { |
| 133 | return CoreVariation |
| 134 | } |
| 135 | return incomingVariation |
| 136 | } |
| 137 | |
| 138 | func (imageTransitionMutator) Mutate(ctx BottomUpMutatorContext, variation string) { |
| 139 | ctx.Module().base().setImageVariation(variation) |
| 140 | if m, ok := ctx.Module().(ImageInterface); ok { |
| 141 | m.SetImageVariation(ctx, variation) |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 142 | } |
| 143 | } |