blob: 68f285936f7b83022cfdfd1843144403d9f2f71b [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
Jiyong Park09d77522019-11-18 11:16:27 +090094type Prebuilt struct {
95 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +090096 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +090097
98 properties PrebuiltProperties
99
100 inputApex android.Path
101 installDir android.InstallPath
102 installFilename string
103 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900104
105 // list of commands to create symlinks for backward compatibility.
106 // these commands will be attached as LOCAL_POST_INSTALL_CMD
107 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900108}
109
Paul Duffin851f3992021-01-13 17:03:51 +0000110type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900111 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000112 //
113 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
114 // for android_common. That is so that it will have the same arch variant as, and so be compatible
115 // with, the source `apex` module type that it replaces.
Jiyong Park09d77522019-11-18 11:16:27 +0900116 Src *string
117 Arch struct {
118 Arm struct {
119 Src *string
120 }
121 Arm64 struct {
122 Src *string
123 }
124 X86 struct {
125 Src *string
126 }
127 X86_64 struct {
128 Src *string
129 }
130 }
Paul Duffin851f3992021-01-13 17:03:51 +0000131}
132
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000133// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
134//
135// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
136// to use methods on it that are specific to the current module.
137//
138// See the ApexFileProperties.Src property.
139func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
140 multiTargets := prebuilt.MultiTargets()
141 if len(multiTargets) != 1 {
142 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
143 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000144 }
145 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000146 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000147 case android.Arm:
148 src = String(p.Arch.Arm.Src)
149 case android.Arm64:
150 src = String(p.Arch.Arm64.Src)
151 case android.X86:
152 src = String(p.Arch.X86.Src)
153 case android.X86_64:
154 src = String(p.Arch.X86_64.Src)
155 default:
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000156 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
157 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000158 }
159 if src == "" {
160 src = String(p.Src)
161 }
Paul Duffin851f3992021-01-13 17:03:51 +0000162
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000163 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000164}
165
166type PrebuiltProperties struct {
167 ApexFileProperties
Paul Duffin064b70c2020-11-02 17:32:38 +0000168 DeapexerProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900169
170 Installable *bool
171 // Optional name for the installed apex. If unspecified, name of the
172 // module is used as the file name
173 Filename *string
174
175 // Names of modules to be overridden. Listed modules can only be other binaries
176 // (in Make or Soong).
177 // This does not completely prevent installation of the overridden binaries, but if both
178 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
179 // from PRODUCT_PACKAGES.
180 Overrides []string
181}
182
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700183func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
184 return false
185}
186
Jiyong Park09d77522019-11-18 11:16:27 +0900187func (p *Prebuilt) installable() bool {
188 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
189}
190
Jiyong Park09d77522019-11-18 11:16:27 +0900191func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
192 switch tag {
193 case "":
194 return android.Paths{p.outputApex}, nil
195 default:
196 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
197 }
198}
199
200func (p *Prebuilt) InstallFilename() string {
201 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
202}
203
Jiyong Park09d77522019-11-18 11:16:27 +0900204func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900205 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900206}
207
208// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000209//
210// If this needs to make files from within a `.apex` file available for use by other Soong modules,
211// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
212// it does so as follows:
213//
214// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
215// makes them available for use by other modules, at both Soong and ninja levels.
216//
217// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
218// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
219// dexpreopt, will work the same way from source and prebuilt.
220//
221// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
222// itself so that they can retrieve the file paths to those files.
223//
Jiyong Park09d77522019-11-18 11:16:27 +0900224func PrebuiltFactory() android.Module {
225 module := &Prebuilt{}
226 module.AddProperties(&module.properties)
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000227 android.InitPrebuiltModuleWithSrcSupplier(module, module.properties.prebuiltApexSelector, "src")
Jiyong Park09d77522019-11-18 11:16:27 +0900228 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000229
230 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
231 props := struct {
232 Name *string
233 }{
234 Name: proptools.StringPtr(module.BaseModuleName() + ".deapexer"),
235 }
236 ctx.CreateModule(privateDeapexerFactory,
237 &props,
238 &module.properties.ApexFileProperties,
239 &module.properties.DeapexerProperties,
240 )
241 })
242
Jiyong Park09d77522019-11-18 11:16:27 +0900243 return module
244}
245
Paul Duffin064b70c2020-11-02 17:32:38 +0000246func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
247 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
248 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
249 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
250 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
251 // and not a renamed prebuilt module then that will be detected and reported as an error when
252 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100253 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000254 if ctx.OtherModuleExists(prebuiltName) {
255 name = prebuiltName
256 }
257 return name
258}
259
Paul Duffina7139422021-02-08 11:01:58 +0000260type exportedDependencyTag struct {
261 blueprint.BaseDependencyTag
262 name string
263}
264
265// Mark this tag so dependencies that use it are excluded from visibility enforcement.
266//
267// This does allow any prebuilt_apex to reference any module which does open up a small window for
268// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
269// avoids opening up a much bigger window by widening the visibility of modules that need files
270// provided by the prebuilt_apex to include all the possible locations they may be defined, which
271// could include everything below vendor/.
272//
273// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
274// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
275// the .apex file. It will also have to be included in the module's apex_available property too.
276// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
277// incorrectly.
278func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
279
280var (
281 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_lib"}
282)
283
Liz Kammer356f7d42021-01-26 09:18:53 -0500284func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +0000285 // Add dependencies onto the java modules that represent the java libraries that are provided by
286 // and exported from this prebuilt apex.
287 for _, lib := range p.properties.Exported_java_libs {
288 dep := prebuiltApexExportedModuleName(ctx, lib)
Paul Duffina7139422021-02-08 11:01:58 +0000289 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
Paul Duffin064b70c2020-11-02 17:32:38 +0000290 }
291}
292
293var _ ApexInfoMutator = (*Prebuilt)(nil)
294
295// ApexInfoMutator marks any modules for which this apex exports a file as requiring an apex
296// specific variant and checks that they are supported.
297//
298// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
299// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
300//
301// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
302// across prebuilt_apex modules. That is because there is no way to determine whether two
303// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
304// been built from different source at different times or they could have been built with different
305// build options that affect the libraries.
306//
307// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
308// modules were compatible it would be a lot of work and would not provide much benefit for a couple
309// of reasons:
310// * The number of prebuilt_apex modules that will be exporting files for the same module will be
311// low as the prebuilt_apex only exports files for the direct dependencies that require it and
312// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
313// few com.android.art* apex files that contain the same contents and could export files for the
314// same modules but only one of them needs to do so. Contrast that with source apex modules which
315// need apex specific variants for every module that contributes code to the apex, whether direct
316// or indirect.
317// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
318// extra copying of files. Contrast that with source apex modules that has to build each variant
319// from source.
320func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
321
322 // Collect direct dependencies into contents.
323 contents := make(map[string]android.ApexMembership)
324
325 // Collect the list of dependencies.
326 var dependencies []android.ApexModule
327 mctx.VisitDirectDeps(func(m android.Module) {
328 tag := mctx.OtherModuleDependencyTag(m)
Paul Duffina7139422021-02-08 11:01:58 +0000329 if tag == exportedJavaLibTag {
Paul Duffin064b70c2020-11-02 17:32:38 +0000330 depName := mctx.OtherModuleName(m)
331
332 // It is an error if the other module is not a prebuilt.
333 if _, ok := m.(android.PrebuiltInterface); !ok {
334 mctx.PropertyErrorf("exported_java_libs", "%q is not a prebuilt module", depName)
335 return
336 }
337
338 // It is an error if the other module is not an ApexModule.
339 if _, ok := m.(android.ApexModule); !ok {
340 mctx.PropertyErrorf("exported_java_libs", "%q is not usable within an apex", depName)
341 return
342 }
343
344 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
345 // behavior whether there is a corresponding source module present or not.
346 depName = android.RemoveOptionalPrebuiltPrefix(depName)
347
348 // Remember that this module was added as a direct dependency.
349 contents[depName] = contents[depName].Add(true)
350
351 // Add the module to the list of dependencies that need to have an APEX variant.
352 dependencies = append(dependencies, m.(android.ApexModule))
353 }
354 })
355
356 // Create contents for the prebuilt_apex and store it away for later use.
357 apexContents := android.NewApexContents(contents)
358 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
359 Contents: apexContents,
360 })
361
362 // Create an ApexInfo for the prebuilt_apex.
363 apexInfo := android.ApexInfo{
364 ApexVariationName: mctx.ModuleName(),
365 InApexes: []string{mctx.ModuleName()},
366 ApexContents: []*android.ApexContents{apexContents},
367 ForPrebuiltApex: true,
368 }
369
370 // Mark the dependencies of this module as requiring a variant for this module.
371 for _, am := range dependencies {
372 am.BuildForApex(apexInfo)
373 }
Jiyong Park09d77522019-11-18 11:16:27 +0900374}
375
376func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900377 // TODO(jungjw): Check the key validity.
378 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
379 p.installDir = android.PathForModuleInstall(ctx, "apex")
380 p.installFilename = p.InstallFilename()
381 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
382 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
383 }
384 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
385 ctx.Build(pctx, android.BuildParams{
386 Rule: android.Cp,
387 Input: p.inputApex,
388 Output: p.outputApex,
389 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900390
391 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800392 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900393 return
394 }
395
Jiyong Park09d77522019-11-18 11:16:27 +0900396 if p.installable() {
397 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
398 }
399
Jooyung Han002ab682020-01-08 01:57:58 +0900400 // in case that prebuilt_apex replaces source apex (using prefer: prop)
401 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
402 // or that prebuilt_apex overrides other apexes (using overrides: prop)
403 for _, overridden := range p.properties.Overrides {
404 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
405 }
Jiyong Park09d77522019-11-18 11:16:27 +0900406}
407
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900408func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
409 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900410 Class: "ETC",
411 OutputFile: android.OptionalPathForPath(p.inputApex),
412 Include: "$(BUILD_PREBUILT)",
413 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700414 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900415 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
416 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
417 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
418 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900419 if len(p.compatSymlinks) > 0 {
420 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
421 }
Jiyong Park09d77522019-11-18 11:16:27 +0900422 },
423 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900424 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900425}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700426
427type ApexSet struct {
428 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900429 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700430
431 properties ApexSetProperties
432
433 installDir android.InstallPath
434 installFilename string
435 outputApex android.WritablePath
436
437 // list of commands to create symlinks for backward compatibility.
438 // these commands will be attached as LOCAL_POST_INSTALL_CMD
439 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900440
441 hostRequired []string
442 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700443}
444
445type ApexSetProperties struct {
446 // the .apks file path that contains prebuilt apex files to be extracted.
447 Set *string
448
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700449 Sanitized struct {
450 None struct {
451 Set *string
452 }
453 Address struct {
454 Set *string
455 }
456 Hwaddress struct {
457 Set *string
458 }
459 }
460
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700461 // whether the extracted apex file installable.
462 Installable *bool
463
464 // optional name for the installed apex. If unspecified, name of the
465 // module is used as the file name
466 Filename *string
467
468 // names of modules to be overridden. Listed modules can only be other binaries
469 // (in Make or Soong).
470 // This does not completely prevent installation of the overridden binaries, but if both
471 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
472 // from PRODUCT_PACKAGES.
473 Overrides []string
474
475 // apexes in this set use prerelease SDK version
476 Prerelease *bool
477}
478
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700479func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
480 var srcs []string
481 if a.properties.Set != nil {
482 srcs = append(srcs, *a.properties.Set)
483 }
484
485 var sanitizers []string
486 if ctx.Host() {
487 sanitizers = ctx.Config().SanitizeHost()
488 } else {
489 sanitizers = ctx.Config().SanitizeDevice()
490 }
491
492 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
493 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
494 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
495 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
496 } else if a.properties.Sanitized.None.Set != nil {
497 srcs = append(srcs, *a.properties.Sanitized.None.Set)
498 }
499
500 return srcs
501}
502
503func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
504 if sanitizer == "address" {
505 return a.properties.Sanitized.Address.Set != nil
506 }
507 if sanitizer == "hwaddress" {
508 return a.properties.Sanitized.Hwaddress.Set != nil
509 }
510
511 return false
512}
513
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700514func (a *ApexSet) installable() bool {
515 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
516}
517
518func (a *ApexSet) InstallFilename() string {
519 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
520}
521
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700522func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900523 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700524}
525
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900526func (a *ApexSet) Overrides() []string {
527 return a.properties.Overrides
528}
529
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700530// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
531func apexSetFactory() android.Module {
532 module := &ApexSet{}
533 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700534
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000535 srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string {
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700536 return module.prebuiltSrcs(ctx)
537 }
538
539 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700540 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
541 return module
542}
543
544func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
545 a.installFilename = a.InstallFilename()
546 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
547 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
548 }
549
Jiyong Park10e926b2020-07-16 21:38:56 +0900550 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700551 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
552 ctx.Build(pctx,
553 android.BuildParams{
554 Rule: extractMatchingApex,
555 Description: "Extract an apex from an apex set",
556 Inputs: android.Paths{apexSet},
557 Output: a.outputApex,
558 Args: map[string]string{
559 "abis": strings.Join(java.SupportedAbis(ctx), ","),
560 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700561 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700562 },
563 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900564
565 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800566 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900567 return
568 }
569
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700570 a.installDir = android.PathForModuleInstall(ctx, "apex")
571 if a.installable() {
572 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
573 }
574
575 // in case that apex_set replaces source apex (using prefer: prop)
576 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
577 // or that apex_set overrides other apexes (using overrides: prop)
578 for _, overridden := range a.properties.Overrides {
579 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
580 }
Jooyung Han29637162020-06-30 06:34:23 +0900581
582 if ctx.Config().InstallExtraFlattenedApexes() {
583 // flattened apex should be in /system_ext/apex
584 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
585 a.postInstallCommands = append(a.postInstallCommands,
586 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
587 a.outputApex.String(),
588 flattenedApexDir.ToMakePath().String(),
589 ))
590 a.hostRequired = []string{"deapexer", "debugfs"}
591 }
592}
593
594type systemExtContext struct {
595 android.ModuleContext
596}
597
598func (*systemExtContext) SystemExtSpecific() bool {
599 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700600}
601
602func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
603 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900604 Class: "ETC",
605 OutputFile: android.OptionalPathForPath(a.outputApex),
606 Include: "$(BUILD_PREBUILT)",
607 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700608 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700609 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700610 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
611 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
612 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
613 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900614 postInstallCommands := append([]string{}, a.postInstallCommands...)
615 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
616 if len(postInstallCommands) > 0 {
617 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700618 }
619 },
620 },
621 }}
622}