blob: b85379849638542b958ce8942c0a8510a88ca468 [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//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
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"
25)
26
27var _ android.ImageInterface = (*Module)(nil)
28
Ivan Lozano3968d8f2020-12-14 11:27:52 -050029type ImageVariantType string
Inseob Kim74d25562020-08-04 00:41:38 +090030
31const (
Ivan Lozano3968d8f2020-12-14 11:27:52 -050032 coreImageVariant ImageVariantType = "core"
33 vendorImageVariant ImageVariantType = "vendor"
34 productImageVariant ImageVariantType = "product"
35 ramdiskImageVariant ImageVariantType = "ramdisk"
36 vendorRamdiskImageVariant ImageVariantType = "vendor_ramdisk"
37 recoveryImageVariant ImageVariantType = "recovery"
38 hostImageVariant ImageVariantType = "host"
Inseob Kim74d25562020-08-04 00:41:38 +090039)
40
Inseob Kime498dd92020-08-04 09:24:04 +090041const (
42 // VendorVariationPrefix is the variant prefix used for /vendor code that compiles
43 // against the VNDK.
44 VendorVariationPrefix = "vendor."
45
46 // ProductVariationPrefix is the variant prefix used for /product code that compiles
47 // against the VNDK.
48 ProductVariationPrefix = "product."
49)
50
51func (ctx *moduleContext) ProductSpecific() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090052 return ctx.ModuleContext.ProductSpecific() || ctx.mod.productSpecificModuleContext()
Inseob Kime498dd92020-08-04 09:24:04 +090053}
54
55func (ctx *moduleContext) SocSpecific() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090056 return ctx.ModuleContext.SocSpecific() || ctx.mod.socSpecificModuleContext()
Justin Yunebcf0c52021-01-08 18:00:19 +090057}
58
59func (ctx *moduleContext) DeviceSpecific() bool {
Justin Yun7f99ec72021-04-12 13:19:28 +090060 return ctx.ModuleContext.DeviceSpecific() || ctx.mod.deviceSpecificModuleContext()
Inseob Kime498dd92020-08-04 09:24:04 +090061}
62
63func (ctx *moduleContextImpl) inProduct() bool {
Ivan Lozanof9e21722020-12-02 09:00:51 -050064 return ctx.mod.InProduct()
Inseob Kime498dd92020-08-04 09:24:04 +090065}
66
67func (ctx *moduleContextImpl) inVendor() bool {
Ivan Lozano3968d8f2020-12-14 11:27:52 -050068 return ctx.mod.InVendor()
Inseob Kime498dd92020-08-04 09:24:04 +090069}
70
71func (ctx *moduleContextImpl) inRamdisk() bool {
72 return ctx.mod.InRamdisk()
73}
74
Yifan Hong60e0cfb2020-10-21 15:17:56 -070075func (ctx *moduleContextImpl) inVendorRamdisk() bool {
76 return ctx.mod.InVendorRamdisk()
77}
78
Inseob Kime498dd92020-08-04 09:24:04 +090079func (ctx *moduleContextImpl) inRecovery() bool {
80 return ctx.mod.InRecovery()
81}
82
Justin Yun7f99ec72021-04-12 13:19:28 +090083func (c *Module) productSpecificModuleContext() bool {
84 // Additionally check if this module is inProduct() that means it is a "product" variant of a
85 // module. As well as product specific modules, product variants must be installed to /product.
86 return c.InProduct()
87}
88
89func (c *Module) socSpecificModuleContext() bool {
90 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
91 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
92 // unless they have "odm_available: true".
93 return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm()
94}
95
96func (c *Module) deviceSpecificModuleContext() bool {
97 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
98 return c.InVendor() && c.VendorVariantToOdm()
99}
100
Justin Yun63e9ec72020-10-29 16:49:43 +0900101// Returns true when this module is configured to have core and vendor variants.
Inseob Kime498dd92020-08-04 09:24:04 +0900102func (c *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900103 return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available)
104}
105
106// Returns true when this module creates a vendor variant and wants to install the vendor variant
107// to the odm partition.
108func (c *Module) VendorVariantToOdm() bool {
109 return Bool(c.VendorProperties.Odm_available)
Inseob Kime498dd92020-08-04 09:24:04 +0900110}
111
Justin Yun63e9ec72020-10-29 16:49:43 +0900112// Returns true when this module is configured to have core and product variants.
113func (c *Module) HasProductVariant() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900114 return Bool(c.VendorProperties.Product_available)
Justin Yun63e9ec72020-10-29 16:49:43 +0900115}
116
117// Returns true when this module is configured to have core and either product or vendor variants.
118func (c *Module) HasNonSystemVariants() bool {
Justin Yun6977e8a2020-10-29 18:24:11 +0900119 return c.HasVendorVariant() || c.HasProductVariant()
Justin Yun63e9ec72020-10-29 16:49:43 +0900120}
121
Inseob Kime498dd92020-08-04 09:24:04 +0900122// Returns true if the module is "product" variant. Usually these modules are installed in /product
Ivan Lozanof9e21722020-12-02 09:00:51 -0500123func (c *Module) InProduct() bool {
Inseob Kime498dd92020-08-04 09:24:04 +0900124 return c.Properties.ImageVariationPrefix == ProductVariationPrefix
125}
126
127// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
Ivan Lozano3968d8f2020-12-14 11:27:52 -0500128func (c *Module) InVendor() bool {
Inseob Kime498dd92020-08-04 09:24:04 +0900129 return c.Properties.ImageVariationPrefix == VendorVariationPrefix
130}
131
132func (c *Module) InRamdisk() bool {
133 return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk()
134}
135
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700136func (c *Module) InVendorRamdisk() bool {
137 return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk()
138}
139
Inseob Kime498dd92020-08-04 09:24:04 +0900140func (c *Module) InRecovery() bool {
141 return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery()
142}
143
144func (c *Module) OnlyInRamdisk() bool {
145 return c.ModuleBase.InstallInRamdisk()
146}
147
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700148func (c *Module) OnlyInVendorRamdisk() bool {
149 return c.ModuleBase.InstallInVendorRamdisk()
150}
151
Inseob Kime498dd92020-08-04 09:24:04 +0900152func (c *Module) OnlyInRecovery() bool {
153 return c.ModuleBase.InstallInRecovery()
154}
155
Justin Yun6977e8a2020-10-29 18:24:11 +0900156func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool {
157 if v.Kind() != reflect.Struct {
158 return true
159 }
160 for i := 0; i < v.NumField(); i++ {
161 prop := v.Field(i)
162 if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" {
163 vendor_prop := prop.FieldByName("Vendor")
164 product_prop := prop.FieldByName("Product")
165 if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct {
166 // Neither Target.Vendor nor Target.Product is defined
167 continue
168 }
169 if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct ||
170 !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) {
171 // If only one of either Target.Vendor or Target.Product is
172 // defined or they have different values, it fails the build
173 // since VNDK must have the same properties for both vendor
174 // and product variants.
175 return false
176 }
177 } else if !visitPropsAndCompareVendorAndProductProps(prop) {
178 // Visit the substructures to find Target.Vendor and Target.Product
179 return false
180 }
181 }
182 return true
183}
184
185// In the case of VNDK, vendor and product variants must have the same properties.
186// VNDK installs only one file and shares it for both vendor and product modules on
187// runtime. We may not define different versions of a VNDK lib for each partition.
188// This function is used only for the VNDK modules that is available to both vendor
189// and product partitions.
190func (c *Module) compareVendorAndProductProps() bool {
Justin Yunc0d8c492021-01-07 17:45:31 +0900191 if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) {
Justin Yun6977e8a2020-10-29 18:24:11 +0900192 panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name()))
193 }
194 for _, properties := range c.GetProperties() {
195 if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) {
196 return false
197 }
198 }
199 return true
200}
201
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400202// ImageMutatableModule provides a common image mutation interface for LinkableInterface modules.
203type ImageMutatableModule interface {
204 android.Module
205 LinkableInterface
206
207 // AndroidModuleBase returns the android.ModuleBase for this module
208 AndroidModuleBase() *android.ModuleBase
209
210 // VendorAvailable returns true if this module is available on the vendor image.
211 VendorAvailable() bool
212
213 // OdmAvailable returns true if this module is available on the odm image.
214 OdmAvailable() bool
215
216 // ProductAvailable returns true if this module is available on the product image.
217 ProductAvailable() bool
218
219 // RamdiskAvailable returns true if this module is available on the ramdisk image.
220 RamdiskAvailable() bool
221
222 // RecoveryAvailable returns true if this module is available on the recovery image.
223 RecoveryAvailable() bool
224
225 // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image.
226 VendorRamdiskAvailable() bool
227
228 // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt.
229 IsSnapshotPrebuilt() bool
230
231 // SnapshotVersion returns the snapshot version for this module.
232 SnapshotVersion(mctx android.BaseModuleContext) string
233
234 // SdkVersion returns the SDK version for this module.
235 SdkVersion() string
236
237 // ExtraVariants returns the list of extra variants this module requires.
238 ExtraVariants() []string
239
240 // AppendExtraVariant returns an extra variant to the list of extra variants this module requires.
241 AppendExtraVariant(extraVariant string)
242
243 // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed.
244 SetRamdiskVariantNeeded(b bool)
245
246 // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed.
247 SetVendorRamdiskVariantNeeded(b bool)
248
249 // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed.
250 SetRecoveryVariantNeeded(b bool)
251
252 // SetCoreVariantNeeded sets whether the Core Variant is needed.
253 SetCoreVariantNeeded(b bool)
254}
255
256var _ ImageMutatableModule = (*Module)(nil)
257
Inseob Kime498dd92020-08-04 09:24:04 +0900258func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400259 m.CheckVndkProperties(mctx)
260 MutateImage(mctx, m)
261}
262
263// CheckVndkProperties checks whether the VNDK-related properties are set correctly.
264// If properties are not set correctly, results in a module context property error.
265func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) {
Inseob Kime498dd92020-08-04 09:24:04 +0900266 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
267 productSpecific := mctx.ProductSpecific()
268
Inseob Kime498dd92020-08-04 09:24:04 +0900269 if vndkdep := m.vndkdep; vndkdep != nil {
270 if vndkdep.isVndk() {
271 if vendorSpecific || productSpecific {
272 if !vndkdep.isVndkExt() {
273 mctx.PropertyErrorf("vndk",
274 "must set `extends: \"...\"` to vndk extension")
Justin Yunc0d8c492021-01-07 17:45:31 +0900275 } else if Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900276 mctx.PropertyErrorf("vendor_available",
277 "must not set at the same time as `vndk: {extends: \"...\"}`")
Justin Yunc0d8c492021-01-07 17:45:31 +0900278 } else if Bool(m.VendorProperties.Product_available) {
Justin Yun63e9ec72020-10-29 16:49:43 +0900279 mctx.PropertyErrorf("product_available",
280 "must not set at the same time as `vndk: {extends: \"...\"}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900281 }
282 } else {
283 if vndkdep.isVndkExt() {
284 mctx.PropertyErrorf("vndk",
285 "must set `vendor: true` or `product_specific: true` to set `extends: %q`",
286 m.getVndkExtendsModuleName())
287 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900288 if !Bool(m.VendorProperties.Vendor_available) {
Inseob Kime498dd92020-08-04 09:24:04 +0900289 mctx.PropertyErrorf("vndk",
Justin Yunc0d8c492021-01-07 17:45:31 +0900290 "vendor_available must be set to true when `vndk: {enabled: true}`")
Inseob Kime498dd92020-08-04 09:24:04 +0900291 }
Justin Yunc0d8c492021-01-07 17:45:31 +0900292 if Bool(m.VendorProperties.Product_available) {
Justin Yunfd9e8042020-12-23 18:23:14 +0900293 // If a VNDK module creates both product and vendor variants, they
294 // must have the same properties since they share a single VNDK
295 // library on runtime.
Justin Yun6977e8a2020-10-29 18:24:11 +0900296 if !m.compareVendorAndProductProps() {
297 mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules")
298 }
299 }
Inseob Kime498dd92020-08-04 09:24:04 +0900300 }
301 } else {
302 if vndkdep.isVndkSp() {
303 mctx.PropertyErrorf("vndk",
304 "must set `enabled: true` to set `support_system_process: true`")
305 }
306 if vndkdep.isVndkExt() {
307 mctx.PropertyErrorf("vndk",
308 "must set `enabled: true` to set `extends: %q`",
309 m.getVndkExtendsModuleName())
310 }
311 }
312 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400313}
314
315func (m *Module) VendorAvailable() bool {
316 return Bool(m.VendorProperties.Vendor_available)
317}
318
319func (m *Module) OdmAvailable() bool {
320 return Bool(m.VendorProperties.Odm_available)
321}
322
323func (m *Module) ProductAvailable() bool {
324 return Bool(m.VendorProperties.Product_available)
325}
326
327func (m *Module) RamdiskAvailable() bool {
328 return Bool(m.Properties.Ramdisk_available)
329}
330
331func (m *Module) VendorRamdiskAvailable() bool {
332 return Bool(m.Properties.Vendor_ramdisk_available)
333}
334
335func (m *Module) AndroidModuleBase() *android.ModuleBase {
336 return &m.ModuleBase
337}
338
339func (m *Module) RecoveryAvailable() bool {
340 return Bool(m.Properties.Recovery_available)
341}
342
343func (m *Module) ExtraVariants() []string {
344 return m.Properties.ExtraVariants
345}
346
347func (m *Module) AppendExtraVariant(extraVariant string) {
348 m.Properties.ExtraVariants = append(m.Properties.ExtraVariants, extraVariant)
349}
350
351func (m *Module) SetRamdiskVariantNeeded(b bool) {
352 m.Properties.RamdiskVariantNeeded = b
353}
354
355func (m *Module) SetVendorRamdiskVariantNeeded(b bool) {
356 m.Properties.VendorRamdiskVariantNeeded = b
357}
358
359func (m *Module) SetRecoveryVariantNeeded(b bool) {
360 m.Properties.RecoveryVariantNeeded = b
361}
362
363func (m *Module) SetCoreVariantNeeded(b bool) {
364 m.Properties.CoreVariantNeeded = b
365}
366
367func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
368 if snapshot, ok := m.linker.(snapshotInterface); ok {
369 return snapshot.version()
370 } else {
371 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
372 // Should we be panicking here instead?
373 return ""
374 }
375}
376
377func (m *Module) KernelHeadersDecorator() bool {
378 if _, ok := m.linker.(*kernelHeadersDecorator); ok {
379 return true
380 }
381 return false
382}
383
384// MutateImage handles common image mutations for ImageMutatableModule interfaces.
385func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) {
386 // Validation check
387 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
388 productSpecific := mctx.ProductSpecific()
389
390 if m.VendorAvailable() {
391 if vendorSpecific {
392 mctx.PropertyErrorf("vendor_available",
393 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
394 }
395 if m.OdmAvailable() {
396 mctx.PropertyErrorf("vendor_available",
397 "doesn't make sense at the same time as `odm_available: true`")
398 }
399 }
400
401 if m.OdmAvailable() {
402 if vendorSpecific {
403 mctx.PropertyErrorf("odm_available",
404 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
405 }
406 }
407
408 if m.ProductAvailable() {
409 if productSpecific {
410 mctx.PropertyErrorf("product_available",
411 "doesn't make sense at the same time as `product_specific: true`")
412 }
413 if vendorSpecific {
414 mctx.PropertyErrorf("product_available",
415 "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`")
416 }
417 }
Inseob Kime498dd92020-08-04 09:24:04 +0900418
419 var coreVariantNeeded bool = false
420 var ramdiskVariantNeeded bool = false
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700421 var vendorRamdiskVariantNeeded bool = false
Inseob Kime498dd92020-08-04 09:24:04 +0900422 var recoveryVariantNeeded bool = false
423
424 var vendorVariants []string
425 var productVariants []string
426
427 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
428 boardVndkVersion := mctx.DeviceConfig().VndkVersion()
429 productVndkVersion := mctx.DeviceConfig().ProductVndkVersion()
Jose Galmes6f843bc2020-12-11 13:36:29 -0800430 recoverySnapshotVersion := mctx.DeviceConfig().RecoverySnapshotVersion()
431 usingRecoverySnapshot := recoverySnapshotVersion != "current" &&
432 recoverySnapshotVersion != ""
Inseob Kime498dd92020-08-04 09:24:04 +0900433 if boardVndkVersion == "current" {
434 boardVndkVersion = platformVndkVersion
435 }
436 if productVndkVersion == "current" {
437 productVndkVersion = platformVndkVersion
438 }
439
Colin Cross627280f2021-04-26 16:53:58 -0700440 if m.IsLlndkLibrary() || m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800441 // This is an LLNDK library. The implementation of the library will be on /system,
442 // and vendor and product variants will be created with LLNDK stubs.
443 // The LLNDK libraries need vendor variants even if there is no VNDK.
444 // The obsolete llndk_library and llndk_headers modules also need the vendor variants
445 // so the cc_library LLNDK stubs can depend on them.
Colin Cross1f3f1302021-04-26 18:37:44 -0700446 if m.NeedsLlndkVariants() {
Colin Crossb5f6fa62021-01-06 17:05:04 -0800447 coreVariantNeeded = true
448 }
449 if platformVndkVersion != "" {
450 vendorVariants = append(vendorVariants, platformVndkVersion)
451 productVariants = append(productVariants, platformVndkVersion)
452 }
453 if boardVndkVersion != "" {
454 vendorVariants = append(vendorVariants, boardVndkVersion)
455 }
456 if productVndkVersion != "" {
457 productVariants = append(productVariants, productVndkVersion)
458 }
459 } 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() {
480 if isVendorProprietaryModule(mctx) {
481 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)
490 // VNDK is always PLATFORM_VNDK_VERSION
491 if !m.IsVndk() {
492 productVariants = append(productVariants, productVndkVersion)
493 }
Inseob Kime498dd92020-08-04 09:24:04 +0900494 }
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400495 } else if vendorSpecific && m.SdkVersion() == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900496 // This will be available in /vendor (or /odm) only
497
498 // kernel_headers is a special module type whose exported headers
499 // are coming from DeviceKernelHeaders() which is always vendor
500 // dependent. They'll always have both vendor variants.
501 // For other modules, we assume that modules under proprietary
502 // paths are compatible for BOARD_VNDK_VERSION. The other modules
503 // are regarded as AOSP, which is PLATFORM_VNDK_VERSION.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400504 if m.KernelHeadersDecorator() {
Inseob Kime498dd92020-08-04 09:24:04 +0900505 vendorVariants = append(vendorVariants,
506 platformVndkVersion,
507 boardVndkVersion,
508 )
Bill Peckham945441c2020-08-31 16:07:58 -0700509 } else if isVendorProprietaryModule(mctx) {
Inseob Kime498dd92020-08-04 09:24:04 +0900510 vendorVariants = append(vendorVariants, boardVndkVersion)
511 } else {
512 vendorVariants = append(vendorVariants, platformVndkVersion)
513 }
514 } else {
515 // This is either in /system (or similar: /data), or is a
516 // modules built with the NDK. Modules built with the NDK
517 // will be restricted using the existing link type checks.
518 coreVariantNeeded = true
519 }
520
521 if boardVndkVersion != "" && productVndkVersion != "" {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400522 if coreVariantNeeded && productSpecific && m.SdkVersion() == "" {
Inseob Kime498dd92020-08-04 09:24:04 +0900523 // The module has "product_specific: true" that does not create core variant.
524 coreVariantNeeded = false
525 productVariants = append(productVariants, productVndkVersion)
526 }
527 } else {
528 // Unless PRODUCT_PRODUCT_VNDK_VERSION is set, product partition has no
529 // restriction to use system libs.
530 // No product variants defined in this case.
531 productVariants = []string{}
532 }
533
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400534 if m.RamdiskAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900535 ramdiskVariantNeeded = true
536 }
537
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400538 if m.AndroidModuleBase().InstallInRamdisk() {
Inseob Kime498dd92020-08-04 09:24:04 +0900539 ramdiskVariantNeeded = true
540 coreVariantNeeded = false
541 }
542
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400543 if m.VendorRamdiskAvailable() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700544 vendorRamdiskVariantNeeded = true
545 }
546
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400547 if m.AndroidModuleBase().InstallInVendorRamdisk() {
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700548 vendorRamdiskVariantNeeded = true
549 coreVariantNeeded = false
550 }
551
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400552 if m.RecoveryAvailable() {
Inseob Kime498dd92020-08-04 09:24:04 +0900553 recoveryVariantNeeded = true
554 }
555
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400556 if m.AndroidModuleBase().InstallInRecovery() {
Inseob Kime498dd92020-08-04 09:24:04 +0900557 recoveryVariantNeeded = true
558 coreVariantNeeded = false
559 }
560
Jose Galmes6f843bc2020-12-11 13:36:29 -0800561 // If using a snapshot, the recovery variant under AOSP directories is not needed,
562 // except for kernel headers, which needs all variants.
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400563 if m.KernelHeadersDecorator() &&
564 !m.IsSnapshotPrebuilt() &&
Jose Galmes6f843bc2020-12-11 13:36:29 -0800565 usingRecoverySnapshot &&
566 !isRecoveryProprietaryModule(mctx) {
567 recoveryVariantNeeded = false
568 }
569
Inseob Kime498dd92020-08-04 09:24:04 +0900570 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400571 m.AppendExtraVariant(VendorVariationPrefix + variant)
Inseob Kime498dd92020-08-04 09:24:04 +0900572 }
573
574 for _, variant := range android.FirstUniqueStrings(productVariants) {
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400575 m.AppendExtraVariant(ProductVariationPrefix + variant)
Inseob Kime498dd92020-08-04 09:24:04 +0900576 }
577
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400578 m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
579 m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
580 m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
581 m.SetCoreVariantNeeded(coreVariantNeeded)
Jose Galmes6f843bc2020-12-11 13:36:29 -0800582
583 // Disable the module if no variants are needed.
584 if !ramdiskVariantNeeded &&
585 !recoveryVariantNeeded &&
586 !coreVariantNeeded &&
Ivan Lozano3a7d0002021-03-30 12:19:36 -0400587 len(m.ExtraVariants()) == 0 {
Jose Galmes6f843bc2020-12-11 13:36:29 -0800588 m.Disable()
589 }
Inseob Kime498dd92020-08-04 09:24:04 +0900590}
591
592func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
593 return c.Properties.CoreVariantNeeded
594}
595
596func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
597 return c.Properties.RamdiskVariantNeeded
598}
599
Yifan Hong60e0cfb2020-10-21 15:17:56 -0700600func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
601 return c.Properties.VendorRamdiskVariantNeeded
602}
603
Inseob Kim08758f02021-04-08 21:13:22 +0900604func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
605 return false
606}
607
Inseob Kime498dd92020-08-04 09:24:04 +0900608func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
609 return c.Properties.RecoveryVariantNeeded
610}
611
612func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
613 return c.Properties.ExtraVariants
614}
615
Justin Yun63e9ec72020-10-29 16:49:43 +0900616func squashVendorSrcs(m *Module) {
617 if lib, ok := m.compiler.(*libraryDecorator); ok {
618 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
619 lib.baseCompiler.Properties.Target.Vendor.Srcs...)
620
621 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
622 lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
623
624 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
625 lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
626 }
627}
628
629func squashProductSrcs(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.Product.Srcs...)
633
634 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
635 lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
636
637 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
638 lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
639 }
640}
641
642func squashRecoverySrcs(m *Module) {
643 if lib, ok := m.compiler.(*libraryDecorator); ok {
644 lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
645 lib.baseCompiler.Properties.Target.Recovery.Srcs...)
646
647 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
648 lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
649
650 lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
651 lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
652 }
653}
654
655func squashVendorRamdiskSrcs(m *Module) {
656 if lib, ok := m.compiler.(*libraryDecorator); ok {
657 lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
658 }
659}
660
Inseob Kime498dd92020-08-04 09:24:04 +0900661func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
662 m := module.(*Module)
Yifan Hong6da33c22020-10-27 15:01:21 -0700663 if variant == android.RamdiskVariation {
Inseob Kime498dd92020-08-04 09:24:04 +0900664 m.MakeAsPlatform()
Yifan Hong6da33c22020-10-27 15:01:21 -0700665 } else if variant == android.VendorRamdiskVariation {
666 m.MakeAsPlatform()
667 squashVendorRamdiskSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900668 } else if variant == android.RecoveryVariation {
669 m.MakeAsPlatform()
670 squashRecoverySrcs(m)
671 } else if strings.HasPrefix(variant, VendorVariationPrefix) {
672 m.Properties.ImageVariationPrefix = VendorVariationPrefix
673 m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
674 squashVendorSrcs(m)
675
676 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
677 // Hide other vendor variants to avoid collision.
678 vndkVersion := ctx.DeviceConfig().VndkVersion()
679 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
680 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800681 m.HideFromMake()
Inseob Kime498dd92020-08-04 09:24:04 +0900682 }
683 } else if strings.HasPrefix(variant, ProductVariationPrefix) {
684 m.Properties.ImageVariationPrefix = ProductVariationPrefix
685 m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
Justin Yun6977e8a2020-10-29 18:24:11 +0900686 squashProductSrcs(m)
Inseob Kime498dd92020-08-04 09:24:04 +0900687 }
688}