blob: 55a9e421ae829c962051b9972f41658556bdc71c [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
Jiakai Zhang774dd302021-09-26 03:54:25 +0000106
107 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
108 // APEX bundle will create an APEX variant.
109 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900110}
111
Paul Duffina9c81102021-06-15 11:34:01 +0100112// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
113// module that is common to Prebuilt and ApexSet.
114func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
115 p.prebuiltCommonProperties = properties
116 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
117 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
118}
119
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100120func (p *prebuiltCommon) ApexVariationName() string {
121 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
122}
123
Jiyong Park10e926b2020-07-16 21:38:56 +0900124func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
125 return &p.prebuilt
126}
127
128func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100129 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900130}
131
132func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
133 // If the device is configured to use flattened APEX, force disable the prebuilt because
134 // the prebuilt is a non-flattened one.
135 forceDisable := ctx.Config().FlattenApex()
136
137 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
138 // to build the prebuilts themselves.
139 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
140
141 // Force disable the prebuilts when coverage is enabled.
142 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
143 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
144
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700145 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
146 sanitized := ctx.Module().(sanitizedPrebuilt)
147 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
148 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900149
150 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100151 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900152 return true
153 }
154 return false
155}
156
Paul Duffina9c81102021-06-15 11:34:01 +0100157func (p *prebuiltCommon) InstallFilename() string {
158 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
159}
160
161func (p *prebuiltCommon) Name() string {
162 return p.prebuilt.Name(p.ModuleBase.Name())
163}
164
165func (p *prebuiltCommon) Overrides() []string {
166 return p.prebuiltCommonProperties.Overrides
167}
168
169func (p *prebuiltCommon) installable() bool {
170 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
171}
172
Paul Duffina35f8db2021-06-15 19:10:11 +0100173// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
174// modules that this depends upon.
175func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
176 // Walk the dependencies of this module looking for the java modules that it exports.
177 ctx.WalkDeps(func(child, parent android.Module) bool {
178 tag := ctx.OtherModuleDependencyTag(child)
179
180 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000181 if java.IsBootclasspathFragmentContentDepTag(tag) ||
182 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffina35f8db2021-06-15 19:10:11 +0100183 // If the exported java module provides a dex jar path then add it to the list of apexFiles.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100184 path := child.(interface {
185 DexJarBuildPath() java.OptionalDexJarPath
186 }).DexJarBuildPath()
187 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000188 af := apexFile{
Paul Duffina35f8db2021-06-15 19:10:11 +0100189 module: child,
190 moduleDir: ctx.OtherModuleDir(child),
191 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100192 builtFile: path.Path(),
Paul Duffina35f8db2021-06-15 19:10:11 +0100193 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000194 }
195 if module, ok := child.(java.DexpreopterInterface); ok {
196 for _, install := range module.DexpreoptBuiltInstalledForApex() {
197 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
198 }
199 }
200 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffina35f8db2021-06-15 19:10:11 +0100201 }
Jiakai Zhang774dd302021-09-26 03:54:25 +0000202 } else if tag == exportedBootclasspathFragmentTag ||
203 tag == exportedSystemserverclasspathFragmentTag {
204 // Visit the children of the bootclasspath_fragment and systemserver_fragment.
Paul Duffina35f8db2021-06-15 19:10:11 +0100205 return true
206 }
207
208 return false
209 })
210}
211
Jiakai Zhang204356f2021-09-09 08:12:46 +0000212func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
213 for _, fi := range p.apexFilesForAndroidMk {
214 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
215 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
216 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
217 }
218}
219
Paul Duffina9c81102021-06-15 11:34:01 +0100220func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffina35f8db2021-06-15 19:10:11 +0100221 entriesList := []android.AndroidMkEntries{
Paul Duffina9c81102021-06-15 11:34:01 +0100222 {
223 Class: "ETC",
224 OutputFile: android.OptionalPathForPath(p.outputApex),
225 Include: "$(BUILD_PREBUILT)",
226 Host_required: p.hostRequired,
227 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
228 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
229 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
230 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
231 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
232 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
233 postInstallCommands := append([]string{}, p.postInstallCommands...)
234 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
235 if len(postInstallCommands) > 0 {
236 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
237 }
Jiakai Zhang204356f2021-09-09 08:12:46 +0000238 p.addRequiredModules(entries)
Paul Duffina9c81102021-06-15 11:34:01 +0100239 },
240 },
241 },
242 }
Paul Duffina35f8db2021-06-15 19:10:11 +0100243
244 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
245 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
246 // apex specific variants of the exported java modules available for use from within make.
247 apexName := p.BaseModuleName()
248 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin155c1772021-06-17 13:33:09 +0100249 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffina35f8db2021-06-15 19:10:11 +0100250 entriesList = append(entriesList, entries)
251 }
252
253 return entriesList
Paul Duffina9c81102021-06-15 11:34:01 +0100254}
255
Paul Duffin155c1772021-06-17 13:33:09 +0100256// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
257func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
258 moduleName := fi.androidMkModuleName + "." + apexName
259 entries := android.AndroidMkEntries{
260 Class: fi.class.nameInMake(),
261 OverrideName: moduleName,
262 OutputFile: android.OptionalPathForPath(fi.builtFile),
263 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
264 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
265 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
266 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
267
268 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
269 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
270 // we will have foo.jar.jar
271 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
272 var classesJar android.Path
273 var headerJar android.Path
274 if javaModule, ok := fi.module.(java.ApexDependency); ok {
275 classesJar = javaModule.ImplementationAndResourcesJars()[0]
276 headerJar = javaModule.HeaderJars()[0]
277 } else {
278 classesJar = fi.builtFile
279 headerJar = fi.builtFile
280 }
281 entries.SetString("LOCAL_SOONG_CLASSES_JAR", classesJar.String())
282 entries.SetString("LOCAL_SOONG_HEADER_JAR", headerJar.String())
283 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
284 entries.SetString("LOCAL_DEX_PREOPT", "false")
285 },
286 },
287 ExtraFooters: []android.AndroidMkExtraFootersFunc{
288 func(w io.Writer, name, prefix, moduleDir string) {
289 // m <module_name> will build <module_name>.<apex_name> as well.
290 if fi.androidMkModuleName != moduleName {
291 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
292 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
293 }
294 },
295 },
296 }
297 return entries
298}
299
Paul Duffin5dda3e32021-05-05 14:13:27 +0100300// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
301// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
302type prebuiltApexModuleCreator interface {
303 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
304}
305
306// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
307// prebuiltApexModuleCreator's createPrebuiltApexModules method.
308//
309// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
310// will need to access dependencies added by that (exported modules) but must run before the
311// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
312// exported modules.
313func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
314 module := ctx.Module()
315 if creator, ok := module.(prebuiltApexModuleCreator); ok {
316 creator.createPrebuiltApexModules(ctx)
317 }
318}
319
Jiakai Zhang774dd302021-09-26 03:54:25 +0000320func (p *prebuiltCommon) getExportedDependencies() map[string]exportedDependencyTag {
321 dependencies := make(map[string]exportedDependencyTag)
322
323 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
324 dependencies[dep] = exportedJavaLibTag
325 }
326
327 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
328 dependencies[dep] = exportedBootclasspathFragmentTag
329 }
330
331 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
332 dependencies[dep] = exportedSystemserverclasspathFragmentTag
333 }
334
335 return dependencies
336}
337
Paul Duffin57f83592021-05-05 15:09:44 +0100338// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
339func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
340 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100341
Jiakai Zhang774dd302021-09-26 03:54:25 +0000342 for dep, tag := range p.getExportedDependencies() {
343 prebuiltDep := android.PrebuiltNameFromSource(dep)
344 ctx.AddDependency(module, tag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100345 }
Paul Duffindfd33262021-04-06 17:02:08 +0100346}
347
Paul Duffinb17d0442021-05-05 12:07:00 +0100348// Implements android.DepInInSameApex
349func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
350 tag := ctx.OtherModuleDependencyTag(dep)
351 _, ok := tag.(exportedDependencyTag)
352 return ok
353}
354
Paul Duffindfd33262021-04-06 17:02:08 +0100355// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
356// specific variant and checks that they are supported.
357//
358// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
359// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
360//
361// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
362// across prebuilt_apex modules. That is because there is no way to determine whether two
363// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
364// been built from different source at different times or they could have been built with different
365// build options that affect the libraries.
366//
367// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
368// modules were compatible it would be a lot of work and would not provide much benefit for a couple
369// of reasons:
370// * The number of prebuilt_apex modules that will be exporting files for the same module will be
371// low as the prebuilt_apex only exports files for the direct dependencies that require it and
372// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
373// few com.android.art* apex files that contain the same contents and could export files for the
374// same modules but only one of them needs to do so. Contrast that with source apex modules which
375// need apex specific variants for every module that contributes code to the apex, whether direct
376// or indirect.
377// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
378// extra copying of files. Contrast that with source apex modules that has to build each variant
379// from source.
380func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
381
382 // Collect direct dependencies into contents.
383 contents := make(map[string]android.ApexMembership)
384
385 // Collect the list of dependencies.
386 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100387 mctx.WalkDeps(func(child, parent android.Module) bool {
388 // If the child is not in the same apex as the parent then exit immediately and do not visit
389 // any of the child's dependencies.
390 if !android.IsDepInSameApex(mctx, parent, child) {
391 return false
392 }
393
394 tag := mctx.OtherModuleDependencyTag(child)
395 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100396 if exportedTag, ok := tag.(exportedDependencyTag); ok {
397 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100398
399 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100400 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100401 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100402 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100403 }
404
405 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100406 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100407 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100408 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100409 }
Paul Duffindfd33262021-04-06 17:02:08 +0100410 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100411
Paul Duffin46ab2912021-06-29 18:38:38 +0100412 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
413 // variant.
414 if _, ok := child.(android.ApexModule); !ok {
415 return false
416 }
417
Paul Duffinb17d0442021-05-05 12:07:00 +0100418 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
419 // behavior whether there is a corresponding source module present or not.
420 depName = android.RemoveOptionalPrebuiltPrefix(depName)
421
422 // Remember if this module was added as a direct dependency.
423 direct := parent == mctx.Module()
424 contents[depName] = contents[depName].Add(direct)
425
426 // Add the module to the list of dependencies that need to have an APEX variant.
427 dependencies = append(dependencies, child.(android.ApexModule))
428
429 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100430 })
431
432 // Create contents for the prebuilt_apex and store it away for later use.
433 apexContents := android.NewApexContents(contents)
434 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
435 Contents: apexContents,
436 })
437
438 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100439 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100440 apexInfo := android.ApexInfo{
Martin Stjernholmbe105032021-05-26 16:57:39 +0100441 ApexVariationName: apexVariationName,
442 InApexVariants: []string{apexVariationName},
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100443 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100444 ApexContents: []*android.ApexContents{apexContents},
445 ForPrebuiltApex: true,
446 }
447
448 // Mark the dependencies of this module as requiring a variant for this module.
449 for _, am := range dependencies {
450 am.BuildForApex(apexInfo)
451 }
452}
453
Paul Duffin11216db2021-03-01 14:14:52 +0000454// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
455// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
456// deapexer.
457type prebuiltApexSelectorModule struct {
458 android.ModuleBase
459
460 apexFileProperties ApexFileProperties
461
462 inputApex android.Path
463}
464
465func privateApexSelectorModuleFactory() android.Module {
466 module := &prebuiltApexSelectorModule{}
467 module.AddProperties(
468 &module.apexFileProperties,
469 )
470 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
471 return module
472}
473
474func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
475 return android.Paths{p.inputApex}
476}
477
478func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
479 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
480}
481
Jiyong Park09d77522019-11-18 11:16:27 +0900482type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900483 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900484
Paul Duffinbb0dc132021-05-05 16:58:08 +0100485 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900486
Paul Duffina9c81102021-06-15 11:34:01 +0100487 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900488}
489
Paul Duffin851f3992021-01-13 17:03:51 +0000490type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900491 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000492 //
493 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
494 // for android_common. That is so that it will have the same arch variant as, and so be compatible
495 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000496 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900497 Arch struct {
498 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000499 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900500 }
501 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000502 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900503 }
504 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000505 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900506 }
507 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000508 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900509 }
510 }
Paul Duffin851f3992021-01-13 17:03:51 +0000511}
512
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000513// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
514//
515// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
516// to use methods on it that are specific to the current module.
517//
518// See the ApexFileProperties.Src property.
519func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
520 multiTargets := prebuilt.MultiTargets()
521 if len(multiTargets) != 1 {
522 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
523 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000524 }
525 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000526 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000527 case android.Arm:
528 src = String(p.Arch.Arm.Src)
529 case android.Arm64:
530 src = String(p.Arch.Arm64.Src)
531 case android.X86:
532 src = String(p.Arch.X86.Src)
533 case android.X86_64:
534 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000535 }
536 if src == "" {
537 src = String(p.Src)
538 }
Paul Duffin851f3992021-01-13 17:03:51 +0000539
Paul Duffinc0609c62021-03-01 17:27:16 +0000540 if src == "" {
541 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
542 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
543 // logic from reporting a more general, less useful message.
544 }
545
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000546 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000547}
548
549type PrebuiltProperties struct {
550 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900551
Paul Duffina9c81102021-06-15 11:34:01 +0100552 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900553}
554
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700555func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
556 return false
557}
558
Jiyong Park09d77522019-11-18 11:16:27 +0900559func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
560 switch tag {
561 case "":
562 return android.Paths{p.outputApex}, nil
563 default:
564 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
565 }
566}
567
Jiyong Park09d77522019-11-18 11:16:27 +0900568// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
569func PrebuiltFactory() android.Module {
570 module := &Prebuilt{}
Paul Duffina9c81102021-06-15 11:34:01 +0100571 module.AddProperties(&module.properties)
572 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000573
Jiyong Park09d77522019-11-18 11:16:27 +0900574 return module
575}
576
Paul Duffin5dda3e32021-05-05 14:13:27 +0100577func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000578 props := struct {
579 Name *string
580 }{
581 Name: proptools.StringPtr(name),
582 }
583
584 ctx.CreateModule(privateApexSelectorModuleFactory,
585 &props,
586 apexFileProperties,
587 )
588}
589
Paul Duffin5dda3e32021-05-05 14:13:27 +0100590// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
591//
Paul Duffin57f83592021-05-05 15:09:44 +0100592// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
593// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
594// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000595func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100596 // Only create the deapexer module if it is needed.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000597 if len(p.getExportedDependencies()) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100598 return
599 }
600
Paul Duffin57f83592021-05-05 15:09:44 +0100601 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin5466a362021-06-07 10:25:31 +0100602 commonModules := []string{}
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100603 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100604 ctx.WalkDeps(func(child, parent android.Module) bool {
605 tag := ctx.OtherModuleDependencyTag(child)
606
Paul Duffinfef55002021-06-17 14:56:05 +0100607 // If the child is not in the same apex as the parent then ignore it and all its children.
608 if !android.IsDepInSameApex(ctx, parent, child) {
609 return false
610 }
611
Paul Duffin57f83592021-05-05 15:09:44 +0100612 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffinfef55002021-06-17 14:56:05 +0100613 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffin5466a362021-06-07 10:25:31 +0100614 commonModules = append(commonModules, name)
615
616 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100617 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffin5466a362021-06-07 10:25:31 +0100618
Paul Duffinfef55002021-06-17 14:56:05 +0100619 // Visit the dependencies of this module just in case they also require files from the
620 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100621 return true
622 }
623
624 return false
625 })
626
Paul Duffin3bae0682021-05-05 18:03:47 +0100627 // Create properties for deapexer module.
628 deapexerProperties := &DeapexerProperties{
Paul Duffin5466a362021-06-07 10:25:31 +0100629 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100630 // dependency as well as transitive ones.
Paul Duffin5466a362021-06-07 10:25:31 +0100631 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100632 }
633
634 // Populate the exported files property in a fixed order.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100635 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100636
Paul Duffin11216db2021-03-01 14:14:52 +0000637 props := struct {
638 Name *string
639 Selected_apex *string
640 }{
641 Name: proptools.StringPtr(deapexerName),
642 Selected_apex: proptools.StringPtr(apexFileSource),
643 }
644 ctx.CreateModule(privateDeapexerFactory,
645 &props,
646 deapexerProperties,
647 )
648}
649
Paul Duffin11216db2021-03-01 14:14:52 +0000650func apexSelectorModuleName(baseModuleName string) string {
651 return baseModuleName + ".apex.selector"
652}
653
Paul Duffin064b70c2020-11-02 17:32:38 +0000654func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
655 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
656 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
657 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
658 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
659 // and not a renamed prebuilt module then that will be detected and reported as an error when
660 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100661 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000662 if ctx.OtherModuleExists(prebuiltName) {
663 name = prebuiltName
664 }
665 return name
666}
667
Paul Duffina7139422021-02-08 11:01:58 +0000668type exportedDependencyTag struct {
669 blueprint.BaseDependencyTag
670 name string
671}
672
673// Mark this tag so dependencies that use it are excluded from visibility enforcement.
674//
675// This does allow any prebuilt_apex to reference any module which does open up a small window for
676// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
677// avoids opening up a much bigger window by widening the visibility of modules that need files
678// provided by the prebuilt_apex to include all the possible locations they may be defined, which
679// could include everything below vendor/.
680//
681// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
682// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
683// the .apex file. It will also have to be included in the module's apex_available property too.
684// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
685// incorrectly.
686func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
687
Paul Duffinfef55002021-06-17 14:56:05 +0100688func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
689
690var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
691
Paul Duffina7139422021-02-08 11:01:58 +0000692var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000693 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
694 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
695 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000696)
697
Paul Duffin5dda3e32021-05-05 14:13:27 +0100698var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
699
700// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
701// build.
702//
703// If this needs to make files from within a `.apex` file available for use by other Soong modules,
704// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
705// it does so as follows:
706//
707// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
708// makes them available for use by other modules, at both Soong and ninja levels.
709//
710// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
711// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
712// dexpreopt, will work the same way from source and prebuilt.
713//
714// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
715// itself so that they can retrieve the file paths to those files.
716//
717// It also creates a child module `selector` that is responsible for selecting the appropriate
718// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
719// 1. To dedup the selection logic so it only runs in one module.
720// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
721// `apex_set`.
722//
723// prebuilt_apex
724// / | \
725// / | \
726// V V V
727// selector <--- deapexer <--- exported java lib
728//
729func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
730 baseModuleName := p.BaseModuleName()
731
732 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
733 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
734
735 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000736 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100737
738 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100739 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100740}
741
Paul Duffin57f83592021-05-05 15:09:44 +0100742func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
743 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000744}
745
746var _ ApexInfoMutator = (*Prebuilt)(nil)
747
Paul Duffin064b70c2020-11-02 17:32:38 +0000748func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100749 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900750}
751
752func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900753 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100754 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900755 p.installDir = android.PathForModuleInstall(ctx, "apex")
756 p.installFilename = p.InstallFilename()
757 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
758 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
759 }
760 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
761 ctx.Build(pctx, android.BuildParams{
762 Rule: android.Cp,
763 Input: p.inputApex,
764 Output: p.outputApex,
765 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900766
767 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800768 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900769 return
770 }
771
Paul Duffina35f8db2021-06-15 19:10:11 +0100772 // Save the files that need to be made available to Make.
773 p.initApexFilesForAndroidMk(ctx)
774
Jiyong Park09d77522019-11-18 11:16:27 +0900775 if p.installable() {
776 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
777 }
778
Jooyung Han002ab682020-01-08 01:57:58 +0900779 // in case that prebuilt_apex replaces source apex (using prefer: prop)
780 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
781 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100782 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900783 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
784 }
Jiyong Park09d77522019-11-18 11:16:27 +0900785}
786
Paul Duffin24704672021-04-06 16:09:30 +0100787// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
788// module. It extracts the correct apex to use and makes it available for use by apex_set.
789type prebuiltApexExtractorModule struct {
790 android.ModuleBase
791
792 properties ApexExtractorProperties
793
794 extractedApex android.WritablePath
795}
796
797func privateApexExtractorModuleFactory() android.Module {
798 module := &prebuiltApexExtractorModule{}
799 module.AddProperties(
800 &module.properties,
801 )
802 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
803 return module
804}
805
806func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
807 return android.Paths{p.extractedApex}
808}
809
810func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
811 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
812 return p.properties.prebuiltSrcs(ctx)
813 }
814 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
815 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
816 ctx.Build(pctx,
817 android.BuildParams{
818 Rule: extractMatchingApex,
819 Description: "Extract an apex from an apex set",
820 Inputs: android.Paths{apexSet},
821 Output: p.extractedApex,
822 Args: map[string]string{
823 "abis": strings.Join(java.SupportedAbis(ctx), ","),
824 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
825 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
826 },
827 })
828}
829
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700830type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900831 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700832
833 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700834}
835
Paul Duffin24704672021-04-06 16:09:30 +0100836type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700837 // the .apks file path that contains prebuilt apex files to be extracted.
838 Set *string
839
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700840 Sanitized struct {
841 None struct {
842 Set *string
843 }
844 Address struct {
845 Set *string
846 }
847 Hwaddress struct {
848 Set *string
849 }
850 }
851
Paul Duffin24704672021-04-06 16:09:30 +0100852 // apexes in this set use prerelease SDK version
853 Prerelease *bool
854}
855
856func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
857 var srcs []string
858 if e.Set != nil {
859 srcs = append(srcs, *e.Set)
860 }
861
862 var sanitizers []string
863 if ctx.Host() {
864 sanitizers = ctx.Config().SanitizeHost()
865 } else {
866 sanitizers = ctx.Config().SanitizeDevice()
867 }
868
869 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
870 srcs = append(srcs, *e.Sanitized.Address.Set)
871 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
872 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
873 } else if e.Sanitized.None.Set != nil {
874 srcs = append(srcs, *e.Sanitized.None.Set)
875 }
876
877 return srcs
878}
879
880type ApexSetProperties struct {
881 ApexExtractorProperties
882
Paul Duffina9c81102021-06-15 11:34:01 +0100883 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700884}
885
886func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
887 if sanitizer == "address" {
888 return a.properties.Sanitized.Address.Set != nil
889 }
890 if sanitizer == "hwaddress" {
891 return a.properties.Sanitized.Hwaddress.Set != nil
892 }
893
894 return false
895}
896
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700897// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
898func apexSetFactory() android.Module {
899 module := &ApexSet{}
Paul Duffina9c81102021-06-15 11:34:01 +0100900 module.AddProperties(&module.properties)
901 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100902
Paul Duffin24704672021-04-06 16:09:30 +0100903 return module
904}
905
Paul Duffin5dda3e32021-05-05 14:13:27 +0100906func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100907 props := struct {
908 Name *string
909 }{
910 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700911 }
912
Paul Duffin24704672021-04-06 16:09:30 +0100913 ctx.CreateModule(privateApexExtractorModuleFactory,
914 &props,
915 apexExtractorProperties,
916 )
917}
918
919func apexExtractorModuleName(baseModuleName string) string {
920 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700921}
922
Paul Duffin5dda3e32021-05-05 14:13:27 +0100923var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
924
925// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
926// modules.
927//
928// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
929// prebuilt_apex except that instead of creating a selector module which selects one .apex file
930// from those provided this creates an extractor module which extracts the appropriate .apex file
931// from the zip file containing them.
932func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
933 baseModuleName := a.BaseModuleName()
934
935 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
936 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
937
938 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000939 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100940
941 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100942 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100943}
944
Paul Duffin57f83592021-05-05 15:09:44 +0100945func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
946 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100947}
948
949var _ ApexInfoMutator = (*ApexSet)(nil)
950
951func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
952 a.apexInfoMutator(mctx)
953}
954
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700955func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
956 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100957 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
958 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700959 }
960
Paul Duffinbb0dc132021-05-05 16:58:08 +0100961 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700962 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100963 ctx.Build(pctx, android.BuildParams{
964 Rule: android.Cp,
965 Input: inputApex,
966 Output: a.outputApex,
967 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900968
969 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800970 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900971 return
972 }
973
Paul Duffina35f8db2021-06-15 19:10:11 +0100974 // Save the files that need to be made available to Make.
975 a.initApexFilesForAndroidMk(ctx)
976
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700977 a.installDir = android.PathForModuleInstall(ctx, "apex")
978 if a.installable() {
979 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
980 }
981
982 // in case that apex_set replaces source apex (using prefer: prop)
983 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
984 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100985 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700986 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
987 }
Jooyung Han29637162020-06-30 06:34:23 +0900988
989 if ctx.Config().InstallExtraFlattenedApexes() {
990 // flattened apex should be in /system_ext/apex
991 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
992 a.postInstallCommands = append(a.postInstallCommands,
993 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
994 a.outputApex.String(),
995 flattenedApexDir.ToMakePath().String(),
996 ))
997 a.hostRequired = []string{"deapexer", "debugfs"}
998 }
999}
1000
1001type systemExtContext struct {
1002 android.ModuleContext
1003}
1004
1005func (*systemExtContext) SystemExtSpecific() bool {
1006 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001007}