blob: 9eec2558803bdcdfae4be82b2a0e3904cb8b0476 [file] [log] [blame]
Inseob Kime498dd92020-08-04 09:24:04 +09001// 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//
Colin Crossd079e0b2022-08-16 10:27:33 -07007// http://www.apache.org/licenses/LICENSE-2.0
Inseob Kime498dd92020-08-04 09:24:04 +09008//
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.
14package 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
19import (
Justin Yun6977e8a2020-10-29 18:24:11 +090020 "fmt"
21 "reflect"
Inseob Kime498dd92020-08-04 09:24:04 +090022 "strings"
23
24 "android/soong/android"
Kiyoung Kim48f37782021-07-07 12:42:39 +090025 "android/soong/snapshot"
Jooyung Han85707de2023-12-01 14:21:13 +090026
27 "github.com/google/blueprint/proptools"
Inseob Kime498dd92020-08-04 09:24:04 +090028)
29
30var _ android.ImageInterface = (*Module)(nil)
31
Ivan Lozano3968d8f2020-12-14 11:27:52 -050032type ImageVariantType string
Inseob Kim74d25562020-08-04 00:41:38 +090033
34const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050035 coreImageVariant ImageVariantType = "core"
36 vendorImageVariant ImageVariantType = "vendor"
37 productImageVariant ImageVariantType = "product"
38 ramdiskImageVariant ImageVariantType = "ramdisk"
39 vendorRamdiskImageVariant ImageVariantType = "vendor_ramdisk"
40 recoveryImageVariant ImageVariantType = "recovery"
41 hostImageVariant ImageVariantType = "host"
Inseob Kim74d25562020-08-04 00:41:38 +090042)
43
Inseob Kime498dd92020-08-04 09:24:04 +090044const (
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090045 // VendorVariation is the variant name used for /vendor code that does not
46 // compile against the VNDK.
47 VendorVariation = "vendor"
48
Inseob Kime498dd92020-08-04 09:24:04 +090049 // VendorVariationPrefix is the variant prefix used for /vendor code that compiles
50 // against the VNDK.
51 VendorVariationPrefix = "vendor."
52
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090053 // ProductVariation is the variant name used for /product code that does not
54 // compile against the VNDK.
55 ProductVariation = "product"
56
Inseob Kime498dd92020-08-04 09:24:04 +090057 // ProductVariationPrefix is the variant prefix used for /product code that compiles
58 // against the VNDK.
59 ProductVariationPrefix = "product."
60)
61
Inseob Kime498dd92020-08-04 09:24:04 +090062func (ctx *moduleContextImpl) inProduct() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -050063 return ctx.mod.InProduct()
Inseob Kime498dd92020-08-04 09:24:04 +090064}
65
66func (ctx *moduleContextImpl) inVendor() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050067 return ctx.mod.InVendor()
Inseob Kime498dd92020-08-04 09:24:04 +090068}
69
70func (ctx *moduleContextImpl) inRamdisk() bool {
71 return ctx.mod.InRamdisk()
72}
73
Yifan Hong60e0cfb2020-10-21 15:17:56 -070074func (ctx *moduleContextImpl) inVendorRamdisk() bool {
75 return ctx.mod.InVendorRamdisk()
76}
77
Inseob Kime498dd92020-08-04 09:24:04 +090078func (ctx *moduleContextImpl) inRecovery() bool {
79 return ctx.mod.InRecovery()
80}
81
Colin Crossea30d852023-11-29 16:00:16 -080082func (c *Module) InstallInProduct() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090083 // Additionally check if this module is inProduct() that means it is a "product" variant of a
84 // module. As well as product specific modules, product variants must be installed to /product.
85 return c.InProduct()
86}
87
Colin Crossea30d852023-11-29 16:00:16 -080088func (c *Module) InstallInVendor() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090089 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
90 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
91 // unless they have "odm_available: true".
92 return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm()
93}
94
Colin Crossea30d852023-11-29 16:00:16 -080095func (c *Module) InstallInOdm() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090096 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
97 return c.InVendor() && c.VendorVariantToOdm()
98}
99
Justin Yun63e9ec72020-10-29 16:49:43 +0900100// Returns true when this module is configured to have core and vendor variants.
Inseob Kime498dd92020-08-04 09:24:04 +0900101func (c *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900102 return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available)
103}
104
105// Returns true when this module creates a vendor variant and wants to install the vendor variant
106// to the odm partition.
107func (c *Module) VendorVariantToOdm() bool {
108 return Bool(c.VendorProperties.Odm_available)
Inseob Kime498dd92020-08-04 09:24:04 +0900109}
110
Justin Yun63e9ec72020-10-29 16:49:43 +0900111// Returns true when this module is configured to have core and product variants.
112func (c *Module) HasProductVariant() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900113 return Bool(c.VendorProperties.Product_available)
Justin Yun63e9ec72020-10-29 16:49:43 +0900114}
115
116// Returns true when this module is configured to have core and either product or vendor variants.
117func (c *Module) HasNonSystemVariants() bool {
Justin Yun6977e8a2020-10-29 18:24:11 +0900118 return c.HasVendorVariant() || c.HasProductVariant()
Justin Yun63e9ec72020-10-29 16:49:43 +0900119}
120
Inseob Kime498dd92020-08-04 09:24:04 +0900121// Returns true if the module is "product" variant. Usually these modules are installed in /product
Ivan Lozanof9e21722020-12-02 09:00:51 -0500122func (c *Module) InProduct() bool {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900123 return c.Properties.ImageVariation == ProductVariation
Inseob Kime498dd92020-08-04 09:24:04 +0900124}
125
126// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500127func (c *Module) InVendor() bool {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900128 return c.Properties.ImageVariation == VendorVariation
Inseob Kime498dd92020-08-04 09:24:04 +0900129}
130
131func (c *Module) InRamdisk() bool {
132 return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk()
133}
134
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700135func (c *Module) InVendorRamdisk() bool {
136 return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk()
137}
138
Inseob Kime498dd92020-08-04 09:24:04 +0900139func (c *Module) InRecovery() bool {
140 return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery()
141}
142
143func (c *Module) OnlyInRamdisk() bool {
144 return c.ModuleBase.InstallInRamdisk()
145}
146
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700147func (c *Module) OnlyInVendorRamdisk() bool {
148 return c.ModuleBase.InstallInVendorRamdisk()
149}
150
Inseob Kime498dd92020-08-04 09:24:04 +0900151func (c *Module) OnlyInRecovery() bool {
152 return c.ModuleBase.InstallInRecovery()
153}
154
Justin Yun6977e8a2020-10-29 18:24:11 +0900155func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool {
156 if v.Kind() != reflect.Struct {
157 return true
158 }
159 for i := 0; i < v.NumField(); i++ {
160 prop := v.Field(i)
161 if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" {
162 vendor_prop := prop.FieldByName("Vendor")
163 product_prop := prop.FieldByName("Product")
164 if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct {
165 // Neither Target.Vendor nor Target.Product is defined
166 continue
167 }
168 if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct ||
169 !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) {
170 // If only one of either Target.Vendor or Target.Product is
171 // defined or they have different values, it fails the build
172 // since VNDK must have the same properties for both vendor
173 // and product variants.
174 return false
175 }
176 } else if !visitPropsAndCompareVendorAndProductProps(prop) {
177 // Visit the substructures to find Target.Vendor and Target.Product
178 return false
179 }
180 }
181 return true
182}
183
184// In the case of VNDK, vendor and product variants must have the same properties.
185// VNDK installs only one file and shares it for both vendor and product modules on
186// runtime. We may not define different versions of a VNDK lib for each partition.
187// This function is used only for the VNDK modules that is available to both vendor
188// and product partitions.
189func (c *Module) compareVendorAndProductProps() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900190 if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) {
Justin Yun6977e8a2020-10-29 18:24:11 +0900191 panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name()))
192 }
193 for _, properties := range c.GetProperties() {
194 if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) {
195 return false
196 }
197 }
198 return true
199}
200
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400201// ImageMutatableModule provides a common image mutation interface for LinkableInterface modules.
202type ImageMutatableModule interface {
203 android.Module
204 LinkableInterface
205
206 // AndroidModuleBase returns the android.ModuleBase for this module
207 AndroidModuleBase() *android.ModuleBase
208
209 // VendorAvailable returns true if this module is available on the vendor image.
210 VendorAvailable() bool
211
212 // OdmAvailable returns true if this module is available on the odm image.
213 OdmAvailable() bool
214
215 // ProductAvailable returns true if this module is available on the product image.
216 ProductAvailable() bool
217
218 // RamdiskAvailable returns true if this module is available on the ramdisk image.
219 RamdiskAvailable() bool
220
221 // RecoveryAvailable returns true if this module is available on the recovery image.
222 RecoveryAvailable() bool
223
224 // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image.
225 VendorRamdiskAvailable() bool
226
227 // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt.
228 IsSnapshotPrebuilt() bool
229
230 // SnapshotVersion returns the snapshot version for this module.
231 SnapshotVersion(mctx android.BaseModuleContext) string
232
233 // SdkVersion returns the SDK version for this module.
234 SdkVersion() string
235
236 // ExtraVariants returns the list of extra variants this module requires.
237 ExtraVariants() []string
238
239 // AppendExtraVariant returns an extra variant to the list of extra variants this module requires.
240 AppendExtraVariant(extraVariant string)
241
242 // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed.
243 SetRamdiskVariantNeeded(b bool)
244
245 // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed.
246 SetVendorRamdiskVariantNeeded(b bool)
247
248 // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed.
249 SetRecoveryVariantNeeded(b bool)
250
251 // SetCoreVariantNeeded sets whether the Core Variant is needed.
252 SetCoreVariantNeeded(b bool)
253}
254
255var _ ImageMutatableModule = (*Module)(nil)
256
Inseob Kime498dd92020-08-04 09:24:04 +0900257func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400258 m.CheckVndkProperties(mctx)
259 MutateImage(mctx, m)
260}
261
262// CheckVndkProperties checks whether the VNDK-related properties are set correctly.
263// If properties are not set correctly, results in a module context property error.
264func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) {
Inseob Kime498dd92020-08-04 09:24:04 +0900265 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
266 productSpecific := mctx.ProductSpecific()
267
Inseob Kime498dd92020-08-04 09:24:04 +0900268 if vndkdep := m.vndkdep; vndkdep != nil {
269 if vndkdep.isVndk() {
270 if vendorSpecific || productSpecific {
271 if !vndkdep.isVndkExt() {
272 mctx.PropertyErrorf("vndk",
273 "must set `extends: \"...\"` to vndk extension")
Justin Yunc0d8c492021-01-07 17:45:31 +0900274 } else if Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900275 mctx.PropertyErrorf("vendor_available",
276 "must not set at the same time as `vndk: {extends: \"...\"}`")
Justin Yunc0d8c492021-01-07 17:45:31 +0900277 } else if Bool(m.VendorProperties.Product_available) {
Justin Yun63e9ec72020-10-29 16:49:43 +0900278 mctx.PropertyErrorf("product_available",
279 "must not set at the same time as `vndk: {extends: \"...\"}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900280 }
281 } else {
282 if vndkdep.isVndkExt() {
283 mctx.PropertyErrorf("vndk",
284 "must set `vendor: true` or `product_specific: true` to set `extends: %q`",
285 m.getVndkExtendsModuleName())
286 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900287 if !Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900288 mctx.PropertyErrorf("vndk",
Justin Yunc0d8c492021-01-07 17:45:31 +0900289 "vendor_available must be set to true when `vndk: {enabled: true}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900290 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900291 if Bool(m.VendorProperties.Product_available) {
Justin Yunfd9e8042020-12-23 18:23:14 +0900292 // If a VNDK module creates both product and vendor variants, they
293 // must have the same properties since they share a single VNDK
294 // library on runtime.
Justin Yun6977e8a2020-10-29 18:24:11 +0900295 if !m.compareVendorAndProductProps() {
296 mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules")
297 }
298 }
Inseob Kime498dd92020-08-04 09:24:04 +0900299 }
300 } else {
301 if vndkdep.isVndkSp() {
302 mctx.PropertyErrorf("vndk",
303 "must set `enabled: true` to set `support_system_process: true`")
304 }
305 if vndkdep.isVndkExt() {
306 mctx.PropertyErrorf("vndk",
307 "must set `enabled: true` to set `extends: %q`",
308 m.getVndkExtendsModuleName())
309 }
310 }
311 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400312}
313
314func (m *Module) VendorAvailable() bool {
315 return Bool(m.VendorProperties.Vendor_available)
316}
317
318func (m *Module) OdmAvailable() bool {
319 return Bool(m.VendorProperties.Odm_available)
320}
321
322func (m *Module) ProductAvailable() bool {
323 return Bool(m.VendorProperties.Product_available)
324}
325
326func (m *Module) RamdiskAvailable() bool {
327 return Bool(m.Properties.Ramdisk_available)
328}
329
330func (m *Module) VendorRamdiskAvailable() bool {
331 return Bool(m.Properties.Vendor_ramdisk_available)
332}
333
334func (m *Module) AndroidModuleBase() *android.ModuleBase {
335 return &m.ModuleBase
336}
337
338func (m *Module) RecoveryAvailable() bool {
339 return Bool(m.Properties.Recovery_available)
340}
341
342func (m *Module) ExtraVariants() []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200343 return m.Properties.ExtraVersionedImageVariations
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400344}
345
346func (m *Module) AppendExtraVariant(extraVariant string) {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200347 m.Properties.ExtraVersionedImageVariations = append(m.Properties.ExtraVersionedImageVariations, extraVariant)
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400348}
349
350func (m *Module) SetRamdiskVariantNeeded(b bool) {
351 m.Properties.RamdiskVariantNeeded = b
352}
353
354func (m *Module) SetVendorRamdiskVariantNeeded(b bool) {
355 m.Properties.VendorRamdiskVariantNeeded = b
356}
357
358func (m *Module) SetRecoveryVariantNeeded(b bool) {
359 m.Properties.RecoveryVariantNeeded = b
360}
361
362func (m *Module) SetCoreVariantNeeded(b bool) {
363 m.Properties.CoreVariantNeeded = b
364}
365
366func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400367 if snapshot, ok := m.linker.(SnapshotInterface); ok {
368 return snapshot.Version()
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400369 } else {
370 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
371 // Should we be panicking here instead?
372 return ""
373 }
374}
375
376func (m *Module) KernelHeadersDecorator() bool {
377 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
378 return true
379 }
380 return false
381}
382
383// MutateImage handles common image mutations for ImageMutatableModule interfaces.
384func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) {
385 // Validation check
386 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
387 productSpecific := mctx.ProductSpecific()
388
389 if m.VendorAvailable() {
390 if vendorSpecific {
391 mctx.PropertyErrorf("vendor_available",
392 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
393 }
394 if m.OdmAvailable() {
395 mctx.PropertyErrorf("vendor_available",
396 "doesn't make sense at the same time as `odm_available: true`")
397 }
398 }
399
400 if m.OdmAvailable() {
401 if vendorSpecific {
402 mctx.PropertyErrorf("odm_available",
403 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
404 }
405 }
406
407 if m.ProductAvailable() {
408 if productSpecific {
409 mctx.PropertyErrorf("product_available",
410 "doesn't make sense at the same time as `product_specific: true`")
411 }
412 if vendorSpecific {
413 mctx.PropertyErrorf("product_available",
414 "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`")
415 }
416 }
Inseob Kime498dd92020-08-04 09:24:04 +0900417
418 var coreVariantNeeded bool = false
419 var ramdiskVariantNeeded bool = false
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700420 var vendorRamdiskVariantNeeded bool = false
Inseob Kime498dd92020-08-04 09:24:04 +0900421 var recoveryVariantNeeded bool = false
422
423 var vendorVariants []string
424 var productVariants []string
425
426 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
427 boardVndkVersion := mctx.DeviceConfig().VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800428 recoverySnapshotVersion := mctx.DeviceConfig().RecoverySnapshotVersion()
429 usingRecoverySnapshot := recoverySnapshotVersion != "current" &&
430 recoverySnapshotVersion != ""
Justin Yundee806f2021-05-18 23:10:00 +0900431 needVndkVersionVendorVariantForLlndk := false
432 if boardVndkVersion != "" {
433 boardVndkApiLevel, err := android.ApiLevelFromUser(mctx, boardVndkVersion)
434 if err == nil && !boardVndkApiLevel.IsPreview() {
435 // VNDK snapshot newer than v30 has LLNDK stub libraries.
436 // Only the VNDK version less than or equal to v30 requires generating the vendor
437 // variant of the VNDK version from the source tree.
438 needVndkVersionVendorVariantForLlndk = boardVndkApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(mctx, "30"))
439 }
440 }
Inseob Kime498dd92020-08-04 09:24:04 +0900441 if boardVndkVersion == "current" {
442 boardVndkVersion = platformVndkVersion
443 }
Inseob Kime498dd92020-08-04 09:24:04 +0900444
Colin Cross203b4212021-04-26 17:19:41 -0700445 if m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800446 // This is an LLNDK library. The implementation of the library will be on /system,
447 // and vendor and product variants will be created with LLNDK stubs.
448 // The LLNDK libraries need vendor variants even if there is no VNDK.
Colin Cross203b4212021-04-26 17:19:41 -0700449 coreVariantNeeded = true
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900450 vendorVariants = append(vendorVariants, platformVndkVersion)
451 productVariants = append(productVariants, platformVndkVersion)
Justin Yundee806f2021-05-18 23:10:00 +0900452 // Generate vendor variants for boardVndkVersion only if the VNDK snapshot does not
453 // provide the LLNDK stub libraries.
454 if needVndkVersionVendorVariantForLlndk {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800455 vendorVariants = append(vendorVariants, boardVndkVersion)
456 }
Colin Cross5271fea2021-04-27 13:06:04 -0700457 } else if m.NeedsVendorPublicLibraryVariants() {
458 // A vendor public library has the implementation on /vendor, with stub variants
459 // for system and product.
460 coreVariantNeeded = true
461 vendorVariants = append(vendorVariants, boardVndkVersion)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900462 productVariants = append(productVariants, platformVndkVersion)
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400463 } else if m.IsSnapshotPrebuilt() {
Inseob Kime498dd92020-08-04 09:24:04 +0900464 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
465 // PRODUCT_EXTRA_VNDK_VERSIONS.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400466 if m.InstallInRecovery() {
467 recoveryVariantNeeded = true
Inseob Kime498dd92020-08-04 09:24:04 +0900468 } else {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400469 vendorVariants = append(vendorVariants, m.SnapshotVersion(mctx))
Inseob Kime498dd92020-08-04 09:24:04 +0900470 }
Ivan Lozanof9e21722020-12-02 09:00:51 -0500471 } else if m.HasNonSystemVariants() && !m.IsVndkExt() {
Justin Yun63e9ec72020-10-29 16:49:43 +0900472 // This will be available to /system unless it is product_specific
473 // which will be handled later.
Inseob Kime498dd92020-08-04 09:24:04 +0900474 coreVariantNeeded = true
475
476 // We assume that modules under proprietary paths are compatible for
477 // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or
478 // PLATFORM_VNDK_VERSION.
Justin Yun63e9ec72020-10-29 16:49:43 +0900479 if m.HasVendorVariant() {
Kiyoung Kim48f37782021-07-07 12:42:39 +0900480 if snapshot.IsVendorProprietaryModule(mctx) {
Justin Yun63e9ec72020-10-29 16:49:43 +0900481 vendorVariants = append(vendorVariants, boardVndkVersion)
482 } else {
483 vendorVariants = append(vendorVariants, platformVndkVersion)
484 }
Inseob Kime498dd92020-08-04 09:24:04 +0900485 }
486
Justin Yun6977e8a2020-10-29 18:24:11 +0900487 // product_available modules are available to /product.
488 if m.HasProductVariant() {
489 productVariants = append(productVariants, platformVndkVersion)
Inseob Kime498dd92020-08-04 09:24:04 +0900490 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400491 } else if vendorSpecific && m.SdkVersion() == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900492 // This will be available in /vendor (or /odm) only
493
494 // kernel_headers is a special module type whose exported headers
495 // are coming from DeviceKernelHeaders() which is always vendor
496 // dependent. They'll always have both vendor variants.
497 // For other modules, we assume that modules under proprietary
498 // paths are compatible for BOARD_VNDK_VERSION. The other modules
499 // are regarded as AOSP, which is PLATFORM_VNDK_VERSION.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400500 if m.KernelHeadersDecorator() {
Inseob Kime498dd92020-08-04 09:24:04 +0900501 vendorVariants = append(vendorVariants,
502 platformVndkVersion,
503 boardVndkVersion,
504 )
Kiyoung Kim48f37782021-07-07 12:42:39 +0900505 } else if snapshot.IsVendorProprietaryModule(mctx) {
Inseob Kime498dd92020-08-04 09:24:04 +0900506 vendorVariants = append(vendorVariants, boardVndkVersion)
507 } else {
508 vendorVariants = append(vendorVariants, platformVndkVersion)
509 }
510 } else {
511 // This is either in /system (or similar: /data), or is a
jiajia tangcd1c27b2022-07-21 18:04:37 +0800512 // module built with the NDK. Modules built with the NDK
Inseob Kime498dd92020-08-04 09:24:04 +0900513 // will be restricted using the existing link type checks.
514 coreVariantNeeded = true
515 }
516
Justin Yunaf1fde42023-09-27 16:22:10 +0900517 if coreVariantNeeded && productSpecific && m.SdkVersion() == "" {
518 // The module has "product_specific: true" that does not create core variant.
519 coreVariantNeeded = false
520 productVariants = append(productVariants, platformVndkVersion)
Inseob Kime498dd92020-08-04 09:24:04 +0900521 }
522
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400523 if m.RamdiskAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900524 ramdiskVariantNeeded = true
525 }
526
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400527 if m.AndroidModuleBase().InstallInRamdisk() {
Inseob Kime498dd92020-08-04 09:24:04 +0900528 ramdiskVariantNeeded = true
529 coreVariantNeeded = false
530 }
531
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400532 if m.VendorRamdiskAvailable() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700533 vendorRamdiskVariantNeeded = true
534 }
535
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400536 if m.AndroidModuleBase().InstallInVendorRamdisk() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700537 vendorRamdiskVariantNeeded = true
538 coreVariantNeeded = false
539 }
540
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400541 if m.RecoveryAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900542 recoveryVariantNeeded = true
543 }
544
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400545 if m.AndroidModuleBase().InstallInRecovery() {
Inseob Kime498dd92020-08-04 09:24:04 +0900546 recoveryVariantNeeded = true
547 coreVariantNeeded = false
548 }
549
Jose Galmes6f843bc2020-12-11 13:36:29 -0800550 // If using a snapshot, the recovery variant under AOSP directories is not needed,
551 // except for kernel headers, which needs all variants.
Jose Galmes737d0a12021-05-25 22:06:41 -0700552 if !m.KernelHeadersDecorator() &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400553 !m.IsSnapshotPrebuilt() &&
Jose Galmes6f843bc2020-12-11 13:36:29 -0800554 usingRecoverySnapshot &&
Kiyoung Kim48f37782021-07-07 12:42:39 +0900555 !snapshot.IsRecoveryProprietaryModule(mctx) {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800556 recoveryVariantNeeded = false
557 }
558
Inseob Kime498dd92020-08-04 09:24:04 +0900559 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900560 if variant == "" {
561 m.AppendExtraVariant(VendorVariation)
562 } else {
563 m.AppendExtraVariant(VendorVariationPrefix + variant)
564 }
Inseob Kime498dd92020-08-04 09:24:04 +0900565 }
566
567 for _, variant := range android.FirstUniqueStrings(productVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900568 if variant == "" {
569 m.AppendExtraVariant(ProductVariation)
570 } else {
571 m.AppendExtraVariant(ProductVariationPrefix + variant)
572 }
Inseob Kime498dd92020-08-04 09:24:04 +0900573 }
574
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400575 m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
576 m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
577 m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
578 m.SetCoreVariantNeeded(coreVariantNeeded)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800579
580 // Disable the module if no variants are needed.
581 if !ramdiskVariantNeeded &&
582 !recoveryVariantNeeded &&
583 !coreVariantNeeded &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400584 len(m.ExtraVariants()) == 0 {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800585 m.Disable()
586 }
Inseob Kime498dd92020-08-04 09:24:04 +0900587}
588
589func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
590 return c.Properties.CoreVariantNeeded
591}
592
593func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
594 return c.Properties.RamdiskVariantNeeded
595}
596
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700597func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
598 return c.Properties.VendorRamdiskVariantNeeded
599}
600
Inseob Kim08758f02021-04-08 21:13:22 +0900601func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
602 return false
603}
604
Inseob Kime498dd92020-08-04 09:24:04 +0900605func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
606 return c.Properties.RecoveryVariantNeeded
607}
608
609func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200610 return c.Properties.ExtraVersionedImageVariations
Inseob Kime498dd92020-08-04 09:24:04 +0900611}
612
Justin Yun63e9ec72020-10-29 16:49:43 +0900613func squashVendorSrcs(m *Module) {
614 if lib, ok := m.compiler.(*libraryDecorator); ok {
615 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
616 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
617
618 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
619 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
620
621 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
622 lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900623
624 if lib.Properties.Target.Vendor.No_stubs {
625 proptools.Clear(&lib.Properties.Stubs)
626 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900627 }
628}
629
630func squashProductSrcs(m *Module) {
631 if lib, ok := m.compiler.(*libraryDecorator); ok {
632 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
633 lib.baseCompiler.Properties.Target.Product.Srcs...)
634
635 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
636 lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
637
638 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
639 lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900640
641 if lib.Properties.Target.Product.No_stubs {
642 proptools.Clear(&lib.Properties.Stubs)
643 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900644 }
645}
646
647func squashRecoverySrcs(m *Module) {
648 if lib, ok := m.compiler.(*libraryDecorator); ok {
649 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
650 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
651
652 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
653 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
654
655 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
656 lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
657 }
658}
659
660func squashVendorRamdiskSrcs(m *Module) {
661 if lib, ok := m.compiler.(*libraryDecorator); ok {
662 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
663 }
664}
665
Christopher Ferrise0202c42023-07-27 17:06:46 -0700666func squashRamdiskSrcs(m *Module) {
667 if lib, ok := m.compiler.(*libraryDecorator); ok {
668 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Ramdisk.Exclude_srcs...)
669 }
670}
671
Inseob Kime498dd92020-08-04 09:24:04 +0900672func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
673 m := module.(*Module)
Yifan Hong6da33c22020-10-27 15:01:21 -0700674 if variant == android.RamdiskVariation {
Inseob Kime498dd92020-08-04 09:24:04 +0900675 m.MakeAsPlatform()
Christopher Ferrise0202c42023-07-27 17:06:46 -0700676 squashRamdiskSrcs(m)
Yifan Hong6da33c22020-10-27 15:01:21 -0700677 } else if variant == android.VendorRamdiskVariation {
678 m.MakeAsPlatform()
679 squashVendorRamdiskSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900680 } else if variant == android.RecoveryVariation {
681 m.MakeAsPlatform()
682 squashRecoverySrcs(m)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900683 } else if strings.HasPrefix(variant, VendorVariation) {
684 m.Properties.ImageVariation = VendorVariation
685
686 if strings.HasPrefix(variant, VendorVariationPrefix) {
687 m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
688 }
Inseob Kime498dd92020-08-04 09:24:04 +0900689 squashVendorSrcs(m)
690
691 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
692 // Hide other vendor variants to avoid collision.
693 vndkVersion := ctx.DeviceConfig().VndkVersion()
694 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
695 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800696 m.HideFromMake()
Inseob Kime498dd92020-08-04 09:24:04 +0900697 }
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900698 } else if strings.HasPrefix(variant, ProductVariation) {
699 m.Properties.ImageVariation = ProductVariation
700 if strings.HasPrefix(variant, ProductVariationPrefix) {
701 m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
702 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900703 squashProductSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900704 }
Colin Cross5271fea2021-04-27 13:06:04 -0700705
706 if c.NeedsVendorPublicLibraryVariants() &&
707 (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) {
708 c.VendorProperties.IsVendorPublicLibrary = true
709 }
Inseob Kime498dd92020-08-04 09:24:04 +0900710}