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 | |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 80 | func (mod *Module) SetProductVariantNeeded(b bool) { |
| 81 | mod.Properties.ProductVariantNeeded = b |
| 82 | } |
| 83 | |
| 84 | func (mod *Module) SetVendorVariantNeeded(b bool) { |
| 85 | mod.Properties.VendorVariantNeeded = b |
| 86 | } |
| 87 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 88 | func (mod *Module) SnapshotVersion(mctx android.ImageInterfaceContext) string { |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 89 | if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok { |
| 90 | return snapshot.Version() |
| 91 | } else { |
| 92 | mctx.ModuleErrorf("version is unknown for snapshot prebuilt") |
| 93 | return "" |
| 94 | } |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 97 | func (mod *Module) VendorVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 98 | return mod.Properties.VendorVariantNeeded |
| 99 | } |
| 100 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 101 | func (mod *Module) ProductVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 102 | return mod.Properties.ProductVariantNeeded |
| 103 | } |
| 104 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 105 | func (mod *Module) VendorRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 106 | return mod.Properties.VendorRamdiskVariantNeeded |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 109 | func (mod *Module) CoreVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 110 | return mod.Properties.CoreVariantNeeded |
| 111 | } |
| 112 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 113 | func (mod *Module) RamdiskVariantNeeded(android.ImageInterfaceContext) bool { |
Matthew Maurer | c686838 | 2021-07-13 14:12:37 -0700 | [diff] [blame] | 114 | return mod.Properties.RamdiskVariantNeeded |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 115 | } |
| 116 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 117 | func (mod *Module) DebugRamdiskVariantNeeded(ctx android.ImageInterfaceContext) bool { |
Inseob Kim | 08758f0 | 2021-04-08 21:13:22 +0900 | [diff] [blame] | 118 | return false |
| 119 | } |
| 120 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 121 | func (mod *Module) RecoveryVariantNeeded(android.ImageInterfaceContext) bool { |
Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 122 | return mod.Properties.RecoveryVariantNeeded |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 123 | } |
| 124 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 125 | func (mod *Module) ExtraImageVariations(android.ImageInterfaceContext) []string { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 126 | return mod.Properties.ExtraVariants |
| 127 | } |
| 128 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 129 | func (mod *Module) IsSnapshotPrebuilt() bool { |
Ivan Lozano | 3149e6e | 2021-06-01 15:09:53 -0400 | [diff] [blame] | 130 | if p, ok := mod.compiler.(cc.SnapshotInterface); ok { |
| 131 | return p.IsSnapshotPrebuilt() |
| 132 | } |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 133 | return false |
| 134 | } |
| 135 | |
Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 136 | func (mod *Module) InstallInVendor() bool { |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 137 | // Additionally check if this module is inVendor() that means it is a "vendor" variant of a |
| 138 | // module. As well as SoC specific modules, vendor variants must be installed to /vendor |
| 139 | // unless they have "odm_available: true". |
Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 140 | return mod.InVendor() && !mod.VendorVariantToOdm() |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 143 | func (mod *Module) InstallInOdm() bool { |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 144 | // Some vendor variants want to be installed to /odm by setting "odm_available: true". |
Colin Cross | ea30d85 | 2023-11-29 16:00:16 -0800 | [diff] [blame] | 145 | return mod.InVendor() && mod.VendorVariantToOdm() |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 148 | // Returns true when this module creates a vendor variant and wants to install the vendor variant |
| 149 | // to the odm partition. |
| 150 | func (c *Module) VendorVariantToOdm() bool { |
| 151 | return Bool(c.VendorProperties.Odm_available) |
| 152 | } |
| 153 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 154 | func (ctx *moduleContext) ProductSpecific() bool { |
Matthew Maurer | 9f59e8d | 2021-08-19 13:10:05 -0700 | [diff] [blame] | 155 | return ctx.ModuleContext.ProductSpecific() || ctx.RustModule().productSpecificModuleContext() |
| 156 | } |
| 157 | |
| 158 | func (c *Module) productSpecificModuleContext() bool { |
| 159 | // Additionally check if this module is inProduct() that means it is a "product" variant of a |
| 160 | // module. As well as product specific modules, product variants must be installed to /product. |
| 161 | return c.InProduct() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | func (mod *Module) InRecovery() bool { |
Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 165 | return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jose Galmes | 61741e2 | 2022-02-06 22:06:23 -0800 | [diff] [blame] | 168 | func (mod *Module) InRamdisk() bool { |
| 169 | return mod.ModuleBase.InRamdisk() || mod.ModuleBase.InstallInRamdisk() |
| 170 | } |
| 171 | |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 172 | func (mod *Module) InVendorRamdisk() bool { |
| 173 | return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk() |
| 174 | } |
| 175 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 176 | func (mod *Module) OnlyInRamdisk() bool { |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 177 | return mod.ModuleBase.InstallInRamdisk() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | func (mod *Module) OnlyInRecovery() bool { |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 181 | return mod.ModuleBase.InstallInRecovery() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | func (mod *Module) OnlyInVendorRamdisk() bool { |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 185 | return mod.ModuleBase.InstallInVendorRamdisk() |
Matthew Maurer | 52af5b0 | 2021-05-27 10:01:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 188 | // Returns true when this module is configured to have core and vendor variants. |
| 189 | func (mod *Module) HasVendorVariant() bool { |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 190 | return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available) |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Justin Yun | cbca373 | 2021-02-03 19:24:13 +0900 | [diff] [blame] | 193 | // Always returns false because rust modules do not support product variant. |
| 194 | func (mod *Module) HasProductVariant() bool { |
| 195 | return Bool(mod.VendorProperties.Product_available) |
| 196 | } |
| 197 | |
| 198 | func (mod *Module) HasNonSystemVariants() bool { |
| 199 | return mod.HasVendorVariant() || mod.HasProductVariant() |
| 200 | } |
| 201 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame] | 202 | func (mod *Module) InProduct() bool { |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 203 | return mod.Properties.ImageVariation == android.ProductVariation |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 204 | } |
| 205 | |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 206 | // Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor |
| 207 | func (mod *Module) InVendor() bool { |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 208 | return mod.Properties.ImageVariation == android.VendorVariation |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Kiyoung Kim | aa39480 | 2024-01-08 12:55:45 +0900 | [diff] [blame] | 211 | // Returns true if the module is "vendor" or "product" variant. |
| 212 | func (mod *Module) InVendorOrProduct() bool { |
| 213 | return mod.InVendor() || mod.InProduct() |
| 214 | } |
| 215 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 216 | func (mod *Module) SetImageVariation(ctx android.ImageInterfaceContext, variant string) { |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 217 | if variant == android.VendorRamdiskVariation { |
Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 218 | mod.MakeAsPlatform() |
Matthew Maurer | 460ee94 | 2021-02-11 12:31:46 -0800 | [diff] [blame] | 219 | } else if variant == android.RecoveryVariation { |
Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 220 | mod.MakeAsPlatform() |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 221 | } else if strings.HasPrefix(variant, android.VendorVariation) { |
| 222 | mod.Properties.ImageVariation = android.VendorVariation |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 223 | if strings.HasPrefix(variant, cc.VendorVariationPrefix) { |
Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 224 | mod.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix) |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 225 | } |
Jihoon Kang | 47e9184 | 2024-06-19 00:51:16 +0000 | [diff] [blame] | 226 | } else if strings.HasPrefix(variant, android.ProductVariation) { |
| 227 | mod.Properties.ImageVariation = android.ProductVariation |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 228 | if strings.HasPrefix(variant, cc.ProductVariationPrefix) { |
Jihoon Kang | 7583e83 | 2024-06-13 21:25:45 +0000 | [diff] [blame] | 229 | mod.Properties.VndkVersion = strings.TrimPrefix(variant, cc.ProductVariationPrefix) |
Kiyoung Kim | b5fdb2e | 2024-01-03 14:24:34 +0900 | [diff] [blame] | 230 | } |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Cole Faust | fa6e0fd | 2024-10-15 15:22:57 -0700 | [diff] [blame^] | 234 | func (mod *Module) ImageMutatorBegin(mctx android.ImageInterfaceContext) { |
Matthew Maurer | 993db7a | 2023-01-24 16:36:02 -0800 | [diff] [blame] | 235 | if Bool(mod.VendorProperties.Double_loadable) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 236 | mctx.PropertyErrorf("double_loadable", |
| 237 | "Rust modules do not yet support double loading") |
| 238 | } |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 239 | if Bool(mod.Properties.Vendor_ramdisk_available) { |
| 240 | if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) { |
| 241 | 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] | 242 | } |
| 243 | } |
| 244 | |
Ivan Lozano | c08897c | 2021-04-02 12:41:32 -0400 | [diff] [blame] | 245 | cc.MutateImage(mctx, mod) |
| 246 | |
| 247 | if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() { |
| 248 | |
| 249 | if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok { |
| 250 | // Rust does not support prebuilt libraries on non-System images. |
| 251 | mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.") |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 252 | } |
| 253 | } |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 254 | } |