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