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