image.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2019 Google Inc. All rights reserved.
  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 android
  15. // ImageInterface is implemented by modules that need to be split by the imageMutator.
  16. type ImageInterface interface {
  17. // ImageMutatorBegin is called before any other method in the ImageInterface.
  18. ImageMutatorBegin(ctx BaseModuleContext)
  19. // CoreVariantNeeded should return true if the module needs a core variant (installed on the system image).
  20. CoreVariantNeeded(ctx BaseModuleContext) bool
  21. // RamdiskVariantNeeded should return true if the module needs a ramdisk variant (installed on the
  22. // ramdisk partition).
  23. RamdiskVariantNeeded(ctx BaseModuleContext) bool
  24. // VendorRamdiskVariantNeeded should return true if the module needs a vendor ramdisk variant (installed on the
  25. // vendor ramdisk partition).
  26. VendorRamdiskVariantNeeded(ctx BaseModuleContext) bool
  27. // DebugRamdiskVariantNeeded should return true if the module needs a debug ramdisk variant (installed on the
  28. // debug ramdisk partition: $(PRODUCT_OUT)/debug_ramdisk).
  29. DebugRamdiskVariantNeeded(ctx BaseModuleContext) bool
  30. // RecoveryVariantNeeded should return true if the module needs a recovery variant (installed on the
  31. // recovery partition).
  32. RecoveryVariantNeeded(ctx BaseModuleContext) bool
  33. // ExtraImageVariations should return a list of the additional variations needed for the module. After the
  34. // variants are created the SetImageVariation method will be called on each newly created variant with the
  35. // its variation.
  36. ExtraImageVariations(ctx BaseModuleContext) []string
  37. // SetImageVariation is called for each newly created image variant. The receiver is the original
  38. // module, "variation" is the name of the newly created variant and "module" is the newly created
  39. // variant itself.
  40. SetImageVariation(ctx BaseModuleContext, variation string, module Module)
  41. }
  42. const (
  43. // CoreVariation is the variant used for framework-private libraries, or
  44. // SDK libraries. (which framework-private libraries can use), which
  45. // will be installed to the system image.
  46. CoreVariation string = ""
  47. // RecoveryVariation means a module to be installed to recovery image.
  48. RecoveryVariation string = "recovery"
  49. // RamdiskVariation means a module to be installed to ramdisk image.
  50. RamdiskVariation string = "ramdisk"
  51. // VendorRamdiskVariation means a module to be installed to vendor ramdisk image.
  52. VendorRamdiskVariation string = "vendor_ramdisk"
  53. // DebugRamdiskVariation means a module to be installed to debug ramdisk image.
  54. DebugRamdiskVariation string = "debug_ramdisk"
  55. )
  56. // imageMutator creates variants for modules that implement the ImageInterface that
  57. // allow them to build differently for each partition (recovery, core, vendor, etc.).
  58. func imageMutator(ctx BottomUpMutatorContext) {
  59. if ctx.Os() != Android {
  60. return
  61. }
  62. if m, ok := ctx.Module().(ImageInterface); ok {
  63. m.ImageMutatorBegin(ctx)
  64. var variations []string
  65. if m.CoreVariantNeeded(ctx) {
  66. variations = append(variations, CoreVariation)
  67. }
  68. if m.RamdiskVariantNeeded(ctx) {
  69. variations = append(variations, RamdiskVariation)
  70. }
  71. if m.VendorRamdiskVariantNeeded(ctx) {
  72. variations = append(variations, VendorRamdiskVariation)
  73. }
  74. if m.DebugRamdiskVariantNeeded(ctx) {
  75. variations = append(variations, DebugRamdiskVariation)
  76. }
  77. if m.RecoveryVariantNeeded(ctx) {
  78. variations = append(variations, RecoveryVariation)
  79. }
  80. extraVariations := m.ExtraImageVariations(ctx)
  81. variations = append(variations, extraVariations...)
  82. if len(variations) == 0 {
  83. return
  84. }
  85. mod := ctx.CreateVariations(variations...)
  86. for i, v := range variations {
  87. mod[i].base().setImageVariation(v)
  88. m.SetImageVariation(ctx, v, mod[i])
  89. }
  90. }
  91. }