snapshot_base.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2021 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 snapshot
  15. import (
  16. "android/soong/android"
  17. "path/filepath"
  18. )
  19. // Interface for modules which can be captured in the snapshot.
  20. type SnapshotModuleInterfaceBase interface{}
  21. // Defines the specifics of different images to which the snapshot process is applicable, e.g.,
  22. // vendor, recovery, ramdisk.
  23. type SnapshotImage interface {
  24. // Returns true if a snapshot should be generated for this image.
  25. shouldGenerateSnapshot(ctx android.SingletonContext) bool
  26. // Function that returns true if the module is included in this image.
  27. // Using a function return instead of a value to prevent early
  28. // evalution of a function that may be not be defined.
  29. InImage(m SnapshotModuleInterfaceBase) func() bool
  30. // Returns true if a dir under source tree is an SoC-owned proprietary
  31. // directory, such as device/, vendor/, etc.
  32. //
  33. // For a given snapshot (e.g., vendor, recovery, etc.) if
  34. // isProprietaryPath(dir, deviceConfig) returns true, then the module in dir
  35. // will be built from sources.
  36. IsProprietaryPath(dir string, deviceConfig android.DeviceConfig) bool
  37. // Whether a given module has been explicitly excluded from the
  38. // snapshot, e.g., using the exclude_from_vendor_snapshot or
  39. // exclude_from_recovery_snapshot properties.
  40. ExcludeFromSnapshot(m SnapshotModuleInterfaceBase) bool
  41. // Returns true if the build is using a snapshot for this image.
  42. IsUsingSnapshot(cfg android.DeviceConfig) bool
  43. // Returns a version of which the snapshot should be used in this target.
  44. // This will only be meaningful when isUsingSnapshot is true.
  45. TargetSnapshotVersion(cfg android.DeviceConfig) string
  46. // Whether to exclude a given module from the directed snapshot or not.
  47. // If the makefile variable DIRECTED_{IMAGE}_SNAPSHOT is true, directed snapshot is turned on,
  48. // and only modules listed in {IMAGE}_SNAPSHOT_MODULES will be captured.
  49. ExcludeFromDirectedSnapshot(cfg android.DeviceConfig, name string) bool
  50. // Returns target image name
  51. ImageName() string
  52. }
  53. type directoryMap map[string]bool
  54. var (
  55. // Modules under following directories are ignored. They are OEM's and vendor's
  56. // proprietary modules(device/, kernel/, vendor/, and hardware/).
  57. defaultDirectoryExcludedMap = directoryMap{
  58. "device": true,
  59. "hardware": true,
  60. "kernel": true,
  61. "vendor": true,
  62. }
  63. // Modules under following directories are included as they are in AOSP,
  64. // although hardware/ and kernel/ are normally for vendor's own.
  65. defaultDirectoryIncludedMap = directoryMap{
  66. "kernel/configs": true,
  67. "kernel/prebuilts": true,
  68. "kernel/tests": true,
  69. "hardware/interfaces": true,
  70. "hardware/libhardware": true,
  71. "hardware/libhardware_legacy": true,
  72. "hardware/ril": true,
  73. }
  74. )
  75. func isDirectoryExcluded(dir string, excludedMap directoryMap, includedMap directoryMap) bool {
  76. if dir == "." || dir == "/" {
  77. return false
  78. }
  79. if includedMap[dir] {
  80. return false
  81. } else if excludedMap[dir] {
  82. return true
  83. } else if defaultDirectoryIncludedMap[dir] {
  84. return false
  85. } else if defaultDirectoryExcludedMap[dir] {
  86. return true
  87. } else {
  88. return isDirectoryExcluded(filepath.Dir(dir), excludedMap, includedMap)
  89. }
  90. }
  91. // This is to be saved as .json files, which is for development/vendor_snapshot/update.py.
  92. // These flags become Android.bp snapshot module properties.
  93. //
  94. // Attributes are optional and will be populated based on each module's need.
  95. // Common attributes are defined here, languages may extend this struct to add
  96. // additional attributes.
  97. type SnapshotJsonFlags struct {
  98. ModuleName string `json:",omitempty"`
  99. RelativeInstallPath string `json:",omitempty"`
  100. Filename string `json:",omitempty"`
  101. ModuleStemName string `json:",omitempty"`
  102. RustProcMacro bool `json:",omitempty"`
  103. CrateName string `json:",omitempty"`
  104. // dependencies
  105. Required []string `json:",omitempty"`
  106. Overrides []string `json:",omitempty"`
  107. // license information
  108. LicenseKinds []string `json:",omitempty"`
  109. LicenseTexts []string `json:",omitempty"`
  110. }
  111. func (prop *SnapshotJsonFlags) InitBaseSnapshotPropsWithName(m android.Module, name string) {
  112. prop.ModuleName = name
  113. prop.LicenseKinds = m.EffectiveLicenseKinds()
  114. prop.LicenseTexts = m.EffectiveLicenseFiles().Strings()
  115. }
  116. func (prop *SnapshotJsonFlags) InitBaseSnapshotProps(m android.Module) {
  117. prop.InitBaseSnapshotPropsWithName(m, m.Name())
  118. }