blob: 7830f95af77474450e8e896fcdb607c5c3e17879 [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 Duffindfd33262021-04-06 17:02:08 +010097func (p *prebuiltCommon) deapexerDeps(ctx android.BottomUpMutatorContext) {
98 // Add dependencies onto the java modules that represent the java libraries that are provided by
99 // and exported from this prebuilt apex.
Paul Duffin023dba02021-04-22 01:45:29 +0100100 for _, exported := range p.deapexerProperties.Exported_java_libs {
101 dep := prebuiltApexExportedModuleName(ctx, exported)
Paul Duffindfd33262021-04-06 17:02:08 +0100102 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
103 }
Paul Duffin023dba02021-04-22 01:45:29 +0100104
105 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
106 // apex.
107 for _, exported := range p.deapexerProperties.Exported_bootclasspath_fragments {
108 dep := prebuiltApexExportedModuleName(ctx, exported)
109 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedBootclasspathFragmentTag, dep)
110 }
Paul Duffindfd33262021-04-06 17:02:08 +0100111}
112
113// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
114// specific variant and checks that they are supported.
115//
116// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
117// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
118//
119// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
120// across prebuilt_apex modules. That is because there is no way to determine whether two
121// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
122// been built from different source at different times or they could have been built with different
123// build options that affect the libraries.
124//
125// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
126// modules were compatible it would be a lot of work and would not provide much benefit for a couple
127// of reasons:
128// * The number of prebuilt_apex modules that will be exporting files for the same module will be
129// low as the prebuilt_apex only exports files for the direct dependencies that require it and
130// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
131// few com.android.art* apex files that contain the same contents and could export files for the
132// same modules but only one of them needs to do so. Contrast that with source apex modules which
133// need apex specific variants for every module that contributes code to the apex, whether direct
134// or indirect.
135// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
136// extra copying of files. Contrast that with source apex modules that has to build each variant
137// from source.
138func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
139
140 // Collect direct dependencies into contents.
141 contents := make(map[string]android.ApexMembership)
142
143 // Collect the list of dependencies.
144 var dependencies []android.ApexModule
145 mctx.VisitDirectDeps(func(m android.Module) {
146 tag := mctx.OtherModuleDependencyTag(m)
Paul Duffin023dba02021-04-22 01:45:29 +0100147 if exportedTag, ok := tag.(exportedDependencyTag); ok {
148 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100149 depName := mctx.OtherModuleName(m)
150
151 // It is an error if the other module is not a prebuilt.
152 if _, ok := m.(android.PrebuiltInterface); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100153 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffindfd33262021-04-06 17:02:08 +0100154 return
155 }
156
157 // It is an error if the other module is not an ApexModule.
158 if _, ok := m.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100159 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffindfd33262021-04-06 17:02:08 +0100160 return
161 }
162
163 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
164 // behavior whether there is a corresponding source module present or not.
165 depName = android.RemoveOptionalPrebuiltPrefix(depName)
166
167 // Remember that this module was added as a direct dependency.
168 contents[depName] = contents[depName].Add(true)
169
170 // Add the module to the list of dependencies that need to have an APEX variant.
171 dependencies = append(dependencies, m.(android.ApexModule))
172 }
173 })
174
175 // Create contents for the prebuilt_apex and store it away for later use.
176 apexContents := android.NewApexContents(contents)
177 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
178 Contents: apexContents,
179 })
180
181 // Create an ApexInfo for the prebuilt_apex.
182 apexInfo := android.ApexInfo{
Paul Duffin8f146b92021-04-12 17:24:18 +0100183 ApexVariationName: android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName()),
Paul Duffindfd33262021-04-06 17:02:08 +0100184 InApexes: []string{mctx.ModuleName()},
185 ApexContents: []*android.ApexContents{apexContents},
186 ForPrebuiltApex: true,
187 }
188
189 // Mark the dependencies of this module as requiring a variant for this module.
190 for _, am := range dependencies {
191 am.BuildForApex(apexInfo)
192 }
193}
194
Paul Duffin11216db2021-03-01 14:14:52 +0000195// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
196// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
197// deapexer.
198type prebuiltApexSelectorModule struct {
199 android.ModuleBase
200
201 apexFileProperties ApexFileProperties
202
203 inputApex android.Path
204}
205
206func privateApexSelectorModuleFactory() android.Module {
207 module := &prebuiltApexSelectorModule{}
208 module.AddProperties(
209 &module.apexFileProperties,
210 )
211 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
212 return module
213}
214
215func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
216 return android.Paths{p.inputApex}
217}
218
219func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
220 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
221}
222
Jiyong Park09d77522019-11-18 11:16:27 +0900223type Prebuilt struct {
224 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900225 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900226
Paul Duffin11216db2021-03-01 14:14:52 +0000227 properties PrebuiltProperties
228 selectedApexProperties SelectedApexProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900229
230 inputApex android.Path
231 installDir android.InstallPath
232 installFilename string
233 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900234
235 // list of commands to create symlinks for backward compatibility.
236 // these commands will be attached as LOCAL_POST_INSTALL_CMD
237 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900238}
239
Paul Duffin851f3992021-01-13 17:03:51 +0000240type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900241 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000242 //
243 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
244 // for android_common. That is so that it will have the same arch variant as, and so be compatible
245 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000246 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900247 Arch struct {
248 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000249 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900250 }
251 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000252 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900253 }
254 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000255 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900256 }
257 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000258 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900259 }
260 }
Paul Duffin851f3992021-01-13 17:03:51 +0000261}
262
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000263// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
264//
265// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
266// to use methods on it that are specific to the current module.
267//
268// See the ApexFileProperties.Src property.
269func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
270 multiTargets := prebuilt.MultiTargets()
271 if len(multiTargets) != 1 {
272 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
273 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000274 }
275 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000276 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000277 case android.Arm:
278 src = String(p.Arch.Arm.Src)
279 case android.Arm64:
280 src = String(p.Arch.Arm64.Src)
281 case android.X86:
282 src = String(p.Arch.X86.Src)
283 case android.X86_64:
284 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000285 }
286 if src == "" {
287 src = String(p.Src)
288 }
Paul Duffin851f3992021-01-13 17:03:51 +0000289
Paul Duffinc0609c62021-03-01 17:27:16 +0000290 if src == "" {
291 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
292 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
293 // logic from reporting a more general, less useful message.
294 }
295
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000296 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000297}
298
299type PrebuiltProperties struct {
300 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900301
302 Installable *bool
303 // Optional name for the installed apex. If unspecified, name of the
304 // module is used as the file name
305 Filename *string
306
307 // Names of modules to be overridden. Listed modules can only be other binaries
308 // (in Make or Soong).
309 // This does not completely prevent installation of the overridden binaries, but if both
310 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
311 // from PRODUCT_PACKAGES.
312 Overrides []string
313}
314
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700315func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
316 return false
317}
318
Jiyong Park09d77522019-11-18 11:16:27 +0900319func (p *Prebuilt) installable() bool {
320 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
321}
322
Jiyong Park09d77522019-11-18 11:16:27 +0900323func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
324 switch tag {
325 case "":
326 return android.Paths{p.outputApex}, nil
327 default:
328 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
329 }
330}
331
332func (p *Prebuilt) InstallFilename() string {
333 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
334}
335
Jiyong Park09d77522019-11-18 11:16:27 +0900336func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900337 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900338}
339
340// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000341//
342// If this needs to make files from within a `.apex` file available for use by other Soong modules,
343// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
344// it does so as follows:
345//
346// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
347// makes them available for use by other modules, at both Soong and ninja levels.
348//
349// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
350// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
351// dexpreopt, will work the same way from source and prebuilt.
352//
353// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
354// itself so that they can retrieve the file paths to those files.
355//
Paul Duffin11216db2021-03-01 14:14:52 +0000356// It also creates a child module `selector` that is responsible for selecting the appropriate
357// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
358// 1. To dedup the selection logic so it only runs in one module.
359// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
360// `apex_set`.
361//
362// prebuilt_apex
363// / | \
364// / | \
365// V | V
366// selector <--- deapexer <--- exported java lib
367//
Jiyong Park09d77522019-11-18 11:16:27 +0900368func PrebuiltFactory() android.Module {
369 module := &Prebuilt{}
Paul Duffindfd33262021-04-06 17:02:08 +0100370 module.AddProperties(&module.properties, &module.deapexerProperties, &module.selectedApexProperties)
Paul Duffin11216db2021-03-01 14:14:52 +0000371 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900372 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000373
374 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Paul Duffin11216db2021-03-01 14:14:52 +0000375 baseModuleName := module.BaseModuleName()
376
377 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
378 createApexSelectorModule(ctx, apexSelectorModuleName, &module.properties.ApexFileProperties)
379
380 apexFileSource := ":" + apexSelectorModuleName
Paul Duffindfd33262021-04-06 17:02:08 +0100381 if len(module.deapexerProperties.Exported_java_libs) != 0 {
382 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000383 }
Paul Duffin11216db2021-03-01 14:14:52 +0000384
385 // Add a source reference to retrieve the selected apex from the selector module.
386 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin064b70c2020-11-02 17:32:38 +0000387 })
388
Jiyong Park09d77522019-11-18 11:16:27 +0900389 return module
390}
391
Paul Duffin11216db2021-03-01 14:14:52 +0000392func createApexSelectorModule(ctx android.LoadHookContext, name string, apexFileProperties *ApexFileProperties) {
393 props := struct {
394 Name *string
395 }{
396 Name: proptools.StringPtr(name),
397 }
398
399 ctx.CreateModule(privateApexSelectorModuleFactory,
400 &props,
401 apexFileProperties,
402 )
403}
404
405func createDeapexerModule(ctx android.LoadHookContext, deapexerName string, apexFileSource string, deapexerProperties *DeapexerProperties) {
406 props := struct {
407 Name *string
408 Selected_apex *string
409 }{
410 Name: proptools.StringPtr(deapexerName),
411 Selected_apex: proptools.StringPtr(apexFileSource),
412 }
413 ctx.CreateModule(privateDeapexerFactory,
414 &props,
415 deapexerProperties,
416 )
417}
418
419func deapexerModuleName(baseModuleName string) string {
420 return baseModuleName + ".deapexer"
421}
422
423func apexSelectorModuleName(baseModuleName string) string {
424 return baseModuleName + ".apex.selector"
425}
426
Paul Duffin064b70c2020-11-02 17:32:38 +0000427func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
428 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
429 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
430 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
431 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
432 // and not a renamed prebuilt module then that will be detected and reported as an error when
433 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100434 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000435 if ctx.OtherModuleExists(prebuiltName) {
436 name = prebuiltName
437 }
438 return name
439}
440
Paul Duffina7139422021-02-08 11:01:58 +0000441type exportedDependencyTag struct {
442 blueprint.BaseDependencyTag
443 name string
444}
445
446// Mark this tag so dependencies that use it are excluded from visibility enforcement.
447//
448// This does allow any prebuilt_apex to reference any module which does open up a small window for
449// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
450// avoids opening up a much bigger window by widening the visibility of modules that need files
451// provided by the prebuilt_apex to include all the possible locations they may be defined, which
452// could include everything below vendor/.
453//
454// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
455// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
456// the .apex file. It will also have to be included in the module's apex_available property too.
457// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
458// incorrectly.
459func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
460
461var (
Paul Duffin023dba02021-04-22 01:45:29 +0100462 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
463 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000464)
465
Liz Kammer356f7d42021-01-26 09:18:53 -0500466func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100467 p.deapexerDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000468}
469
470var _ ApexInfoMutator = (*Prebuilt)(nil)
471
Paul Duffin064b70c2020-11-02 17:32:38 +0000472func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100473 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900474}
475
476func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900477 // TODO(jungjw): Check the key validity.
Paul Duffin11216db2021-03-01 14:14:52 +0000478 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900479 p.installDir = android.PathForModuleInstall(ctx, "apex")
480 p.installFilename = p.InstallFilename()
481 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
482 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
483 }
484 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
485 ctx.Build(pctx, android.BuildParams{
486 Rule: android.Cp,
487 Input: p.inputApex,
488 Output: p.outputApex,
489 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900490
491 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800492 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900493 return
494 }
495
Jiyong Park09d77522019-11-18 11:16:27 +0900496 if p.installable() {
497 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
498 }
499
Jooyung Han002ab682020-01-08 01:57:58 +0900500 // in case that prebuilt_apex replaces source apex (using prefer: prop)
501 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
502 // or that prebuilt_apex overrides other apexes (using overrides: prop)
503 for _, overridden := range p.properties.Overrides {
504 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
505 }
Jiyong Park09d77522019-11-18 11:16:27 +0900506}
507
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900508func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
509 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900510 Class: "ETC",
511 OutputFile: android.OptionalPathForPath(p.inputApex),
512 Include: "$(BUILD_PREBUILT)",
513 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700514 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900515 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
516 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
517 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
518 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900519 if len(p.compatSymlinks) > 0 {
520 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
521 }
Jiyong Park09d77522019-11-18 11:16:27 +0900522 },
523 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900524 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900525}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700526
Paul Duffin24704672021-04-06 16:09:30 +0100527// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
528// module. It extracts the correct apex to use and makes it available for use by apex_set.
529type prebuiltApexExtractorModule struct {
530 android.ModuleBase
531
532 properties ApexExtractorProperties
533
534 extractedApex android.WritablePath
535}
536
537func privateApexExtractorModuleFactory() android.Module {
538 module := &prebuiltApexExtractorModule{}
539 module.AddProperties(
540 &module.properties,
541 )
542 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
543 return module
544}
545
546func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
547 return android.Paths{p.extractedApex}
548}
549
550func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
551 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
552 return p.properties.prebuiltSrcs(ctx)
553 }
554 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
555 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
556 ctx.Build(pctx,
557 android.BuildParams{
558 Rule: extractMatchingApex,
559 Description: "Extract an apex from an apex set",
560 Inputs: android.Paths{apexSet},
561 Output: p.extractedApex,
562 Args: map[string]string{
563 "abis": strings.Join(java.SupportedAbis(ctx), ","),
564 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
565 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
566 },
567 })
568}
569
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700570type ApexSet struct {
571 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900572 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700573
574 properties ApexSetProperties
575
576 installDir android.InstallPath
577 installFilename string
578 outputApex android.WritablePath
579
580 // list of commands to create symlinks for backward compatibility.
581 // these commands will be attached as LOCAL_POST_INSTALL_CMD
582 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900583
584 hostRequired []string
585 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700586}
587
Paul Duffin24704672021-04-06 16:09:30 +0100588type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700589 // the .apks file path that contains prebuilt apex files to be extracted.
590 Set *string
591
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700592 Sanitized struct {
593 None struct {
594 Set *string
595 }
596 Address struct {
597 Set *string
598 }
599 Hwaddress struct {
600 Set *string
601 }
602 }
603
Paul Duffin24704672021-04-06 16:09:30 +0100604 // apexes in this set use prerelease SDK version
605 Prerelease *bool
606}
607
608func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
609 var srcs []string
610 if e.Set != nil {
611 srcs = append(srcs, *e.Set)
612 }
613
614 var sanitizers []string
615 if ctx.Host() {
616 sanitizers = ctx.Config().SanitizeHost()
617 } else {
618 sanitizers = ctx.Config().SanitizeDevice()
619 }
620
621 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
622 srcs = append(srcs, *e.Sanitized.Address.Set)
623 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
624 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
625 } else if e.Sanitized.None.Set != nil {
626 srcs = append(srcs, *e.Sanitized.None.Set)
627 }
628
629 return srcs
630}
631
632type ApexSetProperties struct {
633 ApexExtractorProperties
634
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700635 // whether the extracted apex file installable.
636 Installable *bool
637
638 // optional name for the installed apex. If unspecified, name of the
639 // module is used as the file name
640 Filename *string
641
642 // names of modules to be overridden. Listed modules can only be other binaries
643 // (in Make or Soong).
644 // This does not completely prevent installation of the overridden binaries, but if both
645 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
646 // from PRODUCT_PACKAGES.
647 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700648}
649
650func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
651 if sanitizer == "address" {
652 return a.properties.Sanitized.Address.Set != nil
653 }
654 if sanitizer == "hwaddress" {
655 return a.properties.Sanitized.Hwaddress.Set != nil
656 }
657
658 return false
659}
660
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700661func (a *ApexSet) installable() bool {
662 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
663}
664
665func (a *ApexSet) InstallFilename() string {
666 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
667}
668
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700669func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900670 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700671}
672
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900673func (a *ApexSet) Overrides() []string {
674 return a.properties.Overrides
675}
676
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700677// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
678func apexSetFactory() android.Module {
679 module := &ApexSet{}
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100680 module.AddProperties(&module.properties, &module.selectedApexProperties, &module.deapexerProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700681
Paul Duffin24704672021-04-06 16:09:30 +0100682 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
683 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
684
685 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
686 baseModuleName := module.BaseModuleName()
687
688 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
689 createApexExtractorModule(ctx, apexExtractorModuleName, &module.properties.ApexExtractorProperties)
690
691 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100692 if len(module.deapexerProperties.Exported_java_libs) != 0 {
693 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
694 }
Paul Duffin24704672021-04-06 16:09:30 +0100695
696 // After passing the arch specific src properties to the creating the apex selector module
697 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
698 })
699
700 return module
701}
702
703func createApexExtractorModule(ctx android.LoadHookContext, name string, apexExtractorProperties *ApexExtractorProperties) {
704 props := struct {
705 Name *string
706 }{
707 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700708 }
709
Paul Duffin24704672021-04-06 16:09:30 +0100710 ctx.CreateModule(privateApexExtractorModuleFactory,
711 &props,
712 apexExtractorProperties,
713 )
714}
715
716func apexExtractorModuleName(baseModuleName string) string {
717 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700718}
719
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100720func (a *ApexSet) DepsMutator(ctx android.BottomUpMutatorContext) {
721 a.deapexerDeps(ctx)
722}
723
724var _ ApexInfoMutator = (*ApexSet)(nil)
725
726func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
727 a.apexInfoMutator(mctx)
728}
729
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700730func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
731 a.installFilename = a.InstallFilename()
732 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
733 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
734 }
735
Paul Duffin24704672021-04-06 16:09:30 +0100736 inputApex := android.OptionalPathForModuleSrc(ctx, a.selectedApexProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700737 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100738 ctx.Build(pctx, android.BuildParams{
739 Rule: android.Cp,
740 Input: inputApex,
741 Output: a.outputApex,
742 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900743
744 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800745 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900746 return
747 }
748
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700749 a.installDir = android.PathForModuleInstall(ctx, "apex")
750 if a.installable() {
751 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
752 }
753
754 // in case that apex_set replaces source apex (using prefer: prop)
755 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
756 // or that apex_set overrides other apexes (using overrides: prop)
757 for _, overridden := range a.properties.Overrides {
758 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
759 }
Jooyung Han29637162020-06-30 06:34:23 +0900760
761 if ctx.Config().InstallExtraFlattenedApexes() {
762 // flattened apex should be in /system_ext/apex
763 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
764 a.postInstallCommands = append(a.postInstallCommands,
765 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
766 a.outputApex.String(),
767 flattenedApexDir.ToMakePath().String(),
768 ))
769 a.hostRequired = []string{"deapexer", "debugfs"}
770 }
771}
772
773type systemExtContext struct {
774 android.ModuleContext
775}
776
777func (*systemExtContext) SystemExtSpecific() bool {
778 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700779}
780
781func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
782 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900783 Class: "ETC",
784 OutputFile: android.OptionalPathForPath(a.outputApex),
785 Include: "$(BUILD_PREBUILT)",
786 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700787 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700788 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700789 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
790 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
791 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
792 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900793 postInstallCommands := append([]string{}, a.postInstallCommands...)
794 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
795 if len(postInstallCommands) > 0 {
796 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700797 }
798 },
799 },
800 }}
801}