Rob Seymour | 925aa09 | 2021-08-10 20:42:03 +0000 | [diff] [blame] | 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 | |
| 15 | package snapshot |
| 16 | |
| 17 | import ( |
| 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 | |
| 44 | func init() { |
| 45 | registerHostBuildComponents(android.InitRegistrationContext) |
| 46 | } |
| 47 | |
| 48 | func registerHostBuildComponents(ctx android.RegistrationContext) { |
| 49 | ctx.RegisterModuleType("host_snapshot", hostSnapshotFactory) |
| 50 | } |
| 51 | |
| 52 | // Relative installation path |
| 53 | type RelativeInstallPath interface { |
| 54 | RelativeInstallPath() string |
| 55 | } |
| 56 | |
| 57 | type hostSnapshot struct { |
| 58 | android.ModuleBase |
| 59 | android.PackagingBase |
| 60 | |
| 61 | zipFile android.OptionalPath |
| 62 | installDir android.InstallPath |
| 63 | } |
| 64 | |
| 65 | func hostSnapshotFactory() android.Module { |
| 66 | module := &hostSnapshot{} |
| 67 | initHostToolsModule(module) |
| 68 | return module |
| 69 | } |
| 70 | func initHostToolsModule(module *hostSnapshot) { |
| 71 | android.InitPackageModule(module) |
| 72 | android.InitAndroidMultiTargetsArchModule(module, android.HostSupported, android.MultilibCommon) |
| 73 | } |
| 74 | |
| 75 | var dependencyTag = struct { |
| 76 | blueprint.BaseDependencyTag |
| 77 | android.InstallAlwaysNeededDependencyTag |
| 78 | android.PackagingItemAlwaysDepTag |
| 79 | }{} |
| 80 | |
| 81 | func (f *hostSnapshot) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 82 | f.AddDeps(ctx, dependencyTag) |
| 83 | } |
| 84 | func (f *hostSnapshot) installFileName() string { |
| 85 | return f.Name() + ".zip" |
| 86 | } |
| 87 | |
| 88 | // Create zipfile with JSON description, notice files... for dependent modules |
| 89 | func (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 |
| 140 | func (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 |
| 144 | outputFile := android.PathForModuleOut(ctx, f.installFileName()).OutputPath |
| 145 | |
| 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"). |
| 161 | Output(outputFile). |
| 162 | Input(metaZipFile). |
| 163 | Input(modsZipFile) |
| 164 | |
| 165 | builder.Build("manifest", fmt.Sprintf("Adding manifest %s", f.installFileName())) |
| 166 | zip := ctx.InstallFile(f.installDir, f.installFileName(), outputFile) |
| 167 | f.zipFile = android.OptionalPathForPath(zip) |
| 168 | |
| 169 | } |
| 170 | |
| 171 | // Implements android.AndroidMkEntriesProvider |
| 172 | func (f *hostSnapshot) AndroidMkEntries() []android.AndroidMkEntries { |
| 173 | if !f.zipFile.Valid() { |
| 174 | return []android.AndroidMkEntries{} |
| 175 | } |
| 176 | |
| 177 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 178 | Class: "ETC", |
| 179 | OutputFile: f.zipFile, |
| 180 | DistFiles: android.MakeDefaultDistFiles(f.zipFile.Path()), |
| 181 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 182 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 183 | entries.SetString("LOCAL_MODULE_PATH", f.installDir.ToMakePath().String()) |
| 184 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", f.installFileName()) |
| 185 | }, |
| 186 | }, |
| 187 | }} |
| 188 | } |
| 189 | |
| 190 | // Get host tools path and relative install string helpers |
| 191 | func hostBinToolPath(m android.Module) android.OptionalPath { |
| 192 | if provider, ok := m.(android.HostToolProvider); ok { |
| 193 | return provider.HostToolPath() |
| 194 | } |
| 195 | return android.OptionalPath{} |
| 196 | |
| 197 | } |
| 198 | func hostRelativePathString(m android.Module) string { |
| 199 | var outString string |
| 200 | if rel, ok := m.(RelativeInstallPath); ok { |
| 201 | outString = rel.RelativeInstallPath() |
| 202 | } |
| 203 | return outString |
| 204 | } |
| 205 | |
| 206 | // Create JSON description for given module, only create descriptions for binary modueles which |
| 207 | // provide a valid HostToolPath |
| 208 | func hostBinJsonDesc(m android.Module) *SnapshotJsonFlags { |
| 209 | path := hostBinToolPath(m) |
| 210 | relPath := hostRelativePathString(m) |
| 211 | if path.Valid() && path.String() != "" { |
| 212 | return &SnapshotJsonFlags{ |
| 213 | ModuleName: m.Name(), |
| 214 | ModuleStemName: filepath.Base(path.String()), |
| 215 | Filename: path.String(), |
| 216 | Required: append(m.HostRequiredModuleNames(), m.RequiredModuleNames()...), |
| 217 | RelativeInstallPath: relPath, |
| 218 | } |
| 219 | } |
| 220 | return nil |
| 221 | } |