Jiyong Park | 073ea55 | 2020-11-09 14:08:34 +0900 | [diff] [blame] | 1 | // Copyright 2020 Google Inc. All rights reserved. |
| 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 android |
| 16 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame^] | 17 | import ( |
| 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | |
| 21 | "github.com/google/blueprint" |
| 22 | ) |
| 23 | |
Jiyong Park | 073ea55 | 2020-11-09 14:08:34 +0900 | [diff] [blame] | 24 | // PackagingSpec abstracts a request to place a built artifact at a certain path in a package. |
| 25 | // A package can be the traditional <partition>.img, but isn't limited to those. Other examples could |
| 26 | // be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS running |
| 27 | // on a VM), or a zip archive for some of the host tools. |
| 28 | type PackagingSpec struct { |
| 29 | // Path relative to the root of the package |
| 30 | relPathInPackage string |
| 31 | |
| 32 | // The path to the built artifact |
| 33 | srcPath Path |
| 34 | |
| 35 | // If this is not empty, then relPathInPackage should be a symlink to this target. (Then |
| 36 | // srcPath is of course ignored.) |
| 37 | symlinkTarget string |
| 38 | |
| 39 | // Whether relPathInPackage should be marked as executable or not |
| 40 | executable bool |
| 41 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame^] | 42 | |
| 43 | type PackageModule interface { |
| 44 | Module |
| 45 | packagingBase() *PackagingBase |
| 46 | |
| 47 | // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator. |
| 48 | AddDeps(ctx BottomUpMutatorContext) |
| 49 | |
| 50 | // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and |
| 51 | // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions, |
| 52 | // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz, |
| 53 | // etc.) from the extracted files |
| 54 | CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) []string |
| 55 | } |
| 56 | |
| 57 | // PackagingBase provides basic functionality for packaging dependencies. A module is expected to |
| 58 | // include this struct and call InitPackageModule. |
| 59 | type PackagingBase struct { |
| 60 | properties PackagingProperties |
| 61 | |
| 62 | // Allows this module to skip missing dependencies. In most cases, this |
| 63 | // is not required, but for rare cases like when there's a dependency |
| 64 | // to a module which exists in certain repo checkouts, this is needed. |
| 65 | IgnoreMissingDependencies bool |
| 66 | } |
| 67 | |
| 68 | type depsProperty struct { |
| 69 | // Modules to include in this package |
| 70 | Deps []string `android:"arch_variant"` |
| 71 | } |
| 72 | |
| 73 | type packagingMultilibProperties struct { |
| 74 | First depsProperty `android:"arch_variant"` |
| 75 | Common depsProperty `android:"arch_variant"` |
| 76 | Lib32 depsProperty `android:"arch_variant"` |
| 77 | Lib64 depsProperty `android:"arch_variant"` |
| 78 | } |
| 79 | |
| 80 | type PackagingProperties struct { |
| 81 | Deps []string `android:"arch_variant"` |
| 82 | Multilib packagingMultilibProperties `android:"arch_variant"` |
| 83 | } |
| 84 | |
| 85 | type packagingDependencyTag struct{ blueprint.BaseDependencyTag } |
| 86 | |
| 87 | var depTag = packagingDependencyTag{} |
| 88 | |
| 89 | func InitPackageModule(p PackageModule) { |
| 90 | base := p.packagingBase() |
| 91 | p.AddProperties(&base.properties) |
| 92 | } |
| 93 | |
| 94 | func (p *PackagingBase) packagingBase() *PackagingBase { |
| 95 | return p |
| 96 | } |
| 97 | |
| 98 | // From deps and multilib.*.deps, select the dependencies that are for the given arch |
| 99 | // deps is for the current archicture when this module is not configured for multi target. |
| 100 | // When configured for multi target, deps is selected for each of the targets and is NOT |
| 101 | // selected for the current architecture which would be Common. |
| 102 | func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string { |
| 103 | var ret []string |
| 104 | if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 { |
| 105 | ret = append(ret, p.properties.Deps...) |
| 106 | } else if arch.Multilib == "lib32" { |
| 107 | ret = append(ret, p.properties.Multilib.Lib32.Deps...) |
| 108 | } else if arch.Multilib == "lib64" { |
| 109 | ret = append(ret, p.properties.Multilib.Lib64.Deps...) |
| 110 | } else if arch == Common { |
| 111 | ret = append(ret, p.properties.Multilib.Common.Deps...) |
| 112 | } |
| 113 | for i, t := range ctx.MultiTargets() { |
| 114 | if t.Arch.ArchType == arch { |
| 115 | ret = append(ret, p.properties.Deps...) |
| 116 | if i == 0 { |
| 117 | ret = append(ret, p.properties.Multilib.First.Deps...) |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | return FirstUniqueStrings(ret) |
| 122 | } |
| 123 | |
| 124 | func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target { |
| 125 | var ret []Target |
| 126 | // The current and the common OS targets are always supported |
| 127 | ret = append(ret, ctx.Target()) |
| 128 | if ctx.Arch().ArchType != Common { |
| 129 | ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}}) |
| 130 | } |
| 131 | // If this module is configured for multi targets, those should be supported as well |
| 132 | ret = append(ret, ctx.MultiTargets()...) |
| 133 | return ret |
| 134 | } |
| 135 | |
| 136 | // See PackageModule.AddDeps |
| 137 | func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext) { |
| 138 | for _, t := range p.getSupportedTargets(ctx) { |
| 139 | for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) { |
| 140 | if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) { |
| 141 | continue |
| 142 | } |
| 143 | ctx.AddFarVariationDependencies(t.Variations(), depTag, dep) |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // See PackageModule.CopyDepsToZip |
| 149 | func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) (entries []string) { |
| 150 | var supportedArches []string |
| 151 | for _, t := range p.getSupportedTargets(ctx) { |
| 152 | supportedArches = append(supportedArches, t.Arch.ArchType.String()) |
| 153 | } |
| 154 | m := make(map[string]PackagingSpec) |
| 155 | ctx.WalkDeps(func(child Module, parent Module) bool { |
| 156 | // Don't track modules with unsupported arch |
| 157 | // TODO(jiyong): remove this when aosp/1501613 lands. |
| 158 | if !InList(child.Target().Arch.ArchType.String(), supportedArches) { |
| 159 | return false |
| 160 | } |
| 161 | for _, ps := range child.PackagingSpecs() { |
| 162 | if _, ok := m[ps.relPathInPackage]; !ok { |
| 163 | m[ps.relPathInPackage] = ps |
| 164 | } |
| 165 | } |
| 166 | return true |
| 167 | }) |
| 168 | |
| 169 | builder := NewRuleBuilder() |
| 170 | |
| 171 | dir := PathForModuleOut(ctx, ".zip").OutputPath |
| 172 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 173 | builder.Command().Text("mkdir").Flag("-p").Text(dir.String()) |
| 174 | |
| 175 | seenDir := make(map[string]bool) |
| 176 | for _, k := range SortedStringKeys(m) { |
| 177 | ps := m[k] |
| 178 | destPath := dir.Join(ctx, ps.relPathInPackage).String() |
| 179 | destDir := filepath.Dir(destPath) |
| 180 | entries = append(entries, ps.relPathInPackage) |
| 181 | if _, ok := seenDir[destDir]; !ok { |
| 182 | seenDir[destDir] = true |
| 183 | builder.Command().Text("mkdir").Flag("-p").Text(destDir) |
| 184 | } |
| 185 | if ps.symlinkTarget == "" { |
| 186 | builder.Command().Text("cp").Input(ps.srcPath).Text(destPath) |
| 187 | } else { |
| 188 | builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath) |
| 189 | } |
| 190 | if ps.executable { |
| 191 | builder.Command().Text("chmod").Flag("a+x").Text(destPath) |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | builder.Command(). |
| 196 | BuiltTool(ctx, "soong_zip"). |
| 197 | FlagWithOutput("-o ", zipOut). |
| 198 | FlagWithArg("-C ", dir.String()). |
| 199 | Flag("-L 0"). // no compression because this will be unzipped soon |
| 200 | FlagWithArg("-D ", dir.String()) |
| 201 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 202 | |
| 203 | builder.Build(pctx, ctx, "zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName())) |
| 204 | return entries |
| 205 | } |