blob: bdd40795276d14918ebfac9d3fe8bde5f6e6b522 [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"
Paul Duffin3bae0682021-05-05 18:03:47 +010019 "path/filepath"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070020 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090021 "strings"
22
23 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070024 "android/soong/java"
25 "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 SelectedApexProperties
61
Jiyong Park10e926b2020-07-16 21:38:56 +090062 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010063
64 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
65 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
66 // dexpreopt and boot jars package check.
67 Exported_java_libs []string
68
69 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
70 // bundle will create an APEX variant.
71 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +090072}
73
74func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
75 return &p.prebuilt
76}
77
78func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +010079 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +090080}
81
82func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
83 // If the device is configured to use flattened APEX, force disable the prebuilt because
84 // the prebuilt is a non-flattened one.
85 forceDisable := ctx.Config().FlattenApex()
86
87 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
88 // to build the prebuilts themselves.
89 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
90
91 // Force disable the prebuilts when coverage is enabled.
92 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
93 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
94
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070095 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
96 sanitized := ctx.Module().(sanitizedPrebuilt)
97 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
98 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090099
100 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100101 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900102 return true
103 }
104 return false
105}
106
Paul Duffin5dda3e32021-05-05 14:13:27 +0100107// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
108// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
109type prebuiltApexModuleCreator interface {
110 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
111}
112
113// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
114// prebuiltApexModuleCreator's createPrebuiltApexModules method.
115//
116// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
117// will need to access dependencies added by that (exported modules) but must run before the
118// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
119// exported modules.
120func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
121 module := ctx.Module()
122 if creator, ok := module.(prebuiltApexModuleCreator); ok {
123 creator.createPrebuiltApexModules(ctx)
124 }
125}
126
Paul Duffin57f83592021-05-05 15:09:44 +0100127// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
128func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
129 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100130 // Add dependencies onto the java modules that represent the java libraries that are provided by
131 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100132 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100133 dep := android.PrebuiltNameFromSource(exported)
134 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100135 }
Paul Duffin023dba02021-04-22 01:45:29 +0100136
137 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
138 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100139 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100140 dep := android.PrebuiltNameFromSource(exported)
141 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100142 }
Paul Duffindfd33262021-04-06 17:02:08 +0100143}
144
Paul Duffinb17d0442021-05-05 12:07:00 +0100145// Implements android.DepInInSameApex
146func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
147 tag := ctx.OtherModuleDependencyTag(dep)
148 _, ok := tag.(exportedDependencyTag)
149 return ok
150}
151
Paul Duffindfd33262021-04-06 17:02:08 +0100152// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
153// specific variant and checks that they are supported.
154//
155// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
156// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
157//
158// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
159// across prebuilt_apex modules. That is because there is no way to determine whether two
160// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
161// been built from different source at different times or they could have been built with different
162// build options that affect the libraries.
163//
164// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
165// modules were compatible it would be a lot of work and would not provide much benefit for a couple
166// of reasons:
167// * The number of prebuilt_apex modules that will be exporting files for the same module will be
168// low as the prebuilt_apex only exports files for the direct dependencies that require it and
169// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
170// few com.android.art* apex files that contain the same contents and could export files for the
171// same modules but only one of them needs to do so. Contrast that with source apex modules which
172// need apex specific variants for every module that contributes code to the apex, whether direct
173// or indirect.
174// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
175// extra copying of files. Contrast that with source apex modules that has to build each variant
176// from source.
177func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
178
179 // Collect direct dependencies into contents.
180 contents := make(map[string]android.ApexMembership)
181
182 // Collect the list of dependencies.
183 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100184 mctx.WalkDeps(func(child, parent android.Module) bool {
185 // If the child is not in the same apex as the parent then exit immediately and do not visit
186 // any of the child's dependencies.
187 if !android.IsDepInSameApex(mctx, parent, child) {
188 return false
189 }
190
191 tag := mctx.OtherModuleDependencyTag(child)
192 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100193 if exportedTag, ok := tag.(exportedDependencyTag); ok {
194 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100195
196 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100197 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100198 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100199 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100200 }
201
202 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100203 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100204 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100205 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100206 }
Paul Duffindfd33262021-04-06 17:02:08 +0100207 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100208
209 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
210 // behavior whether there is a corresponding source module present or not.
211 depName = android.RemoveOptionalPrebuiltPrefix(depName)
212
213 // Remember if this module was added as a direct dependency.
214 direct := parent == mctx.Module()
215 contents[depName] = contents[depName].Add(direct)
216
217 // Add the module to the list of dependencies that need to have an APEX variant.
218 dependencies = append(dependencies, child.(android.ApexModule))
219
220 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100221 })
222
223 // Create contents for the prebuilt_apex and store it away for later use.
224 apexContents := android.NewApexContents(contents)
225 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
226 Contents: apexContents,
227 })
228
229 // Create an ApexInfo for the prebuilt_apex.
230 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100231 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Jiyong Park712e8b52021-05-12 17:13:56 +0900232 InApexVariants: []string{mctx.ModuleName()},
Jiyong Park59b9f142021-05-12 17:13:56 +0900233 InApexModules: []string{mctx.ModuleName()},
Paul Duffindfd33262021-04-06 17:02:08 +0100234 ApexContents: []*android.ApexContents{apexContents},
235 ForPrebuiltApex: true,
236 }
237
238 // Mark the dependencies of this module as requiring a variant for this module.
239 for _, am := range dependencies {
240 am.BuildForApex(apexInfo)
241 }
242}
243
Paul Duffin11216db2021-03-01 14:14:52 +0000244// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
245// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
246// deapexer.
247type prebuiltApexSelectorModule struct {
248 android.ModuleBase
249
250 apexFileProperties ApexFileProperties
251
252 inputApex android.Path
253}
254
255func privateApexSelectorModuleFactory() android.Module {
256 module := &prebuiltApexSelectorModule{}
257 module.AddProperties(
258 &module.apexFileProperties,
259 )
260 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
261 return module
262}
263
264func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
265 return android.Paths{p.inputApex}
266}
267
268func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
269 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
270}
271
Jiyong Park09d77522019-11-18 11:16:27 +0900272type Prebuilt struct {
273 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900274 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900275
Paul Duffinbb0dc132021-05-05 16:58:08 +0100276 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900277
278 inputApex android.Path
279 installDir android.InstallPath
280 installFilename string
281 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900282
283 // list of commands to create symlinks for backward compatibility.
284 // these commands will be attached as LOCAL_POST_INSTALL_CMD
285 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900286}
287
Paul Duffin851f3992021-01-13 17:03:51 +0000288type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900289 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000290 //
291 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
292 // for android_common. That is so that it will have the same arch variant as, and so be compatible
293 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000294 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900295 Arch struct {
296 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000297 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900298 }
299 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000300 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900301 }
302 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000303 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900304 }
305 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000306 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900307 }
308 }
Paul Duffin851f3992021-01-13 17:03:51 +0000309}
310
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000311// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
312//
313// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
314// to use methods on it that are specific to the current module.
315//
316// See the ApexFileProperties.Src property.
317func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
318 multiTargets := prebuilt.MultiTargets()
319 if len(multiTargets) != 1 {
320 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
321 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000322 }
323 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000324 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000325 case android.Arm:
326 src = String(p.Arch.Arm.Src)
327 case android.Arm64:
328 src = String(p.Arch.Arm64.Src)
329 case android.X86:
330 src = String(p.Arch.X86.Src)
331 case android.X86_64:
332 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000333 }
334 if src == "" {
335 src = String(p.Src)
336 }
Paul Duffin851f3992021-01-13 17:03:51 +0000337
Paul Duffinc0609c62021-03-01 17:27:16 +0000338 if src == "" {
339 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
340 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
341 // logic from reporting a more general, less useful message.
342 }
343
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000344 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000345}
346
347type PrebuiltProperties struct {
348 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900349
350 Installable *bool
351 // Optional name for the installed apex. If unspecified, name of the
352 // module is used as the file name
353 Filename *string
354
355 // Names of modules to be overridden. Listed modules can only be other binaries
356 // (in Make or Soong).
357 // This does not completely prevent installation of the overridden binaries, but if both
358 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
359 // from PRODUCT_PACKAGES.
360 Overrides []string
361}
362
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700363func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
364 return false
365}
366
Jiyong Park09d77522019-11-18 11:16:27 +0900367func (p *Prebuilt) installable() bool {
368 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
369}
370
Jiyong Park09d77522019-11-18 11:16:27 +0900371func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
372 switch tag {
373 case "":
374 return android.Paths{p.outputApex}, nil
375 default:
376 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
377 }
378}
379
380func (p *Prebuilt) InstallFilename() string {
381 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
382}
383
Jiyong Park09d77522019-11-18 11:16:27 +0900384func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900385 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900386}
387
388// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
389func PrebuiltFactory() android.Module {
390 module := &Prebuilt{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100391 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
392 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900393 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000394
Jiyong Park09d77522019-11-18 11:16:27 +0900395 return module
396}
397
Paul Duffin5dda3e32021-05-05 14:13:27 +0100398func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000399 props := struct {
400 Name *string
401 }{
402 Name: proptools.StringPtr(name),
403 }
404
405 ctx.CreateModule(privateApexSelectorModuleFactory,
406 &props,
407 apexFileProperties,
408 )
409}
410
Paul Duffin5dda3e32021-05-05 14:13:27 +0100411// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
412//
Paul Duffin57f83592021-05-05 15:09:44 +0100413// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
414// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
415// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100416func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *prebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100417 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100418 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100419 return
420 }
421
Paul Duffin57f83592021-05-05 15:09:44 +0100422 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin3bae0682021-05-05 18:03:47 +0100423 javaModules := []string{}
424 exportedFiles := map[string]string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100425 ctx.WalkDeps(func(child, parent android.Module) bool {
426 tag := ctx.OtherModuleDependencyTag(child)
427
428 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
429 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin3bae0682021-05-05 18:03:47 +0100430 javaModules = append(javaModules, name)
431
432 // Add the dex implementation jar to the set of exported files. The path here must match the
433 // path of the file in the APEX created by apexFileForJavaModule(...).
434 exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
435
Paul Duffin57f83592021-05-05 15:09:44 +0100436 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin57f83592021-05-05 15:09:44 +0100437 // Only visit the children of the bootclasspath_fragment for now.
438 return true
439 }
440
441 return false
442 })
443
Paul Duffin3bae0682021-05-05 18:03:47 +0100444 // Create properties for deapexer module.
445 deapexerProperties := &DeapexerProperties{
446 // Remove any duplicates from the java modules lists as a module may be included via a direct
447 // dependency as well as transitive ones.
448 CommonModules: android.SortedUniqueStrings(javaModules),
449 }
450
451 // Populate the exported files property in a fixed order.
452 for _, tag := range android.SortedStringKeys(exportedFiles) {
453 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
454 Tag: tag,
455 Path: exportedFiles[tag],
456 })
457 }
Paul Duffin57f83592021-05-05 15:09:44 +0100458
Paul Duffin11216db2021-03-01 14:14:52 +0000459 props := struct {
460 Name *string
461 Selected_apex *string
462 }{
463 Name: proptools.StringPtr(deapexerName),
464 Selected_apex: proptools.StringPtr(apexFileSource),
465 }
466 ctx.CreateModule(privateDeapexerFactory,
467 &props,
468 deapexerProperties,
469 )
470}
471
472func deapexerModuleName(baseModuleName string) string {
473 return baseModuleName + ".deapexer"
474}
475
476func apexSelectorModuleName(baseModuleName string) string {
477 return baseModuleName + ".apex.selector"
478}
479
Paul Duffin064b70c2020-11-02 17:32:38 +0000480func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
481 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
482 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
483 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
484 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
485 // and not a renamed prebuilt module then that will be detected and reported as an error when
486 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100487 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000488 if ctx.OtherModuleExists(prebuiltName) {
489 name = prebuiltName
490 }
491 return name
492}
493
Paul Duffina7139422021-02-08 11:01:58 +0000494type exportedDependencyTag struct {
495 blueprint.BaseDependencyTag
496 name string
497}
498
499// Mark this tag so dependencies that use it are excluded from visibility enforcement.
500//
501// This does allow any prebuilt_apex to reference any module which does open up a small window for
502// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
503// avoids opening up a much bigger window by widening the visibility of modules that need files
504// provided by the prebuilt_apex to include all the possible locations they may be defined, which
505// could include everything below vendor/.
506//
507// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
508// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
509// the .apex file. It will also have to be included in the module's apex_available property too.
510// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
511// incorrectly.
512func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
513
514var (
Paul Duffin023dba02021-04-22 01:45:29 +0100515 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
516 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000517)
518
Paul Duffin5dda3e32021-05-05 14:13:27 +0100519var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
520
521// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
522// build.
523//
524// If this needs to make files from within a `.apex` file available for use by other Soong modules,
525// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
526// it does so as follows:
527//
528// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
529// makes them available for use by other modules, at both Soong and ninja levels.
530//
531// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
532// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
533// dexpreopt, will work the same way from source and prebuilt.
534//
535// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
536// itself so that they can retrieve the file paths to those files.
537//
538// It also creates a child module `selector` that is responsible for selecting the appropriate
539// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
540// 1. To dedup the selection logic so it only runs in one module.
541// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
542// `apex_set`.
543//
544// prebuilt_apex
545// / | \
546// / | \
547// V V V
548// selector <--- deapexer <--- exported java lib
549//
550func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
551 baseModuleName := p.BaseModuleName()
552
553 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
554 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
555
556 apexFileSource := ":" + apexSelectorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100557 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100558
559 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100560 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100561}
562
Paul Duffin57f83592021-05-05 15:09:44 +0100563func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
564 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000565}
566
567var _ ApexInfoMutator = (*Prebuilt)(nil)
568
Paul Duffin064b70c2020-11-02 17:32:38 +0000569func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100570 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900571}
572
573func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900574 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100575 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900576 p.installDir = android.PathForModuleInstall(ctx, "apex")
577 p.installFilename = p.InstallFilename()
578 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
579 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
580 }
581 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
582 ctx.Build(pctx, android.BuildParams{
583 Rule: android.Cp,
584 Input: p.inputApex,
585 Output: p.outputApex,
586 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900587
588 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800589 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900590 return
591 }
592
Jiyong Park09d77522019-11-18 11:16:27 +0900593 if p.installable() {
594 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
595 }
596
Jooyung Han002ab682020-01-08 01:57:58 +0900597 // in case that prebuilt_apex replaces source apex (using prefer: prop)
598 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
599 // or that prebuilt_apex overrides other apexes (using overrides: prop)
600 for _, overridden := range p.properties.Overrides {
601 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
602 }
Jiyong Park09d77522019-11-18 11:16:27 +0900603}
604
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900605func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
606 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900607 Class: "ETC",
608 OutputFile: android.OptionalPathForPath(p.inputApex),
609 Include: "$(BUILD_PREBUILT)",
610 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700611 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900612 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
613 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
614 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
615 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900616 if len(p.compatSymlinks) > 0 {
617 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
618 }
Jiyong Park09d77522019-11-18 11:16:27 +0900619 },
620 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900621 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900622}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700623
Paul Duffin24704672021-04-06 16:09:30 +0100624// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
625// module. It extracts the correct apex to use and makes it available for use by apex_set.
626type prebuiltApexExtractorModule struct {
627 android.ModuleBase
628
629 properties ApexExtractorProperties
630
631 extractedApex android.WritablePath
632}
633
634func privateApexExtractorModuleFactory() android.Module {
635 module := &prebuiltApexExtractorModule{}
636 module.AddProperties(
637 &module.properties,
638 )
639 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
640 return module
641}
642
643func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
644 return android.Paths{p.extractedApex}
645}
646
647func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
648 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
649 return p.properties.prebuiltSrcs(ctx)
650 }
651 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
652 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
653 ctx.Build(pctx,
654 android.BuildParams{
655 Rule: extractMatchingApex,
656 Description: "Extract an apex from an apex set",
657 Inputs: android.Paths{apexSet},
658 Output: p.extractedApex,
659 Args: map[string]string{
660 "abis": strings.Join(java.SupportedAbis(ctx), ","),
661 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
662 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
663 },
664 })
665}
666
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700667type ApexSet struct {
668 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900669 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700670
671 properties ApexSetProperties
672
673 installDir android.InstallPath
674 installFilename string
675 outputApex android.WritablePath
676
677 // list of commands to create symlinks for backward compatibility.
678 // these commands will be attached as LOCAL_POST_INSTALL_CMD
679 compatSymlinks []string
680}
681
Paul Duffin24704672021-04-06 16:09:30 +0100682type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700683 // the .apks file path that contains prebuilt apex files to be extracted.
684 Set *string
685
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700686 Sanitized struct {
687 None struct {
688 Set *string
689 }
690 Address struct {
691 Set *string
692 }
693 Hwaddress struct {
694 Set *string
695 }
696 }
697
Paul Duffin24704672021-04-06 16:09:30 +0100698 // apexes in this set use prerelease SDK version
699 Prerelease *bool
700}
701
702func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
703 var srcs []string
704 if e.Set != nil {
705 srcs = append(srcs, *e.Set)
706 }
707
708 var sanitizers []string
709 if ctx.Host() {
710 sanitizers = ctx.Config().SanitizeHost()
711 } else {
712 sanitizers = ctx.Config().SanitizeDevice()
713 }
714
715 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
716 srcs = append(srcs, *e.Sanitized.Address.Set)
717 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
718 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
719 } else if e.Sanitized.None.Set != nil {
720 srcs = append(srcs, *e.Sanitized.None.Set)
721 }
722
723 return srcs
724}
725
726type ApexSetProperties struct {
727 ApexExtractorProperties
728
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700729 // whether the extracted apex file installable.
730 Installable *bool
731
732 // optional name for the installed apex. If unspecified, name of the
733 // module is used as the file name
734 Filename *string
735
736 // names of modules to be overridden. Listed modules can only be other binaries
737 // (in Make or Soong).
738 // This does not completely prevent installation of the overridden binaries, but if both
739 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
740 // from PRODUCT_PACKAGES.
741 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700742}
743
744func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
745 if sanitizer == "address" {
746 return a.properties.Sanitized.Address.Set != nil
747 }
748 if sanitizer == "hwaddress" {
749 return a.properties.Sanitized.Hwaddress.Set != nil
750 }
751
752 return false
753}
754
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700755func (a *ApexSet) installable() bool {
756 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
757}
758
759func (a *ApexSet) InstallFilename() string {
760 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
761}
762
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700763func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900764 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700765}
766
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900767func (a *ApexSet) Overrides() []string {
768 return a.properties.Overrides
769}
770
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700771// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
772func apexSetFactory() android.Module {
773 module := &ApexSet{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100774 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700775
Paul Duffinbb0dc132021-05-05 16:58:08 +0100776 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Paul Duffin24704672021-04-06 16:09:30 +0100777 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
778
Paul Duffin24704672021-04-06 16:09:30 +0100779 return module
780}
781
Paul Duffin5dda3e32021-05-05 14:13:27 +0100782func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100783 props := struct {
784 Name *string
785 }{
786 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700787 }
788
Paul Duffin24704672021-04-06 16:09:30 +0100789 ctx.CreateModule(privateApexExtractorModuleFactory,
790 &props,
791 apexExtractorProperties,
792 )
793}
794
795func apexExtractorModuleName(baseModuleName string) string {
796 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700797}
798
Paul Duffin5dda3e32021-05-05 14:13:27 +0100799var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
800
801// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
802// modules.
803//
804// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
805// prebuilt_apex except that instead of creating a selector module which selects one .apex file
806// from those provided this creates an extractor module which extracts the appropriate .apex file
807// from the zip file containing them.
808func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
809 baseModuleName := a.BaseModuleName()
810
811 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
812 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
813
814 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100815 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100816
817 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100818 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100819}
820
Paul Duffin57f83592021-05-05 15:09:44 +0100821func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
822 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100823}
824
825var _ ApexInfoMutator = (*ApexSet)(nil)
826
827func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
828 a.apexInfoMutator(mctx)
829}
830
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700831func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
832 a.installFilename = a.InstallFilename()
833 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
834 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
835 }
836
Paul Duffinbb0dc132021-05-05 16:58:08 +0100837 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700838 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100839 ctx.Build(pctx, android.BuildParams{
840 Rule: android.Cp,
841 Input: inputApex,
842 Output: a.outputApex,
843 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900844
845 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800846 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900847 return
848 }
849
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700850 a.installDir = android.PathForModuleInstall(ctx, "apex")
851 if a.installable() {
852 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
853 }
854
855 // in case that apex_set replaces source apex (using prefer: prop)
856 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
857 // or that apex_set overrides other apexes (using overrides: prop)
858 for _, overridden := range a.properties.Overrides {
859 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
860 }
861}
862
863func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
864 return []android.AndroidMkEntries{android.AndroidMkEntries{
865 Class: "ETC",
866 OutputFile: android.OptionalPathForPath(a.outputApex),
867 Include: "$(BUILD_PREBUILT)",
868 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700869 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700870 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
871 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
872 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
873 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
874 if len(a.compatSymlinks) > 0 {
875 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(a.compatSymlinks, " && "))
876 }
877 },
878 },
879 }}
880}