image.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 cc
  15. // This file contains image variant related things, including image mutator functions, utility
  16. // functions to determine where a module is installed, etc.
  17. import (
  18. "fmt"
  19. "reflect"
  20. "strings"
  21. "android/soong/android"
  22. "android/soong/snapshot"
  23. )
  24. var _ android.ImageInterface = (*Module)(nil)
  25. type ImageVariantType string
  26. const (
  27. coreImageVariant ImageVariantType = "core"
  28. vendorImageVariant ImageVariantType = "vendor"
  29. productImageVariant ImageVariantType = "product"
  30. ramdiskImageVariant ImageVariantType = "ramdisk"
  31. vendorRamdiskImageVariant ImageVariantType = "vendor_ramdisk"
  32. recoveryImageVariant ImageVariantType = "recovery"
  33. hostImageVariant ImageVariantType = "host"
  34. )
  35. const (
  36. // VendorVariationPrefix is the variant prefix used for /vendor code that compiles
  37. // against the VNDK.
  38. VendorVariationPrefix = "vendor."
  39. // ProductVariationPrefix is the variant prefix used for /product code that compiles
  40. // against the VNDK.
  41. ProductVariationPrefix = "product."
  42. )
  43. func (ctx *moduleContext) ProductSpecific() bool {
  44. return ctx.ModuleContext.ProductSpecific() || ctx.mod.productSpecificModuleContext()
  45. }
  46. func (ctx *moduleContext) SocSpecific() bool {
  47. return ctx.ModuleContext.SocSpecific() || ctx.mod.socSpecificModuleContext()
  48. }
  49. func (ctx *moduleContext) DeviceSpecific() bool {
  50. return ctx.ModuleContext.DeviceSpecific() || ctx.mod.deviceSpecificModuleContext()
  51. }
  52. func (ctx *moduleContextImpl) inProduct() bool {
  53. return ctx.mod.InProduct()
  54. }
  55. func (ctx *moduleContextImpl) inVendor() bool {
  56. return ctx.mod.InVendor()
  57. }
  58. func (ctx *moduleContextImpl) inRamdisk() bool {
  59. return ctx.mod.InRamdisk()
  60. }
  61. func (ctx *moduleContextImpl) inVendorRamdisk() bool {
  62. return ctx.mod.InVendorRamdisk()
  63. }
  64. func (ctx *moduleContextImpl) inRecovery() bool {
  65. return ctx.mod.InRecovery()
  66. }
  67. func (c *Module) productSpecificModuleContext() bool {
  68. // Additionally check if this module is inProduct() that means it is a "product" variant of a
  69. // module. As well as product specific modules, product variants must be installed to /product.
  70. return c.InProduct()
  71. }
  72. func (c *Module) socSpecificModuleContext() bool {
  73. // Additionally check if this module is inVendor() that means it is a "vendor" variant of a
  74. // module. As well as SoC specific modules, vendor variants must be installed to /vendor
  75. // unless they have "odm_available: true".
  76. return c.HasVendorVariant() && c.InVendor() && !c.VendorVariantToOdm()
  77. }
  78. func (c *Module) deviceSpecificModuleContext() bool {
  79. // Some vendor variants want to be installed to /odm by setting "odm_available: true".
  80. return c.InVendor() && c.VendorVariantToOdm()
  81. }
  82. // Returns true when this module is configured to have core and vendor variants.
  83. func (c *Module) HasVendorVariant() bool {
  84. return Bool(c.VendorProperties.Vendor_available) || Bool(c.VendorProperties.Odm_available)
  85. }
  86. // Returns true when this module creates a vendor variant and wants to install the vendor variant
  87. // to the odm partition.
  88. func (c *Module) VendorVariantToOdm() bool {
  89. return Bool(c.VendorProperties.Odm_available)
  90. }
  91. // Returns true when this module is configured to have core and product variants.
  92. func (c *Module) HasProductVariant() bool {
  93. return Bool(c.VendorProperties.Product_available)
  94. }
  95. // Returns true when this module is configured to have core and either product or vendor variants.
  96. func (c *Module) HasNonSystemVariants() bool {
  97. return c.HasVendorVariant() || c.HasProductVariant()
  98. }
  99. // Returns true if the module is "product" variant. Usually these modules are installed in /product
  100. func (c *Module) InProduct() bool {
  101. return c.Properties.ImageVariationPrefix == ProductVariationPrefix
  102. }
  103. // Returns true if the module is "vendor" variant. Usually these modules are installed in /vendor
  104. func (c *Module) InVendor() bool {
  105. return c.Properties.ImageVariationPrefix == VendorVariationPrefix
  106. }
  107. func (c *Module) InRamdisk() bool {
  108. return c.ModuleBase.InRamdisk() || c.ModuleBase.InstallInRamdisk()
  109. }
  110. func (c *Module) InVendorRamdisk() bool {
  111. return c.ModuleBase.InVendorRamdisk() || c.ModuleBase.InstallInVendorRamdisk()
  112. }
  113. func (c *Module) InRecovery() bool {
  114. return c.ModuleBase.InRecovery() || c.ModuleBase.InstallInRecovery()
  115. }
  116. func (c *Module) OnlyInRamdisk() bool {
  117. return c.ModuleBase.InstallInRamdisk()
  118. }
  119. func (c *Module) OnlyInVendorRamdisk() bool {
  120. return c.ModuleBase.InstallInVendorRamdisk()
  121. }
  122. func (c *Module) OnlyInRecovery() bool {
  123. return c.ModuleBase.InstallInRecovery()
  124. }
  125. func visitPropsAndCompareVendorAndProductProps(v reflect.Value) bool {
  126. if v.Kind() != reflect.Struct {
  127. return true
  128. }
  129. for i := 0; i < v.NumField(); i++ {
  130. prop := v.Field(i)
  131. if prop.Kind() == reflect.Struct && v.Type().Field(i).Name == "Target" {
  132. vendor_prop := prop.FieldByName("Vendor")
  133. product_prop := prop.FieldByName("Product")
  134. if vendor_prop.Kind() != reflect.Struct && product_prop.Kind() != reflect.Struct {
  135. // Neither Target.Vendor nor Target.Product is defined
  136. continue
  137. }
  138. if vendor_prop.Kind() != reflect.Struct || product_prop.Kind() != reflect.Struct ||
  139. !reflect.DeepEqual(vendor_prop.Interface(), product_prop.Interface()) {
  140. // If only one of either Target.Vendor or Target.Product is
  141. // defined or they have different values, it fails the build
  142. // since VNDK must have the same properties for both vendor
  143. // and product variants.
  144. return false
  145. }
  146. } else if !visitPropsAndCompareVendorAndProductProps(prop) {
  147. // Visit the substructures to find Target.Vendor and Target.Product
  148. return false
  149. }
  150. }
  151. return true
  152. }
  153. // In the case of VNDK, vendor and product variants must have the same properties.
  154. // VNDK installs only one file and shares it for both vendor and product modules on
  155. // runtime. We may not define different versions of a VNDK lib for each partition.
  156. // This function is used only for the VNDK modules that is available to both vendor
  157. // and product partitions.
  158. func (c *Module) compareVendorAndProductProps() bool {
  159. if !c.IsVndk() && !Bool(c.VendorProperties.Product_available) {
  160. panic(fmt.Errorf("This is only for product available VNDK libs. %q is not a VNDK library or not product available", c.Name()))
  161. }
  162. for _, properties := range c.GetProperties() {
  163. if !visitPropsAndCompareVendorAndProductProps(reflect.ValueOf(properties).Elem()) {
  164. return false
  165. }
  166. }
  167. return true
  168. }
  169. // ImageMutatableModule provides a common image mutation interface for LinkableInterface modules.
  170. type ImageMutatableModule interface {
  171. android.Module
  172. LinkableInterface
  173. // AndroidModuleBase returns the android.ModuleBase for this module
  174. AndroidModuleBase() *android.ModuleBase
  175. // VendorAvailable returns true if this module is available on the vendor image.
  176. VendorAvailable() bool
  177. // OdmAvailable returns true if this module is available on the odm image.
  178. OdmAvailable() bool
  179. // ProductAvailable returns true if this module is available on the product image.
  180. ProductAvailable() bool
  181. // RamdiskAvailable returns true if this module is available on the ramdisk image.
  182. RamdiskAvailable() bool
  183. // RecoveryAvailable returns true if this module is available on the recovery image.
  184. RecoveryAvailable() bool
  185. // VendorRamdiskAvailable returns true if this module is available on the vendor ramdisk image.
  186. VendorRamdiskAvailable() bool
  187. // IsSnapshotPrebuilt returns true if this module is a snapshot prebuilt.
  188. IsSnapshotPrebuilt() bool
  189. // SnapshotVersion returns the snapshot version for this module.
  190. SnapshotVersion(mctx android.BaseModuleContext) string
  191. // SdkVersion returns the SDK version for this module.
  192. SdkVersion() string
  193. // ExtraVariants returns the list of extra variants this module requires.
  194. ExtraVariants() []string
  195. // AppendExtraVariant returns an extra variant to the list of extra variants this module requires.
  196. AppendExtraVariant(extraVariant string)
  197. // SetRamdiskVariantNeeded sets whether the Ramdisk Variant is needed.
  198. SetRamdiskVariantNeeded(b bool)
  199. // SetVendorRamdiskVariantNeeded sets whether the Vendor Ramdisk Variant is needed.
  200. SetVendorRamdiskVariantNeeded(b bool)
  201. // SetRecoveryVariantNeeded sets whether the Recovery Variant is needed.
  202. SetRecoveryVariantNeeded(b bool)
  203. // SetCoreVariantNeeded sets whether the Core Variant is needed.
  204. SetCoreVariantNeeded(b bool)
  205. }
  206. var _ ImageMutatableModule = (*Module)(nil)
  207. func (m *Module) ImageMutatorBegin(mctx android.BaseModuleContext) {
  208. m.CheckVndkProperties(mctx)
  209. MutateImage(mctx, m)
  210. }
  211. // CheckVndkProperties checks whether the VNDK-related properties are set correctly.
  212. // If properties are not set correctly, results in a module context property error.
  213. func (m *Module) CheckVndkProperties(mctx android.BaseModuleContext) {
  214. vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
  215. productSpecific := mctx.ProductSpecific()
  216. if vndkdep := m.vndkdep; vndkdep != nil {
  217. if vndkdep.isVndk() {
  218. if vendorSpecific || productSpecific {
  219. if !vndkdep.isVndkExt() {
  220. mctx.PropertyErrorf("vndk",
  221. "must set `extends: \"...\"` to vndk extension")
  222. } else if Bool(m.VendorProperties.Vendor_available) {
  223. mctx.PropertyErrorf("vendor_available",
  224. "must not set at the same time as `vndk: {extends: \"...\"}`")
  225. } else if Bool(m.VendorProperties.Product_available) {
  226. mctx.PropertyErrorf("product_available",
  227. "must not set at the same time as `vndk: {extends: \"...\"}`")
  228. }
  229. } else {
  230. if vndkdep.isVndkExt() {
  231. mctx.PropertyErrorf("vndk",
  232. "must set `vendor: true` or `product_specific: true` to set `extends: %q`",
  233. m.getVndkExtendsModuleName())
  234. }
  235. if !Bool(m.VendorProperties.Vendor_available) {
  236. mctx.PropertyErrorf("vndk",
  237. "vendor_available must be set to true when `vndk: {enabled: true}`")
  238. }
  239. if Bool(m.VendorProperties.Product_available) {
  240. // If a VNDK module creates both product and vendor variants, they
  241. // must have the same properties since they share a single VNDK
  242. // library on runtime.
  243. if !m.compareVendorAndProductProps() {
  244. mctx.ModuleErrorf("product properties must have the same values with the vendor properties for VNDK modules")
  245. }
  246. }
  247. }
  248. } else {
  249. if vndkdep.isVndkSp() {
  250. mctx.PropertyErrorf("vndk",
  251. "must set `enabled: true` to set `support_system_process: true`")
  252. }
  253. if vndkdep.isVndkExt() {
  254. mctx.PropertyErrorf("vndk",
  255. "must set `enabled: true` to set `extends: %q`",
  256. m.getVndkExtendsModuleName())
  257. }
  258. }
  259. }
  260. }
  261. func (m *Module) VendorAvailable() bool {
  262. return Bool(m.VendorProperties.Vendor_available)
  263. }
  264. func (m *Module) OdmAvailable() bool {
  265. return Bool(m.VendorProperties.Odm_available)
  266. }
  267. func (m *Module) ProductAvailable() bool {
  268. return Bool(m.VendorProperties.Product_available)
  269. }
  270. func (m *Module) RamdiskAvailable() bool {
  271. return Bool(m.Properties.Ramdisk_available)
  272. }
  273. func (m *Module) VendorRamdiskAvailable() bool {
  274. return Bool(m.Properties.Vendor_ramdisk_available)
  275. }
  276. func (m *Module) AndroidModuleBase() *android.ModuleBase {
  277. return &m.ModuleBase
  278. }
  279. func (m *Module) RecoveryAvailable() bool {
  280. return Bool(m.Properties.Recovery_available)
  281. }
  282. func (m *Module) ExtraVariants() []string {
  283. return m.Properties.ExtraVersionedImageVariations
  284. }
  285. func (m *Module) AppendExtraVariant(extraVariant string) {
  286. m.Properties.ExtraVersionedImageVariations = append(m.Properties.ExtraVersionedImageVariations, extraVariant)
  287. }
  288. func (m *Module) SetRamdiskVariantNeeded(b bool) {
  289. m.Properties.RamdiskVariantNeeded = b
  290. }
  291. func (m *Module) SetVendorRamdiskVariantNeeded(b bool) {
  292. m.Properties.VendorRamdiskVariantNeeded = b
  293. }
  294. func (m *Module) SetRecoveryVariantNeeded(b bool) {
  295. m.Properties.RecoveryVariantNeeded = b
  296. }
  297. func (m *Module) SetCoreVariantNeeded(b bool) {
  298. m.Properties.CoreVariantNeeded = b
  299. }
  300. func (m *Module) SnapshotVersion(mctx android.BaseModuleContext) string {
  301. if snapshot, ok := m.linker.(SnapshotInterface); ok {
  302. return snapshot.Version()
  303. } else {
  304. mctx.ModuleErrorf("version is unknown for snapshot prebuilt")
  305. // Should we be panicking here instead?
  306. return ""
  307. }
  308. }
  309. func (m *Module) KernelHeadersDecorator() bool {
  310. if _, ok := m.linker.(*kernelHeadersDecorator); ok {
  311. return true
  312. }
  313. return false
  314. }
  315. // MutateImage handles common image mutations for ImageMutatableModule interfaces.
  316. func MutateImage(mctx android.BaseModuleContext, m ImageMutatableModule) {
  317. // Validation check
  318. vendorSpecific := mctx.SocSpecific() || mctx.DeviceSpecific()
  319. productSpecific := mctx.ProductSpecific()
  320. if m.VendorAvailable() {
  321. if vendorSpecific {
  322. mctx.PropertyErrorf("vendor_available",
  323. "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
  324. }
  325. if m.OdmAvailable() {
  326. mctx.PropertyErrorf("vendor_available",
  327. "doesn't make sense at the same time as `odm_available: true`")
  328. }
  329. }
  330. if m.OdmAvailable() {
  331. if vendorSpecific {
  332. mctx.PropertyErrorf("odm_available",
  333. "doesn't make sense at the same time as `vendor: true`, `proprietary: true`, or `device_specific: true`")
  334. }
  335. }
  336. if m.ProductAvailable() {
  337. if productSpecific {
  338. mctx.PropertyErrorf("product_available",
  339. "doesn't make sense at the same time as `product_specific: true`")
  340. }
  341. if vendorSpecific {
  342. mctx.PropertyErrorf("product_available",
  343. "cannot provide product variant from a vendor module. Please use `product_specific: true` with `vendor_available: true`")
  344. }
  345. }
  346. var coreVariantNeeded bool = false
  347. var ramdiskVariantNeeded bool = false
  348. var vendorRamdiskVariantNeeded bool = false
  349. var recoveryVariantNeeded bool = false
  350. var vendorVariants []string
  351. var productVariants []string
  352. platformVndkVersion := mctx.DeviceConfig().PlatformVndkVersion()
  353. boardVndkVersion := mctx.DeviceConfig().VndkVersion()
  354. productVndkVersion := mctx.DeviceConfig().ProductVndkVersion()
  355. recoverySnapshotVersion := mctx.DeviceConfig().RecoverySnapshotVersion()
  356. usingRecoverySnapshot := recoverySnapshotVersion != "current" &&
  357. recoverySnapshotVersion != ""
  358. needVndkVersionVendorVariantForLlndk := false
  359. if boardVndkVersion != "" {
  360. boardVndkApiLevel, err := android.ApiLevelFromUser(mctx, boardVndkVersion)
  361. if err == nil && !boardVndkApiLevel.IsPreview() {
  362. // VNDK snapshot newer than v30 has LLNDK stub libraries.
  363. // Only the VNDK version less than or equal to v30 requires generating the vendor
  364. // variant of the VNDK version from the source tree.
  365. needVndkVersionVendorVariantForLlndk = boardVndkApiLevel.LessThanOrEqualTo(android.ApiLevelOrPanic(mctx, "30"))
  366. }
  367. }
  368. if boardVndkVersion == "current" {
  369. boardVndkVersion = platformVndkVersion
  370. }
  371. if productVndkVersion == "current" {
  372. productVndkVersion = platformVndkVersion
  373. }
  374. if m.NeedsLlndkVariants() {
  375. // This is an LLNDK library. The implementation of the library will be on /system,
  376. // and vendor and product variants will be created with LLNDK stubs.
  377. // The LLNDK libraries need vendor variants even if there is no VNDK.
  378. coreVariantNeeded = true
  379. if platformVndkVersion != "" {
  380. vendorVariants = append(vendorVariants, platformVndkVersion)
  381. productVariants = append(productVariants, platformVndkVersion)
  382. }
  383. // Generate vendor variants for boardVndkVersion only if the VNDK snapshot does not
  384. // provide the LLNDK stub libraries.
  385. if needVndkVersionVendorVariantForLlndk {
  386. vendorVariants = append(vendorVariants, boardVndkVersion)
  387. }
  388. if productVndkVersion != "" {
  389. productVariants = append(productVariants, productVndkVersion)
  390. }
  391. } else if m.NeedsVendorPublicLibraryVariants() {
  392. // A vendor public library has the implementation on /vendor, with stub variants
  393. // for system and product.
  394. coreVariantNeeded = true
  395. vendorVariants = append(vendorVariants, boardVndkVersion)
  396. if platformVndkVersion != "" {
  397. productVariants = append(productVariants, platformVndkVersion)
  398. }
  399. if productVndkVersion != "" {
  400. productVariants = append(productVariants, productVndkVersion)
  401. }
  402. } else if boardVndkVersion == "" {
  403. // If the device isn't compiling against the VNDK, we always
  404. // use the core mode.
  405. coreVariantNeeded = true
  406. } else if m.IsSnapshotPrebuilt() {
  407. // Make vendor variants only for the versions in BOARD_VNDK_VERSION and
  408. // PRODUCT_EXTRA_VNDK_VERSIONS.
  409. if m.InstallInRecovery() {
  410. recoveryVariantNeeded = true
  411. } else {
  412. vendorVariants = append(vendorVariants, m.SnapshotVersion(mctx))
  413. }
  414. } else if m.HasNonSystemVariants() && !m.IsVndkExt() {
  415. // This will be available to /system unless it is product_specific
  416. // which will be handled later.
  417. coreVariantNeeded = true
  418. // We assume that modules under proprietary paths are compatible for
  419. // BOARD_VNDK_VERSION. The other modules are regarded as AOSP, or
  420. // PLATFORM_VNDK_VERSION.
  421. if m.HasVendorVariant() {
  422. if snapshot.IsVendorProprietaryModule(mctx) {
  423. vendorVariants = append(vendorVariants, boardVndkVersion)
  424. } else {
  425. vendorVariants = append(vendorVariants, platformVndkVersion)
  426. }
  427. }
  428. // product_available modules are available to /product.
  429. if m.HasProductVariant() {
  430. productVariants = append(productVariants, platformVndkVersion)
  431. // VNDK is always PLATFORM_VNDK_VERSION
  432. if !m.IsVndk() {
  433. productVariants = append(productVariants, productVndkVersion)
  434. }
  435. }
  436. } else if vendorSpecific && m.SdkVersion() == "" {
  437. // This will be available in /vendor (or /odm) only
  438. // kernel_headers is a special module type whose exported headers
  439. // are coming from DeviceKernelHeaders() which is always vendor
  440. // dependent. They'll always have both vendor variants.
  441. // For other modules, we assume that modules under proprietary
  442. // paths are compatible for BOARD_VNDK_VERSION. The other modules
  443. // are regarded as AOSP, which is PLATFORM_VNDK_VERSION.
  444. if m.KernelHeadersDecorator() {
  445. vendorVariants = append(vendorVariants,
  446. platformVndkVersion,
  447. boardVndkVersion,
  448. )
  449. } else if snapshot.IsVendorProprietaryModule(mctx) {
  450. vendorVariants = append(vendorVariants, boardVndkVersion)
  451. } else {
  452. vendorVariants = append(vendorVariants, platformVndkVersion)
  453. }
  454. } else {
  455. // This is either in /system (or similar: /data), or is a
  456. // module built with the NDK. Modules built with the NDK
  457. // will be restricted using the existing link type checks.
  458. coreVariantNeeded = true
  459. }
  460. if boardVndkVersion != "" && productVndkVersion != "" {
  461. if coreVariantNeeded && productSpecific && m.SdkVersion() == "" {
  462. // The module has "product_specific: true" that does not create core variant.
  463. coreVariantNeeded = false
  464. productVariants = append(productVariants, productVndkVersion)
  465. }
  466. } else {
  467. // Unless PRODUCT_PRODUCT_VNDK_VERSION is set, product partition has no
  468. // restriction to use system libs.
  469. // No product variants defined in this case.
  470. productVariants = []string{}
  471. }
  472. if m.RamdiskAvailable() {
  473. ramdiskVariantNeeded = true
  474. }
  475. if m.AndroidModuleBase().InstallInRamdisk() {
  476. ramdiskVariantNeeded = true
  477. coreVariantNeeded = false
  478. }
  479. if m.VendorRamdiskAvailable() {
  480. vendorRamdiskVariantNeeded = true
  481. }
  482. if m.AndroidModuleBase().InstallInVendorRamdisk() {
  483. vendorRamdiskVariantNeeded = true
  484. coreVariantNeeded = false
  485. }
  486. if m.RecoveryAvailable() {
  487. recoveryVariantNeeded = true
  488. }
  489. if m.AndroidModuleBase().InstallInRecovery() {
  490. recoveryVariantNeeded = true
  491. coreVariantNeeded = false
  492. }
  493. // If using a snapshot, the recovery variant under AOSP directories is not needed,
  494. // except for kernel headers, which needs all variants.
  495. if !m.KernelHeadersDecorator() &&
  496. !m.IsSnapshotPrebuilt() &&
  497. usingRecoverySnapshot &&
  498. !snapshot.IsRecoveryProprietaryModule(mctx) {
  499. recoveryVariantNeeded = false
  500. }
  501. for _, variant := range android.FirstUniqueStrings(vendorVariants) {
  502. m.AppendExtraVariant(VendorVariationPrefix + variant)
  503. }
  504. for _, variant := range android.FirstUniqueStrings(productVariants) {
  505. m.AppendExtraVariant(ProductVariationPrefix + variant)
  506. }
  507. m.SetRamdiskVariantNeeded(ramdiskVariantNeeded)
  508. m.SetVendorRamdiskVariantNeeded(vendorRamdiskVariantNeeded)
  509. m.SetRecoveryVariantNeeded(recoveryVariantNeeded)
  510. m.SetCoreVariantNeeded(coreVariantNeeded)
  511. // Disable the module if no variants are needed.
  512. if !ramdiskVariantNeeded &&
  513. !recoveryVariantNeeded &&
  514. !coreVariantNeeded &&
  515. len(m.ExtraVariants()) == 0 {
  516. m.Disable()
  517. }
  518. }
  519. func (c *Module) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  520. return c.Properties.CoreVariantNeeded
  521. }
  522. func (c *Module) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  523. return c.Properties.RamdiskVariantNeeded
  524. }
  525. func (c *Module) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  526. return c.Properties.VendorRamdiskVariantNeeded
  527. }
  528. func (c *Module) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  529. return false
  530. }
  531. func (c *Module) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
  532. return c.Properties.RecoveryVariantNeeded
  533. }
  534. func (c *Module) ExtraImageVariations(ctx android.BaseModuleContext) []string {
  535. return c.Properties.ExtraVersionedImageVariations
  536. }
  537. func squashVendorSrcs(m *Module) {
  538. if lib, ok := m.compiler.(*libraryDecorator); ok {
  539. lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
  540. lib.baseCompiler.Properties.Target.Vendor.Srcs...)
  541. lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
  542. lib.baseCompiler.Properties.Target.Vendor.Exclude_srcs...)
  543. lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
  544. lib.baseCompiler.Properties.Target.Vendor.Exclude_generated_sources...)
  545. }
  546. }
  547. func squashProductSrcs(m *Module) {
  548. if lib, ok := m.compiler.(*libraryDecorator); ok {
  549. lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
  550. lib.baseCompiler.Properties.Target.Product.Srcs...)
  551. lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
  552. lib.baseCompiler.Properties.Target.Product.Exclude_srcs...)
  553. lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
  554. lib.baseCompiler.Properties.Target.Product.Exclude_generated_sources...)
  555. }
  556. }
  557. func squashRecoverySrcs(m *Module) {
  558. if lib, ok := m.compiler.(*libraryDecorator); ok {
  559. lib.baseCompiler.Properties.Srcs = append(lib.baseCompiler.Properties.Srcs,
  560. lib.baseCompiler.Properties.Target.Recovery.Srcs...)
  561. lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs,
  562. lib.baseCompiler.Properties.Target.Recovery.Exclude_srcs...)
  563. lib.baseCompiler.Properties.Exclude_generated_sources = append(lib.baseCompiler.Properties.Exclude_generated_sources,
  564. lib.baseCompiler.Properties.Target.Recovery.Exclude_generated_sources...)
  565. }
  566. }
  567. func squashVendorRamdiskSrcs(m *Module) {
  568. if lib, ok := m.compiler.(*libraryDecorator); ok {
  569. lib.baseCompiler.Properties.Exclude_srcs = append(lib.baseCompiler.Properties.Exclude_srcs, lib.baseCompiler.Properties.Target.Vendor_ramdisk.Exclude_srcs...)
  570. }
  571. }
  572. func (c *Module) SetImageVariation(ctx android.BaseModuleContext, variant string, module android.Module) {
  573. m := module.(*Module)
  574. if variant == android.RamdiskVariation {
  575. m.MakeAsPlatform()
  576. } else if variant == android.VendorRamdiskVariation {
  577. m.MakeAsPlatform()
  578. squashVendorRamdiskSrcs(m)
  579. } else if variant == android.RecoveryVariation {
  580. m.MakeAsPlatform()
  581. squashRecoverySrcs(m)
  582. } else if strings.HasPrefix(variant, VendorVariationPrefix) {
  583. m.Properties.ImageVariationPrefix = VendorVariationPrefix
  584. m.Properties.VndkVersion = strings.TrimPrefix(variant, VendorVariationPrefix)
  585. squashVendorSrcs(m)
  586. // Makefile shouldn't know vendor modules other than BOARD_VNDK_VERSION.
  587. // Hide other vendor variants to avoid collision.
  588. vndkVersion := ctx.DeviceConfig().VndkVersion()
  589. if vndkVersion != "current" && vndkVersion != "" && vndkVersion != m.Properties.VndkVersion {
  590. m.Properties.HideFromMake = true
  591. m.HideFromMake()
  592. }
  593. } else if strings.HasPrefix(variant, ProductVariationPrefix) {
  594. m.Properties.ImageVariationPrefix = ProductVariationPrefix
  595. m.Properties.VndkVersion = strings.TrimPrefix(variant, ProductVariationPrefix)
  596. squashProductSrcs(m)
  597. }
  598. if c.NeedsVendorPublicLibraryVariants() &&
  599. (variant == android.CoreVariation || strings.HasPrefix(variant, ProductVariationPrefix)) {
  600. c.VendorProperties.IsVendorPublicLibrary = true
  601. }
  602. }