blob: 628aca3e49c785b1053a7b8a21a12a89658d04fe [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
38func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
39 return mod.InRecovery()
40}
41
42func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
43 return mod.Properties.ExtraVariants
44}
45
46func (ctx *moduleContext) ProductSpecific() bool {
47 return false
48}
49
50func (mod *Module) InRecovery() bool {
51 // TODO(b/165791368)
52 return false
53}
54
Ivan Lozanoe6d30982021-02-05 10:57:43 -050055func (mod *Module) InVendorRamdisk() bool {
56 return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
57}
58
Ivan Lozano6a884432020-12-02 09:15:16 -050059func (mod *Module) OnlyInRamdisk() bool {
60 // TODO(b/165791368)
61 return false
62}
63
64func (mod *Module) OnlyInRecovery() bool {
65 // TODO(b/165791368)
66 return false
67}
68
69func (mod *Module) OnlyInVendorRamdisk() bool {
70 return false
71}
72
73// Returns true when this module is configured to have core and vendor variants.
74func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +090075 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -050076}
77
Justin Yuncbca3732021-02-03 19:24:13 +090078// Always returns false because rust modules do not support product variant.
79func (mod *Module) HasProductVariant() bool {
80 return Bool(mod.VendorProperties.Product_available)
81}
82
83func (mod *Module) HasNonSystemVariants() bool {
84 return mod.HasVendorVariant() || mod.HasProductVariant()
85}
86
Ivan Lozano6a884432020-12-02 09:15:16 -050087func (c *Module) InProduct() bool {
88 return false
89}
90
91func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
92 m := module.(*Module)
Ivan Lozanoe6d30982021-02-05 10:57:43 -050093 if variant == android.VendorRamdiskVariation {
94 m.MakeAsPlatform()
95 } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
Ivan Lozano6a884432020-12-02 09:15:16 -050096 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
97 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
98
99 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
100 // Hide other vendor variants to avoid collision.
101 vndkVersion := ctx.DeviceConfig().VndkVersion()
102 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
103 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800104 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -0500105 }
106 }
107}
108
109func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
110 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
111 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
112
113 // Rust does not support installing to the product image yet.
Justin Yunc0d8c492021-01-07 17:45:31 +0900114 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500115 mctx.PropertyErrorf("product_available",
116 "Rust modules do not yet support being available to the product image")
117 } else if mctx.ProductSpecific() {
118 mctx.PropertyErrorf("product_specific",
119 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900120 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500121 mctx.PropertyErrorf("double_loadable",
122 "Rust modules do not yet support double loading")
123 }
124
125 coreVariantNeeded := true
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500126 vendorRamdiskVariantNeeded := false
127
Ivan Lozano6a884432020-12-02 09:15:16 -0500128 var vendorVariants []string
129
Justin Yunebcf0c52021-01-08 18:00:19 +0900130 if mod.HasVendorVariant() {
131 prop := "vendor_available"
132 if Bool(mod.VendorProperties.Odm_available) {
133 prop = "odm_available"
134 }
135
Ivan Lozano6a884432020-12-02 09:15:16 -0500136 if vendorSpecific {
Justin Yunebcf0c52021-01-08 18:00:19 +0900137 mctx.PropertyErrorf(prop,
138 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
Ivan Lozano6a884432020-12-02 09:15:16 -0500139 }
140
141 if lib, ok := mod.compiler.(libraryInterface); ok {
142 // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available.
143 // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library
144 // mutators until support is added.
145 //
146 // We can't check shared() here because image mutator is called before the library mutator, so we need to
147 // check buildShared()
148 if lib.buildShared() {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500149 mctx.PropertyErrorf(prop, "cannot be set for rust_ffi or rust_ffi_shared modules.")
Justin Yunebcf0c52021-01-08 18:00:19 +0900150 } else {
Ivan Lozano6a884432020-12-02 09:15:16 -0500151 vendorVariants = append(vendorVariants, platformVndkVersion)
152 }
153 }
154 }
155
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500156 if Bool(mod.Properties.Vendor_ramdisk_available) {
157 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
158 mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
159 } else {
160 vendorRamdiskVariantNeeded = true
161 }
162 }
163
Ivan Lozano6a884432020-12-02 09:15:16 -0500164 if vendorSpecific {
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500165 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && (lib.buildShared() || lib.buildDylib() || lib.buildRlib())) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500166 mctx.ModuleErrorf("Rust vendor specific modules are currently only supported for rust_ffi_static modules.")
167 } else {
168 coreVariantNeeded = false
169 vendorVariants = append(vendorVariants, platformVndkVersion)
170 }
171 }
172
173 mod.Properties.CoreVariantNeeded = coreVariantNeeded
Ivan Lozanoe6d30982021-02-05 10:57:43 -0500174 mod.Properties.VendorRamdiskVariantNeeded = vendorRamdiskVariantNeeded
175
Ivan Lozano6a884432020-12-02 09:15:16 -0500176 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
177 mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, cc.VendorVariationPrefix+variant)
178 }
179
180}