blob: 3afb4a7d59dc59a04cc8e09d8a507b1dcb8ac143 [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
26func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
Ivan Lozanoe6d30982021-02-05 10:57:43 -050027 return mod.Properties.VendorRamdiskVariantNeeded
Ivan Lozano6a884432020-12-02 09:15:16 -050028}
29
30func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
31 return mod.Properties.CoreVariantNeeded
32}
33
34func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
35 return mod.InRamdisk()
36}
37
Inseob Kimf84e9c02021-04-08 21:13:22 +090038func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
39 return false
40}
41
Ivan Lozano6a884432020-12-02 09:15:16 -050042func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
43 return mod.InRecovery()
44}
45
46func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
47 return mod.Properties.ExtraVariants
48}
49
50func (ctx *moduleContext) ProductSpecific() bool {
51 return false
52}
53
54func (mod *Module) InRecovery() bool {
55 // TODO(b/165791368)
56 return false
57}
58
Ivan Lozanoe6d30982021-02-05 10:57:43 -050059func (mod *Module) InVendorRamdisk() bool {
60 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
61}
62
Ivan Lozano6a884432020-12-02 09:15:16 -050063func (mod *Module) OnlyInRamdisk() bool {
64 // TODO(b/165791368)
65 return false
66}
67
68func (mod *Module) OnlyInRecovery() bool {
69 // TODO(b/165791368)
70 return false
71}
72
73func (mod *Module) OnlyInVendorRamdisk() bool {
74 return false
75}
76
77// Returns true when this module is configured to have core and vendor variants.
78func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +090079 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -050080}
81
Justin Yuncbca3732021-02-03 19:24:13 +090082// Always returns false because rust modules do not support product variant.
83func (mod *Module) HasProductVariant() bool {
84 return Bool(mod.VendorProperties.Product_available)
85}
86
87func (mod *Module) HasNonSystemVariants() bool {
88 return mod.HasVendorVariant() || mod.HasProductVariant()
89}
90
Ivan Lozano6a884432020-12-02 09:15:16 -050091func (c *Module) InProduct() bool {
92 return false
93}
94
95func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
96 m := module.(*Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -050097 if variant == android.VendorRamdiskVariation {
98 m.MakeAsPlatform()
99 } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500100 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
101 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
102
103 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
104 // Hide other vendor variants to avoid collision.
105 vndkVersion := ctx.DeviceConfig().VndkVersion()
106 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
107 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800108 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -0500109 }
110 }
111}
112
113func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
114 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
115 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
116
117 // Rust does not support installing to the product image yet.
Justin Yunc0d8c492021-01-07 17:45:31 +0900118 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500119 mctx.PropertyErrorf("product_available",
120 "Rust modules do not yet support being available to the product image")
121 } else if mctx.ProductSpecific() {
122 mctx.PropertyErrorf("product_specific",
123 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900124 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500125 mctx.PropertyErrorf("double_loadable",
126 "Rust modules do not yet support double loading")
127 }
128
129 coreVariantNeeded := true
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500130 vendorRamdiskVariantNeeded := false
131
Ivan Lozano6a884432020-12-02 09:15:16 -0500132 var vendorVariants []string
133
Justin Yunebcf0c52021-01-08 18:00:19 +0900134 if mod.HasVendorVariant() {
135 prop := "vendor_available"
136 if Bool(mod.VendorProperties.Odm_available) {
137 prop = "odm_available"
138 }
139
Ivan Lozano6a884432020-12-02 09:15:16 -0500140 if vendorSpecific {
Justin Yunebcf0c52021-01-08 18:00:19 +0900141 mctx.PropertyErrorf(prop,
142 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
Ivan Lozano6a884432020-12-02 09:15:16 -0500143 }
144
145 if lib, ok := mod.compiler.(libraryInterface); ok {
146 // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available.
147 // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library
148 // mutators until support is added.
149 //
150 // We can't check shared() here because image mutator is called before the library mutator, so we need to
151 // check buildShared()
152 if lib.buildShared() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500153 mctx.PropertyErrorf(prop, "cannot be set for rust_ffi or rust_ffi_shared modules.")
Justin Yunebcf0c52021-01-08 18:00:19 +0900154 } else {
Ivan Lozano6a884432020-12-02 09:15:16 -0500155 vendorVariants = append(vendorVariants, platformVndkVersion)
156 }
157 }
158 }
159
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500160 if Bool(mod.Properties.Vendor_ramdisk_available) {
161 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
162 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
163 } else {
164 vendorRamdiskVariantNeeded = true
165 }
166 }
167
Ivan Lozano6a884432020-12-02 09:15:16 -0500168 if vendorSpecific {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500169 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && (lib.buildShared() || lib.buildDylib() || lib.buildRlib())) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500170 mctx.ModuleErrorf("Rust vendor specific modules are currently only supported for rust_ffi_static modules.")
171 } else {
172 coreVariantNeeded = false
173 vendorVariants = append(vendorVariants, platformVndkVersion)
174 }
175 }
176
177 mod.Properties.CoreVariantNeeded = coreVariantNeeded
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500178 mod.Properties.VendorRamdiskVariantNeeded = vendorRamdiskVariantNeeded
179
Ivan Lozano6a884432020-12-02 09:15:16 -0500180 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
181 mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, cc.VendorVariationPrefix+variant)
182 }
183
184}