blob: 1a75f3a659ac667a2da877f270b6c21101afb167 [file] [log] [blame]
Rob Seymour925aa092021-08-10 20:42:03 +00001// 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
15package snapshot
16
17import (
18 "encoding/json"
19 "path/filepath"
20
21 "android/soong/android"
22)
23
24// The host_snapshot module creates a snapshot of host tools to be used
25// in a minimal source tree. In order to create the host_snapshot the
26// user must explicitly list the modules to be included. The
27// host-fake-snapshot, defined in this file, is a utility to help determine
28// which host modules are being used in the minimal source tree.
29//
30// The host-fake-snapshot is designed to run in a full source tree and
31// will result in a snapshot that contains an empty file for each host
32// tool found in the tree. The fake snapshot is only used to determine
33// the host modules that the minimal source tree depends on, hence the
34// snapshot uses an empty file for each module and saves on having to
35// actually build any tool to generate the snapshot. The fake snapshot
36// is compatible with an actual host_snapshot and is installed into a
37// minimal source tree via the development/vendor_snapshot/update.py
38// script.
39//
40// After generating the fake snapshot and installing into the minimal
41// source tree, the dependent modules are determined via the
42// development/vendor_snapshot/update.py script (see script for more
43// information). These modules are then used to define the actual
44// host_snapshot to be used. This is a similar process to the other
45// snapshots (vendor, recovery,...)
46//
47// Example
48//
49// Full source tree:
50// 1/ Generate fake host snapshot
51//
52// Minimal source tree:
53// 2/ Install the fake host snapshot
54// 3/ List the host modules used from the snapshot
55// 4/ Remove fake host snapshot
56//
57// Full source tree:
58// 4/ Create host_snapshot with modules identified in step 3
59//
60// Minimal source tree:
61// 5/ Install host snapshot
62// 6/ Build
63//
64// The host-fake-snapshot is a singleton module, that will be built
65// if HOST_FAKE_SNAPSHOT_ENABLE=true.
66
67func init() {
68 registerHostSnapshotComponents(android.InitRegistrationContext)
69}
70
71func registerHostSnapshotComponents(ctx android.RegistrationContext) {
72 ctx.RegisterSingletonType("host-fake-snapshot", HostToolsFakeAndroidSingleton)
73}
74
75type hostFakeSingleton struct {
76 snapshotDir string
77 zipFile android.OptionalPath
78}
79
80func (c *hostFakeSingleton) init() {
81 c.snapshotDir = "host-fake-snapshot"
82
83}
84func HostToolsFakeAndroidSingleton() android.Singleton {
85 singleton := &hostFakeSingleton{}
86 singleton.init()
87 return singleton
88}
89
90func (c *hostFakeSingleton) GenerateBuildActions(ctx android.SingletonContext) {
91 if !ctx.DeviceConfig().HostFakeSnapshotEnabled() {
92 return
93 }
94 // Find all host binary modules add 'fake' versions to snapshot
95 var outputs android.Paths
96 seen := make(map[string]bool)
97 var jsonData []SnapshotJsonFlags
98 ctx.VisitAllModules(func(module android.Module) {
99 if module.Target().Os != ctx.Config().BuildOSTarget.Os {
100 return
101 }
102 if module.Target().Arch.ArchType != ctx.Config().BuildOSTarget.Arch.ArchType {
103 return
104 }
105
106 if android.IsModulePrebuilt(module) {
107 return
108 }
109
110 if !module.Enabled() || module.IsHideFromMake() {
111 return
112 }
113 apexInfo := ctx.ModuleProvider(module, android.ApexInfoProvider).(android.ApexInfo)
114 if !apexInfo.IsForPlatform() {
115 return
116 }
Ivan Lozano872d5792022-03-23 17:31:39 -0400117 path := hostToolPath(module)
Rob Seymour925aa092021-08-10 20:42:03 +0000118 if path.Valid() && path.String() != "" {
119 outFile := filepath.Join(c.snapshotDir, path.String())
120 if !seen[outFile] {
121 seen[outFile] = true
122 outputs = append(outputs, WriteStringToFileRule(ctx, "", outFile))
Ivan Lozano872d5792022-03-23 17:31:39 -0400123 jsonData = append(jsonData, *hostJsonDesc(module))
Rob Seymour925aa092021-08-10 20:42:03 +0000124 }
125 }
126 })
127
128 marsh, err := json.Marshal(jsonData)
129 if err != nil {
130 ctx.Errorf("host fake snapshot json marshal failure: %#v", err)
131 return
132 }
133 outputs = append(outputs, WriteStringToFileRule(ctx, string(marsh), filepath.Join(c.snapshotDir, "host_snapshot.json")))
134 c.zipFile = zipSnapshot(ctx, c.snapshotDir, c.snapshotDir, outputs)
135
136}
137func (c *hostFakeSingleton) MakeVars(ctx android.MakeVarsContext) {
138 if !c.zipFile.Valid() {
139 return
140 }
141 ctx.Phony(
142 "host-fake-snapshot",
143 c.zipFile.Path())
144
145 ctx.DistForGoal(
146 "host-fake-snapshot",
147 c.zipFile.Path())
148
149}