blob: 6bc1c7424b34f258bbf81b72c3a80ba2f0628684 [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 (
Yu Liu26a716d2024-08-30 23:40:32 +000018 "bytes"
19 "encoding/gob"
20 "errors"
Jiyong Parkdda8f692020-11-09 18:38:48 +090021 "fmt"
22 "path/filepath"
Inseob Kim33f95a92024-07-11 15:44:49 +090023 "sort"
Jeongik Cha76e677f2023-12-21 16:39:15 +090024 "strings"
Jiyong Parkdda8f692020-11-09 18:38:48 +090025
26 "github.com/google/blueprint"
Jiyong Park105e11c2024-05-17 14:58:24 +090027 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090028)
29
Jiyong Parkcc1157c2020-11-25 11:31:13 +090030// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
31// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
32// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
33// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090034type PackagingSpec struct {
35 // Path relative to the root of the package
36 relPathInPackage string
37
38 // The path to the built artifact
39 srcPath Path
40
41 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
42 // srcPath is of course ignored.)
43 symlinkTarget string
44
45 // Whether relPathInPackage should be marked as executable or not
46 executable bool
Dan Willemsen9fe14102021-07-13 21:52:04 -070047
48 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090049
50 partition string
Jiyong Park4152b192024-04-30 21:24:21 +090051
52 // Whether this packaging spec represents an installation of the srcPath (i.e. this struct
53 // is created via InstallFile or InstallSymlink) or a simple packaging (i.e. created via
54 // PackageFile).
55 skipInstall bool
Justin Yun74f3f302024-05-07 14:32:14 +090056
57 // Paths of aconfig files for the built artifact
58 aconfigPaths *Paths
Jiyong Parkc6a773d2024-05-14 21:49:11 +090059
60 // ArchType of the module which produced this packaging spec
61 archType ArchType
Jiyong Park073ea552020-11-09 14:08:34 +090062}
Jiyong Parkdda8f692020-11-09 18:38:48 +090063
Yu Liu26a716d2024-08-30 23:40:32 +000064func (p *PackagingSpec) GobEncode() ([]byte, error) {
65 w := new(bytes.Buffer)
66 encoder := gob.NewEncoder(w)
67 err := errors.Join(encoder.Encode(p.relPathInPackage), encoder.Encode(p.srcPath),
68 encoder.Encode(p.symlinkTarget), encoder.Encode(p.executable),
69 encoder.Encode(p.effectiveLicenseFiles), encoder.Encode(p.partition),
70 encoder.Encode(p.skipInstall), encoder.Encode(p.aconfigPaths),
71 encoder.Encode(p.archType))
72 if err != nil {
73 return nil, err
74 }
75
76 return w.Bytes(), nil
77}
78
79func (p *PackagingSpec) GobDecode(data []byte) error {
80 r := bytes.NewBuffer(data)
81 decoder := gob.NewDecoder(r)
82 err := errors.Join(decoder.Decode(&p.relPathInPackage), decoder.Decode(&p.srcPath),
83 decoder.Decode(&p.symlinkTarget), decoder.Decode(&p.executable),
84 decoder.Decode(&p.effectiveLicenseFiles), decoder.Decode(&p.partition),
85 decoder.Decode(&p.skipInstall), decoder.Decode(&p.aconfigPaths),
86 decoder.Decode(&p.archType))
87 if err != nil {
88 return err
89 }
90
91 return nil
92}
93
Jiyong Park16ef7ac2024-05-01 12:36:10 +000094func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
95 if other == nil {
96 return false
97 }
98 if p.relPathInPackage != other.relPathInPackage {
99 return false
100 }
101 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
102 return false
103 }
104 if p.executable != other.executable {
105 return false
106 }
107 if p.partition != other.partition {
108 return false
109 }
110 return true
111}
112
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +0900113// Get file name of installed package
114func (p *PackagingSpec) FileName() string {
115 if p.relPathInPackage != "" {
116 return filepath.Base(p.relPathInPackage)
117 }
118
119 return ""
120}
121
Jiyong Park6446b622021-02-01 20:08:28 +0900122// Path relative to the root of the package
123func (p *PackagingSpec) RelPathInPackage() string {
124 return p.relPathInPackage
125}
126
Dan Willemsen9fe14102021-07-13 21:52:04 -0700127func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
128 p.relPathInPackage = relPathInPackage
129}
130
131func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
132 if p.effectiveLicenseFiles == nil {
133 return Paths{}
134 }
135 return *p.effectiveLicenseFiles
136}
137
Jooyung Han99c5fe62022-03-21 15:13:38 +0900138func (p *PackagingSpec) Partition() string {
139 return p.partition
140}
141
Jiyong Park4152b192024-04-30 21:24:21 +0900142func (p *PackagingSpec) SkipInstall() bool {
143 return p.skipInstall
144}
145
Justin Yun74f3f302024-05-07 14:32:14 +0900146// Paths of aconfig files for the built artifact
147func (p *PackagingSpec) GetAconfigPaths() Paths {
148 return *p.aconfigPaths
149}
150
Jiyong Parkdda8f692020-11-09 18:38:48 +0900151type PackageModule interface {
152 Module
153 packagingBase() *PackagingBase
154
155 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900156 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
157 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900158 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900159
Jooyung Hana8834282022-03-25 11:40:12 +0900160 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
161 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900162 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900163
Jiyong Parkdda8f692020-11-09 18:38:48 +0900164 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900165 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900166 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
167 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900168 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900169}
170
171// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
172// include this struct and call InitPackageModule.
173type PackagingBase struct {
174 properties PackagingProperties
175
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900176 // Allows this module to skip missing dependencies. In most cases, this is not required, but
177 // for rare cases like when there's a dependency to a module which exists in certain repo
178 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900179 IgnoreMissingDependencies bool
Jiyong Park3ea9b652024-05-15 23:01:54 +0900180
181 // If this is set to true by a module type inheriting PackagingBase, the deps property
182 // collects the first target only even with compile_multilib: true.
183 DepsCollectFirstTargetOnly bool
Jiyong Parkdda8f692020-11-09 18:38:48 +0900184}
185
186type depsProperty struct {
187 // Modules to include in this package
Jiyong Park105e11c2024-05-17 14:58:24 +0900188 Deps proptools.Configurable[[]string] `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900189}
190
191type packagingMultilibProperties struct {
Jiyong Parke6043782024-05-20 16:17:39 +0900192 First depsProperty `android:"arch_variant"`
193 Common depsProperty `android:"arch_variant"`
194 Lib32 depsProperty `android:"arch_variant"`
195 Lib64 depsProperty `android:"arch_variant"`
196 Both depsProperty `android:"arch_variant"`
197 Prefer32 depsProperty `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900198}
199
Jiyong Park2136d152021-02-01 23:24:56 +0900200type packagingArchProperties struct {
201 Arm64 depsProperty
202 Arm depsProperty
203 X86_64 depsProperty
204 X86 depsProperty
205}
206
Jiyong Parkdda8f692020-11-09 18:38:48 +0900207type PackagingProperties struct {
Jiyong Park105e11c2024-05-17 14:58:24 +0900208 Deps proptools.Configurable[[]string] `android:"arch_variant"`
209 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900210 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900211}
212
Jiyong Parkdda8f692020-11-09 18:38:48 +0900213func InitPackageModule(p PackageModule) {
214 base := p.packagingBase()
215 p.AddProperties(&base.properties)
216}
217
218func (p *PackagingBase) packagingBase() *PackagingBase {
219 return p
220}
221
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900222// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
223// the current archicture when this module is not configured for multi target. When configured for
224// multi target, deps is selected for each of the targets and is NOT selected for the current
225// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900226func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
Jiyong Park105e11c2024-05-17 14:58:24 +0900227 get := func(prop proptools.Configurable[[]string]) []string {
228 return prop.GetOrDefault(ctx, nil)
229 }
230
Jiyong Parkdda8f692020-11-09 18:38:48 +0900231 var ret []string
232 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900233 ret = append(ret, get(p.properties.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900234 } else if arch.Multilib == "lib32" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900235 ret = append(ret, get(p.properties.Multilib.Lib32.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900236 // multilib.prefer32.deps are added for lib32 only when they support 32-bit arch
237 for _, dep := range get(p.properties.Multilib.Prefer32.Deps) {
238 if checkIfOtherModuleSupportsLib32(ctx, dep) {
239 ret = append(ret, dep)
240 }
241 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900242 } else if arch.Multilib == "lib64" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900243 ret = append(ret, get(p.properties.Multilib.Lib64.Deps)...)
Jiyong Parke6043782024-05-20 16:17:39 +0900244 // multilib.prefer32.deps are added for lib64 only when they don't support 32-bit arch
245 for _, dep := range get(p.properties.Multilib.Prefer32.Deps) {
246 if !checkIfOtherModuleSupportsLib32(ctx, dep) {
247 ret = append(ret, dep)
248 }
249 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900250 } else if arch == Common {
Jiyong Park105e11c2024-05-17 14:58:24 +0900251 ret = append(ret, get(p.properties.Multilib.Common.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900252 }
Jiyong Park2136d152021-02-01 23:24:56 +0900253
Jiyong Park3ea9b652024-05-15 23:01:54 +0900254 if p.DepsCollectFirstTargetOnly {
Jiyong Park105e11c2024-05-17 14:58:24 +0900255 if len(get(p.properties.Multilib.First.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900256 ctx.PropertyErrorf("multilib.first.deps", "not supported. use \"deps\" instead")
257 }
258 for i, t := range ctx.MultiTargets() {
259 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900260 ret = append(ret, get(p.properties.Multilib.Both.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900261 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900262 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900263 }
264 }
265 }
266 } else {
Jiyong Park105e11c2024-05-17 14:58:24 +0900267 if len(get(p.properties.Multilib.Both.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900268 ctx.PropertyErrorf("multilib.both.deps", "not supported. use \"deps\" instead")
269 }
270 for i, t := range ctx.MultiTargets() {
271 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900272 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900273 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900274 ret = append(ret, get(p.properties.Multilib.First.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900275 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900276 }
277 }
278 }
Jiyong Park2136d152021-02-01 23:24:56 +0900279
280 if ctx.Arch().ArchType == Common {
281 switch arch {
282 case Arm64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900283 ret = append(ret, get(p.properties.Arch.Arm64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900284 case Arm:
Jiyong Park105e11c2024-05-17 14:58:24 +0900285 ret = append(ret, get(p.properties.Arch.Arm.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900286 case X86_64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900287 ret = append(ret, get(p.properties.Arch.X86_64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900288 case X86:
Jiyong Park105e11c2024-05-17 14:58:24 +0900289 ret = append(ret, get(p.properties.Arch.X86.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900290 }
291 }
292
Jiyong Parkdda8f692020-11-09 18:38:48 +0900293 return FirstUniqueStrings(ret)
294}
295
Jiyong Parke6043782024-05-20 16:17:39 +0900296func getSupportedTargets(ctx BaseModuleContext) []Target {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900297 var ret []Target
298 // The current and the common OS targets are always supported
299 ret = append(ret, ctx.Target())
300 if ctx.Arch().ArchType != Common {
301 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
302 }
303 // If this module is configured for multi targets, those should be supported as well
304 ret = append(ret, ctx.MultiTargets()...)
305 return ret
306}
307
Jiyong Parke6043782024-05-20 16:17:39 +0900308// getLib32Target returns the 32-bit target from the list of targets this module supports. If this
309// module doesn't support 32-bit target, nil is returned.
310func getLib32Target(ctx BaseModuleContext) *Target {
311 for _, t := range getSupportedTargets(ctx) {
312 if t.Arch.ArchType.Multilib == "lib32" {
313 return &t
314 }
315 }
316 return nil
317}
318
319// checkIfOtherModuleSUpportsLib32 returns true if 32-bit variant of dep exists.
320func checkIfOtherModuleSupportsLib32(ctx BaseModuleContext, dep string) bool {
321 t := getLib32Target(ctx)
322 if t == nil {
323 // This packaging module doesn't support 32bit. No point of checking if dep supports 32-bit
324 // or not.
325 return false
326 }
327 return ctx.OtherModuleFarDependencyVariantExists(t.Variations(), dep)
328}
329
Jooyung Han092ef812021-03-10 15:40:34 +0900330// PackagingItem is a marker interface for dependency tags.
331// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
332type PackagingItem interface {
333 // IsPackagingItem returns true if the dep is to be packaged
334 IsPackagingItem() bool
335}
336
337// DepTag provides default implementation of PackagingItem interface.
338// PackagingBase-derived modules can define their own dependency tag by embedding this, which
339// can be passed to AddDeps() or AddDependencies().
340type PackagingItemAlwaysDepTag struct {
341}
342
343// IsPackagingItem returns true if the dep is to be packaged
344func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
345 return true
346}
347
Jiyong Parkdda8f692020-11-09 18:38:48 +0900348// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900349func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parke6043782024-05-20 16:17:39 +0900350 for _, t := range getSupportedTargets(ctx) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900351 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
352 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
353 continue
354 }
355 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
356 }
357 }
358}
359
Jeongik Cha54bf8752024-02-08 10:44:37 +0900360func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900361 m := make(map[string]PackagingSpec)
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900362
363 var arches []ArchType
Jiyong Parke6043782024-05-20 16:17:39 +0900364 for _, target := range getSupportedTargets(ctx) {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900365 arches = append(arches, target.Arch.ArchType)
366 }
367
368 // filter out packaging specs for unsupported architecture
369 filterArch := func(ps PackagingSpec) bool {
370 for _, arch := range arches {
371 if arch == ps.archType {
372 return true
373 }
374 }
375 return false
376 }
377
Jooyung Han092ef812021-03-10 15:40:34 +0900378 ctx.VisitDirectDeps(func(child Module) {
379 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
380 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900381 }
Yu Liubad1eef2024-08-21 22:37:35 +0000382 for _, ps := range OtherModuleProviderOrDefault(
383 ctx, child, InstallFilesProvider).TransitivePackagingSpecs.ToList() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900384 if !filterArch(ps) {
385 continue
386 }
387
Jeongik Cha54bf8752024-02-08 10:44:37 +0900388 if filter != nil {
389 if !filter(ps) {
390 continue
391 }
392 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000393 dstPath := ps.relPathInPackage
394 if existingPs, ok := m[dstPath]; ok {
395 if !existingPs.Equals(&ps) {
396 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
397 }
398 continue
Jiyong Parkdda8f692020-11-09 18:38:48 +0900399 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000400
401 m[dstPath] = ps
Jiyong Parkdda8f692020-11-09 18:38:48 +0900402 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900403 })
Jooyung Handf09d172021-05-11 11:13:30 +0900404 return m
405}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900406
Jeongik Cha54bf8752024-02-08 10:44:37 +0900407// See PackageModule.GatherPackagingSpecs
408func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
409 return p.GatherPackagingSpecsWithFilter(ctx, nil)
410}
411
Dan Willemsen9fe14102021-07-13 21:52:04 -0700412// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
413// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700414func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Inseob Kim33f95a92024-07-11 15:44:49 +0900415 dirsToSpecs := make(map[WritablePath]map[string]PackagingSpec)
416 dirsToSpecs[dir] = specs
417 return p.CopySpecsToDirs(ctx, builder, dirsToSpecs)
418}
419
420// CopySpecsToDirs is a helper that will add commands to the rule builder to copy the PackagingSpec
421// entries into corresponding directories.
422func (p *PackagingBase) CopySpecsToDirs(ctx ModuleContext, builder *RuleBuilder, dirsToSpecs map[WritablePath]map[string]PackagingSpec) (entries []string) {
423 empty := true
424 for _, specs := range dirsToSpecs {
425 if len(specs) > 0 {
426 empty = false
427 break
428 }
429 }
430 if empty {
Cole Faust3b3a0112024-01-03 15:16:55 -0800431 return entries
432 }
Inseob Kim33f95a92024-07-11 15:44:49 +0900433
Jiyong Parkdda8f692020-11-09 18:38:48 +0900434 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900435 preparerPath := PathForModuleOut(ctx, "preparer.sh")
436 cmd := builder.Command().Tool(preparerPath)
437 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800438 sb.WriteString("set -e\n")
Inseob Kim33f95a92024-07-11 15:44:49 +0900439
440 dirs := make([]WritablePath, 0, len(dirsToSpecs))
441 for dir, _ := range dirsToSpecs {
442 dirs = append(dirs, dir)
443 }
444 sort.Slice(dirs, func(i, j int) bool {
445 return dirs[i].String() < dirs[j].String()
446 })
447
448 for _, dir := range dirs {
449 specs := dirsToSpecs[dir]
450 for _, k := range SortedKeys(specs) {
451 ps := specs[k]
452 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
453 destDir := filepath.Dir(destPath)
454 entries = append(entries, ps.relPathInPackage)
455 if _, ok := seenDir[destDir]; !ok {
456 seenDir[destDir] = true
457 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
458 }
459 if ps.symlinkTarget == "" {
460 cmd.Implicit(ps.srcPath)
461 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
462 } else {
463 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
464 }
465 if ps.executable {
466 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
467 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900468 }
469 }
470
Jeongik Cha76e677f2023-12-21 16:39:15 +0900471 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
472
Dan Willemsen9fe14102021-07-13 21:52:04 -0700473 return entries
474}
475
476// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900477func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700478 builder := NewRuleBuilder(pctx, ctx)
479
480 dir := PathForModuleOut(ctx, ".zip")
481 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
482 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900483 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700484
Jiyong Parkdda8f692020-11-09 18:38:48 +0900485 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800486 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900487 FlagWithOutput("-o ", zipOut).
488 FlagWithArg("-C ", dir.String()).
489 Flag("-L 0"). // no compression because this will be unzipped soon
490 FlagWithArg("-D ", dir.String())
491 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
492
Colin Crossf1a035e2020-11-16 17:32:30 -0800493 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900494 return entries
495}