blob: fe76bfc4a2f701da4202fc11908cc3dedd86d4ec [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"
Jiyong Park105e11c2024-05-17 14:58:24 +090024 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090025)
26
Jiyong Parkcc1157c2020-11-25 11:31:13 +090027// 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 Park073ea552020-11-09 14:08:34 +090031type 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 Willemsen9fe14102021-07-13 21:52:04 -070044
45 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090046
47 partition string
Jiyong Park4152b192024-04-30 21:24:21 +090048
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 Yun74f3f302024-05-07 14:32:14 +090053
54 // Paths of aconfig files for the built artifact
55 aconfigPaths *Paths
Jiyong Parkc6a773d2024-05-14 21:49:11 +090056
57 // ArchType of the module which produced this packaging spec
58 archType ArchType
Jiyong Parka574d532024-08-28 18:06:43 +090059
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 Park073ea552020-11-09 14:08:34 +090065}
Jiyong Parkdda8f692020-11-09 18:38:48 +090066
Yu Liu467d7c52024-09-18 21:54:44 +000067type packagingSpecGob struct {
Yu Liu5246a7e2024-10-09 20:04:52 +000068 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 Liu467d7c52024-09-18 21:54:44 +000079}
Yu Liu26a716d2024-08-30 23:40:32 +000080
Yu Liu467d7c52024-09-18 21:54:44 +000081func (p *PackagingSpec) ToGob() *packagingSpecGob {
82 return &packagingSpecGob{
Yu Liu5246a7e2024-10-09 20:04:52 +000083 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 Liu467d7c52024-09-18 21:54:44 +000094 }
95}
96
97func (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 Liu5246a7e2024-10-09 20:04:52 +0000102 p.effectiveLicenseFiles = data.EffectiveLicenseFiles
Yu Liu467d7c52024-09-18 21:54:44 +0000103 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
111func (p *PackagingSpec) GobEncode() ([]byte, error) {
112 return blueprint.CustomGobEncode[packagingSpecGob](p)
Yu Liu26a716d2024-08-30 23:40:32 +0000113}
114
115func (p *PackagingSpec) GobDecode(data []byte) error {
Yu Liu467d7c52024-09-18 21:54:44 +0000116 return blueprint.CustomGobDecode[packagingSpecGob](data, p)
Yu Liu26a716d2024-08-30 23:40:32 +0000117}
118
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000119func (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 Kim24dfc1f2020-11-16 10:48:44 +0900138// Get file name of installed package
139func (p *PackagingSpec) FileName() string {
140 if p.relPathInPackage != "" {
141 return filepath.Base(p.relPathInPackage)
142 }
143
144 return ""
145}
146
Jiyong Park6446b622021-02-01 20:08:28 +0900147// Path relative to the root of the package
148func (p *PackagingSpec) RelPathInPackage() string {
149 return p.relPathInPackage
150}
151
Dan Willemsen9fe14102021-07-13 21:52:04 -0700152func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
153 p.relPathInPackage = relPathInPackage
154}
155
156func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
157 if p.effectiveLicenseFiles == nil {
158 return Paths{}
159 }
160 return *p.effectiveLicenseFiles
161}
162
Jooyung Han99c5fe62022-03-21 15:13:38 +0900163func (p *PackagingSpec) Partition() string {
164 return p.partition
165}
166
Jiyong Park4152b192024-04-30 21:24:21 +0900167func (p *PackagingSpec) SkipInstall() bool {
168 return p.skipInstall
169}
170
Justin Yun74f3f302024-05-07 14:32:14 +0900171// Paths of aconfig files for the built artifact
172func (p *PackagingSpec) GetAconfigPaths() Paths {
173 return *p.aconfigPaths
174}
175
Jiyong Parkdda8f692020-11-09 18:38:48 +0900176type PackageModule interface {
177 Module
178 packagingBase() *PackagingBase
179
180 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900181 // 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 Park65b62242020-11-25 12:44:59 +0900183 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900184
Jooyung Hana8834282022-03-25 11:40:12 +0900185 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
186 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900187 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900188
Jiyong Parkdda8f692020-11-09 18:38:48 +0900189 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900190 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900191 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
192 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900193 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900194}
195
196// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
197// include this struct and call InitPackageModule.
198type PackagingBase struct {
199 properties PackagingProperties
200
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900201 // 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 Parkdda8f692020-11-09 18:38:48 +0900204 IgnoreMissingDependencies bool
Jiyong Park3ea9b652024-05-15 23:01:54 +0900205
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 Parkdda8f692020-11-09 18:38:48 +0900209}
210
211type depsProperty struct {
212 // Modules to include in this package
Jiyong Park105e11c2024-05-17 14:58:24 +0900213 Deps proptools.Configurable[[]string] `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900214}
215
216type packagingMultilibProperties struct {
Jiyong Parke6043782024-05-20 16:17:39 +0900217 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 Parkdda8f692020-11-09 18:38:48 +0900223}
224
Jiyong Park2136d152021-02-01 23:24:56 +0900225type packagingArchProperties struct {
226 Arm64 depsProperty
227 Arm depsProperty
228 X86_64 depsProperty
229 X86 depsProperty
230}
231
Jiyong Parkdda8f692020-11-09 18:38:48 +0900232type PackagingProperties struct {
Jiyong Park105e11c2024-05-17 14:58:24 +0900233 Deps proptools.Configurable[[]string] `android:"arch_variant"`
234 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900235 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900236}
237
Jiyong Parkdda8f692020-11-09 18:38:48 +0900238func InitPackageModule(p PackageModule) {
239 base := p.packagingBase()
240 p.AddProperties(&base.properties)
241}
242
243func (p *PackagingBase) packagingBase() *PackagingBase {
244 return p
245}
246
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900247// 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 Parkdda8f692020-11-09 18:38:48 +0900251func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
Jiyong Park105e11c2024-05-17 14:58:24 +0900252 get := func(prop proptools.Configurable[[]string]) []string {
253 return prop.GetOrDefault(ctx, nil)
254 }
255
Jiyong Parkdda8f692020-11-09 18:38:48 +0900256 var ret []string
257 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900258 ret = append(ret, get(p.properties.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900259 } else if arch.Multilib == "lib32" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900260 ret = append(ret, get(p.properties.Multilib.Lib32.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900261 // 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 Parkdda8f692020-11-09 18:38:48 +0900267 } else if arch.Multilib == "lib64" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900268 ret = append(ret, get(p.properties.Multilib.Lib64.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900269 // 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 Parkdda8f692020-11-09 18:38:48 +0900275 } else if arch == Common {
Jiyong Park105e11c2024-05-17 14:58:24 +0900276 ret = append(ret, get(p.properties.Multilib.Common.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900277 }
Jiyong Park2136d152021-02-01 23:24:56 +0900278
Jiyong Park3ea9b652024-05-15 23:01:54 +0900279 if p.DepsCollectFirstTargetOnly {
Jiyong Park105e11c2024-05-17 14:58:24 +0900280 if len(get(p.properties.Multilib.First.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900281 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 Park105e11c2024-05-17 14:58:24 +0900285 ret = append(ret, get(p.properties.Multilib.Both.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900286 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900287 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900288 }
289 }
290 }
291 } else {
Jiyong Park105e11c2024-05-17 14:58:24 +0900292 if len(get(p.properties.Multilib.Both.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900293 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 Park105e11c2024-05-17 14:58:24 +0900297 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900298 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900299 ret = append(ret, get(p.properties.Multilib.First.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900300 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900301 }
302 }
303 }
Jiyong Park2136d152021-02-01 23:24:56 +0900304
305 if ctx.Arch().ArchType == Common {
306 switch arch {
307 case Arm64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900308 ret = append(ret, get(p.properties.Arch.Arm64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900309 case Arm:
Jiyong Park105e11c2024-05-17 14:58:24 +0900310 ret = append(ret, get(p.properties.Arch.Arm.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900311 case X86_64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900312 ret = append(ret, get(p.properties.Arch.X86_64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900313 case X86:
Jiyong Park105e11c2024-05-17 14:58:24 +0900314 ret = append(ret, get(p.properties.Arch.X86.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900315 }
316 }
317
Jiyong Parkdda8f692020-11-09 18:38:48 +0900318 return FirstUniqueStrings(ret)
319}
320
Jiyong Parke6043782024-05-20 16:17:39 +0900321func getSupportedTargets(ctx BaseModuleContext) []Target {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900322 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 Parke6043782024-05-20 16:17:39 +0900333// 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.
335func 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.
345func 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 Han092ef812021-03-10 15:40:34 +0900355// PackagingItem is a marker interface for dependency tags.
356// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
357type 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().
365type PackagingItemAlwaysDepTag struct {
366}
367
368// IsPackagingItem returns true if the dep is to be packaged
369func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
370 return true
371}
372
Jiyong Parkdda8f692020-11-09 18:38:48 +0900373// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900374func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parke6043782024-05-20 16:17:39 +0900375 for _, t := range getSupportedTargets(ctx) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900376 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
377 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
378 continue
379 }
Spandan Das405f2d42024-10-22 18:31:25 +0000380 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 Parkdda8f692020-11-09 18:38:48 +0900391 }
392 }
393}
394
Jeongik Cha54bf8752024-02-08 10:44:37 +0900395func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parka574d532024-08-28 18:06:43 +0900396 // all packaging specs gathered from the dep.
397 var all []PackagingSpec
Spandan Das6c2b01d2024-10-22 22:16:04 +0000398 // 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 Parka574d532024-08-28 18:06:43 +0900403 // list of module names overridden
404 var overridden []string
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900405
406 var arches []ArchType
Jiyong Parke6043782024-05-20 16:17:39 +0900407 for _, target := range getSupportedTargets(ctx) {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900408 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 Han092ef812021-03-10 15:40:34 +0900421 ctx.VisitDirectDeps(func(child Module) {
422 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
423 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900424 }
Yu Liubad1eef2024-08-21 22:37:35 +0000425 for _, ps := range OtherModuleProviderOrDefault(
426 ctx, child, InstallFilesProvider).TransitivePackagingSpecs.ToList() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900427 if !filterArch(ps) {
428 continue
429 }
430
Jeongik Cha54bf8752024-02-08 10:44:37 +0900431 if filter != nil {
432 if !filter(ps) {
433 continue
434 }
435 }
Jiyong Parka574d532024-08-28 18:06:43 +0900436 all = append(all, ps)
Spandan Das6c2b01d2024-10-22 22:16:04 +0000437 depNames = append(depNames, child.Name())
Jiyong Parka574d532024-08-28 18:06:43 +0900438 if ps.overrides != nil {
439 overridden = append(overridden, *ps.overrides...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900440 }
441 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900442 })
Jiyong Parka574d532024-08-28 18:06:43 +0900443
444 // all minus packaging specs that are overridden
445 var filtered []PackagingSpec
Spandan Das6c2b01d2024-10-22 22:16:04 +0000446 for index, ps := range all {
Jiyong Parka574d532024-08-28 18:06:43 +0900447 if ps.owner != "" && InList(ps.owner, overridden) {
448 continue
449 }
Spandan Das6c2b01d2024-10-22 22:16:04 +0000450 // The dependency which requested this packaging spec has been overridden.
451 if InList(depNames[index], overridden) {
452 continue
453 }
Jiyong Parka574d532024-08-28 18:06:43 +0900454 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 Handf09d172021-05-11 11:13:30 +0900468 return m
469}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900470
Jeongik Cha54bf8752024-02-08 10:44:37 +0900471// See PackageModule.GatherPackagingSpecs
472func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
473 return p.GatherPackagingSpecsWithFilter(ctx, nil)
474}
475
Dan Willemsen9fe14102021-07-13 21:52:04 -0700476// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
477// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700478func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Inseob Kim33f95a92024-07-11 15:44:49 +0900479 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.
486func (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 Faust3b3a0112024-01-03 15:16:55 -0800495 return entries
496 }
Inseob Kim33f95a92024-07-11 15:44:49 +0900497
Jiyong Parkdda8f692020-11-09 18:38:48 +0900498 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900499 preparerPath := PathForModuleOut(ctx, "preparer.sh")
500 cmd := builder.Command().Tool(preparerPath)
501 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800502 sb.WriteString("set -e\n")
Inseob Kim33f95a92024-07-11 15:44:49 +0900503
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 Parkdda8f692020-11-09 18:38:48 +0900532 }
533 }
534
Jeongik Cha76e677f2023-12-21 16:39:15 +0900535 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
536
Dan Willemsen9fe14102021-07-13 21:52:04 -0700537 return entries
538}
539
540// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900541func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700542 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 Hana8834282022-03-25 11:40:12 +0900547 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700548
Jiyong Parkdda8f692020-11-09 18:38:48 +0900549 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800550 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900551 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 Crossf1a035e2020-11-16 17:32:30 -0800557 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900558 return entries
559}