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 | |
Colin Cross | ae6c520 | 2019-11-20 13:35:50 -0800 | [diff] [blame] | 17 | // ImageInterface is implemented by modules that need to be split by the imageMutator. |
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 | |
| 22 | // CoreVariantNeeded should return true if the module needs a core variant (installed on the system image). |
| 23 | CoreVariantNeeded(ctx BaseModuleContext) bool |
| 24 | |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 25 | // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the |
| 26 | // ramdisk partition). |
| 27 | RamdiskVariantNeeded(ctx BaseModuleContext) bool |
| 28 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 29 | // VendorRamdiskVariantNeeded should return true if the module needs a vendor ramdisk variant (installed on the |
| 30 | // vendor ramdisk partition). |
| 31 | VendorRamdiskVariantNeeded(ctx BaseModuleContext) bool |
| 32 | |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 33 | // RecoveryVariantNeeded should return true if the module needs a recovery variant (installed on the |
| 34 | // recovery partition). |
| 35 | RecoveryVariantNeeded(ctx BaseModuleContext) bool |
| 36 | |
| 37 | // ExtraImageVariations should return a list of the additional variations needed for the module. After the |
| 38 | // variants are created the SetImageVariation method will be called on each newly created variant with the |
| 39 | // its variation. |
| 40 | ExtraImageVariations(ctx BaseModuleContext) []string |
| 41 | |
| 42 | // SetImageVariation will be passed a newly created recovery variant of the module. ModuleBase implements |
| 43 | // SetImageVariation, most module types will not need to override it, and those that do must call the |
| 44 | // overridden method. Implementors of SetImageVariation must be careful to modify the module argument |
| 45 | // and not the receiver. |
| 46 | SetImageVariation(ctx BaseModuleContext, variation string, module Module) |
| 47 | } |
| 48 | |
| 49 | const ( |
| 50 | // CoreVariation is the variant used for framework-private libraries, or |
| 51 | // SDK libraries. (which framework-private libraries can use), which |
| 52 | // will be installed to the system image. |
Colin Cross | 7113d20 | 2019-11-20 16:39:12 -0800 | [diff] [blame] | 53 | CoreVariation string = "" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 54 | |
| 55 | // RecoveryVariation means a module to be installed to recovery image. |
| 56 | RecoveryVariation string = "recovery" |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 57 | |
| 58 | // RamdiskVariation means a module to be installed to ramdisk image. |
| 59 | RamdiskVariation string = "ramdisk" |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 60 | |
| 61 | // VendorRamdiskVariation means a module to be installed to vendor ramdisk image. |
| 62 | VendorRamdiskVariation string = "vendor_ramdisk" |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 63 | ) |
| 64 | |
Colin Cross | ae6c520 | 2019-11-20 13:35:50 -0800 | [diff] [blame] | 65 | // imageMutator creates variants for modules that implement the ImageInterface that |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 66 | // allow them to build differently for each partition (recovery, core, vendor, etc.). |
Colin Cross | ae6c520 | 2019-11-20 13:35:50 -0800 | [diff] [blame] | 67 | func imageMutator(ctx BottomUpMutatorContext) { |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 68 | if ctx.Os() != Android { |
| 69 | return |
| 70 | } |
| 71 | |
| 72 | if m, ok := ctx.Module().(ImageInterface); ok { |
| 73 | m.ImageMutatorBegin(ctx) |
| 74 | |
| 75 | var variations []string |
| 76 | |
| 77 | if m.CoreVariantNeeded(ctx) { |
| 78 | variations = append(variations, CoreVariation) |
| 79 | } |
Yifan Hong | 1b3348d | 2020-01-21 15:53:22 -0800 | [diff] [blame] | 80 | if m.RamdiskVariantNeeded(ctx) { |
| 81 | variations = append(variations, RamdiskVariation) |
| 82 | } |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 83 | if m.VendorRamdiskVariantNeeded(ctx) { |
| 84 | variations = append(variations, VendorRamdiskVariation) |
| 85 | } |
Colin Cross | 7228ecd | 2019-11-18 16:00:16 -0800 | [diff] [blame] | 86 | if m.RecoveryVariantNeeded(ctx) { |
| 87 | variations = append(variations, RecoveryVariation) |
| 88 | } |
| 89 | |
| 90 | extraVariations := m.ExtraImageVariations(ctx) |
| 91 | variations = append(variations, extraVariations...) |
| 92 | |
| 93 | if len(variations) == 0 { |
| 94 | return |
| 95 | } |
| 96 | |
| 97 | mod := ctx.CreateVariations(variations...) |
| 98 | for i, v := range variations { |
| 99 | mod[i].base().setImageVariation(v) |
| 100 | m.SetImageVariation(ctx, v, mod[i]) |
| 101 | } |
| 102 | } |
| 103 | } |