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" |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 20 | "slices" |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 21 | "sort" |
Jeongik Cha | 76e677f | 2023-12-21 16:39:15 +0900 | [diff] [blame] | 22 | "strings" |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint" |
Yu Liu | 3cadf7d | 2024-10-24 18:47:06 +0000 | [diff] [blame] | 25 | "github.com/google/blueprint/gobtools" |
Jiyong Park | 105e11c | 2024-05-17 14:58:24 +0900 | [diff] [blame] | 26 | "github.com/google/blueprint/proptools" |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 29 | // PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A |
| 30 | // package can be the traditional <partition>.img, but isn't limited to those. Other examples could |
| 31 | // be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS |
| 32 | // 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] | 33 | type PackagingSpec struct { |
| 34 | // Path relative to the root of the package |
| 35 | relPathInPackage string |
| 36 | |
| 37 | // The path to the built artifact |
| 38 | srcPath Path |
| 39 | |
| 40 | // If this is not empty, then relPathInPackage should be a symlink to this target. (Then |
| 41 | // srcPath is of course ignored.) |
| 42 | symlinkTarget string |
| 43 | |
| 44 | // Whether relPathInPackage should be marked as executable or not |
| 45 | executable bool |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 46 | |
| 47 | effectiveLicenseFiles *Paths |
Jooyung Han | 99c5fe6 | 2022-03-21 15:13:38 +0900 | [diff] [blame] | 48 | |
| 49 | partition string |
Jiyong Park | 4152b19 | 2024-04-30 21:24:21 +0900 | [diff] [blame] | 50 | |
| 51 | // Whether this packaging spec represents an installation of the srcPath (i.e. this struct |
| 52 | // is created via InstallFile or InstallSymlink) or a simple packaging (i.e. created via |
| 53 | // PackageFile). |
| 54 | skipInstall bool |
Justin Yun | 74f3f30 | 2024-05-07 14:32:14 +0900 | [diff] [blame] | 55 | |
| 56 | // Paths of aconfig files for the built artifact |
| 57 | aconfigPaths *Paths |
Jiyong Park | c6a773d | 2024-05-14 21:49:11 +0900 | [diff] [blame] | 58 | |
| 59 | // ArchType of the module which produced this packaging spec |
| 60 | archType ArchType |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 61 | |
| 62 | // List of module names that this packaging spec overrides |
| 63 | overrides *[]string |
| 64 | |
| 65 | // Name of the module where this packaging spec is output of |
| 66 | owner string |
Jiyong Park | 073ea55 | 2020-11-09 14:08:34 +0900 | [diff] [blame] | 67 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 68 | |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 69 | type packagingSpecGob struct { |
Yu Liu | 5246a7e | 2024-10-09 20:04:52 +0000 | [diff] [blame] | 70 | RelPathInPackage string |
| 71 | SrcPath Path |
| 72 | SymlinkTarget string |
| 73 | Executable bool |
| 74 | EffectiveLicenseFiles *Paths |
| 75 | Partition string |
| 76 | SkipInstall bool |
| 77 | AconfigPaths *Paths |
| 78 | ArchType ArchType |
| 79 | Overrides *[]string |
| 80 | Owner string |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 81 | } |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 82 | |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 83 | func (p *PackagingSpec) ToGob() *packagingSpecGob { |
| 84 | return &packagingSpecGob{ |
Yu Liu | 5246a7e | 2024-10-09 20:04:52 +0000 | [diff] [blame] | 85 | RelPathInPackage: p.relPathInPackage, |
| 86 | SrcPath: p.srcPath, |
| 87 | SymlinkTarget: p.symlinkTarget, |
| 88 | Executable: p.executable, |
| 89 | EffectiveLicenseFiles: p.effectiveLicenseFiles, |
| 90 | Partition: p.partition, |
| 91 | SkipInstall: p.skipInstall, |
| 92 | AconfigPaths: p.aconfigPaths, |
| 93 | ArchType: p.archType, |
| 94 | Overrides: p.overrides, |
| 95 | Owner: p.owner, |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | func (p *PackagingSpec) FromGob(data *packagingSpecGob) { |
| 100 | p.relPathInPackage = data.RelPathInPackage |
| 101 | p.srcPath = data.SrcPath |
| 102 | p.symlinkTarget = data.SymlinkTarget |
| 103 | p.executable = data.Executable |
Yu Liu | 5246a7e | 2024-10-09 20:04:52 +0000 | [diff] [blame] | 104 | p.effectiveLicenseFiles = data.EffectiveLicenseFiles |
Yu Liu | 467d7c5 | 2024-09-18 21:54:44 +0000 | [diff] [blame] | 105 | p.partition = data.Partition |
| 106 | p.skipInstall = data.SkipInstall |
| 107 | p.aconfigPaths = data.AconfigPaths |
| 108 | p.archType = data.ArchType |
| 109 | p.overrides = data.Overrides |
| 110 | p.owner = data.Owner |
| 111 | } |
| 112 | |
| 113 | func (p *PackagingSpec) GobEncode() ([]byte, error) { |
Yu Liu | 3cadf7d | 2024-10-24 18:47:06 +0000 | [diff] [blame] | 114 | return gobtools.CustomGobEncode[packagingSpecGob](p) |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | func (p *PackagingSpec) GobDecode(data []byte) error { |
Yu Liu | 3cadf7d | 2024-10-24 18:47:06 +0000 | [diff] [blame] | 118 | return gobtools.CustomGobDecode[packagingSpecGob](data, p) |
Yu Liu | 26a716d | 2024-08-30 23:40:32 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Jiyong Park | 16ef7ac | 2024-05-01 12:36:10 +0000 | [diff] [blame] | 121 | func (p *PackagingSpec) Equals(other *PackagingSpec) bool { |
| 122 | if other == nil { |
| 123 | return false |
| 124 | } |
| 125 | if p.relPathInPackage != other.relPathInPackage { |
| 126 | return false |
| 127 | } |
| 128 | if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget { |
| 129 | return false |
| 130 | } |
| 131 | if p.executable != other.executable { |
| 132 | return false |
| 133 | } |
| 134 | if p.partition != other.partition { |
| 135 | return false |
| 136 | } |
| 137 | return true |
| 138 | } |
| 139 | |
Kiyoung Kim | 24dfc1f | 2020-11-16 10:48:44 +0900 | [diff] [blame] | 140 | // Get file name of installed package |
| 141 | func (p *PackagingSpec) FileName() string { |
| 142 | if p.relPathInPackage != "" { |
| 143 | return filepath.Base(p.relPathInPackage) |
| 144 | } |
| 145 | |
| 146 | return "" |
| 147 | } |
| 148 | |
Jiyong Park | 6446b62 | 2021-02-01 20:08:28 +0900 | [diff] [blame] | 149 | // Path relative to the root of the package |
| 150 | func (p *PackagingSpec) RelPathInPackage() string { |
| 151 | return p.relPathInPackage |
| 152 | } |
| 153 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 154 | func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) { |
| 155 | p.relPathInPackage = relPathInPackage |
| 156 | } |
| 157 | |
| 158 | func (p *PackagingSpec) EffectiveLicenseFiles() Paths { |
| 159 | if p.effectiveLicenseFiles == nil { |
| 160 | return Paths{} |
| 161 | } |
| 162 | return *p.effectiveLicenseFiles |
| 163 | } |
| 164 | |
Jooyung Han | 99c5fe6 | 2022-03-21 15:13:38 +0900 | [diff] [blame] | 165 | func (p *PackagingSpec) Partition() string { |
| 166 | return p.partition |
| 167 | } |
| 168 | |
Jiyong Park | 4152b19 | 2024-04-30 21:24:21 +0900 | [diff] [blame] | 169 | func (p *PackagingSpec) SkipInstall() bool { |
| 170 | return p.skipInstall |
| 171 | } |
| 172 | |
Justin Yun | 74f3f30 | 2024-05-07 14:32:14 +0900 | [diff] [blame] | 173 | // Paths of aconfig files for the built artifact |
| 174 | func (p *PackagingSpec) GetAconfigPaths() Paths { |
| 175 | return *p.aconfigPaths |
| 176 | } |
| 177 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 178 | type PackageModule interface { |
| 179 | Module |
| 180 | packagingBase() *PackagingBase |
| 181 | |
| 182 | // 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] | 183 | // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to |
| 184 | // 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] | 185 | AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 186 | |
Jooyung Han | a883428 | 2022-03-25 11:40:12 +0900 | [diff] [blame] | 187 | // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies. |
| 188 | GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 189 | GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec |
Jooyung Han | a883428 | 2022-03-25 11:40:12 +0900 | [diff] [blame] | 190 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 191 | // 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] | 192 | // 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] | 193 | // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz, |
| 194 | // etc.) from the extracted files |
Jooyung Han | a883428 | 2022-03-25 11:40:12 +0900 | [diff] [blame] | 195 | CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // PackagingBase provides basic functionality for packaging dependencies. A module is expected to |
| 199 | // include this struct and call InitPackageModule. |
| 200 | type PackagingBase struct { |
| 201 | properties PackagingProperties |
| 202 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 203 | // Allows this module to skip missing dependencies. In most cases, this is not required, but |
| 204 | // for rare cases like when there's a dependency to a module which exists in certain repo |
| 205 | // checkouts, this is needed. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 206 | IgnoreMissingDependencies bool |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 207 | |
| 208 | // If this is set to true by a module type inheriting PackagingBase, the deps property |
| 209 | // collects the first target only even with compile_multilib: true. |
| 210 | DepsCollectFirstTargetOnly bool |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 211 | |
| 212 | // If this is set to try by a module type inheriting PackagingBase, the module type is |
| 213 | // allowed to utilize High_priority_deps. |
| 214 | AllowHighPriorityDeps bool |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 215 | } |
| 216 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 217 | type DepsProperty struct { |
| 218 | // Deps that have higher priority in packaging when there is a packaging conflict. |
| 219 | // For example, if multiple files are being installed to same filepath, the install file |
| 220 | // of the module listed in this property will have a higher priority over those in other |
| 221 | // deps properties. |
| 222 | High_priority_deps []string `android:"arch_variant"` |
| 223 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 224 | // Modules to include in this package |
Jiyong Park | 105e11c | 2024-05-17 14:58:24 +0900 | [diff] [blame] | 225 | Deps proptools.Configurable[[]string] `android:"arch_variant"` |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | type packagingMultilibProperties struct { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 229 | First DepsProperty `android:"arch_variant"` |
| 230 | Common DepsProperty `android:"arch_variant"` |
| 231 | Lib32 DepsProperty `android:"arch_variant"` |
| 232 | Lib64 DepsProperty `android:"arch_variant"` |
| 233 | Both DepsProperty `android:"arch_variant"` |
| 234 | Prefer32 DepsProperty `android:"arch_variant"` |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 235 | } |
| 236 | |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 237 | type packagingArchProperties struct { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 238 | Arm64 DepsProperty |
| 239 | Arm DepsProperty |
| 240 | X86_64 DepsProperty |
| 241 | X86 DepsProperty |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 242 | } |
| 243 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 244 | type PackagingProperties struct { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 245 | DepsProperty |
| 246 | |
| 247 | Multilib packagingMultilibProperties `android:"arch_variant"` |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 248 | Arch packagingArchProperties |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 249 | } |
| 250 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 251 | func InitPackageModule(p PackageModule) { |
| 252 | base := p.packagingBase() |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 253 | p.AddProperties(&base.properties, &base.properties.DepsProperty) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | func (p *PackagingBase) packagingBase() *PackagingBase { |
| 257 | return p |
| 258 | } |
| 259 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 260 | func (p *PackagingBase) highPriorityDeps() []string { |
| 261 | return slices.Concat( |
| 262 | p.properties.High_priority_deps, |
| 263 | p.properties.Multilib.Both.High_priority_deps, |
| 264 | p.properties.Multilib.Common.High_priority_deps, |
| 265 | p.properties.Multilib.First.High_priority_deps, |
| 266 | p.properties.Multilib.Lib32.High_priority_deps, |
| 267 | p.properties.Multilib.Lib64.High_priority_deps, |
| 268 | p.properties.Multilib.Prefer32.High_priority_deps, |
| 269 | p.properties.Arch.Arm.High_priority_deps, |
| 270 | p.properties.Arch.Arm64.High_priority_deps, |
| 271 | p.properties.Arch.X86.High_priority_deps, |
| 272 | p.properties.Arch.X86_64.High_priority_deps, |
| 273 | ) |
| 274 | } |
| 275 | |
Jiyong Park | cc1157c | 2020-11-25 11:31:13 +0900 | [diff] [blame] | 276 | // From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for |
| 277 | // the current archicture when this module is not configured for multi target. When configured for |
| 278 | // multi target, deps is selected for each of the targets and is NOT selected for the current |
| 279 | // architecture which would be Common. |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 280 | func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 281 | if len(p.highPriorityDeps()) > 0 && !p.AllowHighPriorityDeps { |
| 282 | ctx.ModuleErrorf("Usage of high_priority_deps is not allowed for %s module type", ctx.ModuleType()) |
| 283 | } |
| 284 | |
| 285 | get := func(prop DepsProperty) []string { |
| 286 | return Concat(prop.Deps.GetOrDefault(ctx, nil), prop.High_priority_deps) |
Jiyong Park | 105e11c | 2024-05-17 14:58:24 +0900 | [diff] [blame] | 287 | } |
| 288 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 289 | var ret []string |
| 290 | if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 291 | ret = append(ret, get(p.properties.DepsProperty)...) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 292 | } else if arch.Multilib == "lib32" { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 293 | ret = append(ret, get(p.properties.Multilib.Lib32)...) |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 294 | // multilib.prefer32.deps are added for lib32 only when they support 32-bit arch |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 295 | for _, dep := range get(p.properties.Multilib.Prefer32) { |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 296 | if checkIfOtherModuleSupportsLib32(ctx, dep) { |
| 297 | ret = append(ret, dep) |
| 298 | } |
| 299 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 300 | } else if arch.Multilib == "lib64" { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 301 | ret = append(ret, get(p.properties.Multilib.Lib64)...) |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 302 | // multilib.prefer32.deps are added for lib64 only when they don't support 32-bit arch |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 303 | for _, dep := range get(p.properties.Multilib.Prefer32) { |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 304 | if !checkIfOtherModuleSupportsLib32(ctx, dep) { |
| 305 | ret = append(ret, dep) |
| 306 | } |
| 307 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 308 | } else if arch == Common { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 309 | ret = append(ret, get(p.properties.Multilib.Common)...) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 310 | } |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 311 | |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 312 | if p.DepsCollectFirstTargetOnly { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 313 | if len(get(p.properties.Multilib.First)) > 0 { |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 314 | ctx.PropertyErrorf("multilib.first.deps", "not supported. use \"deps\" instead") |
| 315 | } |
| 316 | for i, t := range ctx.MultiTargets() { |
| 317 | if t.Arch.ArchType == arch { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 318 | ret = append(ret, get(p.properties.Multilib.Both)...) |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 319 | if i == 0 { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 320 | ret = append(ret, get(p.properties.DepsProperty)...) |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | } |
| 324 | } else { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 325 | if len(get(p.properties.Multilib.Both)) > 0 { |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 326 | ctx.PropertyErrorf("multilib.both.deps", "not supported. use \"deps\" instead") |
| 327 | } |
| 328 | for i, t := range ctx.MultiTargets() { |
| 329 | if t.Arch.ArchType == arch { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 330 | ret = append(ret, get(p.properties.DepsProperty)...) |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 331 | if i == 0 { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 332 | ret = append(ret, get(p.properties.Multilib.First)...) |
Jiyong Park | 3ea9b65 | 2024-05-15 23:01:54 +0900 | [diff] [blame] | 333 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | } |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 337 | |
| 338 | if ctx.Arch().ArchType == Common { |
| 339 | switch arch { |
| 340 | case Arm64: |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 341 | ret = append(ret, get(p.properties.Arch.Arm64)...) |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 342 | case Arm: |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 343 | ret = append(ret, get(p.properties.Arch.Arm)...) |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 344 | case X86_64: |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 345 | ret = append(ret, get(p.properties.Arch.X86_64)...) |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 346 | case X86: |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 347 | ret = append(ret, get(p.properties.Arch.X86)...) |
Jiyong Park | 2136d15 | 2021-02-01 23:24:56 +0900 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 351 | return FirstUniqueStrings(ret) |
| 352 | } |
| 353 | |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 354 | func getSupportedTargets(ctx BaseModuleContext) []Target { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 355 | var ret []Target |
| 356 | // The current and the common OS targets are always supported |
| 357 | ret = append(ret, ctx.Target()) |
| 358 | if ctx.Arch().ArchType != Common { |
| 359 | ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}}) |
| 360 | } |
| 361 | // If this module is configured for multi targets, those should be supported as well |
| 362 | ret = append(ret, ctx.MultiTargets()...) |
| 363 | return ret |
| 364 | } |
| 365 | |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 366 | // getLib32Target returns the 32-bit target from the list of targets this module supports. If this |
| 367 | // module doesn't support 32-bit target, nil is returned. |
| 368 | func getLib32Target(ctx BaseModuleContext) *Target { |
| 369 | for _, t := range getSupportedTargets(ctx) { |
| 370 | if t.Arch.ArchType.Multilib == "lib32" { |
| 371 | return &t |
| 372 | } |
| 373 | } |
| 374 | return nil |
| 375 | } |
| 376 | |
| 377 | // checkIfOtherModuleSUpportsLib32 returns true if 32-bit variant of dep exists. |
| 378 | func checkIfOtherModuleSupportsLib32(ctx BaseModuleContext, dep string) bool { |
| 379 | t := getLib32Target(ctx) |
| 380 | if t == nil { |
| 381 | // This packaging module doesn't support 32bit. No point of checking if dep supports 32-bit |
| 382 | // or not. |
| 383 | return false |
| 384 | } |
| 385 | return ctx.OtherModuleFarDependencyVariantExists(t.Variations(), dep) |
| 386 | } |
| 387 | |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 388 | // PackagingItem is a marker interface for dependency tags. |
| 389 | // Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip(). |
| 390 | type PackagingItem interface { |
| 391 | // IsPackagingItem returns true if the dep is to be packaged |
| 392 | IsPackagingItem() bool |
| 393 | } |
| 394 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 395 | var _ PackagingItem = (*PackagingItemAlwaysDepTag)(nil) |
| 396 | |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 397 | // DepTag provides default implementation of PackagingItem interface. |
| 398 | // PackagingBase-derived modules can define their own dependency tag by embedding this, which |
| 399 | // can be passed to AddDeps() or AddDependencies(). |
| 400 | type PackagingItemAlwaysDepTag struct { |
| 401 | } |
| 402 | |
| 403 | // IsPackagingItem returns true if the dep is to be packaged |
| 404 | func (PackagingItemAlwaysDepTag) IsPackagingItem() bool { |
| 405 | return true |
| 406 | } |
| 407 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 408 | // highPriorityDepTag provides default implementation of HighPriorityPackagingItem interface. |
| 409 | type highPriorityDepTag struct { |
| 410 | blueprint.DependencyTag |
| 411 | } |
| 412 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 413 | // See PackageModule.AddDeps |
Jiyong Park | 65b6224 | 2020-11-25 12:44:59 +0900 | [diff] [blame] | 414 | func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 415 | highPriorityDeps := p.highPriorityDeps() |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 416 | for _, t := range getSupportedTargets(ctx) { |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 417 | for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) { |
| 418 | if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) { |
| 419 | continue |
| 420 | } |
Spandan Das | 405f2d4 | 2024-10-22 18:31:25 +0000 | [diff] [blame] | 421 | targetVariation := t.Variations() |
| 422 | sharedVariation := blueprint.Variation{ |
| 423 | Mutator: "link", |
| 424 | Variation: "shared", |
| 425 | } |
| 426 | // If a shared variation exists, use that. Static variants do not provide any standalone files |
| 427 | // for packaging. |
| 428 | if ctx.OtherModuleFarDependencyVariantExists([]blueprint.Variation{sharedVariation}, dep) { |
| 429 | targetVariation = append(targetVariation, sharedVariation) |
| 430 | } |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 431 | if InList(dep, highPriorityDeps) { |
| 432 | depTag = highPriorityDepTag{depTag} |
| 433 | } |
| 434 | |
Spandan Das | 405f2d4 | 2024-10-22 18:31:25 +0000 | [diff] [blame] | 435 | ctx.AddFarVariationDependencies(targetVariation, depTag, dep) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 440 | func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 441 | // packaging specs gathered from the dep that are not high priorities. |
| 442 | var regularPriorities []PackagingSpec |
| 443 | |
| 444 | // all packaging specs gathered from the high priority deps. |
| 445 | var highPriorities []PackagingSpec |
| 446 | |
Spandan Das | 6c2b01d | 2024-10-22 22:16:04 +0000 | [diff] [blame] | 447 | // Name of the dependency which requested the packaging spec. |
| 448 | // If this dep is overridden, the packaging spec will not be installed via this dependency chain. |
| 449 | // (the packaging spec might still be installed if there are some other deps which depend on it). |
| 450 | var depNames []string |
| 451 | |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 452 | // list of module names overridden |
| 453 | var overridden []string |
Jiyong Park | c6a773d | 2024-05-14 21:49:11 +0900 | [diff] [blame] | 454 | |
| 455 | var arches []ArchType |
Jiyong Park | e604378 | 2024-05-20 16:17:39 +0900 | [diff] [blame] | 456 | for _, target := range getSupportedTargets(ctx) { |
Jiyong Park | c6a773d | 2024-05-14 21:49:11 +0900 | [diff] [blame] | 457 | arches = append(arches, target.Arch.ArchType) |
| 458 | } |
| 459 | |
| 460 | // filter out packaging specs for unsupported architecture |
| 461 | filterArch := func(ps PackagingSpec) bool { |
| 462 | for _, arch := range arches { |
| 463 | if arch == ps.archType { |
| 464 | return true |
| 465 | } |
| 466 | } |
| 467 | return false |
| 468 | } |
| 469 | |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 470 | ctx.VisitDirectDeps(func(child Module) { |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 471 | depTag := ctx.OtherModuleDependencyTag(child) |
| 472 | if pi, ok := depTag.(PackagingItem); !ok || !pi.IsPackagingItem() { |
Jooyung Han | 092ef81 | 2021-03-10 15:40:34 +0900 | [diff] [blame] | 473 | return |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 474 | } |
Yu Liu | bad1eef | 2024-08-21 22:37:35 +0000 | [diff] [blame] | 475 | for _, ps := range OtherModuleProviderOrDefault( |
| 476 | ctx, child, InstallFilesProvider).TransitivePackagingSpecs.ToList() { |
Jiyong Park | c6a773d | 2024-05-14 21:49:11 +0900 | [diff] [blame] | 477 | if !filterArch(ps) { |
| 478 | continue |
| 479 | } |
| 480 | |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 481 | if filter != nil { |
| 482 | if !filter(ps) { |
| 483 | continue |
| 484 | } |
| 485 | } |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 486 | |
| 487 | if _, ok := depTag.(highPriorityDepTag); ok { |
| 488 | highPriorities = append(highPriorities, ps) |
| 489 | } else { |
| 490 | regularPriorities = append(regularPriorities, ps) |
| 491 | } |
| 492 | |
Spandan Das | 6c2b01d | 2024-10-22 22:16:04 +0000 | [diff] [blame] | 493 | depNames = append(depNames, child.Name()) |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 494 | if ps.overrides != nil { |
| 495 | overridden = append(overridden, *ps.overrides...) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 496 | } |
| 497 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 498 | }) |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 499 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 500 | filterOverridden := func(input []PackagingSpec) []PackagingSpec { |
| 501 | // input minus packaging specs that are overridden |
| 502 | var filtered []PackagingSpec |
| 503 | for index, ps := range input { |
| 504 | if ps.owner != "" && InList(ps.owner, overridden) { |
| 505 | continue |
| 506 | } |
| 507 | // The dependency which requested this packaging spec has been overridden. |
| 508 | if InList(depNames[index], overridden) { |
| 509 | continue |
| 510 | } |
| 511 | filtered = append(filtered, ps) |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 512 | } |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 513 | return filtered |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 514 | } |
| 515 | |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 516 | filteredRegularPriority := filterOverridden(regularPriorities) |
| 517 | |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 518 | m := make(map[string]PackagingSpec) |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 519 | for _, ps := range filteredRegularPriority { |
Jiyong Park | a574d53 | 2024-08-28 18:06:43 +0900 | [diff] [blame] | 520 | dstPath := ps.relPathInPackage |
| 521 | if existingPs, ok := m[dstPath]; ok { |
| 522 | if !existingPs.Equals(&ps) { |
| 523 | ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps) |
| 524 | } |
| 525 | continue |
| 526 | } |
| 527 | m[dstPath] = ps |
| 528 | } |
Jihoon Kang | 79196c5 | 2024-10-30 18:49:47 +0000 | [diff] [blame^] | 529 | |
| 530 | filteredHighPriority := filterOverridden(highPriorities) |
| 531 | highPriorityPs := make(map[string]PackagingSpec) |
| 532 | for _, ps := range filteredHighPriority { |
| 533 | dstPath := ps.relPathInPackage |
| 534 | if existingPs, ok := highPriorityPs[dstPath]; ok { |
| 535 | if !existingPs.Equals(&ps) { |
| 536 | ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps) |
| 537 | } |
| 538 | continue |
| 539 | } |
| 540 | highPriorityPs[dstPath] = ps |
| 541 | m[dstPath] = ps |
| 542 | } |
| 543 | |
Jooyung Han | df09d17 | 2021-05-11 11:13:30 +0900 | [diff] [blame] | 544 | return m |
| 545 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 546 | |
Jeongik Cha | 54bf875 | 2024-02-08 10:44:37 +0900 | [diff] [blame] | 547 | // See PackageModule.GatherPackagingSpecs |
| 548 | func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec { |
| 549 | return p.GatherPackagingSpecsWithFilter(ctx, nil) |
| 550 | } |
| 551 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 552 | // CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec |
| 553 | // entries into the specified directory. |
Peter Collingbourne | ff56c01 | 2023-03-15 22:24:03 -0700 | [diff] [blame] | 554 | 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] | 555 | dirsToSpecs := make(map[WritablePath]map[string]PackagingSpec) |
| 556 | dirsToSpecs[dir] = specs |
| 557 | return p.CopySpecsToDirs(ctx, builder, dirsToSpecs) |
| 558 | } |
| 559 | |
| 560 | // CopySpecsToDirs is a helper that will add commands to the rule builder to copy the PackagingSpec |
| 561 | // entries into corresponding directories. |
| 562 | func (p *PackagingBase) CopySpecsToDirs(ctx ModuleContext, builder *RuleBuilder, dirsToSpecs map[WritablePath]map[string]PackagingSpec) (entries []string) { |
| 563 | empty := true |
| 564 | for _, specs := range dirsToSpecs { |
| 565 | if len(specs) > 0 { |
| 566 | empty = false |
| 567 | break |
| 568 | } |
| 569 | } |
| 570 | if empty { |
Cole Faust | 3b3a011 | 2024-01-03 15:16:55 -0800 | [diff] [blame] | 571 | return entries |
| 572 | } |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 573 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 574 | seenDir := make(map[string]bool) |
Jeongik Cha | 76e677f | 2023-12-21 16:39:15 +0900 | [diff] [blame] | 575 | preparerPath := PathForModuleOut(ctx, "preparer.sh") |
| 576 | cmd := builder.Command().Tool(preparerPath) |
| 577 | var sb strings.Builder |
Cole Faust | 3b3a011 | 2024-01-03 15:16:55 -0800 | [diff] [blame] | 578 | sb.WriteString("set -e\n") |
Inseob Kim | 33f95a9 | 2024-07-11 15:44:49 +0900 | [diff] [blame] | 579 | |
| 580 | dirs := make([]WritablePath, 0, len(dirsToSpecs)) |
| 581 | for dir, _ := range dirsToSpecs { |
| 582 | dirs = append(dirs, dir) |
| 583 | } |
| 584 | sort.Slice(dirs, func(i, j int) bool { |
| 585 | return dirs[i].String() < dirs[j].String() |
| 586 | }) |
| 587 | |
| 588 | for _, dir := range dirs { |
| 589 | specs := dirsToSpecs[dir] |
| 590 | for _, k := range SortedKeys(specs) { |
| 591 | ps := specs[k] |
| 592 | destPath := filepath.Join(dir.String(), ps.relPathInPackage) |
| 593 | destDir := filepath.Dir(destPath) |
| 594 | entries = append(entries, ps.relPathInPackage) |
| 595 | if _, ok := seenDir[destDir]; !ok { |
| 596 | seenDir[destDir] = true |
| 597 | sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir)) |
| 598 | } |
| 599 | if ps.symlinkTarget == "" { |
| 600 | cmd.Implicit(ps.srcPath) |
| 601 | sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath)) |
| 602 | } else { |
| 603 | sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath)) |
| 604 | } |
| 605 | if ps.executable { |
| 606 | sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath)) |
| 607 | } |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
Jeongik Cha | 76e677f | 2023-12-21 16:39:15 +0900 | [diff] [blame] | 611 | WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String()) |
| 612 | |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 613 | return entries |
| 614 | } |
| 615 | |
| 616 | // See PackageModule.CopyDepsToZip |
Jooyung Han | a883428 | 2022-03-25 11:40:12 +0900 | [diff] [blame] | 617 | 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] | 618 | builder := NewRuleBuilder(pctx, ctx) |
| 619 | |
| 620 | dir := PathForModuleOut(ctx, ".zip") |
| 621 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 622 | builder.Command().Text("mkdir").Flag("-p").Text(dir.String()) |
Jooyung Han | a883428 | 2022-03-25 11:40:12 +0900 | [diff] [blame] | 623 | entries = p.CopySpecsToDir(ctx, builder, specs, dir) |
Dan Willemsen | 9fe1410 | 2021-07-13 21:52:04 -0700 | [diff] [blame] | 624 | |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 625 | builder.Command(). |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 626 | BuiltTool("soong_zip"). |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 627 | FlagWithOutput("-o ", zipOut). |
| 628 | FlagWithArg("-C ", dir.String()). |
| 629 | Flag("-L 0"). // no compression because this will be unzipped soon |
| 630 | FlagWithArg("-D ", dir.String()) |
| 631 | builder.Command().Text("rm").Flag("-rf").Text(dir.String()) |
| 632 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 633 | builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName())) |
Jiyong Park | dda8f69 | 2020-11-09 18:38:48 +0900 | [diff] [blame] | 634 | return entries |
| 635 | } |