blob: bdb9be04d017b625a3d28a0374ab127db8bc64b9 [file] [log] [blame]
Colin Cross7228ecd2019-11-18 16:00:16 -08001// 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
Colin Crossae6c5202019-11-20 13:35:50 -080017// ImageInterface is implemented by modules that need to be split by the imageMutator.
Colin Cross7228ecd2019-11-18 16:00:16 -080018type 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 Hong1b3348d2020-01-21 15:53:22 -080025 // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the
26 // ramdisk partition).
27 RamdiskVariantNeeded(ctx BaseModuleContext) bool
28
Yifan Hong60e0cfb2020-10-21 15:17:56 -070029 // 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
Inseob Kimf84e9c02021-04-08 21:13:22 +090033 // DebugRamdiskVariantNeeded should return true if the module needs a debug ramdisk variant (installed on the
34 // debug ramdisk partition: $(PRODUCT_OUT)/debug_ramdisk/first_stage_ramdisk if BOARD_USES_RECOVERY_AS_ROOT is
35 // true, $(PRODUCT_OUT)/debug_ramdisk otherise).
36 DebugRamdiskVariantNeeded(ctx BaseModuleContext) bool
37
Colin Cross7228ecd2019-11-18 16:00:16 -080038 // RecoveryVariantNeeded should return true if the module needs a recovery variant (installed on the
39 // recovery partition).
40 RecoveryVariantNeeded(ctx BaseModuleContext) bool
41
42 // ExtraImageVariations should return a list of the additional variations needed for the module. After the
43 // variants are created the SetImageVariation method will be called on each newly created variant with the
44 // its variation.
45 ExtraImageVariations(ctx BaseModuleContext) []string
46
47 // SetImageVariation will be passed a newly created recovery variant of the module. ModuleBase implements
48 // SetImageVariation, most module types will not need to override it, and those that do must call the
49 // overridden method. Implementors of SetImageVariation must be careful to modify the module argument
50 // and not the receiver.
51 SetImageVariation(ctx BaseModuleContext, variation string, module Module)
52}
53
54const (
55 // CoreVariation is the variant used for framework-private libraries, or
56 // SDK libraries. (which framework-private libraries can use), which
57 // will be installed to the system image.
Colin Cross7113d202019-11-20 16:39:12 -080058 CoreVariation string = ""
Colin Cross7228ecd2019-11-18 16:00:16 -080059
60 // RecoveryVariation means a module to be installed to recovery image.
61 RecoveryVariation string = "recovery"
Yifan Hong1b3348d2020-01-21 15:53:22 -080062
63 // RamdiskVariation means a module to be installed to ramdisk image.
64 RamdiskVariation string = "ramdisk"
Yifan Hong60e0cfb2020-10-21 15:17:56 -070065
66 // VendorRamdiskVariation means a module to be installed to vendor ramdisk image.
67 VendorRamdiskVariation string = "vendor_ramdisk"
Inseob Kimf84e9c02021-04-08 21:13:22 +090068
69 // DebugRamdiskVariation means a module to be installed to debug ramdisk image.
70 DebugRamdiskVariation string = "debug_ramdisk"
Colin Cross7228ecd2019-11-18 16:00:16 -080071)
72
Colin Crossae6c5202019-11-20 13:35:50 -080073// imageMutator creates variants for modules that implement the ImageInterface that
Colin Cross7228ecd2019-11-18 16:00:16 -080074// allow them to build differently for each partition (recovery, core, vendor, etc.).
Colin Crossae6c5202019-11-20 13:35:50 -080075func imageMutator(ctx BottomUpMutatorContext) {
Colin Cross7228ecd2019-11-18 16:00:16 -080076 if ctx.Os() != Android {
77 return
78 }
79
80 if m, ok := ctx.Module().(ImageInterface); ok {
81 m.ImageMutatorBegin(ctx)
82
83 var variations []string
84
85 if m.CoreVariantNeeded(ctx) {
86 variations = append(variations, CoreVariation)
87 }
Yifan Hong1b3348d2020-01-21 15:53:22 -080088 if m.RamdiskVariantNeeded(ctx) {
89 variations = append(variations, RamdiskVariation)
90 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -070091 if m.VendorRamdiskVariantNeeded(ctx) {
92 variations = append(variations, VendorRamdiskVariation)
93 }
Inseob Kimf84e9c02021-04-08 21:13:22 +090094 if m.DebugRamdiskVariantNeeded(ctx) {
95 variations = append(variations, DebugRamdiskVariation)
96 }
Colin Cross7228ecd2019-11-18 16:00:16 -080097 if m.RecoveryVariantNeeded(ctx) {
98 variations = append(variations, RecoveryVariation)
99 }
100
101 extraVariations := m.ExtraImageVariations(ctx)
102 variations = append(variations, extraVariations...)
103
104 if len(variations) == 0 {
105 return
106 }
107
108 mod := ctx.CreateVariations(variations...)
109 for i, v := range variations {
110 mod[i].base().setImageVariation(v)
111 m.SetImageVariation(ctx, v, mod[i])
112 }
113 }
114}