blob: 8996352d92d9319fe2e9c38962b839273a39f114 [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
Paul Duffindfd33262021-04-06 17:02:08 +010051
52 deapexerProperties DeapexerProperties
53 selectedApexProperties SelectedApexProperties
Jiyong Park10e926b2020-07-16 21:38:56 +090054}
55
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070056type sanitizedPrebuilt interface {
57 hasSanitizedSource(sanitizer string) bool
58}
59
Jiyong Park10e926b2020-07-16 21:38:56 +090060type prebuiltCommonProperties struct {
61 ForceDisable bool `blueprint:"mutated"`
62}
63
64func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
65 return &p.prebuilt
66}
67
68func (p *prebuiltCommon) isForceDisabled() bool {
69 return p.properties.ForceDisable
70}
71
72func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
73 // If the device is configured to use flattened APEX, force disable the prebuilt because
74 // the prebuilt is a non-flattened one.
75 forceDisable := ctx.Config().FlattenApex()
76
77 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
78 // to build the prebuilts themselves.
79 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
80
81 // Force disable the prebuilts when coverage is enabled.
82 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
83 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
84
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070085 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
86 sanitized := ctx.Module().(sanitizedPrebuilt)
87 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
88 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090089
90 if forceDisable && p.prebuilt.SourceExists() {
91 p.properties.ForceDisable = true
92 return true
93 }
94 return false
95}
96
Paul Duffindfd33262021-04-06 17:02:08 +010097func (p *prebuiltCommon) deapexerDeps(ctx android.BottomUpMutatorContext) {
98 // Add dependencies onto the java modules that represent the java libraries that are provided by
99 // and exported from this prebuilt apex.
Paul Duffin023dba02021-04-22 01:45:29 +0100100 for _, exported := range p.deapexerProperties.Exported_java_libs {
101 dep := prebuiltApexExportedModuleName(ctx, exported)
Paul Duffindfd33262021-04-06 17:02:08 +0100102 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
103 }
Paul Duffin023dba02021-04-22 01:45:29 +0100104
105 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
106 // apex.
107 for _, exported := range p.deapexerProperties.Exported_bootclasspath_fragments {
108 dep := prebuiltApexExportedModuleName(ctx, exported)
109 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedBootclasspathFragmentTag, dep)
110 }
Paul Duffindfd33262021-04-06 17:02:08 +0100111}
112
Paul Duffinb17d0442021-05-05 12:07:00 +0100113// Implements android.DepInInSameApex
114func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
115 tag := ctx.OtherModuleDependencyTag(dep)
116 _, ok := tag.(exportedDependencyTag)
117 return ok
118}
119
Paul Duffindfd33262021-04-06 17:02:08 +0100120// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
121// specific variant and checks that they are supported.
122//
123// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
124// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
125//
126// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
127// across prebuilt_apex modules. That is because there is no way to determine whether two
128// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
129// been built from different source at different times or they could have been built with different
130// build options that affect the libraries.
131//
132// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
133// modules were compatible it would be a lot of work and would not provide much benefit for a couple
134// of reasons:
135// * The number of prebuilt_apex modules that will be exporting files for the same module will be
136// low as the prebuilt_apex only exports files for the direct dependencies that require it and
137// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
138// few com.android.art* apex files that contain the same contents and could export files for the
139// same modules but only one of them needs to do so. Contrast that with source apex modules which
140// need apex specific variants for every module that contributes code to the apex, whether direct
141// or indirect.
142// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
143// extra copying of files. Contrast that with source apex modules that has to build each variant
144// from source.
145func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
146
147 // Collect direct dependencies into contents.
148 contents := make(map[string]android.ApexMembership)
149
150 // Collect the list of dependencies.
151 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100152 mctx.WalkDeps(func(child, parent android.Module) bool {
153 // If the child is not in the same apex as the parent then exit immediately and do not visit
154 // any of the child's dependencies.
155 if !android.IsDepInSameApex(mctx, parent, child) {
156 return false
157 }
158
159 tag := mctx.OtherModuleDependencyTag(child)
160 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100161 if exportedTag, ok := tag.(exportedDependencyTag); ok {
162 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100163
164 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100165 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100166 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100167 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100168 }
169
170 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100171 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100172 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100173 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100174 }
Paul Duffindfd33262021-04-06 17:02:08 +0100175 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100176
177 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
178 // behavior whether there is a corresponding source module present or not.
179 depName = android.RemoveOptionalPrebuiltPrefix(depName)
180
181 // Remember if this module was added as a direct dependency.
182 direct := parent == mctx.Module()
183 contents[depName] = contents[depName].Add(direct)
184
185 // Add the module to the list of dependencies that need to have an APEX variant.
186 dependencies = append(dependencies, child.(android.ApexModule))
187
188 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100189 })
190
191 // Create contents for the prebuilt_apex and store it away for later use.
192 apexContents := android.NewApexContents(contents)
193 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
194 Contents: apexContents,
195 })
196
197 // Create an ApexInfo for the prebuilt_apex.
198 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100199 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Paul Duffindfd33262021-04-06 17:02:08 +0100200 InApexes: []string{mctx.ModuleName()},
201 ApexContents: []*android.ApexContents{apexContents},
202 ForPrebuiltApex: true,
203 }
204
205 // Mark the dependencies of this module as requiring a variant for this module.
206 for _, am := range dependencies {
207 am.BuildForApex(apexInfo)
208 }
209}
210
Paul Duffin11216db2021-03-01 14:14:52 +0000211// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
212// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
213// deapexer.
214type prebuiltApexSelectorModule struct {
215 android.ModuleBase
216
217 apexFileProperties ApexFileProperties
218
219 inputApex android.Path
220}
221
222func privateApexSelectorModuleFactory() android.Module {
223 module := &prebuiltApexSelectorModule{}
224 module.AddProperties(
225 &module.apexFileProperties,
226 )
227 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
228 return module
229}
230
231func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
232 return android.Paths{p.inputApex}
233}
234
235func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
236 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
237}
238
Jiyong Park09d77522019-11-18 11:16:27 +0900239type Prebuilt struct {
240 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900241 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900242
Paul Duffin11216db2021-03-01 14:14:52 +0000243 properties PrebuiltProperties
244 selectedApexProperties SelectedApexProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900245
246 inputApex android.Path
247 installDir android.InstallPath
248 installFilename string
249 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900250
251 // list of commands to create symlinks for backward compatibility.
252 // these commands will be attached as LOCAL_POST_INSTALL_CMD
253 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900254}
255
Paul Duffin851f3992021-01-13 17:03:51 +0000256type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900257 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000258 //
259 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
260 // for android_common. That is so that it will have the same arch variant as, and so be compatible
261 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000262 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900263 Arch struct {
264 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000265 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900266 }
267 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000268 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900269 }
270 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000271 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900272 }
273 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000274 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900275 }
276 }
Paul Duffin851f3992021-01-13 17:03:51 +0000277}
278
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000279// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
280//
281// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
282// to use methods on it that are specific to the current module.
283//
284// See the ApexFileProperties.Src property.
285func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
286 multiTargets := prebuilt.MultiTargets()
287 if len(multiTargets) != 1 {
288 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
289 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000290 }
291 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000292 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000293 case android.Arm:
294 src = String(p.Arch.Arm.Src)
295 case android.Arm64:
296 src = String(p.Arch.Arm64.Src)
297 case android.X86:
298 src = String(p.Arch.X86.Src)
299 case android.X86_64:
300 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000301 }
302 if src == "" {
303 src = String(p.Src)
304 }
Paul Duffin851f3992021-01-13 17:03:51 +0000305
Paul Duffinc0609c62021-03-01 17:27:16 +0000306 if src == "" {
307 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
308 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
309 // logic from reporting a more general, less useful message.
310 }
311
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000312 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000313}
314
315type PrebuiltProperties struct {
316 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900317
318 Installable *bool
319 // Optional name for the installed apex. If unspecified, name of the
320 // module is used as the file name
321 Filename *string
322
323 // Names of modules to be overridden. Listed modules can only be other binaries
324 // (in Make or Soong).
325 // This does not completely prevent installation of the overridden binaries, but if both
326 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
327 // from PRODUCT_PACKAGES.
328 Overrides []string
329}
330
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700331func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
332 return false
333}
334
Jiyong Park09d77522019-11-18 11:16:27 +0900335func (p *Prebuilt) installable() bool {
336 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
337}
338
Jiyong Park09d77522019-11-18 11:16:27 +0900339func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
340 switch tag {
341 case "":
342 return android.Paths{p.outputApex}, nil
343 default:
344 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
345 }
346}
347
348func (p *Prebuilt) InstallFilename() string {
349 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
350}
351
Jiyong Park09d77522019-11-18 11:16:27 +0900352func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900353 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900354}
355
356// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000357//
358// If this needs to make files from within a `.apex` file available for use by other Soong modules,
359// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
360// it does so as follows:
361//
362// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
363// makes them available for use by other modules, at both Soong and ninja levels.
364//
365// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
366// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
367// dexpreopt, will work the same way from source and prebuilt.
368//
369// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
370// itself so that they can retrieve the file paths to those files.
371//
Paul Duffin11216db2021-03-01 14:14:52 +0000372// It also creates a child module `selector` that is responsible for selecting the appropriate
373// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
374// 1. To dedup the selection logic so it only runs in one module.
375// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
376// `apex_set`.
377//
378// prebuilt_apex
379// / | \
380// / | \
381// V | V
382// selector <--- deapexer <--- exported java lib
383//
Jiyong Park09d77522019-11-18 11:16:27 +0900384func PrebuiltFactory() android.Module {
385 module := &Prebuilt{}
Paul Duffindfd33262021-04-06 17:02:08 +0100386 module.AddProperties(&module.properties, &module.deapexerProperties, &module.selectedApexProperties)
Paul Duffin11216db2021-03-01 14:14:52 +0000387 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900388 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000389
390 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Paul Duffin11216db2021-03-01 14:14:52 +0000391 baseModuleName := module.BaseModuleName()
392
393 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
394 createApexSelectorModule(ctx, apexSelectorModuleName, &module.properties.ApexFileProperties)
395
396 apexFileSource := ":" + apexSelectorModuleName
Paul Duffindfd33262021-04-06 17:02:08 +0100397 if len(module.deapexerProperties.Exported_java_libs) != 0 {
398 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000399 }
Paul Duffin11216db2021-03-01 14:14:52 +0000400
401 // Add a source reference to retrieve the selected apex from the selector module.
402 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin064b70c2020-11-02 17:32:38 +0000403 })
404
Jiyong Park09d77522019-11-18 11:16:27 +0900405 return module
406}
407
Paul Duffin11216db2021-03-01 14:14:52 +0000408func createApexSelectorModule(ctx android.LoadHookContext, name string, apexFileProperties *ApexFileProperties) {
409 props := struct {
410 Name *string
411 }{
412 Name: proptools.StringPtr(name),
413 }
414
415 ctx.CreateModule(privateApexSelectorModuleFactory,
416 &props,
417 apexFileProperties,
418 )
419}
420
421func createDeapexerModule(ctx android.LoadHookContext, deapexerName string, apexFileSource string, deapexerProperties *DeapexerProperties) {
422 props := struct {
423 Name *string
424 Selected_apex *string
425 }{
426 Name: proptools.StringPtr(deapexerName),
427 Selected_apex: proptools.StringPtr(apexFileSource),
428 }
429 ctx.CreateModule(privateDeapexerFactory,
430 &props,
431 deapexerProperties,
432 )
433}
434
435func deapexerModuleName(baseModuleName string) string {
436 return baseModuleName + ".deapexer"
437}
438
439func apexSelectorModuleName(baseModuleName string) string {
440 return baseModuleName + ".apex.selector"
441}
442
Paul Duffin064b70c2020-11-02 17:32:38 +0000443func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
444 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
445 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
446 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
447 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
448 // and not a renamed prebuilt module then that will be detected and reported as an error when
449 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100450 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000451 if ctx.OtherModuleExists(prebuiltName) {
452 name = prebuiltName
453 }
454 return name
455}
456
Paul Duffina7139422021-02-08 11:01:58 +0000457type exportedDependencyTag struct {
458 blueprint.BaseDependencyTag
459 name string
460}
461
462// Mark this tag so dependencies that use it are excluded from visibility enforcement.
463//
464// This does allow any prebuilt_apex to reference any module which does open up a small window for
465// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
466// avoids opening up a much bigger window by widening the visibility of modules that need files
467// provided by the prebuilt_apex to include all the possible locations they may be defined, which
468// could include everything below vendor/.
469//
470// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
471// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
472// the .apex file. It will also have to be included in the module's apex_available property too.
473// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
474// incorrectly.
475func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
476
477var (
Paul Duffin023dba02021-04-22 01:45:29 +0100478 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
479 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000480)
481
Liz Kammer356f7d42021-01-26 09:18:53 -0500482func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100483 p.deapexerDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000484}
485
486var _ ApexInfoMutator = (*Prebuilt)(nil)
487
Paul Duffin064b70c2020-11-02 17:32:38 +0000488func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100489 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900490}
491
492func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900493 // TODO(jungjw): Check the key validity.
Paul Duffin11216db2021-03-01 14:14:52 +0000494 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900495 p.installDir = android.PathForModuleInstall(ctx, "apex")
496 p.installFilename = p.InstallFilename()
497 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
498 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
499 }
500 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
501 ctx.Build(pctx, android.BuildParams{
502 Rule: android.Cp,
503 Input: p.inputApex,
504 Output: p.outputApex,
505 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900506
507 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800508 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900509 return
510 }
511
Jiyong Park09d77522019-11-18 11:16:27 +0900512 if p.installable() {
513 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
514 }
515
Jooyung Han002ab682020-01-08 01:57:58 +0900516 // in case that prebuilt_apex replaces source apex (using prefer: prop)
517 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
518 // or that prebuilt_apex overrides other apexes (using overrides: prop)
519 for _, overridden := range p.properties.Overrides {
520 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
521 }
Jiyong Park09d77522019-11-18 11:16:27 +0900522}
523
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900524func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
525 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900526 Class: "ETC",
527 OutputFile: android.OptionalPathForPath(p.inputApex),
528 Include: "$(BUILD_PREBUILT)",
529 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700530 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900531 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
532 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
533 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
534 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900535 if len(p.compatSymlinks) > 0 {
536 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
537 }
Jiyong Park09d77522019-11-18 11:16:27 +0900538 },
539 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900540 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900541}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700542
Paul Duffin24704672021-04-06 16:09:30 +0100543// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
544// module. It extracts the correct apex to use and makes it available for use by apex_set.
545type prebuiltApexExtractorModule struct {
546 android.ModuleBase
547
548 properties ApexExtractorProperties
549
550 extractedApex android.WritablePath
551}
552
553func privateApexExtractorModuleFactory() android.Module {
554 module := &prebuiltApexExtractorModule{}
555 module.AddProperties(
556 &module.properties,
557 )
558 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
559 return module
560}
561
562func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
563 return android.Paths{p.extractedApex}
564}
565
566func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
567 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
568 return p.properties.prebuiltSrcs(ctx)
569 }
570 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
571 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
572 ctx.Build(pctx,
573 android.BuildParams{
574 Rule: extractMatchingApex,
575 Description: "Extract an apex from an apex set",
576 Inputs: android.Paths{apexSet},
577 Output: p.extractedApex,
578 Args: map[string]string{
579 "abis": strings.Join(java.SupportedAbis(ctx), ","),
580 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
581 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
582 },
583 })
584}
585
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700586type ApexSet struct {
587 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900588 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700589
590 properties ApexSetProperties
591
592 installDir android.InstallPath
593 installFilename string
594 outputApex android.WritablePath
595
596 // list of commands to create symlinks for backward compatibility.
597 // these commands will be attached as LOCAL_POST_INSTALL_CMD
598 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900599
600 hostRequired []string
601 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700602}
603
Paul Duffin24704672021-04-06 16:09:30 +0100604type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700605 // the .apks file path that contains prebuilt apex files to be extracted.
606 Set *string
607
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700608 Sanitized struct {
609 None struct {
610 Set *string
611 }
612 Address struct {
613 Set *string
614 }
615 Hwaddress struct {
616 Set *string
617 }
618 }
619
Paul Duffin24704672021-04-06 16:09:30 +0100620 // apexes in this set use prerelease SDK version
621 Prerelease *bool
622}
623
624func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
625 var srcs []string
626 if e.Set != nil {
627 srcs = append(srcs, *e.Set)
628 }
629
630 var sanitizers []string
631 if ctx.Host() {
632 sanitizers = ctx.Config().SanitizeHost()
633 } else {
634 sanitizers = ctx.Config().SanitizeDevice()
635 }
636
637 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
638 srcs = append(srcs, *e.Sanitized.Address.Set)
639 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
640 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
641 } else if e.Sanitized.None.Set != nil {
642 srcs = append(srcs, *e.Sanitized.None.Set)
643 }
644
645 return srcs
646}
647
648type ApexSetProperties struct {
649 ApexExtractorProperties
650
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700651 // whether the extracted apex file installable.
652 Installable *bool
653
654 // optional name for the installed apex. If unspecified, name of the
655 // module is used as the file name
656 Filename *string
657
658 // names of modules to be overridden. Listed modules can only be other binaries
659 // (in Make or Soong).
660 // This does not completely prevent installation of the overridden binaries, but if both
661 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
662 // from PRODUCT_PACKAGES.
663 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700664}
665
666func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
667 if sanitizer == "address" {
668 return a.properties.Sanitized.Address.Set != nil
669 }
670 if sanitizer == "hwaddress" {
671 return a.properties.Sanitized.Hwaddress.Set != nil
672 }
673
674 return false
675}
676
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700677func (a *ApexSet) installable() bool {
678 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
679}
680
681func (a *ApexSet) InstallFilename() string {
682 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
683}
684
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700685func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900686 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700687}
688
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900689func (a *ApexSet) Overrides() []string {
690 return a.properties.Overrides
691}
692
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700693// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
694func apexSetFactory() android.Module {
695 module := &ApexSet{}
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100696 module.AddProperties(&module.properties, &module.selectedApexProperties, &module.deapexerProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700697
Paul Duffin24704672021-04-06 16:09:30 +0100698 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
699 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
700
701 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
702 baseModuleName := module.BaseModuleName()
703
704 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
705 createApexExtractorModule(ctx, apexExtractorModuleName, &module.properties.ApexExtractorProperties)
706
707 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100708 if len(module.deapexerProperties.Exported_java_libs) != 0 {
709 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
710 }
Paul Duffin24704672021-04-06 16:09:30 +0100711
712 // After passing the arch specific src properties to the creating the apex selector module
713 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
714 })
715
716 return module
717}
718
719func createApexExtractorModule(ctx android.LoadHookContext, name string, apexExtractorProperties *ApexExtractorProperties) {
720 props := struct {
721 Name *string
722 }{
723 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700724 }
725
Paul Duffin24704672021-04-06 16:09:30 +0100726 ctx.CreateModule(privateApexExtractorModuleFactory,
727 &props,
728 apexExtractorProperties,
729 )
730}
731
732func apexExtractorModuleName(baseModuleName string) string {
733 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700734}
735
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100736func (a *ApexSet) DepsMutator(ctx android.BottomUpMutatorContext) {
737 a.deapexerDeps(ctx)
738}
739
740var _ ApexInfoMutator = (*ApexSet)(nil)
741
742func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
743 a.apexInfoMutator(mctx)
744}
745
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700746func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
747 a.installFilename = a.InstallFilename()
748 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
749 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
750 }
751
Paul Duffin24704672021-04-06 16:09:30 +0100752 inputApex := android.OptionalPathForModuleSrc(ctx, a.selectedApexProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700753 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100754 ctx.Build(pctx, android.BuildParams{
755 Rule: android.Cp,
756 Input: inputApex,
757 Output: a.outputApex,
758 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900759
760 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800761 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900762 return
763 }
764
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700765 a.installDir = android.PathForModuleInstall(ctx, "apex")
766 if a.installable() {
767 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
768 }
769
770 // in case that apex_set replaces source apex (using prefer: prop)
771 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
772 // or that apex_set overrides other apexes (using overrides: prop)
773 for _, overridden := range a.properties.Overrides {
774 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
775 }
Jooyung Han29637162020-06-30 06:34:23 +0900776
777 if ctx.Config().InstallExtraFlattenedApexes() {
778 // flattened apex should be in /system_ext/apex
779 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
780 a.postInstallCommands = append(a.postInstallCommands,
781 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
782 a.outputApex.String(),
783 flattenedApexDir.ToMakePath().String(),
784 ))
785 a.hostRequired = []string{"deapexer", "debugfs"}
786 }
787}
788
789type systemExtContext struct {
790 android.ModuleContext
791}
792
793func (*systemExtContext) SystemExtSpecific() bool {
794 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700795}
796
797func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
798 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900799 Class: "ETC",
800 OutputFile: android.OptionalPathForPath(a.outputApex),
801 Include: "$(BUILD_PREBUILT)",
802 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700803 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700804 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700805 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
806 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
807 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
808 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900809 postInstallCommands := append([]string{}, a.postInstallCommands...)
810 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
811 if len(postInstallCommands) > 0 {
812 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700813 }
814 },
815 },
816 }}
817}