Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [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 | package cc |
| 15 | |
| 16 | // This file contains image variant related things, including image mutator functions, utility |
| 17 | // functions to determine where a module is installed, etc. |
| 18 | |
| 19 | import ( |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 20 | "fmt" |
| 21 | "reflect" |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 22 | "strings" |
| 23 | |
| 24 | "android/soong/android" |
| 25 | ) |
| 26 | |
| 27 | var _ android.ImageInterface = (*Module)(nil) |
| 28 | |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 29 | type ImageVariantType string |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 30 | |
| 31 | const ( |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 32 | coreImageVariant ImageVariantType = "core" |
| 33 | vendorImageVariant ImageVariantType = "vendor" |
| 34 | productImageVariant ImageVariantType = "product" |
| 35 | ramdiskImageVariant ImageVariantType = "ramdisk" |
| 36 | vendorRamdiskImageVariant ImageVariantType = "vendor_ramdisk" |
| 37 | recoveryImageVariant ImageVariantType = "recovery" |
| 38 | hostImageVariant ImageVariantType = "host" |
Inseob Kim | 74d2556 | 2020-08-04 00:41:38 +0900 | [diff] [blame] | 39 | ) |
| 40 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 41 | const ( |
| 42 | // VendorVariationPrefix is the variant prefix used for /vendor code that compiles |
| 43 | // against the VNDK. |
| 44 | VendorVariationPrefix = "vendor." |
| 45 | |
| 46 | // ProductVariationPrefix is the variant prefix used for /product code that compiles |
| 47 | // against the VNDK. |
| 48 | ProductVariationPrefix = "product." |
| 49 | ) |
| 50 | |
| 51 | func (ctx *moduleContext) ProductSpecific() bool { |
Justin Yun | 7f99ec7 | 2021-04-12 13:19:28 +0900 | [diff] [blame] | 52 | return ctx.ModuleContext.ProductSpecific() || ctx.mod.productSpecificModuleContext() |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | func (ctx *moduleContext) SocSpecific() bool { |
Justin Yun | 7f99ec7 | 2021-04-12 13:19:28 +0900 | [diff] [blame] | 56 | return ctx.ModuleContext.SocSpecific() || ctx.mod.socSpecificModuleContext() |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | func (ctx *moduleContext) DeviceSpecific() bool { |
Justin Yun | 7f99ec7 | 2021-04-12 13:19:28 +0900 | [diff] [blame] | 60 | return ctx.ModuleContext.DeviceSpecific() || ctx.mod.deviceSpecificModuleContext() |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func (ctx *moduleContextImpl) inProduct() bool { |
Ivan Lozano | f9e2172 | 2020-12-02 09:00:51 -0500 | [diff] [blame] | 64 | return ctx.mod.InProduct() |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | func (ctx *moduleContextImpl) inVendor() bool { |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 68 | return ctx.mod.InVendor() |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | func (ctx *moduleContextImpl) inRamdisk() bool { |
| 72 | return ctx.mod.InRamdisk() |
| 73 | } |
| 74 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 75 | func (ctx *moduleContextImpl) inVendorRamdisk() bool { |
| 76 | return ctx.mod.InVendorRamdisk() |
| 77 | } |
| 78 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 79 | func (ctx *moduleContextImpl) inRecovery() bool { |
| 80 | return ctx.mod.InRecovery() |
| 81 | } |
| 82 | |
Justin Yun | 7f99ec7 | 2021-04-12 13:19:28 +0900 | [diff] [blame] | 83 | func (c *Module) productSpecificModuleContext() bool { |
| 84 | // Additionally check if this module is inProduct() that means it is a "product" variant of a |
| 85 | // module. As well as product specific modules, product variants must be installed to /product. |
| 86 | return c.InProduct() |
| 87 | } |
| 88 | |
| 89 | func (c *Module) socSpecificModuleContext() bool { |
| 90 | // Additionally check if this module is inVendor() that means it is a "vendor" variant of a |
| 91 | // module. As well as SoC specific modules, vendor variants must be installed to /vendor |
| 92 | // unless they have "odm_available: true". |
| 93 | return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm() |
| 94 | } |
| 95 | |
| 96 | func (c *Module) deviceSpecificModuleContext() bool { |
| 97 | // Some vendor variants want to be installed to /odm by setting "odm_available: true". |
| 98 | return c.InVendor() && c.VendorVariantToOdm() |
| 99 | } |
| 100 | |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 101 | // Returns true when this module is configured to have core and vendor variants. |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 102 | func (c *Module) HasVendorVariant() bool { |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 103 | return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available) |
| 104 | } |
| 105 | |
| 106 | // Returns true when this module creates a vendor variant and wants to install the vendor variant |
| 107 | // to the odm partition. |
| 108 | func (c *Module) VendorVariantToOdm() bool { |
| 109 | return Bool(c.VendorProperties.Odm_available) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 110 | } |
| 111 | |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 112 | // Returns true when this module is configured to have core and product variants. |
| 113 | func (c *Module) HasProductVariant() bool { |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 114 | return Bool(c.VendorProperties.Product_available) |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Returns true when this module is configured to have core and either product or vendor variants. |
| 118 | func (c *Module) HasNonSystemVariants() bool { |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 119 | return c.HasVendorVariant() || c.HasProductVariant() |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 120 | } |
| 121 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 122 | // Returns true if the module is "product" variant. Usually these modules are installed in /product |
Ivan Lozano | f9e2172 | 2020-12-02 09:00:51 -0500 | [diff] [blame] | 123 | func (c *Module) InProduct() bool { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 124 | return c.Properties.ImageVariationPrefix == ProductVariationPrefix |
| 125 | } |
| 126 | |
| 127 | // Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor |
Ivan Lozano | 3968d8f | 2020-12-14 11:27:52 -0500 | [diff] [blame] | 128 | func (c *Module) InVendor() bool { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 129 | return c.Properties.ImageVariationPrefix == VendorVariationPrefix |
| 130 | } |
| 131 | |
| 132 | func (c *Module) InRamdisk() bool { |
| 133 | return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk() |
| 134 | } |
| 135 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 136 | func (c *Module) InVendorRamdisk() bool { |
| 137 | return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk() |
| 138 | } |
| 139 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 140 | func (c *Module) InRecovery() bool { |
| 141 | return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery() |
| 142 | } |
| 143 | |
| 144 | func (c *Module) OnlyInRamdisk() bool { |
| 145 | return c.ModuleBase.InstallInRamdisk() |
| 146 | } |
| 147 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 148 | func (c *Module) OnlyInVendorRamdisk() bool { |
| 149 | return c.ModuleBase.InstallInVendorRamdisk() |
| 150 | } |
| 151 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 152 | func (c *Module) OnlyInRecovery() bool { |
| 153 | return c.ModuleBase.InstallInRecovery() |
| 154 | } |
| 155 | |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 156 | func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool { |
| 157 | if v.Kind() != reflect.Struct { |
| 158 | return true |
| 159 | } |
| 160 | for i := 0; i < v.NumField(); i++ { |
| 161 | prop := v.Field(i) |
| 162 | if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" { |
| 163 | vendor_prop := prop.FieldByName("Vendor") |
| 164 | product_prop := prop.FieldByName("Product") |
| 165 | if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct { |
| 166 | // Neither Target.Vendor nor Target.Product is defined |
| 167 | continue |
| 168 | } |
| 169 | if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct || |
| 170 | !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) { |
| 171 | // If only one of either Target.Vendor or Target.Product is |
| 172 | // defined or they have different values, it fails the build |
| 173 | // since VNDK must have the same properties for both vendor |
| 174 | // and product variants. |
| 175 | return false |
| 176 | } |
| 177 | } else if !visitPropsAndCompareVendorAndProductProps(prop) { |
| 178 | // Visit the substructures to find Target.Vendor and Target.Product |
| 179 | return false |
| 180 | } |
| 181 | } |
| 182 | return true |
| 183 | } |
| 184 | |
| 185 | // In the case of VNDK, vendor and product variants must have the same properties. |
| 186 | // VNDK installs only one file and shares it for both vendor and product modules on |
| 187 | // runtime. We may not define different versions of a VNDK lib for each partition. |
| 188 | // This function is used only for the VNDK modules that is available to both vendor |
| 189 | // and product partitions. |
| 190 | func (c *Module) compareVendorAndProductProps() bool { |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 191 | if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) { |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 192 | panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name())) |
| 193 | } |
| 194 | for _, properties := range c.GetProperties() { |
| 195 | if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) { |
| 196 | return false |
| 197 | } |
| 198 | } |
| 199 | return true |
| 200 | } |
| 201 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 202 | // ImageMutatableModule provides a common image mutation interface for LinkableInterface modules. |
| 203 | type ImageMutatableModule interface { |
| 204 | android.Module |
| 205 | LinkableInterface |
| 206 | |
| 207 | // AndroidModuleBase returns the android.ModuleBase for this module |
| 208 | AndroidModuleBase() *android.ModuleBase |
| 209 | |
| 210 | // VendorAvailable returns true if this module is available on the vendor image. |
| 211 | VendorAvailable() bool |
| 212 | |
| 213 | // OdmAvailable returns true if this module is available on the odm image. |
| 214 | OdmAvailable() bool |
| 215 | |
| 216 | // ProductAvailable returns true if this module is available on the product image. |
| 217 | ProductAvailable() bool |
| 218 | |
| 219 | // RamdiskAvailable returns true if this module is available on the ramdisk image. |
| 220 | RamdiskAvailable() bool |
| 221 | |
| 222 | // RecoveryAvailable returns true if this module is available on the recovery image. |
| 223 | RecoveryAvailable() bool |
| 224 | |
| 225 | // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image. |
| 226 | VendorRamdiskAvailable() bool |
| 227 | |
| 228 | // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt. |
| 229 | IsSnapshotPrebuilt() bool |
| 230 | |
| 231 | // SnapshotVersion returns the snapshot version for this module. |
| 232 | SnapshotVersion(mctx android.BaseModuleContext) string |
| 233 | |
| 234 | // SdkVersion returns the SDK version for this module. |
| 235 | SdkVersion() string |
| 236 | |
| 237 | // ExtraVariants returns the list of extra variants this module requires. |
| 238 | ExtraVariants() []string |
| 239 | |
| 240 | // AppendExtraVariant returns an extra variant to the list of extra variants this module requires. |
| 241 | AppendExtraVariant(extraVariant string) |
| 242 | |
| 243 | // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed. |
| 244 | SetRamdiskVariantNeeded(b bool) |
| 245 | |
| 246 | // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed. |
| 247 | SetVendorRamdiskVariantNeeded(b bool) |
| 248 | |
| 249 | // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed. |
| 250 | SetRecoveryVariantNeeded(b bool) |
| 251 | |
| 252 | // SetCoreVariantNeeded sets whether the Core Variant is needed. |
| 253 | SetCoreVariantNeeded(b bool) |
| 254 | } |
| 255 | |
| 256 | var _ ImageMutatableModule = (*Module)(nil) |
| 257 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 258 | func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) { |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 259 | m.CheckVndkProperties(mctx) |
| 260 | MutateImage(mctx, m) |
| 261 | } |
| 262 | |
| 263 | // CheckVndkProperties checks whether the VNDK-related properties are set correctly. |
| 264 | // If properties are not set correctly, results in a module context property error. |
| 265 | func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 266 | vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific() |
| 267 | productSpecific := mctx.ProductSpecific() |
| 268 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 269 | if vndkdep := m.vndkdep; vndkdep != nil { |
| 270 | if vndkdep.isVndk() { |
| 271 | if vendorSpecific || productSpecific { |
| 272 | if !vndkdep.isVndkExt() { |
| 273 | mctx.PropertyErrorf("vndk", |
| 274 | "must set `extends: \"...\"` to vndk extension") |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 275 | } else if Bool(m.VendorProperties.Vendor_available) { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 276 | mctx.PropertyErrorf("vendor_available", |
| 277 | "must not set at the same time as `vndk: {extends: \"...\"}`") |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 278 | } else if Bool(m.VendorProperties.Product_available) { |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 279 | mctx.PropertyErrorf("product_available", |
| 280 | "must not set at the same time as `vndk: {extends: \"...\"}`") |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 281 | } |
| 282 | } else { |
| 283 | if vndkdep.isVndkExt() { |
| 284 | mctx.PropertyErrorf("vndk", |
| 285 | "must set `vendor: true` or `product_specific: true` to set `extends: %q`", |
| 286 | m.getVndkExtendsModuleName()) |
| 287 | } |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 288 | if !Bool(m.VendorProperties.Vendor_available) { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 289 | mctx.PropertyErrorf("vndk", |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 290 | "vendor_available must be set to true when `vndk: {enabled: true}`") |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 291 | } |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 292 | if Bool(m.VendorProperties.Product_available) { |
Justin Yun | fd9e804 | 2020-12-23 18:23:14 +0900 | [diff] [blame] | 293 | // If a VNDK module creates both product and vendor variants, they |
| 294 | // must have the same properties since they share a single VNDK |
| 295 | // library on runtime. |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 296 | if !m.compareVendorAndProductProps() { |
| 297 | mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules") |
| 298 | } |
| 299 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 300 | } |
| 301 | } else { |
| 302 | if vndkdep.isVndkSp() { |
| 303 | mctx.PropertyErrorf("vndk", |
| 304 | "must set `enabled: true` to set `support_system_process: true`") |
| 305 | } |
| 306 | if vndkdep.isVndkExt() { |
| 307 | mctx.PropertyErrorf("vndk", |
| 308 | "must set `enabled: true` to set `extends: %q`", |
| 309 | m.getVndkExtendsModuleName()) |
| 310 | } |
| 311 | } |
| 312 | } |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | func (m *Module) VendorAvailable() bool { |
| 316 | return Bool(m.VendorProperties.Vendor_available) |
| 317 | } |
| 318 | |
| 319 | func (m *Module) OdmAvailable() bool { |
| 320 | return Bool(m.VendorProperties.Odm_available) |
| 321 | } |
| 322 | |
| 323 | func (m *Module) ProductAvailable() bool { |
| 324 | return Bool(m.VendorProperties.Product_available) |
| 325 | } |
| 326 | |
| 327 | func (m *Module) RamdiskAvailable() bool { |
| 328 | return Bool(m.Properties.Ramdisk_available) |
| 329 | } |
| 330 | |
| 331 | func (m *Module) VendorRamdiskAvailable() bool { |
| 332 | return Bool(m.Properties.Vendor_ramdisk_available) |
| 333 | } |
| 334 | |
| 335 | func (m *Module) AndroidModuleBase() *android.ModuleBase { |
| 336 | return &m.ModuleBase |
| 337 | } |
| 338 | |
| 339 | func (m *Module) RecoveryAvailable() bool { |
| 340 | return Bool(m.Properties.Recovery_available) |
| 341 | } |
| 342 | |
| 343 | func (m *Module) ExtraVariants() []string { |
| 344 | return m.Properties.ExtraVariants |
| 345 | } |
| 346 | |
| 347 | func (m *Module) AppendExtraVariant(extraVariant string) { |
| 348 | m.Properties.ExtraVariants = append(m.Properties.ExtraVariants, extraVariant) |
| 349 | } |
| 350 | |
| 351 | func (m *Module) SetRamdiskVariantNeeded(b bool) { |
| 352 | m.Properties.RamdiskVariantNeeded = b |
| 353 | } |
| 354 | |
| 355 | func (m *Module) SetVendorRamdiskVariantNeeded(b bool) { |
| 356 | m.Properties.VendorRamdiskVariantNeeded = b |
| 357 | } |
| 358 | |
| 359 | func (m *Module) SetRecoveryVariantNeeded(b bool) { |
| 360 | m.Properties.RecoveryVariantNeeded = b |
| 361 | } |
| 362 | |
| 363 | func (m *Module) SetCoreVariantNeeded(b bool) { |
| 364 | m.Properties.CoreVariantNeeded = b |
| 365 | } |
| 366 | |
| 367 | func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string { |
| 368 | if snapshot, ok := m.linker.(snapshotInterface); ok { |
| 369 | return snapshot.version() |
| 370 | } else { |
| 371 | mctx.ModuleErrorf("version is unknown for snapshot prebuilt") |
| 372 | // Should we be panicking here instead? |
| 373 | return "" |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | func (m *Module) KernelHeadersDecorator() bool { |
| 378 | if _, ok := m.linker.(*kernelHeadersDecorator); ok { |
| 379 | return true |
| 380 | } |
| 381 | return false |
| 382 | } |
| 383 | |
| 384 | // MutateImage handles common image mutations for ImageMutatableModule interfaces. |
| 385 | func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) { |
| 386 | // Validation check |
| 387 | vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific() |
| 388 | productSpecific := mctx.ProductSpecific() |
| 389 | |
| 390 | if m.VendorAvailable() { |
| 391 | if vendorSpecific { |
| 392 | mctx.PropertyErrorf("vendor_available", |
| 393 | "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`") |
| 394 | } |
| 395 | if m.OdmAvailable() { |
| 396 | mctx.PropertyErrorf("vendor_available", |
| 397 | "doesn't make sense at the same time as `odm_available: true`") |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if m.OdmAvailable() { |
| 402 | if vendorSpecific { |
| 403 | mctx.PropertyErrorf("odm_available", |
| 404 | "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`") |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | if m.ProductAvailable() { |
| 409 | if productSpecific { |
| 410 | mctx.PropertyErrorf("product_available", |
| 411 | "doesn't make sense at the same time as `product_specific: true`") |
| 412 | } |
| 413 | if vendorSpecific { |
| 414 | mctx.PropertyErrorf("product_available", |
| 415 | "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`") |
| 416 | } |
| 417 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 418 | |
| 419 | var coreVariantNeeded bool = false |
| 420 | var ramdiskVariantNeeded bool = false |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 421 | var vendorRamdiskVariantNeeded bool = false |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 422 | var recoveryVariantNeeded bool = false |
| 423 | |
| 424 | var vendorVariants []string |
| 425 | var productVariants []string |
| 426 | |
| 427 | platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion() |
| 428 | boardVndkVersion := mctx.DeviceConfig().VndkVersion() |
| 429 | productVndkVersion := mctx.DeviceConfig().ProductVndkVersion() |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 430 | recoverySnapshotVersion := mctx.DeviceConfig().RecoverySnapshotVersion() |
| 431 | usingRecoverySnapshot := recoverySnapshotVersion != "current" && |
| 432 | recoverySnapshotVersion != "" |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 433 | if boardVndkVersion == "current" { |
| 434 | boardVndkVersion = platformVndkVersion |
| 435 | } |
| 436 | if productVndkVersion == "current" { |
| 437 | productVndkVersion = platformVndkVersion |
| 438 | } |
| 439 | |
Colin Cross | 203b421 | 2021-04-26 17:19:41 -0700 | [diff] [blame] | 440 | if m.NeedsLlndkVariants() { |
Colin Cross | b5f6fa6 | 2021-01-06 17:05:04 -0800 | [diff] [blame] | 441 | // This is an LLNDK library. The implementation of the library will be on /system, |
| 442 | // and vendor and product variants will be created with LLNDK stubs. |
| 443 | // The LLNDK libraries need vendor variants even if there is no VNDK. |
Colin Cross | 203b421 | 2021-04-26 17:19:41 -0700 | [diff] [blame] | 444 | coreVariantNeeded = true |
Colin Cross | b5f6fa6 | 2021-01-06 17:05:04 -0800 | [diff] [blame] | 445 | if platformVndkVersion != "" { |
| 446 | vendorVariants = append(vendorVariants, platformVndkVersion) |
| 447 | productVariants = append(productVariants, platformVndkVersion) |
| 448 | } |
| 449 | if boardVndkVersion != "" { |
| 450 | vendorVariants = append(vendorVariants, boardVndkVersion) |
| 451 | } |
| 452 | if productVndkVersion != "" { |
| 453 | productVariants = append(productVariants, productVndkVersion) |
| 454 | } |
Colin Cross | 5271fea | 2021-04-27 13:06:04 -0700 | [diff] [blame^] | 455 | } else if m.NeedsVendorPublicLibraryVariants() { |
| 456 | // A vendor public library has the implementation on /vendor, with stub variants |
| 457 | // for system and product. |
| 458 | coreVariantNeeded = true |
| 459 | vendorVariants = append(vendorVariants, boardVndkVersion) |
| 460 | if platformVndkVersion != "" { |
| 461 | productVariants = append(productVariants, platformVndkVersion) |
| 462 | } |
| 463 | if productVndkVersion != "" { |
| 464 | productVariants = append(productVariants, productVndkVersion) |
| 465 | } |
Colin Cross | b5f6fa6 | 2021-01-06 17:05:04 -0800 | [diff] [blame] | 466 | } else if boardVndkVersion == "" { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 467 | // If the device isn't compiling against the VNDK, we always |
| 468 | // use the core mode. |
| 469 | coreVariantNeeded = true |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 470 | } else if m.IsSnapshotPrebuilt() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 471 | // Make vendor variants only for the versions in BOARD_VNDK_VERSION and |
| 472 | // PRODUCT_EXTRA_VNDK_VERSIONS. |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 473 | if m.InstallInRecovery() { |
| 474 | recoveryVariantNeeded = true |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 475 | } else { |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 476 | vendorVariants = append(vendorVariants, m.SnapshotVersion(mctx)) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 477 | } |
Ivan Lozano | f9e2172 | 2020-12-02 09:00:51 -0500 | [diff] [blame] | 478 | } else if m.HasNonSystemVariants() && !m.IsVndkExt() { |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 479 | // This will be available to /system unless it is product_specific |
| 480 | // which will be handled later. |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 481 | coreVariantNeeded = true |
| 482 | |
| 483 | // We assume that modules under proprietary paths are compatible for |
| 484 | // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or |
| 485 | // PLATFORM_VNDK_VERSION. |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 486 | if m.HasVendorVariant() { |
| 487 | if isVendorProprietaryModule(mctx) { |
| 488 | vendorVariants = append(vendorVariants, boardVndkVersion) |
| 489 | } else { |
| 490 | vendorVariants = append(vendorVariants, platformVndkVersion) |
| 491 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 492 | } |
| 493 | |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 494 | // product_available modules are available to /product. |
| 495 | if m.HasProductVariant() { |
| 496 | productVariants = append(productVariants, platformVndkVersion) |
| 497 | // VNDK is always PLATFORM_VNDK_VERSION |
| 498 | if !m.IsVndk() { |
| 499 | productVariants = append(productVariants, productVndkVersion) |
| 500 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 501 | } |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 502 | } else if vendorSpecific && m.SdkVersion() == "" { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 503 | // This will be available in /vendor (or /odm) only |
| 504 | |
| 505 | // kernel_headers is a special module type whose exported headers |
| 506 | // are coming from DeviceKernelHeaders() which is always vendor |
| 507 | // dependent. They'll always have both vendor variants. |
| 508 | // For other modules, we assume that modules under proprietary |
| 509 | // paths are compatible for BOARD_VNDK_VERSION. The other modules |
| 510 | // are regarded as AOSP, which is PLATFORM_VNDK_VERSION. |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 511 | if m.KernelHeadersDecorator() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 512 | vendorVariants = append(vendorVariants, |
| 513 | platformVndkVersion, |
| 514 | boardVndkVersion, |
| 515 | ) |
Bill Peckham | 945441c | 2020-08-31 16:07:58 -0700 | [diff] [blame] | 516 | } else if isVendorProprietaryModule(mctx) { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 517 | vendorVariants = append(vendorVariants, boardVndkVersion) |
| 518 | } else { |
| 519 | vendorVariants = append(vendorVariants, platformVndkVersion) |
| 520 | } |
| 521 | } else { |
| 522 | // This is either in /system (or similar: /data), or is a |
| 523 | // modules built with the NDK. Modules built with the NDK |
| 524 | // will be restricted using the existing link type checks. |
| 525 | coreVariantNeeded = true |
| 526 | } |
| 527 | |
| 528 | if boardVndkVersion != "" && productVndkVersion != "" { |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 529 | if coreVariantNeeded && productSpecific && m.SdkVersion() == "" { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 530 | // The module has "product_specific: true" that does not create core variant. |
| 531 | coreVariantNeeded = false |
| 532 | productVariants = append(productVariants, productVndkVersion) |
| 533 | } |
| 534 | } else { |
| 535 | // Unless PRODUCT_PRODUCT_VNDK_VERSION is set, product partition has no |
| 536 | // restriction to use system libs. |
| 537 | // No product variants defined in this case. |
| 538 | productVariants = []string{} |
| 539 | } |
| 540 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 541 | if m.RamdiskAvailable() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 542 | ramdiskVariantNeeded = true |
| 543 | } |
| 544 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 545 | if m.AndroidModuleBase().InstallInRamdisk() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 546 | ramdiskVariantNeeded = true |
| 547 | coreVariantNeeded = false |
| 548 | } |
| 549 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 550 | if m.VendorRamdiskAvailable() { |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 551 | vendorRamdiskVariantNeeded = true |
| 552 | } |
| 553 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 554 | if m.AndroidModuleBase().InstallInVendorRamdisk() { |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 555 | vendorRamdiskVariantNeeded = true |
| 556 | coreVariantNeeded = false |
| 557 | } |
| 558 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 559 | if m.RecoveryAvailable() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 560 | recoveryVariantNeeded = true |
| 561 | } |
| 562 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 563 | if m.AndroidModuleBase().InstallInRecovery() { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 564 | recoveryVariantNeeded = true |
| 565 | coreVariantNeeded = false |
| 566 | } |
| 567 | |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 568 | // If using a snapshot, the recovery variant under AOSP directories is not needed, |
| 569 | // except for kernel headers, which needs all variants. |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 570 | if m.KernelHeadersDecorator() && |
| 571 | !m.IsSnapshotPrebuilt() && |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 572 | usingRecoverySnapshot && |
| 573 | !isRecoveryProprietaryModule(mctx) { |
| 574 | recoveryVariantNeeded = false |
| 575 | } |
| 576 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 577 | for _, variant := range android.FirstUniqueStrings(vendorVariants) { |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 578 | m.AppendExtraVariant(VendorVariationPrefix + variant) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | for _, variant := range android.FirstUniqueStrings(productVariants) { |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 582 | m.AppendExtraVariant(ProductVariationPrefix + variant) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 583 | } |
| 584 | |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 585 | m.SetRamdiskVariantNeeded(ramdiskVariantNeeded) |
| 586 | m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded) |
| 587 | m.SetRecoveryVariantNeeded(recoveryVariantNeeded) |
| 588 | m.SetCoreVariantNeeded(coreVariantNeeded) |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 589 | |
| 590 | // Disable the module if no variants are needed. |
| 591 | if !ramdiskVariantNeeded && |
| 592 | !recoveryVariantNeeded && |
| 593 | !coreVariantNeeded && |
Ivan Lozano | 3a7d000 | 2021-03-30 12:19:36 -0400 | [diff] [blame] | 594 | len(m.ExtraVariants()) == 0 { |
Jose Galmes | 6f843bc | 2020-12-11 13:36:29 -0800 | [diff] [blame] | 595 | m.Disable() |
| 596 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 600 | return c.Properties.CoreVariantNeeded |
| 601 | } |
| 602 | |
| 603 | func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 604 | return c.Properties.RamdiskVariantNeeded |
| 605 | } |
| 606 | |
Yifan Hong | 60e0cfb | 2020-10-21 15:17:56 -0700 | [diff] [blame] | 607 | func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 608 | return c.Properties.VendorRamdiskVariantNeeded |
| 609 | } |
| 610 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 611 | func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 612 | return c.Properties.RecoveryVariantNeeded |
| 613 | } |
| 614 | |
| 615 | func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 616 | return c.Properties.ExtraVariants |
| 617 | } |
| 618 | |
Justin Yun | 63e9ec7 | 2020-10-29 16:49:43 +0900 | [diff] [blame] | 619 | func squashVendorSrcs(m *Module) { |
| 620 | if lib, ok := m.compiler.(*libraryDecorator); ok { |
| 621 | lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs, |
| 622 | lib.baseCompiler.Properties.Target.Vendor.Srcs...) |
| 623 | |
| 624 | lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, |
| 625 | lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...) |
| 626 | |
| 627 | lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources, |
| 628 | lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...) |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | func squashProductSrcs(m *Module) { |
| 633 | if lib, ok := m.compiler.(*libraryDecorator); ok { |
| 634 | lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs, |
| 635 | lib.baseCompiler.Properties.Target.Product.Srcs...) |
| 636 | |
| 637 | lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, |
| 638 | lib.baseCompiler.Properties.Target.Product.Exclude_srcs...) |
| 639 | |
| 640 | lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources, |
| 641 | lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | func squashRecoverySrcs(m *Module) { |
| 646 | if lib, ok := m.compiler.(*libraryDecorator); ok { |
| 647 | lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs, |
| 648 | lib.baseCompiler.Properties.Target.Recovery.Srcs...) |
| 649 | |
| 650 | lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, |
| 651 | lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...) |
| 652 | |
| 653 | lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources, |
| 654 | lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...) |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | func squashVendorRamdiskSrcs(m *Module) { |
| 659 | if lib, ok := m.compiler.(*libraryDecorator); ok { |
| 660 | lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...) |
| 661 | } |
| 662 | } |
| 663 | |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 664 | func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) { |
| 665 | m := module.(*Module) |
Yifan Hong | 6da33c2 | 2020-10-27 15:01:21 -0700 | [diff] [blame] | 666 | if variant == android.RamdiskVariation { |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 667 | m.MakeAsPlatform() |
Yifan Hong | 6da33c2 | 2020-10-27 15:01:21 -0700 | [diff] [blame] | 668 | } else if variant == android.VendorRamdiskVariation { |
| 669 | m.MakeAsPlatform() |
| 670 | squashVendorRamdiskSrcs(m) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 671 | } else if variant == android.RecoveryVariation { |
| 672 | m.MakeAsPlatform() |
| 673 | squashRecoverySrcs(m) |
| 674 | } else if strings.HasPrefix(variant, VendorVariationPrefix) { |
| 675 | m.Properties.ImageVariationPrefix = VendorVariationPrefix |
| 676 | m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix) |
| 677 | squashVendorSrcs(m) |
| 678 | |
| 679 | // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION. |
| 680 | // Hide other vendor variants to avoid collision. |
| 681 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 682 | if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion { |
| 683 | m.Properties.HideFromMake = true |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 684 | m.HideFromMake() |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 685 | } |
| 686 | } else if strings.HasPrefix(variant, ProductVariationPrefix) { |
| 687 | m.Properties.ImageVariationPrefix = ProductVariationPrefix |
| 688 | m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix) |
Justin Yun | 6977e8a | 2020-10-29 18:24:11 +0900 | [diff] [blame] | 689 | squashProductSrcs(m) |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 690 | } |
Colin Cross | 5271fea | 2021-04-27 13:06:04 -0700 | [diff] [blame^] | 691 | |
| 692 | if c.NeedsVendorPublicLibraryVariants() && |
| 693 | (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) { |
| 694 | c.VendorProperties.IsVendorPublicLibrary = true |
| 695 | } |
Inseob Kim | e498dd9 | 2020-08-04 09:24:04 +0900 | [diff] [blame] | 696 | } |