blob: 3893a2bcdd6498b50d2e021d538c0d15e4aa67ea [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.
100 for _, lib := range p.deapexerProperties.Exported_java_libs {
101 dep := prebuiltApexExportedModuleName(ctx, lib)
102 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
103 }
104}
105
106// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
107// specific variant and checks that they are supported.
108//
109// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
110// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
111//
112// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
113// across prebuilt_apex modules. That is because there is no way to determine whether two
114// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
115// been built from different source at different times or they could have been built with different
116// build options that affect the libraries.
117//
118// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
119// modules were compatible it would be a lot of work and would not provide much benefit for a couple
120// of reasons:
121// * The number of prebuilt_apex modules that will be exporting files for the same module will be
122// low as the prebuilt_apex only exports files for the direct dependencies that require it and
123// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
124// few com.android.art* apex files that contain the same contents and could export files for the
125// same modules but only one of them needs to do so. Contrast that with source apex modules which
126// need apex specific variants for every module that contributes code to the apex, whether direct
127// or indirect.
128// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
129// extra copying of files. Contrast that with source apex modules that has to build each variant
130// from source.
131func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
132
133 // Collect direct dependencies into contents.
134 contents := make(map[string]android.ApexMembership)
135
136 // Collect the list of dependencies.
137 var dependencies []android.ApexModule
138 mctx.VisitDirectDeps(func(m android.Module) {
139 tag := mctx.OtherModuleDependencyTag(m)
140 if tag == exportedJavaLibTag {
141 depName := mctx.OtherModuleName(m)
142
143 // It is an error if the other module is not a prebuilt.
144 if _, ok := m.(android.PrebuiltInterface); !ok {
145 mctx.PropertyErrorf("exported_java_libs", "%q is not a prebuilt module", depName)
146 return
147 }
148
149 // It is an error if the other module is not an ApexModule.
150 if _, ok := m.(android.ApexModule); !ok {
151 mctx.PropertyErrorf("exported_java_libs", "%q is not usable within an apex", depName)
152 return
153 }
154
155 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
156 // behavior whether there is a corresponding source module present or not.
157 depName = android.RemoveOptionalPrebuiltPrefix(depName)
158
159 // Remember that this module was added as a direct dependency.
160 contents[depName] = contents[depName].Add(true)
161
162 // Add the module to the list of dependencies that need to have an APEX variant.
163 dependencies = append(dependencies, m.(android.ApexModule))
164 }
165 })
166
167 // Create contents for the prebuilt_apex and store it away for later use.
168 apexContents := android.NewApexContents(contents)
169 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
170 Contents: apexContents,
171 })
172
173 // Create an ApexInfo for the prebuilt_apex.
174 apexInfo := android.ApexInfo{
175 ApexVariationName: mctx.ModuleName(),
176 InApexes: []string{mctx.ModuleName()},
177 ApexContents: []*android.ApexContents{apexContents},
178 ForPrebuiltApex: true,
179 }
180
181 // Mark the dependencies of this module as requiring a variant for this module.
182 for _, am := range dependencies {
183 am.BuildForApex(apexInfo)
184 }
185}
186
Paul Duffin11216db2021-03-01 14:14:52 +0000187// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
188// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
189// deapexer.
190type prebuiltApexSelectorModule struct {
191 android.ModuleBase
192
193 apexFileProperties ApexFileProperties
194
195 inputApex android.Path
196}
197
198func privateApexSelectorModuleFactory() android.Module {
199 module := &prebuiltApexSelectorModule{}
200 module.AddProperties(
201 &module.apexFileProperties,
202 )
203 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
204 return module
205}
206
207func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
208 return android.Paths{p.inputApex}
209}
210
211func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
212 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
213}
214
Jiyong Park09d77522019-11-18 11:16:27 +0900215type Prebuilt struct {
216 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900217 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900218
Paul Duffin11216db2021-03-01 14:14:52 +0000219 properties PrebuiltProperties
220 selectedApexProperties SelectedApexProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900221
222 inputApex android.Path
223 installDir android.InstallPath
224 installFilename string
225 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900226
227 // list of commands to create symlinks for backward compatibility.
228 // these commands will be attached as LOCAL_POST_INSTALL_CMD
229 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900230}
231
Paul Duffin851f3992021-01-13 17:03:51 +0000232type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900233 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000234 //
235 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
236 // for android_common. That is so that it will have the same arch variant as, and so be compatible
237 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000238 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900239 Arch struct {
240 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000241 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900242 }
243 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000244 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900245 }
246 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000247 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900248 }
249 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000250 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900251 }
252 }
Paul Duffin851f3992021-01-13 17:03:51 +0000253}
254
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000255// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
256//
257// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
258// to use methods on it that are specific to the current module.
259//
260// See the ApexFileProperties.Src property.
261func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
262 multiTargets := prebuilt.MultiTargets()
263 if len(multiTargets) != 1 {
264 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
265 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000266 }
267 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000268 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000269 case android.Arm:
270 src = String(p.Arch.Arm.Src)
271 case android.Arm64:
272 src = String(p.Arch.Arm64.Src)
273 case android.X86:
274 src = String(p.Arch.X86.Src)
275 case android.X86_64:
276 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000277 }
278 if src == "" {
279 src = String(p.Src)
280 }
Paul Duffin851f3992021-01-13 17:03:51 +0000281
Paul Duffinc0609c62021-03-01 17:27:16 +0000282 if src == "" {
283 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
284 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
285 // logic from reporting a more general, less useful message.
286 }
287
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000288 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000289}
290
291type PrebuiltProperties struct {
292 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900293
294 Installable *bool
295 // Optional name for the installed apex. If unspecified, name of the
296 // module is used as the file name
297 Filename *string
298
299 // Names of modules to be overridden. Listed modules can only be other binaries
300 // (in Make or Soong).
301 // This does not completely prevent installation of the overridden binaries, but if both
302 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
303 // from PRODUCT_PACKAGES.
304 Overrides []string
305}
306
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700307func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
308 return false
309}
310
Jiyong Park09d77522019-11-18 11:16:27 +0900311func (p *Prebuilt) installable() bool {
312 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
313}
314
Jiyong Park09d77522019-11-18 11:16:27 +0900315func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
316 switch tag {
317 case "":
318 return android.Paths{p.outputApex}, nil
319 default:
320 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
321 }
322}
323
324func (p *Prebuilt) InstallFilename() string {
325 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
326}
327
Jiyong Park09d77522019-11-18 11:16:27 +0900328func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900329 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900330}
331
332// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000333//
334// If this needs to make files from within a `.apex` file available for use by other Soong modules,
335// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
336// it does so as follows:
337//
338// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
339// makes them available for use by other modules, at both Soong and ninja levels.
340//
341// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
342// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
343// dexpreopt, will work the same way from source and prebuilt.
344//
345// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
346// itself so that they can retrieve the file paths to those files.
347//
Paul Duffin11216db2021-03-01 14:14:52 +0000348// It also creates a child module `selector` that is responsible for selecting the appropriate
349// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
350// 1. To dedup the selection logic so it only runs in one module.
351// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
352// `apex_set`.
353//
354// prebuilt_apex
355// / | \
356// / | \
357// V | V
358// selector <--- deapexer <--- exported java lib
359//
Jiyong Park09d77522019-11-18 11:16:27 +0900360func PrebuiltFactory() android.Module {
361 module := &Prebuilt{}
Paul Duffindfd33262021-04-06 17:02:08 +0100362 module.AddProperties(&module.properties, &module.deapexerProperties, &module.selectedApexProperties)
Paul Duffin11216db2021-03-01 14:14:52 +0000363 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900364 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000365
366 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Paul Duffin11216db2021-03-01 14:14:52 +0000367 baseModuleName := module.BaseModuleName()
368
369 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
370 createApexSelectorModule(ctx, apexSelectorModuleName, &module.properties.ApexFileProperties)
371
372 apexFileSource := ":" + apexSelectorModuleName
Paul Duffindfd33262021-04-06 17:02:08 +0100373 if len(module.deapexerProperties.Exported_java_libs) != 0 {
374 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000375 }
Paul Duffin11216db2021-03-01 14:14:52 +0000376
377 // Add a source reference to retrieve the selected apex from the selector module.
378 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin064b70c2020-11-02 17:32:38 +0000379 })
380
Jiyong Park09d77522019-11-18 11:16:27 +0900381 return module
382}
383
Paul Duffin11216db2021-03-01 14:14:52 +0000384func createApexSelectorModule(ctx android.LoadHookContext, name string, apexFileProperties *ApexFileProperties) {
385 props := struct {
386 Name *string
387 }{
388 Name: proptools.StringPtr(name),
389 }
390
391 ctx.CreateModule(privateApexSelectorModuleFactory,
392 &props,
393 apexFileProperties,
394 )
395}
396
397func createDeapexerModule(ctx android.LoadHookContext, deapexerName string, apexFileSource string, deapexerProperties *DeapexerProperties) {
398 props := struct {
399 Name *string
400 Selected_apex *string
401 }{
402 Name: proptools.StringPtr(deapexerName),
403 Selected_apex: proptools.StringPtr(apexFileSource),
404 }
405 ctx.CreateModule(privateDeapexerFactory,
406 &props,
407 deapexerProperties,
408 )
409}
410
411func deapexerModuleName(baseModuleName string) string {
412 return baseModuleName + ".deapexer"
413}
414
415func apexSelectorModuleName(baseModuleName string) string {
416 return baseModuleName + ".apex.selector"
417}
418
Paul Duffin064b70c2020-11-02 17:32:38 +0000419func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
420 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
421 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
422 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
423 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
424 // and not a renamed prebuilt module then that will be detected and reported as an error when
425 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100426 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000427 if ctx.OtherModuleExists(prebuiltName) {
428 name = prebuiltName
429 }
430 return name
431}
432
Paul Duffina7139422021-02-08 11:01:58 +0000433type exportedDependencyTag struct {
434 blueprint.BaseDependencyTag
435 name string
436}
437
438// Mark this tag so dependencies that use it are excluded from visibility enforcement.
439//
440// This does allow any prebuilt_apex to reference any module which does open up a small window for
441// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
442// avoids opening up a much bigger window by widening the visibility of modules that need files
443// provided by the prebuilt_apex to include all the possible locations they may be defined, which
444// could include everything below vendor/.
445//
446// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
447// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
448// the .apex file. It will also have to be included in the module's apex_available property too.
449// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
450// incorrectly.
451func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
452
453var (
454 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_lib"}
455)
456
Liz Kammer356f7d42021-01-26 09:18:53 -0500457func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100458 p.deapexerDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000459}
460
461var _ ApexInfoMutator = (*Prebuilt)(nil)
462
Paul Duffin064b70c2020-11-02 17:32:38 +0000463func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100464 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900465}
466
467func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900468 // TODO(jungjw): Check the key validity.
Paul Duffin11216db2021-03-01 14:14:52 +0000469 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900470 p.installDir = android.PathForModuleInstall(ctx, "apex")
471 p.installFilename = p.InstallFilename()
472 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
473 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
474 }
475 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
476 ctx.Build(pctx, android.BuildParams{
477 Rule: android.Cp,
478 Input: p.inputApex,
479 Output: p.outputApex,
480 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900481
482 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800483 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900484 return
485 }
486
Jiyong Park09d77522019-11-18 11:16:27 +0900487 if p.installable() {
488 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
489 }
490
Jooyung Han002ab682020-01-08 01:57:58 +0900491 // in case that prebuilt_apex replaces source apex (using prefer: prop)
492 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
493 // or that prebuilt_apex overrides other apexes (using overrides: prop)
494 for _, overridden := range p.properties.Overrides {
495 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
496 }
Jiyong Park09d77522019-11-18 11:16:27 +0900497}
498
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900499func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
500 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900501 Class: "ETC",
502 OutputFile: android.OptionalPathForPath(p.inputApex),
503 Include: "$(BUILD_PREBUILT)",
504 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700505 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900506 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
507 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
508 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
509 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900510 if len(p.compatSymlinks) > 0 {
511 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
512 }
Jiyong Park09d77522019-11-18 11:16:27 +0900513 },
514 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900515 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900516}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700517
518type ApexSet struct {
519 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900520 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700521
522 properties ApexSetProperties
523
524 installDir android.InstallPath
525 installFilename string
526 outputApex android.WritablePath
527
528 // list of commands to create symlinks for backward compatibility.
529 // these commands will be attached as LOCAL_POST_INSTALL_CMD
530 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900531
532 hostRequired []string
533 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700534}
535
536type ApexSetProperties struct {
537 // the .apks file path that contains prebuilt apex files to be extracted.
538 Set *string
539
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700540 Sanitized struct {
541 None struct {
542 Set *string
543 }
544 Address struct {
545 Set *string
546 }
547 Hwaddress struct {
548 Set *string
549 }
550 }
551
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700552 // whether the extracted apex file installable.
553 Installable *bool
554
555 // optional name for the installed apex. If unspecified, name of the
556 // module is used as the file name
557 Filename *string
558
559 // names of modules to be overridden. Listed modules can only be other binaries
560 // (in Make or Soong).
561 // This does not completely prevent installation of the overridden binaries, but if both
562 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
563 // from PRODUCT_PACKAGES.
564 Overrides []string
565
566 // apexes in this set use prerelease SDK version
567 Prerelease *bool
568}
569
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700570func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
571 var srcs []string
572 if a.properties.Set != nil {
573 srcs = append(srcs, *a.properties.Set)
574 }
575
576 var sanitizers []string
577 if ctx.Host() {
578 sanitizers = ctx.Config().SanitizeHost()
579 } else {
580 sanitizers = ctx.Config().SanitizeDevice()
581 }
582
583 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
584 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
585 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
586 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
587 } else if a.properties.Sanitized.None.Set != nil {
588 srcs = append(srcs, *a.properties.Sanitized.None.Set)
589 }
590
591 return srcs
592}
593
594func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
595 if sanitizer == "address" {
596 return a.properties.Sanitized.Address.Set != nil
597 }
598 if sanitizer == "hwaddress" {
599 return a.properties.Sanitized.Hwaddress.Set != nil
600 }
601
602 return false
603}
604
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700605func (a *ApexSet) installable() bool {
606 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
607}
608
609func (a *ApexSet) InstallFilename() string {
610 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
611}
612
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700613func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900614 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700615}
616
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900617func (a *ApexSet) Overrides() []string {
618 return a.properties.Overrides
619}
620
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700621// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
622func apexSetFactory() android.Module {
623 module := &ApexSet{}
624 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700625
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000626 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700627 return module.prebuiltSrcs(ctx)
628 }
629
630 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700631 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
632 return module
633}
634
635func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
636 a.installFilename = a.InstallFilename()
637 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
638 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
639 }
640
Jiyong Park10e926b2020-07-16 21:38:56 +0900641 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700642 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
643 ctx.Build(pctx,
644 android.BuildParams{
645 Rule: extractMatchingApex,
646 Description: "Extract an apex from an apex set",
647 Inputs: android.Paths{apexSet},
648 Output: a.outputApex,
649 Args: map[string]string{
650 "abis": strings.Join(java.SupportedAbis(ctx), ","),
651 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700652 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700653 },
654 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900655
656 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800657 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900658 return
659 }
660
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700661 a.installDir = android.PathForModuleInstall(ctx, "apex")
662 if a.installable() {
663 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
664 }
665
666 // in case that apex_set replaces source apex (using prefer: prop)
667 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
668 // or that apex_set overrides other apexes (using overrides: prop)
669 for _, overridden := range a.properties.Overrides {
670 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
671 }
Jooyung Han29637162020-06-30 06:34:23 +0900672
673 if ctx.Config().InstallExtraFlattenedApexes() {
674 // flattened apex should be in /system_ext/apex
675 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
676 a.postInstallCommands = append(a.postInstallCommands,
677 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
678 a.outputApex.String(),
679 flattenedApexDir.ToMakePath().String(),
680 ))
681 a.hostRequired = []string{"deapexer", "debugfs"}
682 }
683}
684
685type systemExtContext struct {
686 android.ModuleContext
687}
688
689func (*systemExtContext) SystemExtSpecific() bool {
690 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700691}
692
693func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
694 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900695 Class: "ETC",
696 OutputFile: android.OptionalPathForPath(a.outputApex),
697 Include: "$(BUILD_PREBUILT)",
698 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700699 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700700 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700701 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
702 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
703 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
704 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900705 postInstallCommands := append([]string{}, a.postInstallCommands...)
706 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
707 if len(postInstallCommands) > 0 {
708 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700709 }
710 },
711 },
712 }}
713}