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