blob: a7260a641927aee7483e4cc77e5eb54e861f12cc [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"
Jeongik Cha76e677f2023-12-21 16:39:15 +090020 "strings"
Jiyong Parkdda8f692020-11-09 18:38:48 +090021
22 "github.com/google/blueprint"
23)
24
Jiyong Parkcc1157c2020-11-25 11:31:13 +090025// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
26// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
27// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
28// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090029type PackagingSpec struct {
30 // Path relative to the root of the package
31 relPathInPackage string
32
33 // The path to the built artifact
34 srcPath Path
35
36 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
37 // srcPath is of course ignored.)
38 symlinkTarget string
39
40 // Whether relPathInPackage should be marked as executable or not
41 executable bool
Dan Willemsen9fe14102021-07-13 21:52:04 -070042
43 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090044
45 partition string
Jiyong Park4152b192024-04-30 21:24:21 +090046
47 // Whether this packaging spec represents an installation of the srcPath (i.e. this struct
48 // is created via InstallFile or InstallSymlink) or a simple packaging (i.e. created via
49 // PackageFile).
50 skipInstall bool
Justin Yun74f3f302024-05-07 14:32:14 +090051
52 // Paths of aconfig files for the built artifact
53 aconfigPaths *Paths
Jiyong Park073ea552020-11-09 14:08:34 +090054}
Jiyong Parkdda8f692020-11-09 18:38:48 +090055
Jiyong Park16ef7ac2024-05-01 12:36:10 +000056func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
57 if other == nil {
58 return false
59 }
60 if p.relPathInPackage != other.relPathInPackage {
61 return false
62 }
63 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
64 return false
65 }
66 if p.executable != other.executable {
67 return false
68 }
69 if p.partition != other.partition {
70 return false
71 }
72 return true
73}
74
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090075// Get file name of installed package
76func (p *PackagingSpec) FileName() string {
77 if p.relPathInPackage != "" {
78 return filepath.Base(p.relPathInPackage)
79 }
80
81 return ""
82}
83
Jiyong Park6446b622021-02-01 20:08:28 +090084// Path relative to the root of the package
85func (p *PackagingSpec) RelPathInPackage() string {
86 return p.relPathInPackage
87}
88
Dan Willemsen9fe14102021-07-13 21:52:04 -070089func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
90 p.relPathInPackage = relPathInPackage
91}
92
93func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
94 if p.effectiveLicenseFiles == nil {
95 return Paths{}
96 }
97 return *p.effectiveLicenseFiles
98}
99
Jooyung Han99c5fe62022-03-21 15:13:38 +0900100func (p *PackagingSpec) Partition() string {
101 return p.partition
102}
103
Jiyong Park4152b192024-04-30 21:24:21 +0900104func (p *PackagingSpec) SkipInstall() bool {
105 return p.skipInstall
106}
107
Justin Yun74f3f302024-05-07 14:32:14 +0900108// Paths of aconfig files for the built artifact
109func (p *PackagingSpec) GetAconfigPaths() Paths {
110 return *p.aconfigPaths
111}
112
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113type PackageModule interface {
114 Module
115 packagingBase() *PackagingBase
116
117 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900118 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
119 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900120 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900121
Jooyung Hana8834282022-03-25 11:40:12 +0900122 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
123 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900124 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900125
Jiyong Parkdda8f692020-11-09 18:38:48 +0900126 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900127 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900128 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
129 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900130 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900131}
132
133// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
134// include this struct and call InitPackageModule.
135type PackagingBase struct {
136 properties PackagingProperties
137
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900138 // Allows this module to skip missing dependencies. In most cases, this is not required, but
139 // for rare cases like when there's a dependency to a module which exists in certain repo
140 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900141 IgnoreMissingDependencies bool
142}
143
144type depsProperty struct {
145 // Modules to include in this package
146 Deps []string `android:"arch_variant"`
147}
148
149type packagingMultilibProperties struct {
150 First depsProperty `android:"arch_variant"`
151 Common depsProperty `android:"arch_variant"`
152 Lib32 depsProperty `android:"arch_variant"`
153 Lib64 depsProperty `android:"arch_variant"`
154}
155
Jiyong Park2136d152021-02-01 23:24:56 +0900156type packagingArchProperties struct {
157 Arm64 depsProperty
158 Arm depsProperty
159 X86_64 depsProperty
160 X86 depsProperty
161}
162
Jiyong Parkdda8f692020-11-09 18:38:48 +0900163type PackagingProperties struct {
164 Deps []string `android:"arch_variant"`
165 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900166 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900167}
168
Jiyong Parkdda8f692020-11-09 18:38:48 +0900169func InitPackageModule(p PackageModule) {
170 base := p.packagingBase()
171 p.AddProperties(&base.properties)
172}
173
174func (p *PackagingBase) packagingBase() *PackagingBase {
175 return p
176}
177
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900178// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
179// the current archicture when this module is not configured for multi target. When configured for
180// multi target, deps is selected for each of the targets and is NOT selected for the current
181// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900182func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
183 var ret []string
184 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
185 ret = append(ret, p.properties.Deps...)
186 } else if arch.Multilib == "lib32" {
187 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
188 } else if arch.Multilib == "lib64" {
189 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
190 } else if arch == Common {
191 ret = append(ret, p.properties.Multilib.Common.Deps...)
192 }
Jiyong Park2136d152021-02-01 23:24:56 +0900193
Jiyong Parkdda8f692020-11-09 18:38:48 +0900194 for i, t := range ctx.MultiTargets() {
195 if t.Arch.ArchType == arch {
196 ret = append(ret, p.properties.Deps...)
197 if i == 0 {
198 ret = append(ret, p.properties.Multilib.First.Deps...)
199 }
200 }
201 }
Jiyong Park2136d152021-02-01 23:24:56 +0900202
203 if ctx.Arch().ArchType == Common {
204 switch arch {
205 case Arm64:
206 ret = append(ret, p.properties.Arch.Arm64.Deps...)
207 case Arm:
208 ret = append(ret, p.properties.Arch.Arm.Deps...)
209 case X86_64:
210 ret = append(ret, p.properties.Arch.X86_64.Deps...)
211 case X86:
212 ret = append(ret, p.properties.Arch.X86.Deps...)
213 }
214 }
215
Jiyong Parkdda8f692020-11-09 18:38:48 +0900216 return FirstUniqueStrings(ret)
217}
218
219func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
220 var ret []Target
221 // The current and the common OS targets are always supported
222 ret = append(ret, ctx.Target())
223 if ctx.Arch().ArchType != Common {
224 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
225 }
226 // If this module is configured for multi targets, those should be supported as well
227 ret = append(ret, ctx.MultiTargets()...)
228 return ret
229}
230
Jooyung Han092ef812021-03-10 15:40:34 +0900231// PackagingItem is a marker interface for dependency tags.
232// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
233type PackagingItem interface {
234 // IsPackagingItem returns true if the dep is to be packaged
235 IsPackagingItem() bool
236}
237
238// DepTag provides default implementation of PackagingItem interface.
239// PackagingBase-derived modules can define their own dependency tag by embedding this, which
240// can be passed to AddDeps() or AddDependencies().
241type PackagingItemAlwaysDepTag struct {
242}
243
244// IsPackagingItem returns true if the dep is to be packaged
245func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
246 return true
247}
248
Jiyong Parkdda8f692020-11-09 18:38:48 +0900249// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900250func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900251 for _, t := range p.getSupportedTargets(ctx) {
252 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
253 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
254 continue
255 }
256 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
257 }
258 }
259}
260
Jeongik Cha54bf8752024-02-08 10:44:37 +0900261func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900262 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900263 ctx.VisitDirectDeps(func(child Module) {
264 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
265 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900266 }
Jooyung Han092ef812021-03-10 15:40:34 +0900267 for _, ps := range child.TransitivePackagingSpecs() {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900268 if filter != nil {
269 if !filter(ps) {
270 continue
271 }
272 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000273 dstPath := ps.relPathInPackage
274 if existingPs, ok := m[dstPath]; ok {
275 if !existingPs.Equals(&ps) {
276 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
277 }
278 continue
Jiyong Parkdda8f692020-11-09 18:38:48 +0900279 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000280
281 m[dstPath] = ps
Jiyong Parkdda8f692020-11-09 18:38:48 +0900282 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900283 })
Jooyung Handf09d172021-05-11 11:13:30 +0900284 return m
285}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900286
Jeongik Cha54bf8752024-02-08 10:44:37 +0900287// See PackageModule.GatherPackagingSpecs
288func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
289 return p.GatherPackagingSpecsWithFilter(ctx, nil)
290}
291
Dan Willemsen9fe14102021-07-13 21:52:04 -0700292// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
293// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700294func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Cole Faust3b3a0112024-01-03 15:16:55 -0800295 if len(specs) == 0 {
296 return entries
297 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900298 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900299 preparerPath := PathForModuleOut(ctx, "preparer.sh")
300 cmd := builder.Command().Tool(preparerPath)
301 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800302 sb.WriteString("set -e\n")
Cole Faust18994c72023-02-28 16:02:16 -0800303 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900304 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700305 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900306 destDir := filepath.Dir(destPath)
307 entries = append(entries, ps.relPathInPackage)
308 if _, ok := seenDir[destDir]; !ok {
309 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900310 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900311 }
312 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900313 cmd.Implicit(ps.srcPath)
314 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900315 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900316 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900317 }
318 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900319 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900320 }
321 }
322
Jeongik Cha76e677f2023-12-21 16:39:15 +0900323 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
324
Dan Willemsen9fe14102021-07-13 21:52:04 -0700325 return entries
326}
327
328// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900329func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700330 builder := NewRuleBuilder(pctx, ctx)
331
332 dir := PathForModuleOut(ctx, ".zip")
333 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
334 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900335 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700336
Jiyong Parkdda8f692020-11-09 18:38:48 +0900337 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800338 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900339 FlagWithOutput("-o ", zipOut).
340 FlagWithArg("-C ", dir.String()).
341 Flag("-L 0"). // no compression because this will be unzipped soon
342 FlagWithArg("-D ", dir.String())
343 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
344
Colin Crossf1a035e2020-11-16 17:32:30 -0800345 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900346 return entries
347}