blob: 50bf02a4aa340ecfea9d0a545aef0e67e7e85f34 [file] [log] [blame]
Ivan Lozano6a884432020-12-02 09:15:16 -05001// 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.
14
15package rust
16
17import (
18 "strings"
19
20 "android/soong/android"
21 "android/soong/cc"
22)
23
24var _ android.ImageInterface = (*Module)(nil)
25
Ivan Lozano699e2182021-03-22 14:29:47 -040026var _ cc.ImageMutatableModule = (*Module)(nil)
27
28func (mod *Module) VendorAvailable() bool {
29 return Bool(mod.VendorProperties.Vendor_available)
30}
31
32func (mod *Module) OdmAvailable() bool {
33 return Bool(mod.VendorProperties.Odm_available)
34}
35
36func (mod *Module) ProductAvailable() bool {
Matthew Maurer52af5b02021-05-27 10:01:36 -070037 return Bool(mod.VendorProperties.Product_available)
Ivan Lozano699e2182021-03-22 14:29:47 -040038}
39
40func (mod *Module) RamdiskAvailable() bool {
Matthew Maurerc6868382021-07-13 14:12:37 -070041 return Bool(mod.Properties.Ramdisk_available)
Ivan Lozano699e2182021-03-22 14:29:47 -040042}
43
44func (mod *Module) VendorRamdiskAvailable() bool {
45 return Bool(mod.Properties.Vendor_ramdisk_available)
46}
47
48func (mod *Module) AndroidModuleBase() *android.ModuleBase {
49 return &mod.ModuleBase
50}
51
52func (mod *Module) RecoveryAvailable() bool {
Matthew Maurer460ee942021-02-11 12:31:46 -080053 return Bool(mod.Properties.Recovery_available)
Ivan Lozano699e2182021-03-22 14:29:47 -040054}
55
56func (mod *Module) ExtraVariants() []string {
57 return mod.Properties.ExtraVariants
58}
59
60func (mod *Module) AppendExtraVariant(extraVariant string) {
61 mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, extraVariant)
62}
63
64func (mod *Module) SetRamdiskVariantNeeded(b bool) {
Matthew Maurerc6868382021-07-13 14:12:37 -070065 mod.Properties.RamdiskVariantNeeded = b
Ivan Lozano699e2182021-03-22 14:29:47 -040066}
67
68func (mod *Module) SetVendorRamdiskVariantNeeded(b bool) {
69 mod.Properties.VendorRamdiskVariantNeeded = b
70}
71
72func (mod *Module) SetRecoveryVariantNeeded(b bool) {
Matthew Maurer460ee942021-02-11 12:31:46 -080073 mod.Properties.RecoveryVariantNeeded = b
Ivan Lozano699e2182021-03-22 14:29:47 -040074}
75
76func (mod *Module) SetCoreVariantNeeded(b bool) {
77 mod.Properties.CoreVariantNeeded = b
78}
79
80func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040081 if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok {
82 return snapshot.Version()
83 } else {
84 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
85 return ""
86 }
Ivan Lozano699e2182021-03-22 14:29:47 -040087}
88
Ivan Lozano6a884432020-12-02 09:15:16 -050089func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Ivan Lozanoe6d30982021-02-05 10:57:43 -050090 return mod.Properties.VendorRamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -050091}
92
93func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
94 return mod.Properties.CoreVariantNeeded
95}
96
97func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
Matthew Maurerc6868382021-07-13 14:12:37 -070098 return mod.Properties.RamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -050099}
100
Inseob Kim08758f02021-04-08 21:13:22 +0900101func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
102 return false
103}
104
Ivan Lozano6a884432020-12-02 09:15:16 -0500105func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800106 return mod.Properties.RecoveryVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -0500107}
108
109func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
110 return mod.Properties.ExtraVariants
111}
112
Ivan Lozano699e2182021-03-22 14:29:47 -0400113func (mod *Module) IsSnapshotPrebuilt() bool {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400114 if p, ok := mod.compiler.(cc.SnapshotInterface); ok {
115 return p.IsSnapshotPrebuilt()
116 }
Ivan Lozano699e2182021-03-22 14:29:47 -0400117 return false
118}
119
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400120func (ctx *moduleContext) SocSpecific() bool {
121 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
122 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
123 // unless they have "odm_available: true".
124 return ctx.ModuleContext.SocSpecific() || (ctx.RustModule().InVendor() && !ctx.RustModule().VendorVariantToOdm())
125}
126
127func (ctx *moduleContext) DeviceSpecific() bool {
128 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
129 return ctx.ModuleContext.DeviceSpecific() || (ctx.RustModule().InVendor() && ctx.RustModule().VendorVariantToOdm())
130}
131
Matthew Maurer993db7a2023-01-24 16:36:02 -0800132func (ctx *moduleContext) SystemExtSpecific() bool {
133 return ctx.ModuleContext.SystemExtSpecific()
134}
135
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400136// Returns true when this module creates a vendor variant and wants to install the vendor variant
137// to the odm partition.
138func (c *Module) VendorVariantToOdm() bool {
139 return Bool(c.VendorProperties.Odm_available)
140}
141
Ivan Lozano6a884432020-12-02 09:15:16 -0500142func (ctx *moduleContext) ProductSpecific() bool {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700143 return ctx.ModuleContext.ProductSpecific() || ctx.RustModule().productSpecificModuleContext()
144}
145
146func (c *Module) productSpecificModuleContext() bool {
147 // Additionally check if this module is inProduct() that means it is a "product" variant of a
148 // module. As well as product specific modules, product variants must be installed to /product.
149 return c.InProduct()
Ivan Lozano6a884432020-12-02 09:15:16 -0500150}
151
152func (mod *Module) InRecovery() bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800153 return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery()
Ivan Lozano6a884432020-12-02 09:15:16 -0500154}
155
Jose Galmes61741e22022-02-06 22:06:23 -0800156func (mod *Module) InRamdisk() bool {
157 return mod.ModuleBase.InRamdisk() || mod.ModuleBase.InstallInRamdisk()
158}
159
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500160func (mod *Module) InVendorRamdisk() bool {
161 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
162}
163
Ivan Lozano6a884432020-12-02 09:15:16 -0500164func (mod *Module) OnlyInRamdisk() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800165 return mod.ModuleBase.InstallInRamdisk()
Ivan Lozano6a884432020-12-02 09:15:16 -0500166}
167
168func (mod *Module) OnlyInRecovery() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800169 return mod.ModuleBase.InstallInRecovery()
Ivan Lozano6a884432020-12-02 09:15:16 -0500170}
171
172func (mod *Module) OnlyInVendorRamdisk() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800173 return mod.ModuleBase.InstallInVendorRamdisk()
Matthew Maurer52af5b02021-05-27 10:01:36 -0700174}
175
Ivan Lozano6a884432020-12-02 09:15:16 -0500176// Returns true when this module is configured to have core and vendor variants.
177func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900178 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -0500179}
180
Justin Yuncbca3732021-02-03 19:24:13 +0900181// Always returns false because rust modules do not support product variant.
182func (mod *Module) HasProductVariant() bool {
183 return Bool(mod.VendorProperties.Product_available)
184}
185
186func (mod *Module) HasNonSystemVariants() bool {
187 return mod.HasVendorVariant() || mod.HasProductVariant()
188}
189
Ivan Lozano699e2182021-03-22 14:29:47 -0400190func (mod *Module) InProduct() bool {
Matthew Maurer52af5b02021-05-27 10:01:36 -0700191 return mod.Properties.ImageVariationPrefix == cc.ProductVariationPrefix
Ivan Lozano6a884432020-12-02 09:15:16 -0500192}
193
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400194// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
195func (mod *Module) InVendor() bool {
196 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
197}
198
Ivan Lozano6a884432020-12-02 09:15:16 -0500199func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
200 m := module.(*Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500201 if variant == android.VendorRamdiskVariation {
202 m.MakeAsPlatform()
Matthew Maurer460ee942021-02-11 12:31:46 -0800203 } else if variant == android.RecoveryVariation {
204 m.MakeAsPlatform()
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500205 } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500206 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
207 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
208
209 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
210 // Hide other vendor variants to avoid collision.
211 vndkVersion := ctx.DeviceConfig().VndkVersion()
212 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
213 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800214 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -0500215 }
Matthew Maurer52af5b02021-05-27 10:01:36 -0700216 } else if strings.HasPrefix(variant, cc.ProductVariationPrefix) {
217 m.Properties.ImageVariationPrefix = cc.ProductVariationPrefix
218 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.ProductVariationPrefix)
Ivan Lozano6a884432020-12-02 09:15:16 -0500219 }
220}
221
222func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500223 // Rust does not support installing to the product image yet.
Ivan Lozano1921e802021-05-20 13:39:16 -0400224 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
225
Matthew Maurer993db7a2023-01-24 16:36:02 -0800226 if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500227 mctx.PropertyErrorf("double_loadable",
228 "Rust modules do not yet support double loading")
229 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500230 if Bool(mod.Properties.Vendor_ramdisk_available) {
231 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
232 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500233 }
234 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400235 if vendorSpecific {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400236 if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
237 mctx.PropertyErrorf("vendor", "Vendor-only dylibs are not yet supported, use rust_library_rlib.")
238 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400239 }
Matthew Maurer993db7a2023-01-24 16:36:02 -0800240 if mctx.ProductSpecific() {
241 if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
242 mctx.PropertyErrorf("product", "Product-only dylibs are not yet supported, use rust_library_rlib.")
243 }
244 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500245
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400246 cc.MutateImage(mctx, mod)
247
248 if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() {
249
250 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
251 // Rust does not support prebuilt libraries on non-System images.
252 mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.")
Ivan Lozano6a884432020-12-02 09:15:16 -0500253 }
254 }
Ivan Lozano6a884432020-12-02 09:15:16 -0500255}