blob: f0eee6c75aec1f0895123a74429e7fc0e4aa604f [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.
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000230 apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName())
Paul Duffindfd33262021-04-06 17:02:08 +0100231 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000232 ApexVariationName: apexVariationName,
233 InApexVariants: []string{apexVariationName},
234 InApexModules: []string{apexVariationName},
Paul Duffindfd33262021-04-06 17:02:08 +0100235 ApexContents: []*android.ApexContents{apexContents},
236 ForPrebuiltApex: true,
237 }
238
239 // Mark the dependencies of this module as requiring a variant for this module.
240 for _, am := range dependencies {
241 am.BuildForApex(apexInfo)
242 }
243}
244
Paul Duffin11216db2021-03-01 14:14:52 +0000245// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
246// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
247// deapexer.
248type prebuiltApexSelectorModule struct {
249 android.ModuleBase
250
251 apexFileProperties ApexFileProperties
252
253 inputApex android.Path
254}
255
256func privateApexSelectorModuleFactory() android.Module {
257 module := &prebuiltApexSelectorModule{}
258 module.AddProperties(
259 &module.apexFileProperties,
260 )
261 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
262 return module
263}
264
265func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
266 return android.Paths{p.inputApex}
267}
268
269func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
270 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
271}
272
Jiyong Park09d77522019-11-18 11:16:27 +0900273type Prebuilt struct {
274 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900275 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900276
Paul Duffinbb0dc132021-05-05 16:58:08 +0100277 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900278
279 inputApex android.Path
280 installDir android.InstallPath
281 installFilename string
282 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900283
284 // list of commands to create symlinks for backward compatibility.
285 // these commands will be attached as LOCAL_POST_INSTALL_CMD
286 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900287}
288
Paul Duffin851f3992021-01-13 17:03:51 +0000289type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900290 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000291 //
292 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
293 // for android_common. That is so that it will have the same arch variant as, and so be compatible
294 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000295 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900296 Arch struct {
297 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000298 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900299 }
300 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000301 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900302 }
303 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000304 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900305 }
306 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000307 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900308 }
309 }
Paul Duffin851f3992021-01-13 17:03:51 +0000310}
311
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000312// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
313//
314// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
315// to use methods on it that are specific to the current module.
316//
317// See the ApexFileProperties.Src property.
318func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
319 multiTargets := prebuilt.MultiTargets()
320 if len(multiTargets) != 1 {
321 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
322 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000323 }
324 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000325 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000326 case android.Arm:
327 src = String(p.Arch.Arm.Src)
328 case android.Arm64:
329 src = String(p.Arch.Arm64.Src)
330 case android.X86:
331 src = String(p.Arch.X86.Src)
332 case android.X86_64:
333 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000334 }
335 if src == "" {
336 src = String(p.Src)
337 }
Paul Duffin851f3992021-01-13 17:03:51 +0000338
Paul Duffinc0609c62021-03-01 17:27:16 +0000339 if src == "" {
340 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
341 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
342 // logic from reporting a more general, less useful message.
343 }
344
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000345 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000346}
347
348type PrebuiltProperties struct {
349 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900350
351 Installable *bool
352 // Optional name for the installed apex. If unspecified, name of the
353 // module is used as the file name
354 Filename *string
355
356 // Names of modules to be overridden. Listed modules can only be other binaries
357 // (in Make or Soong).
358 // This does not completely prevent installation of the overridden binaries, but if both
359 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
360 // from PRODUCT_PACKAGES.
361 Overrides []string
362}
363
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700364func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
365 return false
366}
367
Jiyong Park09d77522019-11-18 11:16:27 +0900368func (p *Prebuilt) installable() bool {
369 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
370}
371
Jiyong Park09d77522019-11-18 11:16:27 +0900372func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
373 switch tag {
374 case "":
375 return android.Paths{p.outputApex}, nil
376 default:
377 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
378 }
379}
380
381func (p *Prebuilt) InstallFilename() string {
382 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
383}
384
Jiyong Park09d77522019-11-18 11:16:27 +0900385func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900386 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900387}
388
389// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
390func PrebuiltFactory() android.Module {
391 module := &Prebuilt{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100392 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
393 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900394 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000395
Jiyong Park09d77522019-11-18 11:16:27 +0900396 return module
397}
398
Paul Duffin5dda3e32021-05-05 14:13:27 +0100399func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000400 props := struct {
401 Name *string
402 }{
403 Name: proptools.StringPtr(name),
404 }
405
406 ctx.CreateModule(privateApexSelectorModuleFactory,
407 &props,
408 apexFileProperties,
409 )
410}
411
Paul Duffin5dda3e32021-05-05 14:13:27 +0100412// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
413//
Paul Duffin57f83592021-05-05 15:09:44 +0100414// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
415// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
416// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100417func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *prebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100418 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100419 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100420 return
421 }
422
Paul Duffin57f83592021-05-05 15:09:44 +0100423 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin3bae0682021-05-05 18:03:47 +0100424 javaModules := []string{}
425 exportedFiles := map[string]string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100426 ctx.WalkDeps(func(child, parent android.Module) bool {
427 tag := ctx.OtherModuleDependencyTag(child)
428
429 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
430 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin3bae0682021-05-05 18:03:47 +0100431 javaModules = append(javaModules, name)
432
433 // Add the dex implementation jar to the set of exported files. The path here must match the
434 // path of the file in the APEX created by apexFileForJavaModule(...).
435 exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
436
Paul Duffin57f83592021-05-05 15:09:44 +0100437 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin57f83592021-05-05 15:09:44 +0100438 // Only visit the children of the bootclasspath_fragment for now.
439 return true
440 }
441
442 return false
443 })
444
Paul Duffin3bae0682021-05-05 18:03:47 +0100445 // Create properties for deapexer module.
446 deapexerProperties := &DeapexerProperties{
447 // Remove any duplicates from the java modules lists as a module may be included via a direct
448 // dependency as well as transitive ones.
449 CommonModules: android.SortedUniqueStrings(javaModules),
450 }
451
452 // Populate the exported files property in a fixed order.
453 for _, tag := range android.SortedStringKeys(exportedFiles) {
454 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
455 Tag: tag,
456 Path: exportedFiles[tag],
457 })
458 }
Paul Duffin57f83592021-05-05 15:09:44 +0100459
Paul Duffin11216db2021-03-01 14:14:52 +0000460 props := struct {
461 Name *string
462 Selected_apex *string
463 }{
464 Name: proptools.StringPtr(deapexerName),
465 Selected_apex: proptools.StringPtr(apexFileSource),
466 }
467 ctx.CreateModule(privateDeapexerFactory,
468 &props,
469 deapexerProperties,
470 )
471}
472
473func deapexerModuleName(baseModuleName string) string {
474 return baseModuleName + ".deapexer"
475}
476
477func apexSelectorModuleName(baseModuleName string) string {
478 return baseModuleName + ".apex.selector"
479}
480
Paul Duffin064b70c2020-11-02 17:32:38 +0000481func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
482 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
483 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
484 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
485 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
486 // and not a renamed prebuilt module then that will be detected and reported as an error when
487 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100488 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000489 if ctx.OtherModuleExists(prebuiltName) {
490 name = prebuiltName
491 }
492 return name
493}
494
Paul Duffina7139422021-02-08 11:01:58 +0000495type exportedDependencyTag struct {
496 blueprint.BaseDependencyTag
497 name string
498}
499
500// Mark this tag so dependencies that use it are excluded from visibility enforcement.
501//
502// This does allow any prebuilt_apex to reference any module which does open up a small window for
503// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
504// avoids opening up a much bigger window by widening the visibility of modules that need files
505// provided by the prebuilt_apex to include all the possible locations they may be defined, which
506// could include everything below vendor/.
507//
508// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
509// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
510// the .apex file. It will also have to be included in the module's apex_available property too.
511// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
512// incorrectly.
513func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
514
515var (
Paul Duffin023dba02021-04-22 01:45:29 +0100516 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
517 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000518)
519
Paul Duffin5dda3e32021-05-05 14:13:27 +0100520var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
521
522// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
523// build.
524//
525// If this needs to make files from within a `.apex` file available for use by other Soong modules,
526// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
527// it does so as follows:
528//
529// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
530// makes them available for use by other modules, at both Soong and ninja levels.
531//
532// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
533// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
534// dexpreopt, will work the same way from source and prebuilt.
535//
536// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
537// itself so that they can retrieve the file paths to those files.
538//
539// It also creates a child module `selector` that is responsible for selecting the appropriate
540// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
541// 1. To dedup the selection logic so it only runs in one module.
542// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
543// `apex_set`.
544//
545// prebuilt_apex
546// / | \
547// / | \
548// V V V
549// selector <--- deapexer <--- exported java lib
550//
551func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
552 baseModuleName := p.BaseModuleName()
553
554 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
555 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
556
557 apexFileSource := ":" + apexSelectorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100558 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100559
560 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100561 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100562}
563
Paul Duffin57f83592021-05-05 15:09:44 +0100564func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
565 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000566}
567
568var _ ApexInfoMutator = (*Prebuilt)(nil)
569
Paul Duffin064b70c2020-11-02 17:32:38 +0000570func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100571 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900572}
573
574func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900575 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100576 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900577 p.installDir = android.PathForModuleInstall(ctx, "apex")
578 p.installFilename = p.InstallFilename()
579 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
580 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
581 }
582 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
583 ctx.Build(pctx, android.BuildParams{
584 Rule: android.Cp,
585 Input: p.inputApex,
586 Output: p.outputApex,
587 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900588
589 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800590 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900591 return
592 }
593
Jiyong Park09d77522019-11-18 11:16:27 +0900594 if p.installable() {
595 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
596 }
597
Jooyung Han002ab682020-01-08 01:57:58 +0900598 // in case that prebuilt_apex replaces source apex (using prefer: prop)
599 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
600 // or that prebuilt_apex overrides other apexes (using overrides: prop)
601 for _, overridden := range p.properties.Overrides {
602 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
603 }
Jiyong Park09d77522019-11-18 11:16:27 +0900604}
605
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900606func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
607 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900608 Class: "ETC",
609 OutputFile: android.OptionalPathForPath(p.inputApex),
610 Include: "$(BUILD_PREBUILT)",
611 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700612 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900613 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
614 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
615 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
616 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900617 if len(p.compatSymlinks) > 0 {
618 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
619 }
Jiyong Park09d77522019-11-18 11:16:27 +0900620 },
621 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900622 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900623}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700624
Paul Duffin24704672021-04-06 16:09:30 +0100625// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
626// module. It extracts the correct apex to use and makes it available for use by apex_set.
627type prebuiltApexExtractorModule struct {
628 android.ModuleBase
629
630 properties ApexExtractorProperties
631
632 extractedApex android.WritablePath
633}
634
635func privateApexExtractorModuleFactory() android.Module {
636 module := &prebuiltApexExtractorModule{}
637 module.AddProperties(
638 &module.properties,
639 )
640 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
641 return module
642}
643
644func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
645 return android.Paths{p.extractedApex}
646}
647
648func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
649 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
650 return p.properties.prebuiltSrcs(ctx)
651 }
652 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
653 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
654 ctx.Build(pctx,
655 android.BuildParams{
656 Rule: extractMatchingApex,
657 Description: "Extract an apex from an apex set",
658 Inputs: android.Paths{apexSet},
659 Output: p.extractedApex,
660 Args: map[string]string{
661 "abis": strings.Join(java.SupportedAbis(ctx), ","),
662 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
663 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
664 },
665 })
666}
667
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700668type ApexSet struct {
669 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900670 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700671
672 properties ApexSetProperties
673
674 installDir android.InstallPath
675 installFilename string
676 outputApex android.WritablePath
677
678 // list of commands to create symlinks for backward compatibility.
679 // these commands will be attached as LOCAL_POST_INSTALL_CMD
680 compatSymlinks []string
681}
682
Paul Duffin24704672021-04-06 16:09:30 +0100683type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700684 // the .apks file path that contains prebuilt apex files to be extracted.
685 Set *string
686
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700687 Sanitized struct {
688 None struct {
689 Set *string
690 }
691 Address struct {
692 Set *string
693 }
694 Hwaddress struct {
695 Set *string
696 }
697 }
698
Paul Duffin24704672021-04-06 16:09:30 +0100699 // apexes in this set use prerelease SDK version
700 Prerelease *bool
701}
702
703func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
704 var srcs []string
705 if e.Set != nil {
706 srcs = append(srcs, *e.Set)
707 }
708
709 var sanitizers []string
710 if ctx.Host() {
711 sanitizers = ctx.Config().SanitizeHost()
712 } else {
713 sanitizers = ctx.Config().SanitizeDevice()
714 }
715
716 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
717 srcs = append(srcs, *e.Sanitized.Address.Set)
718 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
719 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
720 } else if e.Sanitized.None.Set != nil {
721 srcs = append(srcs, *e.Sanitized.None.Set)
722 }
723
724 return srcs
725}
726
727type ApexSetProperties struct {
728 ApexExtractorProperties
729
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700730 // whether the extracted apex file installable.
731 Installable *bool
732
733 // optional name for the installed apex. If unspecified, name of the
734 // module is used as the file name
735 Filename *string
736
737 // names of modules to be overridden. Listed modules can only be other binaries
738 // (in Make or Soong).
739 // This does not completely prevent installation of the overridden binaries, but if both
740 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
741 // from PRODUCT_PACKAGES.
742 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700743}
744
745func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
746 if sanitizer == "address" {
747 return a.properties.Sanitized.Address.Set != nil
748 }
749 if sanitizer == "hwaddress" {
750 return a.properties.Sanitized.Hwaddress.Set != nil
751 }
752
753 return false
754}
755
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700756func (a *ApexSet) installable() bool {
757 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
758}
759
760func (a *ApexSet) InstallFilename() string {
761 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
762}
763
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700764func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900765 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700766}
767
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900768func (a *ApexSet) Overrides() []string {
769 return a.properties.Overrides
770}
771
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700772// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
773func apexSetFactory() android.Module {
774 module := &ApexSet{}
Paul Duffinbb0dc132021-05-05 16:58:08 +0100775 module.AddProperties(&module.properties, &module.prebuiltCommonProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700776
Paul Duffinbb0dc132021-05-05 16:58:08 +0100777 android.InitSingleSourcePrebuiltModule(module, &module.prebuiltCommonProperties, "Selected_apex")
Paul Duffin24704672021-04-06 16:09:30 +0100778 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
779
Paul Duffin24704672021-04-06 16:09:30 +0100780 return module
781}
782
Paul Duffin5dda3e32021-05-05 14:13:27 +0100783func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100784 props := struct {
785 Name *string
786 }{
787 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700788 }
789
Paul Duffin24704672021-04-06 16:09:30 +0100790 ctx.CreateModule(privateApexExtractorModuleFactory,
791 &props,
792 apexExtractorProperties,
793 )
794}
795
796func apexExtractorModuleName(baseModuleName string) string {
797 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700798}
799
Paul Duffin5dda3e32021-05-05 14:13:27 +0100800var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
801
802// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
803// modules.
804//
805// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
806// prebuilt_apex except that instead of creating a selector module which selects one .apex file
807// from those provided this creates an extractor module which extracts the appropriate .apex file
808// from the zip file containing them.
809func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
810 baseModuleName := a.BaseModuleName()
811
812 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
813 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
814
815 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinbb0dc132021-05-05 16:58:08 +0100816 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, &a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100817
818 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100819 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100820}
821
Paul Duffin57f83592021-05-05 15:09:44 +0100822func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
823 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100824}
825
826var _ ApexInfoMutator = (*ApexSet)(nil)
827
828func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
829 a.apexInfoMutator(mctx)
830}
831
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700832func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
833 a.installFilename = a.InstallFilename()
834 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
835 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
836 }
837
Paul Duffinbb0dc132021-05-05 16:58:08 +0100838 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700839 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100840 ctx.Build(pctx, android.BuildParams{
841 Rule: android.Cp,
842 Input: inputApex,
843 Output: a.outputApex,
844 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900845
846 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800847 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900848 return
849 }
850
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700851 a.installDir = android.PathForModuleInstall(ctx, "apex")
852 if a.installable() {
853 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
854 }
855
856 // in case that apex_set replaces source apex (using prefer: prop)
857 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
858 // or that apex_set overrides other apexes (using overrides: prop)
859 for _, overridden := range a.properties.Overrides {
860 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
861 }
862}
863
864func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
865 return []android.AndroidMkEntries{android.AndroidMkEntries{
866 Class: "ETC",
867 OutputFile: android.OptionalPathForPath(a.outputApex),
868 Include: "$(BUILD_PREBUILT)",
869 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700870 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700871 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
872 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
873 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
874 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
875 if len(a.compatSymlinks) > 0 {
876 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(a.compatSymlinks, " && "))
877 }
878 },
879 },
880 }}
881}