blob: 9f6c8ada92f94b44e6757bef90b0f95d151dc3a6 [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 The Android Open Source Project
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 apex
16
17import (
18 "fmt"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070019 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090020 "strings"
21
22 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070023 "android/soong/java"
Jiyong Park10e926b2020-07-16 21:38:56 +090024
Jaewoong Jungfa00c062020-05-14 14:15:24 -070025 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090026
27 "github.com/google/blueprint/proptools"
28)
29
Jaewoong Jungfa00c062020-05-14 14:15:24 -070030var (
31 extractMatchingApex = pctx.StaticRule(
32 "extractMatchingApex",
33 blueprint.RuleParams{
34 Command: `rm -rf "$out" && ` +
35 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
36 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
37 `${in}`,
38 CommandDeps: []string{"${extract_apks}"},
39 },
40 "abis", "allow-prereleased", "sdk-version")
41)
42
Jiyong Park10e926b2020-07-16 21:38:56 +090043type prebuilt interface {
44 isForceDisabled() bool
45 InstallFilename() string
46}
47
48type prebuiltCommon struct {
49 prebuilt android.Prebuilt
50 properties prebuiltCommonProperties
51}
52
53type prebuiltCommonProperties struct {
54 ForceDisable bool `blueprint:"mutated"`
55}
56
57func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
58 return &p.prebuilt
59}
60
61func (p *prebuiltCommon) isForceDisabled() bool {
62 return p.properties.ForceDisable
63}
64
65func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
66 // If the device is configured to use flattened APEX, force disable the prebuilt because
67 // the prebuilt is a non-flattened one.
68 forceDisable := ctx.Config().FlattenApex()
69
70 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
71 // to build the prebuilts themselves.
72 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
73
74 // Force disable the prebuilts when coverage is enabled.
75 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
76 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
77
78 // b/137216042 don't use prebuilts when address sanitizer is on
79 forceDisable = forceDisable || android.InList("address", ctx.Config().SanitizeDevice()) ||
80 android.InList("hwaddress", ctx.Config().SanitizeDevice())
81
82 if forceDisable && p.prebuilt.SourceExists() {
83 p.properties.ForceDisable = true
84 return true
85 }
86 return false
87}
88
Jiyong Park09d77522019-11-18 11:16:27 +090089type Prebuilt struct {
90 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +090091 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +090092
93 properties PrebuiltProperties
94
95 inputApex android.Path
96 installDir android.InstallPath
97 installFilename string
98 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +090099
100 // list of commands to create symlinks for backward compatibility.
101 // these commands will be attached as LOCAL_POST_INSTALL_CMD
102 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900103}
104
105type PrebuiltProperties struct {
106 // the path to the prebuilt .apex file to import.
Jiyong Park10e926b2020-07-16 21:38:56 +0900107 Source string `blueprint:"mutated"`
Jiyong Park09d77522019-11-18 11:16:27 +0900108
109 Src *string
110 Arch struct {
111 Arm struct {
112 Src *string
113 }
114 Arm64 struct {
115 Src *string
116 }
117 X86 struct {
118 Src *string
119 }
120 X86_64 struct {
121 Src *string
122 }
123 }
124
125 Installable *bool
126 // Optional name for the installed apex. If unspecified, name of the
127 // module is used as the file name
128 Filename *string
129
130 // Names of modules to be overridden. Listed modules can only be other binaries
131 // (in Make or Soong).
132 // This does not completely prevent installation of the overridden binaries, but if both
133 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
134 // from PRODUCT_PACKAGES.
135 Overrides []string
136}
137
138func (p *Prebuilt) installable() bool {
139 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
140}
141
Jiyong Park09d77522019-11-18 11:16:27 +0900142func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
143 switch tag {
144 case "":
145 return android.Paths{p.outputApex}, nil
146 default:
147 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
148 }
149}
150
151func (p *Prebuilt) InstallFilename() string {
152 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
153}
154
Jiyong Park09d77522019-11-18 11:16:27 +0900155func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900156 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900157}
158
159// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
160func PrebuiltFactory() android.Module {
161 module := &Prebuilt{}
162 module.AddProperties(&module.properties)
163 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
164 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
165 return module
166}
167
168func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900169 // This is called before prebuilt_select and prebuilt_postdeps mutators
170 // The mutators requires that src to be set correctly for each arch so that
171 // arch variants are disabled when src is not provided for the arch.
172 if len(ctx.MultiTargets()) != 1 {
173 ctx.ModuleErrorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
174 return
175 }
176 var src string
177 switch ctx.MultiTargets()[0].Arch.ArchType {
178 case android.Arm:
179 src = String(p.properties.Arch.Arm.Src)
180 case android.Arm64:
181 src = String(p.properties.Arch.Arm64.Src)
182 case android.X86:
183 src = String(p.properties.Arch.X86.Src)
184 case android.X86_64:
185 src = String(p.properties.Arch.X86_64.Src)
186 default:
187 ctx.ModuleErrorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
188 return
189 }
190 if src == "" {
191 src = String(p.properties.Src)
192 }
193 p.properties.Source = src
194}
195
196func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900197 // TODO(jungjw): Check the key validity.
198 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
199 p.installDir = android.PathForModuleInstall(ctx, "apex")
200 p.installFilename = p.InstallFilename()
201 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
202 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
203 }
204 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
205 ctx.Build(pctx, android.BuildParams{
206 Rule: android.Cp,
207 Input: p.inputApex,
208 Output: p.outputApex,
209 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900210
211 if p.prebuiltCommon.checkForceDisable(ctx) {
212 p.SkipInstall()
213 return
214 }
215
Jiyong Park09d77522019-11-18 11:16:27 +0900216 if p.installable() {
217 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
218 }
219
Jooyung Han002ab682020-01-08 01:57:58 +0900220 // in case that prebuilt_apex replaces source apex (using prefer: prop)
221 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
222 // or that prebuilt_apex overrides other apexes (using overrides: prop)
223 for _, overridden := range p.properties.Overrides {
224 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
225 }
Jiyong Park09d77522019-11-18 11:16:27 +0900226}
227
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900228func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
229 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900230 Class: "ETC",
231 OutputFile: android.OptionalPathForPath(p.inputApex),
232 Include: "$(BUILD_PREBUILT)",
233 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
234 func(entries *android.AndroidMkEntries) {
235 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
236 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
237 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
238 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900239 if len(p.compatSymlinks) > 0 {
240 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
241 }
Jiyong Park09d77522019-11-18 11:16:27 +0900242 },
243 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900244 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900245}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700246
247type ApexSet struct {
248 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900249 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700250
251 properties ApexSetProperties
252
253 installDir android.InstallPath
254 installFilename string
255 outputApex android.WritablePath
256
257 // list of commands to create symlinks for backward compatibility.
258 // these commands will be attached as LOCAL_POST_INSTALL_CMD
259 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900260
261 hostRequired []string
262 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700263}
264
265type ApexSetProperties struct {
266 // the .apks file path that contains prebuilt apex files to be extracted.
267 Set *string
268
269 // whether the extracted apex file installable.
270 Installable *bool
271
272 // optional name for the installed apex. If unspecified, name of the
273 // module is used as the file name
274 Filename *string
275
276 // names of modules to be overridden. Listed modules can only be other binaries
277 // (in Make or Soong).
278 // This does not completely prevent installation of the overridden binaries, but if both
279 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
280 // from PRODUCT_PACKAGES.
281 Overrides []string
282
283 // apexes in this set use prerelease SDK version
284 Prerelease *bool
285}
286
287func (a *ApexSet) installable() bool {
288 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
289}
290
291func (a *ApexSet) InstallFilename() string {
292 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
293}
294
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700295func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900296 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700297}
298
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900299func (a *ApexSet) Overrides() []string {
300 return a.properties.Overrides
301}
302
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700303// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
304func apexSetFactory() android.Module {
305 module := &ApexSet{}
306 module.AddProperties(&module.properties)
307 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Set")
308 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
309 return module
310}
311
312func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
313 a.installFilename = a.InstallFilename()
314 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
315 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
316 }
317
Jiyong Park10e926b2020-07-16 21:38:56 +0900318 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700319 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
320 ctx.Build(pctx,
321 android.BuildParams{
322 Rule: extractMatchingApex,
323 Description: "Extract an apex from an apex set",
324 Inputs: android.Paths{apexSet},
325 Output: a.outputApex,
326 Args: map[string]string{
327 "abis": strings.Join(java.SupportedAbis(ctx), ","),
328 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700329 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700330 },
331 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900332
333 if a.prebuiltCommon.checkForceDisable(ctx) {
334 a.SkipInstall()
335 return
336 }
337
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700338 a.installDir = android.PathForModuleInstall(ctx, "apex")
339 if a.installable() {
340 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
341 }
342
343 // in case that apex_set replaces source apex (using prefer: prop)
344 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
345 // or that apex_set overrides other apexes (using overrides: prop)
346 for _, overridden := range a.properties.Overrides {
347 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
348 }
Jooyung Han29637162020-06-30 06:34:23 +0900349
350 if ctx.Config().InstallExtraFlattenedApexes() {
351 // flattened apex should be in /system_ext/apex
352 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
353 a.postInstallCommands = append(a.postInstallCommands,
354 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
355 a.outputApex.String(),
356 flattenedApexDir.ToMakePath().String(),
357 ))
358 a.hostRequired = []string{"deapexer", "debugfs"}
359 }
360}
361
362type systemExtContext struct {
363 android.ModuleContext
364}
365
366func (*systemExtContext) SystemExtSpecific() bool {
367 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700368}
369
370func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
371 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900372 Class: "ETC",
373 OutputFile: android.OptionalPathForPath(a.outputApex),
374 Include: "$(BUILD_PREBUILT)",
375 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700376 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
377 func(entries *android.AndroidMkEntries) {
378 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
379 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
380 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
381 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900382 postInstallCommands := append([]string{}, a.postInstallCommands...)
383 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
384 if len(postInstallCommands) > 0 {
385 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700386 }
387 },
388 },
389 }}
390}