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