blob: 5ff10ae22e64be5052d5c8d1e1ae3c8eca1a26bc [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
74func (c *Module) InProduct() bool {
75 return false
76}
77
78func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
79 m := module.(*Module)
80 if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
81 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
82 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
83
84 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
85 // Hide other vendor variants to avoid collision.
86 vndkVersion := ctx.DeviceConfig().VndkVersion()
87 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
88 m.Properties.HideFromMake = true
Colin Crossa9c8c9f2020-12-16 10:20:23 -080089 m.HideFromMake()
Ivan Lozano6a884432020-12-02 09:15:16 -050090 }
91 }
92}
93
94func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
95 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
96 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
97
98 // Rust does not support installing to the product image yet.
Justin Yunc0d8c492021-01-07 17:45:31 +090099 if Bool(mod.VendorProperties.Product_available) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500100 mctx.PropertyErrorf("product_available",
101 "Rust modules do not yet support being available to the product image")
102 } else if mctx.ProductSpecific() {
103 mctx.PropertyErrorf("product_specific",
104 "Rust modules do not yet support installing to the product image.")
Justin Yunc0d8c492021-01-07 17:45:31 +0900105 } else if Bool(mod.VendorProperties.Double_loadable) {
Ivan Lozano6a884432020-12-02 09:15:16 -0500106 mctx.PropertyErrorf("double_loadable",
107 "Rust modules do not yet support double loading")
108 }
109
110 coreVariantNeeded := true
111 var vendorVariants []string
112
Justin Yunebcf0c52021-01-08 18:00:19 +0900113 if mod.HasVendorVariant() {
114 prop := "vendor_available"
115 if Bool(mod.VendorProperties.Odm_available) {
116 prop = "odm_available"
117 }
118
Ivan Lozano6a884432020-12-02 09:15:16 -0500119 if vendorSpecific {
Justin Yunebcf0c52021-01-08 18:00:19 +0900120 mctx.PropertyErrorf(prop,
121 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
Ivan Lozano6a884432020-12-02 09:15:16 -0500122 }
123
124 if lib, ok := mod.compiler.(libraryInterface); ok {
125 // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available.
126 // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library
127 // mutators until support is added.
128 //
129 // We can't check shared() here because image mutator is called before the library mutator, so we need to
130 // check buildShared()
131 if lib.buildShared() {
Justin Yunebcf0c52021-01-08 18:00:19 +0900132 mctx.PropertyErrorf(prop, "can only be set for rust_ffi_static modules.")
133 } else {
Ivan Lozano6a884432020-12-02 09:15:16 -0500134 vendorVariants = append(vendorVariants, platformVndkVersion)
135 }
136 }
137 }
138
139 if vendorSpecific {
140 if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && !lib.static()) {
141 mctx.ModuleErrorf("Rust vendor specific modules are currently only supported for rust_ffi_static modules.")
142 } else {
143 coreVariantNeeded = false
144 vendorVariants = append(vendorVariants, platformVndkVersion)
145 }
146 }
147
148 mod.Properties.CoreVariantNeeded = coreVariantNeeded
149 for _, variant := range android.FirstUniqueStrings(vendorVariants) {
150 mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, cc.VendorVariationPrefix+variant)
151 }
152
153}