blob: a2b875528c2d678b2cfcfd0a925edb8a5507893c [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"
Jiyong Park105e11c2024-05-17 14:58:24 +090023 "github.com/google/blueprint/proptools"
Jiyong Parkdda8f692020-11-09 18:38:48 +090024)
25
Jiyong Parkcc1157c2020-11-25 11:31:13 +090026// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
27// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
28// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
29// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090030type PackagingSpec struct {
31 // Path relative to the root of the package
32 relPathInPackage string
33
34 // The path to the built artifact
35 srcPath Path
36
37 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
38 // srcPath is of course ignored.)
39 symlinkTarget string
40
41 // Whether relPathInPackage should be marked as executable or not
42 executable bool
Dan Willemsen9fe14102021-07-13 21:52:04 -070043
44 effectiveLicenseFiles *Paths
Jooyung Han99c5fe62022-03-21 15:13:38 +090045
46 partition string
Jiyong Park4152b192024-04-30 21:24:21 +090047
48 // Whether this packaging spec represents an installation of the srcPath (i.e. this struct
49 // is created via InstallFile or InstallSymlink) or a simple packaging (i.e. created via
50 // PackageFile).
51 skipInstall bool
Justin Yun74f3f302024-05-07 14:32:14 +090052
53 // Paths of aconfig files for the built artifact
54 aconfigPaths *Paths
Jiyong Parkc6a773d2024-05-14 21:49:11 +090055
56 // ArchType of the module which produced this packaging spec
57 archType ArchType
Jiyong Park073ea552020-11-09 14:08:34 +090058}
Jiyong Parkdda8f692020-11-09 18:38:48 +090059
Jiyong Park16ef7ac2024-05-01 12:36:10 +000060func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
61 if other == nil {
62 return false
63 }
64 if p.relPathInPackage != other.relPathInPackage {
65 return false
66 }
67 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
68 return false
69 }
70 if p.executable != other.executable {
71 return false
72 }
73 if p.partition != other.partition {
74 return false
75 }
76 return true
77}
78
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090079// Get file name of installed package
80func (p *PackagingSpec) FileName() string {
81 if p.relPathInPackage != "" {
82 return filepath.Base(p.relPathInPackage)
83 }
84
85 return ""
86}
87
Jiyong Park6446b622021-02-01 20:08:28 +090088// Path relative to the root of the package
89func (p *PackagingSpec) RelPathInPackage() string {
90 return p.relPathInPackage
91}
92
Dan Willemsen9fe14102021-07-13 21:52:04 -070093func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
94 p.relPathInPackage = relPathInPackage
95}
96
97func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
98 if p.effectiveLicenseFiles == nil {
99 return Paths{}
100 }
101 return *p.effectiveLicenseFiles
102}
103
Jooyung Han99c5fe62022-03-21 15:13:38 +0900104func (p *PackagingSpec) Partition() string {
105 return p.partition
106}
107
Jiyong Park4152b192024-04-30 21:24:21 +0900108func (p *PackagingSpec) SkipInstall() bool {
109 return p.skipInstall
110}
111
Justin Yun74f3f302024-05-07 14:32:14 +0900112// Paths of aconfig files for the built artifact
113func (p *PackagingSpec) GetAconfigPaths() Paths {
114 return *p.aconfigPaths
115}
116
Jiyong Parkdda8f692020-11-09 18:38:48 +0900117type PackageModule interface {
118 Module
119 packagingBase() *PackagingBase
120
121 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900122 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
123 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900124 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900125
Jooyung Hana8834282022-03-25 11:40:12 +0900126 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
127 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900128 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900129
Jiyong Parkdda8f692020-11-09 18:38:48 +0900130 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900131 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900132 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
133 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900134 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900135}
136
137// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
138// include this struct and call InitPackageModule.
139type PackagingBase struct {
140 properties PackagingProperties
141
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900142 // Allows this module to skip missing dependencies. In most cases, this is not required, but
143 // for rare cases like when there's a dependency to a module which exists in certain repo
144 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900145 IgnoreMissingDependencies bool
Jiyong Park3ea9b652024-05-15 23:01:54 +0900146
147 // If this is set to true by a module type inheriting PackagingBase, the deps property
148 // collects the first target only even with compile_multilib: true.
149 DepsCollectFirstTargetOnly bool
Jiyong Parkdda8f692020-11-09 18:38:48 +0900150}
151
152type depsProperty struct {
153 // Modules to include in this package
Jiyong Park105e11c2024-05-17 14:58:24 +0900154 Deps proptools.Configurable[[]string] `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900155}
156
157type packagingMultilibProperties struct {
158 First depsProperty `android:"arch_variant"`
159 Common depsProperty `android:"arch_variant"`
160 Lib32 depsProperty `android:"arch_variant"`
161 Lib64 depsProperty `android:"arch_variant"`
Jiyong Park3ea9b652024-05-15 23:01:54 +0900162 Both depsProperty `android:"arch_variant"`
Jiyong Parkdda8f692020-11-09 18:38:48 +0900163}
164
Jiyong Park2136d152021-02-01 23:24:56 +0900165type packagingArchProperties struct {
166 Arm64 depsProperty
167 Arm depsProperty
168 X86_64 depsProperty
169 X86 depsProperty
170}
171
Jiyong Parkdda8f692020-11-09 18:38:48 +0900172type PackagingProperties struct {
Jiyong Park105e11c2024-05-17 14:58:24 +0900173 Deps proptools.Configurable[[]string] `android:"arch_variant"`
174 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900175 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900176}
177
Jiyong Parkdda8f692020-11-09 18:38:48 +0900178func InitPackageModule(p PackageModule) {
179 base := p.packagingBase()
180 p.AddProperties(&base.properties)
181}
182
183func (p *PackagingBase) packagingBase() *PackagingBase {
184 return p
185}
186
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900187// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
188// the current archicture when this module is not configured for multi target. When configured for
189// multi target, deps is selected for each of the targets and is NOT selected for the current
190// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900191func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
Jiyong Park105e11c2024-05-17 14:58:24 +0900192 get := func(prop proptools.Configurable[[]string]) []string {
193 return prop.GetOrDefault(ctx, nil)
194 }
195
Jiyong Parkdda8f692020-11-09 18:38:48 +0900196 var ret []string
197 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900198 ret = append(ret, get(p.properties.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900199 } else if arch.Multilib == "lib32" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900200 ret = append(ret, get(p.properties.Multilib.Lib32.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900201 } else if arch.Multilib == "lib64" {
Jiyong Park105e11c2024-05-17 14:58:24 +0900202 ret = append(ret, get(p.properties.Multilib.Lib64.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900203 } else if arch == Common {
Jiyong Park105e11c2024-05-17 14:58:24 +0900204 ret = append(ret, get(p.properties.Multilib.Common.Deps)...)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900205 }
Jiyong Park2136d152021-02-01 23:24:56 +0900206
Jiyong Park3ea9b652024-05-15 23:01:54 +0900207 if p.DepsCollectFirstTargetOnly {
Jiyong Park105e11c2024-05-17 14:58:24 +0900208 if len(get(p.properties.Multilib.First.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900209 ctx.PropertyErrorf("multilib.first.deps", "not supported. use \"deps\" instead")
210 }
211 for i, t := range ctx.MultiTargets() {
212 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900213 ret = append(ret, get(p.properties.Multilib.Both.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900214 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900215 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900216 }
217 }
218 }
219 } else {
Jiyong Park105e11c2024-05-17 14:58:24 +0900220 if len(get(p.properties.Multilib.Both.Deps)) > 0 {
Jiyong Park3ea9b652024-05-15 23:01:54 +0900221 ctx.PropertyErrorf("multilib.both.deps", "not supported. use \"deps\" instead")
222 }
223 for i, t := range ctx.MultiTargets() {
224 if t.Arch.ArchType == arch {
Jiyong Park105e11c2024-05-17 14:58:24 +0900225 ret = append(ret, get(p.properties.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900226 if i == 0 {
Jiyong Park105e11c2024-05-17 14:58:24 +0900227 ret = append(ret, get(p.properties.Multilib.First.Deps)...)
Jiyong Park3ea9b652024-05-15 23:01:54 +0900228 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900229 }
230 }
231 }
Jiyong Park2136d152021-02-01 23:24:56 +0900232
233 if ctx.Arch().ArchType == Common {
234 switch arch {
235 case Arm64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900236 ret = append(ret, get(p.properties.Arch.Arm64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900237 case Arm:
Jiyong Park105e11c2024-05-17 14:58:24 +0900238 ret = append(ret, get(p.properties.Arch.Arm.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900239 case X86_64:
Jiyong Park105e11c2024-05-17 14:58:24 +0900240 ret = append(ret, get(p.properties.Arch.X86_64.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900241 case X86:
Jiyong Park105e11c2024-05-17 14:58:24 +0900242 ret = append(ret, get(p.properties.Arch.X86.Deps)...)
Jiyong Park2136d152021-02-01 23:24:56 +0900243 }
244 }
245
Jiyong Parkdda8f692020-11-09 18:38:48 +0900246 return FirstUniqueStrings(ret)
247}
248
249func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
250 var ret []Target
251 // The current and the common OS targets are always supported
252 ret = append(ret, ctx.Target())
253 if ctx.Arch().ArchType != Common {
254 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
255 }
256 // If this module is configured for multi targets, those should be supported as well
257 ret = append(ret, ctx.MultiTargets()...)
258 return ret
259}
260
Jooyung Han092ef812021-03-10 15:40:34 +0900261// PackagingItem is a marker interface for dependency tags.
262// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
263type PackagingItem interface {
264 // IsPackagingItem returns true if the dep is to be packaged
265 IsPackagingItem() bool
266}
267
268// DepTag provides default implementation of PackagingItem interface.
269// PackagingBase-derived modules can define their own dependency tag by embedding this, which
270// can be passed to AddDeps() or AddDependencies().
271type PackagingItemAlwaysDepTag struct {
272}
273
274// IsPackagingItem returns true if the dep is to be packaged
275func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
276 return true
277}
278
Jiyong Parkdda8f692020-11-09 18:38:48 +0900279// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900280func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900281 for _, t := range p.getSupportedTargets(ctx) {
282 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
283 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
284 continue
285 }
286 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
287 }
288 }
289}
290
Jeongik Cha54bf8752024-02-08 10:44:37 +0900291func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900292 m := make(map[string]PackagingSpec)
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900293
294 var arches []ArchType
295 for _, target := range p.getSupportedTargets(ctx) {
296 arches = append(arches, target.Arch.ArchType)
297 }
298
299 // filter out packaging specs for unsupported architecture
300 filterArch := func(ps PackagingSpec) bool {
301 for _, arch := range arches {
302 if arch == ps.archType {
303 return true
304 }
305 }
306 return false
307 }
308
Jooyung Han092ef812021-03-10 15:40:34 +0900309 ctx.VisitDirectDeps(func(child Module) {
310 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
311 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900312 }
Jooyung Han092ef812021-03-10 15:40:34 +0900313 for _, ps := range child.TransitivePackagingSpecs() {
Jiyong Parkc6a773d2024-05-14 21:49:11 +0900314 if !filterArch(ps) {
315 continue
316 }
317
Jeongik Cha54bf8752024-02-08 10:44:37 +0900318 if filter != nil {
319 if !filter(ps) {
320 continue
321 }
322 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000323 dstPath := ps.relPathInPackage
324 if existingPs, ok := m[dstPath]; ok {
325 if !existingPs.Equals(&ps) {
326 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
327 }
328 continue
Jiyong Parkdda8f692020-11-09 18:38:48 +0900329 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000330
331 m[dstPath] = ps
Jiyong Parkdda8f692020-11-09 18:38:48 +0900332 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900333 })
Jooyung Handf09d172021-05-11 11:13:30 +0900334 return m
335}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900336
Jeongik Cha54bf8752024-02-08 10:44:37 +0900337// See PackageModule.GatherPackagingSpecs
338func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
339 return p.GatherPackagingSpecsWithFilter(ctx, nil)
340}
341
Dan Willemsen9fe14102021-07-13 21:52:04 -0700342// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
343// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700344func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Cole Faust3b3a0112024-01-03 15:16:55 -0800345 if len(specs) == 0 {
346 return entries
347 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900348 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900349 preparerPath := PathForModuleOut(ctx, "preparer.sh")
350 cmd := builder.Command().Tool(preparerPath)
351 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800352 sb.WriteString("set -e\n")
Cole Faust18994c72023-02-28 16:02:16 -0800353 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900354 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700355 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900356 destDir := filepath.Dir(destPath)
357 entries = append(entries, ps.relPathInPackage)
358 if _, ok := seenDir[destDir]; !ok {
359 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900360 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900361 }
362 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900363 cmd.Implicit(ps.srcPath)
364 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900365 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900366 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900367 }
368 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900369 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900370 }
371 }
372
Jeongik Cha76e677f2023-12-21 16:39:15 +0900373 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
374
Dan Willemsen9fe14102021-07-13 21:52:04 -0700375 return entries
376}
377
378// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900379func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700380 builder := NewRuleBuilder(pctx, ctx)
381
382 dir := PathForModuleOut(ctx, ".zip")
383 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
384 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900385 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700386
Jiyong Parkdda8f692020-11-09 18:38:48 +0900387 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800388 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900389 FlagWithOutput("-o ", zipOut).
390 FlagWithArg("-C ", dir.String()).
391 Flag("-L 0"). // no compression because this will be unzipped soon
392 FlagWithArg("-D ", dir.String())
393 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
394
Colin Crossf1a035e2020-11-16 17:32:30 -0800395 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900396 return entries
397}