blob: 792b571889d802e6d1c08c27046877ca1ba27c95 [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 (
Jaewoong Jungfa00c062020-05-14 14:15:24 -070018 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090019 "strings"
20
21 "android/soong/android"
Spandan Das2069c3f2023-12-06 19:40:24 +000022 "android/soong/dexpreopt"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070023 "android/soong/java"
Wei Li340ee8e2022-03-18 17:33:24 -070024 "android/soong/provenance"
Anton Hansson805e0a52022-11-25 14:06:46 +000025
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} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -070036 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
37 `-screen-densities=all -extract-single ` +
Jaewoong Jungfa00c062020-05-14 14:15:24 -070038 `${in}`,
39 CommandDeps: []string{"${extract_apks}"},
40 },
Pranav Gupta51645ff2023-03-20 16:19:53 -070041 "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
Jooyung Han26ec8482024-07-31 15:04:05 +090042 decompressApex = pctx.StaticRule("decompressApex", blueprint.RuleParams{
Jooyung Han8fa61162024-08-08 10:42:47 +090043 Command: `rm -rf $out && ${deapexer} decompress --copy-if-uncompressed --input ${in} --output ${out}`,
Jooyung Han26ec8482024-07-31 15:04:05 +090044 CommandDeps: []string{"${deapexer}"},
Jooyung Han8fa61162024-08-08 10:42:47 +090045 Description: "decompress $out",
Jooyung Han26ec8482024-07-31 15:04:05 +090046 })
Jaewoong Jungfa00c062020-05-14 14:15:24 -070047)
48
Jiyong Park10e926b2020-07-16 21:38:56 +090049type prebuilt interface {
50 isForceDisabled() bool
51 InstallFilename() string
52}
53
54type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010055 android.ModuleBase
Spandan Das2069c3f2023-12-06 19:40:24 +000056 java.Dexpreopter
Paul Duffinbb0dc132021-05-05 16:58:08 +010057 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010058
Paul Duffinbb0dc132021-05-05 16:58:08 +010059 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010060 prebuiltCommonProperties *PrebuiltCommonProperties
61
62 installDir android.InstallPath
63 installFilename string
Colin Cross6340ea52021-11-04 12:01:18 -070064 installedFile android.InstallPath
Paul Duffinef6b6952021-06-15 11:34:01 +010065 outputApex android.WritablePath
66
Jooyung Han286957d2023-10-30 16:17:56 +090067 // fragment for this apex for apexkeys.txt
68 apexKeysPath android.WritablePath
69
Colin Cross6340ea52021-11-04 12:01:18 -070070 // Installed locations of symlinks for backward compatibility.
71 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010072
Jiakai Zhange6e90db2022-01-28 14:58:56 +000073 hostRequired []string
74 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090075}
76
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070077type sanitizedPrebuilt interface {
78 hasSanitizedSource(sanitizer string) bool
79}
80
Paul Duffinef6b6952021-06-15 11:34:01 +010081type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010082 SelectedApexProperties
83
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010084 // Canonical name of this APEX. Used to determine the path to the activated APEX on
85 // device (/apex/<apex_name>). If unspecified, follows the name property.
86 Apex_name *string
87
Spandan Das3576e762024-01-03 18:57:03 +000088 // Name of the source APEX that gets shadowed by this prebuilt
89 // e.g. com.mycompany.android.myapex
90 // If unspecified, follows the naming convention that the source apex of
91 // the prebuilt is Name() without "prebuilt_" prefix
92 Source_apex_name *string
93
Jiyong Park10e926b2020-07-16 21:38:56 +090094 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010095
Paul Duffinef6b6952021-06-15 11:34:01 +010096 // whether the extracted apex file is installable.
97 Installable *bool
98
99 // optional name for the installed apex. If unspecified, name of the
100 // module is used as the file name
101 Filename *string
102
103 // names of modules to be overridden. Listed modules can only be other binaries
104 // (in Make or Soong).
105 // This does not completely prevent installation of the overridden binaries, but if both
106 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
107 // from PRODUCT_PACKAGES.
108 Overrides []string
109
Paul Duffin3bae0682021-05-05 18:03:47 +0100110 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
111 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
112 // dexpreopt and boot jars package check.
113 Exported_java_libs []string
114
115 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
116 // bundle will create an APEX variant.
117 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000118
119 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
120 // APEX bundle will create an APEX variant.
121 Exported_systemserverclasspath_fragments []string
Spandan Dasa747d2e2024-03-11 21:37:25 +0000122
123 // Path to the .prebuilt_info file of the prebuilt apex.
124 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used to
125 // generate the prebuilt.
126 Prebuilt_info *string `android:"path"`
Jiyong Park10e926b2020-07-16 21:38:56 +0900127}
128
Paul Duffinef6b6952021-06-15 11:34:01 +0100129// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
130// module that is common to Prebuilt and ApexSet.
131func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
132 p.prebuiltCommonProperties = properties
133 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
134 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
135}
136
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100137func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000138 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
139}
140
141func (p *prebuiltCommon) BaseModuleName() string {
142 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100143}
144
Jiyong Park10e926b2020-07-16 21:38:56 +0900145func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
146 return &p.prebuilt
147}
148
149func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100150 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900151}
152
153func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900154 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900155
156 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
157 // to build the prebuilts themselves.
158 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
159
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700160 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
161 sanitized := ctx.Module().(sanitizedPrebuilt)
162 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
163 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900164
165 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100166 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900167 return true
168 }
169 return false
170}
171
Paul Duffinef6b6952021-06-15 11:34:01 +0100172func (p *prebuiltCommon) InstallFilename() string {
173 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
174}
175
176func (p *prebuiltCommon) Name() string {
177 return p.prebuilt.Name(p.ModuleBase.Name())
178}
179
180func (p *prebuiltCommon) Overrides() []string {
181 return p.prebuiltCommonProperties.Overrides
182}
183
184func (p *prebuiltCommon) installable() bool {
185 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
186}
187
Spandan Das2069c3f2023-12-06 19:40:24 +0000188// To satisfy java.DexpreopterInterface
189func (p *prebuiltCommon) IsInstallable() bool {
190 return p.installable()
191}
192
193// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100194func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000195 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
196 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
197 p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
198 }
199}
Paul Duffinc30aea22021-06-15 19:10:11 +0100200
Spandan Das2069c3f2023-12-06 19:40:24 +0000201// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
202func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext) {
203 // If this apex does not export anything, return
204 if !p.hasExportedDeps() {
205 return
206 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000207 // If this prebuilt apex has not been selected, return
208 if p.IsHideFromMake() {
209 return
210 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000211 // Use apex_name to determine the api domain of this prebuilt apex
212 apexName := p.ApexVariationName()
Spandan Dasfae468e2023-12-12 23:23:53 +0000213 di, err := android.FindDeapexerProviderForModule(ctx)
214 if err != nil {
215 ctx.ModuleErrorf(err.Error())
216 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000217 dc := dexpreopt.GetGlobalConfig(ctx)
218 systemServerJarList := dc.AllApexSystemServerJars(ctx)
219
220 for i := 0; i < systemServerJarList.Len(); i++ {
221 sscpApex := systemServerJarList.Apex(i)
222 sscpJar := systemServerJarList.Jar(i)
223 if apexName != sscpApex {
224 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100225 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000226 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
227 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100228}
229
Jiakai Zhang204356f2021-09-09 08:12:46 +0000230func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000231 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000232}
233
Paul Duffinef6b6952021-06-15 11:34:01 +0100234func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100235 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100236 {
237 Class: "ETC",
238 OutputFile: android.OptionalPathForPath(p.outputApex),
239 Include: "$(BUILD_PREBUILT)",
240 Host_required: p.hostRequired,
241 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
242 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800243 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100244 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700245 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
246 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700247 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100248 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
249 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900250 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000251 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100252 },
253 },
254 },
255 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100256
Spandan Das2069c3f2023-12-06 19:40:24 +0000257 // Add the dexpreopt artifacts to androidmk
258 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
259 entriesList = append(entriesList, install.ToMakeEntries())
260 }
261
Paul Duffinc30aea22021-06-15 19:10:11 +0100262 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100263}
264
Paul Duffin5dda3e32021-05-05 14:13:27 +0100265// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
266// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
267type prebuiltApexModuleCreator interface {
Colin Crossda279cf2024-09-17 14:25:45 -0700268 createPrebuiltApexModules(ctx android.BottomUpMutatorContext)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100269}
270
271// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
272// prebuiltApexModuleCreator's createPrebuiltApexModules method.
273//
274// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
275// will need to access dependencies added by that (exported modules) but must run before the
276// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
277// exported modules.
Colin Crossda279cf2024-09-17 14:25:45 -0700278func prebuiltApexModuleCreatorMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100279 module := ctx.Module()
280 if creator, ok := module.(prebuiltApexModuleCreator); ok {
281 creator.createPrebuiltApexModules(ctx)
282 }
283}
284
Liz Kammer2dc72442023-04-20 10:10:48 -0400285func (p *prebuiltCommon) hasExportedDeps() bool {
286 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
287 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
288 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000289}
290
Paul Duffin57f83592021-05-05 15:09:44 +0100291// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
292func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
293 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100294
Liz Kammer2dc72442023-04-20 10:10:48 -0400295 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000296 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400297 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
298 }
299
300 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
301 prebuiltDep := android.PrebuiltNameFromSource(dep)
302 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
303 }
304
305 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
306 prebuiltDep := android.PrebuiltNameFromSource(dep)
307 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100308 }
Paul Duffindfd33262021-04-06 17:02:08 +0100309}
310
Paul Duffinb17d0442021-05-05 12:07:00 +0100311// Implements android.DepInInSameApex
312func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
313 tag := ctx.OtherModuleDependencyTag(dep)
314 _, ok := tag.(exportedDependencyTag)
315 return ok
316}
317
Paul Duffindfd33262021-04-06 17:02:08 +0100318// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
319// specific variant and checks that they are supported.
320//
321// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
322// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
323//
324// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
325// across prebuilt_apex modules. That is because there is no way to determine whether two
326// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
327// been built from different source at different times or they could have been built with different
328// build options that affect the libraries.
329//
330// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
331// modules were compatible it would be a lot of work and would not provide much benefit for a couple
332// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700333// - The number of prebuilt_apex modules that will be exporting files for the same module will be
334// low as the prebuilt_apex only exports files for the direct dependencies that require it and
335// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
336// few com.android.art* apex files that contain the same contents and could export files for the
337// same modules but only one of them needs to do so. Contrast that with source apex modules which
338// need apex specific variants for every module that contributes code to the apex, whether direct
339// or indirect.
340// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
341// extra copying of files. Contrast that with source apex modules that has to build each variant
342// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100343func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
344
345 // Collect direct dependencies into contents.
346 contents := make(map[string]android.ApexMembership)
347
348 // Collect the list of dependencies.
349 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100350 mctx.WalkDeps(func(child, parent android.Module) bool {
351 // If the child is not in the same apex as the parent then exit immediately and do not visit
352 // any of the child's dependencies.
353 if !android.IsDepInSameApex(mctx, parent, child) {
354 return false
355 }
356
357 tag := mctx.OtherModuleDependencyTag(child)
358 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100359 if exportedTag, ok := tag.(exportedDependencyTag); ok {
360 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100361
362 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100363 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100364 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100365 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100366 }
367
368 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100369 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100370 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100371 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100372 }
Paul Duffindfd33262021-04-06 17:02:08 +0100373 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100374
Paul Duffinfee8cf32021-06-29 18:38:38 +0100375 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
376 // variant.
377 if _, ok := child.(android.ApexModule); !ok {
378 return false
379 }
380
Paul Duffinb17d0442021-05-05 12:07:00 +0100381 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
382 // behavior whether there is a corresponding source module present or not.
383 depName = android.RemoveOptionalPrebuiltPrefix(depName)
384
385 // Remember if this module was added as a direct dependency.
386 direct := parent == mctx.Module()
387 contents[depName] = contents[depName].Add(direct)
388
389 // Add the module to the list of dependencies that need to have an APEX variant.
390 dependencies = append(dependencies, child.(android.ApexModule))
391
392 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100393 })
394
395 // Create contents for the prebuilt_apex and store it away for later use.
396 apexContents := android.NewApexContents(contents)
Spandan Dasf5e03f12024-01-25 19:25:42 +0000397 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100398 Contents: apexContents,
399 })
400
401 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100402 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100403 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000404 ApexVariationName: apexVariationName,
405 InApexVariants: []string{apexVariationName},
Spandan Das3576e762024-01-03 18:57:03 +0000406 InApexModules: []string{p.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100407 ApexContents: []*android.ApexContents{apexContents},
408 ForPrebuiltApex: true,
409 }
410
411 // Mark the dependencies of this module as requiring a variant for this module.
412 for _, am := range dependencies {
413 am.BuildForApex(apexInfo)
414 }
415}
416
Paul Duffin11216db2021-03-01 14:14:52 +0000417// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
418// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
419// deapexer.
420type prebuiltApexSelectorModule struct {
421 android.ModuleBase
422
423 apexFileProperties ApexFileProperties
424
425 inputApex android.Path
426}
427
428func privateApexSelectorModuleFactory() android.Module {
429 module := &prebuiltApexSelectorModule{}
430 module.AddProperties(
431 &module.apexFileProperties,
432 )
433 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
434 return module
435}
436
437func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
438 return android.Paths{p.inputApex}
439}
440
441func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
442 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
443}
444
Jiyong Park09d77522019-11-18 11:16:27 +0900445type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900446 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900447
Paul Duffinbb0dc132021-05-05 16:58:08 +0100448 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900449
Paul Duffinef6b6952021-06-15 11:34:01 +0100450 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700451
452 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900453}
454
Paul Duffin851f3992021-01-13 17:03:51 +0000455type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900456 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000457 //
458 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
459 // for android_common. That is so that it will have the same arch variant as, and so be compatible
460 // with, the source `apex` module type that it replaces.
Cole Faust642e7202024-08-14 17:46:12 -0700461 Src proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park09d77522019-11-18 11:16:27 +0900462 Arch struct {
463 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000464 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900465 }
466 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000467 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900468 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800469 Riscv64 struct {
470 Src *string `android:"path"`
471 }
Jiyong Park09d77522019-11-18 11:16:27 +0900472 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000473 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900474 }
475 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000476 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900477 }
478 }
Paul Duffin851f3992021-01-13 17:03:51 +0000479}
480
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000481// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
482//
483// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
484// to use methods on it that are specific to the current module.
485//
486// See the ApexFileProperties.Src property.
487func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
488 multiTargets := prebuilt.MultiTargets()
489 if len(multiTargets) != 1 {
490 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
491 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000492 }
493 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000494 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000495 case android.Arm:
496 src = String(p.Arch.Arm.Src)
497 case android.Arm64:
498 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800499 case android.Riscv64:
500 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700501 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
502 if src == "" {
503 src = String(p.Arch.Arm64.Src)
504 }
Paul Duffin851f3992021-01-13 17:03:51 +0000505 case android.X86:
506 src = String(p.Arch.X86.Src)
507 case android.X86_64:
508 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000509 }
510 if src == "" {
Cole Faust642e7202024-08-14 17:46:12 -0700511 src = p.Src.GetOrDefault(ctx, "")
Paul Duffin851f3992021-01-13 17:03:51 +0000512 }
Paul Duffin851f3992021-01-13 17:03:51 +0000513
Paul Duffinc0609c62021-03-01 17:27:16 +0000514 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700515 if ctx.Config().AllowMissingDependencies() {
516 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
517 } else {
518 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
519 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000520 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
521 // logic from reporting a more general, less useful message.
522 }
523
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000524 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000525}
526
527type PrebuiltProperties struct {
528 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900529
Paul Duffinef6b6952021-06-15 11:34:01 +0100530 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900531}
532
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700533func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
534 return false
535}
536
Jiyong Park09d77522019-11-18 11:16:27 +0900537// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
538func PrebuiltFactory() android.Module {
539 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100540 module.AddProperties(&module.properties)
541 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000542
Jiyong Park09d77522019-11-18 11:16:27 +0900543 return module
544}
545
Colin Crossda279cf2024-09-17 14:25:45 -0700546func createApexSelectorModule(ctx android.BottomUpMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000547 props := struct {
548 Name *string
549 }{
550 Name: proptools.StringPtr(name),
551 }
552
553 ctx.CreateModule(privateApexSelectorModuleFactory,
554 &props,
555 apexFileProperties,
556 )
557}
558
Paul Duffin5dda3e32021-05-05 14:13:27 +0100559// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
560//
Paul Duffin57f83592021-05-05 15:09:44 +0100561// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
562// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
563// the listed modules need access to files from within the prebuilt .apex file.
Colin Crossda279cf2024-09-17 14:25:45 -0700564func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.BottomUpMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100565 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400566 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100567 return
568 }
569
Paul Duffin57f83592021-05-05 15:09:44 +0100570 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100571 commonModules := []string{}
Spandan Das2ea84dd2024-01-25 22:12:50 +0000572 dexpreoptProfileGuidedModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100573 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100574 ctx.WalkDeps(func(child, parent android.Module) bool {
575 tag := ctx.OtherModuleDependencyTag(child)
576
Paul Duffin7db57e02021-06-17 14:56:05 +0100577 // If the child is not in the same apex as the parent then ignore it and all its children.
578 if !android.IsDepInSameApex(ctx, parent, child) {
579 return false
580 }
581
Spandan Das161e4682024-01-19 00:22:22 +0000582 name := java.ModuleStemForDeapexing(child)
Paul Duffin7db57e02021-06-17 14:56:05 +0100583 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100584 commonModules = append(commonModules, name)
585
Spandan Das2ea84dd2024-01-25 22:12:50 +0000586 extract := child.(android.RequiredFilesFromPrebuiltApex)
587 requiredFiles := extract.RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100588 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100589
Spandan Das2ea84dd2024-01-25 22:12:50 +0000590 if extract.UseProfileGuidedDexpreopt() {
591 dexpreoptProfileGuidedModules = append(dexpreoptProfileGuidedModules, name)
592 }
593
Paul Duffin7db57e02021-06-17 14:56:05 +0100594 // Visit the dependencies of this module just in case they also require files from the
595 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100596 return true
597 }
598
599 return false
600 })
601
Paul Duffin3bae0682021-05-05 18:03:47 +0100602 // Create properties for deapexer module.
603 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100604 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100605 // dependency as well as transitive ones.
Spandan Das2ea84dd2024-01-25 22:12:50 +0000606 CommonModules: android.SortedUniqueStrings(commonModules),
607 DexpreoptProfileGuidedModules: android.SortedUniqueStrings(dexpreoptProfileGuidedModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100608 }
609
610 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100611 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100612
Paul Duffin11216db2021-03-01 14:14:52 +0000613 props := struct {
614 Name *string
615 Selected_apex *string
616 }{
617 Name: proptools.StringPtr(deapexerName),
618 Selected_apex: proptools.StringPtr(apexFileSource),
619 }
620 ctx.CreateModule(privateDeapexerFactory,
621 &props,
622 deapexerProperties,
623 )
624}
625
Paul Duffin11216db2021-03-01 14:14:52 +0000626func apexSelectorModuleName(baseModuleName string) string {
627 return baseModuleName + ".apex.selector"
628}
629
Paul Duffin064b70c2020-11-02 17:32:38 +0000630func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
631 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
632 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
633 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
634 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
635 // and not a renamed prebuilt module then that will be detected and reported as an error when
636 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100637 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000638 if ctx.OtherModuleExists(prebuiltName) {
639 name = prebuiltName
640 }
641 return name
642}
643
Paul Duffina7139422021-02-08 11:01:58 +0000644type exportedDependencyTag struct {
645 blueprint.BaseDependencyTag
646 name string
647}
648
649// Mark this tag so dependencies that use it are excluded from visibility enforcement.
650//
651// This does allow any prebuilt_apex to reference any module which does open up a small window for
652// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
653// avoids opening up a much bigger window by widening the visibility of modules that need files
654// provided by the prebuilt_apex to include all the possible locations they may be defined, which
655// could include everything below vendor/.
656//
657// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
658// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
659// the .apex file. It will also have to be included in the module's apex_available property too.
660// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
661// incorrectly.
662func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
663
Paul Duffin7db57e02021-06-17 14:56:05 +0100664func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
665
666var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
667
Paul Duffina7139422021-02-08 11:01:58 +0000668var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000669 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
670 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
671 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000672)
673
Paul Duffin5dda3e32021-05-05 14:13:27 +0100674var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
675
676// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
677// build.
678//
679// If this needs to make files from within a `.apex` file available for use by other Soong modules,
680// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
681// it does so as follows:
682//
Colin Crossd079e0b2022-08-16 10:27:33 -0700683// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
684// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100685//
Colin Crossd079e0b2022-08-16 10:27:33 -0700686// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
687// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
688// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100689//
Colin Crossd079e0b2022-08-16 10:27:33 -0700690// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
691// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100692//
693// It also creates a child module `selector` that is responsible for selecting the appropriate
694// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
Paul Duffin5dda3e32021-05-05 14:13:27 +0100695//
Colin Crossd079e0b2022-08-16 10:27:33 -0700696// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100697//
Colin Crossd079e0b2022-08-16 10:27:33 -0700698// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
699// `apex_set`.
700//
701// prebuilt_apex
702// / | \
703// / | \
704// V V V
705// selector <--- deapexer <--- exported java lib
Colin Crossda279cf2024-09-17 14:25:45 -0700706func (p *Prebuilt) createPrebuiltApexModules(ctx android.BottomUpMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +0000707 apexSelectorModuleName := apexSelectorModuleName(p.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +0100708 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
709
710 apexFileSource := ":" + apexSelectorModuleName
Spandan Das3576e762024-01-03 18:57:03 +0000711 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(p.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100712
713 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100714 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100715}
716
Paul Duffin57f83592021-05-05 15:09:44 +0100717func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
718 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000719}
720
Spandan Das2069c3f2023-12-06 19:40:24 +0000721func (p *prebuiltCommon) DepsMutator(ctx android.BottomUpMutatorContext) {
722 if p.hasExportedDeps() {
723 // Create a dependency from the prebuilt apex (prebuilt_apex/apex_set) to the internal deapexer module
Spandan Dasbcc2ef02024-08-14 18:55:59 +0000724 // The deapexer will return a provider which will be used to determine the exported artfifacts from this prebuilt.
Spandan Das3576e762024-01-03 18:57:03 +0000725 ctx.AddDependency(ctx.Module(), android.DeapexerTag, deapexerModuleName(p.Name()))
Spandan Das2069c3f2023-12-06 19:40:24 +0000726 }
727}
728
Paul Duffin064b70c2020-11-02 17:32:38 +0000729var _ ApexInfoMutator = (*Prebuilt)(nil)
730
Paul Duffin064b70c2020-11-02 17:32:38 +0000731func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100732 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900733}
734
Spandan Dasda739a32023-12-13 00:06:32 +0000735// Set a provider containing information about the jars and .prof provided by the apex
736// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
737// Used by dex_bootjars to generate the boot image
738func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext) {
739 if !p.hasExportedDeps() {
740 // nothing to do
741 return
742 }
743 if di, err := android.FindDeapexerProviderForModule(ctx); err == nil {
Spandan Das5be63332023-12-13 00:06:32 +0000744 javaModuleToDexPath := map[string]android.Path{}
745 for _, commonModule := range di.GetExportedModuleNames() {
746 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
747 javaModuleToDexPath[commonModule] = dex
748 }
749 }
750
Spandan Dasda739a32023-12-13 00:06:32 +0000751 exports := android.ApexExportsInfo{
Spandan Das5be63332023-12-13 00:06:32 +0000752 ApexName: p.ApexVariationName(),
753 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
754 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
Spandan Dasda739a32023-12-13 00:06:32 +0000755 }
Spandan Dasa41b8ec2023-12-22 00:05:04 +0000756 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000757 } else {
758 ctx.ModuleErrorf(err.Error())
759 }
760}
761
Spandan Dasa747d2e2024-03-11 21:37:25 +0000762// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
763// with information about whether source or prebuilt of an apex was used during the build.
764func (p *prebuiltCommon) providePrebuiltInfo(ctx android.ModuleContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +0000765 info := android.PrebuiltInfo{
766 Name: p.BaseModuleName(),
Spandan Dasa747d2e2024-03-11 21:37:25 +0000767 Is_prebuilt: true,
768 }
769 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
770 if p.prebuiltCommonProperties.Prebuilt_info != nil {
771 info.Prebuilt_info_file_path = android.PathForModuleSrc(ctx, *p.prebuiltCommonProperties.Prebuilt_info).String()
772 }
Spandan Das3490dfd2024-03-11 21:37:25 +0000773 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
Spandan Dasa747d2e2024-03-11 21:37:25 +0000774}
775
Spandan Das3d0d31a2024-05-03 21:36:48 +0000776// Uses an object provided by its deps to validate that the contents of bcpf have been added to the global
777// PRODUCT_APEX_BOOT_JARS
778// This validation will only run on the apex which is active for this product/release_config
779func validateApexClasspathFragments(ctx android.ModuleContext) {
780 ctx.VisitDirectDeps(func(m android.Module) {
781 if info, exists := android.OtherModuleProvider(ctx, m, java.ClasspathFragmentValidationInfoProvider); exists {
782 ctx.ModuleErrorf("%s in contents of %s must also be declared in PRODUCT_APEX_BOOT_JARS", info.UnknownJars, info.ClasspathFragmentModuleName)
783 }
784 })
785}
786
Jiyong Park09d77522019-11-18 11:16:27 +0900787func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000788 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000789 if !p.IsHideFromMake() {
790 validateApexClasspathFragments(ctx)
791 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000792
Jooyung Han286957d2023-10-30 16:17:56 +0900793 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900794 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100795 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900796 p.installDir = android.PathForModuleInstall(ctx, "apex")
797 p.installFilename = p.InstallFilename()
798 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
799 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
800 }
801 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
802 ctx.Build(pctx, android.BuildParams{
803 Rule: android.Cp,
804 Input: p.inputApex,
805 Output: p.outputApex,
806 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900807
808 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800809 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900810 return
811 }
812
Spandan Das2069c3f2023-12-06 19:40:24 +0000813 // dexpreopt any system server jars if present
814 p.dexpreoptSystemServerJars(ctx)
815
Spandan Dasda739a32023-12-13 00:06:32 +0000816 // provide info used for generating the boot image
817 p.provideApexExportsInfo(ctx)
818
Spandan Dasa747d2e2024-03-11 21:37:25 +0000819 p.providePrebuiltInfo(ctx)
820
Paul Duffinc30aea22021-06-15 19:10:11 +0100821 // Save the files that need to be made available to Make.
822 p.initApexFilesForAndroidMk(ctx)
823
Colin Crossccba23d2021-11-12 19:01:29 +0000824 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900825 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000826 // or that prebuilt_apex overrides other apexes (using overrides: prop)
827 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900828 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700829 }
830
831 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800832 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700833 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900834 }
mrziwang1587b9c2024-06-13 11:26:04 -0700835
836 ctx.SetOutputFiles(android.Paths{p.outputApex}, "")
Jiyong Park09d77522019-11-18 11:16:27 +0900837}
838
Wei Li340ee8e2022-03-18 17:33:24 -0700839func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
840 return p.provenanceMetaDataFile
841}
842
Paul Duffin24704672021-04-06 16:09:30 +0100843// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
844// module. It extracts the correct apex to use and makes it available for use by apex_set.
845type prebuiltApexExtractorModule struct {
846 android.ModuleBase
847
848 properties ApexExtractorProperties
849
850 extractedApex android.WritablePath
851}
852
853func privateApexExtractorModuleFactory() android.Module {
854 module := &prebuiltApexExtractorModule{}
855 module.AddProperties(
856 &module.properties,
857 )
858 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
859 return module
860}
861
862func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
863 return android.Paths{p.extractedApex}
864}
865
866func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
867 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
868 return p.properties.prebuiltSrcs(ctx)
869 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000870 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100871 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
872 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000873 // Filter out NativeBridge archs (b/260115309)
874 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100875 ctx.Build(pctx,
876 android.BuildParams{
877 Rule: extractMatchingApex,
878 Description: "Extract an apex from an apex set",
879 Inputs: android.Paths{apexSet},
880 Output: p.extractedApex,
881 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000882 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000883 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100884 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700885 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100886 },
887 })
888}
889
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700890type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900891 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700892
893 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700894}
895
Paul Duffin24704672021-04-06 16:09:30 +0100896type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700897 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000898 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700899
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700900 Sanitized struct {
901 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000902 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700903 }
904 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000905 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700906 }
907 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000908 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700909 }
910 }
911
Paul Duffin24704672021-04-06 16:09:30 +0100912 // apexes in this set use prerelease SDK version
913 Prerelease *bool
914}
915
916func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
917 var srcs []string
918 if e.Set != nil {
919 srcs = append(srcs, *e.Set)
920 }
921
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900922 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100923
924 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
925 srcs = append(srcs, *e.Sanitized.Address.Set)
926 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
927 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
928 } else if e.Sanitized.None.Set != nil {
929 srcs = append(srcs, *e.Sanitized.None.Set)
930 }
931
932 return srcs
933}
934
935type ApexSetProperties struct {
936 ApexExtractorProperties
937
Paul Duffinef6b6952021-06-15 11:34:01 +0100938 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700939}
940
941func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
942 if sanitizer == "address" {
943 return a.properties.Sanitized.Address.Set != nil
944 }
945 if sanitizer == "hwaddress" {
946 return a.properties.Sanitized.Hwaddress.Set != nil
947 }
948
949 return false
950}
951
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700952// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
953func apexSetFactory() android.Module {
954 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100955 module.AddProperties(&module.properties)
956 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100957
Paul Duffin24704672021-04-06 16:09:30 +0100958 return module
959}
960
Colin Crossda279cf2024-09-17 14:25:45 -0700961func createApexExtractorModule(ctx android.BottomUpMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100962 props := struct {
963 Name *string
964 }{
965 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700966 }
967
Paul Duffin24704672021-04-06 16:09:30 +0100968 ctx.CreateModule(privateApexExtractorModuleFactory,
969 &props,
970 apexExtractorProperties,
971 )
972}
973
974func apexExtractorModuleName(baseModuleName string) string {
975 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700976}
977
Paul Duffin5dda3e32021-05-05 14:13:27 +0100978var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
979
980// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
981// modules.
982//
983// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
984// prebuilt_apex except that instead of creating a selector module which selects one .apex file
985// from those provided this creates an extractor module which extracts the appropriate .apex file
986// from the zip file containing them.
Colin Crossda279cf2024-09-17 14:25:45 -0700987func (a *ApexSet) createPrebuiltApexModules(ctx android.BottomUpMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +0000988 apexExtractorModuleName := apexExtractorModuleName(a.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +0100989 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
990
991 apexFileSource := ":" + apexExtractorModuleName
Spandan Das3576e762024-01-03 18:57:03 +0000992 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(a.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100993
994 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100995 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100996}
997
Paul Duffin57f83592021-05-05 15:09:44 +0100998func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
999 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01001000}
1001
1002var _ ApexInfoMutator = (*ApexSet)(nil)
1003
1004func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
1005 a.apexInfoMutator(mctx)
1006}
1007
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001008func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +00001009 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +00001010 if !a.IsHideFromMake() {
1011 validateApexClasspathFragments(ctx)
1012 }
Spandan Das3d0d31a2024-05-03 21:36:48 +00001013
Jooyung Han286957d2023-10-30 16:17:56 +09001014 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001015 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +01001016 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
1017 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001018 }
1019
Paul Duffinbb0dc132021-05-05 16:58:08 +01001020 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001021 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Jooyung Han26ec8482024-07-31 15:04:05 +09001022
1023 // Build the output APEX. If compression is not enabled, make sure the output is not compressed even if the input is compressed
1024 buildRule := android.Cp
1025 if !ctx.Config().ApexCompressionEnabled() {
1026 buildRule = decompressApex
1027 }
Paul Duffin24704672021-04-06 16:09:30 +01001028 ctx.Build(pctx, android.BuildParams{
Jooyung Han26ec8482024-07-31 15:04:05 +09001029 Rule: buildRule,
Paul Duffin24704672021-04-06 16:09:30 +01001030 Input: inputApex,
1031 Output: a.outputApex,
1032 })
Jiyong Park10e926b2020-07-16 21:38:56 +09001033
1034 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001035 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +09001036 return
1037 }
1038
Spandan Das2069c3f2023-12-06 19:40:24 +00001039 // dexpreopt any system server jars if present
1040 a.dexpreoptSystemServerJars(ctx)
1041
Spandan Dasda739a32023-12-13 00:06:32 +00001042 // provide info used for generating the boot image
1043 a.provideApexExportsInfo(ctx)
1044
Spandan Dasa747d2e2024-03-11 21:37:25 +00001045 a.providePrebuiltInfo(ctx)
1046
Paul Duffinc30aea22021-06-15 19:10:11 +01001047 // Save the files that need to be made available to Make.
1048 a.initApexFilesForAndroidMk(ctx)
1049
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001050 a.installDir = android.PathForModuleInstall(ctx, "apex")
1051 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001052 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001053 }
1054
1055 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001056 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001057 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001058 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001059 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001060 }
mrziwang1587b9c2024-06-13 11:26:04 -07001061
1062 ctx.SetOutputFiles(android.Paths{a.outputApex}, "")
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001063}
1064
Paul Duffinef6b6952021-06-15 11:34:01 +01001065type systemExtContext struct {
1066 android.ModuleContext
1067}
1068
1069func (*systemExtContext) SystemExtSpecific() bool {
1070 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001071}