blob: 72c0c177786b4b21258cdaa9707efefc784b2863 [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
41}
Jiyong Parkdda8f692020-11-09 18:38:48 +090042
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090043// Get file name of installed package
44func (p *PackagingSpec) FileName() string {
45 if p.relPathInPackage != "" {
46 return filepath.Base(p.relPathInPackage)
47 }
48
49 return ""
50}
51
Jiyong Park6446b622021-02-01 20:08:28 +090052// Path relative to the root of the package
53func (p *PackagingSpec) RelPathInPackage() string {
54 return p.relPathInPackage
55}
56
Jiyong Parkdda8f692020-11-09 18:38:48 +090057type PackageModule interface {
58 Module
59 packagingBase() *PackagingBase
60
61 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +090062 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
63 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +090064 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090065
66 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +090067 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +090068 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
69 // etc.) from the extracted files
Paul Duffin4076a752021-02-02 10:59:54 +000070 CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +090071}
72
73// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
74// include this struct and call InitPackageModule.
75type PackagingBase struct {
76 properties PackagingProperties
77
Jiyong Parkcc1157c2020-11-25 11:31:13 +090078 // Allows this module to skip missing dependencies. In most cases, this is not required, but
79 // for rare cases like when there's a dependency to a module which exists in certain repo
80 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +090081 IgnoreMissingDependencies bool
82}
83
84type depsProperty struct {
85 // Modules to include in this package
86 Deps []string `android:"arch_variant"`
87}
88
89type packagingMultilibProperties struct {
90 First depsProperty `android:"arch_variant"`
91 Common depsProperty `android:"arch_variant"`
92 Lib32 depsProperty `android:"arch_variant"`
93 Lib64 depsProperty `android:"arch_variant"`
94}
95
Jiyong Park2136d152021-02-01 23:24:56 +090096type packagingArchProperties struct {
97 Arm64 depsProperty
98 Arm depsProperty
99 X86_64 depsProperty
100 X86 depsProperty
101}
102
Jiyong Parkdda8f692020-11-09 18:38:48 +0900103type PackagingProperties struct {
104 Deps []string `android:"arch_variant"`
105 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900106 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900107}
108
Jiyong Parkdda8f692020-11-09 18:38:48 +0900109func InitPackageModule(p PackageModule) {
110 base := p.packagingBase()
111 p.AddProperties(&base.properties)
112}
113
114func (p *PackagingBase) packagingBase() *PackagingBase {
115 return p
116}
117
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900118// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
119// the current archicture when this module is not configured for multi target. When configured for
120// multi target, deps is selected for each of the targets and is NOT selected for the current
121// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900122func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
123 var ret []string
124 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
125 ret = append(ret, p.properties.Deps...)
126 } else if arch.Multilib == "lib32" {
127 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
128 } else if arch.Multilib == "lib64" {
129 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
130 } else if arch == Common {
131 ret = append(ret, p.properties.Multilib.Common.Deps...)
132 }
Jiyong Park2136d152021-02-01 23:24:56 +0900133
Jiyong Parkdda8f692020-11-09 18:38:48 +0900134 for i, t := range ctx.MultiTargets() {
135 if t.Arch.ArchType == arch {
136 ret = append(ret, p.properties.Deps...)
137 if i == 0 {
138 ret = append(ret, p.properties.Multilib.First.Deps...)
139 }
140 }
141 }
Jiyong Park2136d152021-02-01 23:24:56 +0900142
143 if ctx.Arch().ArchType == Common {
144 switch arch {
145 case Arm64:
146 ret = append(ret, p.properties.Arch.Arm64.Deps...)
147 case Arm:
148 ret = append(ret, p.properties.Arch.Arm.Deps...)
149 case X86_64:
150 ret = append(ret, p.properties.Arch.X86_64.Deps...)
151 case X86:
152 ret = append(ret, p.properties.Arch.X86.Deps...)
153 }
154 }
155
Jiyong Parkdda8f692020-11-09 18:38:48 +0900156 return FirstUniqueStrings(ret)
157}
158
159func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
160 var ret []Target
161 // The current and the common OS targets are always supported
162 ret = append(ret, ctx.Target())
163 if ctx.Arch().ArchType != Common {
164 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
165 }
166 // If this module is configured for multi targets, those should be supported as well
167 ret = append(ret, ctx.MultiTargets()...)
168 return ret
169}
170
Jooyung Han092ef812021-03-10 15:40:34 +0900171// PackagingItem is a marker interface for dependency tags.
172// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
173type PackagingItem interface {
174 // IsPackagingItem returns true if the dep is to be packaged
175 IsPackagingItem() bool
176}
177
178// DepTag provides default implementation of PackagingItem interface.
179// PackagingBase-derived modules can define their own dependency tag by embedding this, which
180// can be passed to AddDeps() or AddDependencies().
181type PackagingItemAlwaysDepTag struct {
182}
183
184// IsPackagingItem returns true if the dep is to be packaged
185func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
186 return true
187}
188
Jiyong Parkdda8f692020-11-09 18:38:48 +0900189// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900190func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900191 for _, t := range p.getSupportedTargets(ctx) {
192 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
193 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
194 continue
195 }
196 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
197 }
198 }
199}
200
201// See PackageModule.CopyDepsToZip
Paul Duffin4076a752021-02-02 10:59:54 +0000202func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut WritablePath) (entries []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900203 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900204 ctx.VisitDirectDeps(func(child Module) {
205 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
206 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900207 }
Jooyung Han092ef812021-03-10 15:40:34 +0900208 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900209 if _, ok := m[ps.relPathInPackage]; !ok {
210 m[ps.relPathInPackage] = ps
211 }
212 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900213 })
214
Colin Crossf1a035e2020-11-16 17:32:30 -0800215 builder := NewRuleBuilder(pctx, ctx)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900216
Paul Duffin4076a752021-02-02 10:59:54 +0000217 dir := PathForModuleOut(ctx, ".zip")
Jiyong Parkdda8f692020-11-09 18:38:48 +0900218 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
219 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
220
221 seenDir := make(map[string]bool)
222 for _, k := range SortedStringKeys(m) {
223 ps := m[k]
224 destPath := dir.Join(ctx, ps.relPathInPackage).String()
225 destDir := filepath.Dir(destPath)
226 entries = append(entries, ps.relPathInPackage)
227 if _, ok := seenDir[destDir]; !ok {
228 seenDir[destDir] = true
229 builder.Command().Text("mkdir").Flag("-p").Text(destDir)
230 }
231 if ps.symlinkTarget == "" {
232 builder.Command().Text("cp").Input(ps.srcPath).Text(destPath)
233 } else {
234 builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath)
235 }
236 if ps.executable {
237 builder.Command().Text("chmod").Flag("a+x").Text(destPath)
238 }
239 }
240
241 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800242 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900243 FlagWithOutput("-o ", zipOut).
244 FlagWithArg("-C ", dir.String()).
245 Flag("-L 0"). // no compression because this will be unzipped soon
246 FlagWithArg("-D ", dir.String())
247 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
248
Colin Crossf1a035e2020-11-16 17:32:30 -0800249 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900250 return entries
251}
Colin Crossffe6b9d2020-12-01 15:40:06 -0800252
253// packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
254// topological order.
255type packagingSpecsDepSet struct {
256 depSet
257}
258
259// newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and
260// transitive contents.
261func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet {
262 return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
263}
264
265// ToList returns the packagingSpecsDepSet flattened to a list in topological order.
266func (d *packagingSpecsDepSet) ToList() []PackagingSpec {
267 if d == nil {
268 return nil
269 }
270 return d.depSet.ToList().([]PackagingSpec)
271}