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