blob: ecd84a2f06ed6cf5e8c3674c8131ec433377cced [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"
20
21 "github.com/google/blueprint"
22)
23
Jiyong Parkcc1157c2020-11-25 11:31:13 +090024// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
25// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
26// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
27// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090028type PackagingSpec struct {
29 // Path relative to the root of the package
30 relPathInPackage string
31
32 // The path to the built artifact
33 srcPath Path
34
35 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
36 // srcPath is of course ignored.)
37 symlinkTarget string
38
39 // Whether relPathInPackage should be marked as executable or not
40 executable bool
Dan Willemsen9fe14102021-07-13 21:52:04 -070041
42 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090043
44 partition string
Jiyong Park073ea552020-11-09 14:08:34 +090045}
Jiyong Parkdda8f692020-11-09 18:38:48 +090046
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090047// Get file name of installed package
48func (p *PackagingSpec) FileName() string {
49 if p.relPathInPackage != "" {
50 return filepath.Base(p.relPathInPackage)
51 }
52
53 return ""
54}
55
Jiyong Park6446b622021-02-01 20:08:28 +090056// Path relative to the root of the package
57func (p *PackagingSpec) RelPathInPackage() string {
58 return p.relPathInPackage
59}
60
Dan Willemsen9fe14102021-07-13 21:52:04 -070061func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
62 p.relPathInPackage = relPathInPackage
63}
64
65func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
66 if p.effectiveLicenseFiles == nil {
67 return Paths{}
68 }
69 return *p.effectiveLicenseFiles
70}
71
Jooyung Han99c5fe62022-03-21 15:13:38 +090072func (p *PackagingSpec) Partition() string {
73 return p.partition
74}
75
Jiyong Parkdda8f692020-11-09 18:38:48 +090076type PackageModule interface {
77 Module
78 packagingBase() *PackagingBase
79
80 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +090081 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
82 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +090083 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090084
Jooyung Hana8834282022-03-25 11:40:12 +090085 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
86 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
87
Jiyong Parkdda8f692020-11-09 18:38:48 +090088 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +090089 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +090090 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
91 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +090092 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +090093}
94
95// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
96// include this struct and call InitPackageModule.
97type PackagingBase struct {
98 properties PackagingProperties
99
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900100 // Allows this module to skip missing dependencies. In most cases, this is not required, but
101 // for rare cases like when there's a dependency to a module which exists in certain repo
102 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900103 IgnoreMissingDependencies bool
104}
105
106type depsProperty struct {
107 // Modules to include in this package
108 Deps []string `android:"arch_variant"`
109}
110
111type packagingMultilibProperties struct {
112 First depsProperty `android:"arch_variant"`
113 Common depsProperty `android:"arch_variant"`
114 Lib32 depsProperty `android:"arch_variant"`
115 Lib64 depsProperty `android:"arch_variant"`
116}
117
Jiyong Park2136d152021-02-01 23:24:56 +0900118type packagingArchProperties struct {
119 Arm64 depsProperty
120 Arm depsProperty
121 X86_64 depsProperty
122 X86 depsProperty
123}
124
Jiyong Parkdda8f692020-11-09 18:38:48 +0900125type PackagingProperties struct {
126 Deps []string `android:"arch_variant"`
127 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900128 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900129}
130
Jiyong Parkdda8f692020-11-09 18:38:48 +0900131func InitPackageModule(p PackageModule) {
132 base := p.packagingBase()
133 p.AddProperties(&base.properties)
134}
135
136func (p *PackagingBase) packagingBase() *PackagingBase {
137 return p
138}
139
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900140// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
141// the current archicture when this module is not configured for multi target. When configured for
142// multi target, deps is selected for each of the targets and is NOT selected for the current
143// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900144func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
145 var ret []string
146 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
147 ret = append(ret, p.properties.Deps...)
148 } else if arch.Multilib == "lib32" {
149 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
150 } else if arch.Multilib == "lib64" {
151 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
152 } else if arch == Common {
153 ret = append(ret, p.properties.Multilib.Common.Deps...)
154 }
Jiyong Park2136d152021-02-01 23:24:56 +0900155
Jiyong Parkdda8f692020-11-09 18:38:48 +0900156 for i, t := range ctx.MultiTargets() {
157 if t.Arch.ArchType == arch {
158 ret = append(ret, p.properties.Deps...)
159 if i == 0 {
160 ret = append(ret, p.properties.Multilib.First.Deps...)
161 }
162 }
163 }
Jiyong Park2136d152021-02-01 23:24:56 +0900164
165 if ctx.Arch().ArchType == Common {
166 switch arch {
167 case Arm64:
168 ret = append(ret, p.properties.Arch.Arm64.Deps...)
169 case Arm:
170 ret = append(ret, p.properties.Arch.Arm.Deps...)
171 case X86_64:
172 ret = append(ret, p.properties.Arch.X86_64.Deps...)
173 case X86:
174 ret = append(ret, p.properties.Arch.X86.Deps...)
175 }
176 }
177
Jiyong Parkdda8f692020-11-09 18:38:48 +0900178 return FirstUniqueStrings(ret)
179}
180
181func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
182 var ret []Target
183 // The current and the common OS targets are always supported
184 ret = append(ret, ctx.Target())
185 if ctx.Arch().ArchType != Common {
186 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
187 }
188 // If this module is configured for multi targets, those should be supported as well
189 ret = append(ret, ctx.MultiTargets()...)
190 return ret
191}
192
Jooyung Han092ef812021-03-10 15:40:34 +0900193// PackagingItem is a marker interface for dependency tags.
194// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
195type PackagingItem interface {
196 // IsPackagingItem returns true if the dep is to be packaged
197 IsPackagingItem() bool
198}
199
200// DepTag provides default implementation of PackagingItem interface.
201// PackagingBase-derived modules can define their own dependency tag by embedding this, which
202// can be passed to AddDeps() or AddDependencies().
203type PackagingItemAlwaysDepTag struct {
204}
205
206// IsPackagingItem returns true if the dep is to be packaged
207func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
208 return true
209}
210
Jiyong Parkdda8f692020-11-09 18:38:48 +0900211// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900212func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900213 for _, t := range p.getSupportedTargets(ctx) {
214 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
215 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
216 continue
217 }
218 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
219 }
220 }
221}
222
Jooyung Hana8834282022-03-25 11:40:12 +0900223// See PackageModule.GatherPackagingSpecs
Jooyung Handf09d172021-05-11 11:13:30 +0900224func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900225 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900226 ctx.VisitDirectDeps(func(child Module) {
227 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
228 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900229 }
Jooyung Han092ef812021-03-10 15:40:34 +0900230 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900231 if _, ok := m[ps.relPathInPackage]; !ok {
232 m[ps.relPathInPackage] = ps
233 }
234 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900235 })
Jooyung Handf09d172021-05-11 11:13:30 +0900236 return m
237}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900238
Dan Willemsen9fe14102021-07-13 21:52:04 -0700239// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
240// entries into the specified directory.
Jooyung Hana8834282022-03-25 11:40:12 +0900241func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir ModuleOutPath) (entries []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900242 seenDir := make(map[string]bool)
Jooyung Hana8834282022-03-25 11:40:12 +0900243 for _, k := range SortedStringKeys(specs) {
244 ps := specs[k]
Jiyong Parkdda8f692020-11-09 18:38:48 +0900245 destPath := dir.Join(ctx, ps.relPathInPackage).String()
246 destDir := filepath.Dir(destPath)
247 entries = append(entries, ps.relPathInPackage)
248 if _, ok := seenDir[destDir]; !ok {
249 seenDir[destDir] = true
250 builder.Command().Text("mkdir").Flag("-p").Text(destDir)
251 }
252 if ps.symlinkTarget == "" {
253 builder.Command().Text("cp").Input(ps.srcPath).Text(destPath)
254 } else {
255 builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath)
256 }
257 if ps.executable {
258 builder.Command().Text("chmod").Flag("a+x").Text(destPath)
259 }
260 }
261
Dan Willemsen9fe14102021-07-13 21:52:04 -0700262 return entries
263}
264
265// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900266func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700267 builder := NewRuleBuilder(pctx, ctx)
268
269 dir := PathForModuleOut(ctx, ".zip")
270 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
271 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900272 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700273
Jiyong Parkdda8f692020-11-09 18:38:48 +0900274 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800275 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900276 FlagWithOutput("-o ", zipOut).
277 FlagWithArg("-C ", dir.String()).
278 Flag("-L 0"). // no compression because this will be unzipped soon
279 FlagWithArg("-D ", dir.String())
280 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
281
Colin Crossf1a035e2020-11-16 17:32:30 -0800282 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900283 return entries
284}
Colin Crossffe6b9d2020-12-01 15:40:06 -0800285
286// packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
287// topological order.
288type packagingSpecsDepSet struct {
289 depSet
290}
291
292// newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and
293// transitive contents.
294func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet {
295 return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
296}
297
298// ToList returns the packagingSpecsDepSet flattened to a list in topological order.
299func (d *packagingSpecsDepSet) ToList() []PackagingSpec {
300 if d == nil {
301 return nil
302 }
303 return d.depSet.ToList().([]PackagingSpec)
304}