blob: 368175cad8241edcf122cb4da4906cbdf57d55ad [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070019 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090020 "strings"
21
22 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070023 "android/soong/java"
Jiyong Park10e926b2020-07-16 21:38:56 +090024
Jaewoong Jungfa00c062020-05-14 14:15:24 -070025 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090026
27 "github.com/google/blueprint/proptools"
28)
29
Jaewoong Jungfa00c062020-05-14 14:15:24 -070030var (
31 extractMatchingApex = pctx.StaticRule(
32 "extractMatchingApex",
33 blueprint.RuleParams{
34 Command: `rm -rf "$out" && ` +
35 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
36 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
37 `${in}`,
38 CommandDeps: []string{"${extract_apks}"},
39 },
40 "abis", "allow-prereleased", "sdk-version")
41)
42
Jiyong Park10e926b2020-07-16 21:38:56 +090043type prebuilt interface {
44 isForceDisabled() bool
45 InstallFilename() string
46}
47
48type prebuiltCommon struct {
49 prebuilt android.Prebuilt
50 properties prebuiltCommonProperties
Paul Duffindfd33262021-04-06 17:02:08 +010051
52 deapexerProperties DeapexerProperties
53 selectedApexProperties SelectedApexProperties
Jiyong Park10e926b2020-07-16 21:38:56 +090054}
55
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070056type sanitizedPrebuilt interface {
57 hasSanitizedSource(sanitizer string) bool
58}
59
Jiyong Park10e926b2020-07-16 21:38:56 +090060type prebuiltCommonProperties struct {
61 ForceDisable bool `blueprint:"mutated"`
62}
63
64func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
65 return &p.prebuilt
66}
67
68func (p *prebuiltCommon) isForceDisabled() bool {
69 return p.properties.ForceDisable
70}
71
72func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
73 // If the device is configured to use flattened APEX, force disable the prebuilt because
74 // the prebuilt is a non-flattened one.
75 forceDisable := ctx.Config().FlattenApex()
76
77 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
78 // to build the prebuilts themselves.
79 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
80
81 // Force disable the prebuilts when coverage is enabled.
82 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
83 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
84
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070085 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
86 sanitized := ctx.Module().(sanitizedPrebuilt)
87 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
88 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090089
90 if forceDisable && p.prebuilt.SourceExists() {
91 p.properties.ForceDisable = true
92 return true
93 }
94 return false
95}
96
Paul Duffin5dda3e32021-05-05 14:13:27 +010097// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
98// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
99type prebuiltApexModuleCreator interface {
100 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
101}
102
103// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
104// prebuiltApexModuleCreator's createPrebuiltApexModules method.
105//
106// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
107// will need to access dependencies added by that (exported modules) but must run before the
108// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
109// exported modules.
110func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
111 module := ctx.Module()
112 if creator, ok := module.(prebuiltApexModuleCreator); ok {
113 creator.createPrebuiltApexModules(ctx)
114 }
115}
116
Paul Duffin57f83592021-05-05 15:09:44 +0100117// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
118func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
119 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100120 // Add dependencies onto the java modules that represent the java libraries that are provided by
121 // and exported from this prebuilt apex.
Paul Duffin023dba02021-04-22 01:45:29 +0100122 for _, exported := range p.deapexerProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100123 dep := android.PrebuiltNameFromSource(exported)
124 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100125 }
Paul Duffin023dba02021-04-22 01:45:29 +0100126
127 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
128 // apex.
129 for _, exported := range p.deapexerProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100130 dep := android.PrebuiltNameFromSource(exported)
131 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100132 }
Paul Duffindfd33262021-04-06 17:02:08 +0100133}
134
Paul Duffinb17d0442021-05-05 12:07:00 +0100135// Implements android.DepInInSameApex
136func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
137 tag := ctx.OtherModuleDependencyTag(dep)
138 _, ok := tag.(exportedDependencyTag)
139 return ok
140}
141
Paul Duffindfd33262021-04-06 17:02:08 +0100142// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
143// specific variant and checks that they are supported.
144//
145// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
146// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
147//
148// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
149// across prebuilt_apex modules. That is because there is no way to determine whether two
150// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
151// been built from different source at different times or they could have been built with different
152// build options that affect the libraries.
153//
154// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
155// modules were compatible it would be a lot of work and would not provide much benefit for a couple
156// of reasons:
157// * The number of prebuilt_apex modules that will be exporting files for the same module will be
158// low as the prebuilt_apex only exports files for the direct dependencies that require it and
159// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
160// few com.android.art* apex files that contain the same contents and could export files for the
161// same modules but only one of them needs to do so. Contrast that with source apex modules which
162// need apex specific variants for every module that contributes code to the apex, whether direct
163// or indirect.
164// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
165// extra copying of files. Contrast that with source apex modules that has to build each variant
166// from source.
167func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
168
169 // Collect direct dependencies into contents.
170 contents := make(map[string]android.ApexMembership)
171
172 // Collect the list of dependencies.
173 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100174 mctx.WalkDeps(func(child, parent android.Module) bool {
175 // If the child is not in the same apex as the parent then exit immediately and do not visit
176 // any of the child's dependencies.
177 if !android.IsDepInSameApex(mctx, parent, child) {
178 return false
179 }
180
181 tag := mctx.OtherModuleDependencyTag(child)
182 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100183 if exportedTag, ok := tag.(exportedDependencyTag); ok {
184 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100185
186 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100187 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100188 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100189 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100190 }
191
192 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100193 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100194 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100195 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100196 }
Paul Duffindfd33262021-04-06 17:02:08 +0100197 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100198
199 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
200 // behavior whether there is a corresponding source module present or not.
201 depName = android.RemoveOptionalPrebuiltPrefix(depName)
202
203 // Remember if this module was added as a direct dependency.
204 direct := parent == mctx.Module()
205 contents[depName] = contents[depName].Add(direct)
206
207 // Add the module to the list of dependencies that need to have an APEX variant.
208 dependencies = append(dependencies, child.(android.ApexModule))
209
210 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100211 })
212
213 // Create contents for the prebuilt_apex and store it away for later use.
214 apexContents := android.NewApexContents(contents)
215 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
216 Contents: apexContents,
217 })
218
219 // Create an ApexInfo for the prebuilt_apex.
220 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100221 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Paul Duffindfd33262021-04-06 17:02:08 +0100222 InApexes: []string{mctx.ModuleName()},
223 ApexContents: []*android.ApexContents{apexContents},
224 ForPrebuiltApex: true,
225 }
226
227 // Mark the dependencies of this module as requiring a variant for this module.
228 for _, am := range dependencies {
229 am.BuildForApex(apexInfo)
230 }
231}
232
Paul Duffin11216db2021-03-01 14:14:52 +0000233// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
234// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
235// deapexer.
236type prebuiltApexSelectorModule struct {
237 android.ModuleBase
238
239 apexFileProperties ApexFileProperties
240
241 inputApex android.Path
242}
243
244func privateApexSelectorModuleFactory() android.Module {
245 module := &prebuiltApexSelectorModule{}
246 module.AddProperties(
247 &module.apexFileProperties,
248 )
249 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
250 return module
251}
252
253func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
254 return android.Paths{p.inputApex}
255}
256
257func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
258 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
259}
260
Jiyong Park09d77522019-11-18 11:16:27 +0900261type Prebuilt struct {
262 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900263 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900264
Paul Duffin11216db2021-03-01 14:14:52 +0000265 properties PrebuiltProperties
266 selectedApexProperties SelectedApexProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900267
268 inputApex android.Path
269 installDir android.InstallPath
270 installFilename string
271 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900272
273 // list of commands to create symlinks for backward compatibility.
274 // these commands will be attached as LOCAL_POST_INSTALL_CMD
275 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900276}
277
Paul Duffin851f3992021-01-13 17:03:51 +0000278type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900279 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000280 //
281 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
282 // for android_common. That is so that it will have the same arch variant as, and so be compatible
283 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000284 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900285 Arch struct {
286 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000287 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900288 }
289 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000290 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900291 }
292 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000293 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900294 }
295 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000296 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900297 }
298 }
Paul Duffin851f3992021-01-13 17:03:51 +0000299}
300
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000301// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
302//
303// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
304// to use methods on it that are specific to the current module.
305//
306// See the ApexFileProperties.Src property.
307func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
308 multiTargets := prebuilt.MultiTargets()
309 if len(multiTargets) != 1 {
310 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
311 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000312 }
313 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000314 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000315 case android.Arm:
316 src = String(p.Arch.Arm.Src)
317 case android.Arm64:
318 src = String(p.Arch.Arm64.Src)
319 case android.X86:
320 src = String(p.Arch.X86.Src)
321 case android.X86_64:
322 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000323 }
324 if src == "" {
325 src = String(p.Src)
326 }
Paul Duffin851f3992021-01-13 17:03:51 +0000327
Paul Duffinc0609c62021-03-01 17:27:16 +0000328 if src == "" {
329 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
330 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
331 // logic from reporting a more general, less useful message.
332 }
333
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000334 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000335}
336
337type PrebuiltProperties struct {
338 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900339
340 Installable *bool
341 // Optional name for the installed apex. If unspecified, name of the
342 // module is used as the file name
343 Filename *string
344
345 // Names of modules to be overridden. Listed modules can only be other binaries
346 // (in Make or Soong).
347 // This does not completely prevent installation of the overridden binaries, but if both
348 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
349 // from PRODUCT_PACKAGES.
350 Overrides []string
351}
352
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700353func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
354 return false
355}
356
Jiyong Park09d77522019-11-18 11:16:27 +0900357func (p *Prebuilt) installable() bool {
358 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
359}
360
Jiyong Park09d77522019-11-18 11:16:27 +0900361func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
362 switch tag {
363 case "":
364 return android.Paths{p.outputApex}, nil
365 default:
366 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
367 }
368}
369
370func (p *Prebuilt) InstallFilename() string {
371 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
372}
373
Jiyong Park09d77522019-11-18 11:16:27 +0900374func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900375 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900376}
377
378// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
379func PrebuiltFactory() android.Module {
380 module := &Prebuilt{}
Paul Duffindfd33262021-04-06 17:02:08 +0100381 module.AddProperties(&module.properties, &module.deapexerProperties, &module.selectedApexProperties)
Paul Duffin11216db2021-03-01 14:14:52 +0000382 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900383 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000384
Jiyong Park09d77522019-11-18 11:16:27 +0900385 return module
386}
387
Paul Duffin5dda3e32021-05-05 14:13:27 +0100388func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000389 props := struct {
390 Name *string
391 }{
392 Name: proptools.StringPtr(name),
393 }
394
395 ctx.CreateModule(privateApexSelectorModuleFactory,
396 &props,
397 apexFileProperties,
398 )
399}
400
Paul Duffin5dda3e32021-05-05 14:13:27 +0100401// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
402//
Paul Duffin57f83592021-05-05 15:09:44 +0100403// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
404// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
405// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100406func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, deapexerProperties *DeapexerProperties) {
407 // Only create the deapexer module if it is needed.
Paul Duffin57f83592021-05-05 15:09:44 +0100408 if len(deapexerProperties.Exported_java_libs)+len(deapexerProperties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100409 return
410 }
411
Paul Duffin57f83592021-05-05 15:09:44 +0100412 // Compute the deapexer properties from the transitive dependencies of this module.
413 deapexerProperties = &DeapexerProperties{}
414 ctx.WalkDeps(func(child, parent android.Module) bool {
415 tag := ctx.OtherModuleDependencyTag(child)
416
417 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
418 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
419 deapexerProperties.Exported_java_libs = append(deapexerProperties.Exported_java_libs, name)
420 } else if tag == exportedBootclasspathFragmentTag {
421 deapexerProperties.Exported_bootclasspath_fragments = append(deapexerProperties.Exported_bootclasspath_fragments, name)
422 // Only visit the children of the bootclasspath_fragment for now.
423 return true
424 }
425
426 return false
427 })
428
429 // Remove any duplicates from the deapexer lists.
430 deapexerProperties.Exported_bootclasspath_fragments = android.FirstUniqueStrings(deapexerProperties.Exported_bootclasspath_fragments)
431 deapexerProperties.Exported_java_libs = android.FirstUniqueStrings(deapexerProperties.Exported_java_libs)
432
Paul Duffin11216db2021-03-01 14:14:52 +0000433 props := struct {
434 Name *string
435 Selected_apex *string
436 }{
437 Name: proptools.StringPtr(deapexerName),
438 Selected_apex: proptools.StringPtr(apexFileSource),
439 }
440 ctx.CreateModule(privateDeapexerFactory,
441 &props,
442 deapexerProperties,
443 )
444}
445
446func deapexerModuleName(baseModuleName string) string {
447 return baseModuleName + ".deapexer"
448}
449
450func apexSelectorModuleName(baseModuleName string) string {
451 return baseModuleName + ".apex.selector"
452}
453
Paul Duffin064b70c2020-11-02 17:32:38 +0000454func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
455 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
456 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
457 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
458 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
459 // and not a renamed prebuilt module then that will be detected and reported as an error when
460 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100461 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000462 if ctx.OtherModuleExists(prebuiltName) {
463 name = prebuiltName
464 }
465 return name
466}
467
Paul Duffina7139422021-02-08 11:01:58 +0000468type exportedDependencyTag struct {
469 blueprint.BaseDependencyTag
470 name string
471}
472
473// Mark this tag so dependencies that use it are excluded from visibility enforcement.
474//
475// This does allow any prebuilt_apex to reference any module which does open up a small window for
476// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
477// avoids opening up a much bigger window by widening the visibility of modules that need files
478// provided by the prebuilt_apex to include all the possible locations they may be defined, which
479// could include everything below vendor/.
480//
481// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
482// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
483// the .apex file. It will also have to be included in the module's apex_available property too.
484// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
485// incorrectly.
486func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
487
488var (
Paul Duffin023dba02021-04-22 01:45:29 +0100489 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
490 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000491)
492
Paul Duffin5dda3e32021-05-05 14:13:27 +0100493var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
494
495// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
496// build.
497//
498// If this needs to make files from within a `.apex` file available for use by other Soong modules,
499// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
500// it does so as follows:
501//
502// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
503// makes them available for use by other modules, at both Soong and ninja levels.
504//
505// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
506// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
507// dexpreopt, will work the same way from source and prebuilt.
508//
509// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
510// itself so that they can retrieve the file paths to those files.
511//
512// It also creates a child module `selector` that is responsible for selecting the appropriate
513// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
514// 1. To dedup the selection logic so it only runs in one module.
515// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
516// `apex_set`.
517//
518// prebuilt_apex
519// / | \
520// / | \
521// V V V
522// selector <--- deapexer <--- exported java lib
523//
524func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
525 baseModuleName := p.BaseModuleName()
526
527 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
528 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
529
530 apexFileSource := ":" + apexSelectorModuleName
531 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.deapexerProperties)
532
533 // Add a source reference to retrieve the selected apex from the selector module.
534 p.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
535}
536
Paul Duffin57f83592021-05-05 15:09:44 +0100537func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
538 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000539}
540
541var _ ApexInfoMutator = (*Prebuilt)(nil)
542
Paul Duffin064b70c2020-11-02 17:32:38 +0000543func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100544 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900545}
546
547func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900548 // TODO(jungjw): Check the key validity.
Paul Duffin11216db2021-03-01 14:14:52 +0000549 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900550 p.installDir = android.PathForModuleInstall(ctx, "apex")
551 p.installFilename = p.InstallFilename()
552 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
553 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
554 }
555 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
556 ctx.Build(pctx, android.BuildParams{
557 Rule: android.Cp,
558 Input: p.inputApex,
559 Output: p.outputApex,
560 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900561
562 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800563 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900564 return
565 }
566
Jiyong Park09d77522019-11-18 11:16:27 +0900567 if p.installable() {
568 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
569 }
570
Jooyung Han002ab682020-01-08 01:57:58 +0900571 // in case that prebuilt_apex replaces source apex (using prefer: prop)
572 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
573 // or that prebuilt_apex overrides other apexes (using overrides: prop)
574 for _, overridden := range p.properties.Overrides {
575 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
576 }
Jiyong Park09d77522019-11-18 11:16:27 +0900577}
578
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900579func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
580 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900581 Class: "ETC",
582 OutputFile: android.OptionalPathForPath(p.inputApex),
583 Include: "$(BUILD_PREBUILT)",
584 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700585 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900586 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
587 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
588 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
589 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900590 if len(p.compatSymlinks) > 0 {
591 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
592 }
Jiyong Park09d77522019-11-18 11:16:27 +0900593 },
594 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900595 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900596}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700597
Paul Duffin24704672021-04-06 16:09:30 +0100598// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
599// module. It extracts the correct apex to use and makes it available for use by apex_set.
600type prebuiltApexExtractorModule struct {
601 android.ModuleBase
602
603 properties ApexExtractorProperties
604
605 extractedApex android.WritablePath
606}
607
608func privateApexExtractorModuleFactory() android.Module {
609 module := &prebuiltApexExtractorModule{}
610 module.AddProperties(
611 &module.properties,
612 )
613 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
614 return module
615}
616
617func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
618 return android.Paths{p.extractedApex}
619}
620
621func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
622 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
623 return p.properties.prebuiltSrcs(ctx)
624 }
625 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
626 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
627 ctx.Build(pctx,
628 android.BuildParams{
629 Rule: extractMatchingApex,
630 Description: "Extract an apex from an apex set",
631 Inputs: android.Paths{apexSet},
632 Output: p.extractedApex,
633 Args: map[string]string{
634 "abis": strings.Join(java.SupportedAbis(ctx), ","),
635 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
636 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
637 },
638 })
639}
640
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700641type ApexSet struct {
642 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900643 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700644
645 properties ApexSetProperties
646
647 installDir android.InstallPath
648 installFilename string
649 outputApex android.WritablePath
650
651 // list of commands to create symlinks for backward compatibility.
652 // these commands will be attached as LOCAL_POST_INSTALL_CMD
653 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900654
655 hostRequired []string
656 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700657}
658
Paul Duffin24704672021-04-06 16:09:30 +0100659type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700660 // the .apks file path that contains prebuilt apex files to be extracted.
661 Set *string
662
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700663 Sanitized struct {
664 None struct {
665 Set *string
666 }
667 Address struct {
668 Set *string
669 }
670 Hwaddress struct {
671 Set *string
672 }
673 }
674
Paul Duffin24704672021-04-06 16:09:30 +0100675 // apexes in this set use prerelease SDK version
676 Prerelease *bool
677}
678
679func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
680 var srcs []string
681 if e.Set != nil {
682 srcs = append(srcs, *e.Set)
683 }
684
685 var sanitizers []string
686 if ctx.Host() {
687 sanitizers = ctx.Config().SanitizeHost()
688 } else {
689 sanitizers = ctx.Config().SanitizeDevice()
690 }
691
692 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
693 srcs = append(srcs, *e.Sanitized.Address.Set)
694 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
695 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
696 } else if e.Sanitized.None.Set != nil {
697 srcs = append(srcs, *e.Sanitized.None.Set)
698 }
699
700 return srcs
701}
702
703type ApexSetProperties struct {
704 ApexExtractorProperties
705
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700706 // whether the extracted apex file installable.
707 Installable *bool
708
709 // optional name for the installed apex. If unspecified, name of the
710 // module is used as the file name
711 Filename *string
712
713 // names of modules to be overridden. Listed modules can only be other binaries
714 // (in Make or Soong).
715 // This does not completely prevent installation of the overridden binaries, but if both
716 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
717 // from PRODUCT_PACKAGES.
718 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700719}
720
721func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
722 if sanitizer == "address" {
723 return a.properties.Sanitized.Address.Set != nil
724 }
725 if sanitizer == "hwaddress" {
726 return a.properties.Sanitized.Hwaddress.Set != nil
727 }
728
729 return false
730}
731
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700732func (a *ApexSet) installable() bool {
733 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
734}
735
736func (a *ApexSet) InstallFilename() string {
737 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
738}
739
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700740func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900741 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700742}
743
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900744func (a *ApexSet) Overrides() []string {
745 return a.properties.Overrides
746}
747
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700748// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
749func apexSetFactory() android.Module {
750 module := &ApexSet{}
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100751 module.AddProperties(&module.properties, &module.selectedApexProperties, &module.deapexerProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700752
Paul Duffin24704672021-04-06 16:09:30 +0100753 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
754 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
755
Paul Duffin24704672021-04-06 16:09:30 +0100756 return module
757}
758
Paul Duffin5dda3e32021-05-05 14:13:27 +0100759func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100760 props := struct {
761 Name *string
762 }{
763 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700764 }
765
Paul Duffin24704672021-04-06 16:09:30 +0100766 ctx.CreateModule(privateApexExtractorModuleFactory,
767 &props,
768 apexExtractorProperties,
769 )
770}
771
772func apexExtractorModuleName(baseModuleName string) string {
773 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700774}
775
Paul Duffin5dda3e32021-05-05 14:13:27 +0100776var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
777
778// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
779// modules.
780//
781// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
782// prebuilt_apex except that instead of creating a selector module which selects one .apex file
783// from those provided this creates an extractor module which extracts the appropriate .apex file
784// from the zip file containing them.
785func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
786 baseModuleName := a.BaseModuleName()
787
788 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
789 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
790
791 apexFileSource := ":" + apexExtractorModuleName
792 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.deapexerProperties)
793
794 // After passing the arch specific src properties to the creating the apex selector module
795 a.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
796}
797
Paul Duffin57f83592021-05-05 15:09:44 +0100798func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
799 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100800}
801
802var _ ApexInfoMutator = (*ApexSet)(nil)
803
804func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
805 a.apexInfoMutator(mctx)
806}
807
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700808func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
809 a.installFilename = a.InstallFilename()
810 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
811 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
812 }
813
Paul Duffin24704672021-04-06 16:09:30 +0100814 inputApex := android.OptionalPathForModuleSrc(ctx, a.selectedApexProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700815 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100816 ctx.Build(pctx, android.BuildParams{
817 Rule: android.Cp,
818 Input: inputApex,
819 Output: a.outputApex,
820 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900821
822 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800823 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900824 return
825 }
826
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700827 a.installDir = android.PathForModuleInstall(ctx, "apex")
828 if a.installable() {
829 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
830 }
831
832 // in case that apex_set replaces source apex (using prefer: prop)
833 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
834 // or that apex_set overrides other apexes (using overrides: prop)
835 for _, overridden := range a.properties.Overrides {
836 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
837 }
Jooyung Han29637162020-06-30 06:34:23 +0900838
839 if ctx.Config().InstallExtraFlattenedApexes() {
840 // flattened apex should be in /system_ext/apex
841 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
842 a.postInstallCommands = append(a.postInstallCommands,
843 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
844 a.outputApex.String(),
845 flattenedApexDir.ToMakePath().String(),
846 ))
847 a.hostRequired = []string{"deapexer", "debugfs"}
848 }
849}
850
851type systemExtContext struct {
852 android.ModuleContext
853}
854
855func (*systemExtContext) SystemExtSpecific() bool {
856 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700857}
858
859func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
860 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900861 Class: "ETC",
862 OutputFile: android.OptionalPathForPath(a.outputApex),
863 Include: "$(BUILD_PREBUILT)",
864 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700865 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700866 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700867 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
868 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
869 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
870 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900871 postInstallCommands := append([]string{}, a.postInstallCommands...)
872 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
873 if len(postInstallCommands) > 0 {
874 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700875 }
876 },
877 },
878 }}
879}