| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project | 
|  | 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 rust | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "strings" | 
|  | 19 |  | 
|  | 20 | "android/soong/android" | 
|  | 21 | "android/soong/cc" | 
|  | 22 | ) | 
|  | 23 |  | 
|  | 24 | var _ android.ImageInterface = (*Module)(nil) | 
|  | 25 |  | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 26 | var _ cc.ImageMutatableModule = (*Module)(nil) | 
|  | 27 |  | 
|  | 28 | func (mod *Module) VendorAvailable() bool { | 
|  | 29 | return Bool(mod.VendorProperties.Vendor_available) | 
|  | 30 | } | 
|  | 31 |  | 
|  | 32 | func (mod *Module) OdmAvailable() bool { | 
|  | 33 | return Bool(mod.VendorProperties.Odm_available) | 
|  | 34 | } | 
|  | 35 |  | 
|  | 36 | func (mod *Module) ProductAvailable() bool { | 
| Matthew Maurer | 52af5b0 | 2021-05-27 10:01:36 -0700 | [diff] [blame] | 37 | return Bool(mod.VendorProperties.Product_available) | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 38 | } | 
|  | 39 |  | 
|  | 40 | func (mod *Module) RamdiskAvailable() bool { | 
| Matthew Maurer | c686838 | 2021-07-13 14:12:37 -0700 | [diff] [blame] | 41 | return Bool(mod.Properties.Ramdisk_available) | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 42 | } | 
|  | 43 |  | 
|  | 44 | func (mod *Module) VendorRamdiskAvailable() bool { | 
|  | 45 | return Bool(mod.Properties.Vendor_ramdisk_available) | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | func (mod *Module) AndroidModuleBase() *android.ModuleBase { | 
|  | 49 | return &mod.ModuleBase | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | func (mod *Module) RecoveryAvailable() bool { | 
| Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 53 | return Bool(mod.Properties.Recovery_available) | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 54 | } | 
|  | 55 |  | 
|  | 56 | func (mod *Module) ExtraVariants() []string { | 
|  | 57 | return mod.Properties.ExtraVariants | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | func (mod *Module) AppendExtraVariant(extraVariant string) { | 
|  | 61 | mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, extraVariant) | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | func (mod *Module) SetRamdiskVariantNeeded(b bool) { | 
| Matthew Maurer | c686838 | 2021-07-13 14:12:37 -0700 | [diff] [blame] | 65 | mod.Properties.RamdiskVariantNeeded = b | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
|  | 68 | func (mod *Module) SetVendorRamdiskVariantNeeded(b bool) { | 
|  | 69 | mod.Properties.VendorRamdiskVariantNeeded = b | 
|  | 70 | } | 
|  | 71 |  | 
|  | 72 | func (mod *Module) SetRecoveryVariantNeeded(b bool) { | 
| Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 73 | mod.Properties.RecoveryVariantNeeded = b | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 74 | } | 
|  | 75 |  | 
|  | 76 | func (mod *Module) SetCoreVariantNeeded(b bool) { | 
|  | 77 | mod.Properties.CoreVariantNeeded = b | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string { | 
| Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 81 | if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok { | 
|  | 82 | return snapshot.Version() | 
|  | 83 | } else { | 
|  | 84 | mctx.ModuleErrorf("version is unknown for snapshot prebuilt") | 
|  | 85 | return "" | 
|  | 86 | } | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 89 | func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 90 | return mod.Properties.VendorRamdiskVariantNeeded | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 91 | } | 
|  | 92 |  | 
|  | 93 | func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 94 | return mod.Properties.CoreVariantNeeded | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool { | 
| Matthew Maurer | c686838 | 2021-07-13 14:12:37 -0700 | [diff] [blame] | 98 | return mod.Properties.RamdiskVariantNeeded | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 99 | } | 
|  | 100 |  | 
| Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 101 | func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { | 
|  | 102 | return false | 
|  | 103 | } | 
|  | 104 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 105 | func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool { | 
| Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 106 | return mod.Properties.RecoveryVariantNeeded | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
|  | 109 | func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string { | 
|  | 110 | return mod.Properties.ExtraVariants | 
|  | 111 | } | 
|  | 112 |  | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 113 | func (mod *Module) IsSnapshotPrebuilt() bool { | 
| Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 114 | if p, ok := mod.compiler.(cc.SnapshotInterface); ok { | 
|  | 115 | return p.IsSnapshotPrebuilt() | 
|  | 116 | } | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 117 | return false | 
|  | 118 | } | 
|  | 119 |  | 
| Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 120 | func (ctx *moduleContext) SocSpecific() bool { | 
|  | 121 | // Additionally check if this module is inVendor() that means it is a "vendor" variant of a | 
|  | 122 | // module. As well as SoC specific modules, vendor variants must be installed to /vendor | 
|  | 123 | // unless they have "odm_available: true". | 
|  | 124 | return ctx.ModuleContext.SocSpecific() || (ctx.RustModule().InVendor() && !ctx.RustModule().VendorVariantToOdm()) | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | func (ctx *moduleContext) DeviceSpecific() bool { | 
|  | 128 | // Some vendor variants want to be installed to /odm by setting "odm_available: true". | 
|  | 129 | return ctx.ModuleContext.DeviceSpecific() || (ctx.RustModule().InVendor() && ctx.RustModule().VendorVariantToOdm()) | 
|  | 130 | } | 
|  | 131 |  | 
| Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 132 | func (ctx *moduleContext) SystemExtSpecific() bool { | 
|  | 133 | return ctx.ModuleContext.SystemExtSpecific() | 
|  | 134 | } | 
|  | 135 |  | 
| Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 136 | // Returns true when this module creates a vendor variant and wants to install the vendor variant | 
|  | 137 | // to the odm partition. | 
|  | 138 | func (c *Module) VendorVariantToOdm() bool { | 
|  | 139 | return Bool(c.VendorProperties.Odm_available) | 
|  | 140 | } | 
|  | 141 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 142 | func (ctx *moduleContext) ProductSpecific() bool { | 
| Matthew Maurer | 9f59e8d | 2021-08-19 13:10:05 -0700 | [diff] [blame] | 143 | return ctx.ModuleContext.ProductSpecific() || ctx.RustModule().productSpecificModuleContext() | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | func (c *Module) productSpecificModuleContext() bool { | 
|  | 147 | // Additionally check if this module is inProduct() that means it is a "product" variant of a | 
|  | 148 | // module. As well as product specific modules, product variants must be installed to /product. | 
|  | 149 | return c.InProduct() | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
|  | 152 | func (mod *Module) InRecovery() bool { | 
| Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 153 | return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery() | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
| Jose Galmes | 61741e2 | 2022-02-06 22:06:23 -0800 | [diff] [blame] | 156 | func (mod *Module) InRamdisk() bool { | 
|  | 157 | return mod.ModuleBase.InRamdisk() || mod.ModuleBase.InstallInRamdisk() | 
|  | 158 | } | 
|  | 159 |  | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 160 | func (mod *Module) InVendorRamdisk() bool { | 
|  | 161 | return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk() | 
|  | 162 | } | 
|  | 163 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 164 | func (mod *Module) OnlyInRamdisk() bool { | 
| Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 165 | return mod.ModuleBase.InstallInRamdisk() | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 | func (mod *Module) OnlyInRecovery() bool { | 
| Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 169 | return mod.ModuleBase.InstallInRecovery() | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | func (mod *Module) OnlyInVendorRamdisk() bool { | 
| Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 173 | return mod.ModuleBase.InstallInVendorRamdisk() | 
| Matthew Maurer | 52af5b0 | 2021-05-27 10:01:36 -0700 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 176 | // Returns true when this module is configured to have core and vendor variants. | 
|  | 177 | func (mod *Module) HasVendorVariant() bool { | 
| Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 178 | return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available) | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 179 | } | 
|  | 180 |  | 
| Justin Yun | cbca373 | 2021-02-03 19:24:13 +0900 | [diff] [blame] | 181 | // Always returns false because rust modules do not support product variant. | 
|  | 182 | func (mod *Module) HasProductVariant() bool { | 
|  | 183 | return Bool(mod.VendorProperties.Product_available) | 
|  | 184 | } | 
|  | 185 |  | 
|  | 186 | func (mod *Module) HasNonSystemVariants() bool { | 
|  | 187 | return mod.HasVendorVariant() || mod.HasProductVariant() | 
|  | 188 | } | 
|  | 189 |  | 
| Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 190 | func (mod *Module) InProduct() bool { | 
| Matthew Maurer | 52af5b0 | 2021-05-27 10:01:36 -0700 | [diff] [blame] | 191 | return mod.Properties.ImageVariationPrefix == cc.ProductVariationPrefix | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 194 | // Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor | 
|  | 195 | func (mod *Module) InVendor() bool { | 
|  | 196 | return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix | 
|  | 197 | } | 
|  | 198 |  | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 199 | func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) { | 
|  | 200 | m := module.(*Module) | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 201 | if variant == android.VendorRamdiskVariation { | 
|  | 202 | m.MakeAsPlatform() | 
| Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 203 | } else if variant == android.RecoveryVariation { | 
|  | 204 | m.MakeAsPlatform() | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 205 | } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) { | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 206 | m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix | 
|  | 207 | m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix) | 
|  | 208 |  | 
|  | 209 | // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION. | 
|  | 210 | // Hide other vendor variants to avoid collision. | 
|  | 211 | vndkVersion := ctx.DeviceConfig().VndkVersion() | 
|  | 212 | if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion { | 
|  | 213 | m.Properties.HideFromMake = true | 
| Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 214 | m.HideFromMake() | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 215 | } | 
| Matthew Maurer | 52af5b0 | 2021-05-27 10:01:36 -0700 | [diff] [blame] | 216 | } else if strings.HasPrefix(variant, cc.ProductVariationPrefix) { | 
|  | 217 | m.Properties.ImageVariationPrefix = cc.ProductVariationPrefix | 
|  | 218 | m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.ProductVariationPrefix) | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 219 | } | 
|  | 220 | } | 
|  | 221 |  | 
|  | 222 | func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) { | 
| Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 223 | if Bool(mod.VendorProperties.Double_loadable) { | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 224 | mctx.PropertyErrorf("double_loadable", | 
|  | 225 | "Rust modules do not yet support double loading") | 
|  | 226 | } | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 227 | if Bool(mod.Properties.Vendor_ramdisk_available) { | 
|  | 228 | if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) { | 
|  | 229 | mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.") | 
| Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 230 | } | 
|  | 231 | } | 
|  | 232 |  | 
| Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 233 | cc.MutateImage(mctx, mod) | 
|  | 234 |  | 
|  | 235 | if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() { | 
|  | 236 |  | 
|  | 237 | if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok { | 
|  | 238 | // Rust does not support prebuilt libraries on non-System images. | 
|  | 239 | mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.") | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 240 | } | 
|  | 241 | } | 
| Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 242 | } |