blob: 4c0c72280059260209979d1d4b010cdc31224480 [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 (
45 // VendorVariationPrefix is the variant prefix used for /vendor code that compiles
46 // against the VNDK.
47 VendorVariationPrefix = "vendor."
48
49 // ProductVariationPrefix is the variant prefix used for /product code that compiles
50 // against the VNDK.
51 ProductVariationPrefix = "product."
52)
53
Inseob Kime498dd92020-08-04 09:24:04 +090054func (ctx *moduleContextImpl) inProduct() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -050055 return ctx.mod.InProduct()
Inseob Kime498dd92020-08-04 09:24:04 +090056}
57
58func (ctx *moduleContextImpl) inVendor() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050059 return ctx.mod.InVendor()
Inseob Kime498dd92020-08-04 09:24:04 +090060}
61
62func (ctx *moduleContextImpl) inRamdisk() bool {
63 return ctx.mod.InRamdisk()
64}
65
Yifan Hong60e0cfb2020-10-21 15:17:56 -070066func (ctx *moduleContextImpl) inVendorRamdisk() bool {
67 return ctx.mod.InVendorRamdisk()
68}
69
Inseob Kime498dd92020-08-04 09:24:04 +090070func (ctx *moduleContextImpl) inRecovery() bool {
71 return ctx.mod.InRecovery()
72}
73
Colin Crossea30d852023-11-29 16:00:16 -080074func (c *Module) InstallInProduct() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090075 // Additionally check if this module is inProduct() that means it is a "product" variant of a
76 // module. As well as product specific modules, product variants must be installed to /product.
77 return c.InProduct()
78}
79
Colin Crossea30d852023-11-29 16:00:16 -080080func (c *Module) InstallInVendor() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090081 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
82 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
83 // unless they have "odm_available: true".
84 return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm()
85}
86
Colin Crossea30d852023-11-29 16:00:16 -080087func (c *Module) InstallInOdm() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090088 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
89 return c.InVendor() && c.VendorVariantToOdm()
90}
91
Justin Yun63e9ec72020-10-29 16:49:43 +090092// Returns true when this module is configured to have core and vendor variants.
Inseob Kime498dd92020-08-04 09:24:04 +090093func (c *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +090094 return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available)
95}
96
97// Returns true when this module creates a vendor variant and wants to install the vendor variant
98// to the odm partition.
99func (c *Module) VendorVariantToOdm() bool {
100 return Bool(c.VendorProperties.Odm_available)
Inseob Kime498dd92020-08-04 09:24:04 +0900101}
102
Justin Yun63e9ec72020-10-29 16:49:43 +0900103// Returns true when this module is configured to have core and product variants.
104func (c *Module) HasProductVariant() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900105 return Bool(c.VendorProperties.Product_available)
Justin Yun63e9ec72020-10-29 16:49:43 +0900106}
107
108// Returns true when this module is configured to have core and either product or vendor variants.
109func (c *Module) HasNonSystemVariants() bool {
Justin Yun6977e8a2020-10-29 18:24:11 +0900110 return c.HasVendorVariant() || c.HasProductVariant()
Justin Yun63e9ec72020-10-29 16:49:43 +0900111}
112
Inseob Kime498dd92020-08-04 09:24:04 +0900113// Returns true if the module is "product" variant. Usually these modules are installed in /product
Ivan Lozanof9e21722020-12-02 09:00:51 -0500114func (c *Module) InProduct() bool {
Inseob Kime498dd92020-08-04 09:24:04 +0900115 return c.Properties.ImageVariationPrefix == ProductVariationPrefix
116}
117
118// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500119func (c *Module) InVendor() bool {
Inseob Kime498dd92020-08-04 09:24:04 +0900120 return c.Properties.ImageVariationPrefix == VendorVariationPrefix
121}
122
123func (c *Module) InRamdisk() bool {
124 return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk()
125}
126
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700127func (c *Module) InVendorRamdisk() bool {
128 return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk()
129}
130
Inseob Kime498dd92020-08-04 09:24:04 +0900131func (c *Module) InRecovery() bool {
132 return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery()
133}
134
135func (c *Module) OnlyInRamdisk() bool {
136 return c.ModuleBase.InstallInRamdisk()
137}
138
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700139func (c *Module) OnlyInVendorRamdisk() bool {
140 return c.ModuleBase.InstallInVendorRamdisk()
141}
142
Inseob Kime498dd92020-08-04 09:24:04 +0900143func (c *Module) OnlyInRecovery() bool {
144 return c.ModuleBase.InstallInRecovery()
145}
146
Justin Yun6977e8a2020-10-29 18:24:11 +0900147func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool {
148 if v.Kind() != reflect.Struct {
149 return true
150 }
151 for i := 0; i < v.NumField(); i++ {
152 prop := v.Field(i)
153 if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" {
154 vendor_prop := prop.FieldByName("Vendor")
155 product_prop := prop.FieldByName("Product")
156 if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct {
157 // Neither Target.Vendor nor Target.Product is defined
158 continue
159 }
160 if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct ||
161 !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) {
162 // If only one of either Target.Vendor or Target.Product is
163 // defined or they have different values, it fails the build
164 // since VNDK must have the same properties for both vendor
165 // and product variants.
166 return false
167 }
168 } else if !visitPropsAndCompareVendorAndProductProps(prop) {
169 // Visit the substructures to find Target.Vendor and Target.Product
170 return false
171 }
172 }
173 return true
174}
175
176// In the case of VNDK, vendor and product variants must have the same properties.
177// VNDK installs only one file and shares it for both vendor and product modules on
178// runtime. We may not define different versions of a VNDK lib for each partition.
179// This function is used only for the VNDK modules that is available to both vendor
180// and product partitions.
181func (c *Module) compareVendorAndProductProps() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900182 if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) {
Justin Yun6977e8a2020-10-29 18:24:11 +0900183 panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name()))
184 }
185 for _, properties := range c.GetProperties() {
186 if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) {
187 return false
188 }
189 }
190 return true
191}
192
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400193// ImageMutatableModule provides a common image mutation interface for LinkableInterface modules.
194type ImageMutatableModule interface {
195 android.Module
196 LinkableInterface
197
198 // AndroidModuleBase returns the android.ModuleBase for this module
199 AndroidModuleBase() *android.ModuleBase
200
201 // VendorAvailable returns true if this module is available on the vendor image.
202 VendorAvailable() bool
203
204 // OdmAvailable returns true if this module is available on the odm image.
205 OdmAvailable() bool
206
207 // ProductAvailable returns true if this module is available on the product image.
208 ProductAvailable() bool
209
210 // RamdiskAvailable returns true if this module is available on the ramdisk image.
211 RamdiskAvailable() bool
212
213 // RecoveryAvailable returns true if this module is available on the recovery image.
214 RecoveryAvailable() bool
215
216 // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image.
217 VendorRamdiskAvailable() bool
218
219 // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt.
220 IsSnapshotPrebuilt() bool
221
222 // SnapshotVersion returns the snapshot version for this module.
223 SnapshotVersion(mctx android.BaseModuleContext) string
224
225 // SdkVersion returns the SDK version for this module.
226 SdkVersion() string
227
228 // ExtraVariants returns the list of extra variants this module requires.
229 ExtraVariants() []string
230
231 // AppendExtraVariant returns an extra variant to the list of extra variants this module requires.
232 AppendExtraVariant(extraVariant string)
233
234 // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed.
235 SetRamdiskVariantNeeded(b bool)
236
237 // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed.
238 SetVendorRamdiskVariantNeeded(b bool)
239
240 // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed.
241 SetRecoveryVariantNeeded(b bool)
242
243 // SetCoreVariantNeeded sets whether the Core Variant is needed.
244 SetCoreVariantNeeded(b bool)
245}
246
247var _ ImageMutatableModule = (*Module)(nil)
248
Inseob Kime498dd92020-08-04 09:24:04 +0900249func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400250 m.CheckVndkProperties(mctx)
251 MutateImage(mctx, m)
252}
253
254// CheckVndkProperties checks whether the VNDK-related properties are set correctly.
255// If properties are not set correctly, results in a module context property error.
256func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) {
Inseob Kime498dd92020-08-04 09:24:04 +0900257 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
258 productSpecific := mctx.ProductSpecific()
259
Inseob Kime498dd92020-08-04 09:24:04 +0900260 if vndkdep := m.vndkdep; vndkdep != nil {
261 if vndkdep.isVndk() {
262 if vendorSpecific || productSpecific {
263 if !vndkdep.isVndkExt() {
264 mctx.PropertyErrorf("vndk",
265 "must set `extends: \"...\"` to vndk extension")
Justin Yunc0d8c492021-01-07 17:45:31 +0900266 } else if Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900267 mctx.PropertyErrorf("vendor_available",
268 "must not set at the same time as `vndk: {extends: \"...\"}`")
Justin Yunc0d8c492021-01-07 17:45:31 +0900269 } else if Bool(m.VendorProperties.Product_available) {
Justin Yun63e9ec72020-10-29 16:49:43 +0900270 mctx.PropertyErrorf("product_available",
271 "must not set at the same time as `vndk: {extends: \"...\"}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900272 }
273 } else {
274 if vndkdep.isVndkExt() {
275 mctx.PropertyErrorf("vndk",
276 "must set `vendor: true` or `product_specific: true` to set `extends: %q`",
277 m.getVndkExtendsModuleName())
278 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900279 if !Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900280 mctx.PropertyErrorf("vndk",
Justin Yunc0d8c492021-01-07 17:45:31 +0900281 "vendor_available must be set to true when `vndk: {enabled: true}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900282 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900283 if Bool(m.VendorProperties.Product_available) {
Justin Yunfd9e8042020-12-23 18:23:14 +0900284 // If a VNDK module creates both product and vendor variants, they
285 // must have the same properties since they share a single VNDK
286 // library on runtime.
Justin Yun6977e8a2020-10-29 18:24:11 +0900287 if !m.compareVendorAndProductProps() {
288 mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules")
289 }
290 }
Inseob Kime498dd92020-08-04 09:24:04 +0900291 }
292 } else {
293 if vndkdep.isVndkSp() {
294 mctx.PropertyErrorf("vndk",
295 "must set `enabled: true` to set `support_system_process: true`")
296 }
297 if vndkdep.isVndkExt() {
298 mctx.PropertyErrorf("vndk",
299 "must set `enabled: true` to set `extends: %q`",
300 m.getVndkExtendsModuleName())
301 }
302 }
303 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400304}
305
306func (m *Module) VendorAvailable() bool {
307 return Bool(m.VendorProperties.Vendor_available)
308}
309
310func (m *Module) OdmAvailable() bool {
311 return Bool(m.VendorProperties.Odm_available)
312}
313
314func (m *Module) ProductAvailable() bool {
315 return Bool(m.VendorProperties.Product_available)
316}
317
318func (m *Module) RamdiskAvailable() bool {
319 return Bool(m.Properties.Ramdisk_available)
320}
321
322func (m *Module) VendorRamdiskAvailable() bool {
323 return Bool(m.Properties.Vendor_ramdisk_available)
324}
325
326func (m *Module) AndroidModuleBase() *android.ModuleBase {
327 return &m.ModuleBase
328}
329
330func (m *Module) RecoveryAvailable() bool {
331 return Bool(m.Properties.Recovery_available)
332}
333
334func (m *Module) ExtraVariants() []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200335 return m.Properties.ExtraVersionedImageVariations
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400336}
337
338func (m *Module) AppendExtraVariant(extraVariant string) {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200339 m.Properties.ExtraVersionedImageVariations = append(m.Properties.ExtraVersionedImageVariations, extraVariant)
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400340}
341
342func (m *Module) SetRamdiskVariantNeeded(b bool) {
343 m.Properties.RamdiskVariantNeeded = b
344}
345
346func (m *Module) SetVendorRamdiskVariantNeeded(b bool) {
347 m.Properties.VendorRamdiskVariantNeeded = b
348}
349
350func (m *Module) SetRecoveryVariantNeeded(b bool) {
351 m.Properties.RecoveryVariantNeeded = b
352}
353
354func (m *Module) SetCoreVariantNeeded(b bool) {
355 m.Properties.CoreVariantNeeded = b
356}
357
358func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozanod1dec542021-05-26 15:33:11 -0400359 if snapshot, ok := m.linker.(SnapshotInterface); ok {
360 return snapshot.Version()
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400361 } else {
362 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
363 // Should we be panicking here instead?
364 return ""
365 }
366}
367
368func (m *Module) KernelHeadersDecorator() bool {
369 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
370 return true
371 }
372 return false
373}
374
375// MutateImage handles common image mutations for ImageMutatableModule interfaces.
376func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) {
377 // Validation check
378 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
379 productSpecific := mctx.ProductSpecific()
380
381 if m.VendorAvailable() {
382 if vendorSpecific {
383 mctx.PropertyErrorf("vendor_available",
384 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
385 }
386 if m.OdmAvailable() {
387 mctx.PropertyErrorf("vendor_available",
388 "doesn't make sense at the same time as `odm_available: true`")
389 }
390 }
391
392 if m.OdmAvailable() {
393 if vendorSpecific {
394 mctx.PropertyErrorf("odm_available",
395 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
396 }
397 }
398
399 if m.ProductAvailable() {
400 if productSpecific {
401 mctx.PropertyErrorf("product_available",
402 "doesn't make sense at the same time as `product_specific: true`")
403 }
404 if vendorSpecific {
405 mctx.PropertyErrorf("product_available",
406 "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`")
407 }
408 }
Inseob Kime498dd92020-08-04 09:24:04 +0900409
410 var coreVariantNeeded bool = false
411 var ramdiskVariantNeeded bool = false
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700412 var vendorRamdiskVariantNeeded bool = false
Inseob Kime498dd92020-08-04 09:24:04 +0900413 var recoveryVariantNeeded bool = false
414
415 var vendorVariants []string
416 var productVariants []string
417
418 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
419 boardVndkVersion := mctx.DeviceConfig().VndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800420 recoverySnapshotVersion := mctx.DeviceConfig().RecoverySnapshotVersion()
421 usingRecoverySnapshot := recoverySnapshotVersion != "current" &&
422 recoverySnapshotVersion != ""
Justin Yundee806f2021-05-18 23:10:00 +0900423 needVndkVersionVendorVariantForLlndk := false
424 if boardVndkVersion != "" {
425 boardVndkApiLevel, err := android.ApiLevelFromUser(mctx, boardVndkVersion)
426 if err == nil && !boardVndkApiLevel.IsPreview() {
427 // VNDK snapshot newer than v30 has LLNDK stub libraries.
428 // Only the VNDK version less than or equal to v30 requires generating the vendor
429 // variant of the VNDK version from the source tree.
430 needVndkVersionVendorVariantForLlndk = boardVndkApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(mctx, "30"))
431 }
432 }
Inseob Kime498dd92020-08-04 09:24:04 +0900433 if boardVndkVersion == "current" {
434 boardVndkVersion = platformVndkVersion
435 }
Inseob Kime498dd92020-08-04 09:24:04 +0900436
Colin Cross203b4212021-04-26 17:19:41 -0700437 if m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800438 // This is an LLNDK library. The implementation of the library will be on /system,
439 // and vendor and product variants will be created with LLNDK stubs.
440 // The LLNDK libraries need vendor variants even if there is no VNDK.
Colin Cross203b4212021-04-26 17:19:41 -0700441 coreVariantNeeded = true
Colin Crossb5f6fa62021-01-06 17:05:04 -0800442 if platformVndkVersion != "" {
443 vendorVariants = append(vendorVariants, platformVndkVersion)
444 productVariants = append(productVariants, platformVndkVersion)
445 }
Justin Yundee806f2021-05-18 23:10:00 +0900446 // Generate vendor variants for boardVndkVersion only if the VNDK snapshot does not
447 // provide the LLNDK stub libraries.
448 if needVndkVersionVendorVariantForLlndk {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800449 vendorVariants = append(vendorVariants, boardVndkVersion)
450 }
Colin Cross5271fea2021-04-27 13:06:04 -0700451 } else if m.NeedsVendorPublicLibraryVariants() {
452 // A vendor public library has the implementation on /vendor, with stub variants
453 // for system and product.
454 coreVariantNeeded = true
455 vendorVariants = append(vendorVariants, boardVndkVersion)
456 if platformVndkVersion != "" {
457 productVariants = append(productVariants, platformVndkVersion)
458 }
Colin Crossb5f6fa62021-01-06 17:05:04 -0800459 } else if boardVndkVersion == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900460 // If the device isn't compiling against the VNDK, we always
461 // use the core mode.
462 coreVariantNeeded = true
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) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400560 m.AppendExtraVariant(VendorVariationPrefix + variant)
Inseob Kime498dd92020-08-04 09:24:04 +0900561 }
562
563 for _, variant := range android.FirstUniqueStrings(productVariants) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400564 m.AppendExtraVariant(ProductVariationPrefix + variant)
Inseob Kime498dd92020-08-04 09:24:04 +0900565 }
566
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400567 m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
568 m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
569 m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
570 m.SetCoreVariantNeeded(coreVariantNeeded)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800571
572 // Disable the module if no variants are needed.
573 if !ramdiskVariantNeeded &&
574 !recoveryVariantNeeded &&
575 !coreVariantNeeded &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400576 len(m.ExtraVariants()) == 0 {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800577 m.Disable()
578 }
Inseob Kime498dd92020-08-04 09:24:04 +0900579}
580
581func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
582 return c.Properties.CoreVariantNeeded
583}
584
585func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
586 return c.Properties.RamdiskVariantNeeded
587}
588
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700589func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
590 return c.Properties.VendorRamdiskVariantNeeded
591}
592
Inseob Kim08758f02021-04-08 21:13:22 +0900593func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
594 return false
595}
596
Inseob Kime498dd92020-08-04 09:24:04 +0900597func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
598 return c.Properties.RecoveryVariantNeeded
599}
600
601func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
Lukacs T. Berki2f5c3402021-06-15 11:27:56 +0200602 return c.Properties.ExtraVersionedImageVariations
Inseob Kime498dd92020-08-04 09:24:04 +0900603}
604
Justin Yun63e9ec72020-10-29 16:49:43 +0900605func squashVendorSrcs(m *Module) {
606 if lib, ok := m.compiler.(*libraryDecorator); ok {
607 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
608 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
609
610 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
611 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
612
613 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
614 lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900615
616 if lib.Properties.Target.Vendor.No_stubs {
617 proptools.Clear(&lib.Properties.Stubs)
618 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900619 }
620}
621
622func squashProductSrcs(m *Module) {
623 if lib, ok := m.compiler.(*libraryDecorator); ok {
624 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
625 lib.baseCompiler.Properties.Target.Product.Srcs...)
626
627 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
628 lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
629
630 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
631 lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
Jooyung Han85707de2023-12-01 14:21:13 +0900632
633 if lib.Properties.Target.Product.No_stubs {
634 proptools.Clear(&lib.Properties.Stubs)
635 }
Justin Yun63e9ec72020-10-29 16:49:43 +0900636 }
637}
638
639func squashRecoverySrcs(m *Module) {
640 if lib, ok := m.compiler.(*libraryDecorator); ok {
641 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
642 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
643
644 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
645 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
646
647 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
648 lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
649 }
650}
651
652func squashVendorRamdiskSrcs(m *Module) {
653 if lib, ok := m.compiler.(*libraryDecorator); ok {
654 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
655 }
656}
657
Christopher Ferrise0202c42023-07-27 17:06:46 -0700658func squashRamdiskSrcs(m *Module) {
659 if lib, ok := m.compiler.(*libraryDecorator); ok {
660 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Ramdisk.Exclude_srcs...)
661 }
662}
663
Inseob Kime498dd92020-08-04 09:24:04 +0900664func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
665 m := module.(*Module)
Yifan Hong6da33c22020-10-27 15:01:21 -0700666 if variant == android.RamdiskVariation {
Inseob Kime498dd92020-08-04 09:24:04 +0900667 m.MakeAsPlatform()
Christopher Ferrise0202c42023-07-27 17:06:46 -0700668 squashRamdiskSrcs(m)
Yifan Hong6da33c22020-10-27 15:01:21 -0700669 } else if variant == android.VendorRamdiskVariation {
670 m.MakeAsPlatform()
671 squashVendorRamdiskSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900672 } else if variant == android.RecoveryVariation {
673 m.MakeAsPlatform()
674 squashRecoverySrcs(m)
675 } else if strings.HasPrefix(variant, VendorVariationPrefix) {
676 m.Properties.ImageVariationPrefix = VendorVariationPrefix
677 m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
678 squashVendorSrcs(m)
679
680 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
681 // Hide other vendor variants to avoid collision.
682 vndkVersion := ctx.DeviceConfig().VndkVersion()
683 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
684 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800685 m.HideFromMake()
Inseob Kime498dd92020-08-04 09:24:04 +0900686 }
687 } else if strings.HasPrefix(variant, ProductVariationPrefix) {
688 m.Properties.ImageVariationPrefix = ProductVariationPrefix
689 m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
Justin Yun6977e8a2020-10-29 18:24:11 +0900690 squashProductSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900691 }
Colin Cross5271fea2021-04-27 13:06:04 -0700692
693 if c.NeedsVendorPublicLibraryVariants() &&
694 (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) {
695 c.VendorProperties.IsVendorPublicLibrary = true
696 }
Inseob Kime498dd92020-08-04 09:24:04 +0900697}