blob: 6292da648fff8cf2c86a626c6ad124544533e046 [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"
24 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090025
26 "github.com/google/blueprint/proptools"
27)
28
Jaewoong Jungfa00c062020-05-14 14:15:24 -070029var (
30 extractMatchingApex = pctx.StaticRule(
31 "extractMatchingApex",
32 blueprint.RuleParams{
33 Command: `rm -rf "$out" && ` +
34 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
35 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
36 `${in}`,
37 CommandDeps: []string{"${extract_apks}"},
38 },
39 "abis", "allow-prereleased", "sdk-version")
40)
41
Jiyong Park10e926b2020-07-16 21:38:56 +090042type prebuilt interface {
43 isForceDisabled() bool
44 InstallFilename() string
45}
46
47type prebuiltCommon struct {
48 prebuilt android.Prebuilt
49 properties prebuiltCommonProperties
50}
51
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070052type sanitizedPrebuilt interface {
53 hasSanitizedSource(sanitizer string) bool
54}
55
Jiyong Park10e926b2020-07-16 21:38:56 +090056type prebuiltCommonProperties struct {
57 ForceDisable bool `blueprint:"mutated"`
58}
59
60func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
61 return &p.prebuilt
62}
63
64func (p *prebuiltCommon) isForceDisabled() bool {
65 return p.properties.ForceDisable
66}
67
68func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
69 // If the device is configured to use flattened APEX, force disable the prebuilt because
70 // the prebuilt is a non-flattened one.
71 forceDisable := ctx.Config().FlattenApex()
72
73 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
74 // to build the prebuilts themselves.
75 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
76
77 // Force disable the prebuilts when coverage is enabled.
78 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
79 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
80
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070081 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
82 sanitized := ctx.Module().(sanitizedPrebuilt)
83 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
84 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +090085
86 if forceDisable && p.prebuilt.SourceExists() {
87 p.properties.ForceDisable = true
88 return true
89 }
90 return false
91}
92
Jiyong Park09d77522019-11-18 11:16:27 +090093type Prebuilt struct {
94 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +090095 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +090096
97 properties PrebuiltProperties
98
99 inputApex android.Path
100 installDir android.InstallPath
101 installFilename string
102 outputApex android.WritablePath
Jooyung Han002ab682020-01-08 01:57:58 +0900103
104 // list of commands to create symlinks for backward compatibility.
105 // these commands will be attached as LOCAL_POST_INSTALL_CMD
106 compatSymlinks []string
Jiyong Park09d77522019-11-18 11:16:27 +0900107}
108
Paul Duffin851f3992021-01-13 17:03:51 +0000109type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900110 // the path to the prebuilt .apex file to import.
Jiyong Park10e926b2020-07-16 21:38:56 +0900111 Source string `blueprint:"mutated"`
Jiyong Park09d77522019-11-18 11:16:27 +0900112
113 Src *string
114 Arch struct {
115 Arm struct {
116 Src *string
117 }
118 Arm64 struct {
119 Src *string
120 }
121 X86 struct {
122 Src *string
123 }
124 X86_64 struct {
125 Src *string
126 }
127 }
Paul Duffin851f3992021-01-13 17:03:51 +0000128}
129
130func (p *ApexFileProperties) selectSource(ctx android.BottomUpMutatorContext) error {
131 // This is called before prebuilt_select and prebuilt_postdeps mutators
132 // The mutators requires that src to be set correctly for each arch so that
133 // arch variants are disabled when src is not provided for the arch.
134 if len(ctx.MultiTargets()) != 1 {
135 return fmt.Errorf("compile_multilib shouldn't be \"both\" for prebuilt_apex")
136 }
137 var src string
138 switch ctx.MultiTargets()[0].Arch.ArchType {
139 case android.Arm:
140 src = String(p.Arch.Arm.Src)
141 case android.Arm64:
142 src = String(p.Arch.Arm64.Src)
143 case android.X86:
144 src = String(p.Arch.X86.Src)
145 case android.X86_64:
146 src = String(p.Arch.X86_64.Src)
147 default:
148 return fmt.Errorf("prebuilt_apex does not support %q", ctx.MultiTargets()[0].Arch.String())
149 }
150 if src == "" {
151 src = String(p.Src)
152 }
153 p.Source = src
154
155 return nil
156}
157
158type PrebuiltProperties struct {
159 ApexFileProperties
Paul Duffin064b70c2020-11-02 17:32:38 +0000160 DeapexerProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900161
162 Installable *bool
163 // Optional name for the installed apex. If unspecified, name of the
164 // module is used as the file name
165 Filename *string
166
167 // Names of modules to be overridden. Listed modules can only be other binaries
168 // (in Make or Soong).
169 // This does not completely prevent installation of the overridden binaries, but if both
170 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
171 // from PRODUCT_PACKAGES.
172 Overrides []string
173}
174
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700175func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
176 return false
177}
178
Jiyong Park09d77522019-11-18 11:16:27 +0900179func (p *Prebuilt) installable() bool {
180 return p.properties.Installable == nil || proptools.Bool(p.properties.Installable)
181}
182
Jiyong Park09d77522019-11-18 11:16:27 +0900183func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
184 switch tag {
185 case "":
186 return android.Paths{p.outputApex}, nil
187 default:
188 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
189 }
190}
191
192func (p *Prebuilt) InstallFilename() string {
193 return proptools.StringDefault(p.properties.Filename, p.BaseModuleName()+imageApexSuffix)
194}
195
Jiyong Park09d77522019-11-18 11:16:27 +0900196func (p *Prebuilt) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900197 return p.prebuiltCommon.prebuilt.Name(p.ModuleBase.Name())
Jiyong Park09d77522019-11-18 11:16:27 +0900198}
199
200// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
Paul Duffin064b70c2020-11-02 17:32:38 +0000201//
202// If this needs to make files from within a `.apex` file available for use by other Soong modules,
203// e.g. make dex implementation jars available for java_import modules isted in exported_java_libs,
204// it does so as follows:
205//
206// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
207// makes them available for use by other modules, at both Soong and ninja levels.
208//
209// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
210// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
211// dexpreopt, will work the same way from source and prebuilt.
212//
213// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
214// itself so that they can retrieve the file paths to those files.
215//
Jiyong Park09d77522019-11-18 11:16:27 +0900216func PrebuiltFactory() android.Module {
217 module := &Prebuilt{}
218 module.AddProperties(&module.properties)
219 android.InitSingleSourcePrebuiltModule(module, &module.properties, "Source")
220 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000221
222 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
223 props := struct {
224 Name *string
225 }{
226 Name: proptools.StringPtr(module.BaseModuleName() + ".deapexer"),
227 }
228 ctx.CreateModule(privateDeapexerFactory,
229 &props,
230 &module.properties.ApexFileProperties,
231 &module.properties.DeapexerProperties,
232 )
233 })
234
Jiyong Park09d77522019-11-18 11:16:27 +0900235 return module
236}
237
Paul Duffin064b70c2020-11-02 17:32:38 +0000238func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
239 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
240 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
241 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
242 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
243 // and not a renamed prebuilt module then that will be detected and reported as an error when
244 // processing the dependency in ApexInfoMutator().
245 prebuiltName := "prebuilt_" + name
246 if ctx.OtherModuleExists(prebuiltName) {
247 name = prebuiltName
248 }
249 return name
250}
251
Liz Kammer356f7d42021-01-26 09:18:53 -0500252func prebuiltSelectSourceMutator(ctx android.BottomUpMutatorContext) {
253 p, ok := ctx.Module().(*Prebuilt)
254 if !ok {
Jiyong Park09d77522019-11-18 11:16:27 +0900255 return
256 }
Liz Kammer356f7d42021-01-26 09:18:53 -0500257 if err := p.properties.selectSource(ctx); err != nil {
258 ctx.ModuleErrorf("%s", err)
259 }
260}
Paul Duffin064b70c2020-11-02 17:32:38 +0000261
Paul Duffina7139422021-02-08 11:01:58 +0000262type exportedDependencyTag struct {
263 blueprint.BaseDependencyTag
264 name string
265}
266
267// Mark this tag so dependencies that use it are excluded from visibility enforcement.
268//
269// This does allow any prebuilt_apex to reference any module which does open up a small window for
270// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
271// avoids opening up a much bigger window by widening the visibility of modules that need files
272// provided by the prebuilt_apex to include all the possible locations they may be defined, which
273// could include everything below vendor/.
274//
275// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
276// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
277// the .apex file. It will also have to be included in the module's apex_available property too.
278// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
279// incorrectly.
280func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
281
282var (
283 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_lib"}
284)
285
Liz Kammer356f7d42021-01-26 09:18:53 -0500286func (p *Prebuilt) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +0000287 // Add dependencies onto the java modules that represent the java libraries that are provided by
288 // and exported from this prebuilt apex.
289 for _, lib := range p.properties.Exported_java_libs {
290 dep := prebuiltApexExportedModuleName(ctx, lib)
Paul Duffina7139422021-02-08 11:01:58 +0000291 ctx.AddFarVariationDependencies(ctx.Config().AndroidCommonTarget.Variations(), exportedJavaLibTag, dep)
Paul Duffin064b70c2020-11-02 17:32:38 +0000292 }
293}
294
295var _ ApexInfoMutator = (*Prebuilt)(nil)
296
297// ApexInfoMutator marks any modules for which this apex exports a file as requiring an apex
298// specific variant and checks that they are supported.
299//
300// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
301// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
302//
303// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
304// across prebuilt_apex modules. That is because there is no way to determine whether two
305// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
306// been built from different source at different times or they could have been built with different
307// build options that affect the libraries.
308//
309// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
310// modules were compatible it would be a lot of work and would not provide much benefit for a couple
311// of reasons:
312// * The number of prebuilt_apex modules that will be exporting files for the same module will be
313// low as the prebuilt_apex only exports files for the direct dependencies that require it and
314// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
315// few com.android.art* apex files that contain the same contents and could export files for the
316// same modules but only one of them needs to do so. Contrast that with source apex modules which
317// need apex specific variants for every module that contributes code to the apex, whether direct
318// or indirect.
319// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
320// extra copying of files. Contrast that with source apex modules that has to build each variant
321// from source.
322func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
323
324 // Collect direct dependencies into contents.
325 contents := make(map[string]android.ApexMembership)
326
327 // Collect the list of dependencies.
328 var dependencies []android.ApexModule
329 mctx.VisitDirectDeps(func(m android.Module) {
330 tag := mctx.OtherModuleDependencyTag(m)
Paul Duffina7139422021-02-08 11:01:58 +0000331 if tag == exportedJavaLibTag {
Paul Duffin064b70c2020-11-02 17:32:38 +0000332 depName := mctx.OtherModuleName(m)
333
334 // It is an error if the other module is not a prebuilt.
335 if _, ok := m.(android.PrebuiltInterface); !ok {
336 mctx.PropertyErrorf("exported_java_libs", "%q is not a prebuilt module", depName)
337 return
338 }
339
340 // It is an error if the other module is not an ApexModule.
341 if _, ok := m.(android.ApexModule); !ok {
342 mctx.PropertyErrorf("exported_java_libs", "%q is not usable within an apex", depName)
343 return
344 }
345
346 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
347 // behavior whether there is a corresponding source module present or not.
348 depName = android.RemoveOptionalPrebuiltPrefix(depName)
349
350 // Remember that this module was added as a direct dependency.
351 contents[depName] = contents[depName].Add(true)
352
353 // Add the module to the list of dependencies that need to have an APEX variant.
354 dependencies = append(dependencies, m.(android.ApexModule))
355 }
356 })
357
358 // Create contents for the prebuilt_apex and store it away for later use.
359 apexContents := android.NewApexContents(contents)
360 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
361 Contents: apexContents,
362 })
363
364 // Create an ApexInfo for the prebuilt_apex.
365 apexInfo := android.ApexInfo{
366 ApexVariationName: mctx.ModuleName(),
367 InApexes: []string{mctx.ModuleName()},
368 ApexContents: []*android.ApexContents{apexContents},
369 ForPrebuiltApex: true,
370 }
371
372 // Mark the dependencies of this module as requiring a variant for this module.
373 for _, am := range dependencies {
374 am.BuildForApex(apexInfo)
375 }
Jiyong Park09d77522019-11-18 11:16:27 +0900376}
377
378func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900379 // TODO(jungjw): Check the key validity.
380 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
381 p.installDir = android.PathForModuleInstall(ctx, "apex")
382 p.installFilename = p.InstallFilename()
383 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
384 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
385 }
386 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
387 ctx.Build(pctx, android.BuildParams{
388 Rule: android.Cp,
389 Input: p.inputApex,
390 Output: p.outputApex,
391 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900392
393 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800394 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900395 return
396 }
397
Jiyong Park09d77522019-11-18 11:16:27 +0900398 if p.installable() {
399 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
400 }
401
Jooyung Han002ab682020-01-08 01:57:58 +0900402 // in case that prebuilt_apex replaces source apex (using prefer: prop)
403 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
404 // or that prebuilt_apex overrides other apexes (using overrides: prop)
405 for _, overridden := range p.properties.Overrides {
406 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
407 }
Jiyong Park09d77522019-11-18 11:16:27 +0900408}
409
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900410func (p *Prebuilt) AndroidMkEntries() []android.AndroidMkEntries {
411 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jiyong Park09d77522019-11-18 11:16:27 +0900412 Class: "ETC",
413 OutputFile: android.OptionalPathForPath(p.inputApex),
414 Include: "$(BUILD_PREBUILT)",
415 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700416 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jiyong Park09d77522019-11-18 11:16:27 +0900417 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
418 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
419 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
420 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.properties.Overrides...)
Jooyung Han002ab682020-01-08 01:57:58 +0900421 if len(p.compatSymlinks) > 0 {
422 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(p.compatSymlinks, " && "))
423 }
Jiyong Park09d77522019-11-18 11:16:27 +0900424 },
425 },
Jiyong Park0b0e1b92019-12-03 13:24:29 +0900426 }}
Jiyong Park09d77522019-11-18 11:16:27 +0900427}
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700428
429type ApexSet struct {
430 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900431 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700432
433 properties ApexSetProperties
434
435 installDir android.InstallPath
436 installFilename string
437 outputApex android.WritablePath
438
439 // list of commands to create symlinks for backward compatibility.
440 // these commands will be attached as LOCAL_POST_INSTALL_CMD
441 compatSymlinks []string
442}
443
444type ApexSetProperties struct {
445 // the .apks file path that contains prebuilt apex files to be extracted.
446 Set *string
447
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700448 Sanitized struct {
449 None struct {
450 Set *string
451 }
452 Address struct {
453 Set *string
454 }
455 Hwaddress struct {
456 Set *string
457 }
458 }
459
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700460 // whether the extracted apex file installable.
461 Installable *bool
462
463 // optional name for the installed apex. If unspecified, name of the
464 // module is used as the file name
465 Filename *string
466
467 // names of modules to be overridden. Listed modules can only be other binaries
468 // (in Make or Soong).
469 // This does not completely prevent installation of the overridden binaries, but if both
470 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
471 // from PRODUCT_PACKAGES.
472 Overrides []string
473
474 // apexes in this set use prerelease SDK version
475 Prerelease *bool
476}
477
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700478func (a *ApexSet) prebuiltSrcs(ctx android.BaseModuleContext) []string {
479 var srcs []string
480 if a.properties.Set != nil {
481 srcs = append(srcs, *a.properties.Set)
482 }
483
484 var sanitizers []string
485 if ctx.Host() {
486 sanitizers = ctx.Config().SanitizeHost()
487 } else {
488 sanitizers = ctx.Config().SanitizeDevice()
489 }
490
491 if android.InList("address", sanitizers) && a.properties.Sanitized.Address.Set != nil {
492 srcs = append(srcs, *a.properties.Sanitized.Address.Set)
493 } else if android.InList("hwaddress", sanitizers) && a.properties.Sanitized.Hwaddress.Set != nil {
494 srcs = append(srcs, *a.properties.Sanitized.Hwaddress.Set)
495 } else if a.properties.Sanitized.None.Set != nil {
496 srcs = append(srcs, *a.properties.Sanitized.None.Set)
497 }
498
499 return srcs
500}
501
502func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
503 if sanitizer == "address" {
504 return a.properties.Sanitized.Address.Set != nil
505 }
506 if sanitizer == "hwaddress" {
507 return a.properties.Sanitized.Hwaddress.Set != nil
508 }
509
510 return false
511}
512
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700513func (a *ApexSet) installable() bool {
514 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
515}
516
517func (a *ApexSet) InstallFilename() string {
518 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
519}
520
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700521func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900522 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700523}
524
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900525func (a *ApexSet) Overrides() []string {
526 return a.properties.Overrides
527}
528
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700529// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
530func apexSetFactory() android.Module {
531 module := &ApexSet{}
532 module.AddProperties(&module.properties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700533
534 srcsSupplier := func(ctx android.BaseModuleContext) []string {
535 return module.prebuiltSrcs(ctx)
536 }
537
538 android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "set")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700539 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
540 return module
541}
542
543func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
544 a.installFilename = a.InstallFilename()
545 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
546 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
547 }
548
Jiyong Park10e926b2020-07-16 21:38:56 +0900549 apexSet := a.prebuiltCommon.prebuilt.SingleSourcePath(ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700550 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
551 ctx.Build(pctx,
552 android.BuildParams{
553 Rule: extractMatchingApex,
554 Description: "Extract an apex from an apex set",
555 Inputs: android.Paths{apexSet},
556 Output: a.outputApex,
557 Args: map[string]string{
558 "abis": strings.Join(java.SupportedAbis(ctx), ","),
559 "allow-prereleased": strconv.FormatBool(proptools.Bool(a.properties.Prerelease)),
Dan Albert4f378d72020-07-23 17:32:15 -0700560 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700561 },
562 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900563
564 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800565 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900566 return
567 }
568
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700569 a.installDir = android.PathForModuleInstall(ctx, "apex")
570 if a.installable() {
571 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
572 }
573
574 // in case that apex_set replaces source apex (using prefer: prop)
575 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
576 // or that apex_set overrides other apexes (using overrides: prop)
577 for _, overridden := range a.properties.Overrides {
578 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
579 }
580}
581
582func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
583 return []android.AndroidMkEntries{android.AndroidMkEntries{
584 Class: "ETC",
585 OutputFile: android.OptionalPathForPath(a.outputApex),
586 Include: "$(BUILD_PREBUILT)",
587 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossf79fee82020-07-03 13:18:24 -0700588 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700589 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
590 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
591 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
592 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
593 if len(a.compatSymlinks) > 0 {
594 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(a.compatSymlinks, " && "))
595 }
596 },
597 },
598 }}
599}