blob: 4951d2b7b65e4ea79db6c9de050ff175e02adb1a [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 {
71 return mod.IsVndk() || Bool(mod.VendorProperties.Vendor_available)
72}
73
74func (c *Module) VendorAvailable() bool {
75 return Bool(c.VendorProperties.Vendor_available)
76}
77
78func (c *Module) InProduct() bool {
79 return false
80}
81
82func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
83 m := module.(*Module)
84 if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
85 m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
86 m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
87
88 // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
89 // Hide other vendor variants to avoid collision.
90 vndkVersion := ctx.DeviceConfig().VndkVersion()
91 if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
92 m.Properties.HideFromMake = true
93 m.SkipInstall()
94 }
95 }
96}
97
98func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
99 vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
100 platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
101
102 // Rust does not support installing to the product image yet.
103 if mod.VendorProperties.Product_available != nil {
104 mctx.PropertyErrorf("product_available",
105 "Rust modules do not yet support being available to the product image")
106 } else if mctx.ProductSpecific() {
107 mctx.PropertyErrorf("product_specific",
108 "Rust modules do not yet support installing to the product image.")
109 } else if mod.VendorProperties.Double_loadable != nil {
110 mctx.PropertyErrorf("double_loadable",
111 "Rust modules do not yet support double loading")
112 }
113
114 coreVariantNeeded := true
115 var vendorVariants []string
116
117 if mod.VendorProperties.Vendor_available != nil {
118 if vendorSpecific {
119 mctx.PropertyErrorf("vendor_available",
120 "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific:true`")
121 }
122
123 if lib, ok := mod.compiler.(libraryInterface); ok {
124 // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available.
125 // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library
126 // mutators until support is added.
127 //
128 // We can't check shared() here because image mutator is called before the library mutator, so we need to
129 // check buildShared()
130 if lib.buildShared() {
131 mctx.PropertyErrorf("vendor_available",
132 "vendor_available can only be set for rust_ffi_static modules.")
133 } else if Bool(mod.VendorProperties.Vendor_available) == true {
134 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}