Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 1 | // 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 | |
| 15 | package rust |
| 16 | |
| 17 | import ( |
| 18 | "strings" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/cc" |
| 22 | ) |
| 23 | |
| 24 | var _ android.ImageInterface = (*Module)(nil) |
| 25 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 26 | var _ cc.ImageMutatableModule = (*Module)(nil) |
| 27 | |
| 28 | func (mod *Module) VendorAvailable() bool { |
| 29 | return Bool(mod.VendorProperties.Vendor_available) |
| 30 | } |
| 31 | |
| 32 | func (mod *Module) OdmAvailable() bool { |
| 33 | return Bool(mod.VendorProperties.Odm_available) |
| 34 | } |
| 35 | |
| 36 | func (mod *Module) ProductAvailable() bool { |
| 37 | return false |
| 38 | } |
| 39 | |
| 40 | func (mod *Module) RamdiskAvailable() bool { |
| 41 | return false |
| 42 | } |
| 43 | |
| 44 | func (mod *Module) VendorRamdiskAvailable() bool { |
| 45 | return Bool(mod.Properties.Vendor_ramdisk_available) |
| 46 | } |
| 47 | |
| 48 | func (mod *Module) AndroidModuleBase() *android.ModuleBase { |
| 49 | return &mod.ModuleBase |
| 50 | } |
| 51 | |
| 52 | func (mod *Module) RecoveryAvailable() bool { |
| 53 | return false |
| 54 | } |
| 55 | |
| 56 | func (mod *Module) ExtraVariants() []string { |
| 57 | return mod.Properties.ExtraVariants |
| 58 | } |
| 59 | |
| 60 | func (mod *Module) AppendExtraVariant(extraVariant string) { |
| 61 | mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, extraVariant) |
| 62 | } |
| 63 | |
| 64 | func (mod *Module) SetRamdiskVariantNeeded(b bool) { |
| 65 | if b { |
| 66 | panic("Setting ramdisk variant needed for Rust module is unsupported: " + mod.BaseModuleName()) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func (mod *Module) SetVendorRamdiskVariantNeeded(b bool) { |
| 71 | mod.Properties.VendorRamdiskVariantNeeded = b |
| 72 | } |
| 73 | |
| 74 | func (mod *Module) SetRecoveryVariantNeeded(b bool) { |
| 75 | if b { |
| 76 | panic("Setting recovery variant needed for Rust module is unsupported: " + mod.BaseModuleName()) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | func (mod *Module) SetCoreVariantNeeded(b bool) { |
| 81 | mod.Properties.CoreVariantNeeded = b |
| 82 | } |
| 83 | |
| 84 | func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string { |
| 85 | panic("Rust modules do not support snapshotting: " + mod.BaseModuleName()) |
| 86 | } |
| 87 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 88 | func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 89 | return mod.Properties.VendorRamdiskVariantNeeded |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 93 | return mod.Properties.CoreVariantNeeded |
| 94 | } |
| 95 | |
| 96 | func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool { |
| 97 | return mod.InRamdisk() |
| 98 | } |
| 99 | |
| 100 | func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool { |
| 101 | return mod.InRecovery() |
| 102 | } |
| 103 | |
| 104 | func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string { |
| 105 | return mod.Properties.ExtraVariants |
| 106 | } |
| 107 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 108 | func (mod *Module) IsSnapshotPrebuilt() bool { |
| 109 | // Rust does not support prebuilts in its snapshots |
| 110 | return false |
| 111 | } |
| 112 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 113 | func (ctx *moduleContext) ProductSpecific() bool { |
| 114 | return false |
| 115 | } |
| 116 | |
| 117 | func (mod *Module) InRecovery() bool { |
| 118 | // TODO(b/165791368) |
| 119 | return false |
| 120 | } |
| 121 | |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 122 | func (mod *Module) InVendorRamdisk() bool { |
| 123 | return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk() |
| 124 | } |
| 125 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 126 | func (mod *Module) OnlyInRamdisk() bool { |
| 127 | // TODO(b/165791368) |
| 128 | return false |
| 129 | } |
| 130 | |
| 131 | func (mod *Module) OnlyInRecovery() bool { |
| 132 | // TODO(b/165791368) |
| 133 | return false |
| 134 | } |
| 135 | |
| 136 | func (mod *Module) OnlyInVendorRamdisk() bool { |
| 137 | return false |
| 138 | } |
| 139 | |
| 140 | // Returns true when this module is configured to have core and vendor variants. |
| 141 | func (mod *Module) HasVendorVariant() bool { |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 142 | return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available) |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 143 | } |
| 144 | |
Justin Yun | cbca373 | 2021-02-03 19:24:13 +0900 | [diff] [blame] | 145 | // Always returns false because rust modules do not support product variant. |
| 146 | func (mod *Module) HasProductVariant() bool { |
| 147 | return Bool(mod.VendorProperties.Product_available) |
| 148 | } |
| 149 | |
| 150 | func (mod *Module) HasNonSystemVariants() bool { |
| 151 | return mod.HasVendorVariant() || mod.HasProductVariant() |
| 152 | } |
| 153 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 154 | func (mod *Module) InProduct() bool { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 155 | return false |
| 156 | } |
| 157 | |
| 158 | func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) { |
| 159 | m := module.(*Module) |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 160 | if variant == android.VendorRamdiskVariation { |
| 161 | m.MakeAsPlatform() |
| 162 | } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 163 | m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix |
| 164 | m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix) |
| 165 | |
| 166 | // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION. |
| 167 | // Hide other vendor variants to avoid collision. |
| 168 | vndkVersion := ctx.DeviceConfig().VndkVersion() |
| 169 | if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion { |
| 170 | m.Properties.HideFromMake = true |
Colin Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 171 | m.HideFromMake() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 177 | // Rust does not support installing to the product image yet. |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 178 | if Bool(mod.VendorProperties.Product_available) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 179 | mctx.PropertyErrorf("product_available", |
| 180 | "Rust modules do not yet support being available to the product image") |
| 181 | } else if mctx.ProductSpecific() { |
| 182 | mctx.PropertyErrorf("product_specific", |
| 183 | "Rust modules do not yet support installing to the product image.") |
Justin Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 184 | } else if Bool(mod.VendorProperties.Double_loadable) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 185 | mctx.PropertyErrorf("double_loadable", |
| 186 | "Rust modules do not yet support double loading") |
| 187 | } |
| 188 | |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 189 | if mod.HasVendorVariant() { |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 190 | if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildShared() { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 191 | // Explicitly disallow rust_ffi variants which produce shared libraries from setting vendor_available. |
| 192 | // Vendor variants do not produce an error for dylibs, rlibs with dylib-std linkage are disabled in the respective library |
| 193 | // mutators until support is added. |
| 194 | // |
| 195 | // We can't check shared() here because image mutator is called before the library mutator, so we need to |
| 196 | // check buildShared() |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 197 | |
| 198 | mctx.PropertyErrorf("vendor_available", "cannot be set for rust_ffi or rust_ffi_shared modules.") |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 202 | if Bool(mod.Properties.Vendor_ramdisk_available) { |
| 203 | if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) { |
| 204 | mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.") |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 208 | vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 209 | if vendorSpecific { |
Ivan Lozano | e6d3098 | 2021-02-05 10:57:43 -0500 | [diff] [blame] | 210 | if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && (lib.buildShared() || lib.buildDylib() || lib.buildRlib())) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 211 | mctx.ModuleErrorf("Rust vendor specific modules are currently only supported for rust_ffi_static modules.") |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
Ivan Lozano | 699e218 | 2021-03-22 14:29:47 -0400 | [diff] [blame^] | 215 | cc.MutateImage(mctx, mod) |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 216 | } |