blob: ba7482c07611c29c4a77a77472dcd65beada9531 [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"
Paul Duffin3bae0682021-05-05 18:03:47 +010020 "path/filepath"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070021 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090022 "strings"
23
24 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070025 "android/soong/java"
Jiyong Park10e926b2020-07-16 21:38:56 +090026
Jaewoong Jungfa00c062020-05-14 14:15:24 -070027 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090028
29 "github.com/google/blueprint/proptools"
30)
31
Jaewoong Jungfa00c062020-05-14 14:15:24 -070032var (
33 extractMatchingApex = pctx.StaticRule(
34 "extractMatchingApex",
35 blueprint.RuleParams{
36 Command: `rm -rf "$out" && ` +
37 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
38 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
39 `${in}`,
40 CommandDeps: []string{"${extract_apks}"},
41 },
42 "abis", "allow-prereleased", "sdk-version")
43)
44
Jiyong Park10e926b2020-07-16 21:38:56 +090045type prebuilt interface {
46 isForceDisabled() bool
47 InstallFilename() string
48}
49
50type prebuiltCommon struct {
Paul Duffina9c81102021-06-15 11:34:01 +010051 android.ModuleBase
Paul Duffinbb0dc132021-05-05 16:58:08 +010052 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010053
Paul Duffinbb0dc132021-05-05 16:58:08 +010054 // Properties common to both prebuilt_apex and apex_set.
Paul Duffina9c81102021-06-15 11:34:01 +010055 prebuiltCommonProperties *PrebuiltCommonProperties
56
57 installDir android.InstallPath
58 installFilename string
59 outputApex android.WritablePath
60
Paul Duffina35f8db2021-06-15 19:10:11 +010061 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
62 // to create make modules in prebuiltCommon.AndroidMkEntries.
63 apexFilesForAndroidMk []apexFile
64
Paul Duffina9c81102021-06-15 11:34:01 +010065 // list of commands to create symlinks for backward compatibility.
66 // these commands will be attached as LOCAL_POST_INSTALL_CMD
67 compatSymlinks []string
68
69 hostRequired []string
70 postInstallCommands []string
Jiyong Park10e926b2020-07-16 21:38:56 +090071}
72
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070073type sanitizedPrebuilt interface {
74 hasSanitizedSource(sanitizer string) bool
75}
76
Paul Duffina9c81102021-06-15 11:34:01 +010077type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010078 SelectedApexProperties
79
Jiyong Park10e926b2020-07-16 21:38:56 +090080 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010081
Paul Duffina9c81102021-06-15 11:34:01 +010082 // whether the extracted apex file is installable.
83 Installable *bool
84
85 // optional name for the installed apex. If unspecified, name of the
86 // module is used as the file name
87 Filename *string
88
89 // names of modules to be overridden. Listed modules can only be other binaries
90 // (in Make or Soong).
91 // This does not completely prevent installation of the overridden binaries, but if both
92 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
93 // from PRODUCT_PACKAGES.
94 Overrides []string
95
Paul Duffin3bae0682021-05-05 18:03:47 +010096 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
97 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
98 // dexpreopt and boot jars package check.
99 Exported_java_libs []string
100
101 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
102 // bundle will create an APEX variant.
103 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900104}
105
Paul Duffina9c81102021-06-15 11:34:01 +0100106// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
107// module that is common to Prebuilt and ApexSet.
108func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
109 p.prebuiltCommonProperties = properties
110 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
111 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
112}
113
Jiyong Park10e926b2020-07-16 21:38:56 +0900114func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
115 return &p.prebuilt
116}
117
118func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100119 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900120}
121
122func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
123 // If the device is configured to use flattened APEX, force disable the prebuilt because
124 // the prebuilt is a non-flattened one.
125 forceDisable := ctx.Config().FlattenApex()
126
127 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
128 // to build the prebuilts themselves.
129 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
130
131 // Force disable the prebuilts when coverage is enabled.
132 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
133 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
134
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700135 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
136 sanitized := ctx.Module().(sanitizedPrebuilt)
137 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
138 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900139
140 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100141 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900142 return true
143 }
144 return false
145}
146
Paul Duffina9c81102021-06-15 11:34:01 +0100147func (p *prebuiltCommon) InstallFilename() string {
148 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
149}
150
151func (p *prebuiltCommon) Name() string {
152 return p.prebuilt.Name(p.ModuleBase.Name())
153}
154
155func (p *prebuiltCommon) Overrides() []string {
156 return p.prebuiltCommonProperties.Overrides
157}
158
159func (p *prebuiltCommon) installable() bool {
160 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
161}
162
Paul Duffina35f8db2021-06-15 19:10:11 +0100163// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
164// modules that this depends upon.
165func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
166 // Walk the dependencies of this module looking for the java modules that it exports.
167 ctx.WalkDeps(func(child, parent android.Module) bool {
168 tag := ctx.OtherModuleDependencyTag(child)
169
170 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
171 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
172 // If the exported java module provides a dex jar path then add it to the list of apexFiles.
173 path := child.(interface{ DexJarBuildPath() android.Path }).DexJarBuildPath()
174 if path != nil {
175 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, apexFile{
176 module: child,
177 moduleDir: ctx.OtherModuleDir(child),
178 androidMkModuleName: name,
179 builtFile: path,
180 class: javaSharedLib,
181 })
182 }
183 } else if tag == exportedBootclasspathFragmentTag {
184 // Visit the children of the bootclasspath_fragment.
185 return true
186 }
187
188 return false
189 })
190}
191
Paul Duffina9c81102021-06-15 11:34:01 +0100192func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffina35f8db2021-06-15 19:10:11 +0100193 entriesList := []android.AndroidMkEntries{
Paul Duffina9c81102021-06-15 11:34:01 +0100194 {
195 Class: "ETC",
196 OutputFile: android.OptionalPathForPath(p.outputApex),
197 Include: "$(BUILD_PREBUILT)",
198 Host_required: p.hostRequired,
199 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
200 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
201 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
202 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
203 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
204 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
205 postInstallCommands := append([]string{}, p.postInstallCommands...)
206 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
207 if len(postInstallCommands) > 0 {
208 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
209 }
210 },
211 },
212 },
213 }
Paul Duffina35f8db2021-06-15 19:10:11 +0100214
215 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
216 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
217 // apex specific variants of the exported java modules available for use from within make.
218 apexName := p.BaseModuleName()
219 for _, fi := range p.apexFilesForAndroidMk {
220 moduleName := fi.androidMkModuleName + "." + apexName
221 entries := android.AndroidMkEntries{
222 Class: fi.class.nameInMake(),
223 OverrideName: moduleName,
224 OutputFile: android.OptionalPathForPath(fi.builtFile),
225 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
226 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
227 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
228 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
229
230 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
231 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
232 // we will have foo.jar.jar
233 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin85fa3442021-06-17 20:31:40 +0100234 var classesJar android.Path
235 var headerJar android.Path
236 if javaModule, ok := fi.module.(java.ApexDependency); ok {
237 classesJar = javaModule.ImplementationAndResourcesJars()[0]
238 headerJar = javaModule.HeaderJars()[0]
239 } else {
240 classesJar = fi.builtFile
241 headerJar = fi.builtFile
242 }
243 entries.SetString("LOCAL_SOONG_CLASSES_JAR", classesJar.String())
244 entries.SetString("LOCAL_SOONG_HEADER_JAR", headerJar.String())
Paul Duffina35f8db2021-06-15 19:10:11 +0100245 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
246 entries.SetString("LOCAL_DEX_PREOPT", "false")
247 },
248 },
249 ExtraFooters: []android.AndroidMkExtraFootersFunc{
250 func(w io.Writer, name, prefix, moduleDir string) {
251 // m <module_name> will build <module_name>.<apex_name> as well.
252 if fi.androidMkModuleName != moduleName {
253 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
254 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
255 }
256 },
257 },
258 }
259
260 entriesList = append(entriesList, entries)
261 }
262
263 return entriesList
Paul Duffina9c81102021-06-15 11:34:01 +0100264}
265
Paul Duffin5dda3e32021-05-05 14:13:27 +0100266// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
267// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
268type prebuiltApexModuleCreator interface {
269 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
270}
271
272// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
273// prebuiltApexModuleCreator's createPrebuiltApexModules method.
274//
275// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
276// will need to access dependencies added by that (exported modules) but must run before the
277// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
278// exported modules.
279func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
280 module := ctx.Module()
281 if creator, ok := module.(prebuiltApexModuleCreator); ok {
282 creator.createPrebuiltApexModules(ctx)
283 }
284}
285
Paul Duffin57f83592021-05-05 15:09:44 +0100286// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
287func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
288 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100289 // Add dependencies onto the java modules that represent the java libraries that are provided by
290 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100291 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100292 dep := android.PrebuiltNameFromSource(exported)
293 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100294 }
Paul Duffin023dba02021-04-22 01:45:29 +0100295
296 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
297 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100298 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100299 dep := android.PrebuiltNameFromSource(exported)
300 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100301 }
Paul Duffindfd33262021-04-06 17:02:08 +0100302}
303
Paul Duffinb17d0442021-05-05 12:07:00 +0100304// Implements android.DepInInSameApex
305func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
306 tag := ctx.OtherModuleDependencyTag(dep)
307 _, ok := tag.(exportedDependencyTag)
308 return ok
309}
310
Paul Duffindfd33262021-04-06 17:02:08 +0100311// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
312// specific variant and checks that they are supported.
313//
314// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
315// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
316//
317// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
318// across prebuilt_apex modules. That is because there is no way to determine whether two
319// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
320// been built from different source at different times or they could have been built with different
321// build options that affect the libraries.
322//
323// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
324// modules were compatible it would be a lot of work and would not provide much benefit for a couple
325// of reasons:
326// * The number of prebuilt_apex modules that will be exporting files for the same module will be
327// low as the prebuilt_apex only exports files for the direct dependencies that require it and
328// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
329// few com.android.art* apex files that contain the same contents and could export files for the
330// same modules but only one of them needs to do so. Contrast that with source apex modules which
331// need apex specific variants for every module that contributes code to the apex, whether direct
332// or indirect.
333// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
334// extra copying of files. Contrast that with source apex modules that has to build each variant
335// from source.
336func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
337
338 // Collect direct dependencies into contents.
339 contents := make(map[string]android.ApexMembership)
340
341 // Collect the list of dependencies.
342 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100343 mctx.WalkDeps(func(child, parent android.Module) bool {
344 // If the child is not in the same apex as the parent then exit immediately and do not visit
345 // any of the child's dependencies.
346 if !android.IsDepInSameApex(mctx, parent, child) {
347 return false
348 }
349
350 tag := mctx.OtherModuleDependencyTag(child)
351 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100352 if exportedTag, ok := tag.(exportedDependencyTag); ok {
353 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100354
355 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100356 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100357 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100358 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100359 }
360
361 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100362 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100363 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100364 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100365 }
Paul Duffindfd33262021-04-06 17:02:08 +0100366 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100367
368 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
369 // behavior whether there is a corresponding source module present or not.
370 depName = android.RemoveOptionalPrebuiltPrefix(depName)
371
372 // Remember if this module was added as a direct dependency.
373 direct := parent == mctx.Module()
374 contents[depName] = contents[depName].Add(direct)
375
376 // Add the module to the list of dependencies that need to have an APEX variant.
377 dependencies = append(dependencies, child.(android.ApexModule))
378
379 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100380 })
381
382 // Create contents for the prebuilt_apex and store it away for later use.
383 apexContents := android.NewApexContents(contents)
384 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
385 Contents: apexContents,
386 })
387
388 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmbe105032021-05-26 16:57:39 +0100389 apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName())
Paul Duffindfd33262021-04-06 17:02:08 +0100390 apexInfo := android.ApexInfo{
Martin Stjernholmbe105032021-05-26 16:57:39 +0100391 ApexVariationName: apexVariationName,
392 InApexVariants: []string{apexVariationName},
393 InApexModules: []string{apexVariationName},
Paul Duffindfd33262021-04-06 17:02:08 +0100394 ApexContents: []*android.ApexContents{apexContents},
395 ForPrebuiltApex: true,
396 }
397
398 // Mark the dependencies of this module as requiring a variant for this module.
399 for _, am := range dependencies {
400 am.BuildForApex(apexInfo)
401 }
402}
403
Paul Duffin11216db2021-03-01 14:14:52 +0000404// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
405// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
406// deapexer.
407type prebuiltApexSelectorModule struct {
408 android.ModuleBase
409
410 apexFileProperties ApexFileProperties
411
412 inputApex android.Path
413}
414
415func privateApexSelectorModuleFactory() android.Module {
416 module := &prebuiltApexSelectorModule{}
417 module.AddProperties(
418 &module.apexFileProperties,
419 )
420 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
421 return module
422}
423
424func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
425 return android.Paths{p.inputApex}
426}
427
428func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
429 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
430}
431
Jiyong Park09d77522019-11-18 11:16:27 +0900432type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900433 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900434
Paul Duffinbb0dc132021-05-05 16:58:08 +0100435 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900436
Paul Duffina9c81102021-06-15 11:34:01 +0100437 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900438}
439
Paul Duffin851f3992021-01-13 17:03:51 +0000440type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900441 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000442 //
443 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
444 // for android_common. That is so that it will have the same arch variant as, and so be compatible
445 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000446 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900447 Arch struct {
448 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000449 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900450 }
451 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000452 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900453 }
454 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000455 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900456 }
457 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000458 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900459 }
460 }
Paul Duffin851f3992021-01-13 17:03:51 +0000461}
462
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000463// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
464//
465// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
466// to use methods on it that are specific to the current module.
467//
468// See the ApexFileProperties.Src property.
469func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
470 multiTargets := prebuilt.MultiTargets()
471 if len(multiTargets) != 1 {
472 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
473 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000474 }
475 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000476 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000477 case android.Arm:
478 src = String(p.Arch.Arm.Src)
479 case android.Arm64:
480 src = String(p.Arch.Arm64.Src)
481 case android.X86:
482 src = String(p.Arch.X86.Src)
483 case android.X86_64:
484 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000485 }
486 if src == "" {
487 src = String(p.Src)
488 }
Paul Duffin851f3992021-01-13 17:03:51 +0000489
Paul Duffinc0609c62021-03-01 17:27:16 +0000490 if src == "" {
491 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
492 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
493 // logic from reporting a more general, less useful message.
494 }
495
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000496 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000497}
498
499type PrebuiltProperties struct {
500 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900501
Paul Duffina9c81102021-06-15 11:34:01 +0100502 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900503}
504
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700505func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
506 return false
507}
508
Jiyong Park09d77522019-11-18 11:16:27 +0900509func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
510 switch tag {
511 case "":
512 return android.Paths{p.outputApex}, nil
513 default:
514 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
515 }
516}
517
Jiyong Park09d77522019-11-18 11:16:27 +0900518// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
519func PrebuiltFactory() android.Module {
520 module := &Prebuilt{}
Paul Duffina9c81102021-06-15 11:34:01 +0100521 module.AddProperties(&module.properties)
522 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000523
Jiyong Park09d77522019-11-18 11:16:27 +0900524 return module
525}
526
Paul Duffin5dda3e32021-05-05 14:13:27 +0100527func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000528 props := struct {
529 Name *string
530 }{
531 Name: proptools.StringPtr(name),
532 }
533
534 ctx.CreateModule(privateApexSelectorModuleFactory,
535 &props,
536 apexFileProperties,
537 )
538}
539
Paul Duffin5dda3e32021-05-05 14:13:27 +0100540// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
541//
Paul Duffin57f83592021-05-05 15:09:44 +0100542// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
543// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
544// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffina9c81102021-06-15 11:34:01 +0100545func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *PrebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100546 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100547 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100548 return
549 }
550
Paul Duffin57f83592021-05-05 15:09:44 +0100551 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin5466a362021-06-07 10:25:31 +0100552 commonModules := []string{}
553 exportedFilesByKey := map[string]string{}
554 requiringModulesByKey := map[string]android.Module{}
Paul Duffin57f83592021-05-05 15:09:44 +0100555 ctx.WalkDeps(func(child, parent android.Module) bool {
556 tag := ctx.OtherModuleDependencyTag(child)
557
558 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
559 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin5466a362021-06-07 10:25:31 +0100560 commonModules = append(commonModules, name)
Paul Duffin3bae0682021-05-05 18:03:47 +0100561
562 // Add the dex implementation jar to the set of exported files. The path here must match the
563 // path of the file in the APEX created by apexFileForJavaModule(...).
Paul Duffin5466a362021-06-07 10:25:31 +0100564 exportedFilesByKey[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
Paul Duffin3bae0682021-05-05 18:03:47 +0100565
Paul Duffin57f83592021-05-05 15:09:44 +0100566 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin5466a362021-06-07 10:25:31 +0100567 commonModules = append(commonModules, name)
568
569 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
570 for k, v := range requiredFiles {
571 if f, ok := exportedFilesByKey[k]; ok && f != v {
572 otherModule := requiringModulesByKey[k]
573 ctx.ModuleErrorf("inconsistent paths have been requested for key %q, %s requires path %s while %s requires path %s",
574 k, child, v, otherModule, f)
575 continue
576 }
577 exportedFilesByKey[k] = v
578 requiringModulesByKey[k] = child
579 }
580
581 // Make sure to visit the children of the bootclasspath_fragment.
Paul Duffin57f83592021-05-05 15:09:44 +0100582 return true
583 }
584
585 return false
586 })
587
Paul Duffin3bae0682021-05-05 18:03:47 +0100588 // Create properties for deapexer module.
589 deapexerProperties := &DeapexerProperties{
Paul Duffin5466a362021-06-07 10:25:31 +0100590 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100591 // dependency as well as transitive ones.
Paul Duffin5466a362021-06-07 10:25:31 +0100592 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100593 }
594
595 // Populate the exported files property in a fixed order.
Paul Duffin5466a362021-06-07 10:25:31 +0100596 for _, tag := range android.SortedStringKeys(exportedFilesByKey) {
Paul Duffin3bae0682021-05-05 18:03:47 +0100597 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
598 Tag: tag,
Paul Duffin5466a362021-06-07 10:25:31 +0100599 Path: exportedFilesByKey[tag],
Paul Duffin3bae0682021-05-05 18:03:47 +0100600 })
601 }
Paul Duffin57f83592021-05-05 15:09:44 +0100602
Paul Duffin11216db2021-03-01 14:14:52 +0000603 props := struct {
604 Name *string
605 Selected_apex *string
606 }{
607 Name: proptools.StringPtr(deapexerName),
608 Selected_apex: proptools.StringPtr(apexFileSource),
609 }
610 ctx.CreateModule(privateDeapexerFactory,
611 &props,
612 deapexerProperties,
613 )
614}
615
616func deapexerModuleName(baseModuleName string) string {
617 return baseModuleName + ".deapexer"
618}
619
620func apexSelectorModuleName(baseModuleName string) string {
621 return baseModuleName + ".apex.selector"
622}
623
Paul Duffin064b70c2020-11-02 17:32:38 +0000624func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
625 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
626 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
627 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
628 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
629 // and not a renamed prebuilt module then that will be detected and reported as an error when
630 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100631 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000632 if ctx.OtherModuleExists(prebuiltName) {
633 name = prebuiltName
634 }
635 return name
636}
637
Paul Duffina7139422021-02-08 11:01:58 +0000638type exportedDependencyTag struct {
639 blueprint.BaseDependencyTag
640 name string
641}
642
643// Mark this tag so dependencies that use it are excluded from visibility enforcement.
644//
645// This does allow any prebuilt_apex to reference any module which does open up a small window for
646// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
647// avoids opening up a much bigger window by widening the visibility of modules that need files
648// provided by the prebuilt_apex to include all the possible locations they may be defined, which
649// could include everything below vendor/.
650//
651// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
652// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
653// the .apex file. It will also have to be included in the module's apex_available property too.
654// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
655// incorrectly.
656func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
657
658var (
Paul Duffin023dba02021-04-22 01:45:29 +0100659 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
660 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000661)
662
Paul Duffin5dda3e32021-05-05 14:13:27 +0100663var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
664
665// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
666// build.
667//
668// If this needs to make files from within a `.apex` file available for use by other Soong modules,
669// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
670// it does so as follows:
671//
672// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
673// makes them available for use by other modules, at both Soong and ninja levels.
674//
675// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
676// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
677// dexpreopt, will work the same way from source and prebuilt.
678//
679// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
680// itself so that they can retrieve the file paths to those files.
681//
682// It also creates a child module `selector` that is responsible for selecting the appropriate
683// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
684// 1. To dedup the selection logic so it only runs in one module.
685// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
686// `apex_set`.
687//
688// prebuilt_apex
689// / | \
690// / | \
691// V V V
692// selector <--- deapexer <--- exported java lib
693//
694func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
695 baseModuleName := p.BaseModuleName()
696
697 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
698 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
699
700 apexFileSource := ":" + apexSelectorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100701 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100702
703 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100704 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100705}
706
Paul Duffin57f83592021-05-05 15:09:44 +0100707func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
708 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000709}
710
711var _ ApexInfoMutator = (*Prebuilt)(nil)
712
Paul Duffin064b70c2020-11-02 17:32:38 +0000713func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100714 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900715}
716
717func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900718 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100719 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900720 p.installDir = android.PathForModuleInstall(ctx, "apex")
721 p.installFilename = p.InstallFilename()
722 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
723 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
724 }
725 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
726 ctx.Build(pctx, android.BuildParams{
727 Rule: android.Cp,
728 Input: p.inputApex,
729 Output: p.outputApex,
730 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900731
732 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800733 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900734 return
735 }
736
Paul Duffina35f8db2021-06-15 19:10:11 +0100737 // Save the files that need to be made available to Make.
738 p.initApexFilesForAndroidMk(ctx)
739
Jiyong Park09d77522019-11-18 11:16:27 +0900740 if p.installable() {
741 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
742 }
743
Jooyung Han002ab682020-01-08 01:57:58 +0900744 // in case that prebuilt_apex replaces source apex (using prefer: prop)
745 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
746 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100747 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900748 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
749 }
Jiyong Park09d77522019-11-18 11:16:27 +0900750}
751
Paul Duffin24704672021-04-06 16:09:30 +0100752// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
753// module. It extracts the correct apex to use and makes it available for use by apex_set.
754type prebuiltApexExtractorModule struct {
755 android.ModuleBase
756
757 properties ApexExtractorProperties
758
759 extractedApex android.WritablePath
760}
761
762func privateApexExtractorModuleFactory() android.Module {
763 module := &prebuiltApexExtractorModule{}
764 module.AddProperties(
765 &module.properties,
766 )
767 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
768 return module
769}
770
771func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
772 return android.Paths{p.extractedApex}
773}
774
775func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
776 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
777 return p.properties.prebuiltSrcs(ctx)
778 }
779 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
780 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
781 ctx.Build(pctx,
782 android.BuildParams{
783 Rule: extractMatchingApex,
784 Description: "Extract an apex from an apex set",
785 Inputs: android.Paths{apexSet},
786 Output: p.extractedApex,
787 Args: map[string]string{
788 "abis": strings.Join(java.SupportedAbis(ctx), ","),
789 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
790 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
791 },
792 })
793}
794
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700795type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900796 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700797
798 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700799}
800
Paul Duffin24704672021-04-06 16:09:30 +0100801type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700802 // the .apks file path that contains prebuilt apex files to be extracted.
803 Set *string
804
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700805 Sanitized struct {
806 None struct {
807 Set *string
808 }
809 Address struct {
810 Set *string
811 }
812 Hwaddress struct {
813 Set *string
814 }
815 }
816
Paul Duffin24704672021-04-06 16:09:30 +0100817 // apexes in this set use prerelease SDK version
818 Prerelease *bool
819}
820
821func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
822 var srcs []string
823 if e.Set != nil {
824 srcs = append(srcs, *e.Set)
825 }
826
827 var sanitizers []string
828 if ctx.Host() {
829 sanitizers = ctx.Config().SanitizeHost()
830 } else {
831 sanitizers = ctx.Config().SanitizeDevice()
832 }
833
834 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
835 srcs = append(srcs, *e.Sanitized.Address.Set)
836 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
837 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
838 } else if e.Sanitized.None.Set != nil {
839 srcs = append(srcs, *e.Sanitized.None.Set)
840 }
841
842 return srcs
843}
844
845type ApexSetProperties struct {
846 ApexExtractorProperties
847
Paul Duffina9c81102021-06-15 11:34:01 +0100848 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700849}
850
851func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
852 if sanitizer == "address" {
853 return a.properties.Sanitized.Address.Set != nil
854 }
855 if sanitizer == "hwaddress" {
856 return a.properties.Sanitized.Hwaddress.Set != nil
857 }
858
859 return false
860}
861
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700862// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
863func apexSetFactory() android.Module {
864 module := &ApexSet{}
Paul Duffina9c81102021-06-15 11:34:01 +0100865 module.AddProperties(&module.properties)
866 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100867
Paul Duffin24704672021-04-06 16:09:30 +0100868 return module
869}
870
Paul Duffin5dda3e32021-05-05 14:13:27 +0100871func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100872 props := struct {
873 Name *string
874 }{
875 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700876 }
877
Paul Duffin24704672021-04-06 16:09:30 +0100878 ctx.CreateModule(privateApexExtractorModuleFactory,
879 &props,
880 apexExtractorProperties,
881 )
882}
883
884func apexExtractorModuleName(baseModuleName string) string {
885 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700886}
887
Paul Duffin5dda3e32021-05-05 14:13:27 +0100888var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
889
890// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
891// modules.
892//
893// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
894// prebuilt_apex except that instead of creating a selector module which selects one .apex file
895// from those provided this creates an extractor module which extracts the appropriate .apex file
896// from the zip file containing them.
897func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
898 baseModuleName := a.BaseModuleName()
899
900 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
901 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
902
903 apexFileSource := ":" + apexExtractorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100904 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100905
906 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100907 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100908}
909
Paul Duffin57f83592021-05-05 15:09:44 +0100910func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
911 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100912}
913
914var _ ApexInfoMutator = (*ApexSet)(nil)
915
916func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
917 a.apexInfoMutator(mctx)
918}
919
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700920func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
921 a.installFilename = a.InstallFilename()
922 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
923 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
924 }
925
Paul Duffinbb0dc132021-05-05 16:58:08 +0100926 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700927 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100928 ctx.Build(pctx, android.BuildParams{
929 Rule: android.Cp,
930 Input: inputApex,
931 Output: a.outputApex,
932 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900933
934 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800935 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900936 return
937 }
938
Paul Duffina35f8db2021-06-15 19:10:11 +0100939 // Save the files that need to be made available to Make.
940 a.initApexFilesForAndroidMk(ctx)
941
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700942 a.installDir = android.PathForModuleInstall(ctx, "apex")
943 if a.installable() {
944 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
945 }
946
947 // in case that apex_set replaces source apex (using prefer: prop)
948 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
949 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100950 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700951 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
952 }
Jooyung Han29637162020-06-30 06:34:23 +0900953
954 if ctx.Config().InstallExtraFlattenedApexes() {
955 // flattened apex should be in /system_ext/apex
956 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
957 a.postInstallCommands = append(a.postInstallCommands,
958 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
959 a.outputApex.String(),
960 flattenedApexDir.ToMakePath().String(),
961 ))
962 a.hostRequired = []string{"deapexer", "debugfs"}
963 }
964}
965
966type systemExtContext struct {
967 android.ModuleContext
968}
969
970func (*systemExtContext) SystemExtSpecific() bool {
971 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700972}