image.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. package rust
  15. import (
  16. "strings"
  17. "android/soong/android"
  18. "android/soong/cc"
  19. )
  20. var _ android.ImageInterface = (*Module)(nil)
  21. var _ cc.ImageMutatableModule = (*Module)(nil)
  22. func (mod *Module) VendorAvailable() bool {
  23. return Bool(mod.VendorProperties.Vendor_available)
  24. }
  25. func (mod *Module) OdmAvailable() bool {
  26. return Bool(mod.VendorProperties.Odm_available)
  27. }
  28. func (mod *Module) ProductAvailable() bool {
  29. return Bool(mod.VendorProperties.Product_available)
  30. }
  31. func (mod *Module) RamdiskAvailable() bool {
  32. return Bool(mod.Properties.Ramdisk_available)
  33. }
  34. func (mod *Module) VendorRamdiskAvailable() bool {
  35. return Bool(mod.Properties.Vendor_ramdisk_available)
  36. }
  37. func (mod *Module) AndroidModuleBase() *android.ModuleBase {
  38. return &mod.ModuleBase
  39. }
  40. func (mod *Module) RecoveryAvailable() bool {
  41. return Bool(mod.Properties.Recovery_available)
  42. }
  43. func (mod *Module) ExtraVariants() []string {
  44. return mod.Properties.ExtraVariants
  45. }
  46. func (mod *Module) AppendExtraVariant(extraVariant string) {
  47. mod.Properties.ExtraVariants = append(mod.Properties.ExtraVariants, extraVariant)
  48. }
  49. func (mod *Module) SetRamdiskVariantNeeded(b bool) {
  50. mod.Properties.RamdiskVariantNeeded = b
  51. }
  52. func (mod *Module) SetVendorRamdiskVariantNeeded(b bool) {
  53. mod.Properties.VendorRamdiskVariantNeeded = b
  54. }
  55. func (mod *Module) SetRecoveryVariantNeeded(b bool) {
  56. mod.Properties.RecoveryVariantNeeded = b
  57. }
  58. func (mod *Module) SetCoreVariantNeeded(b bool) {
  59. mod.Properties.CoreVariantNeeded = b
  60. }
  61. func (mod *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
  62. if snapshot, ok := mod.compiler.(cc.SnapshotInterface); ok {
  63. return snapshot.Version()
  64. } else {
  65. mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
  66. return ""
  67. }
  68. }
  69. func (mod *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  70. return mod.Properties.VendorRamdiskVariantNeeded
  71. }
  72. func (mod *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  73. return mod.Properties.CoreVariantNeeded
  74. }
  75. func (mod *Module) RamdiskVariantNeeded(android.BaseModuleContext) bool {
  76. return mod.Properties.RamdiskVariantNeeded
  77. }
  78. func (mod *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  79. return false
  80. }
  81. func (mod *Module) RecoveryVariantNeeded(android.BaseModuleContext) bool {
  82. return mod.Properties.RecoveryVariantNeeded
  83. }
  84. func (mod *Module) ExtraImageVariations(android.BaseModuleContext) []string {
  85. return mod.Properties.ExtraVariants
  86. }
  87. func (mod *Module) IsSnapshotPrebuilt() bool {
  88. if p, ok := mod.compiler.(cc.SnapshotInterface); ok {
  89. return p.IsSnapshotPrebuilt()
  90. }
  91. return false
  92. }
  93. func (ctx *moduleContext) SocSpecific() bool {
  94. // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
  95. // module. As well as SoC specific modules, vendor variants must be installed to /vendor
  96. // unless they have "odm_available: true".
  97. return ctx.ModuleContext.SocSpecific() || (ctx.RustModule().InVendor() && !ctx.RustModule().VendorVariantToOdm())
  98. }
  99. func (ctx *moduleContext) DeviceSpecific() bool {
  100. // Some vendor variants want to be installed to /odm by setting "odm_available: true".
  101. return ctx.ModuleContext.DeviceSpecific() || (ctx.RustModule().InVendor() && ctx.RustModule().VendorVariantToOdm())
  102. }
  103. func (ctx *moduleContext) SystemExtSpecific() bool {
  104. return ctx.ModuleContext.SystemExtSpecific()
  105. }
  106. // Returns true when this module creates a vendor variant and wants to install the vendor variant
  107. // to the odm partition.
  108. func (c *Module) VendorVariantToOdm() bool {
  109. return Bool(c.VendorProperties.Odm_available)
  110. }
  111. func (ctx *moduleContext) ProductSpecific() bool {
  112. return ctx.ModuleContext.ProductSpecific() || ctx.RustModule().productSpecificModuleContext()
  113. }
  114. func (c *Module) productSpecificModuleContext() bool {
  115. // Additionally check if this module is inProduct() that means it is a "product" variant of a
  116. // module. As well as product specific modules, product variants must be installed to /product.
  117. return c.InProduct()
  118. }
  119. func (mod *Module) InRecovery() bool {
  120. return mod.ModuleBase.InRecovery() || mod.ModuleBase.InstallInRecovery()
  121. }
  122. func (mod *Module) InRamdisk() bool {
  123. return mod.ModuleBase.InRamdisk() || mod.ModuleBase.InstallInRamdisk()
  124. }
  125. func (mod *Module) InVendorRamdisk() bool {
  126. return mod.ModuleBase.InVendorRamdisk() || mod.ModuleBase.InstallInVendorRamdisk()
  127. }
  128. func (mod *Module) OnlyInRamdisk() bool {
  129. return mod.ModuleBase.InstallInRamdisk()
  130. }
  131. func (mod *Module) OnlyInRecovery() bool {
  132. return mod.ModuleBase.InstallInRecovery()
  133. }
  134. func (mod *Module) OnlyInVendorRamdisk() bool {
  135. return mod.ModuleBase.InstallInVendorRamdisk()
  136. }
  137. // Returns true when this module is configured to have core and vendor variants.
  138. func (mod *Module) HasVendorVariant() bool {
  139. return Bool(mod.VendorProperties.Vendor_available) || Bool(mod.VendorProperties.Odm_available)
  140. }
  141. // Always returns false because rust modules do not support product variant.
  142. func (mod *Module) HasProductVariant() bool {
  143. return Bool(mod.VendorProperties.Product_available)
  144. }
  145. func (mod *Module) HasNonSystemVariants() bool {
  146. return mod.HasVendorVariant() || mod.HasProductVariant()
  147. }
  148. func (mod *Module) InProduct() bool {
  149. return mod.Properties.ImageVariationPrefix == cc.ProductVariationPrefix
  150. }
  151. // Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
  152. func (mod *Module) InVendor() bool {
  153. return mod.Properties.ImageVariationPrefix == cc.VendorVariationPrefix
  154. }
  155. func (mod *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
  156. m := module.(*Module)
  157. if variant == android.VendorRamdiskVariation {
  158. m.MakeAsPlatform()
  159. } else if variant == android.RecoveryVariation {
  160. m.MakeAsPlatform()
  161. } else if strings.HasPrefix(variant, cc.VendorVariationPrefix) {
  162. m.Properties.ImageVariationPrefix = cc.VendorVariationPrefix
  163. m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.VendorVariationPrefix)
  164. // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
  165. // Hide other vendor variants to avoid collision.
  166. vndkVersion := ctx.DeviceConfig().VndkVersion()
  167. if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
  168. m.Properties.HideFromMake = true
  169. m.HideFromMake()
  170. }
  171. } else if strings.HasPrefix(variant, cc.ProductVariationPrefix) {
  172. m.Properties.ImageVariationPrefix = cc.ProductVariationPrefix
  173. m.Properties.VndkVersion = strings.TrimPrefix(variant, cc.ProductVariationPrefix)
  174. }
  175. }
  176. func (mod *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
  177. // Rust does not support installing to the product image yet.
  178. vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
  179. if Bool(mod.VendorProperties.Double_loadable) {
  180. mctx.PropertyErrorf("double_loadable",
  181. "Rust modules do not yet support double loading")
  182. }
  183. if Bool(mod.Properties.Vendor_ramdisk_available) {
  184. if lib, ok := mod.compiler.(libraryInterface); !ok || (ok && lib.buildShared()) {
  185. mctx.PropertyErrorf("vendor_ramdisk_available", "cannot be set for rust_ffi or rust_ffi_shared modules.")
  186. }
  187. }
  188. if vendorSpecific {
  189. if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
  190. mctx.PropertyErrorf("vendor", "Vendor-only dylibs are not yet supported, use rust_library_rlib.")
  191. }
  192. }
  193. if mctx.ProductSpecific() {
  194. if lib, ok := mod.compiler.(libraryInterface); ok && lib.buildDylib() {
  195. mctx.PropertyErrorf("product", "Product-only dylibs are not yet supported, use rust_library_rlib.")
  196. }
  197. }
  198. cc.MutateImage(mctx, mod)
  199. if !mod.Properties.CoreVariantNeeded || mod.HasNonSystemVariants() {
  200. if _, ok := mod.compiler.(*prebuiltLibraryDecorator); ok {
  201. // Rust does not support prebuilt libraries on non-System images.
  202. mctx.ModuleErrorf("Rust prebuilt modules not supported for non-system images.")
  203. }
  204. }
  205. }