blob: fe61da1e936aee625748840c186fec85186c5ce3 [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
Jiyong Park16ef7ac2024-05-01 12:36:10 +000053func (p *PackagingSpec) Equals(other *PackagingSpec) bool {
54 if other == nil {
55 return false
56 }
57 if p.relPathInPackage != other.relPathInPackage {
58 return false
59 }
60 if p.srcPath != other.srcPath || p.symlinkTarget != other.symlinkTarget {
61 return false
62 }
63 if p.executable != other.executable {
64 return false
65 }
66 if p.partition != other.partition {
67 return false
68 }
69 return true
70}
71
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090072// Get file name of installed package
73func (p *PackagingSpec) FileName() string {
74 if p.relPathInPackage != "" {
75 return filepath.Base(p.relPathInPackage)
76 }
77
78 return ""
79}
80
Jiyong Park6446b622021-02-01 20:08:28 +090081// Path relative to the root of the package
82func (p *PackagingSpec) RelPathInPackage() string {
83 return p.relPathInPackage
84}
85
Dan Willemsen9fe14102021-07-13 21:52:04 -070086func (p *PackagingSpec) SetRelPathInPackage(relPathInPackage string) {
87 p.relPathInPackage = relPathInPackage
88}
89
90func (p *PackagingSpec) EffectiveLicenseFiles() Paths {
91 if p.effectiveLicenseFiles == nil {
92 return Paths{}
93 }
94 return *p.effectiveLicenseFiles
95}
96
Jooyung Han99c5fe62022-03-21 15:13:38 +090097func (p *PackagingSpec) Partition() string {
98 return p.partition
99}
100
Jiyong Park4152b192024-04-30 21:24:21 +0900101func (p *PackagingSpec) SkipInstall() bool {
102 return p.skipInstall
103}
104
Jiyong Parkdda8f692020-11-09 18:38:48 +0900105type PackageModule interface {
106 Module
107 packagingBase() *PackagingBase
108
109 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jooyung Han092ef812021-03-10 15:40:34 +0900110 // When adding the dependencies, depTag is used as the tag. If `deps` modules are meant to
111 // be copied to a zip in CopyDepsToZip, `depTag` should implement PackagingItem marker interface.
Jiyong Park65b62242020-11-25 12:44:59 +0900112 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900113
Jooyung Hana8834282022-03-25 11:40:12 +0900114 // GatherPackagingSpecs gathers PackagingSpecs of transitive dependencies.
115 GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec
Jeongik Cha54bf8752024-02-08 10:44:37 +0900116 GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec
Jooyung Hana8834282022-03-25 11:40:12 +0900117
Jiyong Parkdda8f692020-11-09 18:38:48 +0900118 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900119 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +0900120 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
121 // etc.) from the extracted files
Jooyung Hana8834282022-03-25 11:40:12 +0900122 CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) []string
Jiyong Parkdda8f692020-11-09 18:38:48 +0900123}
124
125// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
126// include this struct and call InitPackageModule.
127type PackagingBase struct {
128 properties PackagingProperties
129
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900130 // Allows this module to skip missing dependencies. In most cases, this is not required, but
131 // for rare cases like when there's a dependency to a module which exists in certain repo
132 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900133 IgnoreMissingDependencies bool
134}
135
136type depsProperty struct {
137 // Modules to include in this package
138 Deps []string `android:"arch_variant"`
139}
140
141type packagingMultilibProperties struct {
142 First depsProperty `android:"arch_variant"`
143 Common depsProperty `android:"arch_variant"`
144 Lib32 depsProperty `android:"arch_variant"`
145 Lib64 depsProperty `android:"arch_variant"`
146}
147
Jiyong Park2136d152021-02-01 23:24:56 +0900148type packagingArchProperties struct {
149 Arm64 depsProperty
150 Arm depsProperty
151 X86_64 depsProperty
152 X86 depsProperty
153}
154
Jiyong Parkdda8f692020-11-09 18:38:48 +0900155type PackagingProperties struct {
156 Deps []string `android:"arch_variant"`
157 Multilib packagingMultilibProperties `android:"arch_variant"`
Jiyong Park2136d152021-02-01 23:24:56 +0900158 Arch packagingArchProperties
Jiyong Parkdda8f692020-11-09 18:38:48 +0900159}
160
Jiyong Parkdda8f692020-11-09 18:38:48 +0900161func InitPackageModule(p PackageModule) {
162 base := p.packagingBase()
163 p.AddProperties(&base.properties)
164}
165
166func (p *PackagingBase) packagingBase() *PackagingBase {
167 return p
168}
169
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900170// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
171// the current archicture when this module is not configured for multi target. When configured for
172// multi target, deps is selected for each of the targets and is NOT selected for the current
173// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900174func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
175 var ret []string
176 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
177 ret = append(ret, p.properties.Deps...)
178 } else if arch.Multilib == "lib32" {
179 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
180 } else if arch.Multilib == "lib64" {
181 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
182 } else if arch == Common {
183 ret = append(ret, p.properties.Multilib.Common.Deps...)
184 }
Jiyong Park2136d152021-02-01 23:24:56 +0900185
Jiyong Parkdda8f692020-11-09 18:38:48 +0900186 for i, t := range ctx.MultiTargets() {
187 if t.Arch.ArchType == arch {
188 ret = append(ret, p.properties.Deps...)
189 if i == 0 {
190 ret = append(ret, p.properties.Multilib.First.Deps...)
191 }
192 }
193 }
Jiyong Park2136d152021-02-01 23:24:56 +0900194
195 if ctx.Arch().ArchType == Common {
196 switch arch {
197 case Arm64:
198 ret = append(ret, p.properties.Arch.Arm64.Deps...)
199 case Arm:
200 ret = append(ret, p.properties.Arch.Arm.Deps...)
201 case X86_64:
202 ret = append(ret, p.properties.Arch.X86_64.Deps...)
203 case X86:
204 ret = append(ret, p.properties.Arch.X86.Deps...)
205 }
206 }
207
Jiyong Parkdda8f692020-11-09 18:38:48 +0900208 return FirstUniqueStrings(ret)
209}
210
211func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
212 var ret []Target
213 // The current and the common OS targets are always supported
214 ret = append(ret, ctx.Target())
215 if ctx.Arch().ArchType != Common {
216 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
217 }
218 // If this module is configured for multi targets, those should be supported as well
219 ret = append(ret, ctx.MultiTargets()...)
220 return ret
221}
222
Jooyung Han092ef812021-03-10 15:40:34 +0900223// PackagingItem is a marker interface for dependency tags.
224// Direct dependencies with a tag implementing PackagingItem are packaged in CopyDepsToZip().
225type PackagingItem interface {
226 // IsPackagingItem returns true if the dep is to be packaged
227 IsPackagingItem() bool
228}
229
230// DepTag provides default implementation of PackagingItem interface.
231// PackagingBase-derived modules can define their own dependency tag by embedding this, which
232// can be passed to AddDeps() or AddDependencies().
233type PackagingItemAlwaysDepTag struct {
234}
235
236// IsPackagingItem returns true if the dep is to be packaged
237func (PackagingItemAlwaysDepTag) IsPackagingItem() bool {
238 return true
239}
240
Jiyong Parkdda8f692020-11-09 18:38:48 +0900241// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900242func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900243 for _, t := range p.getSupportedTargets(ctx) {
244 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
245 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
246 continue
247 }
248 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
249 }
250 }
251}
252
Jeongik Cha54bf8752024-02-08 10:44:37 +0900253func (p *PackagingBase) GatherPackagingSpecsWithFilter(ctx ModuleContext, filter func(PackagingSpec) bool) map[string]PackagingSpec {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900254 m := make(map[string]PackagingSpec)
Jooyung Han092ef812021-03-10 15:40:34 +0900255 ctx.VisitDirectDeps(func(child Module) {
256 if pi, ok := ctx.OtherModuleDependencyTag(child).(PackagingItem); !ok || !pi.IsPackagingItem() {
257 return
Jiyong Parkdda8f692020-11-09 18:38:48 +0900258 }
Jooyung Han092ef812021-03-10 15:40:34 +0900259 for _, ps := range child.TransitivePackagingSpecs() {
Jeongik Cha54bf8752024-02-08 10:44:37 +0900260 if filter != nil {
261 if !filter(ps) {
262 continue
263 }
264 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000265 dstPath := ps.relPathInPackage
266 if existingPs, ok := m[dstPath]; ok {
267 if !existingPs.Equals(&ps) {
268 ctx.ModuleErrorf("packaging conflict at %v:\n%v\n%v", dstPath, existingPs, ps)
269 }
270 continue
Jiyong Parkdda8f692020-11-09 18:38:48 +0900271 }
Jiyong Park16ef7ac2024-05-01 12:36:10 +0000272
273 m[dstPath] = ps
Jiyong Parkdda8f692020-11-09 18:38:48 +0900274 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900275 })
Jooyung Handf09d172021-05-11 11:13:30 +0900276 return m
277}
Jiyong Parkdda8f692020-11-09 18:38:48 +0900278
Jeongik Cha54bf8752024-02-08 10:44:37 +0900279// See PackageModule.GatherPackagingSpecs
280func (p *PackagingBase) GatherPackagingSpecs(ctx ModuleContext) map[string]PackagingSpec {
281 return p.GatherPackagingSpecsWithFilter(ctx, nil)
282}
283
Dan Willemsen9fe14102021-07-13 21:52:04 -0700284// CopySpecsToDir is a helper that will add commands to the rule builder to copy the PackagingSpec
285// entries into the specified directory.
Peter Collingbourneff56c012023-03-15 22:24:03 -0700286func (p *PackagingBase) CopySpecsToDir(ctx ModuleContext, builder *RuleBuilder, specs map[string]PackagingSpec, dir WritablePath) (entries []string) {
Cole Faust3b3a0112024-01-03 15:16:55 -0800287 if len(specs) == 0 {
288 return entries
289 }
Jiyong Parkdda8f692020-11-09 18:38:48 +0900290 seenDir := make(map[string]bool)
Jeongik Cha76e677f2023-12-21 16:39:15 +0900291 preparerPath := PathForModuleOut(ctx, "preparer.sh")
292 cmd := builder.Command().Tool(preparerPath)
293 var sb strings.Builder
Cole Faust3b3a0112024-01-03 15:16:55 -0800294 sb.WriteString("set -e\n")
Cole Faust18994c72023-02-28 16:02:16 -0800295 for _, k := range SortedKeys(specs) {
Jooyung Hana8834282022-03-25 11:40:12 +0900296 ps := specs[k]
Peter Collingbourneff56c012023-03-15 22:24:03 -0700297 destPath := filepath.Join(dir.String(), ps.relPathInPackage)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900298 destDir := filepath.Dir(destPath)
299 entries = append(entries, ps.relPathInPackage)
300 if _, ok := seenDir[destDir]; !ok {
301 seenDir[destDir] = true
Jeongik Cha76e677f2023-12-21 16:39:15 +0900302 sb.WriteString(fmt.Sprintf("mkdir -p %s\n", destDir))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900303 }
304 if ps.symlinkTarget == "" {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900305 cmd.Implicit(ps.srcPath)
306 sb.WriteString(fmt.Sprintf("cp %s %s\n", ps.srcPath, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900307 } else {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900308 sb.WriteString(fmt.Sprintf("ln -sf %s %s\n", ps.symlinkTarget, destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900309 }
310 if ps.executable {
Jeongik Cha76e677f2023-12-21 16:39:15 +0900311 sb.WriteString(fmt.Sprintf("chmod a+x %s\n", destPath))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900312 }
313 }
314
Jeongik Cha76e677f2023-12-21 16:39:15 +0900315 WriteExecutableFileRuleVerbatim(ctx, preparerPath, sb.String())
316
Dan Willemsen9fe14102021-07-13 21:52:04 -0700317 return entries
318}
319
320// See PackageModule.CopyDepsToZip
Jooyung Hana8834282022-03-25 11:40:12 +0900321func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, specs map[string]PackagingSpec, zipOut WritablePath) (entries []string) {
Dan Willemsen9fe14102021-07-13 21:52:04 -0700322 builder := NewRuleBuilder(pctx, ctx)
323
324 dir := PathForModuleOut(ctx, ".zip")
325 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
326 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
Jooyung Hana8834282022-03-25 11:40:12 +0900327 entries = p.CopySpecsToDir(ctx, builder, specs, dir)
Dan Willemsen9fe14102021-07-13 21:52:04 -0700328
Jiyong Parkdda8f692020-11-09 18:38:48 +0900329 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800330 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900331 FlagWithOutput("-o ", zipOut).
332 FlagWithArg("-C ", dir.String()).
333 Flag("-L 0"). // no compression because this will be unzipped soon
334 FlagWithArg("-D ", dir.String())
335 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
336
Colin Crossf1a035e2020-11-16 17:32:30 -0800337 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900338 return entries
339}