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