blob: f8c5ca5f1942e6755c9c7a0d74edcac4e5d56bf3 [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
Justin Yundee806f2021-05-18 23:10:00 +0900431 needVndkVersionVendorVariantForLlndk := false
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900432
Colin Cross203b4212021-04-26 17:19:41 -0700433 if m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800434 // This is an LLNDK library. The implementation of the library will be on /system,
435 // and vendor and product variants will be created with LLNDK stubs.
436 // The LLNDK libraries need vendor variants even if there is no VNDK.
Colin Cross203b4212021-04-26 17:19:41 -0700437 coreVariantNeeded = true
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900438 vendorVariants = append(vendorVariants, "")
439 productVariants = append(productVariants, "")
Justin Yundee806f2021-05-18 23:10:00 +0900440 // Generate vendor variants for boardVndkVersion only if the VNDK snapshot does not
441 // provide the LLNDK stub libraries.
442 if needVndkVersionVendorVariantForLlndk {
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900443 vendorVariants = append(vendorVariants, "")
Colin Crossb5f6fa62021-01-06 17:05:04 -0800444 }
Colin Cross5271fea2021-04-27 13:06:04 -0700445 } else if m.NeedsVendorPublicLibraryVariants() {
446 // A vendor public library has the implementation on /vendor, with stub variants
447 // for system and product.
448 coreVariantNeeded = true
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900449 vendorVariants = append(vendorVariants, "")
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900450 productVariants = append(productVariants, "")
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400451 } else if m.IsSnapshotPrebuilt() {
Inseob Kime498dd92020-08-04 09:24:04 +0900452 // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
453 // PRODUCT_EXTRA_VNDK_VERSIONS.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400454 if m.InstallInRecovery() {
455 recoveryVariantNeeded = true
Inseob Kime498dd92020-08-04 09:24:04 +0900456 } else {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400457 vendorVariants = append(vendorVariants, m.SnapshotVersion(mctx))
Inseob Kime498dd92020-08-04 09:24:04 +0900458 }
Ivan Lozanof9e21722020-12-02 09:00:51 -0500459 } else if m.HasNonSystemVariants() && !m.IsVndkExt() {
Justin Yun63e9ec72020-10-29 16:49:43 +0900460 // This will be available to /system unless it is product_specific
461 // which will be handled later.
Inseob Kime498dd92020-08-04 09:24:04 +0900462 coreVariantNeeded = true
463
464 // We assume that modules under proprietary paths are compatible for
465 // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or
466 // PLATFORM_VNDK_VERSION.
Justin Yun63e9ec72020-10-29 16:49:43 +0900467 if m.HasVendorVariant() {
Kiyoung Kim37693d02024-04-04 09:56:15 +0900468 vendorVariants = append(vendorVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900469 }
470
Justin Yun6977e8a2020-10-29 18:24:11 +0900471 // product_available modules are available to /product.
472 if m.HasProductVariant() {
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900473 productVariants = append(productVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900474 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400475 } else if vendorSpecific && m.SdkVersion() == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900476 // This will be available in /vendor (or /odm) only
Kiyoung Kim4e765b12024-04-04 17:33:42 +0900477 vendorVariants = append(vendorVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900478 } else {
479 // This is either in /system (or similar: /data), or is a
jiajia tangcd1c27b2022-07-21 18:04:37 +0800480 // module built with the NDK. Modules built with the NDK
Inseob Kime498dd92020-08-04 09:24:04 +0900481 // will be restricted using the existing link type checks.
482 coreVariantNeeded = true
483 }
484
Justin Yunaf1fde42023-09-27 16:22:10 +0900485 if coreVariantNeeded && productSpecific && m.SdkVersion() == "" {
486 // The module has "product_specific: true" that does not create core variant.
487 coreVariantNeeded = false
Kiyoung Kimfa13ff12024-03-18 16:01:19 +0900488 productVariants = append(productVariants, "")
Inseob Kime498dd92020-08-04 09:24:04 +0900489 }
490
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400491 if m.RamdiskAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900492 ramdiskVariantNeeded = true
493 }
494
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400495 if m.AndroidModuleBase().InstallInRamdisk() {
Inseob Kime498dd92020-08-04 09:24:04 +0900496 ramdiskVariantNeeded = true
497 coreVariantNeeded = false
498 }
499
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400500 if m.VendorRamdiskAvailable() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700501 vendorRamdiskVariantNeeded = true
502 }
503
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400504 if m.AndroidModuleBase().InstallInVendorRamdisk() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700505 vendorRamdiskVariantNeeded = true
506 coreVariantNeeded = false
507 }
508
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400509 if m.RecoveryAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900510 recoveryVariantNeeded = true
511 }
512
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400513 if m.AndroidModuleBase().InstallInRecovery() {
Inseob Kime498dd92020-08-04 09:24:04 +0900514 recoveryVariantNeeded = true
515 coreVariantNeeded = false
516 }
517
518 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900519 if variant == "" {
520 m.AppendExtraVariant(VendorVariation)
521 } else {
522 m.AppendExtraVariant(VendorVariationPrefix + variant)
523 }
Inseob Kime498dd92020-08-04 09:24:04 +0900524 }
525
526 for _, variant := range android.FirstUniqueStrings(productVariants) {
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900527 if variant == "" {
528 m.AppendExtraVariant(ProductVariation)
529 } else {
530 m.AppendExtraVariant(ProductVariationPrefix + variant)
531 }
Inseob Kime498dd92020-08-04 09:24:04 +0900532 }
533
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400534 m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
535 m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
536 m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
537 m.SetCoreVariantNeeded(coreVariantNeeded)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800538
539 // Disable the module if no variants are needed.
540 if !ramdiskVariantNeeded &&
541 !recoveryVariantNeeded &&
542 !coreVariantNeeded &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400543 len(m.ExtraVariants()) == 0 {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800544 m.Disable()
545 }
Inseob Kime498dd92020-08-04 09:24:04 +0900546}
547
548func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
549 return c.Properties.CoreVariantNeeded
550}
551
552func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
553 return c.Properties.RamdiskVariantNeeded
554}
555
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700556func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
557 return c.Properties.VendorRamdiskVariantNeeded
558}
559
Inseob Kim08758f02021-04-08 21:13:22 +0900560func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
561 return false
562}
563
Inseob Kime498dd92020-08-04 09:24:04 +0900564func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
565 return c.Properties.RecoveryVariantNeeded
566}
567
568func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200569 return c.Properties.ExtraVersionedImageVariations
Inseob Kime498dd92020-08-04 09:24:04 +0900570}
571
Justin Yun63e9ec72020-10-29 16:49:43 +0900572func squashVendorSrcs(m *Module) {
573 if lib, ok := m.compiler.(*libraryDecorator); ok {
574 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
575 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
576
577 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
578 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
579
580 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
581 lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900582
583 if lib.Properties.Target.Vendor.No_stubs {
584 proptools.Clear(&lib.Properties.Stubs)
585 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900586 }
587}
588
589func squashProductSrcs(m *Module) {
590 if lib, ok := m.compiler.(*libraryDecorator); ok {
591 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
592 lib.baseCompiler.Properties.Target.Product.Srcs...)
593
594 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
595 lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
596
597 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
598 lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900599
600 if lib.Properties.Target.Product.No_stubs {
601 proptools.Clear(&lib.Properties.Stubs)
602 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900603 }
604}
605
606func squashRecoverySrcs(m *Module) {
607 if lib, ok := m.compiler.(*libraryDecorator); ok {
608 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
609 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
610
611 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
612 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
613
614 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
615 lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
616 }
617}
618
619func squashVendorRamdiskSrcs(m *Module) {
620 if lib, ok := m.compiler.(*libraryDecorator); ok {
621 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
622 }
623}
624
Christopher Ferrise0202c42023-07-27 17:06:46 -0700625func squashRamdiskSrcs(m *Module) {
626 if lib, ok := m.compiler.(*libraryDecorator); ok {
627 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Ramdisk.Exclude_srcs...)
628 }
629}
630
Inseob Kime498dd92020-08-04 09:24:04 +0900631func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
632 m := module.(*Module)
Yifan Hong6da33c22020-10-27 15:01:21 -0700633 if variant == android.RamdiskVariation {
Inseob Kime498dd92020-08-04 09:24:04 +0900634 m.MakeAsPlatform()
Christopher Ferrise0202c42023-07-27 17:06:46 -0700635 squashRamdiskSrcs(m)
Yifan Hong6da33c22020-10-27 15:01:21 -0700636 } else if variant == android.VendorRamdiskVariation {
637 m.MakeAsPlatform()
638 squashVendorRamdiskSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900639 } else if variant == android.RecoveryVariation {
640 m.MakeAsPlatform()
641 squashRecoverySrcs(m)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900642 } else if strings.HasPrefix(variant, VendorVariation) {
643 m.Properties.ImageVariation = VendorVariation
644
645 if strings.HasPrefix(variant, VendorVariationPrefix) {
646 m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
647 }
Inseob Kime498dd92020-08-04 09:24:04 +0900648 squashVendorSrcs(m)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900649 } else if strings.HasPrefix(variant, ProductVariation) {
650 m.Properties.ImageVariation = ProductVariation
651 if strings.HasPrefix(variant, ProductVariationPrefix) {
652 m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
653 }
Justin Yun6977e8a2020-10-29 18:24:11 +0900654 squashProductSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900655 }
Colin Cross5271fea2021-04-27 13:06:04 -0700656
657 if c.NeedsVendorPublicLibraryVariants() &&
658 (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) {
659 c.VendorProperties.IsVendorPublicLibrary = true
660 }
Inseob Kime498dd92020-08-04 09:24:04 +0900661}