blob: 09a382e6edec6a0118fb5210668bb7693ecf04d9 [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 "fmt"
20 "path/filepath"
21 "sort"
22
23 "github.com/google/blueprint"
24 "github.com/google/blueprint/proptools"
25
26 "android/soong/android"
27)
28
29//
30// The host_snapshot module creates a snapshot of the modules defined in
31// the deps property. The modules within the deps property (host tools)
32// are ones that return a valid path via HostToolPath() of the
33// HostToolProvider. The created snapshot contains the binaries and any
34// transitive PackagingSpecs of the included host tools, along with a JSON
35// meta file.
36//
37// The snapshot is installed into a source tree via
38// development/vendor_snapshot/update.py, the included modules are
39// provided as preferred prebuilts.
40//
41// To determine which tools to include in the host snapshot see
42// host_fake_snapshot.go.
43
44func init() {
45 registerHostBuildComponents(android.InitRegistrationContext)
46}
47
48func registerHostBuildComponents(ctx android.RegistrationContext) {
49 ctx.RegisterModuleType("host_snapshot", hostSnapshotFactory)
50}
51
52// Relative installation path
53type RelativeInstallPath interface {
54 RelativeInstallPath() string
55}
56
57type hostSnapshot struct {
58 android.ModuleBase
59 android.PackagingBase
60
Rob Seymour0800ef72022-01-31 20:33:52 +000061 outputFile android.OutputPath
Rob Seymour925aa092021-08-10 20:42:03 +000062 installDir android.InstallPath
63}
64
65func hostSnapshotFactory() android.Module {
66 module := &hostSnapshot{}
67 initHostToolsModule(module)
68 return module
69}
70func initHostToolsModule(module *hostSnapshot) {
71 android.InitPackageModule(module)
72 android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon)
73}
74
75var dependencyTag = struct {
76 blueprint.BaseDependencyTag
77 android.InstallAlwaysNeededDependencyTag
78 android.PackagingItemAlwaysDepTag
79}{}
80
81func (f *hostSnapshot) DepsMutator(ctx android.BottomUpMutatorContext) {
82 f.AddDeps(ctx, dependencyTag)
83}
84func (f *hostSnapshot) installFileName() string {
85 return f.Name() + ".zip"
86}
87
88// Create zipfile with JSON description, notice files... for dependent modules
89func (f *hostSnapshot) CreateMetaData(ctx android.ModuleContext, fileName string) android.OutputPath {
90 var jsonData []SnapshotJsonFlags
91 var metaPaths android.Paths
92
93 metaZipFile := android.PathForModuleOut(ctx, fileName).OutputPath
94
95 // Create JSON file based on the direct dependencies
96 ctx.VisitDirectDeps(func(dep android.Module) {
97 desc := hostBinJsonDesc(dep)
98 if desc != nil {
99 jsonData = append(jsonData, *desc)
100 }
101 if len(dep.EffectiveLicenseFiles()) > 0 {
102 noticeFile := android.PathForModuleOut(ctx, "NOTICE_FILES", dep.Name()+".txt").OutputPath
103 android.CatFileRule(ctx, dep.EffectiveLicenseFiles(), noticeFile)
104 metaPaths = append(metaPaths, noticeFile)
105 }
106
107 })
108 // Sort notice paths and json data for repeatble build
109 sort.Slice(jsonData, func(i, j int) bool {
110 return (jsonData[i].ModuleName < jsonData[j].ModuleName)
111 })
112 sort.Slice(metaPaths, func(i, j int) bool {
113 return (metaPaths[i].String() < metaPaths[j].String())
114 })
115
116 marsh, err := json.Marshal(jsonData)
117 if err != nil {
118 ctx.ModuleErrorf("host snapshot json marshal failure: %#v", err)
119 return android.OutputPath{}
120 }
121
122 jsonZipFile := android.PathForModuleOut(ctx, "host_snapshot.json").OutputPath
123 metaPaths = append(metaPaths, jsonZipFile)
124 rspFile := android.PathForModuleOut(ctx, "host_snapshot.rsp").OutputPath
125 android.WriteFileRule(ctx, jsonZipFile, string(marsh))
126
127 builder := android.NewRuleBuilder(pctx, ctx)
128
129 builder.Command().
130 BuiltTool("soong_zip").
131 FlagWithArg("-C ", android.PathForModuleOut(ctx).OutputPath.String()).
132 FlagWithOutput("-o ", metaZipFile).
133 FlagWithRspFileInputList("-r ", rspFile, metaPaths)
134 builder.Build("zip_meta", fmt.Sprintf("zipping meta data for %s", ctx.ModuleName()))
135
136 return metaZipFile
137}
138
139// Create the host tool zip file
140func (f *hostSnapshot) GenerateAndroidBuildActions(ctx android.ModuleContext) {
141 // Create a zip file for the binaries, and a zip of the meta data, then merge zips
142 depsZipFile := android.PathForModuleOut(ctx, f.Name()+"_deps.zip").OutputPath
143 modsZipFile := android.PathForModuleOut(ctx, f.Name()+"_mods.zip").OutputPath
Rob Seymour0800ef72022-01-31 20:33:52 +0000144 f.outputFile = android.PathForModuleOut(ctx, f.installFileName()).OutputPath
Rob Seymour925aa092021-08-10 20:42:03 +0000145
146 f.installDir = android.PathForModuleInstall(ctx)
147
148 f.CopyDepsToZip(ctx, depsZipFile)
149
150 builder := android.NewRuleBuilder(pctx, ctx)
151 builder.Command().
152 BuiltTool("zip2zip").
153 FlagWithInput("-i ", depsZipFile).
154 FlagWithOutput("-o ", modsZipFile).
155 Text("**/*:" + proptools.ShellEscape(f.installDir.String()))
156
157 metaZipFile := f.CreateMetaData(ctx, f.Name()+"_meta.zip")
158
159 builder.Command().
160 BuiltTool("merge_zips").
Rob Seymour0800ef72022-01-31 20:33:52 +0000161 Output(f.outputFile).
Rob Seymour925aa092021-08-10 20:42:03 +0000162 Input(metaZipFile).
163 Input(modsZipFile)
164
165 builder.Build("manifest", fmt.Sprintf("Adding manifest %s", f.installFileName()))
Rob Seymour0800ef72022-01-31 20:33:52 +0000166 ctx.InstallFile(f.installDir, f.installFileName(), f.outputFile)
Rob Seymour925aa092021-08-10 20:42:03 +0000167
168}
169
170// Implements android.AndroidMkEntriesProvider
171func (f *hostSnapshot) AndroidMkEntries() []android.AndroidMkEntries {
Rob Seymour925aa092021-08-10 20:42:03 +0000172 return []android.AndroidMkEntries{android.AndroidMkEntries{
173 Class: "ETC",
Rob Seymour0800ef72022-01-31 20:33:52 +0000174 OutputFile: android.OptionalPathForPath(f.outputFile),
175 DistFiles: android.MakeDefaultDistFiles(f.outputFile),
Rob Seymour925aa092021-08-10 20:42:03 +0000176 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
177 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800178 entries.SetString("LOCAL_MODULE_PATH", f.installDir.String())
Rob Seymour925aa092021-08-10 20:42:03 +0000179 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName())
180 },
181 },
182 }}
183}
184
185// Get host tools path and relative install string helpers
186func hostBinToolPath(m android.Module) android.OptionalPath {
187 if provider, ok := m.(android.HostToolProvider); ok {
188 return provider.HostToolPath()
189 }
190 return android.OptionalPath{}
191
192}
193func hostRelativePathString(m android.Module) string {
194 var outString string
195 if rel, ok := m.(RelativeInstallPath); ok {
196 outString = rel.RelativeInstallPath()
197 }
198 return outString
199}
200
201// Create JSON description for given module, only create descriptions for binary modueles which
202// provide a valid HostToolPath
203func hostBinJsonDesc(m android.Module) *SnapshotJsonFlags {
204 path := hostBinToolPath(m)
205 relPath := hostRelativePathString(m)
206 if path.Valid() && path.String() != "" {
207 return &SnapshotJsonFlags{
208 ModuleName: m.Name(),
209 ModuleStemName: filepath.Base(path.String()),
210 Filename: path.String(),
211 Required: append(m.HostRequiredModuleNames(), m.RequiredModuleNames()...),
212 RelativeInstallPath: relPath,
213 }
214 }
215 return nil
216}