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 | |
| 26 | func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 27 | return false |
| 28 | } |
| 29 | |
| 30 | func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 31 | return mod.Properties.CoreVariantNeeded |
| 32 | } |
| 33 | |
| 34 | func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool { |
| 35 | return mod.InRamdisk() |
| 36 | } |
| 37 | |
| 38 | func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool { |
| 39 | return mod.InRecovery() |
| 40 | } |
| 41 | |
| 42 | func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string { |
| 43 | return mod.Properties.ExtraVariants |
| 44 | } |
| 45 | |
| 46 | func (ctx *moduleContext) ProductSpecific() bool { |
| 47 | return false |
| 48 | } |
| 49 | |
| 50 | func (mod *Module) InRecovery() bool { |
| 51 | // TODO(b/165791368) |
| 52 | return false |
| 53 | } |
| 54 | |
| 55 | func (mod *Module) OnlyInRamdisk() bool { |
| 56 | // TODO(b/165791368) |
| 57 | return false |
| 58 | } |
| 59 | |
| 60 | func (mod *Module) OnlyInRecovery() bool { |
| 61 | // TODO(b/165791368) |
| 62 | return false |
| 63 | } |
| 64 | |
| 65 | func (mod *Module) OnlyInVendorRamdisk() bool { |
| 66 | return false |
| 67 | } |
| 68 | |
| 69 | // Returns true when this module is configured to have core and vendor variants. |
| 70 | func (mod *Module) HasVendorVariant() bool { |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 71 | return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available) |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Justin Yun | cbca373 | 2021-02-03 19:24:13 +0900 | [diff] [blame^] | 74 | // Always returns false because rust modules do not support product variant. |
| 75 | func (mod *Module) HasProductVariant() bool { |
| 76 | return Bool(mod.VendorProperties.Product_available) |
| 77 | } |
| 78 | |
| 79 | func (mod *Module) HasNonSystemVariants() bool { |
| 80 | return mod.HasVendorVariant() || mod.HasProductVariant() |
| 81 | } |
| 82 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 83 | func (c *Module) InProduct() bool { |
| 84 | return false |
| 85 | } |
| 86 | |
| 87 | func (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 Cross | a9c8c9f | 2020-12-16 10:20:23 -0800 | [diff] [blame] | 98 | m.HideFromMake() |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | func (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 Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 108 | if Bool(mod.VendorProperties.Product_available) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 109 | 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 Yun | c0d8c49 | 2021-01-07 17:45:31 +0900 | [diff] [blame] | 114 | } else if Bool(mod.VendorProperties.Double_loadable) { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 115 | mctx.PropertyErrorf("double_loadable", |
| 116 | "Rust modules do not yet support double loading") |
| 117 | } |
| 118 | |
| 119 | coreVariantNeeded := true |
| 120 | var vendorVariants []string |
| 121 | |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 122 | if mod.HasVendorVariant() { |
| 123 | prop := "vendor_available" |
| 124 | if Bool(mod.VendorProperties.Odm_available) { |
| 125 | prop = "odm_available" |
| 126 | } |
| 127 | |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 128 | if vendorSpecific { |
Justin Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 129 | mctx.PropertyErrorf(prop, |
| 130 | "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`") |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 131 | } |
| 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 Yun | ebcf0c5 | 2021-01-08 18:00:19 +0900 | [diff] [blame] | 141 | mctx.PropertyErrorf(prop, "can only be set for rust_ffi_static modules.") |
| 142 | } else { |
Ivan Lozano | 6a88443 | 2020-12-02 09:15:16 -0500 | [diff] [blame] | 143 | 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 | } |