blob: aac321933b35f53acdf1d58fd36abff44a3bfe05 [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 Park073ea552020-11-09 14:08:34 +090024// PackagingSpec abstracts a request to place a built artifact at a certain path in a package.
25// A 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 running
27// on a VM), or a zip archive for some of the host tools.
28type 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
61 // returns zip entries in it. This is expected to be called in GenerateAndroidBuildActions,
62 // 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
72 // Allows this module to skip missing dependencies. In most cases, this
73 // is not required, but for rare cases like when there's a dependency
74 // to a module which exists in certain repo checkouts, this is needed.
75 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
104// From deps and multilib.*.deps, select the dependencies that are for the given arch
105// deps is for the current archicture when this module is not configured for multi target.
106// When configured for multi target, deps is selected for each of the targets and is NOT
107// selected for the current architecture which would be Common.
108func (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) {
156 var supportedArches []string
157 for _, t := range p.getSupportedTargets(ctx) {
158 supportedArches = append(supportedArches, t.Arch.ArchType.String())
159 }
160 m := make(map[string]PackagingSpec)
161 ctx.WalkDeps(func(child Module, parent Module) bool {
162 // Don't track modules with unsupported arch
163 // TODO(jiyong): remove this when aosp/1501613 lands.
164 if !InList(child.Target().Arch.ArchType.String(), supportedArches) {
165 return false
166 }
167 for _, ps := range child.PackagingSpecs() {
168 if _, ok := m[ps.relPathInPackage]; !ok {
169 m[ps.relPathInPackage] = ps
170 }
171 }
172 return true
173 })
174
175 builder := NewRuleBuilder()
176
177 dir := PathForModuleOut(ctx, ".zip").OutputPath
178 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
179 builder.Command().Text("mkdir").Flag("-p").Text(dir.String())
180
181 seenDir := make(map[string]bool)
182 for _, k := range SortedStringKeys(m) {
183 ps := m[k]
184 destPath := dir.Join(ctx, ps.relPathInPackage).String()
185 destDir := filepath.Dir(destPath)
186 entries = append(entries, ps.relPathInPackage)
187 if _, ok := seenDir[destDir]; !ok {
188 seenDir[destDir] = true
189 builder.Command().Text("mkdir").Flag("-p").Text(destDir)
190 }
191 if ps.symlinkTarget == "" {
192 builder.Command().Text("cp").Input(ps.srcPath).Text(destPath)
193 } else {
194 builder.Command().Text("ln").Flag("-sf").Text(ps.symlinkTarget).Text(destPath)
195 }
196 if ps.executable {
197 builder.Command().Text("chmod").Flag("a+x").Text(destPath)
198 }
199 }
200
201 builder.Command().
202 BuiltTool(ctx, "soong_zip").
203 FlagWithOutput("-o ", zipOut).
204 FlagWithArg("-C ", dir.String()).
205 Flag("-L 0"). // no compression because this will be unzipped soon
206 FlagWithArg("-D ", dir.String())
207 builder.Command().Text("rm").Flag("-rf").Text(dir.String())
208
209 builder.Build(pctx, ctx, "zip_deps", fmt.Sprintf("Zipping deps for %s", ctx.ModuleName()))
210 return entries
211}