snapshot_etc.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 etc
  15. // This file implements snapshot module of 'prebuilt_etc' type
  16. // 'snapshot_etc' module defines android.PrebuiltInterface so it can be handled
  17. // as prebuilt of 'prebuilt_etc' type.
  18. // Properties of 'snapshot_etc' follows properties from snapshotJsonFlags type
  19. import (
  20. "android/soong/android"
  21. "fmt"
  22. "strings"
  23. "github.com/google/blueprint"
  24. "github.com/google/blueprint/proptools"
  25. )
  26. func RegisterSnapshotEtcModule(ctx android.RegistrationContext) {
  27. ctx.RegisterModuleType("snapshot_etc", SnapshotEtcFactory)
  28. }
  29. func init() {
  30. RegisterSnapshotEtcModule(android.InitRegistrationContext)
  31. }
  32. // snapshot_etc is a prebuilt module type to be installed under etc which is auto-generated by
  33. // development/vendor_snapshot/update.py. This module will override prebuilt_etc module with same
  34. // name when 'prefer' property is true.
  35. func SnapshotEtcFactory() android.Module {
  36. module := &SnapshotEtc{}
  37. module.AddProperties(&module.properties)
  38. var srcsSupplier = func(_ android.BaseModuleContext, prebuilt android.Module) []string {
  39. s, ok := prebuilt.(*SnapshotEtc)
  40. if !ok || s.properties.Src == nil {
  41. return []string{}
  42. }
  43. return []string{*s.properties.Src}
  44. }
  45. android.InitAndroidArchModule(module, android.DeviceSupported, android.MultilibFirst)
  46. android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "src")
  47. return module
  48. }
  49. type snapshotEtcProperties struct {
  50. Src *string `android:"path,arch_variant"` // Source of snapshot_etc file
  51. Filename *string `android:"arch_variant"` // Target file name when it differs from module name
  52. Relative_install_path *string `android:"arch_variant"` // Relative install path when it should be installed subdirectory of etc
  53. }
  54. type SnapshotEtc struct {
  55. android.ModuleBase
  56. prebuilt android.Prebuilt
  57. properties snapshotEtcProperties
  58. outputFilePath android.OutputPath
  59. installDirPath android.InstallPath
  60. }
  61. func (s *SnapshotEtc) Prebuilt() *android.Prebuilt {
  62. return &s.prebuilt
  63. }
  64. func (s *SnapshotEtc) Name() string {
  65. return s.prebuilt.Name(s.BaseModuleName())
  66. }
  67. func (s *SnapshotEtc) GenerateAndroidBuildActions(ctx android.ModuleContext) {
  68. if s.properties.Src == nil {
  69. ctx.PropertyErrorf("src", "missing prebuilt source file")
  70. return
  71. }
  72. sourceFilePath := s.prebuilt.SingleSourcePath(ctx)
  73. // Determine the output file basename.
  74. // If Filename is set, use the name specified by the property.
  75. // Otherwise use the module name.
  76. filename := proptools.String(s.properties.Filename)
  77. if filename == "" {
  78. filename = ctx.ModuleName()
  79. }
  80. s.outputFilePath = android.PathForModuleOut(ctx, filename).OutputPath
  81. if strings.Contains(filename, "/") {
  82. ctx.PropertyErrorf("filename", "filename cannot contain separator '/'")
  83. return
  84. }
  85. subDir := ""
  86. if s.properties.Relative_install_path != nil {
  87. subDir = *s.properties.Relative_install_path
  88. }
  89. s.installDirPath = android.PathForModuleInstall(ctx, "etc", subDir)
  90. // This ensures that outputFilePath has the correct name for others to
  91. // use, as the source file may have a different name.
  92. ctx.Build(pctx, android.BuildParams{
  93. Rule: android.Cp,
  94. Input: sourceFilePath,
  95. Output: s.outputFilePath,
  96. Description: "Install snapshot etc module " + s.BaseModuleName(),
  97. })
  98. ctx.InstallFile(s.installDirPath, s.outputFilePath.Base(), sourceFilePath)
  99. }
  100. func (p *SnapshotEtc) AndroidMkEntries() []android.AndroidMkEntries {
  101. return []android.AndroidMkEntries{{
  102. Class: "ETC",
  103. OutputFile: android.OptionalPathForPath(p.outputFilePath),
  104. ExtraEntries: []android.AndroidMkExtraEntriesFunc{
  105. func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
  106. entries.SetString("LOCAL_MODULE_TAGS", "optional")
  107. entries.SetString("LOCAL_MODULE_PATH", p.installDirPath.String())
  108. entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base())
  109. },
  110. },
  111. }}
  112. }
  113. type snapshotEtcDependencyTag struct {
  114. blueprint.DependencyTag
  115. }
  116. var tag = snapshotEtcDependencyTag{}
  117. func (s *SnapshotEtc) CoreVariantNeeded(ctx android.BaseModuleContext) bool {
  118. return !s.ModuleBase.InstallInRecovery() && !s.ModuleBase.InstallInRamdisk() &&
  119. !s.ModuleBase.InstallInVendorRamdisk() && !s.ModuleBase.InstallInDebugRamdisk()
  120. }
  121. func (p *SnapshotEtc) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  122. return p.ModuleBase.InstallInRamdisk()
  123. }
  124. func (p *SnapshotEtc) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  125. return p.ModuleBase.InstallInVendorRamdisk()
  126. }
  127. func (p *SnapshotEtc) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool {
  128. return p.ModuleBase.InstallInDebugRamdisk()
  129. }
  130. func (p *SnapshotEtc) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool {
  131. return p.ModuleBase.InstallInRecovery()
  132. }
  133. func (p *SnapshotEtc) ExtraImageVariations(ctx android.BaseModuleContext) []string {
  134. return nil
  135. }
  136. func (p *SnapshotEtc) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) {
  137. }
  138. func (p *SnapshotEtc) ImageMutatorBegin(ctx android.BaseModuleContext) {}
  139. func (p *SnapshotEtc) OutputFiles(tag string) (android.Paths, error) {
  140. switch tag {
  141. case "":
  142. return android.Paths{p.outputFilePath}, nil
  143. default:
  144. return nil, fmt.Errorf("unsupported module reference tag %q", tag)
  145. }
  146. }
  147. var _ android.PrebuiltInterface = (*SnapshotEtc)(nil)
  148. var _ android.ImageInterface = (*SnapshotEtc)(nil)
  149. var _ android.OutputFileProducer = (*SnapshotEtc)(nil)