blob: c4cc6b98c309cb7ac4f2bad8d70cdaddc8f98c16 [file] [log] [blame]
Jiyong Park073ea552020-11-09 14:08:34 +09001// 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
15package android
16
Jiyong Parkdda8f692020-11-09 18:38:48 +090017import (
18 "fmt"
19 "path/filepath"
Inseob Kim33f95a92024-07-11 15:44:49 +090020 "sort"
Jeongik Cha76e677f2023-12-21 16:39:15 +090021 "strings"
Jiyong Parkdda8f692020-11-09 18:38:48 +090022
23 "github.com/google/blueprint"
Yu Liu3cadf7d2024-10-24 18:47:06 +000024 "github.com/google/blueprint/gobtools"
Jiyong Park105e11c2024-05-17 14:58:24 +090025 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090026)
27
Jiyong Parkcc1157c2020-11-25 11:31:13 +090028// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
29// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
30// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
31// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090032type PackagingSpec struct {
33 // Path relative to the root of the package
34 relPathInPackage string
35
36 // The path to the built artifact
37 srcPath Path
38
39 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
40 // srcPath is of course ignored.)
41 symlinkTarget string
42
43 // Whether relPathInPackage should be marked as executable or not
44 executable bool
Dan Willemsen9fe14102021-07-13 21:52:04 -070045
46 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090047
48 partition string
Jiyong Park4152b192024-04-30 21:24:21 +090049
50 // Whether this packaging spec represents an installation of the srcPath (i.e. this struct
51 // is created via InstallFile or InstallSymlink) or a simple packaging (i.e. created via
52 // PackageFile).
53 skipInstall bool
Justin Yun74f3f302024-05-07 14:32:14 +090054
55 // Paths of aconfig files for the built artifact
56 aconfigPaths *Paths
Jiyong Parkc6a773d2024-05-14 21:49:11 +090057
58 // ArchType of the module which produced this packaging spec
59 archType ArchType
Jiyong Parka574d532024-08-28 18:06:43 +090060
61 // List of module names that this packaging spec overrides
62 overrides *[]string
63
64 // Name of the module where this packaging spec is output of
65 owner string
Jiyong Park073ea552020-11-09 14:08:34 +090066}
Jiyong Parkdda8f692020-11-09 18:38:48 +090067
Yu Liu467d7c52024-09-18 21:54:44 +000068type packagingSpecGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +000069 RelPathInPackage string
70 SrcPath Path
71 SymlinkTarget string
72 Executable bool
73 EffectiveLicenseFiles *Paths
74 Partition string
75 SkipInstall bool
76 AconfigPaths *Paths
77 ArchType ArchType
78 Overrides *[]string
79 Owner string
Yu Liu467d7c52024-09-18 21:54:44 +000080}
Yu Liu26a716d2024-08-30 23:40:32 +000081
Yu Liu467d7c52024-09-18 21:54:44 +000082func (p *PackagingSpec) ToGob() *packagingSpecGob {
83 return &packagingSpecGob{
Yu Liu5246a7e2024-10-09 20:04:52 +000084 RelPathInPackage: p.relPathInPackage,
85 SrcPath: p.srcPath,
86 SymlinkTarget: p.symlinkTarget,
87 Executable: p.executable,
88 EffectiveLicenseFiles: p.effectiveLicenseFiles,
89 Partition: p.partition,
90 SkipInstall: p.skipInstall,
91 AconfigPaths: p.aconfigPaths,
92 ArchType: p.archType,
93 Overrides: p.overrides,
94 Owner: p.owner,
Yu Liu467d7c52024-09-18 21:54:44 +000095 }
96}
97
98func (p *PackagingSpec) FromGob(data *packagingSpecGob) {
99 p.relPathInPackage = data.RelPathInPackage
100 p.srcPath = data.SrcPath
101 p.symlinkTarget = data.SymlinkTarget
102 p.executable = data.Executable
Yu Liu5246a7e2024-10-09 20:04:52 +0000103 p.effectiveLicenseFiles = data.EffectiveLicenseFiles
Yu Liu467d7c52024-09-18 21:54:44 +0000104 p.partition = data.Partition
105 p.skipInstall = data.SkipInstall
106 p.aconfigPaths = data.AconfigPaths
107 p.archType = data.ArchType
108 p.overrides = data.Overrides
109 p.owner = data.Owner
110}
111
112func (p *PackagingSpec) GobEncode() ([]byte, error) {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000113 return gobtools.CustomGobEncode[packagingSpecGob](p)
Yu Liu26a716d2024-08-30 23:40:32 +0000114}
115
116func (p *PackagingSpec) GobDecode(data []byte) error {
Yu Liu3cadf7d2024-10-24 18:47:06 +0000117 return gobtools.CustomGobDecode[packagingSpecGob](data, p)
Yu Liu26a716d2024-08-30 23:40:32 +0000118}
119
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000120func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
121 if other == nil {
122 return false
123 }
124 if p.relPathInPackage != other.relPathInPackage {
125 return false
126 }
127 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
128 return false
129 }
130 if p.executable != other.executable {
131 return false
132 }
133 if p.partition != other.partition {
134 return false
135 }
136 return true
137}
138
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900139// Get file name of installed package
140func (p *PackagingSpec) FileName() string {
141 if p.relPathInPackage != "" {
142 return filepath.Base(p.relPathInPackage)
143 }
144
145 return ""
146}
147
Jiyong Park6446b622021-02-01 20:08:28 +0900148// Path relative to the root of the package
149func (p *PackagingSpec) RelPathInPackage() string {
150 return p.relPathInPackage
151}
152
Dan Willemsen9fe14102021-07-13 21:52:04 -0700153func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
154 p.relPathInPackage = relPathInPackage
155}
156
157func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
158 if p.effectiveLicenseFiles == nil {
159 return Paths{}
160 }
161 return *p.effectiveLicenseFiles
162}
163
Jooyung Han99c5fe62022-03-21 15:13:38 +0900164func (p *PackagingSpec) Partition() string {
165 return p.partition
166}
167
Jiyong Park4152b192024-04-30 21:24:21 +0900168func (p *PackagingSpec) SkipInstall() bool {
169 return p.skipInstall
170}
171
Justin Yun74f3f302024-05-07 14:32:14 +0900172// Paths of aconfig files for the built artifact
173func (p *PackagingSpec) GetAconfigPaths() Paths {
174 return *p.aconfigPaths
175}
176
Jiyong Parkdda8f692020-11-09 18:38:48 +0900177type PackageModule interface {
178 Module
179 packagingBase() *PackagingBase
180
181 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900182 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
183 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900184 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900185
Jooyung Hana8834282022-03-25 11:40:12 +0900186 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
187 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900188 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900189
Jiyong Parkdda8f692020-11-09 18:38:48 +0900190 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900191 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900192 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
193 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900194 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900195}
196
197// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
198// include this struct and call InitPackageModule.
199type PackagingBase struct {
200 properties PackagingProperties
201
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900202 // Allows this module to skip missing dependencies. In most cases, this is not required, but
203 // for rare cases like when there's a dependency to a module which exists in certain repo
204 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900205 IgnoreMissingDependencies bool
Jiyong Park3ea9b652024-05-15 23:01:54 +0900206
207 // If this is set to true by a module type inheriting PackagingBase, the deps property
208 // collects the first target only even with compile_multilib: true.
209 DepsCollectFirstTargetOnly bool
Jihoon Kang79196c52024-10-30 18:49:47 +0000210
211 // If this is set to try by a module type inheriting PackagingBase, the module type is
212 // allowed to utilize High_priority_deps.
213 AllowHighPriorityDeps bool
Jiyong Parkdda8f692020-11-09 18:38:48 +0900214}
215
Jihoon Kang79196c52024-10-30 18:49:47 +0000216type DepsProperty struct {
217 // Deps that have higher priority in packaging when there is a packaging conflict.
218 // For example, if multiple files are being installed to same filepath, the install file
219 // of the module listed in this property will have a higher priority over those in other
220 // deps properties.
221 High_priority_deps []string `android:"arch_variant"`
222
Jiyong Parkdda8f692020-11-09 18:38:48 +0900223 // Modules to include in this package
Jiyong Park105e11c2024-05-17 14:58:24 +0900224 Deps proptools.Configurable[[]string] `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900225}
226
227type packagingMultilibProperties struct {
Jihoon Kang79196c52024-10-30 18:49:47 +0000228 First DepsProperty `android:"arch_variant"`
229 Common DepsProperty `android:"arch_variant"`
230 Lib32 DepsProperty `android:"arch_variant"`
231 Lib64 DepsProperty `android:"arch_variant"`
232 Both DepsProperty `android:"arch_variant"`
233 Prefer32 DepsProperty `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900234}
235
Jiyong Park2136d152021-02-01 23:24:56 +0900236type packagingArchProperties struct {
Jihoon Kang79196c52024-10-30 18:49:47 +0000237 Arm64 DepsProperty
238 Arm DepsProperty
239 X86_64 DepsProperty
240 X86 DepsProperty
Jiyong Park2136d152021-02-01 23:24:56 +0900241}
242
Jiyong Parkdda8f692020-11-09 18:38:48 +0900243type PackagingProperties struct {
Jihoon Kang79196c52024-10-30 18:49:47 +0000244 DepsProperty
245
246 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900247 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900248}
249
Jiyong Parkdda8f692020-11-09 18:38:48 +0900250func InitPackageModule(p PackageModule) {
251 base := p.packagingBase()
Jihoon Kang79196c52024-10-30 18:49:47 +0000252 p.AddProperties(&base.properties, &base.properties.DepsProperty)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900253}
254
255func (p *PackagingBase) packagingBase() *PackagingBase {
256 return p
257}
258
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900259// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
260// the current archicture when this module is not configured for multi target. When configured for
261// multi target, deps is selected for each of the targets and is NOT selected for the current
262// architecture which would be Common.
Cole Faust0c5eaed2024-11-01 11:05:00 -0700263// It returns two lists, the normal and high priority deps, respectively.
264func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) ([]string, []string) {
265 var normalDeps []string
266 var highPriorityDeps []string
267
268 get := func(prop DepsProperty) {
269 normalDeps = append(normalDeps, prop.Deps.GetOrDefault(ctx, nil)...)
270 highPriorityDeps = append(highPriorityDeps, prop.High_priority_deps...)
271 }
272 has := func(prop DepsProperty) bool {
273 return len(prop.Deps.GetOrDefault(ctx, nil)) > 0 || len(prop.High_priority_deps) > 0
Jihoon Kang79196c52024-10-30 18:49:47 +0000274 }
275
Jiyong Parkdda8f692020-11-09 18:38:48 +0900276 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700277 get(p.properties.DepsProperty)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900278 } else if arch.Multilib == "lib32" {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700279 get(p.properties.Multilib.Lib32)
Jiyong Parke6043782024-05-20 16:17:39 +0900280 // multilib.prefer32.deps are added for lib32 only when they support 32-bit arch
Cole Faust0c5eaed2024-11-01 11:05:00 -0700281 for _, dep := range p.properties.Multilib.Prefer32.Deps.GetOrDefault(ctx, nil) {
Jiyong Parke6043782024-05-20 16:17:39 +0900282 if checkIfOtherModuleSupportsLib32(ctx, dep) {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700283 normalDeps = append(normalDeps, dep)
284 }
285 }
286 for _, dep := range p.properties.Multilib.Prefer32.High_priority_deps {
287 if checkIfOtherModuleSupportsLib32(ctx, dep) {
288 highPriorityDeps = append(highPriorityDeps, dep)
Jiyong Parke6043782024-05-20 16:17:39 +0900289 }
290 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900291 } else if arch.Multilib == "lib64" {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700292 get(p.properties.Multilib.Lib64)
Jiyong Parke6043782024-05-20 16:17:39 +0900293 // multilib.prefer32.deps are added for lib64 only when they don't support 32-bit arch
Cole Faust0c5eaed2024-11-01 11:05:00 -0700294 for _, dep := range p.properties.Multilib.Prefer32.Deps.GetOrDefault(ctx, nil) {
Jiyong Parke6043782024-05-20 16:17:39 +0900295 if !checkIfOtherModuleSupportsLib32(ctx, dep) {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700296 normalDeps = append(normalDeps, dep)
297 }
298 }
299 for _, dep := range p.properties.Multilib.Prefer32.High_priority_deps {
300 if !checkIfOtherModuleSupportsLib32(ctx, dep) {
301 highPriorityDeps = append(highPriorityDeps, dep)
Jiyong Parke6043782024-05-20 16:17:39 +0900302 }
303 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900304 } else if arch == Common {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700305 get(p.properties.Multilib.Common)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900306 }
Jiyong Park2136d152021-02-01 23:24:56 +0900307
Jiyong Park3ea9b652024-05-15 23:01:54 +0900308 if p.DepsCollectFirstTargetOnly {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700309 if has(p.properties.Multilib.First) {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900310 ctx.PropertyErrorf("multilib.first.deps", "not supported. use \"deps\" instead")
311 }
312 for i, t := range ctx.MultiTargets() {
313 if t.Arch.ArchType == arch {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700314 get(p.properties.Multilib.Both)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900315 if i == 0 {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700316 get(p.properties.DepsProperty)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900317 }
318 }
319 }
320 } else {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700321 if has(p.properties.Multilib.Both) {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900322 ctx.PropertyErrorf("multilib.both.deps", "not supported. use \"deps\" instead")
323 }
324 for i, t := range ctx.MultiTargets() {
325 if t.Arch.ArchType == arch {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700326 get(p.properties.DepsProperty)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900327 if i == 0 {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700328 get(p.properties.Multilib.First)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900329 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900330 }
331 }
332 }
Jiyong Park2136d152021-02-01 23:24:56 +0900333
334 if ctx.Arch().ArchType == Common {
335 switch arch {
336 case Arm64:
Cole Faust0c5eaed2024-11-01 11:05:00 -0700337 get(p.properties.Arch.Arm64)
Jiyong Park2136d152021-02-01 23:24:56 +0900338 case Arm:
Cole Faust0c5eaed2024-11-01 11:05:00 -0700339 get(p.properties.Arch.Arm)
Jiyong Park2136d152021-02-01 23:24:56 +0900340 case X86_64:
Cole Faust0c5eaed2024-11-01 11:05:00 -0700341 get(p.properties.Arch.X86_64)
Jiyong Park2136d152021-02-01 23:24:56 +0900342 case X86:
Cole Faust0c5eaed2024-11-01 11:05:00 -0700343 get(p.properties.Arch.X86)
Jiyong Park2136d152021-02-01 23:24:56 +0900344 }
345 }
346
Cole Faust0c5eaed2024-11-01 11:05:00 -0700347 if len(highPriorityDeps) > 0 && !p.AllowHighPriorityDeps {
348 ctx.ModuleErrorf("Usage of high_priority_deps is not allowed for %s module type", ctx.ModuleType())
349 }
350
351 return FirstUniqueStrings(normalDeps), FirstUniqueStrings(highPriorityDeps)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900352}
353
Jiyong Parke6043782024-05-20 16:17:39 +0900354func getSupportedTargets(ctx BaseModuleContext) []Target {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900355 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 Parke6043782024-05-20 16:17:39 +0900366// 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.
368func 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.
378func 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 Han092ef812021-03-10 15:40:34 +0900388// PackagingItem is a marker interface for dependency tags.
389// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
390type PackagingItem interface {
391 // IsPackagingItem returns true if the dep is to be packaged
392 IsPackagingItem() bool
393}
394
Jihoon Kang79196c52024-10-30 18:49:47 +0000395var _ PackagingItem = (*PackagingItemAlwaysDepTag)(nil)
396
Jooyung Han092ef812021-03-10 15:40:34 +0900397// 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().
400type PackagingItemAlwaysDepTag struct {
401}
402
403// IsPackagingItem returns true if the dep is to be packaged
404func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
405 return true
406}
407
Jihoon Kang79196c52024-10-30 18:49:47 +0000408// highPriorityDepTag provides default implementation of HighPriorityPackagingItem interface.
409type highPriorityDepTag struct {
410 blueprint.DependencyTag
411}
412
Jiyong Parkdda8f692020-11-09 18:38:48 +0900413// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900414func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Cole Faust0c5eaed2024-11-01 11:05:00 -0700415 addDep := func(t Target, dep string, highPriority bool) {
416 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
417 return
418 }
419 targetVariation := t.Variations()
420 sharedVariation := blueprint.Variation{
421 Mutator: "link",
422 Variation: "shared",
423 }
424 // If a shared variation exists, use that. Static variants do not provide any standalone files
425 // for packaging.
426 if ctx.OtherModuleFarDependencyVariantExists([]blueprint.Variation{sharedVariation}, dep) {
427 targetVariation = append(targetVariation, sharedVariation)
428 }
429 depTagToUse := depTag
430 if highPriority {
431 depTagToUse = highPriorityDepTag{depTag}
432 }
Jihoon Kang79196c52024-10-30 18:49:47 +0000433
Cole Faust0c5eaed2024-11-01 11:05:00 -0700434 ctx.AddFarVariationDependencies(targetVariation, depTagToUse, dep)
435 }
436 for _, t := range getSupportedTargets(ctx) {
437 normalDeps, highPriorityDeps := p.getDepsForArch(ctx, t.Arch.ArchType)
438 for _, dep := range normalDeps {
439 addDep(t, dep, false)
440 }
441 for _, dep := range highPriorityDeps {
442 addDep(t, dep, true)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900443 }
444 }
445}
446
Jeongik Cha54bf8752024-02-08 10:44:37 +0900447func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jihoon Kang79196c52024-10-30 18:49:47 +0000448 // packaging specs gathered from the dep that are not high priorities.
449 var regularPriorities []PackagingSpec
450
451 // all packaging specs gathered from the high priority deps.
452 var highPriorities []PackagingSpec
453
Spandan Das6c2b01d2024-10-22 22:16:04 +0000454 // Name of the dependency which requested the packaging spec.
455 // If this dep is overridden, the packaging spec will not be installed via this dependency chain.
456 // (the packaging spec might still be installed if there are some other deps which depend on it).
457 var depNames []string
458
Jiyong Parka574d532024-08-28 18:06:43 +0900459 // list of module names overridden
460 var overridden []string
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900461
462 var arches []ArchType
Jiyong Parke6043782024-05-20 16:17:39 +0900463 for _, target := range getSupportedTargets(ctx) {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900464 arches = append(arches, target.Arch.ArchType)
465 }
466
467 // filter out packaging specs for unsupported architecture
468 filterArch := func(ps PackagingSpec) bool {
469 for _, arch := range arches {
470 if arch == ps.archType {
471 return true
472 }
473 }
474 return false
475 }
476
Yu Liuac483e02024-11-11 22:29:30 +0000477 ctx.VisitDirectDepsProxy(func(child ModuleProxy) {
Jihoon Kang79196c52024-10-30 18:49:47 +0000478 depTag := ctx.OtherModuleDependencyTag(child)
479 if pi, ok := depTag.(PackagingItem); !ok || !pi.IsPackagingItem() {
Jooyung Han092ef812021-03-10 15:40:34 +0900480 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900481 }
Yu Liubad1eef2024-08-21 22:37:35 +0000482 for _, ps := range OtherModuleProviderOrDefault(
483 ctx, child, InstallFilesProvider).TransitivePackagingSpecs.ToList() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900484 if !filterArch(ps) {
485 continue
486 }
487
Jeongik Cha54bf8752024-02-08 10:44:37 +0900488 if filter != nil {
489 if !filter(ps) {
490 continue
491 }
492 }
Jihoon Kang79196c52024-10-30 18:49:47 +0000493
494 if _, ok := depTag.(highPriorityDepTag); ok {
495 highPriorities = append(highPriorities, ps)
496 } else {
497 regularPriorities = append(regularPriorities, ps)
498 }
499
Spandan Das6c2b01d2024-10-22 22:16:04 +0000500 depNames = append(depNames, child.Name())
Jiyong Parka574d532024-08-28 18:06:43 +0900501 if ps.overrides != nil {
502 overridden = append(overridden, *ps.overrides...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900503 }
504 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900505 })
Jiyong Parka574d532024-08-28 18:06:43 +0900506
Jihoon Kang79196c52024-10-30 18:49:47 +0000507 filterOverridden := func(input []PackagingSpec) []PackagingSpec {
508 // input minus packaging specs that are overridden
509 var filtered []PackagingSpec
510 for index, ps := range input {
511 if ps.owner != "" && InList(ps.owner, overridden) {
512 continue
513 }
514 // The dependency which requested this packaging spec has been overridden.
515 if InList(depNames[index], overridden) {
516 continue
517 }
518 filtered = append(filtered, ps)
Jiyong Parka574d532024-08-28 18:06:43 +0900519 }
Jihoon Kang79196c52024-10-30 18:49:47 +0000520 return filtered
Jiyong Parka574d532024-08-28 18:06:43 +0900521 }
522
Jihoon Kang79196c52024-10-30 18:49:47 +0000523 filteredRegularPriority := filterOverridden(regularPriorities)
524
Jiyong Parka574d532024-08-28 18:06:43 +0900525 m := make(map[string]PackagingSpec)
Jihoon Kang79196c52024-10-30 18:49:47 +0000526 for _, ps := range filteredRegularPriority {
Jiyong Parka574d532024-08-28 18:06:43 +0900527 dstPath := ps.relPathInPackage
528 if existingPs, ok := m[dstPath]; ok {
529 if !existingPs.Equals(&ps) {
530 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
531 }
532 continue
533 }
534 m[dstPath] = ps
535 }
Jihoon Kang79196c52024-10-30 18:49:47 +0000536
537 filteredHighPriority := filterOverridden(highPriorities)
538 highPriorityPs := make(map[string]PackagingSpec)
539 for _, ps := range filteredHighPriority {
540 dstPath := ps.relPathInPackage
541 if existingPs, ok := highPriorityPs[dstPath]; ok {
542 if !existingPs.Equals(&ps) {
543 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
544 }
545 continue
546 }
547 highPriorityPs[dstPath] = ps
548 m[dstPath] = ps
549 }
550
Jooyung Handf09d172021-05-11 11:13:30 +0900551 return m
552}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900553
Jeongik Cha54bf8752024-02-08 10:44:37 +0900554// See PackageModule.GatherPackagingSpecs
555func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
556 return p.GatherPackagingSpecsWithFilter(ctx, nil)
557}
558
Dan Willemsen9fe14102021-07-13 21:52:04 -0700559// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
560// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700561func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Inseob Kim33f95a92024-07-11 15:44:49 +0900562 dirsToSpecs := make(map[WritablePath]map[string]PackagingSpec)
563 dirsToSpecs[dir] = specs
564 return p.CopySpecsToDirs(ctx, builder, dirsToSpecs)
565}
566
567// CopySpecsToDirs is a helper that will add commands to the rule builder to copy the PackagingSpec
568// entries into corresponding directories.
569func (p *PackagingBase) CopySpecsToDirs(ctx ModuleContext, builder *RuleBuilder, dirsToSpecs map[WritablePath]map[string]PackagingSpec) (entries []string) {
570 empty := true
571 for _, specs := range dirsToSpecs {
572 if len(specs) > 0 {
573 empty = false
574 break
575 }
576 }
577 if empty {
Cole Faust3b3a0112024-01-03 15:16:55 -0800578 return entries
579 }
Inseob Kim33f95a92024-07-11 15:44:49 +0900580
Jiyong Parkdda8f692020-11-09 18:38:48 +0900581 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900582 preparerPath := PathForModuleOut(ctx, "preparer.sh")
583 cmd := builder.Command().Tool(preparerPath)
584 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800585 sb.WriteString("set -e\n")
Inseob Kim33f95a92024-07-11 15:44:49 +0900586
587 dirs := make([]WritablePath, 0, len(dirsToSpecs))
588 for dir, _ := range dirsToSpecs {
589 dirs = append(dirs, dir)
590 }
591 sort.Slice(dirs, func(i, j int) bool {
592 return dirs[i].String() < dirs[j].String()
593 })
594
595 for _, dir := range dirs {
596 specs := dirsToSpecs[dir]
597 for _, k := range SortedKeys(specs) {
598 ps := specs[k]
599 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
600 destDir := filepath.Dir(destPath)
601 entries = append(entries, ps.relPathInPackage)
602 if _, ok := seenDir[destDir]; !ok {
603 seenDir[destDir] = true
604 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
605 }
606 if ps.symlinkTarget == "" {
607 cmd.Implicit(ps.srcPath)
608 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
609 } else {
610 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
611 }
612 if ps.executable {
613 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
614 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900615 }
616 }
617
Jeongik Cha76e677f2023-12-21 16:39:15 +0900618 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
619
Dan Willemsen9fe14102021-07-13 21:52:04 -0700620 return entries
621}
622
623// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900624func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700625 builder := NewRuleBuilder(pctx, ctx)
626
627 dir := PathForModuleOut(ctx, ".zip")
628 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
629 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900630 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700631
Jiyong Parkdda8f692020-11-09 18:38:48 +0900632 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800633 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900634 FlagWithOutput("-o ", zipOut).
635 FlagWithArg("-C ", dir.String()).
636 Flag("-L 0"). // no compression because this will be unzipped soon
637 FlagWithArg("-D ", dir.String())
638 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
639
Colin Crossf1a035e2020-11-16 17:32:30 -0800640 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900641 return entries
642}