blob: cef15c4c371b29530c854a9e314f66f7319d34b5 [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"
Jiyong Park10e926b2020-07-16 21:38:56 +090025
Jaewoong Jungfa00c062020-05-14 14:15:24 -070026 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090027
28 "github.com/google/blueprint/proptools"
29)
30
Jaewoong Jungfa00c062020-05-14 14:15:24 -070031var (
32 extractMatchingApex = pctx.StaticRule(
33 "extractMatchingApex",
34 blueprint.RuleParams{
35 Command: `rm -rf "$out" && ` +
36 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
37 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
38 `${in}`,
39 CommandDeps: []string{"${extract_apks}"},
40 },
41 "abis", "allow-prereleased", "sdk-version")
42)
43
Jiyong Park10e926b2020-07-16 21:38:56 +090044type prebuilt interface {
45 isForceDisabled() bool
46 InstallFilename() string
47}
48
49type prebuiltCommon struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010050 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010051
Paul Duffinbb0dc132021-05-05 16:58:08 +010052 // Properties common to both prebuilt_apex and apex_set.
53 prebuiltCommonProperties prebuiltCommonProperties
Jiyong Park10e926b2020-07-16 21:38:56 +090054}
55
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070056type sanitizedPrebuilt interface {
57 hasSanitizedSource(sanitizer string) bool
58}
59
Jiyong Park10e926b2020-07-16 21:38:56 +090060type prebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010061 SelectedApexProperties
62
Jiyong Park10e926b2020-07-16 21:38:56 +090063 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010064
65 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
66 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
67 // dexpreopt and boot jars package check.
68 Exported_java_libs []string
69
70 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
71 // bundle will create an APEX variant.
72 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +090073}
74
75func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
76 return &p.prebuilt
77}
78
79func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +010080 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +090081}
82
83func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
84 // If the device is configured to use flattened APEX, force disable the prebuilt because
85 // the prebuilt is a non-flattened one.
86 forceDisable := ctx.Config().FlattenApex()
87
88 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
89 // to build the prebuilts themselves.
90 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
91
92 // Force disable the prebuilts when coverage is enabled.
93 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
94 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
95
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070096 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
97 sanitized := ctx.Module().(sanitizedPrebuilt)
98 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
99 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900100
101 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100102 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900103 return true
104 }
105 return false
106}
107
Paul Duffin5dda3e32021-05-05 14:13:27 +0100108// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
109// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
110type prebuiltApexModuleCreator interface {
111 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
112}
113
114// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
115// prebuiltApexModuleCreator's createPrebuiltApexModules method.
116//
117// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
118// will need to access dependencies added by that (exported modules) but must run before the
119// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
120// exported modules.
121func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
122 module := ctx.Module()
123 if creator, ok := module.(prebuiltApexModuleCreator); ok {
124 creator.createPrebuiltApexModules(ctx)
125 }
126}
127
Paul Duffin57f83592021-05-05 15:09:44 +0100128// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
129func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
130 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100131 // Add dependencies onto the java modules that represent the java libraries that are provided by
132 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100133 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100134 dep := android.PrebuiltNameFromSource(exported)
135 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100136 }
Paul Duffin023dba02021-04-22 01:45:29 +0100137
138 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
139 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100140 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100141 dep := android.PrebuiltNameFromSource(exported)
142 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100143 }
Paul Duffindfd33262021-04-06 17:02:08 +0100144}
145
Paul Duffinb17d0442021-05-05 12:07:00 +0100146// Implements android.DepInInSameApex
147func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
148 tag := ctx.OtherModuleDependencyTag(dep)
149 _, ok := tag.(exportedDependencyTag)
150 return ok
151}
152
Paul Duffindfd33262021-04-06 17:02:08 +0100153// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
154// specific variant and checks that they are supported.
155//
156// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
157// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
158//
159// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
160// across prebuilt_apex modules. That is because there is no way to determine whether two
161// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
162// been built from different source at different times or they could have been built with different
163// build options that affect the libraries.
164//
165// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
166// modules were compatible it would be a lot of work and would not provide much benefit for a couple
167// of reasons:
168// * The number of prebuilt_apex modules that will be exporting files for the same module will be
169// low as the prebuilt_apex only exports files for the direct dependencies that require it and
170// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
171// few com.android.art* apex files that contain the same contents and could export files for the
172// same modules but only one of them needs to do so. Contrast that with source apex modules which
173// need apex specific variants for every module that contributes code to the apex, whether direct
174// or indirect.
175// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
176// extra copying of files. Contrast that with source apex modules that has to build each variant
177// from source.
178func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
179
180 // Collect direct dependencies into contents.
181 contents := make(map[string]android.ApexMembership)
182
183 // Collect the list of dependencies.
184 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100185 mctx.WalkDeps(func(child, parent android.Module) bool {
186 // If the child is not in the same apex as the parent then exit immediately and do not visit
187 // any of the child's dependencies.
188 if !android.IsDepInSameApex(mctx, parent, child) {
189 return false
190 }
191
192 tag := mctx.OtherModuleDependencyTag(child)
193 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100194 if exportedTag, ok := tag.(exportedDependencyTag); ok {
195 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100196
197 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100198 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100199 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100200 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100201 }
202
203 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100204 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100205 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100206 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100207 }
Paul Duffindfd33262021-04-06 17:02:08 +0100208 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100209
210 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
211 // behavior whether there is a corresponding source module present or not.
212 depName = android.RemoveOptionalPrebuiltPrefix(depName)
213
214 // Remember if this module was added as a direct dependency.
215 direct := parent == mctx.Module()
216 contents[depName] = contents[depName].Add(direct)
217
218 // Add the module to the list of dependencies that need to have an APEX variant.
219 dependencies = append(dependencies, child.(android.ApexModule))
220
221 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100222 })
223
224 // Create contents for the prebuilt_apex and store it away for later use.
225 apexContents := android.NewApexContents(contents)
226 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
227 Contents: apexContents,
228 })
229
230 // Create an ApexInfo for the prebuilt_apex.
231 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100232 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Jiyong Parkab50b072021-05-12 17:13:56 +0900233 InApexVariants: []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 Crossaa255532020-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
Jooyung Han29637162020-06-30 06:34:23 +0900680
681 hostRequired []string
682 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700683}
684
Paul Duffin24704672021-04-06 16:09:30 +0100685type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700686 // the .apks file path that contains prebuilt apex files to be extracted.
687 Set *string
688
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700689 Sanitized struct {
690 None struct {
691 Set *string
692 }
693 Address struct {
694 Set *string
695 }
696 Hwaddress struct {
697 Set *string
698 }
699 }
700
Paul Duffin24704672021-04-06 16:09:30 +0100701 // apexes in this set use prerelease SDK version
702 Prerelease *bool
703}
704
705func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
706 var srcs []string
707 if e.Set != nil {
708 srcs = append(srcs, *e.Set)
709 }
710
711 var sanitizers []string
712 if ctx.Host() {
713 sanitizers = ctx.Config().SanitizeHost()
714 } else {
715 sanitizers = ctx.Config().SanitizeDevice()
716 }
717
718 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
719 srcs = append(srcs, *e.Sanitized.Address.Set)
720 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
721 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
722 } else if e.Sanitized.None.Set != nil {
723 srcs = append(srcs, *e.Sanitized.None.Set)
724 }
725
726 return srcs
727}
728
729type ApexSetProperties struct {
730 ApexExtractorProperties
731
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700732 // whether the extracted apex file installable.
733 Installable *bool
734
735 // optional name for the installed apex. If unspecified, name of the
736 // module is used as the file name
737 Filename *string
738
739 // names of modules to be overridden. Listed modules can only be other binaries
740 // (in Make or Soong).
741 // This does not completely prevent installation of the overridden binaries, but if both
742 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
743 // from PRODUCT_PACKAGES.
744 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700745}
746
747func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
748 if sanitizer == "address" {
749 return a.properties.Sanitized.Address.Set != nil
750 }
751 if sanitizer == "hwaddress" {
752 return a.properties.Sanitized.Hwaddress.Set != nil
753 }
754
755 return false
756}
757
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700758func (a *ApexSet) installable() bool {
759 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
760}
761
762func (a *ApexSet) InstallFilename() string {
763 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
764}
765
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700766func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900767 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700768}
769
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900770func (a *ApexSet) Overrides() []string {
771 return a.properties.Overrides
772}
773
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700774// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
775func apexSetFactory() android.Module {
776 module := &ApexSet{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100777 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700778
Paul Duffinbb0dc132021-05-05 16:58:08 +0100779 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Paul Duffin24704672021-04-06 16:09:30 +0100780 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
781
Paul Duffin24704672021-04-06 16:09:30 +0100782 return module
783}
784
Paul Duffin5dda3e32021-05-05 14:13:27 +0100785func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100786 props := struct {
787 Name *string
788 }{
789 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700790 }
791
Paul Duffin24704672021-04-06 16:09:30 +0100792 ctx.CreateModule(privateApexExtractorModuleFactory,
793 &props,
794 apexExtractorProperties,
795 )
796}
797
798func apexExtractorModuleName(baseModuleName string) string {
799 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700800}
801
Paul Duffin5dda3e32021-05-05 14:13:27 +0100802var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
803
804// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
805// modules.
806//
807// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
808// prebuilt_apex except that instead of creating a selector module which selects one .apex file
809// from those provided this creates an extractor module which extracts the appropriate .apex file
810// from the zip file containing them.
811func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
812 baseModuleName := a.BaseModuleName()
813
814 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
815 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
816
817 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100818 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100819
820 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100821 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100822}
823
Paul Duffin57f83592021-05-05 15:09:44 +0100824func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
825 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100826}
827
828var _ ApexInfoMutator = (*ApexSet)(nil)
829
830func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
831 a.apexInfoMutator(mctx)
832}
833
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700834func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
835 a.installFilename = a.InstallFilename()
836 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
837 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
838 }
839
Paul Duffinbb0dc132021-05-05 16:58:08 +0100840 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700841 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100842 ctx.Build(pctx, android.BuildParams{
843 Rule: android.Cp,
844 Input: inputApex,
845 Output: a.outputApex,
846 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900847
848 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800849 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900850 return
851 }
852
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700853 a.installDir = android.PathForModuleInstall(ctx, "apex")
854 if a.installable() {
855 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
856 }
857
858 // in case that apex_set replaces source apex (using prefer: prop)
859 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
860 // or that apex_set overrides other apexes (using overrides: prop)
861 for _, overridden := range a.properties.Overrides {
862 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
863 }
Jooyung Han29637162020-06-30 06:34:23 +0900864
865 if ctx.Config().InstallExtraFlattenedApexes() {
866 // flattened apex should be in /system_ext/apex
867 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
868 a.postInstallCommands = append(a.postInstallCommands,
869 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
870 a.outputApex.String(),
871 flattenedApexDir.ToMakePath().String(),
872 ))
873 a.hostRequired = []string{"deapexer", "debugfs"}
874 }
875}
876
877type systemExtContext struct {
878 android.ModuleContext
879}
880
881func (*systemExtContext) SystemExtSpecific() bool {
882 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700883}
884
885func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
886 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900887 Class: "ETC",
888 OutputFile: android.OptionalPathForPath(a.outputApex),
889 Include: "$(BUILD_PREBUILT)",
890 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700891 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700892 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700893 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
894 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
895 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
896 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900897 postInstallCommands := append([]string{}, a.postInstallCommands...)
898 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
899 if len(postInstallCommands) > 0 {
900 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700901 }
902 },
903 },
904 }}
905}