blob: 10a70a3b7b5d1285a7b2a408237cb56986b72bde [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
Paul Duffin24704672021-04-06 16:09:30 +0100518// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
519// module. It extracts the correct apex to use and makes it available for use by apex_set.
520type prebuiltApexExtractorModule struct {
521 android.ModuleBase
522
523 properties ApexExtractorProperties
524
525 extractedApex android.WritablePath
526}
527
528func privateApexExtractorModuleFactory() android.Module {
529 module := &prebuiltApexExtractorModule{}
530 module.AddProperties(
531 &module.properties,
532 )
533 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
534 return module
535}
536
537func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
538 return android.Paths{p.extractedApex}
539}
540
541func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
542 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
543 return p.properties.prebuiltSrcs(ctx)
544 }
545 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
546 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
547 ctx.Build(pctx,
548 android.BuildParams{
549 Rule: extractMatchingApex,
550 Description: "Extract an apex from an apex set",
551 Inputs: android.Paths{apexSet},
552 Output: p.extractedApex,
553 Args: map[string]string{
554 "abis": strings.Join(java.SupportedAbis(ctx), ","),
555 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
556 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
557 },
558 })
559}
560
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700561type ApexSet struct {
562 android.ModuleBase
Jiyong Park10e926b2020-07-16 21:38:56 +0900563 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700564
565 properties ApexSetProperties
566
567 installDir android.InstallPath
568 installFilename string
569 outputApex android.WritablePath
570
571 // list of commands to create symlinks for backward compatibility.
572 // these commands will be attached as LOCAL_POST_INSTALL_CMD
573 compatSymlinks []string
Jooyung Han29637162020-06-30 06:34:23 +0900574
575 hostRequired []string
576 postInstallCommands []string
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700577}
578
Paul Duffin24704672021-04-06 16:09:30 +0100579type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700580 // the .apks file path that contains prebuilt apex files to be extracted.
581 Set *string
582
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700583 Sanitized struct {
584 None struct {
585 Set *string
586 }
587 Address struct {
588 Set *string
589 }
590 Hwaddress struct {
591 Set *string
592 }
593 }
594
Paul Duffin24704672021-04-06 16:09:30 +0100595 // apexes in this set use prerelease SDK version
596 Prerelease *bool
597}
598
599func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
600 var srcs []string
601 if e.Set != nil {
602 srcs = append(srcs, *e.Set)
603 }
604
605 var sanitizers []string
606 if ctx.Host() {
607 sanitizers = ctx.Config().SanitizeHost()
608 } else {
609 sanitizers = ctx.Config().SanitizeDevice()
610 }
611
612 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
613 srcs = append(srcs, *e.Sanitized.Address.Set)
614 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
615 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
616 } else if e.Sanitized.None.Set != nil {
617 srcs = append(srcs, *e.Sanitized.None.Set)
618 }
619
620 return srcs
621}
622
623type ApexSetProperties struct {
624 ApexExtractorProperties
625
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700626 // whether the extracted apex file installable.
627 Installable *bool
628
629 // optional name for the installed apex. If unspecified, name of the
630 // module is used as the file name
631 Filename *string
632
633 // names of modules to be overridden. Listed modules can only be other binaries
634 // (in Make or Soong).
635 // This does not completely prevent installation of the overridden binaries, but if both
636 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
637 // from PRODUCT_PACKAGES.
638 Overrides []string
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700639}
640
641func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
642 if sanitizer == "address" {
643 return a.properties.Sanitized.Address.Set != nil
644 }
645 if sanitizer == "hwaddress" {
646 return a.properties.Sanitized.Hwaddress.Set != nil
647 }
648
649 return false
650}
651
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700652func (a *ApexSet) installable() bool {
653 return a.properties.Installable == nil || proptools.Bool(a.properties.Installable)
654}
655
656func (a *ApexSet) InstallFilename() string {
657 return proptools.StringDefault(a.properties.Filename, a.BaseModuleName()+imageApexSuffix)
658}
659
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700660func (a *ApexSet) Name() string {
Jiyong Park10e926b2020-07-16 21:38:56 +0900661 return a.prebuiltCommon.prebuilt.Name(a.ModuleBase.Name())
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700662}
663
Jiyong Park8d6c51e2020-06-12 17:26:31 +0900664func (a *ApexSet) Overrides() []string {
665 return a.properties.Overrides
666}
667
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700668// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
669func apexSetFactory() android.Module {
670 module := &ApexSet{}
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100671 module.AddProperties(&module.properties, &module.selectedApexProperties, &module.deapexerProperties)
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700672
Paul Duffin24704672021-04-06 16:09:30 +0100673 android.InitSingleSourcePrebuiltModule(module, &module.selectedApexProperties, "Selected_apex")
674 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
675
676 android.AddLoadHook(module, func(ctx android.LoadHookContext) {
677 baseModuleName := module.BaseModuleName()
678
679 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
680 createApexExtractorModule(ctx, apexExtractorModuleName, &module.properties.ApexExtractorProperties)
681
682 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100683 if len(module.deapexerProperties.Exported_java_libs) != 0 {
684 createDeapexerModule(ctx, deapexerModuleName(baseModuleName), apexFileSource, &module.deapexerProperties)
685 }
Paul Duffin24704672021-04-06 16:09:30 +0100686
687 // After passing the arch specific src properties to the creating the apex selector module
688 module.selectedApexProperties.Selected_apex = proptools.StringPtr(apexFileSource)
689 })
690
691 return module
692}
693
694func createApexExtractorModule(ctx android.LoadHookContext, name string, apexExtractorProperties *ApexExtractorProperties) {
695 props := struct {
696 Name *string
697 }{
698 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700699 }
700
Paul Duffin24704672021-04-06 16:09:30 +0100701 ctx.CreateModule(privateApexExtractorModuleFactory,
702 &props,
703 apexExtractorProperties,
704 )
705}
706
707func apexExtractorModuleName(baseModuleName string) string {
708 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700709}
710
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100711func (a *ApexSet) DepsMutator(ctx android.BottomUpMutatorContext) {
712 a.deapexerDeps(ctx)
713}
714
715var _ ApexInfoMutator = (*ApexSet)(nil)
716
717func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
718 a.apexInfoMutator(mctx)
719}
720
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700721func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
722 a.installFilename = a.InstallFilename()
723 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
724 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
725 }
726
Paul Duffin24704672021-04-06 16:09:30 +0100727 inputApex := android.OptionalPathForModuleSrc(ctx, a.selectedApexProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700728 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100729 ctx.Build(pctx, android.BuildParams{
730 Rule: android.Cp,
731 Input: inputApex,
732 Output: a.outputApex,
733 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900734
735 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800736 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900737 return
738 }
739
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700740 a.installDir = android.PathForModuleInstall(ctx, "apex")
741 if a.installable() {
742 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
743 }
744
745 // in case that apex_set replaces source apex (using prefer: prop)
746 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
747 // or that apex_set overrides other apexes (using overrides: prop)
748 for _, overridden := range a.properties.Overrides {
749 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
750 }
Jooyung Han29637162020-06-30 06:34:23 +0900751
752 if ctx.Config().InstallExtraFlattenedApexes() {
753 // flattened apex should be in /system_ext/apex
754 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
755 a.postInstallCommands = append(a.postInstallCommands,
756 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
757 a.outputApex.String(),
758 flattenedApexDir.ToMakePath().String(),
759 ))
760 a.hostRequired = []string{"deapexer", "debugfs"}
761 }
762}
763
764type systemExtContext struct {
765 android.ModuleContext
766}
767
768func (*systemExtContext) SystemExtSpecific() bool {
769 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700770}
771
772func (a *ApexSet) AndroidMkEntries() []android.AndroidMkEntries {
773 return []android.AndroidMkEntries{android.AndroidMkEntries{
Jooyung Han29637162020-06-30 06:34:23 +0900774 Class: "ETC",
775 OutputFile: android.OptionalPathForPath(a.outputApex),
776 Include: "$(BUILD_PREBUILT)",
777 Host_required: a.hostRequired,
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700778 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
Colin Crossaa255532020-07-03 13:18:24 -0700779 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700780 entries.SetString("LOCAL_MODULE_PATH", a.installDir.ToMakePath().String())
781 entries.SetString("LOCAL_MODULE_STEM", a.installFilename)
782 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !a.installable())
783 entries.AddStrings("LOCAL_OVERRIDES_MODULES", a.properties.Overrides...)
Jooyung Han29637162020-06-30 06:34:23 +0900784 postInstallCommands := append([]string{}, a.postInstallCommands...)
785 postInstallCommands = append(postInstallCommands, a.compatSymlinks...)
786 if len(postInstallCommands) > 0 {
787 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700788 }
789 },
790 },
791 }}
792}