snapshot.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. "path/filepath"
  17. "sort"
  18. "android/soong/android"
  19. )
  20. // This file contains singletons to capture snapshots. This singleton will generate snapshot of each target
  21. // image, and capturing snapshot module will be delegated to each module which implements GenerateSnapshotAction
  22. // function and register with RegisterSnapshotAction.
  23. var pctx = android.NewPackageContext("android/soong/snapshot")
  24. func init() {
  25. pctx.Import("android/soong/android")
  26. }
  27. type SnapshotSingleton struct {
  28. // Name, e.g., "vendor", "recovery", "ramdisk".
  29. name string
  30. // Make variable that points to the snapshot file, e.g.,
  31. // "SOONG_RECOVERY_SNAPSHOT_ZIP".
  32. makeVar string
  33. // Path to the snapshot zip file.
  34. snapshotZipFile android.OptionalPath
  35. // Implementation of the image interface specific to the image
  36. // associated with this snapshot (e.g., specific to the vendor image,
  37. // recovery image, etc.).
  38. Image SnapshotImage
  39. // Whether this singleton is for fake snapshot or not.
  40. // Fake snapshot is a snapshot whose prebuilt binaries and headers are empty.
  41. // It is much faster to generate, and can be used to inspect dependencies.
  42. Fake bool
  43. }
  44. // The output files to be included in the snapshot.
  45. type SnapshotPaths struct {
  46. // All files to be included in the snapshot
  47. OutputFiles android.Paths
  48. // Notice files of the snapshot output files
  49. NoticeFiles android.Paths
  50. }
  51. // Interface of function to capture snapshot from each module
  52. // Returns snapshot ouputs and notice files.
  53. type GenerateSnapshotAction func(snapshot SnapshotSingleton, ctx android.SingletonContext, snapshotArchDir string) SnapshotPaths
  54. var snapshotActionList []GenerateSnapshotAction
  55. // Register GenerateSnapshotAction function so it can be called while generating snapshot
  56. func RegisterSnapshotAction(x GenerateSnapshotAction) {
  57. snapshotActionList = append(snapshotActionList, x)
  58. }
  59. func (c *SnapshotSingleton) GenerateBuildActions(ctx android.SingletonContext) {
  60. if !c.Image.shouldGenerateSnapshot(ctx) {
  61. return
  62. }
  63. var snapshotOutputs android.Paths
  64. // Snapshot zipped artifacts will be captured under {SNAPSHOT_ARCH} directory
  65. snapshotDir := c.name + "-snapshot"
  66. if c.Fake {
  67. // If this is a fake snapshot singleton, place all files under fake/ subdirectory to avoid
  68. // collision with real snapshot files
  69. snapshotDir = filepath.Join("fake", snapshotDir)
  70. }
  71. snapshotArchDir := filepath.Join(snapshotDir, ctx.DeviceConfig().DeviceArch())
  72. noticeDir := filepath.Join(snapshotArchDir, "NOTICE_FILES")
  73. installedNotices := make(map[string]bool)
  74. for _, f := range snapshotActionList {
  75. snapshotPaths := f(*c, ctx, snapshotArchDir)
  76. snapshotOutputs = append(snapshotOutputs, snapshotPaths.OutputFiles...)
  77. for _, notice := range snapshotPaths.NoticeFiles {
  78. if _, ok := installedNotices[notice.String()]; !ok {
  79. installedNotices[notice.String()] = true
  80. snapshotOutputs = append(snapshotOutputs, CopyFileRule(
  81. pctx, ctx, notice, filepath.Join(noticeDir, notice.String())))
  82. }
  83. }
  84. }
  85. // All artifacts are ready. Sort them to normalize ninja and then zip.
  86. sort.Slice(snapshotOutputs, func(i, j int) bool {
  87. return snapshotOutputs[i].String() < snapshotOutputs[j].String()
  88. })
  89. zipPath := android.PathForOutput(
  90. ctx,
  91. snapshotDir,
  92. c.name+"-"+ctx.Config().DeviceName()+".zip")
  93. zipRule := android.NewRuleBuilder(pctx, ctx)
  94. // filenames in rspfile from FlagWithRspFileInputList might be single-quoted. Remove it with tr
  95. snapshotOutputList := android.PathForOutput(
  96. ctx,
  97. snapshotDir,
  98. c.name+"-"+ctx.Config().DeviceName()+"_list")
  99. rspFile := snapshotOutputList.ReplaceExtension(ctx, "rsp")
  100. zipRule.Command().
  101. Text("tr").
  102. FlagWithArg("-d ", "\\'").
  103. FlagWithRspFileInputList("< ", rspFile, snapshotOutputs).
  104. FlagWithOutput("> ", snapshotOutputList)
  105. zipRule.Temporary(snapshotOutputList)
  106. zipRule.Command().
  107. BuiltTool("soong_zip").
  108. FlagWithOutput("-o ", zipPath).
  109. FlagWithArg("-C ", android.PathForOutput(ctx, snapshotDir).String()).
  110. FlagWithInput("-l ", snapshotOutputList)
  111. zipRule.Build(zipPath.String(), c.name+" snapshot "+zipPath.String())
  112. zipRule.DeleteTemporaryFiles()
  113. c.snapshotZipFile = android.OptionalPathForPath(zipPath)
  114. }
  115. func (c *SnapshotSingleton) MakeVars(ctx android.MakeVarsContext) {
  116. ctx.Strict(
  117. c.makeVar,
  118. c.snapshotZipFile.String())
  119. }