blob: c278dcdf9cb8c498a36288f696832541e9c8bbad [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
Jihoon Kang47e91842024-06-19 00:51:16 +000022 // VendorVariantNeeded should return true if the module needs a vendor variant (installed on the vendor image).
23 VendorVariantNeeded(ctx BaseModuleContext) bool
24
25 // ProductVariantNeeded should return true if the module needs a product variant (unstalled on the product image).
26 ProductVariantNeeded(ctx BaseModuleContext) bool
27
Colin Cross7228ecd2019-11-18 16:00:16 -080028 // CoreVariantNeeded should return true if the module needs a core variant (installed on the system image).
29 CoreVariantNeeded(ctx BaseModuleContext) bool
30
Yifan Hong1b3348d2020-01-21 15:53:22 -080031 // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the
32 // ramdisk partition).
33 RamdiskVariantNeeded(ctx BaseModuleContext) bool
34
Yifan Hong60e0cfb2020-10-21 15:17:56 -070035 // 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 Kim08758f02021-04-08 21:13:22 +090039 // 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 Cross7228ecd2019-11-18 16:00:16 -080043 // 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. Berki2f5c3402021-06-15 11:27:56 +020052 // SetImageVariation is called for each newly created image variant. The receiver is the original
Jihoon Kang7583e832024-06-13 21:25:45 +000053 // module, "variation" is the name of the newly created variant. "variation" is set on the receiver.
54 SetImageVariation(ctx BaseModuleContext, variation string)
Colin Cross7228ecd2019-11-18 16:00:16 -080055}
56
57const (
Jihoon Kang47e91842024-06-19 00:51:16 +000058 // 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 Cross7228ecd2019-11-18 16:00:16 -080066 // 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 Cross7113d202019-11-20 16:39:12 -080069 CoreVariation string = ""
Colin Cross7228ecd2019-11-18 16:00:16 -080070
71 // RecoveryVariation means a module to be installed to recovery image.
72 RecoveryVariation string = "recovery"
Yifan Hong1b3348d2020-01-21 15:53:22 -080073
74 // RamdiskVariation means a module to be installed to ramdisk image.
75 RamdiskVariation string = "ramdisk"
Yifan Hong60e0cfb2020-10-21 15:17:56 -070076
77 // VendorRamdiskVariation means a module to be installed to vendor ramdisk image.
78 VendorRamdiskVariation string = "vendor_ramdisk"
Inseob Kim08758f02021-04-08 21:13:22 +090079
80 // DebugRamdiskVariation means a module to be installed to debug ramdisk image.
81 DebugRamdiskVariation string = "debug_ramdisk"
Colin Cross7228ecd2019-11-18 16:00:16 -080082)
83
Colin Crossae6c5202019-11-20 13:35:50 -080084// imageMutator creates variants for modules that implement the ImageInterface that
Colin Cross7228ecd2019-11-18 16:00:16 -080085// allow them to build differently for each partition (recovery, core, vendor, etc.).
Colin Crossae6c5202019-11-20 13:35:50 -080086func imageMutator(ctx BottomUpMutatorContext) {
Colin Cross7228ecd2019-11-18 16:00:16 -080087 if ctx.Os() != Android {
88 return
89 }
90
91 if m, ok := ctx.Module().(ImageInterface); ok {
92 m.ImageMutatorBegin(ctx)
93
94 var variations []string
95
96 if m.CoreVariantNeeded(ctx) {
97 variations = append(variations, CoreVariation)
98 }
Yifan Hong1b3348d2020-01-21 15:53:22 -080099 if m.RamdiskVariantNeeded(ctx) {
100 variations = append(variations, RamdiskVariation)
101 }
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700102 if m.VendorRamdiskVariantNeeded(ctx) {
103 variations = append(variations, VendorRamdiskVariation)
104 }
Inseob Kim08758f02021-04-08 21:13:22 +0900105 if m.DebugRamdiskVariantNeeded(ctx) {
106 variations = append(variations, DebugRamdiskVariation)
107 }
Colin Cross7228ecd2019-11-18 16:00:16 -0800108 if m.RecoveryVariantNeeded(ctx) {
109 variations = append(variations, RecoveryVariation)
110 }
Jihoon Kang47e91842024-06-19 00:51:16 +0000111 if m.VendorVariantNeeded(ctx) {
112 variations = append(variations, VendorVariation)
113 }
114 if m.ProductVariantNeeded(ctx) {
115 variations = append(variations, ProductVariation)
116 }
Colin Cross7228ecd2019-11-18 16:00:16 -0800117
118 extraVariations := m.ExtraImageVariations(ctx)
119 variations = append(variations, extraVariations...)
120
121 if len(variations) == 0 {
122 return
123 }
124
125 mod := ctx.CreateVariations(variations...)
126 for i, v := range variations {
127 mod[i].base().setImageVariation(v)
Jihoon Kang7583e832024-06-13 21:25:45 +0000128 mod[i].(ImageInterface).SetImageVariation(ctx, v)
Colin Cross7228ecd2019-11-18 16:00:16 -0800129 }
130 }
131}