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 | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 24 | // PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A |
| 25 | // 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 |
| 27 | // running on a VM), or a zip archive for some of the host tools. |
Jiyong Park | 073ea55 | 2020-11-09 14:08:34 +0900 | [diff] [blame] | 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 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 43 | // Get file name of installed package |
| 44 | func (p *PackagingSpec) FileName() string { |
| 45 | if p.relPathInPackage != "" { |
| 46 | return filepath.Base(p.relPathInPackage) |
| 47 | } |
| 48 | |
| 49 | return "" |
| 50 | } |
| 51 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 52 | type PackageModule interface { |
| 53 | Module |
| 54 | packagingBase() *PackagingBase |
| 55 | |
| 56 | // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator. |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 57 | // When adding the dependencies, depTag is used as the tag. |
| 58 | AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 59 | |
| 60 | // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 61 | // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions, |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 62 | // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz, |
| 63 | // etc.) from the extracted files |
| 64 | CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) []string |
| 65 | } |
| 66 | |
| 67 | // PackagingBase provides basic functionality for packaging dependencies. A module is expected to |
| 68 | // include this struct and call InitPackageModule. |
| 69 | type PackagingBase struct { |
| 70 | properties PackagingProperties |
| 71 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 72 | // Allows this module to skip missing dependencies. In most cases, this is not required, but |
| 73 | // for rare cases like when there's a dependency to a module which exists in certain repo |
| 74 | // checkouts, this is needed. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 75 | IgnoreMissingDependencies bool |
| 76 | } |
| 77 | |
| 78 | type depsProperty struct { |
| 79 | // Modules to include in this package |
| 80 | Deps []string `android:"arch_variant"` |
| 81 | } |
| 82 | |
| 83 | type packagingMultilibProperties struct { |
| 84 | First depsProperty `android:"arch_variant"` |
| 85 | Common depsProperty `android:"arch_variant"` |
| 86 | Lib32 depsProperty `android:"arch_variant"` |
| 87 | Lib64 depsProperty `android:"arch_variant"` |
| 88 | } |
| 89 | |
| 90 | type PackagingProperties struct { |
| 91 | Deps []string `android:"arch_variant"` |
| 92 | Multilib packagingMultilibProperties `android:"arch_variant"` |
| 93 | } |
| 94 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 95 | func InitPackageModule(p PackageModule) { |
| 96 | base := p.packagingBase() |
| 97 | p.AddProperties(&base.properties) |
| 98 | } |
| 99 | |
| 100 | func (p *PackagingBase) packagingBase() *PackagingBase { |
| 101 | return p |
| 102 | } |
| 103 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 104 | // From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for |
| 105 | // the current archicture when this module is not configured for multi target. When configured for |
| 106 | // multi target, deps is selected for each of the targets and is NOT selected for the current |
| 107 | // architecture which would be Common. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 108 | func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string { |
| 109 | var ret []string |
| 110 | if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 { |
| 111 | ret = append(ret, p.properties.Deps...) |
| 112 | } else if arch.Multilib == "lib32" { |
| 113 | ret = append(ret, p.properties.Multilib.Lib32.Deps...) |
| 114 | } else if arch.Multilib == "lib64" { |
| 115 | ret = append(ret, p.properties.Multilib.Lib64.Deps...) |
| 116 | } else if arch == Common { |
| 117 | ret = append(ret, p.properties.Multilib.Common.Deps...) |
| 118 | } |
| 119 | for i, t := range ctx.MultiTargets() { |
| 120 | if t.Arch.ArchType == arch { |
| 121 | ret = append(ret, p.properties.Deps...) |
| 122 | if i == 0 { |
| 123 | ret = append(ret, p.properties.Multilib.First.Deps...) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | return FirstUniqueStrings(ret) |
| 128 | } |
| 129 | |
| 130 | func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target { |
| 131 | var ret []Target |
| 132 | // The current and the common OS targets are always supported |
| 133 | ret = append(ret, ctx.Target()) |
| 134 | if ctx.Arch().ArchType != Common { |
| 135 | ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}}) |
| 136 | } |
| 137 | // If this module is configured for multi targets, those should be supported as well |
| 138 | ret = append(ret, ctx.MultiTargets()...) |
| 139 | return ret |
| 140 | } |
| 141 | |
| 142 | // See PackageModule.AddDeps |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 143 | func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 144 | for _, t := range p.getSupportedTargets(ctx) { |
| 145 | for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) { |
| 146 | if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) { |
| 147 | continue |
| 148 | } |
| 149 | ctx.AddFarVariationDependencies(t.Variations(), depTag, dep) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // See PackageModule.CopyDepsToZip |
| 155 | func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) (entries []string) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 156 | m := make(map[string]PackagingSpec) |
| 157 | ctx.WalkDeps(func(child Module, parent Module) bool { |
Jiyong Park | d630bdd | 2020-11-25 11:47:24 +0900 | [diff] [blame] | 158 | if !IsInstallDepNeeded(ctx.OtherModuleDependencyTag(child)) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 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 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 169 | builder := NewRuleBuilder(pctx, ctx) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 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(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 196 | BuiltTool("soong_zip"). |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 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 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 203 | builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName())) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 204 | return entries |
| 205 | } |
Colin Cross | ffe6b9d | 2020-12-01 15:40:06 -0800 | [diff] [blame^] | 206 | |
| 207 | // packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses |
| 208 | // topological order. |
| 209 | type packagingSpecsDepSet struct { |
| 210 | depSet |
| 211 | } |
| 212 | |
| 213 | // newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and |
| 214 | // transitive contents. |
| 215 | func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet { |
| 216 | return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)} |
| 217 | } |
| 218 | |
| 219 | // ToList returns the packagingSpecsDepSet flattened to a list in topological order. |
| 220 | func (d *packagingSpecsDepSet) ToList() []PackagingSpec { |
| 221 | if d == nil { |
| 222 | return nil |
| 223 | } |
| 224 | return d.depSet.ToList().([]PackagingSpec) |
| 225 | } |