blob: 383f828bda6ff0ed64e914f0a5b2ca779bf1efc7 [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
Justin Yun74f3f302024-05-07 14:32:14 +090051
52 // Paths of aconfig files for the built artifact
53 aconfigPaths *Paths
Jiyong Parkc6a773d2024-05-14 21:49:11 +090054
55 // ArchType of the module which produced this packaging spec
56 archType ArchType
Jiyong Park073ea552020-11-09 14:08:34 +090057}
Jiyong Parkdda8f692020-11-09 18:38:48 +090058
Jiyong Park16ef7ac2024-05-01 12:36:10 +000059func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
60 if other == nil {
61 return false
62 }
63 if p.relPathInPackage != other.relPathInPackage {
64 return false
65 }
66 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
67 return false
68 }
69 if p.executable != other.executable {
70 return false
71 }
72 if p.partition != other.partition {
73 return false
74 }
75 return true
76}
77
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090078// Get file name of installed package
79func (p *PackagingSpec) FileName() string {
80 if p.relPathInPackage != "" {
81 return filepath.Base(p.relPathInPackage)
82 }
83
84 return ""
85}
86
Jiyong Park6446b622021-02-01 20:08:28 +090087// Path relative to the root of the package
88func (p *PackagingSpec) RelPathInPackage() string {
89 return p.relPathInPackage
90}
91
Dan Willemsen9fe14102021-07-13 21:52:04 -070092func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
93 p.relPathInPackage = relPathInPackage
94}
95
96func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
97 if p.effectiveLicenseFiles == nil {
98 return Paths{}
99 }
100 return *p.effectiveLicenseFiles
101}
102
Jooyung Han99c5fe62022-03-21 15:13:38 +0900103func (p *PackagingSpec) Partition() string {
104 return p.partition
105}
106
Jiyong Park4152b192024-04-30 21:24:21 +0900107func (p *PackagingSpec) SkipInstall() bool {
108 return p.skipInstall
109}
110
Justin Yun74f3f302024-05-07 14:32:14 +0900111// Paths of aconfig files for the built artifact
112func (p *PackagingSpec) GetAconfigPaths() Paths {
113 return *p.aconfigPaths
114}
115
Jiyong Parkdda8f692020-11-09 18:38:48 +0900116type PackageModule interface {
117 Module
118 packagingBase() *PackagingBase
119
120 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900121 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
122 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900123 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900124
Jooyung Hana8834282022-03-25 11:40:12 +0900125 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
126 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900127 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900128
Jiyong Parkdda8f692020-11-09 18:38:48 +0900129 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900130 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900131 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
132 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900133 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900134}
135
136// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
137// include this struct and call InitPackageModule.
138type PackagingBase struct {
139 properties PackagingProperties
140
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900141 // Allows this module to skip missing dependencies. In most cases, this is not required, but
142 // for rare cases like when there's a dependency to a module which exists in certain repo
143 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900144 IgnoreMissingDependencies bool
145}
146
147type depsProperty struct {
148 // Modules to include in this package
149 Deps []string `android:"arch_variant"`
150}
151
152type packagingMultilibProperties struct {
153 First depsProperty `android:"arch_variant"`
154 Common depsProperty `android:"arch_variant"`
155 Lib32 depsProperty `android:"arch_variant"`
156 Lib64 depsProperty `android:"arch_variant"`
157}
158
Jiyong Park2136d152021-02-01 23:24:56 +0900159type packagingArchProperties struct {
160 Arm64 depsProperty
161 Arm depsProperty
162 X86_64 depsProperty
163 X86 depsProperty
164}
165
Jiyong Parkdda8f692020-11-09 18:38:48 +0900166type PackagingProperties struct {
167 Deps []string `android:"arch_variant"`
168 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900169 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900170}
171
Jiyong Parkdda8f692020-11-09 18:38:48 +0900172func InitPackageModule(p PackageModule) {
173 base := p.packagingBase()
174 p.AddProperties(&base.properties)
175}
176
177func (p *PackagingBase) packagingBase() *PackagingBase {
178 return p
179}
180
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900181// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
182// the current archicture when this module is not configured for multi target. When configured for
183// multi target, deps is selected for each of the targets and is NOT selected for the current
184// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900185func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
186 var ret []string
187 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
188 ret = append(ret, p.properties.Deps...)
189 } else if arch.Multilib == "lib32" {
190 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
191 } else if arch.Multilib == "lib64" {
192 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
193 } else if arch == Common {
194 ret = append(ret, p.properties.Multilib.Common.Deps...)
195 }
Jiyong Park2136d152021-02-01 23:24:56 +0900196
Jiyong Parkdda8f692020-11-09 18:38:48 +0900197 for i, t := range ctx.MultiTargets() {
198 if t.Arch.ArchType == arch {
199 ret = append(ret, p.properties.Deps...)
200 if i == 0 {
201 ret = append(ret, p.properties.Multilib.First.Deps...)
202 }
203 }
204 }
Jiyong Park2136d152021-02-01 23:24:56 +0900205
206 if ctx.Arch().ArchType == Common {
207 switch arch {
208 case Arm64:
209 ret = append(ret, p.properties.Arch.Arm64.Deps...)
210 case Arm:
211 ret = append(ret, p.properties.Arch.Arm.Deps...)
212 case X86_64:
213 ret = append(ret, p.properties.Arch.X86_64.Deps...)
214 case X86:
215 ret = append(ret, p.properties.Arch.X86.Deps...)
216 }
217 }
218
Jiyong Parkdda8f692020-11-09 18:38:48 +0900219 return FirstUniqueStrings(ret)
220}
221
222func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
223 var ret []Target
224 // The current and the common OS targets are always supported
225 ret = append(ret, ctx.Target())
226 if ctx.Arch().ArchType != Common {
227 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
228 }
229 // If this module is configured for multi targets, those should be supported as well
230 ret = append(ret, ctx.MultiTargets()...)
231 return ret
232}
233
Jooyung Han092ef812021-03-10 15:40:34 +0900234// PackagingItem is a marker interface for dependency tags.
235// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
236type PackagingItem interface {
237 // IsPackagingItem returns true if the dep is to be packaged
238 IsPackagingItem() bool
239}
240
241// DepTag provides default implementation of PackagingItem interface.
242// PackagingBase-derived modules can define their own dependency tag by embedding this, which
243// can be passed to AddDeps() or AddDependencies().
244type PackagingItemAlwaysDepTag struct {
245}
246
247// IsPackagingItem returns true if the dep is to be packaged
248func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
249 return true
250}
251
Jiyong Parkdda8f692020-11-09 18:38:48 +0900252// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900253func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900254 for _, t := range p.getSupportedTargets(ctx) {
255 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
256 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
257 continue
258 }
259 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
260 }
261 }
262}
263
Jeongik Cha54bf8752024-02-08 10:44:37 +0900264func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900265 m := make(map[string]PackagingSpec)
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900266
267 var arches []ArchType
268 for _, target := range p.getSupportedTargets(ctx) {
269 arches = append(arches, target.Arch.ArchType)
270 }
271
272 // filter out packaging specs for unsupported architecture
273 filterArch := func(ps PackagingSpec) bool {
274 for _, arch := range arches {
275 if arch == ps.archType {
276 return true
277 }
278 }
279 return false
280 }
281
Jooyung Han092ef812021-03-10 15:40:34 +0900282 ctx.VisitDirectDeps(func(child Module) {
283 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
284 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900285 }
Jooyung Han092ef812021-03-10 15:40:34 +0900286 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900287 if !filterArch(ps) {
288 continue
289 }
290
Jeongik Cha54bf8752024-02-08 10:44:37 +0900291 if filter != nil {
292 if !filter(ps) {
293 continue
294 }
295 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000296 dstPath := ps.relPathInPackage
297 if existingPs, ok := m[dstPath]; ok {
298 if !existingPs.Equals(&ps) {
299 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
300 }
301 continue
Jiyong Parkdda8f692020-11-09 18:38:48 +0900302 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000303
304 m[dstPath] = ps
Jiyong Parkdda8f692020-11-09 18:38:48 +0900305 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900306 })
Jooyung Handf09d172021-05-11 11:13:30 +0900307 return m
308}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900309
Jeongik Cha54bf8752024-02-08 10:44:37 +0900310// See PackageModule.GatherPackagingSpecs
311func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
312 return p.GatherPackagingSpecsWithFilter(ctx, nil)
313}
314
Dan Willemsen9fe14102021-07-13 21:52:04 -0700315// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
316// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700317func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Cole Faust3b3a0112024-01-03 15:16:55 -0800318 if len(specs) == 0 {
319 return entries
320 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900321 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900322 preparerPath := PathForModuleOut(ctx, "preparer.sh")
323 cmd := builder.Command().Tool(preparerPath)
324 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800325 sb.WriteString("set -e\n")
Cole Faust18994c72023-02-28 16:02:16 -0800326 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900327 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700328 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900329 destDir := filepath.Dir(destPath)
330 entries = append(entries, ps.relPathInPackage)
331 if _, ok := seenDir[destDir]; !ok {
332 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900333 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900334 }
335 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900336 cmd.Implicit(ps.srcPath)
337 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900338 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900339 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900340 }
341 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900342 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900343 }
344 }
345
Jeongik Cha76e677f2023-12-21 16:39:15 +0900346 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
347
Dan Willemsen9fe14102021-07-13 21:52:04 -0700348 return entries
349}
350
351// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900352func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700353 builder := NewRuleBuilder(pctx, ctx)
354
355 dir := PathForModuleOut(ctx, ".zip")
356 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
357 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900358 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700359
Jiyong Parkdda8f692020-11-09 18:38:48 +0900360 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800361 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900362 FlagWithOutput("-o ", zipOut).
363 FlagWithArg("-C ", dir.String()).
364 Flag("-L 0"). // no compression because this will be unzipped soon
365 FlagWithArg("-D ", dir.String())
366 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
367
Colin Crossf1a035e2020-11-16 17:32:30 -0800368 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900369 return entries
370}