blob: 76cb09fc848ea408da857e81a17e61f294079d64 [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"
Jooyung Han85707de2023-12-01 14:21:13 +090025
26 "github.com/google/blueprint/proptools"
Inseob Kime498dd92020-08-04 09:24:04 +090027)
28
29var _ android.ImageInterface = (*Module)(nil)
30
Ivan Lozano3968d8f2020-12-14 11:27:52 -050031type ImageVariantType string
Inseob Kim74d25562020-08-04 00:41:38 +090032
33const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050034 coreImageVariant ImageVariantType = "core"
35 vendorImageVariant ImageVariantType = "vendor"
36 productImageVariant ImageVariantType = "product"
37 ramdiskImageVariant ImageVariantType = "ramdisk"
38 vendorRamdiskImageVariant ImageVariantType = "vendor_ramdisk"
39 recoveryImageVariant ImageVariantType = "recovery"
40 hostImageVariant ImageVariantType = "host"
Inseob Kim74d25562020-08-04 00:41:38 +090041)
42
Inseob Kime498dd92020-08-04 09:24:04 +090043const (
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090044 // VendorVariation is the variant name used for /vendor code that does not
45 // compile against the VNDK.
46 VendorVariation = "vendor"
47
Inseob Kime498dd92020-08-04 09:24:04 +090048 // VendorVariationPrefix is the variant prefix used for /vendor code that compiles
49 // against the VNDK.
50 VendorVariationPrefix = "vendor."
51
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +090052 // ProductVariation is the variant name used for /product code that does not
53 // compile against the VNDK.
54 ProductVariation = "product"
55
Inseob Kime498dd92020-08-04 09:24:04 +090056 // ProductVariationPrefix is the variant prefix used for /product code that compiles
57 // against the VNDK.
58 ProductVariationPrefix = "product."
59)
60
Inseob Kime498dd92020-08-04 09:24:04 +090061func (ctx *moduleContextImpl) inProduct() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -050062 return ctx.mod.InProduct()
Inseob Kime498dd92020-08-04 09:24:04 +090063}
64
65func (ctx *moduleContextImpl) inVendor() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050066 return ctx.mod.InVendor()
Inseob Kime498dd92020-08-04 09:24:04 +090067}
68
69func (ctx *moduleContextImpl) inRamdisk() bool {
70 return ctx.mod.InRamdisk()
71}
72
Yifan Hong60e0cfb2020-10-21 15:17:56 -070073func (ctx *moduleContextImpl) inVendorRamdisk() bool {
74 return ctx.mod.InVendorRamdisk()
75}
76
Inseob Kime498dd92020-08-04 09:24:04 +090077func (ctx *moduleContextImpl) inRecovery() bool {
78 return ctx.mod.InRecovery()
79}
80
Colin Crossea30d852023-11-29 16:00:16 -080081func (c *Module) InstallInProduct() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090082 // Additionally check if this module is inProduct() that means it is a "product" variant of a
83 // module. As well as product specific modules, product variants must be installed to /product.
84 return c.InProduct()
85}
86
Colin Crossea30d852023-11-29 16:00:16 -080087func (c *Module) InstallInVendor() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090088 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
89 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
90 // unless they have "odm_available: true".
91 return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm()
92}
93
Colin Crossea30d852023-11-29 16:00:16 -080094func (c *Module) InstallInOdm() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090095 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
96 return c.InVendor() && c.VendorVariantToOdm()
97}
98
Justin Yun63e9ec72020-10-29 16:49:43 +090099// Returns true when this module is configured to have core and vendor variants.
Inseob Kime498dd92020-08-04 09:24:04 +0900100func (c *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900101 return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available)
102}
103
104// Returns true when this module creates a vendor variant and wants to install the vendor variant
105// to the odm partition.
106func (c *Module) VendorVariantToOdm() bool {
107 return Bool(c.VendorProperties.Odm_available)
Inseob Kime498dd92020-08-04 09:24:04 +0900108}
109
Justin Yun63e9ec72020-10-29 16:49:43 +0900110// Returns true when this module is configured to have core and product variants.
111func (c *Module) HasProductVariant() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900112 return Bool(c.VendorProperties.Product_available)
Justin Yun63e9ec72020-10-29 16:49:43 +0900113}
114
115// Returns true when this module is configured to have core and either product or vendor variants.
116func (c *Module) HasNonSystemVariants() bool {
Justin Yun6977e8a2020-10-29 18:24:11 +0900117 return c.HasVendorVariant() || c.HasProductVariant()
Justin Yun63e9ec72020-10-29 16:49:43 +0900118}
119
Inseob Kime498dd92020-08-04 09:24:04 +0900120// Returns true if the module is "product" variant. Usually these modules are installed in /product
Ivan Lozanof9e21722020-12-02 09:00:51 -0500121func (c *Module) InProduct() bool {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900122 return c.Properties.ImageVariation == ProductVariation
Inseob Kime498dd92020-08-04 09:24:04 +0900123}
124
125// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500126func (c *Module) InVendor() bool {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900127 return c.Properties.ImageVariation == VendorVariation
Inseob Kime498dd92020-08-04 09:24:04 +0900128}
129
Kiyoung Kimaa394802024-01-08 12:55:45 +0900130// Returns true if the module is "vendor" or "product" variant. This replaces previous UseVndk usages
131// which were misused to check if the module variant is vendor or product.
132func (c *Module) InVendorOrProduct() bool {
133 return c.InVendor() || c.InProduct()
134}
135
Inseob Kime498dd92020-08-04 09:24:04 +0900136func (c *Module) InRamdisk() bool {
137 return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk()
138}
139
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700140func (c *Module) InVendorRamdisk() bool {
141 return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk()
142}
143
Inseob Kime498dd92020-08-04 09:24:04 +0900144func (c *Module) InRecovery() bool {
145 return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery()
146}
147
148func (c *Module) OnlyInRamdisk() bool {
149 return c.ModuleBase.InstallInRamdisk()
150}
151
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700152func (c *Module) OnlyInVendorRamdisk() bool {
153 return c.ModuleBase.InstallInVendorRamdisk()
154}
155
Inseob Kime498dd92020-08-04 09:24:04 +0900156func (c *Module) OnlyInRecovery() bool {
157 return c.ModuleBase.InstallInRecovery()
158}
159
Justin Yun6977e8a2020-10-29 18:24:11 +0900160func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool {
161 if v.Kind() != reflect.Struct {
162 return true
163 }
164 for i := 0; i < v.NumField(); i++ {
165 prop := v.Field(i)
166 if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" {
167 vendor_prop := prop.FieldByName("Vendor")
168 product_prop := prop.FieldByName("Product")
169 if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct {
170 // Neither Target.Vendor nor Target.Product is defined
171 continue
172 }
173 if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct ||
174 !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) {
175 // If only one of either Target.Vendor or Target.Product is
176 // defined or they have different values, it fails the build
177 // since VNDK must have the same properties for both vendor
178 // and product variants.
179 return false
180 }
181 } else if !visitPropsAndCompareVendorAndProductProps(prop) {
182 // Visit the substructures to find Target.Vendor and Target.Product
183 return false
184 }
185 }
186 return true
187}
188
189// In the case of VNDK, vendor and product variants must have the same properties.
190// VNDK installs only one file and shares it for both vendor and product modules on
191// runtime. We may not define different versions of a VNDK lib for each partition.
192// This function is used only for the VNDK modules that is available to both vendor
193// and product partitions.
194func (c *Module) compareVendorAndProductProps() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900195 if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) {
Justin Yun6977e8a2020-10-29 18:24:11 +0900196 panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name()))
197 }
198 for _, properties := range c.GetProperties() {
199 if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) {
200 return false
201 }
202 }
203 return true
204}
205
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400206// ImageMutatableModule provides a common image mutation interface for LinkableInterface modules.
207type ImageMutatableModule interface {
208 android.Module
209 LinkableInterface
210
211 // AndroidModuleBase returns the android.ModuleBase for this module
212 AndroidModuleBase() *android.ModuleBase
213
214 // VendorAvailable returns true if this module is available on the vendor image.
215 VendorAvailable() bool
216
217 // OdmAvailable returns true if this module is available on the odm image.
218 OdmAvailable() bool
219
220 // ProductAvailable returns true if this module is available on the product image.
221 ProductAvailable() bool
222
223 // RamdiskAvailable returns true if this module is available on the ramdisk image.
224 RamdiskAvailable() bool
225
226 // RecoveryAvailable returns true if this module is available on the recovery image.
227 RecoveryAvailable() bool
228
229 // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image.
230 VendorRamdiskAvailable() bool
231
232 // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt.
233 IsSnapshotPrebuilt() bool
234
235 // SnapshotVersion returns the snapshot version for this module.
236 SnapshotVersion(mctx android.BaseModuleContext) string
237
238 // SdkVersion returns the SDK version for this module.
239 SdkVersion() string
240
241 // ExtraVariants returns the list of extra variants this module requires.
242 ExtraVariants() []string
243
244 // AppendExtraVariant returns an extra variant to the list of extra variants this module requires.
245 AppendExtraVariant(extraVariant string)
246
247 // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed.
248 SetRamdiskVariantNeeded(b bool)
249
250 // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed.
251 SetVendorRamdiskVariantNeeded(b bool)
252
253 // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed.
254 SetRecoveryVariantNeeded(b bool)
255
256 // SetCoreVariantNeeded sets whether the Core Variant is needed.
257 SetCoreVariantNeeded(b bool)
258}
259
260var _ ImageMutatableModule = (*Module)(nil)
261
Inseob Kime498dd92020-08-04 09:24:04 +0900262func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400263 m.CheckVndkProperties(mctx)
264 MutateImage(mctx, m)
265}
266
267// CheckVndkProperties checks whether the VNDK-related properties are set correctly.
268// If properties are not set correctly, results in a module context property error.
269func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) {
Inseob Kime498dd92020-08-04 09:24:04 +0900270 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
271 productSpecific := mctx.ProductSpecific()
272
Inseob Kime498dd92020-08-04 09:24:04 +0900273 if vndkdep := m.vndkdep; vndkdep != nil {
274 if vndkdep.isVndk() {
275 if vendorSpecific || productSpecific {
276 if !vndkdep.isVndkExt() {
277 mctx.PropertyErrorf("vndk",
278 "must set `extends: \"...\"` to vndk extension")
Justin Yunc0d8c492021-01-07 17:45:31 +0900279 } else if Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900280 mctx.PropertyErrorf("vendor_available",
281 "must not set at the same time as `vndk: {extends: \"...\"}`")
Justin Yunc0d8c492021-01-07 17:45:31 +0900282 } else if Bool(m.VendorProperties.Product_available) {
Justin Yun63e9ec72020-10-29 16:49:43 +0900283 mctx.PropertyErrorf("product_available",
284 "must not set at the same time as `vndk: {extends: \"...\"}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900285 }
286 } else {
287 if vndkdep.isVndkExt() {
288 mctx.PropertyErrorf("vndk",
289 "must set `vendor: true` or `product_specific: true` to set `extends: %q`",
290 m.getVndkExtendsModuleName())
291 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900292 if !Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900293 mctx.PropertyErrorf("vndk",
Justin Yunc0d8c492021-01-07 17:45:31 +0900294 "vendor_available must be set to true when `vndk: {enabled: true}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900295 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900296 if Bool(m.VendorProperties.Product_available) {
Justin Yunfd9e8042020-12-23 18:23:14 +0900297 // If a VNDK module creates both product and vendor variants, they
298 // must have the same properties since they share a single VNDK
299 // library on runtime.
Justin Yun6977e8a2020-10-29 18:24:11 +0900300 if !m.compareVendorAndProductProps() {
301 mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules")
302 }
303 }
Inseob Kime498dd92020-08-04 09:24:04 +0900304 }
305 } else {
306 if vndkdep.isVndkSp() {
307 mctx.PropertyErrorf("vndk",
308 "must set `enabled: true` to set `support_system_process: true`")
309 }
310 if vndkdep.isVndkExt() {
311 mctx.PropertyErrorf("vndk",
312 "must set `enabled: true` to set `extends: %q`",
313 m.getVndkExtendsModuleName())
314 }
315 }
316 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400317}
318
319func (m *Module) VendorAvailable() bool {
320 return Bool(m.VendorProperties.Vendor_available)
321}
322
323func (m *Module) OdmAvailable() bool {
324 return Bool(m.VendorProperties.Odm_available)
325}
326
327func (m *Module) ProductAvailable() bool {
328 return Bool(m.VendorProperties.Product_available)
329}
330
331func (m *Module) RamdiskAvailable() bool {
332 return Bool(m.Properties.Ramdisk_available)
333}
334
335func (m *Module) VendorRamdiskAvailable() bool {
336 return Bool(m.Properties.Vendor_ramdisk_available)
337}
338
339func (m *Module) AndroidModuleBase() *android.ModuleBase {
340 return &m.ModuleBase
341}
342
343func (m *Module) RecoveryAvailable() bool {
344 return Bool(m.Properties.Recovery_available)
345}
346
347func (m *Module) ExtraVariants() []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200348 return m.Properties.ExtraVersionedImageVariations
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400349}
350
351func (m *Module) AppendExtraVariant(extraVariant string) {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200352 m.Properties.ExtraVersionedImageVariations = append(m.Properties.ExtraVersionedImageVariations, extraVariant)
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400353}
354
355func (m *Module) SetRamdiskVariantNeeded(b bool) {
356 m.Properties.RamdiskVariantNeeded = b
357}
358
359func (m *Module) SetVendorRamdiskVariantNeeded(b bool) {
360 m.Properties.VendorRamdiskVariantNeeded = b
361}
362
363func (m *Module) SetRecoveryVariantNeeded(b bool) {
364 m.Properties.RecoveryVariantNeeded = b
365}
366
367func (m *Module) SetCoreVariantNeeded(b bool) {
368 m.Properties.CoreVariantNeeded = b
369}
370
371func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400372 if snapshot, ok := m.linker.(SnapshotInterface); ok {
373 return snapshot.Version()
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400374 } else {
375 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
376 // Should we be panicking here instead?
377 return ""
378 }
379}
380
381func (m *Module) KernelHeadersDecorator() bool {
382 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
383 return true
384 }
385 return false
386}
387
388// MutateImage handles common image mutations for ImageMutatableModule interfaces.
389func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) {
390 // Validation check
391 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
392 productSpecific := mctx.ProductSpecific()
393
394 if m.VendorAvailable() {
395 if vendorSpecific {
396 mctx.PropertyErrorf("vendor_available",
397 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
398 }
399 if m.OdmAvailable() {
400 mctx.PropertyErrorf("vendor_available",
401 "doesn't make sense at the same time as `odm_available: true`")
402 }
403 }
404
405 if m.OdmAvailable() {
406 if vendorSpecific {
407 mctx.PropertyErrorf("odm_available",
408 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
409 }
410 }
411
412 if m.ProductAvailable() {
413 if productSpecific {
414 mctx.PropertyErrorf("product_available",
415 "doesn't make sense at the same time as `product_specific: true`")
416 }
417 if vendorSpecific {
418 mctx.PropertyErrorf("product_available",
419 "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`")
420 }
421 }
Inseob Kime498dd92020-08-04 09:24:04 +0900422
423 var coreVariantNeeded bool = false
424 var ramdiskVariantNeeded bool = false
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700425 var vendorRamdiskVariantNeeded bool = false
Inseob Kime498dd92020-08-04 09:24:04 +0900426 var recoveryVariantNeeded bool = false
427
428 var vendorVariants []string
429 var productVariants []string
430
Inseob Kime498dd92020-08-04 09:24:04 +0900431 boardVndkVersion := mctx.DeviceConfig().VndkVersion()
Justin Yundee806f2021-05-18 23:10:00 +0900432 needVndkVersionVendorVariantForLlndk := false
433 if boardVndkVersion != "" {
434 boardVndkApiLevel, err := android.ApiLevelFromUser(mctx, boardVndkVersion)
435 if err == nil && !boardVndkApiLevel.IsPreview() {
436 // VNDK snapshot newer than v30 has LLNDK stub libraries.
437 // Only the VNDK version less than or equal to v30 requires generating the vendor
438 // variant of the VNDK version from the source tree.
439 needVndkVersionVendorVariantForLlndk = boardVndkApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(mctx, "30"))
440 }
441 }
Colin Cross203b4212021-04-26 17:19:41 -0700442 if m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800443 // This is an LLNDK library. The implementation of the library will be on /system,
444 // and vendor and product variants will be created with LLNDK stubs.
445 // The LLNDK libraries need vendor variants even if there is no VNDK.
Colin Cross203b4212021-04-26 17:19:41 -0700446 coreVariantNeeded = true
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900447 vendorVariants = append(vendorVariants, "")
448 productVariants = append(productVariants, "")
Justin Yundee806f2021-05-18 23:10:00 +0900449 // Generate vendor variants for boardVndkVersion only if the VNDK snapshot does not
450 // provide the LLNDK stub libraries.
451 if needVndkVersionVendorVariantForLlndk {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800452 vendorVariants = append(vendorVariants, boardVndkVersion)
453 }
Colin Cross5271fea2021-04-27 13:06:04 -0700454 } else if m.NeedsVendorPublicLibraryVariants() {
455 // A vendor public library has the implementation on /vendor, with stub variants
456 // for system and product.
457 coreVariantNeeded = true
458 vendorVariants = append(vendorVariants, boardVndkVersion)
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900459 productVariants = append(productVariants, "")
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400460 } else if m.IsSnapshotPrebuilt() {
Inseob Kime498dd92020-08-04 09:24:04 +0900461 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
462 // PRODUCT_EXTRA_VNDK_VERSIONS.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400463 if m.InstallInRecovery() {
464 recoveryVariantNeeded = true
Inseob Kime498dd92020-08-04 09:24:04 +0900465 } else {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400466 vendorVariants = append(vendorVariants, m.SnapshotVersion(mctx))
Inseob Kime498dd92020-08-04 09:24:04 +0900467 }
Ivan Lozanof9e21722020-12-02 09:00:51 -0500468 } else if m.HasNonSystemVariants() && !m.IsVndkExt() {
Justin Yun63e9ec72020-10-29 16:49:43 +0900469 // This will be available to /system unless it is product_specific
470 // which will be handled later.
Inseob Kime498dd92020-08-04 09:24:04 +0900471 coreVariantNeeded = true
472
473 // We assume that modules under proprietary paths are compatible for
474 // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or
475 // PLATFORM_VNDK_VERSION.
Justin Yun63e9ec72020-10-29 16:49:43 +0900476 if m.HasVendorVariant() {
Kiyoung Kim37693d02024-04-04 09:56:15 +0900477 vendorVariants = append(vendorVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900478 }
479
Justin Yun6977e8a2020-10-29 18:24:11 +0900480 // product_available modules are available to /product.
481 if m.HasProductVariant() {
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900482 productVariants = append(productVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900483 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400484 } else if vendorSpecific && m.SdkVersion() == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900485 // This will be available in /vendor (or /odm) only
486
487 // kernel_headers is a special module type whose exported headers
488 // are coming from DeviceKernelHeaders() which is always vendor
489 // dependent. They'll always have both vendor variants.
490 // For other modules, we assume that modules under proprietary
491 // paths are compatible for BOARD_VNDK_VERSION. The other modules
492 // are regarded as AOSP, which is PLATFORM_VNDK_VERSION.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400493 if m.KernelHeadersDecorator() {
Inseob Kime498dd92020-08-04 09:24:04 +0900494 vendorVariants = append(vendorVariants,
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900495 "",
Inseob Kime498dd92020-08-04 09:24:04 +0900496 boardVndkVersion,
497 )
Inseob Kime498dd92020-08-04 09:24:04 +0900498 } else {
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900499 vendorVariants = append(vendorVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900500 }
501 } else {
502 // This is either in /system (or similar: /data), or is a
jiajia tangcd1c27b2022-07-21 18:04:37 +0800503 // module built with the NDK. Modules built with the NDK
Inseob Kime498dd92020-08-04 09:24:04 +0900504 // will be restricted using the existing link type checks.
505 coreVariantNeeded = true
506 }
507
Justin Yunaf1fde42023-09-27 16:22:10 +0900508 if coreVariantNeeded && productSpecific && m.SdkVersion() == "" {
509 // The module has "product_specific: true" that does not create core variant.
510 coreVariantNeeded = false
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900511 productVariants = append(productVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900512 }
513
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400514 if m.RamdiskAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900515 ramdiskVariantNeeded = true
516 }
517
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400518 if m.AndroidModuleBase().InstallInRamdisk() {
Inseob Kime498dd92020-08-04 09:24:04 +0900519 ramdiskVariantNeeded = true
520 coreVariantNeeded = false
521 }
522
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400523 if m.VendorRamdiskAvailable() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700524 vendorRamdiskVariantNeeded = true
525 }
526
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400527 if m.AndroidModuleBase().InstallInVendorRamdisk() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700528 vendorRamdiskVariantNeeded = true
529 coreVariantNeeded = false
530 }
531
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400532 if m.RecoveryAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900533 recoveryVariantNeeded = true
534 }
535
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400536 if m.AndroidModuleBase().InstallInRecovery() {
Inseob Kime498dd92020-08-04 09:24:04 +0900537 recoveryVariantNeeded = true
538 coreVariantNeeded = false
539 }
540
541 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900542 if variant == "" {
543 m.AppendExtraVariant(VendorVariation)
544 } else {
545 m.AppendExtraVariant(VendorVariationPrefix + variant)
546 }
Inseob Kime498dd92020-08-04 09:24:04 +0900547 }
548
549 for _, variant := range android.FirstUniqueStrings(productVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900550 if variant == "" {
551 m.AppendExtraVariant(ProductVariation)
552 } else {
553 m.AppendExtraVariant(ProductVariationPrefix + variant)
554 }
Inseob Kime498dd92020-08-04 09:24:04 +0900555 }
556
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400557 m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
558 m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
559 m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
560 m.SetCoreVariantNeeded(coreVariantNeeded)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800561
562 // Disable the module if no variants are needed.
563 if !ramdiskVariantNeeded &&
564 !recoveryVariantNeeded &&
565 !coreVariantNeeded &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400566 len(m.ExtraVariants()) == 0 {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800567 m.Disable()
568 }
Inseob Kime498dd92020-08-04 09:24:04 +0900569}
570
571func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
572 return c.Properties.CoreVariantNeeded
573}
574
575func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
576 return c.Properties.RamdiskVariantNeeded
577}
578
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700579func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
580 return c.Properties.VendorRamdiskVariantNeeded
581}
582
Inseob Kim08758f02021-04-08 21:13:22 +0900583func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
584 return false
585}
586
Inseob Kime498dd92020-08-04 09:24:04 +0900587func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
588 return c.Properties.RecoveryVariantNeeded
589}
590
591func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200592 return c.Properties.ExtraVersionedImageVariations
Inseob Kime498dd92020-08-04 09:24:04 +0900593}
594
Justin Yun63e9ec72020-10-29 16:49:43 +0900595func squashVendorSrcs(m *Module) {
596 if lib, ok := m.compiler.(*libraryDecorator); ok {
597 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
598 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
599
600 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
601 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
602
603 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
604 lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900605
606 if lib.Properties.Target.Vendor.No_stubs {
607 proptools.Clear(&lib.Properties.Stubs)
608 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900609 }
610}
611
612func squashProductSrcs(m *Module) {
613 if lib, ok := m.compiler.(*libraryDecorator); ok {
614 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
615 lib.baseCompiler.Properties.Target.Product.Srcs...)
616
617 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
618 lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
619
620 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
621 lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900622
623 if lib.Properties.Target.Product.No_stubs {
624 proptools.Clear(&lib.Properties.Stubs)
625 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900626 }
627}
628
629func squashRecoverySrcs(m *Module) {
630 if lib, ok := m.compiler.(*libraryDecorator); ok {
631 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
632 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
633
634 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
635 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
636
637 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
638 lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
639 }
640}
641
642func squashVendorRamdiskSrcs(m *Module) {
643 if lib, ok := m.compiler.(*libraryDecorator); ok {
644 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
645 }
646}
647
Christopher Ferrise0202c42023-07-27 17:06:46 -0700648func squashRamdiskSrcs(m *Module) {
649 if lib, ok := m.compiler.(*libraryDecorator); ok {
650 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Ramdisk.Exclude_srcs...)
651 }
652}
653
Inseob Kime498dd92020-08-04 09:24:04 +0900654func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
655 m := module.(*Module)
Yifan Hong6da33c22020-10-27 15:01:21 -0700656 if variant == android.RamdiskVariation {
Inseob Kime498dd92020-08-04 09:24:04 +0900657 m.MakeAsPlatform()
Christopher Ferrise0202c42023-07-27 17:06:46 -0700658 squashRamdiskSrcs(m)
Yifan Hong6da33c22020-10-27 15:01:21 -0700659 } else if variant == android.VendorRamdiskVariation {
660 m.MakeAsPlatform()
661 squashVendorRamdiskSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900662 } else if variant == android.RecoveryVariation {
663 m.MakeAsPlatform()
664 squashRecoverySrcs(m)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900665 } else if strings.HasPrefix(variant, VendorVariation) {
666 m.Properties.ImageVariation = VendorVariation
667
668 if strings.HasPrefix(variant, VendorVariationPrefix) {
669 m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
670 }
Inseob Kime498dd92020-08-04 09:24:04 +0900671 squashVendorSrcs(m)
672
673 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
674 // Hide other vendor variants to avoid collision.
675 vndkVersion := ctx.DeviceConfig().VndkVersion()
676 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
677 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800678 m.HideFromMake()
Inseob Kime498dd92020-08-04 09:24:04 +0900679 }
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900680 } else if strings.HasPrefix(variant, ProductVariation) {
681 m.Properties.ImageVariation = ProductVariation
682 if strings.HasPrefix(variant, ProductVariationPrefix) {
683 m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
684 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900685 squashProductSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900686 }
Colin Cross5271fea2021-04-27 13:06:04 -0700687
688 if c.NeedsVendorPublicLibraryVariants() &&
689 (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) {
690 c.VendorProperties.IsVendorPublicLibrary = true
691 }
Inseob Kime498dd92020-08-04 09:24:04 +0900692}