blob: be9d0f0b4e04c28b6e9a8f6975dee1fee6894008 [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()},
Paul Duffindfd33262021-04-06 17:02:08 +0100233 ApexContents: []*android.ApexContents{apexContents},
234 ForPrebuiltApex: true,
235 }
236
237 // Mark the dependencies of this module as requiring a variant for this module.
238 for _, am := range dependencies {
239 am.BuildForApex(apexInfo)
240 }
241}
242
Paul Duffin11216db2021-03-01 14:14:52 +0000243// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
244// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
245// deapexer.
246type prebuiltApexSelectorModule struct {
247 android.ModuleBase
248
249 apexFileProperties ApexFileProperties
250
251 inputApex android.Path
252}
253
254func privateApexSelectorModuleFactory() android.Module {
255 module := &prebuiltApexSelectorModule{}
256 module.AddProperties(
257 &module.apexFileProperties,
258 )
259 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
260 return module
261}
262
263func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
264 return android.Paths{p.inputApex}
265}
266
267func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
268 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
269}
270
Jiyong Park09d77522019-11-18 11:16:27 +0900271type Prebuilt struct {
272 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900273 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900274
Paul Duffinbb0dc132021-05-05 16:58:08 +0100275 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900276
277 inputApex android.Path
278 installDir android.InstallPath
279 installFilename string
280 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900281
282 // list of commands to create symlinks for backward compatibility.
283 // these commands will be attached as LOCAL_POST_INSTALL_CMD
284 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900285}
286
Paul Duffin851f3992021-01-13 17:03:51 +0000287type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900288 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000289 //
290 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
291 // for android_common. That is so that it will have the same arch variant as, and so be compatible
292 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000293 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900294 Arch struct {
295 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000296 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900297 }
298 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000299 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900300 }
301 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000302 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900303 }
304 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000305 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900306 }
307 }
Paul Duffin851f3992021-01-13 17:03:51 +0000308}
309
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000310// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
311//
312// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
313// to use methods on it that are specific to the current module.
314//
315// See the ApexFileProperties.Src property.
316func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
317 multiTargets := prebuilt.MultiTargets()
318 if len(multiTargets) != 1 {
319 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
320 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000321 }
322 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000323 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000324 case android.Arm:
325 src = String(p.Arch.Arm.Src)
326 case android.Arm64:
327 src = String(p.Arch.Arm64.Src)
328 case android.X86:
329 src = String(p.Arch.X86.Src)
330 case android.X86_64:
331 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000332 }
333 if src == "" {
334 src = String(p.Src)
335 }
Paul Duffin851f3992021-01-13 17:03:51 +0000336
Paul Duffinc0609c62021-03-01 17:27:16 +0000337 if src == "" {
338 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
339 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
340 // logic from reporting a more general, less useful message.
341 }
342
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000343 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000344}
345
346type PrebuiltProperties struct {
347 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900348
349 Installable *bool
350 // Optional name for the installed apex. If unspecified, name of the
351 // module is used as the file name
352 Filename *string
353
354 // Names of modules to be overridden. Listed modules can only be other binaries
355 // (in Make or Soong).
356 // This does not completely prevent installation of the overridden binaries, but if both
357 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
358 // from PRODUCT_PACKAGES.
359 Overrides []string
360}
361
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700362func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
363 return false
364}
365
Jiyong Park09d77522019-11-18 11:16:27 +0900366func (p *Prebuilt) installable() bool {
367 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
368}
369
Jiyong Park09d77522019-11-18 11:16:27 +0900370func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
371 switch tag {
372 case "":
373 return android.Paths{p.outputApex}, nil
374 default:
375 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
376 }
377}
378
379func (p *Prebuilt) InstallFilename() string {
380 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
381}
382
Jiyong Park09d77522019-11-18 11:16:27 +0900383func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900384 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900385}
386
387// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
388func PrebuiltFactory() android.Module {
389 module := &Prebuilt{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100390 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
391 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900392 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000393
Jiyong Park09d77522019-11-18 11:16:27 +0900394 return module
395}
396
Paul Duffin5dda3e32021-05-05 14:13:27 +0100397func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000398 props := struct {
399 Name *string
400 }{
401 Name: proptools.StringPtr(name),
402 }
403
404 ctx.CreateModule(privateApexSelectorModuleFactory,
405 &props,
406 apexFileProperties,
407 )
408}
409
Paul Duffin5dda3e32021-05-05 14:13:27 +0100410// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
411//
Paul Duffin57f83592021-05-05 15:09:44 +0100412// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
413// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
414// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100415func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *prebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100416 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100417 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100418 return
419 }
420
Paul Duffin57f83592021-05-05 15:09:44 +0100421 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin3bae0682021-05-05 18:03:47 +0100422 javaModules := []string{}
423 exportedFiles := map[string]string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100424 ctx.WalkDeps(func(child, parent android.Module) bool {
425 tag := ctx.OtherModuleDependencyTag(child)
426
427 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
428 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin3bae0682021-05-05 18:03:47 +0100429 javaModules = append(javaModules, name)
430
431 // Add the dex implementation jar to the set of exported files. The path here must match the
432 // path of the file in the APEX created by apexFileForJavaModule(...).
433 exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
434
Paul Duffin57f83592021-05-05 15:09:44 +0100435 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin57f83592021-05-05 15:09:44 +0100436 // Only visit the children of the bootclasspath_fragment for now.
437 return true
438 }
439
440 return false
441 })
442
Paul Duffin3bae0682021-05-05 18:03:47 +0100443 // Create properties for deapexer module.
444 deapexerProperties := &DeapexerProperties{
445 // Remove any duplicates from the java modules lists as a module may be included via a direct
446 // dependency as well as transitive ones.
447 CommonModules: android.SortedUniqueStrings(javaModules),
448 }
449
450 // Populate the exported files property in a fixed order.
451 for _, tag := range android.SortedStringKeys(exportedFiles) {
452 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
453 Tag: tag,
454 Path: exportedFiles[tag],
455 })
456 }
Paul Duffin57f83592021-05-05 15:09:44 +0100457
Paul Duffin11216db2021-03-01 14:14:52 +0000458 props := struct {
459 Name *string
460 Selected_apex *string
461 }{
462 Name: proptools.StringPtr(deapexerName),
463 Selected_apex: proptools.StringPtr(apexFileSource),
464 }
465 ctx.CreateModule(privateDeapexerFactory,
466 &props,
467 deapexerProperties,
468 )
469}
470
471func deapexerModuleName(baseModuleName string) string {
472 return baseModuleName + ".deapexer"
473}
474
475func apexSelectorModuleName(baseModuleName string) string {
476 return baseModuleName + ".apex.selector"
477}
478
Paul Duffin064b70c2020-11-02 17:32:38 +0000479func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
480 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
481 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
482 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
483 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
484 // and not a renamed prebuilt module then that will be detected and reported as an error when
485 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100486 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000487 if ctx.OtherModuleExists(prebuiltName) {
488 name = prebuiltName
489 }
490 return name
491}
492
Paul Duffina7139422021-02-08 11:01:58 +0000493type exportedDependencyTag struct {
494 blueprint.BaseDependencyTag
495 name string
496}
497
498// Mark this tag so dependencies that use it are excluded from visibility enforcement.
499//
500// This does allow any prebuilt_apex to reference any module which does open up a small window for
501// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
502// avoids opening up a much bigger window by widening the visibility of modules that need files
503// provided by the prebuilt_apex to include all the possible locations they may be defined, which
504// could include everything below vendor/.
505//
506// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
507// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
508// the .apex file. It will also have to be included in the module's apex_available property too.
509// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
510// incorrectly.
511func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
512
513var (
Paul Duffin023dba02021-04-22 01:45:29 +0100514 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
515 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000516)
517
Paul Duffin5dda3e32021-05-05 14:13:27 +0100518var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
519
520// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
521// build.
522//
523// If this needs to make files from within a `.apex` file available for use by other Soong modules,
524// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
525// it does so as follows:
526//
527// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
528// makes them available for use by other modules, at both Soong and ninja levels.
529//
530// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
531// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
532// dexpreopt, will work the same way from source and prebuilt.
533//
534// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
535// itself so that they can retrieve the file paths to those files.
536//
537// It also creates a child module `selector` that is responsible for selecting the appropriate
538// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
539// 1. To dedup the selection logic so it only runs in one module.
540// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
541// `apex_set`.
542//
543// prebuilt_apex
544// / | \
545// / | \
546// V V V
547// selector <--- deapexer <--- exported java lib
548//
549func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
550 baseModuleName := p.BaseModuleName()
551
552 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
553 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
554
555 apexFileSource := ":" + apexSelectorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100556 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100557
558 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100559 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100560}
561
Paul Duffin57f83592021-05-05 15:09:44 +0100562func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
563 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000564}
565
566var _ ApexInfoMutator = (*Prebuilt)(nil)
567
Paul Duffin064b70c2020-11-02 17:32:38 +0000568func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100569 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900570}
571
572func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900573 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100574 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900575 p.installDir = android.PathForModuleInstall(ctx, "apex")
576 p.installFilename = p.InstallFilename()
577 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
578 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
579 }
580 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
581 ctx.Build(pctx, android.BuildParams{
582 Rule: android.Cp,
583 Input: p.inputApex,
584 Output: p.outputApex,
585 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900586
587 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800588 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900589 return
590 }
591
Jiyong Park09d77522019-11-18 11:16:27 +0900592 if p.installable() {
593 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
594 }
595
Jooyung Han002ab682020-01-08 01:57:58 +0900596 // in case that prebuilt_apex replaces source apex (using prefer: prop)
597 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
598 // or that prebuilt_apex overrides other apexes (using overrides: prop)
599 for _, overridden := range p.properties.Overrides {
600 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
601 }
Jiyong Park09d77522019-11-18 11:16:27 +0900602}
603
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900604func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
605 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900606 Class: "ETC",
607 OutputFile: android.OptionalPathForPath(p.inputApex),
608 Include: "$(BUILD_PREBUILT)",
609 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700610 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900611 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
612 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
613 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
614 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900615 if len(p.compatSymlinks) > 0 {
616 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
617 }
Jiyong Park09d77522019-11-18 11:16:27 +0900618 },
619 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900620 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900621}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700622
Paul Duffin24704672021-04-06 16:09:30 +0100623// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
624// module. It extracts the correct apex to use and makes it available for use by apex_set.
625type prebuiltApexExtractorModule struct {
626 android.ModuleBase
627
628 properties ApexExtractorProperties
629
630 extractedApex android.WritablePath
631}
632
633func privateApexExtractorModuleFactory() android.Module {
634 module := &prebuiltApexExtractorModule{}
635 module.AddProperties(
636 &module.properties,
637 )
638 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
639 return module
640}
641
642func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
643 return android.Paths{p.extractedApex}
644}
645
646func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
647 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
648 return p.properties.prebuiltSrcs(ctx)
649 }
650 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
651 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
652 ctx.Build(pctx,
653 android.BuildParams{
654 Rule: extractMatchingApex,
655 Description: "Extract an apex from an apex set",
656 Inputs: android.Paths{apexSet},
657 Output: p.extractedApex,
658 Args: map[string]string{
659 "abis": strings.Join(java.SupportedAbis(ctx), ","),
660 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
661 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
662 },
663 })
664}
665
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700666type ApexSet struct {
667 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900668 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700669
670 properties ApexSetProperties
671
672 installDir android.InstallPath
673 installFilename string
674 outputApex android.WritablePath
675
676 // list of commands to create symlinks for backward compatibility.
677 // these commands will be attached as LOCAL_POST_INSTALL_CMD
678 compatSymlinks []string
679}
680
Paul Duffin24704672021-04-06 16:09:30 +0100681type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700682 // the .apks file path that contains prebuilt apex files to be extracted.
683 Set *string
684
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700685 Sanitized struct {
686 None struct {
687 Set *string
688 }
689 Address struct {
690 Set *string
691 }
692 Hwaddress struct {
693 Set *string
694 }
695 }
696
Paul Duffin24704672021-04-06 16:09:30 +0100697 // apexes in this set use prerelease SDK version
698 Prerelease *bool
699}
700
701func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
702 var srcs []string
703 if e.Set != nil {
704 srcs = append(srcs, *e.Set)
705 }
706
707 var sanitizers []string
708 if ctx.Host() {
709 sanitizers = ctx.Config().SanitizeHost()
710 } else {
711 sanitizers = ctx.Config().SanitizeDevice()
712 }
713
714 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
715 srcs = append(srcs, *e.Sanitized.Address.Set)
716 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
717 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
718 } else if e.Sanitized.None.Set != nil {
719 srcs = append(srcs, *e.Sanitized.None.Set)
720 }
721
722 return srcs
723}
724
725type ApexSetProperties struct {
726 ApexExtractorProperties
727
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700728 // whether the extracted apex file installable.
729 Installable *bool
730
731 // optional name for the installed apex. If unspecified, name of the
732 // module is used as the file name
733 Filename *string
734
735 // names of modules to be overridden. Listed modules can only be other binaries
736 // (in Make or Soong).
737 // This does not completely prevent installation of the overridden binaries, but if both
738 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
739 // from PRODUCT_PACKAGES.
740 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700741}
742
743func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
744 if sanitizer == "address" {
745 return a.properties.Sanitized.Address.Set != nil
746 }
747 if sanitizer == "hwaddress" {
748 return a.properties.Sanitized.Hwaddress.Set != nil
749 }
750
751 return false
752}
753
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700754func (a *ApexSet) installable() bool {
755 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
756}
757
758func (a *ApexSet) InstallFilename() string {
759 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
760}
761
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700762func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900763 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700764}
765
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900766func (a *ApexSet) Overrides() []string {
767 return a.properties.Overrides
768}
769
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700770// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
771func apexSetFactory() android.Module {
772 module := &ApexSet{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100773 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700774
Paul Duffinbb0dc132021-05-05 16:58:08 +0100775 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Paul Duffin24704672021-04-06 16:09:30 +0100776 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
777
Paul Duffin24704672021-04-06 16:09:30 +0100778 return module
779}
780
Paul Duffin5dda3e32021-05-05 14:13:27 +0100781func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100782 props := struct {
783 Name *string
784 }{
785 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700786 }
787
Paul Duffin24704672021-04-06 16:09:30 +0100788 ctx.CreateModule(privateApexExtractorModuleFactory,
789 &props,
790 apexExtractorProperties,
791 )
792}
793
794func apexExtractorModuleName(baseModuleName string) string {
795 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700796}
797
Paul Duffin5dda3e32021-05-05 14:13:27 +0100798var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
799
800// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
801// modules.
802//
803// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
804// prebuilt_apex except that instead of creating a selector module which selects one .apex file
805// from those provided this creates an extractor module which extracts the appropriate .apex file
806// from the zip file containing them.
807func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
808 baseModuleName := a.BaseModuleName()
809
810 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
811 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
812
813 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100814 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100815
816 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100817 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100818}
819
Paul Duffin57f83592021-05-05 15:09:44 +0100820func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
821 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100822}
823
824var _ ApexInfoMutator = (*ApexSet)(nil)
825
826func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
827 a.apexInfoMutator(mctx)
828}
829
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700830func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
831 a.installFilename = a.InstallFilename()
832 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
833 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
834 }
835
Paul Duffinbb0dc132021-05-05 16:58:08 +0100836 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700837 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100838 ctx.Build(pctx, android.BuildParams{
839 Rule: android.Cp,
840 Input: inputApex,
841 Output: a.outputApex,
842 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900843
844 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800845 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900846 return
847 }
848
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700849 a.installDir = android.PathForModuleInstall(ctx, "apex")
850 if a.installable() {
851 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
852 }
853
854 // in case that apex_set replaces source apex (using prefer: prop)
855 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
856 // or that apex_set overrides other apexes (using overrides: prop)
857 for _, overridden := range a.properties.Overrides {
858 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
859 }
860}
861
862func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
863 return []android.AndroidMkEntries{android.AndroidMkEntries{
864 Class: "ETC",
865 OutputFile: android.OptionalPathForPath(a.outputApex),
866 Include: "$(BUILD_PREBUILT)",
867 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700868 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700869 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
870 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
871 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
872 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
873 if len(a.compatSymlinks) > 0 {
874 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(a.compatSymlinks, " && "))
875 }
876 },
877 },
878 }}
879}