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