blob: 6a833769203a250d1ffe3e1f755601511dfe0567 [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 {
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) {
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) {
Matthew Maurer460ee942021-02-11 12:31:46 -080075 mod.Properties.RecoveryVariantNeeded = b
Ivan Lozano699e2182021-03-22 14:29:47 -040076}
77
78func (mod *Module) SetCoreVariantNeeded(b bool) {
79 mod.Properties.CoreVariantNeeded = b
80}
81
82func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
Ivan Lozano3149e6e2021-06-01 15:09:53 -040083 if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok {
84 return snapshot.Version()
85 } else {
86 mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
87 return ""
88 }
Ivan Lozano699e2182021-03-22 14:29:47 -040089}
90
Ivan Lozano6a884432020-12-02 09:15:16 -050091func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Ivan Lozanoe6d30982021-02-05 10:57:43 -050092 return mod.Properties.VendorRamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -050093}
94
95func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
96 return mod.Properties.CoreVariantNeeded
97}
98
99func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
100 return mod.InRamdisk()
101}
102
Inseob Kim08758f02021-04-08 21:13:22 +0900103func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
104 return false
105}
106
Ivan Lozano6a884432020-12-02 09:15:16 -0500107func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800108 return mod.Properties.RecoveryVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -0500109}
110
111func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
112 return mod.Properties.ExtraVariants
113}
114
Ivan Lozano699e2182021-03-22 14:29:47 -0400115func (mod *Module) IsSnapshotPrebuilt() bool {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400116 if p, ok := mod.compiler.(cc.SnapshotInterface); ok {
117 return p.IsSnapshotPrebuilt()
118 }
Ivan Lozano699e2182021-03-22 14:29:47 -0400119 return false
120}
121
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400122func (ctx *moduleContext) SocSpecific() bool {
123 // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
124 // module. As well as SoC specific modules, vendor variants must be installed to /vendor
125 // unless they have "odm_available: true".
126 return ctx.ModuleContext.SocSpecific() || (ctx.RustModule().InVendor() && !ctx.RustModule().VendorVariantToOdm())
127}
128
129func (ctx *moduleContext) DeviceSpecific() bool {
130 // Some vendor variants want to be installed to /odm by setting "odm_available: true".
131 return ctx.ModuleContext.DeviceSpecific() || (ctx.RustModule().InVendor() && ctx.RustModule().VendorVariantToOdm())
132}
133
134// Returns true when this module creates a vendor variant and wants to install the vendor variant
135// to the odm partition.
136func (c *Module) VendorVariantToOdm() bool {
137 return Bool(c.VendorProperties.Odm_available)
138}
139
Ivan Lozano6a884432020-12-02 09:15:16 -0500140func (ctx *moduleContext) ProductSpecific() bool {
141 return false
142}
143
144func (mod *Module) InRecovery() bool {
Matthew Maurer460ee942021-02-11 12:31:46 -0800145 return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery()
Ivan Lozano6a884432020-12-02 09:15:16 -0500146}
147
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500148func (mod *Module) InVendorRamdisk() bool {
149 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
150}
151
Ivan Lozano6a884432020-12-02 09:15:16 -0500152func (mod *Module) OnlyInRamdisk() bool {
153 // TODO(b/165791368)
154 return false
155}
156
157func (mod *Module) OnlyInRecovery() bool {
158 // TODO(b/165791368)
159 return false
160}
161
162func (mod *Module) OnlyInVendorRamdisk() bool {
163 return false
164}
165
166// Returns true when this module is configured to have core and vendor variants.
167func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +0900168 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -0500169}
170
Justin Yuncbca3732021-02-03 19:24:13 +0900171// Always returns false because rust modules do not support product variant.
172func (mod *Module) HasProductVariant() bool {
173 return Bool(mod.VendorProperties.Product_available)
174}
175
176func (mod *Module) HasNonSystemVariants() bool {
177 return mod.HasVendorVariant() || mod.HasProductVariant()
178}
179
Ivan Lozano699e2182021-03-22 14:29:47 -0400180func (mod *Module) InProduct() bool {
Ivan Lozano6a884432020-12-02 09:15:16 -0500181 return false
182}
183
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400184// Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
185func (mod *Module) InVendor() bool {
186 return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
187}
188
Ivan Lozano6a884432020-12-02 09:15:16 -0500189func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
190 m := module.(*Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500191 if variant == android.VendorRamdiskVariation {
192 m.MakeAsPlatform()
Matthew Maurer460ee942021-02-11 12:31:46 -0800193 } else if variant == android.RecoveryVariation {
194 m.MakeAsPlatform()
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500195 } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500196 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
197 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
198
199 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
200 // Hide other vendor variants to avoid collision.
201 vndkVersion := ctx.DeviceConfig().VndkVersion()
202 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
203 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800204 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -0500205 }
206 }
207}
208
209func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500210 // Rust does not support installing to the product image yet.
Ivan Lozano1921e802021-05-20 13:39:16 -0400211 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
212
Justin Yunc0d8c492021-01-07 17:45:31 +0900213 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500214 mctx.PropertyErrorf("product_available",
215 "Rust modules do not yet support being available to the product image")
216 } else if mctx.ProductSpecific() {
217 mctx.PropertyErrorf("product_specific",
218 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900219 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500220 mctx.PropertyErrorf("double_loadable",
221 "Rust modules do not yet support double loading")
222 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500223 if Bool(mod.Properties.Vendor_ramdisk_available) {
224 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
225 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500226 }
227 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400228 if vendorSpecific {
Ivan Lozano3149e6e2021-06-01 15:09:53 -0400229 if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
230 mctx.PropertyErrorf("vendor", "Vendor-only dylibs are not yet supported, use rust_library_rlib.")
231 }
Ivan Lozano1921e802021-05-20 13:39:16 -0400232 }
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500233
Ivan Lozanoc08897c2021-04-02 12:41:32 -0400234 cc.MutateImage(mctx, mod)
235
236 if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() {
237
238 if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
239 // Rust does not support prebuilt libraries on non-System images.
240 mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.")
Ivan Lozano6a884432020-12-02 09:15:16 -0500241 }
242 }
Ivan Lozano6a884432020-12-02 09:15:16 -0500243}