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 |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 41 | |
| 42 | effectiveLicenseFiles *Paths |
Jooyung Han | 99c5fe6 | 2022-03-21 15:13:38 +0900 | [diff] [blame^] | 43 | |
| 44 | partition string |
Jiyong Park | 073ea55 | 2020-11-09 14:08:34 +0900 | [diff] [blame] | 45 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 46 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 47 | // Get file name of installed package |
| 48 | func (p *PackagingSpec) FileName() string { |
| 49 | if p.relPathInPackage != "" { |
| 50 | return filepath.Base(p.relPathInPackage) |
| 51 | } |
| 52 | |
| 53 | return "" |
| 54 | } |
| 55 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 56 | // Path relative to the root of the package |
| 57 | func (p *PackagingSpec) RelPathInPackage() string { |
| 58 | return p.relPathInPackage |
| 59 | } |
| 60 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 61 | func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) { |
| 62 | p.relPathInPackage = relPathInPackage |
| 63 | } |
| 64 | |
| 65 | func (p *PackagingSpec) EffectiveLicenseFiles() Paths { |
| 66 | if p.effectiveLicenseFiles == nil { |
| 67 | return Paths{} |
| 68 | } |
| 69 | return *p.effectiveLicenseFiles |
| 70 | } |
| 71 | |
Jooyung Han | 99c5fe6 | 2022-03-21 15:13:38 +0900 | [diff] [blame^] | 72 | func (p *PackagingSpec) Partition() string { |
| 73 | return p.partition |
| 74 | } |
| 75 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 76 | type PackageModule interface { |
| 77 | Module |
| 78 | packagingBase() *PackagingBase |
| 79 | |
| 80 | // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator. |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 81 | // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to |
| 82 | // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface. |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 83 | AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 84 | |
| 85 | // 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] | 86 | // 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] | 87 | // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz, |
| 88 | // etc.) from the extracted files |
Paul Duffin | 4076a75 | 2021-02-02 10:59:54 +0000 | [diff] [blame] | 89 | CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) []string |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // PackagingBase provides basic functionality for packaging dependencies. A module is expected to |
| 93 | // include this struct and call InitPackageModule. |
| 94 | type PackagingBase struct { |
| 95 | properties PackagingProperties |
| 96 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 97 | // Allows this module to skip missing dependencies. In most cases, this is not required, but |
| 98 | // for rare cases like when there's a dependency to a module which exists in certain repo |
| 99 | // checkouts, this is needed. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 100 | IgnoreMissingDependencies bool |
| 101 | } |
| 102 | |
| 103 | type depsProperty struct { |
| 104 | // Modules to include in this package |
| 105 | Deps []string `android:"arch_variant"` |
| 106 | } |
| 107 | |
| 108 | type packagingMultilibProperties struct { |
| 109 | First depsProperty `android:"arch_variant"` |
| 110 | Common depsProperty `android:"arch_variant"` |
| 111 | Lib32 depsProperty `android:"arch_variant"` |
| 112 | Lib64 depsProperty `android:"arch_variant"` |
| 113 | } |
| 114 | |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 115 | type packagingArchProperties struct { |
| 116 | Arm64 depsProperty |
| 117 | Arm depsProperty |
| 118 | X86_64 depsProperty |
| 119 | X86 depsProperty |
| 120 | } |
| 121 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 122 | type PackagingProperties struct { |
| 123 | Deps []string `android:"arch_variant"` |
| 124 | Multilib packagingMultilibProperties `android:"arch_variant"` |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 125 | Arch packagingArchProperties |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 126 | } |
| 127 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 128 | func InitPackageModule(p PackageModule) { |
| 129 | base := p.packagingBase() |
| 130 | p.AddProperties(&base.properties) |
| 131 | } |
| 132 | |
| 133 | func (p *PackagingBase) packagingBase() *PackagingBase { |
| 134 | return p |
| 135 | } |
| 136 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 137 | // From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for |
| 138 | // the current archicture when this module is not configured for multi target. When configured for |
| 139 | // multi target, deps is selected for each of the targets and is NOT selected for the current |
| 140 | // architecture which would be Common. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 141 | func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string { |
| 142 | var ret []string |
| 143 | if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 { |
| 144 | ret = append(ret, p.properties.Deps...) |
| 145 | } else if arch.Multilib == "lib32" { |
| 146 | ret = append(ret, p.properties.Multilib.Lib32.Deps...) |
| 147 | } else if arch.Multilib == "lib64" { |
| 148 | ret = append(ret, p.properties.Multilib.Lib64.Deps...) |
| 149 | } else if arch == Common { |
| 150 | ret = append(ret, p.properties.Multilib.Common.Deps...) |
| 151 | } |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 152 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 153 | for i, t := range ctx.MultiTargets() { |
| 154 | if t.Arch.ArchType == arch { |
| 155 | ret = append(ret, p.properties.Deps...) |
| 156 | if i == 0 { |
| 157 | ret = append(ret, p.properties.Multilib.First.Deps...) |
| 158 | } |
| 159 | } |
| 160 | } |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 161 | |
| 162 | if ctx.Arch().ArchType == Common { |
| 163 | switch arch { |
| 164 | case Arm64: |
| 165 | ret = append(ret, p.properties.Arch.Arm64.Deps...) |
| 166 | case Arm: |
| 167 | ret = append(ret, p.properties.Arch.Arm.Deps...) |
| 168 | case X86_64: |
| 169 | ret = append(ret, p.properties.Arch.X86_64.Deps...) |
| 170 | case X86: |
| 171 | ret = append(ret, p.properties.Arch.X86.Deps...) |
| 172 | } |
| 173 | } |
| 174 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 175 | return FirstUniqueStrings(ret) |
| 176 | } |
| 177 | |
| 178 | func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target { |
| 179 | var ret []Target |
| 180 | // The current and the common OS targets are always supported |
| 181 | ret = append(ret, ctx.Target()) |
| 182 | if ctx.Arch().ArchType != Common { |
| 183 | ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}}) |
| 184 | } |
| 185 | // If this module is configured for multi targets, those should be supported as well |
| 186 | ret = append(ret, ctx.MultiTargets()...) |
| 187 | return ret |
| 188 | } |
| 189 | |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 190 | // PackagingItem is a marker interface for dependency tags. |
| 191 | // Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip(). |
| 192 | type PackagingItem interface { |
| 193 | // IsPackagingItem returns true if the dep is to be packaged |
| 194 | IsPackagingItem() bool |
| 195 | } |
| 196 | |
| 197 | // DepTag provides default implementation of PackagingItem interface. |
| 198 | // PackagingBase-derived modules can define their own dependency tag by embedding this, which |
| 199 | // can be passed to AddDeps() or AddDependencies(). |
| 200 | type PackagingItemAlwaysDepTag struct { |
| 201 | } |
| 202 | |
| 203 | // IsPackagingItem returns true if the dep is to be packaged |
| 204 | func (PackagingItemAlwaysDepTag) IsPackagingItem() bool { |
| 205 | return true |
| 206 | } |
| 207 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 208 | // See PackageModule.AddDeps |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 209 | func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 210 | for _, t := range p.getSupportedTargets(ctx) { |
| 211 | for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) { |
| 212 | if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) { |
| 213 | continue |
| 214 | } |
| 215 | ctx.AddFarVariationDependencies(t.Variations(), depTag, dep) |
| 216 | } |
| 217 | } |
| 218 | } |
| 219 | |
Jooyung Han | df09d17 | 2021-05-11 11:13:30 +0900 | [diff] [blame] | 220 | // Returns transitive PackagingSpecs from deps |
| 221 | func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 222 | m := make(map[string]PackagingSpec) |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 223 | ctx.VisitDirectDeps(func(child Module) { |
| 224 | if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() { |
| 225 | return |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 226 | } |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 227 | for _, ps := range child.TransitivePackagingSpecs() { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 228 | if _, ok := m[ps.relPathInPackage]; !ok { |
| 229 | m[ps.relPathInPackage] = ps |
| 230 | } |
| 231 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 232 | }) |
Jooyung Han | df09d17 | 2021-05-11 11:13:30 +0900 | [diff] [blame] | 233 | return m |
| 234 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 235 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 236 | // CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec |
| 237 | // entries into the specified directory. |
| 238 | func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, m map[string]PackagingSpec, dir ModuleOutPath) (entries []string) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 239 | seenDir := make(map[string]bool) |
| 240 | for _, k := range SortedStringKeys(m) { |
| 241 | ps := m[k] |
| 242 | destPath := dir.Join(ctx, ps.relPathInPackage).String() |
| 243 | destDir := filepath.Dir(destPath) |
| 244 | entries = append(entries, ps.relPathInPackage) |
| 245 | if _, ok := seenDir[destDir]; !ok { |
| 246 | seenDir[destDir] = true |
| 247 | builder.Command().Text("mkdir").Flag("-p").Text(destDir) |
| 248 | } |
| 249 | if ps.symlinkTarget == "" { |
| 250 | builder.Command().Text("cp").Input(ps.srcPath).Text(destPath) |
| 251 | } else { |
| 252 | builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath) |
| 253 | } |
| 254 | if ps.executable { |
| 255 | builder.Command().Text("chmod").Flag("a+x").Text(destPath) |
| 256 | } |
| 257 | } |
| 258 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 259 | return entries |
| 260 | } |
| 261 | |
| 262 | // See PackageModule.CopyDepsToZip |
| 263 | func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) (entries []string) { |
| 264 | m := p.GatherPackagingSpecs(ctx) |
| 265 | builder := NewRuleBuilder(pctx, ctx) |
| 266 | |
| 267 | dir := PathForModuleOut(ctx, ".zip") |
| 268 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 269 | builder.Command().Text("mkdir").Flag("-p").Text(dir.String()) |
| 270 | entries = p.CopySpecsToDir(ctx, builder, m, dir) |
| 271 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 272 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 273 | BuiltTool("soong_zip"). |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 274 | FlagWithOutput("-o ", zipOut). |
| 275 | FlagWithArg("-C ", dir.String()). |
| 276 | Flag("-L 0"). // no compression because this will be unzipped soon |
| 277 | FlagWithArg("-D ", dir.String()) |
| 278 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 279 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 280 | builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName())) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 281 | return entries |
| 282 | } |
Colin Cross | ffe6b9d | 2020-12-01 15:40:06 -0800 | [diff] [blame] | 283 | |
| 284 | // packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses |
| 285 | // topological order. |
| 286 | type packagingSpecsDepSet struct { |
| 287 | depSet |
| 288 | } |
| 289 | |
| 290 | // newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and |
| 291 | // transitive contents. |
| 292 | func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet { |
| 293 | return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)} |
| 294 | } |
| 295 | |
| 296 | // ToList returns the packagingSpecsDepSet flattened to a list in topological order. |
| 297 | func (d *packagingSpecsDepSet) ToList() []PackagingSpec { |
| 298 | if d == nil { |
| 299 | return nil |
| 300 | } |
| 301 | return d.depSet.ToList().([]PackagingSpec) |
| 302 | } |