image.go 24 KB

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