blob: 2506378d771904c8ad60e7b55a43a86ea3f5c39b [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 Park073ea552020-11-09 14:08:34 +090046}
Jiyong Parkdda8f692020-11-09 18:38:48 +090047
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090048// Get file name of installed package
49func (p *PackagingSpec) FileName() string {
50 if p.relPathInPackage != "" {
51 return filepath.Base(p.relPathInPackage)
52 }
53
54 return ""
55}
56
Jiyong Park6446b622021-02-01 20:08:28 +090057// Path relative to the root of the package
58func (p *PackagingSpec) RelPathInPackage() string {
59 return p.relPathInPackage
60}
61
Dan Willemsen9fe14102021-07-13 21:52:04 -070062func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
63 p.relPathInPackage = relPathInPackage
64}
65
66func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
67 if p.effectiveLicenseFiles == nil {
68 return Paths{}
69 }
70 return *p.effectiveLicenseFiles
71}
72
Jooyung Han99c5fe62022-03-21 15:13:38 +090073func (p *PackagingSpec) Partition() string {
74 return p.partition
75}
76
Jiyong Parkdda8f692020-11-09 18:38:48 +090077type PackageModule interface {
78 Module
79 packagingBase() *PackagingBase
80
81 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +090082 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
83 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +090084 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090085
Jooyung Hana8834282022-03-25 11:40:12 +090086 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
87 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
88
Jiyong Parkdda8f692020-11-09 18:38:48 +090089 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +090090 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +090091 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
92 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +090093 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +090094}
95
96// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
97// include this struct and call InitPackageModule.
98type PackagingBase struct {
99 properties PackagingProperties
100
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900101 // Allows this module to skip missing dependencies. In most cases, this is not required, but
102 // for rare cases like when there's a dependency to a module which exists in certain repo
103 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900104 IgnoreMissingDependencies bool
105}
106
107type depsProperty struct {
108 // Modules to include in this package
109 Deps []string `android:"arch_variant"`
110}
111
112type packagingMultilibProperties struct {
113 First depsProperty `android:"arch_variant"`
114 Common depsProperty `android:"arch_variant"`
115 Lib32 depsProperty `android:"arch_variant"`
116 Lib64 depsProperty `android:"arch_variant"`
117}
118
Jiyong Park2136d152021-02-01 23:24:56 +0900119type packagingArchProperties struct {
120 Arm64 depsProperty
121 Arm depsProperty
122 X86_64 depsProperty
123 X86 depsProperty
124}
125
Jiyong Parkdda8f692020-11-09 18:38:48 +0900126type PackagingProperties struct {
127 Deps []string `android:"arch_variant"`
128 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900129 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900130}
131
Jiyong Parkdda8f692020-11-09 18:38:48 +0900132func InitPackageModule(p PackageModule) {
133 base := p.packagingBase()
134 p.AddProperties(&base.properties)
135}
136
137func (p *PackagingBase) packagingBase() *PackagingBase {
138 return p
139}
140
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900141// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
142// the current archicture when this module is not configured for multi target. When configured for
143// multi target, deps is selected for each of the targets and is NOT selected for the current
144// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900145func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
146 var ret []string
147 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
148 ret = append(ret, p.properties.Deps...)
149 } else if arch.Multilib == "lib32" {
150 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
151 } else if arch.Multilib == "lib64" {
152 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
153 } else if arch == Common {
154 ret = append(ret, p.properties.Multilib.Common.Deps...)
155 }
Jiyong Park2136d152021-02-01 23:24:56 +0900156
Jiyong Parkdda8f692020-11-09 18:38:48 +0900157 for i, t := range ctx.MultiTargets() {
158 if t.Arch.ArchType == arch {
159 ret = append(ret, p.properties.Deps...)
160 if i == 0 {
161 ret = append(ret, p.properties.Multilib.First.Deps...)
162 }
163 }
164 }
Jiyong Park2136d152021-02-01 23:24:56 +0900165
166 if ctx.Arch().ArchType == Common {
167 switch arch {
168 case Arm64:
169 ret = append(ret, p.properties.Arch.Arm64.Deps...)
170 case Arm:
171 ret = append(ret, p.properties.Arch.Arm.Deps...)
172 case X86_64:
173 ret = append(ret, p.properties.Arch.X86_64.Deps...)
174 case X86:
175 ret = append(ret, p.properties.Arch.X86.Deps...)
176 }
177 }
178
Jiyong Parkdda8f692020-11-09 18:38:48 +0900179 return FirstUniqueStrings(ret)
180}
181
182func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
183 var ret []Target
184 // The current and the common OS targets are always supported
185 ret = append(ret, ctx.Target())
186 if ctx.Arch().ArchType != Common {
187 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
188 }
189 // If this module is configured for multi targets, those should be supported as well
190 ret = append(ret, ctx.MultiTargets()...)
191 return ret
192}
193
Jooyung Han092ef812021-03-10 15:40:34 +0900194// PackagingItem is a marker interface for dependency tags.
195// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
196type PackagingItem interface {
197 // IsPackagingItem returns true if the dep is to be packaged
198 IsPackagingItem() bool
199}
200
201// DepTag provides default implementation of PackagingItem interface.
202// PackagingBase-derived modules can define their own dependency tag by embedding this, which
203// can be passed to AddDeps() or AddDependencies().
204type PackagingItemAlwaysDepTag struct {
205}
206
207// IsPackagingItem returns true if the dep is to be packaged
208func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
209 return true
210}
211
Jiyong Parkdda8f692020-11-09 18:38:48 +0900212// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900213func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900214 for _, t := range p.getSupportedTargets(ctx) {
215 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
216 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
217 continue
218 }
219 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
220 }
221 }
222}
223
Jooyung Hana8834282022-03-25 11:40:12 +0900224// See PackageModule.GatherPackagingSpecs
Jooyung Handf09d172021-05-11 11:13:30 +0900225func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900226 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900227 ctx.VisitDirectDeps(func(child Module) {
228 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
229 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900230 }
Jooyung Han092ef812021-03-10 15:40:34 +0900231 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900232 if _, ok := m[ps.relPathInPackage]; !ok {
233 m[ps.relPathInPackage] = ps
234 }
235 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900236 })
Jooyung Handf09d172021-05-11 11:13:30 +0900237 return m
238}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900239
Dan Willemsen9fe14102021-07-13 21:52:04 -0700240// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
241// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700242func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900243 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900244 preparerPath := PathForModuleOut(ctx, "preparer.sh")
245 cmd := builder.Command().Tool(preparerPath)
246 var sb strings.Builder
Cole Faust18994c72023-02-28 16:02:16 -0800247 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900248 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700249 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900250 destDir := filepath.Dir(destPath)
251 entries = append(entries, ps.relPathInPackage)
252 if _, ok := seenDir[destDir]; !ok {
253 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900254 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900255 }
256 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900257 cmd.Implicit(ps.srcPath)
258 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900259 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900260 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900261 }
262 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900263 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900264 }
265 }
266
Jeongik Cha76e677f2023-12-21 16:39:15 +0900267 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
268
Dan Willemsen9fe14102021-07-13 21:52:04 -0700269 return entries
270}
271
272// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900273func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700274 builder := NewRuleBuilder(pctx, ctx)
275
276 dir := PathForModuleOut(ctx, ".zip")
277 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
278 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900279 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700280
Jiyong Parkdda8f692020-11-09 18:38:48 +0900281 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800282 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900283 FlagWithOutput("-o ", zipOut).
284 FlagWithArg("-C ", dir.String()).
285 Flag("-L 0"). // no compression because this will be unzipped soon
286 FlagWithArg("-D ", dir.String())
287 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
288
Colin Crossf1a035e2020-11-16 17:32:30 -0800289 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900290 return entries
291}