blob: 3b54f12c754aee0d0a1d81df9ed2e1e8cd1ad553 [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 {
37 return false
38}
39
40func (mod *Module) RamdiskAvailable() bool {
41 return false
42}
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 {
53 return false
54}
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) {
65 if b {
66 panic("Setting ramdisk variant needed for Rust module is unsupported: " + mod.BaseModuleName())
67 }
68}
69
70func (mod *Module) SetVendorRamdiskVariantNeeded(b bool) {
71 mod.Properties.VendorRamdiskVariantNeeded = b
72}
73
74func (mod *Module) SetRecoveryVariantNeeded(b bool) {
75 if b {
76 panic("Setting recovery variant needed for Rust module is unsupported: " + mod.BaseModuleName())
77 }
78}
79
80func (mod *Module) SetCoreVariantNeeded(b bool) {
81 mod.Properties.CoreVariantNeeded = b
82}
83
84func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040085 if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok {
86 return snapshot.Version()
87 } else {
88 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
89 return ""
90 }
Ivan Lozano699e2182021-03-22 14:29:47 -040091}
92
Ivan Lozano6a884432020-12-02 09:15:16 -050093func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Ivan Lozanoe6d30982021-02-05 10:57:43 -050094 return mod.Properties.VendorRamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -050095}
96
97func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
98 return mod.Properties.CoreVariantNeeded
99}
100
101func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
102 return mod.InRamdisk()
103}
104
Inseob Kim08758f02021-04-08 21:13:22 +0900105func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
106 return false
107}
108
Ivan Lozano6a884432020-12-02 09:15:16 -0500109func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
110 return mod.InRecovery()
111}
112
113func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
114 return mod.Properties.ExtraVariants
115}
116
Ivan Lozano699e2182021-03-22 14:29:47 -0400117func (mod *Module) IsSnapshotPrebuilt() bool {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400118 if p, ok := mod.compiler.(cc.SnapshotInterface); ok {
119 return p.IsSnapshotPrebuilt()
120 }
Ivan Lozano699e2182021-03-22 14:29:47 -0400121 return false
122}
123
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400124func (ctx *moduleContext) SocSpecific() bool {
125 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
126 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
127 // unless they have "odm_available: true".
128 return ctx.ModuleContext.SocSpecific() || (ctx.RustModule().InVendor() && !ctx.RustModule().VendorVariantToOdm())
129}
130
131func (ctx *moduleContext) DeviceSpecific() bool {
132 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
133 return ctx.ModuleContext.DeviceSpecific() || (ctx.RustModule().InVendor() && ctx.RustModule().VendorVariantToOdm())
134}
135
136// 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 {
143 return false
144}
145
146func (mod *Module) InRecovery() bool {
147 // TODO(b/165791368)
148 return false
149}
150
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500151func (mod *Module) InVendorRamdisk() bool {
152 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
153}
154
Ivan Lozano6a884432020-12-02 09:15:16 -0500155func (mod *Module) OnlyInRamdisk() bool {
156 // TODO(b/165791368)
157 return false
158}
159
160func (mod *Module) OnlyInRecovery() bool {
161 // TODO(b/165791368)
162 return false
163}
164
165func (mod *Module) OnlyInVendorRamdisk() bool {
166 return false
167}
168
169// Returns true when this module is configured to have core and vendor variants.
170func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900171 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -0500172}
173
Justin Yuncbca3732021-02-03 19:24:13 +0900174// Always returns false because rust modules do not support product variant.
175func (mod *Module) HasProductVariant() bool {
176 return Bool(mod.VendorProperties.Product_available)
177}
178
179func (mod *Module) HasNonSystemVariants() bool {
180 return mod.HasVendorVariant() || mod.HasProductVariant()
181}
182
Ivan Lozano699e2182021-03-22 14:29:47 -0400183func (mod *Module) InProduct() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500184 return false
185}
186
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400187// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
188func (mod *Module) InVendor() bool {
189 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
190}
191
Ivan Lozano6a884432020-12-02 09:15:16 -0500192func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
193 m := module.(*Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500194 if variant == android.VendorRamdiskVariation {
195 m.MakeAsPlatform()
196 } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500197 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
198 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
199
200 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
201 // Hide other vendor variants to avoid collision.
202 vndkVersion := ctx.DeviceConfig().VndkVersion()
203 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
204 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800205 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -0500206 }
207 }
208}
209
210func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500211 // Rust does not support installing to the product image yet.
Ivan Lozano1921e802021-05-20 13:39:16 -0400212 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
213
Justin Yunc0d8c492021-01-07 17:45:31 +0900214 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500215 mctx.PropertyErrorf("product_available",
216 "Rust modules do not yet support being available to the product image")
217 } else if mctx.ProductSpecific() {
218 mctx.PropertyErrorf("product_specific",
219 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900220 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500221 mctx.PropertyErrorf("double_loadable",
222 "Rust modules do not yet support double loading")
223 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500224 if Bool(mod.Properties.Vendor_ramdisk_available) {
225 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
226 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500227 }
228 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400229 if vendorSpecific {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400230 if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
231 mctx.PropertyErrorf("vendor", "Vendor-only dylibs are not yet supported, use rust_library_rlib.")
232 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400233 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500234
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400235 cc.MutateImage(mctx, mod)
236
237 if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() {
238
239 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
240 // Rust does not support prebuilt libraries on non-System images.
241 mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.")
Ivan Lozano6a884432020-12-02 09:15:16 -0500242 }
243 }
Ivan Lozano6a884432020-12-02 09:15:16 -0500244}