blob: 92694c986e4c73955c86143189fdaa588c437ee9 [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 {
Paul Duffinbb0dc132021-05-05 16:58:08 +010049 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010050
Paul Duffinbb0dc132021-05-05 16:58:08 +010051 // Properties common to both prebuilt_apex and apex_set.
52 prebuiltCommonProperties prebuiltCommonProperties
Jiyong Park10e926b2020-07-16 21:38:56 +090053}
54
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070055type sanitizedPrebuilt interface {
56 hasSanitizedSource(sanitizer string) bool
57}
58
Jiyong Park10e926b2020-07-16 21:38:56 +090059type prebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010060 DeapexerProperties
61 SelectedApexProperties
62
Jiyong Park10e926b2020-07-16 21:38:56 +090063 ForceDisable bool `blueprint:"mutated"`
64}
65
66func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
67 return &p.prebuilt
68}
69
70func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +010071 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +090072}
73
74func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
75 // If the device is configured to use flattened APEX, force disable the prebuilt because
76 // the prebuilt is a non-flattened one.
77 forceDisable := ctx.Config().FlattenApex()
78
79 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
80 // to build the prebuilts themselves.
81 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
82
83 // Force disable the prebuilts when coverage is enabled.
84 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
85 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
86
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070087 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
88 sanitized := ctx.Module().(sanitizedPrebuilt)
89 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
90 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090091
92 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +010093 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +090094 return true
95 }
96 return false
97}
98
Paul Duffin5dda3e32021-05-05 14:13:27 +010099// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
100// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
101type prebuiltApexModuleCreator interface {
102 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
103}
104
105// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
106// prebuiltApexModuleCreator's createPrebuiltApexModules method.
107//
108// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
109// will need to access dependencies added by that (exported modules) but must run before the
110// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
111// exported modules.
112func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
113 module := ctx.Module()
114 if creator, ok := module.(prebuiltApexModuleCreator); ok {
115 creator.createPrebuiltApexModules(ctx)
116 }
117}
118
Paul Duffin57f83592021-05-05 15:09:44 +0100119// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
120func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
121 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100122 // Add dependencies onto the java modules that represent the java libraries that are provided by
123 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100124 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100125 dep := android.PrebuiltNameFromSource(exported)
126 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100127 }
Paul Duffin023dba02021-04-22 01:45:29 +0100128
129 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
130 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100131 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100132 dep := android.PrebuiltNameFromSource(exported)
133 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100134 }
Paul Duffindfd33262021-04-06 17:02:08 +0100135}
136
Paul Duffinb17d0442021-05-05 12:07:00 +0100137// Implements android.DepInInSameApex
138func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
139 tag := ctx.OtherModuleDependencyTag(dep)
140 _, ok := tag.(exportedDependencyTag)
141 return ok
142}
143
Paul Duffindfd33262021-04-06 17:02:08 +0100144// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
145// specific variant and checks that they are supported.
146//
147// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
148// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
149//
150// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
151// across prebuilt_apex modules. That is because there is no way to determine whether two
152// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
153// been built from different source at different times or they could have been built with different
154// build options that affect the libraries.
155//
156// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
157// modules were compatible it would be a lot of work and would not provide much benefit for a couple
158// of reasons:
159// * The number of prebuilt_apex modules that will be exporting files for the same module will be
160// low as the prebuilt_apex only exports files for the direct dependencies that require it and
161// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
162// few com.android.art* apex files that contain the same contents and could export files for the
163// same modules but only one of them needs to do so. Contrast that with source apex modules which
164// need apex specific variants for every module that contributes code to the apex, whether direct
165// or indirect.
166// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
167// extra copying of files. Contrast that with source apex modules that has to build each variant
168// from source.
169func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
170
171 // Collect direct dependencies into contents.
172 contents := make(map[string]android.ApexMembership)
173
174 // Collect the list of dependencies.
175 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100176 mctx.WalkDeps(func(child, parent android.Module) bool {
177 // If the child is not in the same apex as the parent then exit immediately and do not visit
178 // any of the child's dependencies.
179 if !android.IsDepInSameApex(mctx, parent, child) {
180 return false
181 }
182
183 tag := mctx.OtherModuleDependencyTag(child)
184 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100185 if exportedTag, ok := tag.(exportedDependencyTag); ok {
186 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100187
188 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100189 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100190 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100191 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100192 }
193
194 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100195 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100196 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100197 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100198 }
Paul Duffindfd33262021-04-06 17:02:08 +0100199 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100200
201 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
202 // behavior whether there is a corresponding source module present or not.
203 depName = android.RemoveOptionalPrebuiltPrefix(depName)
204
205 // Remember if this module was added as a direct dependency.
206 direct := parent == mctx.Module()
207 contents[depName] = contents[depName].Add(direct)
208
209 // Add the module to the list of dependencies that need to have an APEX variant.
210 dependencies = append(dependencies, child.(android.ApexModule))
211
212 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100213 })
214
215 // Create contents for the prebuilt_apex and store it away for later use.
216 apexContents := android.NewApexContents(contents)
217 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
218 Contents: apexContents,
219 })
220
221 // Create an ApexInfo for the prebuilt_apex.
222 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100223 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Paul Duffindfd33262021-04-06 17:02:08 +0100224 InApexes: []string{mctx.ModuleName()},
225 ApexContents: []*android.ApexContents{apexContents},
226 ForPrebuiltApex: true,
227 }
228
229 // Mark the dependencies of this module as requiring a variant for this module.
230 for _, am := range dependencies {
231 am.BuildForApex(apexInfo)
232 }
233}
234
Paul Duffin11216db2021-03-01 14:14:52 +0000235// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
236// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
237// deapexer.
238type prebuiltApexSelectorModule struct {
239 android.ModuleBase
240
241 apexFileProperties ApexFileProperties
242
243 inputApex android.Path
244}
245
246func privateApexSelectorModuleFactory() android.Module {
247 module := &prebuiltApexSelectorModule{}
248 module.AddProperties(
249 &module.apexFileProperties,
250 )
251 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
252 return module
253}
254
255func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
256 return android.Paths{p.inputApex}
257}
258
259func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
260 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
261}
262
Jiyong Park09d77522019-11-18 11:16:27 +0900263type Prebuilt struct {
264 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900265 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900266
Paul Duffinbb0dc132021-05-05 16:58:08 +0100267 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900268
269 inputApex android.Path
270 installDir android.InstallPath
271 installFilename string
272 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900273
274 // list of commands to create symlinks for backward compatibility.
275 // these commands will be attached as LOCAL_POST_INSTALL_CMD
276 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900277}
278
Paul Duffin851f3992021-01-13 17:03:51 +0000279type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900280 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000281 //
282 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
283 // for android_common. That is so that it will have the same arch variant as, and so be compatible
284 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000285 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900286 Arch struct {
287 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000288 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900289 }
290 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000291 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900292 }
293 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000294 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900295 }
296 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000297 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900298 }
299 }
Paul Duffin851f3992021-01-13 17:03:51 +0000300}
301
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000302// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
303//
304// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
305// to use methods on it that are specific to the current module.
306//
307// See the ApexFileProperties.Src property.
308func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
309 multiTargets := prebuilt.MultiTargets()
310 if len(multiTargets) != 1 {
311 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
312 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000313 }
314 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000315 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000316 case android.Arm:
317 src = String(p.Arch.Arm.Src)
318 case android.Arm64:
319 src = String(p.Arch.Arm64.Src)
320 case android.X86:
321 src = String(p.Arch.X86.Src)
322 case android.X86_64:
323 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000324 }
325 if src == "" {
326 src = String(p.Src)
327 }
Paul Duffin851f3992021-01-13 17:03:51 +0000328
Paul Duffinc0609c62021-03-01 17:27:16 +0000329 if src == "" {
330 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
331 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
332 // logic from reporting a more general, less useful message.
333 }
334
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000335 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000336}
337
338type PrebuiltProperties struct {
339 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900340
341 Installable *bool
342 // Optional name for the installed apex. If unspecified, name of the
343 // module is used as the file name
344 Filename *string
345
346 // Names of modules to be overridden. Listed modules can only be other binaries
347 // (in Make or Soong).
348 // This does not completely prevent installation of the overridden binaries, but if both
349 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
350 // from PRODUCT_PACKAGES.
351 Overrides []string
352}
353
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700354func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
355 return false
356}
357
Jiyong Park09d77522019-11-18 11:16:27 +0900358func (p *Prebuilt) installable() bool {
359 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
360}
361
Jiyong Park09d77522019-11-18 11:16:27 +0900362func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
363 switch tag {
364 case "":
365 return android.Paths{p.outputApex}, nil
366 default:
367 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
368 }
369}
370
371func (p *Prebuilt) InstallFilename() string {
372 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
373}
374
Jiyong Park09d77522019-11-18 11:16:27 +0900375func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900376 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900377}
378
379// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
380func PrebuiltFactory() android.Module {
381 module := &Prebuilt{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100382 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
383 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900384 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000385
Jiyong Park09d77522019-11-18 11:16:27 +0900386 return module
387}
388
Paul Duffin5dda3e32021-05-05 14:13:27 +0100389func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000390 props := struct {
391 Name *string
392 }{
393 Name: proptools.StringPtr(name),
394 }
395
396 ctx.CreateModule(privateApexSelectorModuleFactory,
397 &props,
398 apexFileProperties,
399 )
400}
401
Paul Duffin5dda3e32021-05-05 14:13:27 +0100402// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
403//
Paul Duffin57f83592021-05-05 15:09:44 +0100404// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
405// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
406// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100407func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *prebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100408 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100409 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100410 return
411 }
412
Paul Duffin57f83592021-05-05 15:09:44 +0100413 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100414 deapexerProperties := &DeapexerProperties{}
Paul Duffin57f83592021-05-05 15:09:44 +0100415 ctx.WalkDeps(func(child, parent android.Module) bool {
416 tag := ctx.OtherModuleDependencyTag(child)
417
418 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
419 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
420 deapexerProperties.Exported_java_libs = append(deapexerProperties.Exported_java_libs, name)
421 } else if tag == exportedBootclasspathFragmentTag {
422 deapexerProperties.Exported_bootclasspath_fragments = append(deapexerProperties.Exported_bootclasspath_fragments, name)
423 // Only visit the children of the bootclasspath_fragment for now.
424 return true
425 }
426
427 return false
428 })
429
430 // Remove any duplicates from the deapexer lists.
431 deapexerProperties.Exported_bootclasspath_fragments = android.FirstUniqueStrings(deapexerProperties.Exported_bootclasspath_fragments)
432 deapexerProperties.Exported_java_libs = android.FirstUniqueStrings(deapexerProperties.Exported_java_libs)
433
Paul Duffin11216db2021-03-01 14:14:52 +0000434 props := struct {
435 Name *string
436 Selected_apex *string
437 }{
438 Name: proptools.StringPtr(deapexerName),
439 Selected_apex: proptools.StringPtr(apexFileSource),
440 }
441 ctx.CreateModule(privateDeapexerFactory,
442 &props,
443 deapexerProperties,
444 )
445}
446
447func deapexerModuleName(baseModuleName string) string {
448 return baseModuleName + ".deapexer"
449}
450
451func apexSelectorModuleName(baseModuleName string) string {
452 return baseModuleName + ".apex.selector"
453}
454
Paul Duffin064b70c2020-11-02 17:32:38 +0000455func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
456 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
457 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
458 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
459 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
460 // and not a renamed prebuilt module then that will be detected and reported as an error when
461 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100462 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000463 if ctx.OtherModuleExists(prebuiltName) {
464 name = prebuiltName
465 }
466 return name
467}
468
Paul Duffina7139422021-02-08 11:01:58 +0000469type exportedDependencyTag struct {
470 blueprint.BaseDependencyTag
471 name string
472}
473
474// Mark this tag so dependencies that use it are excluded from visibility enforcement.
475//
476// This does allow any prebuilt_apex to reference any module which does open up a small window for
477// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
478// avoids opening up a much bigger window by widening the visibility of modules that need files
479// provided by the prebuilt_apex to include all the possible locations they may be defined, which
480// could include everything below vendor/.
481//
482// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
483// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
484// the .apex file. It will also have to be included in the module's apex_available property too.
485// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
486// incorrectly.
487func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
488
489var (
Paul Duffin023dba02021-04-22 01:45:29 +0100490 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
491 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000492)
493
Paul Duffin5dda3e32021-05-05 14:13:27 +0100494var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
495
496// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
497// build.
498//
499// If this needs to make files from within a `.apex` file available for use by other Soong modules,
500// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
501// it does so as follows:
502//
503// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
504// makes them available for use by other modules, at both Soong and ninja levels.
505//
506// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
507// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
508// dexpreopt, will work the same way from source and prebuilt.
509//
510// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
511// itself so that they can retrieve the file paths to those files.
512//
513// It also creates a child module `selector` that is responsible for selecting the appropriate
514// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
515// 1. To dedup the selection logic so it only runs in one module.
516// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
517// `apex_set`.
518//
519// prebuilt_apex
520// / | \
521// / | \
522// V V V
523// selector <--- deapexer <--- exported java lib
524//
525func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
526 baseModuleName := p.BaseModuleName()
527
528 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
529 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
530
531 apexFileSource := ":" + apexSelectorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100532 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100533
534 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100535 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100536}
537
Paul Duffin57f83592021-05-05 15:09:44 +0100538func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
539 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000540}
541
542var _ ApexInfoMutator = (*Prebuilt)(nil)
543
Paul Duffin064b70c2020-11-02 17:32:38 +0000544func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100545 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900546}
547
548func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900549 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100550 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900551 p.installDir = android.PathForModuleInstall(ctx, "apex")
552 p.installFilename = p.InstallFilename()
553 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
554 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
555 }
556 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
557 ctx.Build(pctx, android.BuildParams{
558 Rule: android.Cp,
559 Input: p.inputApex,
560 Output: p.outputApex,
561 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900562
563 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800564 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900565 return
566 }
567
Jiyong Park09d77522019-11-18 11:16:27 +0900568 if p.installable() {
569 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
570 }
571
Jooyung Han002ab682020-01-08 01:57:58 +0900572 // in case that prebuilt_apex replaces source apex (using prefer: prop)
573 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
574 // or that prebuilt_apex overrides other apexes (using overrides: prop)
575 for _, overridden := range p.properties.Overrides {
576 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
577 }
Jiyong Park09d77522019-11-18 11:16:27 +0900578}
579
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900580func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
581 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900582 Class: "ETC",
583 OutputFile: android.OptionalPathForPath(p.inputApex),
584 Include: "$(BUILD_PREBUILT)",
585 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700586 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900587 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
588 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
589 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
590 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900591 if len(p.compatSymlinks) > 0 {
592 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
593 }
Jiyong Park09d77522019-11-18 11:16:27 +0900594 },
595 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900596 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900597}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700598
Paul Duffin24704672021-04-06 16:09:30 +0100599// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
600// module. It extracts the correct apex to use and makes it available for use by apex_set.
601type prebuiltApexExtractorModule struct {
602 android.ModuleBase
603
604 properties ApexExtractorProperties
605
606 extractedApex android.WritablePath
607}
608
609func privateApexExtractorModuleFactory() android.Module {
610 module := &prebuiltApexExtractorModule{}
611 module.AddProperties(
612 &module.properties,
613 )
614 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
615 return module
616}
617
618func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
619 return android.Paths{p.extractedApex}
620}
621
622func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
623 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
624 return p.properties.prebuiltSrcs(ctx)
625 }
626 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
627 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
628 ctx.Build(pctx,
629 android.BuildParams{
630 Rule: extractMatchingApex,
631 Description: "Extract an apex from an apex set",
632 Inputs: android.Paths{apexSet},
633 Output: p.extractedApex,
634 Args: map[string]string{
635 "abis": strings.Join(java.SupportedAbis(ctx), ","),
636 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
637 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
638 },
639 })
640}
641
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700642type ApexSet struct {
643 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900644 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700645
646 properties ApexSetProperties
647
648 installDir android.InstallPath
649 installFilename string
650 outputApex android.WritablePath
651
652 // list of commands to create symlinks for backward compatibility.
653 // these commands will be attached as LOCAL_POST_INSTALL_CMD
654 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900655
656 hostRequired []string
657 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700658}
659
Paul Duffin24704672021-04-06 16:09:30 +0100660type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700661 // the .apks file path that contains prebuilt apex files to be extracted.
662 Set *string
663
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700664 Sanitized struct {
665 None struct {
666 Set *string
667 }
668 Address struct {
669 Set *string
670 }
671 Hwaddress struct {
672 Set *string
673 }
674 }
675
Paul Duffin24704672021-04-06 16:09:30 +0100676 // apexes in this set use prerelease SDK version
677 Prerelease *bool
678}
679
680func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
681 var srcs []string
682 if e.Set != nil {
683 srcs = append(srcs, *e.Set)
684 }
685
686 var sanitizers []string
687 if ctx.Host() {
688 sanitizers = ctx.Config().SanitizeHost()
689 } else {
690 sanitizers = ctx.Config().SanitizeDevice()
691 }
692
693 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
694 srcs = append(srcs, *e.Sanitized.Address.Set)
695 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
696 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
697 } else if e.Sanitized.None.Set != nil {
698 srcs = append(srcs, *e.Sanitized.None.Set)
699 }
700
701 return srcs
702}
703
704type ApexSetProperties struct {
705 ApexExtractorProperties
706
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700707 // whether the extracted apex file installable.
708 Installable *bool
709
710 // optional name for the installed apex. If unspecified, name of the
711 // module is used as the file name
712 Filename *string
713
714 // names of modules to be overridden. Listed modules can only be other binaries
715 // (in Make or Soong).
716 // This does not completely prevent installation of the overridden binaries, but if both
717 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
718 // from PRODUCT_PACKAGES.
719 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700720}
721
722func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
723 if sanitizer == "address" {
724 return a.properties.Sanitized.Address.Set != nil
725 }
726 if sanitizer == "hwaddress" {
727 return a.properties.Sanitized.Hwaddress.Set != nil
728 }
729
730 return false
731}
732
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700733func (a *ApexSet) installable() bool {
734 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
735}
736
737func (a *ApexSet) InstallFilename() string {
738 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
739}
740
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700741func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900742 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700743}
744
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900745func (a *ApexSet) Overrides() []string {
746 return a.properties.Overrides
747}
748
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700749// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
750func apexSetFactory() android.Module {
751 module := &ApexSet{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100752 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700753
Paul Duffinbb0dc132021-05-05 16:58:08 +0100754 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Paul Duffin24704672021-04-06 16:09:30 +0100755 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
756
Paul Duffin24704672021-04-06 16:09:30 +0100757 return module
758}
759
Paul Duffin5dda3e32021-05-05 14:13:27 +0100760func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100761 props := struct {
762 Name *string
763 }{
764 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700765 }
766
Paul Duffin24704672021-04-06 16:09:30 +0100767 ctx.CreateModule(privateApexExtractorModuleFactory,
768 &props,
769 apexExtractorProperties,
770 )
771}
772
773func apexExtractorModuleName(baseModuleName string) string {
774 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700775}
776
Paul Duffin5dda3e32021-05-05 14:13:27 +0100777var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
778
779// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
780// modules.
781//
782// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
783// prebuilt_apex except that instead of creating a selector module which selects one .apex file
784// from those provided this creates an extractor module which extracts the appropriate .apex file
785// from the zip file containing them.
786func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
787 baseModuleName := a.BaseModuleName()
788
789 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
790 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
791
792 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100793 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100794
795 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100796 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100797}
798
Paul Duffin57f83592021-05-05 15:09:44 +0100799func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
800 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100801}
802
803var _ ApexInfoMutator = (*ApexSet)(nil)
804
805func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
806 a.apexInfoMutator(mctx)
807}
808
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700809func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
810 a.installFilename = a.InstallFilename()
811 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
812 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
813 }
814
Paul Duffinbb0dc132021-05-05 16:58:08 +0100815 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700816 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100817 ctx.Build(pctx, android.BuildParams{
818 Rule: android.Cp,
819 Input: inputApex,
820 Output: a.outputApex,
821 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900822
823 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800824 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900825 return
826 }
827
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700828 a.installDir = android.PathForModuleInstall(ctx, "apex")
829 if a.installable() {
830 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
831 }
832
833 // in case that apex_set replaces source apex (using prefer: prop)
834 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
835 // or that apex_set overrides other apexes (using overrides: prop)
836 for _, overridden := range a.properties.Overrides {
837 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
838 }
Jooyung Han29637162020-06-30 06:34:23 +0900839
840 if ctx.Config().InstallExtraFlattenedApexes() {
841 // flattened apex should be in /system_ext/apex
842 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
843 a.postInstallCommands = append(a.postInstallCommands,
844 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
845 a.outputApex.String(),
846 flattenedApexDir.ToMakePath().String(),
847 ))
848 a.hostRequired = []string{"deapexer", "debugfs"}
849 }
850}
851
852type systemExtContext struct {
853 android.ModuleContext
854}
855
856func (*systemExtContext) SystemExtSpecific() bool {
857 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700858}
859
860func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
861 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900862 Class: "ETC",
863 OutputFile: android.OptionalPathForPath(a.outputApex),
864 Include: "$(BUILD_PREBUILT)",
865 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700866 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700867 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700868 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
869 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
870 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
871 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900872 postInstallCommands := append([]string{}, a.postInstallCommands...)
873 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
874 if len(postInstallCommands) > 0 {
875 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700876 }
877 },
878 },
879 }}
880}