blob: ac8c1b32cc8cb3ee7f7e57f9fd60d521015432a1 [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 {
27 return false
28}
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
55func (mod *Module) OnlyInRamdisk() bool {
56 // TODO(b/165791368)
57 return false
58}
59
60func (mod *Module) OnlyInRecovery() bool {
61 // TODO(b/165791368)
62 return false
63}
64
65func (mod *Module) OnlyInVendorRamdisk() bool {
66 return false
67}
68
69// Returns true when this module is configured to have core and vendor variants.
70func (mod *Module) HasVendorVariant() bool {
Justin Yunebcf0c52021-01-08 18:00:19 +090071 return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
Ivan Lozano6a884432020-12-02 09:15:16 -050072}
73
Justin Yuncbca3732021-02-03 19:24:13 +090074// Always returns false because rust modules do not support product variant.
75func (mod *Module) HasProductVariant() bool {
76 return Bool(mod.VendorProperties.Product_available)
77}
78
79func (mod *Module) HasNonSystemVariants() bool {
80 return mod.HasVendorVariant() || mod.HasProductVariant()
81}
82
Ivan Lozano6a884432020-12-02 09:15:16 -050083func (c *Module) InProduct() bool {
84 return false
85}
86
87func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
88 m := module.(*Module)
89 if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
90 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
91 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
92
93 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
94 // Hide other vendor variants to avoid collision.
95 vndkVersion := ctx.DeviceConfig().VndkVersion()
96 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
97 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -080098 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -050099 }
100 }
101}
102
103func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
104 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
105 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
106
107 // Rust does not support installing to the product image yet.
Justin Yunc0d8c492021-01-07 17:45:31 +0900108 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500109 mctx.PropertyErrorf("product_available",
110 "Rust modules do not yet support being available to the product image")
111 } else if mctx.ProductSpecific() {
112 mctx.PropertyErrorf("product_specific",
113 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900114 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500115 mctx.PropertyErrorf("double_loadable",
116 "Rust modules do not yet support double loading")
117 }
118
119 coreVariantNeeded := true
120 var vendorVariants []string
121
Justin Yunebcf0c52021-01-08 18:00:19 +0900122 if mod.HasVendorVariant() {
123 prop := "vendor_available"
124 if Bool(mod.VendorProperties.Odm_available) {
125 prop = "odm_available"
126 }
127
Ivan Lozano6a884432020-12-02 09:15:16 -0500128 if vendorSpecific {
Justin Yunebcf0c52021-01-08 18:00:19 +0900129 mctx.PropertyErrorf(prop,
130 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
Ivan Lozano6a884432020-12-02 09:15:16 -0500131 }
132
133 if lib, ok := mod.compiler.(libraryInterface); ok {
134 // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available.
135 // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library
136 // mutators until support is added.
137 //
138 // We can't check shared() here because image mutator is called before the library mutator, so we need to
139 // check buildShared()
140 if lib.buildShared() {
Justin Yunebcf0c52021-01-08 18:00:19 +0900141 mctx.PropertyErrorf(prop, "can only be set for rust_ffi_static modules.")
142 } else {
Ivan Lozano6a884432020-12-02 09:15:16 -0500143 vendorVariants = append(vendorVariants, platformVndkVersion)
144 }
145 }
146 }
147
148 if vendorSpecific {
149 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && !lib.static()) {
150 mctx.ModuleErrorf("Rust vendor specific modules are currently only supported for rust_ffi_static modules.")
151 } else {
152 coreVariantNeeded = false
153 vendorVariants = append(vendorVariants, platformVndkVersion)
154 }
155 }
156
157 mod.Properties.CoreVariantNeeded = coreVariantNeeded
158 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
159 mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, cc.VendorVariationPrefix+variant)
160 }
161
162}