blob: d7be9a9e17d0df140735d75747383cd176ad13c4 [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.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100179 path := child.(interface {
180 DexJarBuildPath() java.OptionalDexJarPath
181 }).DexJarBuildPath()
182 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000183 af := apexFile{
Paul Duffina35f8db2021-06-15 19:10:11 +0100184 module: child,
185 moduleDir: ctx.OtherModuleDir(child),
186 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100187 builtFile: path.Path(),
Paul Duffina35f8db2021-06-15 19:10:11 +0100188 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000189 }
190 if module, ok := child.(java.DexpreopterInterface); ok {
191 for _, install := range module.DexpreoptBuiltInstalledForApex() {
192 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
193 }
194 }
195 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffina35f8db2021-06-15 19:10:11 +0100196 }
197 } else if tag == exportedBootclasspathFragmentTag {
198 // Visit the children of the bootclasspath_fragment.
199 return true
200 }
201
202 return false
203 })
204}
205
Jiakai Zhang204356f2021-09-09 08:12:46 +0000206func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
207 for _, fi := range p.apexFilesForAndroidMk {
208 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
209 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
210 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
211 }
212}
213
Paul Duffina9c81102021-06-15 11:34:01 +0100214func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffina35f8db2021-06-15 19:10:11 +0100215 entriesList := []android.AndroidMkEntries{
Paul Duffina9c81102021-06-15 11:34:01 +0100216 {
217 Class: "ETC",
218 OutputFile: android.OptionalPathForPath(p.outputApex),
219 Include: "$(BUILD_PREBUILT)",
220 Host_required: p.hostRequired,
221 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
222 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
223 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
224 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
225 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
226 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
227 postInstallCommands := append([]string{}, p.postInstallCommands...)
228 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
229 if len(postInstallCommands) > 0 {
230 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
231 }
Jiakai Zhang204356f2021-09-09 08:12:46 +0000232 p.addRequiredModules(entries)
Paul Duffina9c81102021-06-15 11:34:01 +0100233 },
234 },
235 },
236 }
Paul Duffina35f8db2021-06-15 19:10:11 +0100237
238 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
239 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
240 // apex specific variants of the exported java modules available for use from within make.
241 apexName := p.BaseModuleName()
242 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin155c1772021-06-17 13:33:09 +0100243 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffina35f8db2021-06-15 19:10:11 +0100244 entriesList = append(entriesList, entries)
245 }
246
247 return entriesList
Paul Duffina9c81102021-06-15 11:34:01 +0100248}
249
Paul Duffin155c1772021-06-17 13:33:09 +0100250// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
251func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
252 moduleName := fi.androidMkModuleName + "." + apexName
253 entries := android.AndroidMkEntries{
254 Class: fi.class.nameInMake(),
255 OverrideName: moduleName,
256 OutputFile: android.OptionalPathForPath(fi.builtFile),
257 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
258 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
259 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
260 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
261
262 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
263 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
264 // we will have foo.jar.jar
265 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
266 var classesJar android.Path
267 var headerJar android.Path
268 if javaModule, ok := fi.module.(java.ApexDependency); ok {
269 classesJar = javaModule.ImplementationAndResourcesJars()[0]
270 headerJar = javaModule.HeaderJars()[0]
271 } else {
272 classesJar = fi.builtFile
273 headerJar = fi.builtFile
274 }
275 entries.SetString("LOCAL_SOONG_CLASSES_JAR", classesJar.String())
276 entries.SetString("LOCAL_SOONG_HEADER_JAR", headerJar.String())
277 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
278 entries.SetString("LOCAL_DEX_PREOPT", "false")
279 },
280 },
281 ExtraFooters: []android.AndroidMkExtraFootersFunc{
282 func(w io.Writer, name, prefix, moduleDir string) {
283 // m <module_name> will build <module_name>.<apex_name> as well.
284 if fi.androidMkModuleName != moduleName {
285 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
286 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
287 }
288 },
289 },
290 }
291 return entries
292}
293
Paul Duffin5dda3e32021-05-05 14:13:27 +0100294// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
295// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
296type prebuiltApexModuleCreator interface {
297 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
298}
299
300// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
301// prebuiltApexModuleCreator's createPrebuiltApexModules method.
302//
303// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
304// will need to access dependencies added by that (exported modules) but must run before the
305// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
306// exported modules.
307func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
308 module := ctx.Module()
309 if creator, ok := module.(prebuiltApexModuleCreator); ok {
310 creator.createPrebuiltApexModules(ctx)
311 }
312}
313
Paul Duffin57f83592021-05-05 15:09:44 +0100314// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
315func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
316 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100317 // Add dependencies onto the java modules that represent the java libraries that are provided by
318 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100319 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100320 dep := android.PrebuiltNameFromSource(exported)
321 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100322 }
Paul Duffin023dba02021-04-22 01:45:29 +0100323
324 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
325 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100326 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100327 dep := android.PrebuiltNameFromSource(exported)
328 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100329 }
Paul Duffindfd33262021-04-06 17:02:08 +0100330}
331
Paul Duffinb17d0442021-05-05 12:07:00 +0100332// Implements android.DepInInSameApex
333func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
334 tag := ctx.OtherModuleDependencyTag(dep)
335 _, ok := tag.(exportedDependencyTag)
336 return ok
337}
338
Paul Duffindfd33262021-04-06 17:02:08 +0100339// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
340// specific variant and checks that they are supported.
341//
342// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
343// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
344//
345// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
346// across prebuilt_apex modules. That is because there is no way to determine whether two
347// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
348// been built from different source at different times or they could have been built with different
349// build options that affect the libraries.
350//
351// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
352// modules were compatible it would be a lot of work and would not provide much benefit for a couple
353// of reasons:
354// * The number of prebuilt_apex modules that will be exporting files for the same module will be
355// low as the prebuilt_apex only exports files for the direct dependencies that require it and
356// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
357// few com.android.art* apex files that contain the same contents and could export files for the
358// same modules but only one of them needs to do so. Contrast that with source apex modules which
359// need apex specific variants for every module that contributes code to the apex, whether direct
360// or indirect.
361// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
362// extra copying of files. Contrast that with source apex modules that has to build each variant
363// from source.
364func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
365
366 // Collect direct dependencies into contents.
367 contents := make(map[string]android.ApexMembership)
368
369 // Collect the list of dependencies.
370 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100371 mctx.WalkDeps(func(child, parent android.Module) bool {
372 // If the child is not in the same apex as the parent then exit immediately and do not visit
373 // any of the child's dependencies.
374 if !android.IsDepInSameApex(mctx, parent, child) {
375 return false
376 }
377
378 tag := mctx.OtherModuleDependencyTag(child)
379 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100380 if exportedTag, ok := tag.(exportedDependencyTag); ok {
381 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100382
383 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100384 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100385 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100386 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100387 }
388
389 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100390 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100391 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100392 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100393 }
Paul Duffindfd33262021-04-06 17:02:08 +0100394 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100395
Paul Duffin46ab2912021-06-29 18:38:38 +0100396 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
397 // variant.
398 if _, ok := child.(android.ApexModule); !ok {
399 return false
400 }
401
Paul Duffinb17d0442021-05-05 12:07:00 +0100402 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
403 // behavior whether there is a corresponding source module present or not.
404 depName = android.RemoveOptionalPrebuiltPrefix(depName)
405
406 // Remember if this module was added as a direct dependency.
407 direct := parent == mctx.Module()
408 contents[depName] = contents[depName].Add(direct)
409
410 // Add the module to the list of dependencies that need to have an APEX variant.
411 dependencies = append(dependencies, child.(android.ApexModule))
412
413 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100414 })
415
416 // Create contents for the prebuilt_apex and store it away for later use.
417 apexContents := android.NewApexContents(contents)
418 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
419 Contents: apexContents,
420 })
421
422 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100423 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100424 apexInfo := android.ApexInfo{
Martin Stjernholmbe105032021-05-26 16:57:39 +0100425 ApexVariationName: apexVariationName,
426 InApexVariants: []string{apexVariationName},
Martin Stjernholmbfffae72021-06-24 14:37:13 +0100427 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100428 ApexContents: []*android.ApexContents{apexContents},
429 ForPrebuiltApex: true,
430 }
431
432 // Mark the dependencies of this module as requiring a variant for this module.
433 for _, am := range dependencies {
434 am.BuildForApex(apexInfo)
435 }
436}
437
Paul Duffin11216db2021-03-01 14:14:52 +0000438// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
439// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
440// deapexer.
441type prebuiltApexSelectorModule struct {
442 android.ModuleBase
443
444 apexFileProperties ApexFileProperties
445
446 inputApex android.Path
447}
448
449func privateApexSelectorModuleFactory() android.Module {
450 module := &prebuiltApexSelectorModule{}
451 module.AddProperties(
452 &module.apexFileProperties,
453 )
454 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
455 return module
456}
457
458func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
459 return android.Paths{p.inputApex}
460}
461
462func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
463 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
464}
465
Jiyong Park09d77522019-11-18 11:16:27 +0900466type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900467 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900468
Paul Duffinbb0dc132021-05-05 16:58:08 +0100469 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900470
Paul Duffina9c81102021-06-15 11:34:01 +0100471 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900472}
473
Paul Duffin851f3992021-01-13 17:03:51 +0000474type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900475 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000476 //
477 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
478 // for android_common. That is so that it will have the same arch variant as, and so be compatible
479 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000480 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900481 Arch struct {
482 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000483 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900484 }
485 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000486 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900487 }
488 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000489 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900490 }
491 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000492 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900493 }
494 }
Paul Duffin851f3992021-01-13 17:03:51 +0000495}
496
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000497// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
498//
499// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
500// to use methods on it that are specific to the current module.
501//
502// See the ApexFileProperties.Src property.
503func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
504 multiTargets := prebuilt.MultiTargets()
505 if len(multiTargets) != 1 {
506 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
507 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000508 }
509 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000510 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000511 case android.Arm:
512 src = String(p.Arch.Arm.Src)
513 case android.Arm64:
514 src = String(p.Arch.Arm64.Src)
515 case android.X86:
516 src = String(p.Arch.X86.Src)
517 case android.X86_64:
518 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000519 }
520 if src == "" {
521 src = String(p.Src)
522 }
Paul Duffin851f3992021-01-13 17:03:51 +0000523
Paul Duffinc0609c62021-03-01 17:27:16 +0000524 if src == "" {
525 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
526 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
527 // logic from reporting a more general, less useful message.
528 }
529
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000530 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000531}
532
533type PrebuiltProperties struct {
534 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900535
Paul Duffina9c81102021-06-15 11:34:01 +0100536 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900537}
538
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700539func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
540 return false
541}
542
Jiyong Park09d77522019-11-18 11:16:27 +0900543func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
544 switch tag {
545 case "":
546 return android.Paths{p.outputApex}, nil
547 default:
548 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
549 }
550}
551
Jiyong Park09d77522019-11-18 11:16:27 +0900552// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
553func PrebuiltFactory() android.Module {
554 module := &Prebuilt{}
Paul Duffina9c81102021-06-15 11:34:01 +0100555 module.AddProperties(&module.properties)
556 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000557
Jiyong Park09d77522019-11-18 11:16:27 +0900558 return module
559}
560
Paul Duffin5dda3e32021-05-05 14:13:27 +0100561func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000562 props := struct {
563 Name *string
564 }{
565 Name: proptools.StringPtr(name),
566 }
567
568 ctx.CreateModule(privateApexSelectorModuleFactory,
569 &props,
570 apexFileProperties,
571 )
572}
573
Paul Duffin5dda3e32021-05-05 14:13:27 +0100574// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
575//
Paul Duffin57f83592021-05-05 15:09:44 +0100576// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
577// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
578// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffina9c81102021-06-15 11:34:01 +0100579func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *PrebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100580 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100581 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100582 return
583 }
584
Paul Duffin57f83592021-05-05 15:09:44 +0100585 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin5466a362021-06-07 10:25:31 +0100586 commonModules := []string{}
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100587 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100588 ctx.WalkDeps(func(child, parent android.Module) bool {
589 tag := ctx.OtherModuleDependencyTag(child)
590
Paul Duffinfef55002021-06-17 14:56:05 +0100591 // If the child is not in the same apex as the parent then ignore it and all its children.
592 if !android.IsDepInSameApex(ctx, parent, child) {
593 return false
594 }
595
Paul Duffin57f83592021-05-05 15:09:44 +0100596 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffinfef55002021-06-17 14:56:05 +0100597 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffin5466a362021-06-07 10:25:31 +0100598 commonModules = append(commonModules, name)
599
600 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100601 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffin5466a362021-06-07 10:25:31 +0100602
Paul Duffinfef55002021-06-17 14:56:05 +0100603 // Visit the dependencies of this module just in case they also require files from the
604 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100605 return true
606 }
607
608 return false
609 })
610
Paul Duffin3bae0682021-05-05 18:03:47 +0100611 // Create properties for deapexer module.
612 deapexerProperties := &DeapexerProperties{
Paul Duffin5466a362021-06-07 10:25:31 +0100613 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100614 // dependency as well as transitive ones.
Paul Duffin5466a362021-06-07 10:25:31 +0100615 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100616 }
617
618 // Populate the exported files property in a fixed order.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100619 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100620
Paul Duffin11216db2021-03-01 14:14:52 +0000621 props := struct {
622 Name *string
623 Selected_apex *string
624 }{
625 Name: proptools.StringPtr(deapexerName),
626 Selected_apex: proptools.StringPtr(apexFileSource),
627 }
628 ctx.CreateModule(privateDeapexerFactory,
629 &props,
630 deapexerProperties,
631 )
632}
633
634func deapexerModuleName(baseModuleName string) string {
635 return baseModuleName + ".deapexer"
636}
637
638func apexSelectorModuleName(baseModuleName string) string {
639 return baseModuleName + ".apex.selector"
640}
641
Paul Duffin064b70c2020-11-02 17:32:38 +0000642func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
643 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
644 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
645 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
646 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
647 // and not a renamed prebuilt module then that will be detected and reported as an error when
648 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100649 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000650 if ctx.OtherModuleExists(prebuiltName) {
651 name = prebuiltName
652 }
653 return name
654}
655
Paul Duffina7139422021-02-08 11:01:58 +0000656type exportedDependencyTag struct {
657 blueprint.BaseDependencyTag
658 name string
659}
660
661// Mark this tag so dependencies that use it are excluded from visibility enforcement.
662//
663// This does allow any prebuilt_apex to reference any module which does open up a small window for
664// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
665// avoids opening up a much bigger window by widening the visibility of modules that need files
666// provided by the prebuilt_apex to include all the possible locations they may be defined, which
667// could include everything below vendor/.
668//
669// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
670// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
671// the .apex file. It will also have to be included in the module's apex_available property too.
672// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
673// incorrectly.
674func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
675
Paul Duffinfef55002021-06-17 14:56:05 +0100676func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
677
678var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
679
Paul Duffina7139422021-02-08 11:01:58 +0000680var (
Paul Duffin023dba02021-04-22 01:45:29 +0100681 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
682 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000683)
684
Paul Duffin5dda3e32021-05-05 14:13:27 +0100685var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
686
687// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
688// build.
689//
690// If this needs to make files from within a `.apex` file available for use by other Soong modules,
691// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
692// it does so as follows:
693//
694// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
695// makes them available for use by other modules, at both Soong and ninja levels.
696//
697// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
698// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
699// dexpreopt, will work the same way from source and prebuilt.
700//
701// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
702// itself so that they can retrieve the file paths to those files.
703//
704// It also creates a child module `selector` that is responsible for selecting the appropriate
705// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
706// 1. To dedup the selection logic so it only runs in one module.
707// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
708// `apex_set`.
709//
710// prebuilt_apex
711// / | \
712// / | \
713// V V V
714// selector <--- deapexer <--- exported java lib
715//
716func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
717 baseModuleName := p.BaseModuleName()
718
719 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
720 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
721
722 apexFileSource := ":" + apexSelectorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100723 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100724
725 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100726 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100727}
728
Paul Duffin57f83592021-05-05 15:09:44 +0100729func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
730 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000731}
732
733var _ ApexInfoMutator = (*Prebuilt)(nil)
734
Paul Duffin064b70c2020-11-02 17:32:38 +0000735func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100736 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900737}
738
739func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900740 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100741 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900742 p.installDir = android.PathForModuleInstall(ctx, "apex")
743 p.installFilename = p.InstallFilename()
744 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
745 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
746 }
747 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
748 ctx.Build(pctx, android.BuildParams{
749 Rule: android.Cp,
750 Input: p.inputApex,
751 Output: p.outputApex,
752 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900753
754 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800755 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900756 return
757 }
758
Paul Duffina35f8db2021-06-15 19:10:11 +0100759 // Save the files that need to be made available to Make.
760 p.initApexFilesForAndroidMk(ctx)
761
Jiyong Park09d77522019-11-18 11:16:27 +0900762 if p.installable() {
763 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
764 }
765
Jooyung Han002ab682020-01-08 01:57:58 +0900766 // in case that prebuilt_apex replaces source apex (using prefer: prop)
767 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
768 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100769 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900770 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
771 }
Jiyong Park09d77522019-11-18 11:16:27 +0900772}
773
Paul Duffin24704672021-04-06 16:09:30 +0100774// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
775// module. It extracts the correct apex to use and makes it available for use by apex_set.
776type prebuiltApexExtractorModule struct {
777 android.ModuleBase
778
779 properties ApexExtractorProperties
780
781 extractedApex android.WritablePath
782}
783
784func privateApexExtractorModuleFactory() android.Module {
785 module := &prebuiltApexExtractorModule{}
786 module.AddProperties(
787 &module.properties,
788 )
789 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
790 return module
791}
792
793func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
794 return android.Paths{p.extractedApex}
795}
796
797func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
798 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
799 return p.properties.prebuiltSrcs(ctx)
800 }
801 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
802 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
803 ctx.Build(pctx,
804 android.BuildParams{
805 Rule: extractMatchingApex,
806 Description: "Extract an apex from an apex set",
807 Inputs: android.Paths{apexSet},
808 Output: p.extractedApex,
809 Args: map[string]string{
810 "abis": strings.Join(java.SupportedAbis(ctx), ","),
811 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
812 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
813 },
814 })
815}
816
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700817type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900818 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700819
820 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700821}
822
Paul Duffin24704672021-04-06 16:09:30 +0100823type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700824 // the .apks file path that contains prebuilt apex files to be extracted.
825 Set *string
826
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700827 Sanitized struct {
828 None struct {
829 Set *string
830 }
831 Address struct {
832 Set *string
833 }
834 Hwaddress struct {
835 Set *string
836 }
837 }
838
Paul Duffin24704672021-04-06 16:09:30 +0100839 // apexes in this set use prerelease SDK version
840 Prerelease *bool
841}
842
843func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
844 var srcs []string
845 if e.Set != nil {
846 srcs = append(srcs, *e.Set)
847 }
848
849 var sanitizers []string
850 if ctx.Host() {
851 sanitizers = ctx.Config().SanitizeHost()
852 } else {
853 sanitizers = ctx.Config().SanitizeDevice()
854 }
855
856 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
857 srcs = append(srcs, *e.Sanitized.Address.Set)
858 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
859 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
860 } else if e.Sanitized.None.Set != nil {
861 srcs = append(srcs, *e.Sanitized.None.Set)
862 }
863
864 return srcs
865}
866
867type ApexSetProperties struct {
868 ApexExtractorProperties
869
Paul Duffina9c81102021-06-15 11:34:01 +0100870 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700871}
872
873func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
874 if sanitizer == "address" {
875 return a.properties.Sanitized.Address.Set != nil
876 }
877 if sanitizer == "hwaddress" {
878 return a.properties.Sanitized.Hwaddress.Set != nil
879 }
880
881 return false
882}
883
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700884// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
885func apexSetFactory() android.Module {
886 module := &ApexSet{}
Paul Duffina9c81102021-06-15 11:34:01 +0100887 module.AddProperties(&module.properties)
888 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100889
Paul Duffin24704672021-04-06 16:09:30 +0100890 return module
891}
892
Paul Duffin5dda3e32021-05-05 14:13:27 +0100893func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100894 props := struct {
895 Name *string
896 }{
897 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700898 }
899
Paul Duffin24704672021-04-06 16:09:30 +0100900 ctx.CreateModule(privateApexExtractorModuleFactory,
901 &props,
902 apexExtractorProperties,
903 )
904}
905
906func apexExtractorModuleName(baseModuleName string) string {
907 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700908}
909
Paul Duffin5dda3e32021-05-05 14:13:27 +0100910var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
911
912// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
913// modules.
914//
915// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
916// prebuilt_apex except that instead of creating a selector module which selects one .apex file
917// from those provided this creates an extractor module which extracts the appropriate .apex file
918// from the zip file containing them.
919func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
920 baseModuleName := a.BaseModuleName()
921
922 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
923 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
924
925 apexFileSource := ":" + apexExtractorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100926 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100927
928 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100929 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100930}
931
Paul Duffin57f83592021-05-05 15:09:44 +0100932func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
933 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100934}
935
936var _ ApexInfoMutator = (*ApexSet)(nil)
937
938func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
939 a.apexInfoMutator(mctx)
940}
941
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700942func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
943 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100944 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
945 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700946 }
947
Paul Duffinbb0dc132021-05-05 16:58:08 +0100948 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700949 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100950 ctx.Build(pctx, android.BuildParams{
951 Rule: android.Cp,
952 Input: inputApex,
953 Output: a.outputApex,
954 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900955
956 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800957 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900958 return
959 }
960
Paul Duffina35f8db2021-06-15 19:10:11 +0100961 // Save the files that need to be made available to Make.
962 a.initApexFilesForAndroidMk(ctx)
963
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700964 a.installDir = android.PathForModuleInstall(ctx, "apex")
965 if a.installable() {
966 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
967 }
968
969 // in case that apex_set replaces source apex (using prefer: prop)
970 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
971 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100972 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700973 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
974 }
Jooyung Han29637162020-06-30 06:34:23 +0900975
976 if ctx.Config().InstallExtraFlattenedApexes() {
977 // flattened apex should be in /system_ext/apex
978 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
979 a.postInstallCommands = append(a.postInstallCommands,
980 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
981 a.outputApex.String(),
982 flattenedApexDir.ToMakePath().String(),
983 ))
984 a.hostRequired = []string{"deapexer", "debugfs"}
985 }
986}
987
988type systemExtContext struct {
989 android.ModuleContext
990}
991
992func (*systemExtContext) SystemExtSpecific() bool {
993 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700994}