blob: 4833a644bc42a5d6e0ce2e6e0c8ec8f793414740 [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 Duffina35f8db2021-06-15 19:10:11 +010019 "io"
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"
Jiyong Park10e926b2020-07-16 21:38:56 +090025
Jaewoong Jungfa00c062020-05-14 14:15:24 -070026 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090027 "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 Duffina9c81102021-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 Duffina9c81102021-06-15 11:34:01 +010053 prebuiltCommonProperties *PrebuiltCommonProperties
54
55 installDir android.InstallPath
56 installFilename string
57 outputApex android.WritablePath
58
Paul Duffina35f8db2021-06-15 19:10:11 +010059 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
60 // to create make modules in prebuiltCommon.AndroidMkEntries.
61 apexFilesForAndroidMk []apexFile
62
Paul Duffina9c81102021-06-15 11:34:01 +010063 // list of commands to create symlinks for backward compatibility.
64 // these commands will be attached as LOCAL_POST_INSTALL_CMD
65 compatSymlinks []string
66
67 hostRequired []string
68 postInstallCommands []string
Jiyong Park10e926b2020-07-16 21:38:56 +090069}
70
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070071type sanitizedPrebuilt interface {
72 hasSanitizedSource(sanitizer string) bool
73}
74
Paul Duffina9c81102021-06-15 11:34:01 +010075type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010076 SelectedApexProperties
77
Martin Stjernholmbfffae72021-06-24 14:37:13 +010078 // Canonical name of this APEX. Used to determine the path to the activated APEX on
79 // device (/apex/<apex_name>). If unspecified, follows the name property.
80 Apex_name *string
81
Jiyong Park10e926b2020-07-16 21:38:56 +090082 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010083
Paul Duffina9c81102021-06-15 11:34:01 +010084 // whether the extracted apex file is installable.
85 Installable *bool
86
87 // optional name for the installed apex. If unspecified, name of the
88 // module is used as the file name
89 Filename *string
90
91 // names of modules to be overridden. Listed modules can only be other binaries
92 // (in Make or Soong).
93 // This does not completely prevent installation of the overridden binaries, but if both
94 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
95 // from PRODUCT_PACKAGES.
96 Overrides []string
97
Paul Duffin3bae0682021-05-05 18:03:47 +010098 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
99 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
100 // dexpreopt and boot jars package check.
101 Exported_java_libs []string
102
103 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
104 // bundle will create an APEX variant.
105 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900106}
107
Paul Duffina9c81102021-06-15 11:34:01 +0100108// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
109// module that is common to Prebuilt and ApexSet.
110func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
111 p.prebuiltCommonProperties = properties
112 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
113 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
114}
115
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100116func (p *prebuiltCommon) ApexVariationName() string {
117 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
118}
119
Jiyong Park10e926b2020-07-16 21:38:56 +0900120func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
121 return &p.prebuilt
122}
123
124func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100125 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900126}
127
128func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
129 // If the device is configured to use flattened APEX, force disable the prebuilt because
130 // the prebuilt is a non-flattened one.
131 forceDisable := ctx.Config().FlattenApex()
132
133 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
134 // to build the prebuilts themselves.
135 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
136
137 // Force disable the prebuilts when coverage is enabled.
138 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
139 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
140
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700141 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
142 sanitized := ctx.Module().(sanitizedPrebuilt)
143 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
144 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900145
146 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100147 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900148 return true
149 }
150 return false
151}
152
Paul Duffina9c81102021-06-15 11:34:01 +0100153func (p *prebuiltCommon) InstallFilename() string {
154 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
155}
156
157func (p *prebuiltCommon) Name() string {
158 return p.prebuilt.Name(p.ModuleBase.Name())
159}
160
161func (p *prebuiltCommon) Overrides() []string {
162 return p.prebuiltCommonProperties.Overrides
163}
164
165func (p *prebuiltCommon) installable() bool {
166 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
167}
168
Paul Duffina35f8db2021-06-15 19:10:11 +0100169// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
170// modules that this depends upon.
171func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
172 // Walk the dependencies of this module looking for the java modules that it exports.
173 ctx.WalkDeps(func(child, parent android.Module) bool {
174 tag := ctx.OtherModuleDependencyTag(child)
175
176 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
177 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
178 // If the exported java module provides a dex jar path then add it to the list of apexFiles.
179 path := child.(interface{ DexJarBuildPath() android.Path }).DexJarBuildPath()
180 if path != nil {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000181 af := apexFile{
Paul Duffina35f8db2021-06-15 19:10:11 +0100182 module: child,
183 moduleDir: ctx.OtherModuleDir(child),
184 androidMkModuleName: name,
185 builtFile: path,
186 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000187 }
188 if module, ok := child.(java.DexpreopterInterface); ok {
189 for _, install := range module.DexpreoptBuiltInstalledForApex() {
190 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
191 }
192 }
193 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffina35f8db2021-06-15 19:10:11 +0100194 }
195 } else if tag == exportedBootclasspathFragmentTag {
196 // Visit the children of the bootclasspath_fragment.
197 return true
198 }
199
200 return false
201 })
202}
203
Jiakai Zhang204356f2021-09-09 08:12:46 +0000204func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
205 for _, fi := range p.apexFilesForAndroidMk {
206 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
207 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
208 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
209 }
210}
211
Paul Duffina9c81102021-06-15 11:34:01 +0100212func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffina35f8db2021-06-15 19:10:11 +0100213 entriesList := []android.AndroidMkEntries{
Paul Duffina9c81102021-06-15 11:34:01 +0100214 {
215 Class: "ETC",
216 OutputFile: android.OptionalPathForPath(p.outputApex),
217 Include: "$(BUILD_PREBUILT)",
218 Host_required: p.hostRequired,
219 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
220 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
221 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
222 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
223 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
224 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
225 postInstallCommands := append([]string{}, p.postInstallCommands...)
226 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
227 if len(postInstallCommands) > 0 {
228 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
229 }
Jiakai Zhang204356f2021-09-09 08:12:46 +0000230 p.addRequiredModules(entries)
Paul Duffina9c81102021-06-15 11:34:01 +0100231 },
232 },
233 },
234 }
Paul Duffina35f8db2021-06-15 19:10:11 +0100235
236 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
237 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
238 // apex specific variants of the exported java modules available for use from within make.
239 apexName := p.BaseModuleName()
240 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin155c1772021-06-17 13:33:09 +0100241 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffina35f8db2021-06-15 19:10:11 +0100242 entriesList = append(entriesList, entries)
243 }
244
245 return entriesList
Paul Duffina9c81102021-06-15 11:34:01 +0100246}
247
Paul Duffin155c1772021-06-17 13:33:09 +0100248// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
249func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
250 moduleName := fi.androidMkModuleName + "." + apexName
251 entries := android.AndroidMkEntries{
252 Class: fi.class.nameInMake(),
253 OverrideName: moduleName,
254 OutputFile: android.OptionalPathForPath(fi.builtFile),
255 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
256 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
257 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
258 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
259
260 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
261 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
262 // we will have foo.jar.jar
263 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
264 var classesJar android.Path
265 var headerJar android.Path
266 if javaModule, ok := fi.module.(java.ApexDependency); ok {
267 classesJar = javaModule.ImplementationAndResourcesJars()[0]
268 headerJar = javaModule.HeaderJars()[0]
269 } else {
270 classesJar = fi.builtFile
271 headerJar = fi.builtFile
272 }
273 entries.SetString("LOCAL_SOONG_CLASSES_JAR", classesJar.String())
274 entries.SetString("LOCAL_SOONG_HEADER_JAR", headerJar.String())
275 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
276 entries.SetString("LOCAL_DEX_PREOPT", "false")
277 },
278 },
279 ExtraFooters: []android.AndroidMkExtraFootersFunc{
280 func(w io.Writer, name, prefix, moduleDir string) {
281 // m <module_name> will build <module_name>.<apex_name> as well.
282 if fi.androidMkModuleName != moduleName {
283 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
284 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
285 }
286 },
287 },
288 }
289 return entries
290}
291
Paul Duffin5dda3e32021-05-05 14:13:27 +0100292// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
293// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
294type prebuiltApexModuleCreator interface {
295 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
296}
297
298// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
299// prebuiltApexModuleCreator's createPrebuiltApexModules method.
300//
301// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
302// will need to access dependencies added by that (exported modules) but must run before the
303// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
304// exported modules.
305func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
306 module := ctx.Module()
307 if creator, ok := module.(prebuiltApexModuleCreator); ok {
308 creator.createPrebuiltApexModules(ctx)
309 }
310}
311
Paul Duffin57f83592021-05-05 15:09:44 +0100312// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
313func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
314 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100315 // Add dependencies onto the java modules that represent the java libraries that are provided by
316 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100317 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100318 dep := android.PrebuiltNameFromSource(exported)
319 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100320 }
Paul Duffin023dba02021-04-22 01:45:29 +0100321
322 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
323 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100324 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100325 dep := android.PrebuiltNameFromSource(exported)
326 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100327 }
Paul Duffindfd33262021-04-06 17:02:08 +0100328}
329
Paul Duffinb17d0442021-05-05 12:07:00 +0100330// Implements android.DepInInSameApex
331func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
332 tag := ctx.OtherModuleDependencyTag(dep)
333 _, ok := tag.(exportedDependencyTag)
334 return ok
335}
336
Paul Duffindfd33262021-04-06 17:02:08 +0100337// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
338// specific variant and checks that they are supported.
339//
340// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
341// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
342//
343// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
344// across prebuilt_apex modules. That is because there is no way to determine whether two
345// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
346// been built from different source at different times or they could have been built with different
347// build options that affect the libraries.
348//
349// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
350// modules were compatible it would be a lot of work and would not provide much benefit for a couple
351// of reasons:
352// * The number of prebuilt_apex modules that will be exporting files for the same module will be
353// low as the prebuilt_apex only exports files for the direct dependencies that require it and
354// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
355// few com.android.art* apex files that contain the same contents and could export files for the
356// same modules but only one of them needs to do so. Contrast that with source apex modules which
357// need apex specific variants for every module that contributes code to the apex, whether direct
358// or indirect.
359// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
360// extra copying of files. Contrast that with source apex modules that has to build each variant
361// from source.
362func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
363
364 // Collect direct dependencies into contents.
365 contents := make(map[string]android.ApexMembership)
366
367 // Collect the list of dependencies.
368 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100369 mctx.WalkDeps(func(child, parent android.Module) bool {
370 // If the child is not in the same apex as the parent then exit immediately and do not visit
371 // any of the child's dependencies.
372 if !android.IsDepInSameApex(mctx, parent, child) {
373 return false
374 }
375
376 tag := mctx.OtherModuleDependencyTag(child)
377 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100378 if exportedTag, ok := tag.(exportedDependencyTag); ok {
379 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100380
381 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100382 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100383 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100384 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100385 }
386
387 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100388 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100389 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100390 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100391 }
Paul Duffindfd33262021-04-06 17:02:08 +0100392 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100393
Paul Duffin46ab2912021-06-29 18:38:38 +0100394 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
395 // variant.
396 if _, ok := child.(android.ApexModule); !ok {
397 return false
398 }
399
Paul Duffinb17d0442021-05-05 12:07:00 +0100400 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
401 // behavior whether there is a corresponding source module present or not.
402 depName = android.RemoveOptionalPrebuiltPrefix(depName)
403
404 // Remember if this module was added as a direct dependency.
405 direct := parent == mctx.Module()
406 contents[depName] = contents[depName].Add(direct)
407
408 // Add the module to the list of dependencies that need to have an APEX variant.
409 dependencies = append(dependencies, child.(android.ApexModule))
410
411 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100412 })
413
414 // Create contents for the prebuilt_apex and store it away for later use.
415 apexContents := android.NewApexContents(contents)
416 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
417 Contents: apexContents,
418 })
419
420 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100421 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100422 apexInfo := android.ApexInfo{
Martin Stjernholmbe105032021-05-26 16:57:39 +0100423 ApexVariationName: apexVariationName,
424 InApexVariants: []string{apexVariationName},
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100425 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100426 ApexContents: []*android.ApexContents{apexContents},
427 ForPrebuiltApex: true,
428 }
429
430 // Mark the dependencies of this module as requiring a variant for this module.
431 for _, am := range dependencies {
432 am.BuildForApex(apexInfo)
433 }
434}
435
Paul Duffin11216db2021-03-01 14:14:52 +0000436// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
437// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
438// deapexer.
439type prebuiltApexSelectorModule struct {
440 android.ModuleBase
441
442 apexFileProperties ApexFileProperties
443
444 inputApex android.Path
445}
446
447func privateApexSelectorModuleFactory() android.Module {
448 module := &prebuiltApexSelectorModule{}
449 module.AddProperties(
450 &module.apexFileProperties,
451 )
452 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
453 return module
454}
455
456func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
457 return android.Paths{p.inputApex}
458}
459
460func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
461 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
462}
463
Jiyong Park09d77522019-11-18 11:16:27 +0900464type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900465 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900466
Paul Duffinbb0dc132021-05-05 16:58:08 +0100467 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900468
Paul Duffina9c81102021-06-15 11:34:01 +0100469 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900470}
471
Paul Duffin851f3992021-01-13 17:03:51 +0000472type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900473 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000474 //
475 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
476 // for android_common. That is so that it will have the same arch variant as, and so be compatible
477 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000478 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900479 Arch struct {
480 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000481 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900482 }
483 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000484 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900485 }
486 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000487 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900488 }
489 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000490 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900491 }
492 }
Paul Duffin851f3992021-01-13 17:03:51 +0000493}
494
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000495// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
496//
497// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
498// to use methods on it that are specific to the current module.
499//
500// See the ApexFileProperties.Src property.
501func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
502 multiTargets := prebuilt.MultiTargets()
503 if len(multiTargets) != 1 {
504 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
505 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000506 }
507 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000508 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000509 case android.Arm:
510 src = String(p.Arch.Arm.Src)
511 case android.Arm64:
512 src = String(p.Arch.Arm64.Src)
513 case android.X86:
514 src = String(p.Arch.X86.Src)
515 case android.X86_64:
516 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000517 }
518 if src == "" {
519 src = String(p.Src)
520 }
Paul Duffin851f3992021-01-13 17:03:51 +0000521
Paul Duffinc0609c62021-03-01 17:27:16 +0000522 if src == "" {
523 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
524 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
525 // logic from reporting a more general, less useful message.
526 }
527
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000528 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000529}
530
531type PrebuiltProperties struct {
532 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900533
Paul Duffina9c81102021-06-15 11:34:01 +0100534 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900535}
536
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700537func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
538 return false
539}
540
Jiyong Park09d77522019-11-18 11:16:27 +0900541func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
542 switch tag {
543 case "":
544 return android.Paths{p.outputApex}, nil
545 default:
546 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
547 }
548}
549
Jiyong Park09d77522019-11-18 11:16:27 +0900550// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
551func PrebuiltFactory() android.Module {
552 module := &Prebuilt{}
Paul Duffina9c81102021-06-15 11:34:01 +0100553 module.AddProperties(&module.properties)
554 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000555
Jiyong Park09d77522019-11-18 11:16:27 +0900556 return module
557}
558
Paul Duffin5dda3e32021-05-05 14:13:27 +0100559func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000560 props := struct {
561 Name *string
562 }{
563 Name: proptools.StringPtr(name),
564 }
565
566 ctx.CreateModule(privateApexSelectorModuleFactory,
567 &props,
568 apexFileProperties,
569 )
570}
571
Paul Duffin5dda3e32021-05-05 14:13:27 +0100572// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
573//
Paul Duffin57f83592021-05-05 15:09:44 +0100574// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
575// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
576// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffina9c81102021-06-15 11:34:01 +0100577func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *PrebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100578 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100579 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100580 return
581 }
582
Paul Duffin57f83592021-05-05 15:09:44 +0100583 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin5466a362021-06-07 10:25:31 +0100584 commonModules := []string{}
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100585 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100586 ctx.WalkDeps(func(child, parent android.Module) bool {
587 tag := ctx.OtherModuleDependencyTag(child)
588
Paul Duffinfef55002021-06-17 14:56:05 +0100589 // If the child is not in the same apex as the parent then ignore it and all its children.
590 if !android.IsDepInSameApex(ctx, parent, child) {
591 return false
592 }
593
Paul Duffin57f83592021-05-05 15:09:44 +0100594 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffinfef55002021-06-17 14:56:05 +0100595 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffin5466a362021-06-07 10:25:31 +0100596 commonModules = append(commonModules, name)
597
598 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100599 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffin5466a362021-06-07 10:25:31 +0100600
Paul Duffinfef55002021-06-17 14:56:05 +0100601 // Visit the dependencies of this module just in case they also require files from the
602 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100603 return true
604 }
605
606 return false
607 })
608
Paul Duffin3bae0682021-05-05 18:03:47 +0100609 // Create properties for deapexer module.
610 deapexerProperties := &DeapexerProperties{
Paul Duffin5466a362021-06-07 10:25:31 +0100611 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100612 // dependency as well as transitive ones.
Paul Duffin5466a362021-06-07 10:25:31 +0100613 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100614 }
615
616 // Populate the exported files property in a fixed order.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100617 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100618
Paul Duffin11216db2021-03-01 14:14:52 +0000619 props := struct {
620 Name *string
621 Selected_apex *string
622 }{
623 Name: proptools.StringPtr(deapexerName),
624 Selected_apex: proptools.StringPtr(apexFileSource),
625 }
626 ctx.CreateModule(privateDeapexerFactory,
627 &props,
628 deapexerProperties,
629 )
630}
631
632func deapexerModuleName(baseModuleName string) string {
633 return baseModuleName + ".deapexer"
634}
635
636func apexSelectorModuleName(baseModuleName string) string {
637 return baseModuleName + ".apex.selector"
638}
639
Paul Duffin064b70c2020-11-02 17:32:38 +0000640func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
641 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
642 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
643 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
644 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
645 // and not a renamed prebuilt module then that will be detected and reported as an error when
646 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100647 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000648 if ctx.OtherModuleExists(prebuiltName) {
649 name = prebuiltName
650 }
651 return name
652}
653
Paul Duffina7139422021-02-08 11:01:58 +0000654type exportedDependencyTag struct {
655 blueprint.BaseDependencyTag
656 name string
657}
658
659// Mark this tag so dependencies that use it are excluded from visibility enforcement.
660//
661// This does allow any prebuilt_apex to reference any module which does open up a small window for
662// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
663// avoids opening up a much bigger window by widening the visibility of modules that need files
664// provided by the prebuilt_apex to include all the possible locations they may be defined, which
665// could include everything below vendor/.
666//
667// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
668// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
669// the .apex file. It will also have to be included in the module's apex_available property too.
670// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
671// incorrectly.
672func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
673
Paul Duffinfef55002021-06-17 14:56:05 +0100674func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
675
676var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
677
Paul Duffina7139422021-02-08 11:01:58 +0000678var (
Paul Duffin023dba02021-04-22 01:45:29 +0100679 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
680 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000681)
682
Paul Duffin5dda3e32021-05-05 14:13:27 +0100683var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
684
685// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
686// build.
687//
688// If this needs to make files from within a `.apex` file available for use by other Soong modules,
689// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
690// it does so as follows:
691//
692// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
693// makes them available for use by other modules, at both Soong and ninja levels.
694//
695// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
696// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
697// dexpreopt, will work the same way from source and prebuilt.
698//
699// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
700// itself so that they can retrieve the file paths to those files.
701//
702// It also creates a child module `selector` that is responsible for selecting the appropriate
703// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
704// 1. To dedup the selection logic so it only runs in one module.
705// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
706// `apex_set`.
707//
708// prebuilt_apex
709// / | \
710// / | \
711// V V V
712// selector <--- deapexer <--- exported java lib
713//
714func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
715 baseModuleName := p.BaseModuleName()
716
717 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
718 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
719
720 apexFileSource := ":" + apexSelectorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100721 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100722
723 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100724 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100725}
726
Paul Duffin57f83592021-05-05 15:09:44 +0100727func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
728 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000729}
730
731var _ ApexInfoMutator = (*Prebuilt)(nil)
732
Paul Duffin064b70c2020-11-02 17:32:38 +0000733func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100734 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900735}
736
737func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900738 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100739 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900740 p.installDir = android.PathForModuleInstall(ctx, "apex")
741 p.installFilename = p.InstallFilename()
742 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
743 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
744 }
745 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
746 ctx.Build(pctx, android.BuildParams{
747 Rule: android.Cp,
748 Input: p.inputApex,
749 Output: p.outputApex,
750 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900751
752 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800753 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900754 return
755 }
756
Paul Duffina35f8db2021-06-15 19:10:11 +0100757 // Save the files that need to be made available to Make.
758 p.initApexFilesForAndroidMk(ctx)
759
Jiyong Park09d77522019-11-18 11:16:27 +0900760 if p.installable() {
761 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
762 }
763
Jooyung Han002ab682020-01-08 01:57:58 +0900764 // in case that prebuilt_apex replaces source apex (using prefer: prop)
765 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
766 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100767 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900768 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
769 }
Jiyong Park09d77522019-11-18 11:16:27 +0900770}
771
Paul Duffin24704672021-04-06 16:09:30 +0100772// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
773// module. It extracts the correct apex to use and makes it available for use by apex_set.
774type prebuiltApexExtractorModule struct {
775 android.ModuleBase
776
777 properties ApexExtractorProperties
778
779 extractedApex android.WritablePath
780}
781
782func privateApexExtractorModuleFactory() android.Module {
783 module := &prebuiltApexExtractorModule{}
784 module.AddProperties(
785 &module.properties,
786 )
787 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
788 return module
789}
790
791func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
792 return android.Paths{p.extractedApex}
793}
794
795func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
796 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
797 return p.properties.prebuiltSrcs(ctx)
798 }
799 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
800 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
801 ctx.Build(pctx,
802 android.BuildParams{
803 Rule: extractMatchingApex,
804 Description: "Extract an apex from an apex set",
805 Inputs: android.Paths{apexSet},
806 Output: p.extractedApex,
807 Args: map[string]string{
808 "abis": strings.Join(java.SupportedAbis(ctx), ","),
809 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
810 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
811 },
812 })
813}
814
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700815type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900816 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700817
818 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700819}
820
Paul Duffin24704672021-04-06 16:09:30 +0100821type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700822 // the .apks file path that contains prebuilt apex files to be extracted.
823 Set *string
824
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700825 Sanitized struct {
826 None struct {
827 Set *string
828 }
829 Address struct {
830 Set *string
831 }
832 Hwaddress struct {
833 Set *string
834 }
835 }
836
Paul Duffin24704672021-04-06 16:09:30 +0100837 // apexes in this set use prerelease SDK version
838 Prerelease *bool
839}
840
841func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
842 var srcs []string
843 if e.Set != nil {
844 srcs = append(srcs, *e.Set)
845 }
846
847 var sanitizers []string
848 if ctx.Host() {
849 sanitizers = ctx.Config().SanitizeHost()
850 } else {
851 sanitizers = ctx.Config().SanitizeDevice()
852 }
853
854 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
855 srcs = append(srcs, *e.Sanitized.Address.Set)
856 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
857 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
858 } else if e.Sanitized.None.Set != nil {
859 srcs = append(srcs, *e.Sanitized.None.Set)
860 }
861
862 return srcs
863}
864
865type ApexSetProperties struct {
866 ApexExtractorProperties
867
Paul Duffina9c81102021-06-15 11:34:01 +0100868 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700869}
870
871func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
872 if sanitizer == "address" {
873 return a.properties.Sanitized.Address.Set != nil
874 }
875 if sanitizer == "hwaddress" {
876 return a.properties.Sanitized.Hwaddress.Set != nil
877 }
878
879 return false
880}
881
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700882// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
883func apexSetFactory() android.Module {
884 module := &ApexSet{}
Paul Duffina9c81102021-06-15 11:34:01 +0100885 module.AddProperties(&module.properties)
886 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100887
Paul Duffin24704672021-04-06 16:09:30 +0100888 return module
889}
890
Paul Duffin5dda3e32021-05-05 14:13:27 +0100891func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100892 props := struct {
893 Name *string
894 }{
895 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700896 }
897
Paul Duffin24704672021-04-06 16:09:30 +0100898 ctx.CreateModule(privateApexExtractorModuleFactory,
899 &props,
900 apexExtractorProperties,
901 )
902}
903
904func apexExtractorModuleName(baseModuleName string) string {
905 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700906}
907
Paul Duffin5dda3e32021-05-05 14:13:27 +0100908var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
909
910// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
911// modules.
912//
913// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
914// prebuilt_apex except that instead of creating a selector module which selects one .apex file
915// from those provided this creates an extractor module which extracts the appropriate .apex file
916// from the zip file containing them.
917func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
918 baseModuleName := a.BaseModuleName()
919
920 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
921 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
922
923 apexFileSource := ":" + apexExtractorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100924 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100925
926 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100927 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100928}
929
Paul Duffin57f83592021-05-05 15:09:44 +0100930func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
931 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100932}
933
934var _ ApexInfoMutator = (*ApexSet)(nil)
935
936func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
937 a.apexInfoMutator(mctx)
938}
939
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700940func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
941 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100942 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
943 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700944 }
945
Paul Duffinbb0dc132021-05-05 16:58:08 +0100946 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700947 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100948 ctx.Build(pctx, android.BuildParams{
949 Rule: android.Cp,
950 Input: inputApex,
951 Output: a.outputApex,
952 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900953
954 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800955 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900956 return
957 }
958
Paul Duffina35f8db2021-06-15 19:10:11 +0100959 // Save the files that need to be made available to Make.
960 a.initApexFilesForAndroidMk(ctx)
961
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700962 a.installDir = android.PathForModuleInstall(ctx, "apex")
963 if a.installable() {
964 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
965 }
966
967 // in case that apex_set replaces source apex (using prefer: prop)
968 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
969 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100970 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700971 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
972 }
Jooyung Han29637162020-06-30 06:34:23 +0900973
974 if ctx.Config().InstallExtraFlattenedApexes() {
975 // flattened apex should be in /system_ext/apex
976 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
977 a.postInstallCommands = append(a.postInstallCommands,
978 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
979 a.outputApex.String(),
980 flattenedApexDir.ToMakePath().String(),
981 ))
982 a.hostRequired = []string{"deapexer", "debugfs"}
983 }
984}
985
986type systemExtContext struct {
987 android.ModuleContext
988}
989
990func (*systemExtContext) SystemExtSpecific() bool {
991 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700992}