blob: acafcd4f2df4583a10f3899e1db77200d02bfa2b [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
Jiyong Parkdda8f692020-11-09 18:38:48 +0900210}
211
212type depsProperty struct {
213 // Modules to include in this package
Jiyong Park105e11c2024-05-17 14:58:24 +0900214 Deps proptools.Configurable[[]string] `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900215}
216
217type packagingMultilibProperties struct {
Jiyong Parke6043782024-05-20 16:17:39 +0900218 First depsProperty `android:"arch_variant"`
219 Common depsProperty `android:"arch_variant"`
220 Lib32 depsProperty `android:"arch_variant"`
221 Lib64 depsProperty `android:"arch_variant"`
222 Both depsProperty `android:"arch_variant"`
223 Prefer32 depsProperty `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900224}
225
Jiyong Park2136d152021-02-01 23:24:56 +0900226type packagingArchProperties struct {
227 Arm64 depsProperty
228 Arm depsProperty
229 X86_64 depsProperty
230 X86 depsProperty
231}
232
Jiyong Parkdda8f692020-11-09 18:38:48 +0900233type PackagingProperties struct {
Jiyong Park105e11c2024-05-17 14:58:24 +0900234 Deps proptools.Configurable[[]string] `android:"arch_variant"`
235 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900236 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900237}
238
Jiyong Parkdda8f692020-11-09 18:38:48 +0900239func InitPackageModule(p PackageModule) {
240 base := p.packagingBase()
241 p.AddProperties(&base.properties)
242}
243
244func (p *PackagingBase) packagingBase() *PackagingBase {
245 return p
246}
247
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900248// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
249// the current archicture when this module is not configured for multi target. When configured for
250// multi target, deps is selected for each of the targets and is NOT selected for the current
251// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900252func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
Jiyong Park105e11c2024-05-17 14:58:24 +0900253 get := func(prop proptools.Configurable[[]string]) []string {
254 return prop.GetOrDefault(ctx, nil)
255 }
256
Jiyong Parkdda8f692020-11-09 18:38:48 +0900257 var ret []string
258 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900259 ret = append(ret, get(p.properties.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900260 } else if arch.Multilib == "lib32" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900261 ret = append(ret, get(p.properties.Multilib.Lib32.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900262 // multilib.prefer32.deps are added for lib32 only when they support 32-bit arch
263 for _, dep := range get(p.properties.Multilib.Prefer32.Deps) {
264 if checkIfOtherModuleSupportsLib32(ctx, dep) {
265 ret = append(ret, dep)
266 }
267 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900268 } else if arch.Multilib == "lib64" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900269 ret = append(ret, get(p.properties.Multilib.Lib64.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900270 // multilib.prefer32.deps are added for lib64 only when they don't support 32-bit arch
271 for _, dep := range get(p.properties.Multilib.Prefer32.Deps) {
272 if !checkIfOtherModuleSupportsLib32(ctx, dep) {
273 ret = append(ret, dep)
274 }
275 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900276 } else if arch == Common {
Jiyong Park105e11c2024-05-17 14:58:24 +0900277 ret = append(ret, get(p.properties.Multilib.Common.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900278 }
Jiyong Park2136d152021-02-01 23:24:56 +0900279
Jiyong Park3ea9b652024-05-15 23:01:54 +0900280 if p.DepsCollectFirstTargetOnly {
Jiyong Park105e11c2024-05-17 14:58:24 +0900281 if len(get(p.properties.Multilib.First.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900282 ctx.PropertyErrorf("multilib.first.deps", "not supported. use \"deps\" instead")
283 }
284 for i, t := range ctx.MultiTargets() {
285 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900286 ret = append(ret, get(p.properties.Multilib.Both.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900287 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900288 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900289 }
290 }
291 }
292 } else {
Jiyong Park105e11c2024-05-17 14:58:24 +0900293 if len(get(p.properties.Multilib.Both.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900294 ctx.PropertyErrorf("multilib.both.deps", "not supported. use \"deps\" instead")
295 }
296 for i, t := range ctx.MultiTargets() {
297 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900298 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900299 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900300 ret = append(ret, get(p.properties.Multilib.First.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900301 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900302 }
303 }
304 }
Jiyong Park2136d152021-02-01 23:24:56 +0900305
306 if ctx.Arch().ArchType == Common {
307 switch arch {
308 case Arm64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900309 ret = append(ret, get(p.properties.Arch.Arm64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900310 case Arm:
Jiyong Park105e11c2024-05-17 14:58:24 +0900311 ret = append(ret, get(p.properties.Arch.Arm.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900312 case X86_64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900313 ret = append(ret, get(p.properties.Arch.X86_64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900314 case X86:
Jiyong Park105e11c2024-05-17 14:58:24 +0900315 ret = append(ret, get(p.properties.Arch.X86.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900316 }
317 }
318
Jiyong Parkdda8f692020-11-09 18:38:48 +0900319 return FirstUniqueStrings(ret)
320}
321
Jiyong Parke6043782024-05-20 16:17:39 +0900322func getSupportedTargets(ctx BaseModuleContext) []Target {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900323 var ret []Target
324 // The current and the common OS targets are always supported
325 ret = append(ret, ctx.Target())
326 if ctx.Arch().ArchType != Common {
327 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
328 }
329 // If this module is configured for multi targets, those should be supported as well
330 ret = append(ret, ctx.MultiTargets()...)
331 return ret
332}
333
Jiyong Parke6043782024-05-20 16:17:39 +0900334// getLib32Target returns the 32-bit target from the list of targets this module supports. If this
335// module doesn't support 32-bit target, nil is returned.
336func getLib32Target(ctx BaseModuleContext) *Target {
337 for _, t := range getSupportedTargets(ctx) {
338 if t.Arch.ArchType.Multilib == "lib32" {
339 return &t
340 }
341 }
342 return nil
343}
344
345// checkIfOtherModuleSUpportsLib32 returns true if 32-bit variant of dep exists.
346func checkIfOtherModuleSupportsLib32(ctx BaseModuleContext, dep string) bool {
347 t := getLib32Target(ctx)
348 if t == nil {
349 // This packaging module doesn't support 32bit. No point of checking if dep supports 32-bit
350 // or not.
351 return false
352 }
353 return ctx.OtherModuleFarDependencyVariantExists(t.Variations(), dep)
354}
355
Jooyung Han092ef812021-03-10 15:40:34 +0900356// PackagingItem is a marker interface for dependency tags.
357// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
358type PackagingItem interface {
359 // IsPackagingItem returns true if the dep is to be packaged
360 IsPackagingItem() bool
361}
362
363// DepTag provides default implementation of PackagingItem interface.
364// PackagingBase-derived modules can define their own dependency tag by embedding this, which
365// can be passed to AddDeps() or AddDependencies().
366type PackagingItemAlwaysDepTag struct {
367}
368
369// IsPackagingItem returns true if the dep is to be packaged
370func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
371 return true
372}
373
Jiyong Parkdda8f692020-11-09 18:38:48 +0900374// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900375func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parke6043782024-05-20 16:17:39 +0900376 for _, t := range getSupportedTargets(ctx) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900377 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
378 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
379 continue
380 }
Spandan Das405f2d42024-10-22 18:31:25 +0000381 targetVariation := t.Variations()
382 sharedVariation := blueprint.Variation{
383 Mutator: "link",
384 Variation: "shared",
385 }
386 // If a shared variation exists, use that. Static variants do not provide any standalone files
387 // for packaging.
388 if ctx.OtherModuleFarDependencyVariantExists([]blueprint.Variation{sharedVariation}, dep) {
389 targetVariation = append(targetVariation, sharedVariation)
390 }
391 ctx.AddFarVariationDependencies(targetVariation, depTag, dep)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900392 }
393 }
394}
395
Jeongik Cha54bf8752024-02-08 10:44:37 +0900396func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parka574d532024-08-28 18:06:43 +0900397 // all packaging specs gathered from the dep.
398 var all []PackagingSpec
Spandan Das6c2b01d2024-10-22 22:16:04 +0000399 // Name of the dependency which requested the packaging spec.
400 // If this dep is overridden, the packaging spec will not be installed via this dependency chain.
401 // (the packaging spec might still be installed if there are some other deps which depend on it).
402 var depNames []string
403
Jiyong Parka574d532024-08-28 18:06:43 +0900404 // list of module names overridden
405 var overridden []string
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900406
407 var arches []ArchType
Jiyong Parke6043782024-05-20 16:17:39 +0900408 for _, target := range getSupportedTargets(ctx) {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900409 arches = append(arches, target.Arch.ArchType)
410 }
411
412 // filter out packaging specs for unsupported architecture
413 filterArch := func(ps PackagingSpec) bool {
414 for _, arch := range arches {
415 if arch == ps.archType {
416 return true
417 }
418 }
419 return false
420 }
421
Jooyung Han092ef812021-03-10 15:40:34 +0900422 ctx.VisitDirectDeps(func(child Module) {
423 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
424 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900425 }
Yu Liubad1eef2024-08-21 22:37:35 +0000426 for _, ps := range OtherModuleProviderOrDefault(
427 ctx, child, InstallFilesProvider).TransitivePackagingSpecs.ToList() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900428 if !filterArch(ps) {
429 continue
430 }
431
Jeongik Cha54bf8752024-02-08 10:44:37 +0900432 if filter != nil {
433 if !filter(ps) {
434 continue
435 }
436 }
Jiyong Parka574d532024-08-28 18:06:43 +0900437 all = append(all, ps)
Spandan Das6c2b01d2024-10-22 22:16:04 +0000438 depNames = append(depNames, child.Name())
Jiyong Parka574d532024-08-28 18:06:43 +0900439 if ps.overrides != nil {
440 overridden = append(overridden, *ps.overrides...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900441 }
442 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900443 })
Jiyong Parka574d532024-08-28 18:06:43 +0900444
445 // all minus packaging specs that are overridden
446 var filtered []PackagingSpec
Spandan Das6c2b01d2024-10-22 22:16:04 +0000447 for index, ps := range all {
Jiyong Parka574d532024-08-28 18:06:43 +0900448 if ps.owner != "" && InList(ps.owner, overridden) {
449 continue
450 }
Spandan Das6c2b01d2024-10-22 22:16:04 +0000451 // The dependency which requested this packaging spec has been overridden.
452 if InList(depNames[index], overridden) {
453 continue
454 }
Jiyong Parka574d532024-08-28 18:06:43 +0900455 filtered = append(filtered, ps)
456 }
457
458 m := make(map[string]PackagingSpec)
459 for _, ps := range filtered {
460 dstPath := ps.relPathInPackage
461 if existingPs, ok := m[dstPath]; ok {
462 if !existingPs.Equals(&ps) {
463 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
464 }
465 continue
466 }
467 m[dstPath] = ps
468 }
Jooyung Handf09d172021-05-11 11:13:30 +0900469 return m
470}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900471
Jeongik Cha54bf8752024-02-08 10:44:37 +0900472// See PackageModule.GatherPackagingSpecs
473func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
474 return p.GatherPackagingSpecsWithFilter(ctx, nil)
475}
476
Dan Willemsen9fe14102021-07-13 21:52:04 -0700477// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
478// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700479func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Inseob Kim33f95a92024-07-11 15:44:49 +0900480 dirsToSpecs := make(map[WritablePath]map[string]PackagingSpec)
481 dirsToSpecs[dir] = specs
482 return p.CopySpecsToDirs(ctx, builder, dirsToSpecs)
483}
484
485// CopySpecsToDirs is a helper that will add commands to the rule builder to copy the PackagingSpec
486// entries into corresponding directories.
487func (p *PackagingBase) CopySpecsToDirs(ctx ModuleContext, builder *RuleBuilder, dirsToSpecs map[WritablePath]map[string]PackagingSpec) (entries []string) {
488 empty := true
489 for _, specs := range dirsToSpecs {
490 if len(specs) > 0 {
491 empty = false
492 break
493 }
494 }
495 if empty {
Cole Faust3b3a0112024-01-03 15:16:55 -0800496 return entries
497 }
Inseob Kim33f95a92024-07-11 15:44:49 +0900498
Jiyong Parkdda8f692020-11-09 18:38:48 +0900499 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900500 preparerPath := PathForModuleOut(ctx, "preparer.sh")
501 cmd := builder.Command().Tool(preparerPath)
502 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800503 sb.WriteString("set -e\n")
Inseob Kim33f95a92024-07-11 15:44:49 +0900504
505 dirs := make([]WritablePath, 0, len(dirsToSpecs))
506 for dir, _ := range dirsToSpecs {
507 dirs = append(dirs, dir)
508 }
509 sort.Slice(dirs, func(i, j int) bool {
510 return dirs[i].String() < dirs[j].String()
511 })
512
513 for _, dir := range dirs {
514 specs := dirsToSpecs[dir]
515 for _, k := range SortedKeys(specs) {
516 ps := specs[k]
517 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
518 destDir := filepath.Dir(destPath)
519 entries = append(entries, ps.relPathInPackage)
520 if _, ok := seenDir[destDir]; !ok {
521 seenDir[destDir] = true
522 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
523 }
524 if ps.symlinkTarget == "" {
525 cmd.Implicit(ps.srcPath)
526 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
527 } else {
528 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
529 }
530 if ps.executable {
531 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
532 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900533 }
534 }
535
Jeongik Cha76e677f2023-12-21 16:39:15 +0900536 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
537
Dan Willemsen9fe14102021-07-13 21:52:04 -0700538 return entries
539}
540
541// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900542func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700543 builder := NewRuleBuilder(pctx, ctx)
544
545 dir := PathForModuleOut(ctx, ".zip")
546 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
547 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900548 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700549
Jiyong Parkdda8f692020-11-09 18:38:48 +0900550 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800551 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900552 FlagWithOutput("-o ", zipOut).
553 FlagWithArg("-C ", dir.String()).
554 Flag("-L 0"). // no compression because this will be unzipped soon
555 FlagWithArg("-D ", dir.String())
556 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
557
Colin Crossf1a035e2020-11-16 17:32:30 -0800558 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900559 return entries
560}