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