blob: da745ff19194a3c5bc9bcd559ca196c50e5db994 [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"
20
21 "github.com/google/blueprint"
22)
23
Jiyong Parkcc1157c2020-11-25 11:31:13 +090024// PackagingSpec abstracts a request to place a built artifact at a certain path in a package. A
25// package can be the traditional <partition>.img, but isn't limited to those. Other examples could
26// be a new filesystem image that is a subset of system.img (e.g. for an Android-like mini OS
27// running on a VM), or a zip archive for some of the host tools.
Jiyong Park073ea552020-11-09 14:08:34 +090028type PackagingSpec struct {
29 // Path relative to the root of the package
30 relPathInPackage string
31
32 // The path to the built artifact
33 srcPath Path
34
35 // If this is not empty, then relPathInPackage should be a symlink to this target. (Then
36 // srcPath is of course ignored.)
37 symlinkTarget string
38
39 // Whether relPathInPackage should be marked as executable or not
40 executable bool
41}
Jiyong Parkdda8f692020-11-09 18:38:48 +090042
Kiyoung Kim24dfc1f2020-11-16 10:48:44 +090043// Get file name of installed package
44func (p *PackagingSpec) FileName() string {
45 if p.relPathInPackage != "" {
46 return filepath.Base(p.relPathInPackage)
47 }
48
49 return ""
50}
51
Jiyong Parkdda8f692020-11-09 18:38:48 +090052type PackageModule interface {
53 Module
54 packagingBase() *PackagingBase
55
56 // AddDeps adds dependencies to the `deps` modules. This should be called in DepsMutator.
Jiyong Park65b62242020-11-25 12:44:59 +090057 // When adding the dependencies, depTag is used as the tag.
58 AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag)
Jiyong Parkdda8f692020-11-09 18:38:48 +090059
60 // CopyDepsToZip zips the built artifacts of the dependencies into the given zip file and
Jiyong Parkcc1157c2020-11-25 11:31:13 +090061 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
Jiyong Parkdda8f692020-11-09 18:38:48 +090062 // followed by a build rule that unzips it and creates the final output (img, zip, tar.gz,
63 // etc.) from the extracted files
64 CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) []string
65}
66
67// PackagingBase provides basic functionality for packaging dependencies. A module is expected to
68// include this struct and call InitPackageModule.
69type PackagingBase struct {
70 properties PackagingProperties
71
Jiyong Parkcc1157c2020-11-25 11:31:13 +090072 // Allows this module to skip missing dependencies. In most cases, this is not required, but
73 // for rare cases like when there's a dependency to a module which exists in certain repo
74 // checkouts, this is needed.
Jiyong Parkdda8f692020-11-09 18:38:48 +090075 IgnoreMissingDependencies bool
76}
77
78type depsProperty struct {
79 // Modules to include in this package
80 Deps []string `android:"arch_variant"`
81}
82
83type packagingMultilibProperties struct {
84 First depsProperty `android:"arch_variant"`
85 Common depsProperty `android:"arch_variant"`
86 Lib32 depsProperty `android:"arch_variant"`
87 Lib64 depsProperty `android:"arch_variant"`
88}
89
90type PackagingProperties struct {
91 Deps []string `android:"arch_variant"`
92 Multilib packagingMultilibProperties `android:"arch_variant"`
93}
94
Jiyong Parkdda8f692020-11-09 18:38:48 +090095func InitPackageModule(p PackageModule) {
96 base := p.packagingBase()
97 p.AddProperties(&base.properties)
98}
99
100func (p *PackagingBase) packagingBase() *PackagingBase {
101 return p
102}
103
Jiyong Parkcc1157c2020-11-25 11:31:13 +0900104// From deps and multilib.*.deps, select the dependencies that are for the given arch deps is for
105// the current archicture when this module is not configured for multi target. When configured for
106// multi target, deps is selected for each of the targets and is NOT selected for the current
107// architecture which would be Common.
Jiyong Parkdda8f692020-11-09 18:38:48 +0900108func (p *PackagingBase) getDepsForArch(ctx BaseModuleContext, arch ArchType) []string {
109 var ret []string
110 if arch == ctx.Target().Arch.ArchType && len(ctx.MultiTargets()) == 0 {
111 ret = append(ret, p.properties.Deps...)
112 } else if arch.Multilib == "lib32" {
113 ret = append(ret, p.properties.Multilib.Lib32.Deps...)
114 } else if arch.Multilib == "lib64" {
115 ret = append(ret, p.properties.Multilib.Lib64.Deps...)
116 } else if arch == Common {
117 ret = append(ret, p.properties.Multilib.Common.Deps...)
118 }
119 for i, t := range ctx.MultiTargets() {
120 if t.Arch.ArchType == arch {
121 ret = append(ret, p.properties.Deps...)
122 if i == 0 {
123 ret = append(ret, p.properties.Multilib.First.Deps...)
124 }
125 }
126 }
127 return FirstUniqueStrings(ret)
128}
129
130func (p *PackagingBase) getSupportedTargets(ctx BaseModuleContext) []Target {
131 var ret []Target
132 // The current and the common OS targets are always supported
133 ret = append(ret, ctx.Target())
134 if ctx.Arch().ArchType != Common {
135 ret = append(ret, Target{Os: ctx.Os(), Arch: Arch{ArchType: Common}})
136 }
137 // If this module is configured for multi targets, those should be supported as well
138 ret = append(ret, ctx.MultiTargets()...)
139 return ret
140}
141
142// See PackageModule.AddDeps
Jiyong Park65b62242020-11-25 12:44:59 +0900143func (p *PackagingBase) AddDeps(ctx BottomUpMutatorContext, depTag blueprint.DependencyTag) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900144 for _, t := range p.getSupportedTargets(ctx) {
145 for _, dep := range p.getDepsForArch(ctx, t.Arch.ArchType) {
146 if p.IgnoreMissingDependencies && !ctx.OtherModuleExists(dep) {
147 continue
148 }
149 ctx.AddFarVariationDependencies(t.Variations(), depTag, dep)
150 }
151 }
152}
153
154// See PackageModule.CopyDepsToZip
155func (p *PackagingBase) CopyDepsToZip(ctx ModuleContext, zipOut OutputPath) (entries []string) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900156 m := make(map[string]PackagingSpec)
157 ctx.WalkDeps(func(child Module, parent Module) bool {
Jiyong Parkd630bdd2020-11-25 11:47:24 +0900158 if !IsInstallDepNeeded(ctx.OtherModuleDependencyTag(child)) {
Jiyong Parkdda8f692020-11-09 18:38:48 +0900159 return false
160 }
161 for _, ps := range child.PackagingSpecs() {
162 if _, ok := m[ps.relPathInPackage]; !ok {
163 m[ps.relPathInPackage] = ps
164 }
165 }
166 return true
167 })
168
Colin Crossf1a035e2020-11-16 17:32:30 -0800169 builder := NewRuleBuilder(pctx, ctx)
Jiyong Parkdda8f692020-11-09 18:38:48 +0900170
171 dir := PathForModuleOut(ctx, ".zip").OutputPath
172 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
173 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
174
175 seenDir := make(map[string]bool)
176 for _, k := range SortedStringKeys(m) {
177 ps := m[k]
178 destPath := dir.Join(ctx, ps.relPathInPackage).String()
179 destDir := filepath.Dir(destPath)
180 entries = append(entries, ps.relPathInPackage)
181 if _, ok := seenDir[destDir]; !ok {
182 seenDir[destDir] = true
183 builder.Command().Text("mkdir").Flag("-p").Text(destDir)
184 }
185 if ps.symlinkTarget == "" {
186 builder.Command().Text("cp").Input(ps.srcPath).Text(destPath)
187 } else {
188 builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath)
189 }
190 if ps.executable {
191 builder.Command().Text("chmod").Flag("a+x").Text(destPath)
192 }
193 }
194
195 builder.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800196 BuiltTool("soong_zip").
Jiyong Parkdda8f692020-11-09 18:38:48 +0900197 FlagWithOutput("-o ", zipOut).
198 FlagWithArg("-C ", dir.String()).
199 Flag("-L 0"). // no compression because this will be unzipped soon
200 FlagWithArg("-D ", dir.String())
201 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
202
Colin Crossf1a035e2020-11-16 17:32:30 -0800203 builder.Build("zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
Jiyong Parkdda8f692020-11-09 18:38:48 +0900204 return entries
205}
Colin Crossffe6b9d2020-12-01 15:40:06 -0800206
207// packagingSpecsDepSet is a thin type-safe wrapper around the generic depSet. It always uses
208// topological order.
209type packagingSpecsDepSet struct {
210 depSet
211}
212
213// newPackagingSpecsDepSet returns an immutable packagingSpecsDepSet with the given direct and
214// transitive contents.
215func newPackagingSpecsDepSet(direct []PackagingSpec, transitive []*packagingSpecsDepSet) *packagingSpecsDepSet {
216 return &packagingSpecsDepSet{*newDepSet(TOPOLOGICAL, direct, transitive)}
217}
218
219// ToList returns the packagingSpecsDepSet flattened to a list in topological order.
220func (d *packagingSpecsDepSet) ToList() []PackagingSpec {
221 if d == nil {
222 return nil
223 }
224 return d.depSet.ToList().([]PackagingSpec)
225}