blob: bf8c073043268c32d1f55b77cd0c6820fc501be7 [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"
Paul Duffin3bae0682021-05-05 18:03:47 +010019 "path/filepath"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070020 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090021 "strings"
22
23 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070024 "android/soong/java"
25 "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 {
Paul Duffinef6b6952021-06-15 11:34:01 +010049 android.ModuleBase
Paul Duffinbb0dc132021-05-05 16:58:08 +010050 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010051
Paul Duffinbb0dc132021-05-05 16:58:08 +010052 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010053 prebuiltCommonProperties *PrebuiltCommonProperties
54
55 installDir android.InstallPath
56 installFilename string
57 outputApex android.WritablePath
58
59 // list of commands to create symlinks for backward compatibility.
60 // these commands will be attached as LOCAL_POST_INSTALL_CMD
61 compatSymlinks []string
62
63 hostRequired []string
64 postInstallCommands []string
Jiyong Park10e926b2020-07-16 21:38:56 +090065}
66
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070067type sanitizedPrebuilt interface {
68 hasSanitizedSource(sanitizer string) bool
69}
70
Paul Duffinef6b6952021-06-15 11:34:01 +010071type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010072 SelectedApexProperties
73
Jiyong Park10e926b2020-07-16 21:38:56 +090074 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010075
Paul Duffinef6b6952021-06-15 11:34:01 +010076 // whether the extracted apex file is installable.
77 Installable *bool
78
79 // optional name for the installed apex. If unspecified, name of the
80 // module is used as the file name
81 Filename *string
82
83 // names of modules to be overridden. Listed modules can only be other binaries
84 // (in Make or Soong).
85 // This does not completely prevent installation of the overridden binaries, but if both
86 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
87 // from PRODUCT_PACKAGES.
88 Overrides []string
89
Paul Duffin3bae0682021-05-05 18:03:47 +010090 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
91 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
92 // dexpreopt and boot jars package check.
93 Exported_java_libs []string
94
95 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
96 // bundle will create an APEX variant.
97 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +090098}
99
Paul Duffinef6b6952021-06-15 11:34:01 +0100100// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
101// module that is common to Prebuilt and ApexSet.
102func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
103 p.prebuiltCommonProperties = properties
104 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
105 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
106}
107
Jiyong Park10e926b2020-07-16 21:38:56 +0900108func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
109 return &p.prebuilt
110}
111
112func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100113 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900114}
115
116func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
117 // If the device is configured to use flattened APEX, force disable the prebuilt because
118 // the prebuilt is a non-flattened one.
119 forceDisable := ctx.Config().FlattenApex()
120
121 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
122 // to build the prebuilts themselves.
123 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
124
125 // Force disable the prebuilts when coverage is enabled.
126 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
127 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
128
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700129 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
130 sanitized := ctx.Module().(sanitizedPrebuilt)
131 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
132 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900133
134 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100135 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900136 return true
137 }
138 return false
139}
140
Paul Duffinef6b6952021-06-15 11:34:01 +0100141func (p *prebuiltCommon) InstallFilename() string {
142 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
143}
144
145func (p *prebuiltCommon) Name() string {
146 return p.prebuilt.Name(p.ModuleBase.Name())
147}
148
149func (p *prebuiltCommon) Overrides() []string {
150 return p.prebuiltCommonProperties.Overrides
151}
152
153func (p *prebuiltCommon) installable() bool {
154 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
155}
156
157func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
158 return []android.AndroidMkEntries{
159 {
160 Class: "ETC",
161 OutputFile: android.OptionalPathForPath(p.outputApex),
162 Include: "$(BUILD_PREBUILT)",
163 Host_required: p.hostRequired,
164 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
165 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
166 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
167 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
168 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
169 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
170 postInstallCommands := append([]string{}, p.postInstallCommands...)
171 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
172 if len(postInstallCommands) > 0 {
173 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
174 }
175 },
176 },
177 },
178 }
179}
180
Paul Duffin5dda3e32021-05-05 14:13:27 +0100181// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
182// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
183type prebuiltApexModuleCreator interface {
184 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
185}
186
187// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
188// prebuiltApexModuleCreator's createPrebuiltApexModules method.
189//
190// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
191// will need to access dependencies added by that (exported modules) but must run before the
192// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
193// exported modules.
194func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
195 module := ctx.Module()
196 if creator, ok := module.(prebuiltApexModuleCreator); ok {
197 creator.createPrebuiltApexModules(ctx)
198 }
199}
200
Paul Duffin57f83592021-05-05 15:09:44 +0100201// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
202func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
203 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100204 // Add dependencies onto the java modules that represent the java libraries that are provided by
205 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100206 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100207 dep := android.PrebuiltNameFromSource(exported)
208 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100209 }
Paul Duffin023dba02021-04-22 01:45:29 +0100210
211 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
212 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100213 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100214 dep := android.PrebuiltNameFromSource(exported)
215 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100216 }
Paul Duffindfd33262021-04-06 17:02:08 +0100217}
218
Paul Duffinb17d0442021-05-05 12:07:00 +0100219// Implements android.DepInInSameApex
220func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
221 tag := ctx.OtherModuleDependencyTag(dep)
222 _, ok := tag.(exportedDependencyTag)
223 return ok
224}
225
Paul Duffindfd33262021-04-06 17:02:08 +0100226// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
227// specific variant and checks that they are supported.
228//
229// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
230// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
231//
232// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
233// across prebuilt_apex modules. That is because there is no way to determine whether two
234// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
235// been built from different source at different times or they could have been built with different
236// build options that affect the libraries.
237//
238// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
239// modules were compatible it would be a lot of work and would not provide much benefit for a couple
240// of reasons:
241// * The number of prebuilt_apex modules that will be exporting files for the same module will be
242// low as the prebuilt_apex only exports files for the direct dependencies that require it and
243// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
244// few com.android.art* apex files that contain the same contents and could export files for the
245// same modules but only one of them needs to do so. Contrast that with source apex modules which
246// need apex specific variants for every module that contributes code to the apex, whether direct
247// or indirect.
248// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
249// extra copying of files. Contrast that with source apex modules that has to build each variant
250// from source.
251func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
252
253 // Collect direct dependencies into contents.
254 contents := make(map[string]android.ApexMembership)
255
256 // Collect the list of dependencies.
257 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100258 mctx.WalkDeps(func(child, parent android.Module) bool {
259 // If the child is not in the same apex as the parent then exit immediately and do not visit
260 // any of the child's dependencies.
261 if !android.IsDepInSameApex(mctx, parent, child) {
262 return false
263 }
264
265 tag := mctx.OtherModuleDependencyTag(child)
266 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100267 if exportedTag, ok := tag.(exportedDependencyTag); ok {
268 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100269
270 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100271 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100272 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100273 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100274 }
275
276 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100277 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100278 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100279 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100280 }
Paul Duffindfd33262021-04-06 17:02:08 +0100281 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100282
283 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
284 // behavior whether there is a corresponding source module present or not.
285 depName = android.RemoveOptionalPrebuiltPrefix(depName)
286
287 // Remember if this module was added as a direct dependency.
288 direct := parent == mctx.Module()
289 contents[depName] = contents[depName].Add(direct)
290
291 // Add the module to the list of dependencies that need to have an APEX variant.
292 dependencies = append(dependencies, child.(android.ApexModule))
293
294 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100295 })
296
297 // Create contents for the prebuilt_apex and store it away for later use.
298 apexContents := android.NewApexContents(contents)
299 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
300 Contents: apexContents,
301 })
302
303 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000304 apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName())
Paul Duffindfd33262021-04-06 17:02:08 +0100305 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000306 ApexVariationName: apexVariationName,
307 InApexVariants: []string{apexVariationName},
308 InApexModules: []string{apexVariationName},
Paul Duffindfd33262021-04-06 17:02:08 +0100309 ApexContents: []*android.ApexContents{apexContents},
310 ForPrebuiltApex: true,
311 }
312
313 // Mark the dependencies of this module as requiring a variant for this module.
314 for _, am := range dependencies {
315 am.BuildForApex(apexInfo)
316 }
317}
318
Paul Duffin11216db2021-03-01 14:14:52 +0000319// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
320// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
321// deapexer.
322type prebuiltApexSelectorModule struct {
323 android.ModuleBase
324
325 apexFileProperties ApexFileProperties
326
327 inputApex android.Path
328}
329
330func privateApexSelectorModuleFactory() android.Module {
331 module := &prebuiltApexSelectorModule{}
332 module.AddProperties(
333 &module.apexFileProperties,
334 )
335 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
336 return module
337}
338
339func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
340 return android.Paths{p.inputApex}
341}
342
343func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
344 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
345}
346
Jiyong Park09d77522019-11-18 11:16:27 +0900347type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900348 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900349
Paul Duffinbb0dc132021-05-05 16:58:08 +0100350 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900351
Paul Duffinef6b6952021-06-15 11:34:01 +0100352 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900353}
354
Paul Duffin851f3992021-01-13 17:03:51 +0000355type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900356 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000357 //
358 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
359 // for android_common. That is so that it will have the same arch variant as, and so be compatible
360 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000361 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900362 Arch struct {
363 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000364 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900365 }
366 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000367 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900368 }
369 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000370 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900371 }
372 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000373 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900374 }
375 }
Paul Duffin851f3992021-01-13 17:03:51 +0000376}
377
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000378// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
379//
380// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
381// to use methods on it that are specific to the current module.
382//
383// See the ApexFileProperties.Src property.
384func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
385 multiTargets := prebuilt.MultiTargets()
386 if len(multiTargets) != 1 {
387 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
388 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000389 }
390 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000391 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000392 case android.Arm:
393 src = String(p.Arch.Arm.Src)
394 case android.Arm64:
395 src = String(p.Arch.Arm64.Src)
396 case android.X86:
397 src = String(p.Arch.X86.Src)
398 case android.X86_64:
399 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000400 }
401 if src == "" {
402 src = String(p.Src)
403 }
Paul Duffin851f3992021-01-13 17:03:51 +0000404
Paul Duffinc0609c62021-03-01 17:27:16 +0000405 if src == "" {
406 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
407 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
408 // logic from reporting a more general, less useful message.
409 }
410
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000411 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000412}
413
414type PrebuiltProperties struct {
415 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900416
Paul Duffinef6b6952021-06-15 11:34:01 +0100417 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900418}
419
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700420func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
421 return false
422}
423
Jiyong Park09d77522019-11-18 11:16:27 +0900424func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
425 switch tag {
426 case "":
427 return android.Paths{p.outputApex}, nil
428 default:
429 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
430 }
431}
432
Jiyong Park09d77522019-11-18 11:16:27 +0900433// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
434func PrebuiltFactory() android.Module {
435 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100436 module.AddProperties(&module.properties)
437 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000438
Jiyong Park09d77522019-11-18 11:16:27 +0900439 return module
440}
441
Paul Duffin5dda3e32021-05-05 14:13:27 +0100442func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000443 props := struct {
444 Name *string
445 }{
446 Name: proptools.StringPtr(name),
447 }
448
449 ctx.CreateModule(privateApexSelectorModuleFactory,
450 &props,
451 apexFileProperties,
452 )
453}
454
Paul Duffin5dda3e32021-05-05 14:13:27 +0100455// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
456//
Paul Duffin57f83592021-05-05 15:09:44 +0100457// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
458// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
459// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffinef6b6952021-06-15 11:34:01 +0100460func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *PrebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100461 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100462 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100463 return
464 }
465
Paul Duffin57f83592021-05-05 15:09:44 +0100466 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin3bae0682021-05-05 18:03:47 +0100467 javaModules := []string{}
468 exportedFiles := map[string]string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100469 ctx.WalkDeps(func(child, parent android.Module) bool {
470 tag := ctx.OtherModuleDependencyTag(child)
471
472 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
473 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin3bae0682021-05-05 18:03:47 +0100474 javaModules = append(javaModules, name)
475
476 // Add the dex implementation jar to the set of exported files. The path here must match the
477 // path of the file in the APEX created by apexFileForJavaModule(...).
478 exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
479
Paul Duffin57f83592021-05-05 15:09:44 +0100480 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin57f83592021-05-05 15:09:44 +0100481 // Only visit the children of the bootclasspath_fragment for now.
482 return true
483 }
484
485 return false
486 })
487
Paul Duffin3bae0682021-05-05 18:03:47 +0100488 // Create properties for deapexer module.
489 deapexerProperties := &DeapexerProperties{
490 // Remove any duplicates from the java modules lists as a module may be included via a direct
491 // dependency as well as transitive ones.
492 CommonModules: android.SortedUniqueStrings(javaModules),
493 }
494
495 // Populate the exported files property in a fixed order.
496 for _, tag := range android.SortedStringKeys(exportedFiles) {
497 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
498 Tag: tag,
499 Path: exportedFiles[tag],
500 })
501 }
Paul Duffin57f83592021-05-05 15:09:44 +0100502
Paul Duffin11216db2021-03-01 14:14:52 +0000503 props := struct {
504 Name *string
505 Selected_apex *string
506 }{
507 Name: proptools.StringPtr(deapexerName),
508 Selected_apex: proptools.StringPtr(apexFileSource),
509 }
510 ctx.CreateModule(privateDeapexerFactory,
511 &props,
512 deapexerProperties,
513 )
514}
515
516func deapexerModuleName(baseModuleName string) string {
517 return baseModuleName + ".deapexer"
518}
519
520func apexSelectorModuleName(baseModuleName string) string {
521 return baseModuleName + ".apex.selector"
522}
523
Paul Duffin064b70c2020-11-02 17:32:38 +0000524func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
525 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
526 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
527 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
528 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
529 // and not a renamed prebuilt module then that will be detected and reported as an error when
530 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100531 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000532 if ctx.OtherModuleExists(prebuiltName) {
533 name = prebuiltName
534 }
535 return name
536}
537
Paul Duffina7139422021-02-08 11:01:58 +0000538type exportedDependencyTag struct {
539 blueprint.BaseDependencyTag
540 name string
541}
542
543// Mark this tag so dependencies that use it are excluded from visibility enforcement.
544//
545// This does allow any prebuilt_apex to reference any module which does open up a small window for
546// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
547// avoids opening up a much bigger window by widening the visibility of modules that need files
548// provided by the prebuilt_apex to include all the possible locations they may be defined, which
549// could include everything below vendor/.
550//
551// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
552// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
553// the .apex file. It will also have to be included in the module's apex_available property too.
554// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
555// incorrectly.
556func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
557
558var (
Paul Duffin023dba02021-04-22 01:45:29 +0100559 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
560 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000561)
562
Paul Duffin5dda3e32021-05-05 14:13:27 +0100563var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
564
565// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
566// build.
567//
568// If this needs to make files from within a `.apex` file available for use by other Soong modules,
569// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
570// it does so as follows:
571//
572// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
573// makes them available for use by other modules, at both Soong and ninja levels.
574//
575// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
576// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
577// dexpreopt, will work the same way from source and prebuilt.
578//
579// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
580// itself so that they can retrieve the file paths to those files.
581//
582// It also creates a child module `selector` that is responsible for selecting the appropriate
583// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
584// 1. To dedup the selection logic so it only runs in one module.
585// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
586// `apex_set`.
587//
588// prebuilt_apex
589// / | \
590// / | \
591// V V V
592// selector <--- deapexer <--- exported java lib
593//
594func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
595 baseModuleName := p.BaseModuleName()
596
597 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
598 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
599
600 apexFileSource := ":" + apexSelectorModuleName
Paul Duffinef6b6952021-06-15 11:34:01 +0100601 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100602
603 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100604 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100605}
606
Paul Duffin57f83592021-05-05 15:09:44 +0100607func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
608 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000609}
610
611var _ ApexInfoMutator = (*Prebuilt)(nil)
612
Paul Duffin064b70c2020-11-02 17:32:38 +0000613func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100614 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900615}
616
617func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900618 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100619 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900620 p.installDir = android.PathForModuleInstall(ctx, "apex")
621 p.installFilename = p.InstallFilename()
622 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
623 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
624 }
625 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
626 ctx.Build(pctx, android.BuildParams{
627 Rule: android.Cp,
628 Input: p.inputApex,
629 Output: p.outputApex,
630 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900631
632 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800633 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900634 return
635 }
636
Jiyong Park09d77522019-11-18 11:16:27 +0900637 if p.installable() {
638 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
639 }
640
Jooyung Han002ab682020-01-08 01:57:58 +0900641 // in case that prebuilt_apex replaces source apex (using prefer: prop)
642 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
643 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100644 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900645 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
646 }
Jiyong Park09d77522019-11-18 11:16:27 +0900647}
648
Paul Duffin24704672021-04-06 16:09:30 +0100649// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
650// module. It extracts the correct apex to use and makes it available for use by apex_set.
651type prebuiltApexExtractorModule struct {
652 android.ModuleBase
653
654 properties ApexExtractorProperties
655
656 extractedApex android.WritablePath
657}
658
659func privateApexExtractorModuleFactory() android.Module {
660 module := &prebuiltApexExtractorModule{}
661 module.AddProperties(
662 &module.properties,
663 )
664 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
665 return module
666}
667
668func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
669 return android.Paths{p.extractedApex}
670}
671
672func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
673 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
674 return p.properties.prebuiltSrcs(ctx)
675 }
676 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
677 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
678 ctx.Build(pctx,
679 android.BuildParams{
680 Rule: extractMatchingApex,
681 Description: "Extract an apex from an apex set",
682 Inputs: android.Paths{apexSet},
683 Output: p.extractedApex,
684 Args: map[string]string{
685 "abis": strings.Join(java.SupportedAbis(ctx), ","),
686 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
687 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
688 },
689 })
690}
691
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700692type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900693 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700694
695 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700696}
697
Paul Duffin24704672021-04-06 16:09:30 +0100698type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700699 // the .apks file path that contains prebuilt apex files to be extracted.
700 Set *string
701
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700702 Sanitized struct {
703 None struct {
704 Set *string
705 }
706 Address struct {
707 Set *string
708 }
709 Hwaddress struct {
710 Set *string
711 }
712 }
713
Paul Duffin24704672021-04-06 16:09:30 +0100714 // apexes in this set use prerelease SDK version
715 Prerelease *bool
716}
717
718func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
719 var srcs []string
720 if e.Set != nil {
721 srcs = append(srcs, *e.Set)
722 }
723
724 var sanitizers []string
725 if ctx.Host() {
726 sanitizers = ctx.Config().SanitizeHost()
727 } else {
728 sanitizers = ctx.Config().SanitizeDevice()
729 }
730
731 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
732 srcs = append(srcs, *e.Sanitized.Address.Set)
733 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
734 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
735 } else if e.Sanitized.None.Set != nil {
736 srcs = append(srcs, *e.Sanitized.None.Set)
737 }
738
739 return srcs
740}
741
742type ApexSetProperties struct {
743 ApexExtractorProperties
744
Paul Duffinef6b6952021-06-15 11:34:01 +0100745 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700746}
747
748func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
749 if sanitizer == "address" {
750 return a.properties.Sanitized.Address.Set != nil
751 }
752 if sanitizer == "hwaddress" {
753 return a.properties.Sanitized.Hwaddress.Set != nil
754 }
755
756 return false
757}
758
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700759// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
760func apexSetFactory() android.Module {
761 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100762 module.AddProperties(&module.properties)
763 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100764
Paul Duffin24704672021-04-06 16:09:30 +0100765 return module
766}
767
Paul Duffin5dda3e32021-05-05 14:13:27 +0100768func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100769 props := struct {
770 Name *string
771 }{
772 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700773 }
774
Paul Duffin24704672021-04-06 16:09:30 +0100775 ctx.CreateModule(privateApexExtractorModuleFactory,
776 &props,
777 apexExtractorProperties,
778 )
779}
780
781func apexExtractorModuleName(baseModuleName string) string {
782 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700783}
784
Paul Duffin5dda3e32021-05-05 14:13:27 +0100785var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
786
787// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
788// modules.
789//
790// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
791// prebuilt_apex except that instead of creating a selector module which selects one .apex file
792// from those provided this creates an extractor module which extracts the appropriate .apex file
793// from the zip file containing them.
794func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
795 baseModuleName := a.BaseModuleName()
796
797 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
798 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
799
800 apexFileSource := ":" + apexExtractorModuleName
Paul Duffinef6b6952021-06-15 11:34:01 +0100801 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100802
803 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100804 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100805}
806
Paul Duffin57f83592021-05-05 15:09:44 +0100807func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
808 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100809}
810
811var _ ApexInfoMutator = (*ApexSet)(nil)
812
813func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
814 a.apexInfoMutator(mctx)
815}
816
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700817func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
818 a.installFilename = a.InstallFilename()
819 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
820 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
821 }
822
Paul Duffinbb0dc132021-05-05 16:58:08 +0100823 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700824 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100825 ctx.Build(pctx, android.BuildParams{
826 Rule: android.Cp,
827 Input: inputApex,
828 Output: a.outputApex,
829 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900830
831 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800832 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900833 return
834 }
835
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700836 a.installDir = android.PathForModuleInstall(ctx, "apex")
837 if a.installable() {
838 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
839 }
840
841 // in case that apex_set replaces source apex (using prefer: prop)
842 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
843 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100844 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700845 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
846 }
847}
848
Paul Duffinef6b6952021-06-15 11:34:01 +0100849type systemExtContext struct {
850 android.ModuleContext
851}
852
853func (*systemExtContext) SystemExtSpecific() bool {
854 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700855}