blob: 66772183c49868d06e76a8c89dae74bcba4ef432 [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
Jiyong Park073ea552020-11-09 14:08:34 +090051}
Jiyong Parkdda8f692020-11-09 18:38:48 +090052
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090053// Get file name of installed package
54func (p *PackagingSpec) FileName() string {
55 if p.relPathInPackage != "" {
56 return filepath.Base(p.relPathInPackage)
57 }
58
59 return ""
60}
61
Jiyong Park6446b622021-02-01 20:08:28 +090062// Path relative to the root of the package
63func (p *PackagingSpec) RelPathInPackage() string {
64 return p.relPathInPackage
65}
66
Dan Willemsen9fe14102021-07-13 21:52:04 -070067func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
68 p.relPathInPackage = relPathInPackage
69}
70
71func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
72 if p.effectiveLicenseFiles == nil {
73 return Paths{}
74 }
75 return *p.effectiveLicenseFiles
76}
77
Jooyung Han99c5fe62022-03-21 15:13:38 +090078func (p *PackagingSpec) Partition() string {
79 return p.partition
80}
81
Jiyong Park4152b192024-04-30 21:24:21 +090082func (p *PackagingSpec) SkipInstall() bool {
83 return p.skipInstall
84}
85
Jiyong Parkdda8f692020-11-09 18:38:48 +090086type PackageModule interface {
87 Module
88 packagingBase() *PackagingBase
89
90 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +090091 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
92 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +090093 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090094
Jooyung Hana8834282022-03-25 11:40:12 +090095 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
96 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +090097 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +090098
Jiyong Parkdda8f692020-11-09 18:38:48 +090099 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900100 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900101 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
102 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900103 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900104}
105
106// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
107// include this struct and call InitPackageModule.
108type PackagingBase struct {
109 properties PackagingProperties
110
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900111 // Allows this module to skip missing dependencies. In most cases, this is not required, but
112 // for rare cases like when there's a dependency to a module which exists in certain repo
113 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900114 IgnoreMissingDependencies bool
115}
116
117type depsProperty struct {
118 // Modules to include in this package
119 Deps []string `android:"arch_variant"`
120}
121
122type packagingMultilibProperties struct {
123 First depsProperty `android:"arch_variant"`
124 Common depsProperty `android:"arch_variant"`
125 Lib32 depsProperty `android:"arch_variant"`
126 Lib64 depsProperty `android:"arch_variant"`
127}
128
Jiyong Park2136d152021-02-01 23:24:56 +0900129type packagingArchProperties struct {
130 Arm64 depsProperty
131 Arm depsProperty
132 X86_64 depsProperty
133 X86 depsProperty
134}
135
Jiyong Parkdda8f692020-11-09 18:38:48 +0900136type PackagingProperties struct {
137 Deps []string `android:"arch_variant"`
138 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900139 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900140}
141
Jiyong Parkdda8f692020-11-09 18:38:48 +0900142func InitPackageModule(p PackageModule) {
143 base := p.packagingBase()
144 p.AddProperties(&base.properties)
145}
146
147func (p *PackagingBase) packagingBase() *PackagingBase {
148 return p
149}
150
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900151// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
152// the current archicture when this module is not configured for multi target. When configured for
153// multi target, deps is selected for each of the targets and is NOT selected for the current
154// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900155func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
156 var ret []string
157 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
158 ret = append(ret, p.properties.Deps...)
159 } else if arch.Multilib == "lib32" {
160 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
161 } else if arch.Multilib == "lib64" {
162 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
163 } else if arch == Common {
164 ret = append(ret, p.properties.Multilib.Common.Deps...)
165 }
Jiyong Park2136d152021-02-01 23:24:56 +0900166
Jiyong Parkdda8f692020-11-09 18:38:48 +0900167 for i, t := range ctx.MultiTargets() {
168 if t.Arch.ArchType == arch {
169 ret = append(ret, p.properties.Deps...)
170 if i == 0 {
171 ret = append(ret, p.properties.Multilib.First.Deps...)
172 }
173 }
174 }
Jiyong Park2136d152021-02-01 23:24:56 +0900175
176 if ctx.Arch().ArchType == Common {
177 switch arch {
178 case Arm64:
179 ret = append(ret, p.properties.Arch.Arm64.Deps...)
180 case Arm:
181 ret = append(ret, p.properties.Arch.Arm.Deps...)
182 case X86_64:
183 ret = append(ret, p.properties.Arch.X86_64.Deps...)
184 case X86:
185 ret = append(ret, p.properties.Arch.X86.Deps...)
186 }
187 }
188
Jiyong Parkdda8f692020-11-09 18:38:48 +0900189 return FirstUniqueStrings(ret)
190}
191
192func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
193 var ret []Target
194 // The current and the common OS targets are always supported
195 ret = append(ret, ctx.Target())
196 if ctx.Arch().ArchType != Common {
197 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
198 }
199 // If this module is configured for multi targets, those should be supported as well
200 ret = append(ret, ctx.MultiTargets()...)
201 return ret
202}
203
Jooyung Han092ef812021-03-10 15:40:34 +0900204// PackagingItem is a marker interface for dependency tags.
205// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
206type PackagingItem interface {
207 // IsPackagingItem returns true if the dep is to be packaged
208 IsPackagingItem() bool
209}
210
211// DepTag provides default implementation of PackagingItem interface.
212// PackagingBase-derived modules can define their own dependency tag by embedding this, which
213// can be passed to AddDeps() or AddDependencies().
214type PackagingItemAlwaysDepTag struct {
215}
216
217// IsPackagingItem returns true if the dep is to be packaged
218func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
219 return true
220}
221
Jiyong Parkdda8f692020-11-09 18:38:48 +0900222// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900223func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900224 for _, t := range p.getSupportedTargets(ctx) {
225 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
226 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
227 continue
228 }
229 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
230 }
231 }
232}
233
Jeongik Cha54bf8752024-02-08 10:44:37 +0900234func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900235 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900236 ctx.VisitDirectDeps(func(child Module) {
237 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
238 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900239 }
Jooyung Han092ef812021-03-10 15:40:34 +0900240 for _, ps := range child.TransitivePackagingSpecs() {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900241 if filter != nil {
242 if !filter(ps) {
243 continue
244 }
245 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900246 if _, ok := m[ps.relPathInPackage]; !ok {
247 m[ps.relPathInPackage] = ps
248 }
249 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900250 })
Jooyung Handf09d172021-05-11 11:13:30 +0900251 return m
252}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900253
Jeongik Cha54bf8752024-02-08 10:44:37 +0900254// See PackageModule.GatherPackagingSpecs
255func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
256 return p.GatherPackagingSpecsWithFilter(ctx, nil)
257}
258
Dan Willemsen9fe14102021-07-13 21:52:04 -0700259// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
260// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700261func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Cole Faust3b3a0112024-01-03 15:16:55 -0800262 if len(specs) == 0 {
263 return entries
264 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900265 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900266 preparerPath := PathForModuleOut(ctx, "preparer.sh")
267 cmd := builder.Command().Tool(preparerPath)
268 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800269 sb.WriteString("set -e\n")
Cole Faust18994c72023-02-28 16:02:16 -0800270 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900271 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700272 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900273 destDir := filepath.Dir(destPath)
274 entries = append(entries, ps.relPathInPackage)
275 if _, ok := seenDir[destDir]; !ok {
276 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900277 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900278 }
279 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900280 cmd.Implicit(ps.srcPath)
281 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900282 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900283 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900284 }
285 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900286 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900287 }
288 }
289
Jeongik Cha76e677f2023-12-21 16:39:15 +0900290 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
291
Dan Willemsen9fe14102021-07-13 21:52:04 -0700292 return entries
293}
294
295// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900296func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700297 builder := NewRuleBuilder(pctx, ctx)
298
299 dir := PathForModuleOut(ctx, ".zip")
300 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
301 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900302 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700303
Jiyong Parkdda8f692020-11-09 18:38:48 +0900304 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800305 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900306 FlagWithOutput("-o ", zipOut).
307 FlagWithArg("-C ", dir.String()).
308 Flag("-L 0"). // no compression because this will be unzipped soon
309 FlagWithArg("-D ", dir.String())
310 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
311
Colin Crossf1a035e2020-11-16 17:32:30 -0800312 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900313 return entries
314}