blob: 9d84281cbd4f48a476eea6014862f44be5064d1d [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
51}
52
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070053type sanitizedPrebuilt interface {
54 hasSanitizedSource(sanitizer string) bool
55}
56
Jiyong Park10e926b2020-07-16 21:38:56 +090057type prebuiltCommonProperties struct {
58 ForceDisable bool `blueprint:"mutated"`
59}
60
61func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
62 return &p.prebuilt
63}
64
65func (p *prebuiltCommon) isForceDisabled() bool {
66 return p.properties.ForceDisable
67}
68
69func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
70 // If the device is configured to use flattened APEX, force disable the prebuilt because
71 // the prebuilt is a non-flattened one.
72 forceDisable := ctx.Config().FlattenApex()
73
74 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
75 // to build the prebuilts themselves.
76 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
77
78 // Force disable the prebuilts when coverage is enabled.
79 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
80 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
81
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070082 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
83 sanitized := ctx.Module().(sanitizedPrebuilt)
84 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
85 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090086
87 if forceDisable && p.prebuilt.SourceExists() {
88 p.properties.ForceDisable = true
89 return true
90 }
91 return false
92}
93
Paul Duffin11216db2021-03-01 14:14:52 +000094// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
95// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
96// deapexer.
97type prebuiltApexSelectorModule struct {
98 android.ModuleBase
99
100 apexFileProperties ApexFileProperties
101
102 inputApex android.Path
103}
104
105func privateApexSelectorModuleFactory() android.Module {
106 module := &prebuiltApexSelectorModule{}
107 module.AddProperties(
108 &module.apexFileProperties,
109 )
110 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
111 return module
112}
113
114func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
115 return android.Paths{p.inputApex}
116}
117
118func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
119 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
120}
121
Jiyong Park09d77522019-11-18 11:16:27 +0900122type Prebuilt struct {
123 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900124 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900125
Paul Duffin11216db2021-03-01 14:14:52 +0000126 properties PrebuiltProperties
127 selectedApexProperties SelectedApexProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900128
129 inputApex android.Path
130 installDir android.InstallPath
131 installFilename string
132 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900133
134 // list of commands to create symlinks for backward compatibility.
135 // these commands will be attached as LOCAL_POST_INSTALL_CMD
136 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900137}
138
Paul Duffin851f3992021-01-13 17:03:51 +0000139type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900140 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000141 //
142 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
143 // for android_common. That is so that it will have the same arch variant as, and so be compatible
144 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000145 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900146 Arch struct {
147 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000148 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900149 }
150 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000151 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900152 }
153 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000154 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900155 }
156 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000157 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900158 }
159 }
Paul Duffin851f3992021-01-13 17:03:51 +0000160}
161
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000162// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
163//
164// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
165// to use methods on it that are specific to the current module.
166//
167// See the ApexFileProperties.Src property.
168func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
169 multiTargets := prebuilt.MultiTargets()
170 if len(multiTargets) != 1 {
171 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
172 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000173 }
174 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000175 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000176 case android.Arm:
177 src = String(p.Arch.Arm.Src)
178 case android.Arm64:
179 src = String(p.Arch.Arm64.Src)
180 case android.X86:
181 src = String(p.Arch.X86.Src)
182 case android.X86_64:
183 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000184 }
185 if src == "" {
186 src = String(p.Src)
187 }
Paul Duffin851f3992021-01-13 17:03:51 +0000188
Paul Duffinc0609c62021-03-01 17:27:16 +0000189 if src == "" {
190 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
191 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
192 // logic from reporting a more general, less useful message.
193 }
194
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000195 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000196}
197
198type PrebuiltProperties struct {
199 ApexFileProperties
Paul Duffin064b70c2020-11-02 17:32:38 +0000200 DeapexerProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900201
202 Installable *bool
203 // Optional name for the installed apex. If unspecified, name of the
204 // module is used as the file name
205 Filename *string
206
207 // Names of modules to be overridden. Listed modules can only be other binaries
208 // (in Make or Soong).
209 // This does not completely prevent installation of the overridden binaries, but if both
210 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
211 // from PRODUCT_PACKAGES.
212 Overrides []string
213}
214
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700215func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
216 return false
217}
218
Jiyong Park09d77522019-11-18 11:16:27 +0900219func (p *Prebuilt) installable() bool {
220 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
221}
222
Jiyong Park09d77522019-11-18 11:16:27 +0900223func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
224 switch tag {
225 case "":
226 return android.Paths{p.outputApex}, nil
227 default:
228 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
229 }
230}
231
232func (p *Prebuilt) InstallFilename() string {
233 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
234}
235
Jiyong Park09d77522019-11-18 11:16:27 +0900236func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900237 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900238}
239
240// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000241//
242// If this needs to make files from within a `.apex` file available for use by other Soong modules,
243// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
244// it does so as follows:
245//
246// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
247// makes them available for use by other modules, at both Soong and ninja levels.
248//
249// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
250// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
251// dexpreopt, will work the same way from source and prebuilt.
252//
253// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
254// itself so that they can retrieve the file paths to those files.
255//
Paul Duffin11216db2021-03-01 14:14:52 +0000256// It also creates a child module `selector` that is responsible for selecting the appropriate
257// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
258// 1. To dedup the selection logic so it only runs in one module.
259// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
260// `apex_set`.
261//
262// prebuilt_apex
263// / | \
264// / | \
265// V | V
266// selector <--- deapexer <--- exported java lib
267//
Jiyong Park09d77522019-11-18 11:16:27 +0900268func PrebuiltFactory() android.Module {
269 module := &Prebuilt{}
Paul Duffin11216db2021-03-01 14:14:52 +0000270 module.AddProperties(&module.properties, &module.selectedApexProperties)
271 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
Jiyong Park09d77522019-11-18 11:16:27 +0900272 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000273
274 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
Paul Duffin11216db2021-03-01 14:14:52 +0000275 baseModuleName := module.BaseModuleName()
276
277 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
278 createApexSelectorModule(ctx, apexSelectorModuleName, &module.properties.ApexFileProperties)
279
280 apexFileSource := ":" + apexSelectorModuleName
281 if len(module.properties.Exported_java_libs) != 0 {
282 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.properties.DeapexerProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000283 }
Paul Duffin11216db2021-03-01 14:14:52 +0000284
285 // Add a source reference to retrieve the selected apex from the selector module.
286 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin064b70c2020-11-02 17:32:38 +0000287 })
288
Jiyong Park09d77522019-11-18 11:16:27 +0900289 return module
290}
291
Paul Duffin11216db2021-03-01 14:14:52 +0000292func createApexSelectorModule(ctx android.LoadHookContext, name string, apexFileProperties *ApexFileProperties) {
293 props := struct {
294 Name *string
295 }{
296 Name: proptools.StringPtr(name),
297 }
298
299 ctx.CreateModule(privateApexSelectorModuleFactory,
300 &props,
301 apexFileProperties,
302 )
303}
304
305func createDeapexerModule(ctx android.LoadHookContext, deapexerName string, apexFileSource string, deapexerProperties *DeapexerProperties) {
306 props := struct {
307 Name *string
308 Selected_apex *string
309 }{
310 Name: proptools.StringPtr(deapexerName),
311 Selected_apex: proptools.StringPtr(apexFileSource),
312 }
313 ctx.CreateModule(privateDeapexerFactory,
314 &props,
315 deapexerProperties,
316 )
317}
318
319func deapexerModuleName(baseModuleName string) string {
320 return baseModuleName + ".deapexer"
321}
322
323func apexSelectorModuleName(baseModuleName string) string {
324 return baseModuleName + ".apex.selector"
325}
326
Paul Duffin064b70c2020-11-02 17:32:38 +0000327func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
328 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
329 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
330 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
331 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
332 // and not a renamed prebuilt module then that will be detected and reported as an error when
333 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100334 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000335 if ctx.OtherModuleExists(prebuiltName) {
336 name = prebuiltName
337 }
338 return name
339}
340
Paul Duffina7139422021-02-08 11:01:58 +0000341type exportedDependencyTag struct {
342 blueprint.BaseDependencyTag
343 name string
344}
345
346// Mark this tag so dependencies that use it are excluded from visibility enforcement.
347//
348// This does allow any prebuilt_apex to reference any module which does open up a small window for
349// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
350// avoids opening up a much bigger window by widening the visibility of modules that need files
351// provided by the prebuilt_apex to include all the possible locations they may be defined, which
352// could include everything below vendor/.
353//
354// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
355// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
356// the .apex file. It will also have to be included in the module's apex_available property too.
357// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
358// incorrectly.
359func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
360
361var (
362 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_lib"}
363)
364
Liz Kammer356f7d42021-01-26 09:18:53 -0500365func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +0000366 // Add dependencies onto the java modules that represent the java libraries that are provided by
367 // and exported from this prebuilt apex.
368 for _, lib := range p.properties.Exported_java_libs {
369 dep := prebuiltApexExportedModuleName(ctx, lib)
Paul Duffina7139422021-02-08 11:01:58 +0000370 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
Paul Duffin064b70c2020-11-02 17:32:38 +0000371 }
372}
373
374var _ ApexInfoMutator = (*Prebuilt)(nil)
375
376// ApexInfoMutator marks any modules for which this apex exports a file as requiring an apex
377// specific variant and checks that they are supported.
378//
379// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
380// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
381//
382// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
383// across prebuilt_apex modules. That is because there is no way to determine whether two
384// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
385// been built from different source at different times or they could have been built with different
386// build options that affect the libraries.
387//
388// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
389// modules were compatible it would be a lot of work and would not provide much benefit for a couple
390// of reasons:
391// * The number of prebuilt_apex modules that will be exporting files for the same module will be
392// low as the prebuilt_apex only exports files for the direct dependencies that require it and
393// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
394// few com.android.art* apex files that contain the same contents and could export files for the
395// same modules but only one of them needs to do so. Contrast that with source apex modules which
396// need apex specific variants for every module that contributes code to the apex, whether direct
397// or indirect.
398// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
399// extra copying of files. Contrast that with source apex modules that has to build each variant
400// from source.
401func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
402
403 // Collect direct dependencies into contents.
404 contents := make(map[string]android.ApexMembership)
405
406 // Collect the list of dependencies.
407 var dependencies []android.ApexModule
408 mctx.VisitDirectDeps(func(m android.Module) {
409 tag := mctx.OtherModuleDependencyTag(m)
Paul Duffina7139422021-02-08 11:01:58 +0000410 if tag == exportedJavaLibTag {
Paul Duffin064b70c2020-11-02 17:32:38 +0000411 depName := mctx.OtherModuleName(m)
412
413 // It is an error if the other module is not a prebuilt.
414 if _, ok := m.(android.PrebuiltInterface); !ok {
415 mctx.PropertyErrorf("exported_java_libs", "%q is not a prebuilt module", depName)
416 return
417 }
418
419 // It is an error if the other module is not an ApexModule.
420 if _, ok := m.(android.ApexModule); !ok {
421 mctx.PropertyErrorf("exported_java_libs", "%q is not usable within an apex", depName)
422 return
423 }
424
425 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
426 // behavior whether there is a corresponding source module present or not.
427 depName = android.RemoveOptionalPrebuiltPrefix(depName)
428
429 // Remember that this module was added as a direct dependency.
430 contents[depName] = contents[depName].Add(true)
431
432 // Add the module to the list of dependencies that need to have an APEX variant.
433 dependencies = append(dependencies, m.(android.ApexModule))
434 }
435 })
436
437 // Create contents for the prebuilt_apex and store it away for later use.
438 apexContents := android.NewApexContents(contents)
439 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
440 Contents: apexContents,
441 })
442
443 // Create an ApexInfo for the prebuilt_apex.
444 apexInfo := android.ApexInfo{
445 ApexVariationName: mctx.ModuleName(),
446 InApexes: []string{mctx.ModuleName()},
447 ApexContents: []*android.ApexContents{apexContents},
448 ForPrebuiltApex: true,
449 }
450
451 // Mark the dependencies of this module as requiring a variant for this module.
452 for _, am := range dependencies {
453 am.BuildForApex(apexInfo)
454 }
Jiyong Park09d77522019-11-18 11:16:27 +0900455}
456
457func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900458 // TODO(jungjw): Check the key validity.
Paul Duffin11216db2021-03-01 14:14:52 +0000459 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900460 p.installDir = android.PathForModuleInstall(ctx, "apex")
461 p.installFilename = p.InstallFilename()
462 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
463 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
464 }
465 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
466 ctx.Build(pctx, android.BuildParams{
467 Rule: android.Cp,
468 Input: p.inputApex,
469 Output: p.outputApex,
470 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900471
472 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800473 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900474 return
475 }
476
Jiyong Park09d77522019-11-18 11:16:27 +0900477 if p.installable() {
478 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
479 }
480
Jooyung Han002ab682020-01-08 01:57:58 +0900481 // in case that prebuilt_apex replaces source apex (using prefer: prop)
482 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
483 // or that prebuilt_apex overrides other apexes (using overrides: prop)
484 for _, overridden := range p.properties.Overrides {
485 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
486 }
Jiyong Park09d77522019-11-18 11:16:27 +0900487}
488
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900489func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
490 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900491 Class: "ETC",
492 OutputFile: android.OptionalPathForPath(p.inputApex),
493 Include: "$(BUILD_PREBUILT)",
494 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700495 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900496 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
497 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
498 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
499 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900500 if len(p.compatSymlinks) > 0 {
501 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
502 }
Jiyong Park09d77522019-11-18 11:16:27 +0900503 },
504 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900505 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900506}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700507
508type ApexSet struct {
509 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900510 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700511
512 properties ApexSetProperties
513
514 installDir android.InstallPath
515 installFilename string
516 outputApex android.WritablePath
517
518 // list of commands to create symlinks for backward compatibility.
519 // these commands will be attached as LOCAL_POST_INSTALL_CMD
520 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900521
522 hostRequired []string
523 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700524}
525
526type ApexSetProperties struct {
527 // the .apks file path that contains prebuilt apex files to be extracted.
528 Set *string
529
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700530 Sanitized struct {
531 None struct {
532 Set *string
533 }
534 Address struct {
535 Set *string
536 }
537 Hwaddress struct {
538 Set *string
539 }
540 }
541
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700542 // whether the extracted apex file installable.
543 Installable *bool
544
545 // optional name for the installed apex. If unspecified, name of the
546 // module is used as the file name
547 Filename *string
548
549 // names of modules to be overridden. Listed modules can only be other binaries
550 // (in Make or Soong).
551 // This does not completely prevent installation of the overridden binaries, but if both
552 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
553 // from PRODUCT_PACKAGES.
554 Overrides []string
555
556 // apexes in this set use prerelease SDK version
557 Prerelease *bool
558}
559
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700560func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
561 var srcs []string
562 if a.properties.Set != nil {
563 srcs = append(srcs, *a.properties.Set)
564 }
565
566 var sanitizers []string
567 if ctx.Host() {
568 sanitizers = ctx.Config().SanitizeHost()
569 } else {
570 sanitizers = ctx.Config().SanitizeDevice()
571 }
572
573 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
574 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
575 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
576 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
577 } else if a.properties.Sanitized.None.Set != nil {
578 srcs = append(srcs, *a.properties.Sanitized.None.Set)
579 }
580
581 return srcs
582}
583
584func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
585 if sanitizer == "address" {
586 return a.properties.Sanitized.Address.Set != nil
587 }
588 if sanitizer == "hwaddress" {
589 return a.properties.Sanitized.Hwaddress.Set != nil
590 }
591
592 return false
593}
594
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700595func (a *ApexSet) installable() bool {
596 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
597}
598
599func (a *ApexSet) InstallFilename() string {
600 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
601}
602
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700603func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900604 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700605}
606
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900607func (a *ApexSet) Overrides() []string {
608 return a.properties.Overrides
609}
610
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700611// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
612func apexSetFactory() android.Module {
613 module := &ApexSet{}
614 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700615
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000616 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700617 return module.prebuiltSrcs(ctx)
618 }
619
620 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700621 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
622 return module
623}
624
625func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
626 a.installFilename = a.InstallFilename()
627 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
628 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
629 }
630
Jiyong Park10e926b2020-07-16 21:38:56 +0900631 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700632 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
633 ctx.Build(pctx,
634 android.BuildParams{
635 Rule: extractMatchingApex,
636 Description: "Extract an apex from an apex set",
637 Inputs: android.Paths{apexSet},
638 Output: a.outputApex,
639 Args: map[string]string{
640 "abis": strings.Join(java.SupportedAbis(ctx), ","),
641 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700642 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700643 },
644 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900645
646 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800647 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900648 return
649 }
650
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700651 a.installDir = android.PathForModuleInstall(ctx, "apex")
652 if a.installable() {
653 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
654 }
655
656 // in case that apex_set replaces source apex (using prefer: prop)
657 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
658 // or that apex_set overrides other apexes (using overrides: prop)
659 for _, overridden := range a.properties.Overrides {
660 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
661 }
Jooyung Han29637162020-06-30 06:34:23 +0900662
663 if ctx.Config().InstallExtraFlattenedApexes() {
664 // flattened apex should be in /system_ext/apex
665 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
666 a.postInstallCommands = append(a.postInstallCommands,
667 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
668 a.outputApex.String(),
669 flattenedApexDir.ToMakePath().String(),
670 ))
671 a.hostRequired = []string{"deapexer", "debugfs"}
672 }
673}
674
675type systemExtContext struct {
676 android.ModuleContext
677}
678
679func (*systemExtContext) SystemExtSpecific() bool {
680 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700681}
682
683func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
684 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900685 Class: "ETC",
686 OutputFile: android.OptionalPathForPath(a.outputApex),
687 Include: "$(BUILD_PREBUILT)",
688 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700689 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700690 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700691 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
692 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
693 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
694 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900695 postInstallCommands := append([]string{}, a.postInstallCommands...)
696 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
697 if len(postInstallCommands) > 0 {
698 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700699 }
700 },
701 },
702 }}
703}