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