blob: 26929b1ac33b4436e7c787c82d26df73519e5e68 [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
Jihoon Kang47e91842024-06-19 00:51:16 +000080func (mod *Module) SetProductVariantNeeded(b bool) {
81 mod.Properties.ProductVariantNeeded = b
82}
83
84func (mod *Module) SetVendorVariantNeeded(b bool) {
85 mod.Properties.VendorVariantNeeded = b
86}
87
Ivan Lozano699e2182021-03-22 14:29:47 -040088func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040089 if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok {
90 return snapshot.Version()
91 } else {
92 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
93 return ""
94 }
Ivan Lozano699e2182021-03-22 14:29:47 -040095}
96
Jihoon Kang47e91842024-06-19 00:51:16 +000097func (mod *Module) VendorVariantNeeded(ctx android.BaseModuleContext) bool {
98 return mod.Properties.VendorVariantNeeded
99}
100
101func (mod *Module) ProductVariantNeeded(ctx android.BaseModuleContext) bool {
102 return mod.Properties.ProductVariantNeeded
103}
104
Ivan Lozano6a884432020-12-02 09:15:16 -0500105func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500106 return mod.Properties.VendorRamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -0500107}
108
109func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
110 return mod.Properties.CoreVariantNeeded
111}
112
113func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
Matthew Maurerc6868382021-07-13 14:12:37 -0700114 return mod.Properties.RamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -0500115}
116
Inseob Kim08758f02021-04-08 21:13:22 +0900117func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
118 return false
119}
120
Ivan Lozano6a884432020-12-02 09:15:16 -0500121func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800122 return mod.Properties.RecoveryVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -0500123}
124
125func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
126 return mod.Properties.ExtraVariants
127}
128
Ivan Lozano699e2182021-03-22 14:29:47 -0400129func (mod *Module) IsSnapshotPrebuilt() bool {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400130 if p, ok := mod.compiler.(cc.SnapshotInterface); ok {
131 return p.IsSnapshotPrebuilt()
132 }
Ivan Lozano699e2182021-03-22 14:29:47 -0400133 return false
134}
135
Colin Crossea30d852023-11-29 16:00:16 -0800136func (mod *Module) InstallInVendor() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400137 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
138 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
139 // unless they have "odm_available: true".
Colin Crossea30d852023-11-29 16:00:16 -0800140 return mod.InVendor() && !mod.VendorVariantToOdm()
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400141}
142
Colin Crossea30d852023-11-29 16:00:16 -0800143func (mod *Module) InstallInOdm() bool {
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400144 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
Colin Crossea30d852023-11-29 16:00:16 -0800145 return mod.InVendor() && mod.VendorVariantToOdm()
Matthew Maurer993db7a2023-01-24 16:36:02 -0800146}
147
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400148// Returns true when this module creates a vendor variant and wants to install the vendor variant
149// to the odm partition.
150func (c *Module) VendorVariantToOdm() bool {
151 return Bool(c.VendorProperties.Odm_available)
152}
153
Ivan Lozano6a884432020-12-02 09:15:16 -0500154func (ctx *moduleContext) ProductSpecific() bool {
Matthew Maurer9f59e8d2021-08-19 13:10:05 -0700155 return ctx.ModuleContext.ProductSpecific() || ctx.RustModule().productSpecificModuleContext()
156}
157
158func (c *Module) productSpecificModuleContext() bool {
159 // Additionally check if this module is inProduct() that means it is a "product" variant of a
160 // module. As well as product specific modules, product variants must be installed to /product.
161 return c.InProduct()
Ivan Lozano6a884432020-12-02 09:15:16 -0500162}
163
164func (mod *Module) InRecovery() bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800165 return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery()
Ivan Lozano6a884432020-12-02 09:15:16 -0500166}
167
Jose Galmes61741e22022-02-06 22:06:23 -0800168func (mod *Module) InRamdisk() bool {
169 return mod.ModuleBase.InRamdisk() || mod.ModuleBase.InstallInRamdisk()
170}
171
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500172func (mod *Module) InVendorRamdisk() bool {
173 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
174}
175
Ivan Lozano6a884432020-12-02 09:15:16 -0500176func (mod *Module) OnlyInRamdisk() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800177 return mod.ModuleBase.InstallInRamdisk()
Ivan Lozano6a884432020-12-02 09:15:16 -0500178}
179
180func (mod *Module) OnlyInRecovery() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800181 return mod.ModuleBase.InstallInRecovery()
Ivan Lozano6a884432020-12-02 09:15:16 -0500182}
183
184func (mod *Module) OnlyInVendorRamdisk() bool {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800185 return mod.ModuleBase.InstallInVendorRamdisk()
Matthew Maurer52af5b02021-05-27 10:01:36 -0700186}
187
Ivan Lozano6a884432020-12-02 09:15:16 -0500188// Returns true when this module is configured to have core and vendor variants.
189func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900190 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -0500191}
192
Justin Yuncbca3732021-02-03 19:24:13 +0900193// Always returns false because rust modules do not support product variant.
194func (mod *Module) HasProductVariant() bool {
195 return Bool(mod.VendorProperties.Product_available)
196}
197
198func (mod *Module) HasNonSystemVariants() bool {
199 return mod.HasVendorVariant() || mod.HasProductVariant()
200}
201
Ivan Lozano699e2182021-03-22 14:29:47 -0400202func (mod *Module) InProduct() bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000203 return mod.Properties.ImageVariation == android.ProductVariation
Ivan Lozano6a884432020-12-02 09:15:16 -0500204}
205
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400206// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
207func (mod *Module) InVendor() bool {
Jihoon Kang47e91842024-06-19 00:51:16 +0000208 return mod.Properties.ImageVariation == android.VendorVariation
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400209}
210
Kiyoung Kimaa394802024-01-08 12:55:45 +0900211// Returns true if the module is "vendor" or "product" variant.
212func (mod *Module) InVendorOrProduct() bool {
213 return mod.InVendor() || mod.InProduct()
214}
215
Jihoon Kang7583e832024-06-13 21:25:45 +0000216func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string) {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500217 if variant == android.VendorRamdiskVariation {
Jihoon Kang7583e832024-06-13 21:25:45 +0000218 mod.MakeAsPlatform()
Matthew Maurer460ee942021-02-11 12:31:46 -0800219 } else if variant == android.RecoveryVariation {
Jihoon Kang7583e832024-06-13 21:25:45 +0000220 mod.MakeAsPlatform()
Jihoon Kang47e91842024-06-19 00:51:16 +0000221 } else if strings.HasPrefix(variant, android.VendorVariation) {
222 mod.Properties.ImageVariation = android.VendorVariation
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900223 if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Jihoon Kang7583e832024-06-13 21:25:45 +0000224 mod.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900225 }
Jihoon Kang47e91842024-06-19 00:51:16 +0000226 } else if strings.HasPrefix(variant, android.ProductVariation) {
227 mod.Properties.ImageVariation = android.ProductVariation
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900228 if strings.HasPrefix(variant, cc.ProductVariationPrefix) {
Jihoon Kang7583e832024-06-13 21:25:45 +0000229 mod.Properties.VndkVersion = strings.TrimPrefix(variant, cc.ProductVariationPrefix)
Kiyoung Kimb5fdb2e2024-01-03 14:24:34 +0900230 }
Ivan Lozano6a884432020-12-02 09:15:16 -0500231 }
232}
233
234func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Matthew Maurer993db7a2023-01-24 16:36:02 -0800235 if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500236 mctx.PropertyErrorf("double_loadable",
237 "Rust modules do not yet support double loading")
238 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500239 if Bool(mod.Properties.Vendor_ramdisk_available) {
240 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
241 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500242 }
243 }
244
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400245 cc.MutateImage(mctx, mod)
246
247 if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() {
248
249 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
250 // Rust does not support prebuilt libraries on non-System images.
251 mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.")
Ivan Lozano6a884432020-12-02 09:15:16 -0500252 }
253 }
Ivan Lozano6a884432020-12-02 09:15:16 -0500254}