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