blob: e3a0b54be423d7a54159beac0beed5d88580d87d [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
Jiyong Park073ea552020-11-09 14:08:34 +090043}
Jiyong Parkdda8f692020-11-09 18:38:48 +090044
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090045// Get file name of installed package
46func (p *PackagingSpec) FileName() string {
47 if p.relPathInPackage != "" {
48 return filepath.Base(p.relPathInPackage)
49 }
50
51 return ""
52}
53
Jiyong Park6446b622021-02-01 20:08:28 +090054// Path relative to the root of the package
55func (p *PackagingSpec) RelPathInPackage() string {
56 return p.relPathInPackage
57}
58
Dan Willemsen9fe14102021-07-13 21:52:04 -070059func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
60 p.relPathInPackage = relPathInPackage
61}
62
63func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
64 if p.effectiveLicenseFiles == nil {
65 return Paths{}
66 }
67 return *p.effectiveLicenseFiles
68}
69
Jiyong Parkdda8f692020-11-09 18:38:48 +090070type PackageModule interface {
71 Module
72 packagingBase() *PackagingBase
73
74 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +090075 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
76 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +090077 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090078
79 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +090080 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +090081 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
82 // etc.) from the extracted files
Paul Duffin4076a752021-02-02 10:59:54 +000083 CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +090084}
85
86// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
87// include this struct and call InitPackageModule.
88type PackagingBase struct {
89 properties PackagingProperties
90
Jiyong Parkcc1157c2020-11-25 11:31:13 +090091 // Allows this module to skip missing dependencies. In most cases, this is not required, but
92 // for rare cases like when there's a dependency to a module which exists in certain repo
93 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +090094 IgnoreMissingDependencies bool
95}
96
97type depsProperty struct {
98 // Modules to include in this package
99 Deps []string `android:"arch_variant"`
100}
101
102type packagingMultilibProperties struct {
103 First depsProperty `android:"arch_variant"`
104 Common depsProperty `android:"arch_variant"`
105 Lib32 depsProperty `android:"arch_variant"`
106 Lib64 depsProperty `android:"arch_variant"`
107}
108
Jiyong Park2136d152021-02-01 23:24:56 +0900109type packagingArchProperties struct {
110 Arm64 depsProperty
111 Arm depsProperty
112 X86_64 depsProperty
113 X86 depsProperty
114}
115
Jiyong Parkdda8f692020-11-09 18:38:48 +0900116type PackagingProperties struct {
117 Deps []string `android:"arch_variant"`
118 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900119 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900120}
121
Jiyong Parkdda8f692020-11-09 18:38:48 +0900122func InitPackageModule(p PackageModule) {
123 base := p.packagingBase()
124 p.AddProperties(&base.properties)
125}
126
127func (p *PackagingBase) packagingBase() *PackagingBase {
128 return p
129}
130
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900131// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
132// the current archicture when this module is not configured for multi target. When configured for
133// multi target, deps is selected for each of the targets and is NOT selected for the current
134// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900135func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
136 var ret []string
137 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
138 ret = append(ret, p.properties.Deps...)
139 } else if arch.Multilib == "lib32" {
140 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
141 } else if arch.Multilib == "lib64" {
142 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
143 } else if arch == Common {
144 ret = append(ret, p.properties.Multilib.Common.Deps...)
145 }
Jiyong Park2136d152021-02-01 23:24:56 +0900146
Jiyong Parkdda8f692020-11-09 18:38:48 +0900147 for i, t := range ctx.MultiTargets() {
148 if t.Arch.ArchType == arch {
149 ret = append(ret, p.properties.Deps...)
150 if i == 0 {
151 ret = append(ret, p.properties.Multilib.First.Deps...)
152 }
153 }
154 }
Jiyong Park2136d152021-02-01 23:24:56 +0900155
156 if ctx.Arch().ArchType == Common {
157 switch arch {
158 case Arm64:
159 ret = append(ret, p.properties.Arch.Arm64.Deps...)
160 case Arm:
161 ret = append(ret, p.properties.Arch.Arm.Deps...)
162 case X86_64:
163 ret = append(ret, p.properties.Arch.X86_64.Deps...)
164 case X86:
165 ret = append(ret, p.properties.Arch.X86.Deps...)
166 }
167 }
168
Jiyong Parkdda8f692020-11-09 18:38:48 +0900169 return FirstUniqueStrings(ret)
170}
171
172func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
173 var ret []Target
174 // The current and the common OS targets are always supported
175 ret = append(ret, ctx.Target())
176 if ctx.Arch().ArchType != Common {
177 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
178 }
179 // If this module is configured for multi targets, those should be supported as well
180 ret = append(ret, ctx.MultiTargets()...)
181 return ret
182}
183
Jooyung Han092ef812021-03-10 15:40:34 +0900184// PackagingItem is a marker interface for dependency tags.
185// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
186type PackagingItem interface {
187 // IsPackagingItem returns true if the dep is to be packaged
188 IsPackagingItem() bool
189}
190
191// DepTag provides default implementation of PackagingItem interface.
192// PackagingBase-derived modules can define their own dependency tag by embedding this, which
193// can be passed to AddDeps() or AddDependencies().
194type PackagingItemAlwaysDepTag struct {
195}
196
197// IsPackagingItem returns true if the dep is to be packaged
198func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
199 return true
200}
201
Jiyong Parkdda8f692020-11-09 18:38:48 +0900202// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900203func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900204 for _, t := range p.getSupportedTargets(ctx) {
205 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
206 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
207 continue
208 }
209 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
210 }
211 }
212}
213
Jooyung Handf09d172021-05-11 11:13:30 +0900214// Returns transitive PackagingSpecs from deps
215func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900216 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900217 ctx.VisitDirectDeps(func(child Module) {
218 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
219 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900220 }
Jooyung Han092ef812021-03-10 15:40:34 +0900221 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900222 if _, ok := m[ps.relPathInPackage]; !ok {
223 m[ps.relPathInPackage] = ps
224 }
225 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900226 })
Jooyung Handf09d172021-05-11 11:13:30 +0900227 return m
228}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900229
Dan Willemsen9fe14102021-07-13 21:52:04 -0700230// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
231// entries into the specified directory.
232func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, m map[string]PackagingSpec, dir ModuleOutPath) (entries []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900233 seenDir := make(map[string]bool)
234 for _, k := range SortedStringKeys(m) {
235 ps := m[k]
236 destPath := dir.Join(ctx, ps.relPathInPackage).String()
237 destDir := filepath.Dir(destPath)
238 entries = append(entries, ps.relPathInPackage)
239 if _, ok := seenDir[destDir]; !ok {
240 seenDir[destDir] = true
241 builder.Command().Text("mkdir").Flag("-p").Text(destDir)
242 }
243 if ps.symlinkTarget == "" {
244 builder.Command().Text("cp").Input(ps.srcPath).Text(destPath)
245 } else {
246 builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath)
247 }
248 if ps.executable {
249 builder.Command().Text("chmod").Flag("a+x").Text(destPath)
250 }
251 }
252
Dan Willemsen9fe14102021-07-13 21:52:04 -0700253 return entries
254}
255
256// See PackageModule.CopyDepsToZip
257func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) (entries []string) {
258 m := p.GatherPackagingSpecs(ctx)
259 builder := NewRuleBuilder(pctx, ctx)
260
261 dir := PathForModuleOut(ctx, ".zip")
262 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
263 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
264 entries = p.CopySpecsToDir(ctx, builder, m, dir)
265
Jiyong Parkdda8f692020-11-09 18:38:48 +0900266 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800267 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900268 FlagWithOutput("-o ", zipOut).
269 FlagWithArg("-C ", dir.String()).
270 Flag("-L 0"). // no compression because this will be unzipped soon
271 FlagWithArg("-D ", dir.String())
272 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
273
Colin Crossf1a035e2020-11-16 17:32:30 -0800274 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900275 return entries
276}
Colin Crossffe6b9d2020-12-01 15:40:06 -0800277
278// packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
279// topological order.
280type packagingSpecsDepSet struct {
281 depSet
282}
283
284// newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and
285// transitive contents.
286func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet {
287 return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
288}
289
290// ToList returns the packagingSpecsDepSet flattened to a list in topological order.
291func (d *packagingSpecsDepSet) ToList() []PackagingSpec {
292 if d == nil {
293 return nil
294 }
295 return d.depSet.ToList().([]PackagingSpec)
296}