blob: d3f14f9c09f13ebd700f0dffba5e30ca52e11407 [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 bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
111 // bundle will create an APEX variant.
112 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000113
114 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
115 // APEX bundle will create an APEX variant.
116 Exported_systemserverclasspath_fragments []string
Spandan Dasa747d2e2024-03-11 21:37:25 +0000117
118 // Path to the .prebuilt_info file of the prebuilt apex.
119 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used to
120 // generate the prebuilt.
121 Prebuilt_info *string `android:"path"`
Jiyong Park10e926b2020-07-16 21:38:56 +0900122}
123
Paul Duffinef6b6952021-06-15 11:34:01 +0100124// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
125// module that is common to Prebuilt and ApexSet.
126func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
127 p.prebuiltCommonProperties = properties
128 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
129 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
130}
131
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100132func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000133 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
134}
135
136func (p *prebuiltCommon) BaseModuleName() string {
137 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100138}
139
Jiyong Park10e926b2020-07-16 21:38:56 +0900140func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
141 return &p.prebuilt
142}
143
144func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100145 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900146}
147
148func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900149 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900150
151 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
152 // to build the prebuilts themselves.
153 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
154
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700155 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
156 sanitized := ctx.Module().(sanitizedPrebuilt)
157 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
158 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900159
160 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100161 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900162 return true
163 }
164 return false
165}
166
Paul Duffinef6b6952021-06-15 11:34:01 +0100167func (p *prebuiltCommon) InstallFilename() string {
168 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
169}
170
171func (p *prebuiltCommon) Name() string {
172 return p.prebuilt.Name(p.ModuleBase.Name())
173}
174
175func (p *prebuiltCommon) Overrides() []string {
176 return p.prebuiltCommonProperties.Overrides
177}
178
179func (p *prebuiltCommon) installable() bool {
180 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
181}
182
Spandan Das2069c3f2023-12-06 19:40:24 +0000183// To satisfy java.DexpreopterInterface
184func (p *prebuiltCommon) IsInstallable() bool {
185 return p.installable()
186}
187
188// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100189func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000190 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
191 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
192 p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
193 }
194}
Paul Duffinc30aea22021-06-15 19:10:11 +0100195
Spandan Das2069c3f2023-12-06 19:40:24 +0000196// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
Spandan Das52c01a12024-09-20 01:09:48 +0000197func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext, di *android.DeapexerInfo) {
198 if di == nil {
Spandan Das2069c3f2023-12-06 19:40:24 +0000199 return
200 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000201 // If this prebuilt apex has not been selected, return
202 if p.IsHideFromMake() {
203 return
204 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000205 // Use apex_name to determine the api domain of this prebuilt apex
206 apexName := p.ApexVariationName()
Spandan Das52c01a12024-09-20 01:09:48 +0000207 // TODO: do not compute twice
Spandan Das2069c3f2023-12-06 19:40:24 +0000208 dc := dexpreopt.GetGlobalConfig(ctx)
209 systemServerJarList := dc.AllApexSystemServerJars(ctx)
210
211 for i := 0; i < systemServerJarList.Len(); i++ {
212 sscpApex := systemServerJarList.Apex(i)
213 sscpJar := systemServerJarList.Jar(i)
214 if apexName != sscpApex {
215 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100216 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000217 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
218 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100219}
220
Jiakai Zhang204356f2021-09-09 08:12:46 +0000221func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000222 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000223}
224
Paul Duffinef6b6952021-06-15 11:34:01 +0100225func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100226 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100227 {
228 Class: "ETC",
229 OutputFile: android.OptionalPathForPath(p.outputApex),
230 Include: "$(BUILD_PREBUILT)",
231 Host_required: p.hostRequired,
232 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
233 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800234 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100235 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700236 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
237 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700238 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100239 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
240 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900241 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000242 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100243 },
244 },
245 },
246 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100247
Spandan Das2069c3f2023-12-06 19:40:24 +0000248 // Add the dexpreopt artifacts to androidmk
249 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
250 entriesList = append(entriesList, install.ToMakeEntries())
251 }
252
Paul Duffinc30aea22021-06-15 19:10:11 +0100253 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100254}
255
Spandan Das9d6e2092024-09-21 02:50:00 +0000256// DEPRECATED. // TODO (spandandas): Remove this interface.
257
Paul Duffin5dda3e32021-05-05 14:13:27 +0100258// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
259// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
260type prebuiltApexModuleCreator interface {
Colin Crossda279cf2024-09-17 14:25:45 -0700261 createPrebuiltApexModules(ctx android.BottomUpMutatorContext)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100262}
263
264// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
265// prebuiltApexModuleCreator's createPrebuiltApexModules method.
266//
267// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
268// will need to access dependencies added by that (exported modules) but must run before the
269// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
270// exported modules.
Colin Crossda279cf2024-09-17 14:25:45 -0700271func prebuiltApexModuleCreatorMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100272 module := ctx.Module()
273 if creator, ok := module.(prebuiltApexModuleCreator); ok {
274 creator.createPrebuiltApexModules(ctx)
275 }
276}
277
Liz Kammer2dc72442023-04-20 10:10:48 -0400278func (p *prebuiltCommon) hasExportedDeps() bool {
Spandan Das1896fd62024-09-18 21:49:14 +0000279 return len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
Liz Kammer2dc72442023-04-20 10:10:48 -0400280 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000281}
282
Paul Duffin57f83592021-05-05 15:09:44 +0100283// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
284func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
285 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100286
Liz Kammer2dc72442023-04-20 10:10:48 -0400287 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
288 prebuiltDep := android.PrebuiltNameFromSource(dep)
289 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
290 }
291
292 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
293 prebuiltDep := android.PrebuiltNameFromSource(dep)
294 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100295 }
Paul Duffindfd33262021-04-06 17:02:08 +0100296}
297
Paul Duffinb17d0442021-05-05 12:07:00 +0100298// Implements android.DepInInSameApex
299func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
300 tag := ctx.OtherModuleDependencyTag(dep)
301 _, ok := tag.(exportedDependencyTag)
302 return ok
303}
304
Paul Duffindfd33262021-04-06 17:02:08 +0100305// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
306// specific variant and checks that they are supported.
307//
308// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
309// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
310//
311// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
312// across prebuilt_apex modules. That is because there is no way to determine whether two
313// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
314// been built from different source at different times or they could have been built with different
315// build options that affect the libraries.
316//
317// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
318// modules were compatible it would be a lot of work and would not provide much benefit for a couple
319// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700320// - The number of prebuilt_apex modules that will be exporting files for the same module will be
321// low as the prebuilt_apex only exports files for the direct dependencies that require it and
322// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
323// few com.android.art* apex files that contain the same contents and could export files for the
324// same modules but only one of them needs to do so. Contrast that with source apex modules which
325// need apex specific variants for every module that contributes code to the apex, whether direct
326// or indirect.
327// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
328// extra copying of files. Contrast that with source apex modules that has to build each variant
329// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100330func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
331
332 // Collect direct dependencies into contents.
333 contents := make(map[string]android.ApexMembership)
334
335 // Collect the list of dependencies.
336 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100337 mctx.WalkDeps(func(child, parent android.Module) bool {
338 // If the child is not in the same apex as the parent then exit immediately and do not visit
339 // any of the child's dependencies.
340 if !android.IsDepInSameApex(mctx, parent, child) {
341 return false
342 }
343
344 tag := mctx.OtherModuleDependencyTag(child)
345 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100346 if exportedTag, ok := tag.(exportedDependencyTag); ok {
347 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100348
349 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100350 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100351 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100352 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100353 }
354
355 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100356 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100357 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100358 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100359 }
Paul Duffindfd33262021-04-06 17:02:08 +0100360 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100361
Paul Duffinfee8cf32021-06-29 18:38:38 +0100362 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
363 // variant.
364 if _, ok := child.(android.ApexModule); !ok {
365 return false
366 }
367
Paul Duffinb17d0442021-05-05 12:07:00 +0100368 // 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)
Spandan Dasf5e03f12024-01-25 19:25:42 +0000384 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100385 Contents: apexContents,
386 })
387
388 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100389 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100390 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000391 ApexVariationName: apexVariationName,
392 InApexVariants: []string{apexVariationName},
Spandan Das3576e762024-01-03 18:57:03 +0000393 InApexModules: []string{p.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
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
Jiyong Park09d77522019-11-18 11:16:27 +0900404type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900405 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900406
Paul Duffinbb0dc132021-05-05 16:58:08 +0100407 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900408
Paul Duffinef6b6952021-06-15 11:34:01 +0100409 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700410
411 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900412}
413
Paul Duffin851f3992021-01-13 17:03:51 +0000414type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900415 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000416 //
417 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
418 // for android_common. That is so that it will have the same arch variant as, and so be compatible
419 // with, the source `apex` module type that it replaces.
Cole Faust642e7202024-08-14 17:46:12 -0700420 Src proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park09d77522019-11-18 11:16:27 +0900421 Arch struct {
422 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000423 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900424 }
425 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000426 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900427 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800428 Riscv64 struct {
429 Src *string `android:"path"`
430 }
Jiyong Park09d77522019-11-18 11:16:27 +0900431 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000432 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900433 }
434 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000435 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900436 }
437 }
Paul Duffin851f3992021-01-13 17:03:51 +0000438}
439
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000440// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
441//
442// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
443// to use methods on it that are specific to the current module.
444//
445// See the ApexFileProperties.Src property.
Spandan Dase350e362024-09-21 01:49:34 +0000446func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) string {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000447 multiTargets := prebuilt.MultiTargets()
448 if len(multiTargets) != 1 {
449 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
Spandan Dase350e362024-09-21 01:49:34 +0000450 return ""
Paul Duffin851f3992021-01-13 17:03:51 +0000451 }
452 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000453 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000454 case android.Arm:
455 src = String(p.Arch.Arm.Src)
456 case android.Arm64:
457 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800458 case android.Riscv64:
459 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700460 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
461 if src == "" {
462 src = String(p.Arch.Arm64.Src)
463 }
Paul Duffin851f3992021-01-13 17:03:51 +0000464 case android.X86:
465 src = String(p.Arch.X86.Src)
466 case android.X86_64:
467 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000468 }
469 if src == "" {
Cole Faust642e7202024-08-14 17:46:12 -0700470 src = p.Src.GetOrDefault(ctx, "")
Paul Duffin851f3992021-01-13 17:03:51 +0000471 }
Paul Duffin851f3992021-01-13 17:03:51 +0000472
Paul Duffinc0609c62021-03-01 17:27:16 +0000473 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700474 if ctx.Config().AllowMissingDependencies() {
475 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
476 } else {
477 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
478 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000479 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
480 // logic from reporting a more general, less useful message.
481 }
482
Spandan Dase350e362024-09-21 01:49:34 +0000483 return src
Paul Duffin851f3992021-01-13 17:03:51 +0000484}
485
486type PrebuiltProperties struct {
487 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900488
Paul Duffinef6b6952021-06-15 11:34:01 +0100489 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900490}
491
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700492func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
493 return false
494}
495
Jiyong Park09d77522019-11-18 11:16:27 +0900496// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
497func PrebuiltFactory() android.Module {
498 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100499 module.AddProperties(&module.properties)
Spandan Dase350e362024-09-21 01:49:34 +0000500 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
501
502 // init the module as a prebuilt
503 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
504 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
505 // The actual src will be evaluated in GenerateAndroidBuildActions.
506 android.InitPrebuiltModuleWithoutSrcs(module)
507 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000508
Jiyong Park09d77522019-11-18 11:16:27 +0900509 return module
510}
511
Spandan Das52c01a12024-09-20 01:09:48 +0000512func (p *prebuiltCommon) getDeapexerPropertiesIfNeeded(ctx android.ModuleContext) DeapexerProperties {
Paul Duffin57f83592021-05-05 15:09:44 +0100513 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100514 commonModules := []string{}
Spandan Das2ea84dd2024-01-25 22:12:50 +0000515 dexpreoptProfileGuidedModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100516 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100517 ctx.WalkDeps(func(child, parent android.Module) bool {
518 tag := ctx.OtherModuleDependencyTag(child)
519
Paul Duffin7db57e02021-06-17 14:56:05 +0100520 // If the child is not in the same apex as the parent then ignore it and all its children.
521 if !android.IsDepInSameApex(ctx, parent, child) {
522 return false
523 }
524
Spandan Das161e4682024-01-19 00:22:22 +0000525 name := java.ModuleStemForDeapexing(child)
Paul Duffin7db57e02021-06-17 14:56:05 +0100526 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100527 commonModules = append(commonModules, name)
528
Spandan Das2ea84dd2024-01-25 22:12:50 +0000529 extract := child.(android.RequiredFilesFromPrebuiltApex)
530 requiredFiles := extract.RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100531 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100532
Spandan Das2ea84dd2024-01-25 22:12:50 +0000533 if extract.UseProfileGuidedDexpreopt() {
534 dexpreoptProfileGuidedModules = append(dexpreoptProfileGuidedModules, name)
535 }
536
Paul Duffin7db57e02021-06-17 14:56:05 +0100537 // Visit the dependencies of this module just in case they also require files from the
538 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100539 return true
540 }
541
542 return false
543 })
544
Paul Duffin3bae0682021-05-05 18:03:47 +0100545 // Create properties for deapexer module.
Spandan Das52c01a12024-09-20 01:09:48 +0000546 deapexerProperties := DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100547 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100548 // dependency as well as transitive ones.
Spandan Das2ea84dd2024-01-25 22:12:50 +0000549 CommonModules: android.SortedUniqueStrings(commonModules),
550 DexpreoptProfileGuidedModules: android.SortedUniqueStrings(dexpreoptProfileGuidedModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100551 }
552
553 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100554 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Spandan Das52c01a12024-09-20 01:09:48 +0000555 return deapexerProperties
Paul Duffin11216db2021-03-01 14:14:52 +0000556}
557
Paul Duffin064b70c2020-11-02 17:32:38 +0000558func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
559 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
560 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
561 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
562 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
563 // and not a renamed prebuilt module then that will be detected and reported as an error when
564 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100565 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000566 if ctx.OtherModuleExists(prebuiltName) {
567 name = prebuiltName
568 }
569 return name
570}
571
Paul Duffina7139422021-02-08 11:01:58 +0000572type exportedDependencyTag struct {
573 blueprint.BaseDependencyTag
574 name string
575}
576
577// Mark this tag so dependencies that use it are excluded from visibility enforcement.
578//
579// This does allow any prebuilt_apex to reference any module which does open up a small window for
580// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
581// avoids opening up a much bigger window by widening the visibility of modules that need files
582// provided by the prebuilt_apex to include all the possible locations they may be defined, which
583// could include everything below vendor/.
584//
585// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
586// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
587// the .apex file. It will also have to be included in the module's apex_available property too.
588// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
589// incorrectly.
590func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
591
Paul Duffin7db57e02021-06-17 14:56:05 +0100592func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
593
594var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
595
Paul Duffina7139422021-02-08 11:01:58 +0000596var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000597 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
598 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000599)
600
Paul Duffin57f83592021-05-05 15:09:44 +0100601func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
602 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000603}
604
605var _ ApexInfoMutator = (*Prebuilt)(nil)
606
Paul Duffin064b70c2020-11-02 17:32:38 +0000607func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100608 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900609}
610
Spandan Das52c01a12024-09-20 01:09:48 +0000611// creates the build rules to deapex the prebuilt, and returns a deapexerInfo
Spandan Dase350e362024-09-21 01:49:34 +0000612func (p *prebuiltCommon) getDeapexerInfo(ctx android.ModuleContext, apexFile android.Path) *android.DeapexerInfo {
Spandan Das52c01a12024-09-20 01:09:48 +0000613 if !p.hasExportedDeps() {
614 // nothing to do
615 return nil
616 }
Spandan Das52c01a12024-09-20 01:09:48 +0000617 deapexerProps := p.getDeapexerPropertiesIfNeeded(ctx)
618 return deapex(ctx, apexFile, deapexerProps)
619}
620
Spandan Dasda739a32023-12-13 00:06:32 +0000621// Set a provider containing information about the jars and .prof provided by the apex
622// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
623// Used by dex_bootjars to generate the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000624func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext, di *android.DeapexerInfo) {
625 if di == nil {
Spandan Dasda739a32023-12-13 00:06:32 +0000626 return
627 }
Spandan Das52c01a12024-09-20 01:09:48 +0000628 javaModuleToDexPath := map[string]android.Path{}
629 for _, commonModule := range di.GetExportedModuleNames() {
630 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
631 javaModuleToDexPath[commonModule] = dex
Spandan Das5be63332023-12-13 00:06:32 +0000632 }
Spandan Dasda739a32023-12-13 00:06:32 +0000633 }
Spandan Das52c01a12024-09-20 01:09:48 +0000634
635 exports := android.ApexExportsInfo{
636 ApexName: p.ApexVariationName(),
637 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
638 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
639 }
640 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000641}
642
Spandan Dasa747d2e2024-03-11 21:37:25 +0000643// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
644// with information about whether source or prebuilt of an apex was used during the build.
645func (p *prebuiltCommon) providePrebuiltInfo(ctx android.ModuleContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +0000646 info := android.PrebuiltInfo{
647 Name: p.BaseModuleName(),
Spandan Dasa747d2e2024-03-11 21:37:25 +0000648 Is_prebuilt: true,
649 }
650 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
651 if p.prebuiltCommonProperties.Prebuilt_info != nil {
652 info.Prebuilt_info_file_path = android.PathForModuleSrc(ctx, *p.prebuiltCommonProperties.Prebuilt_info).String()
653 }
Spandan Das3490dfd2024-03-11 21:37:25 +0000654 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
Spandan Dasa747d2e2024-03-11 21:37:25 +0000655}
656
Spandan Das3d0d31a2024-05-03 21:36:48 +0000657// Uses an object provided by its deps to validate that the contents of bcpf have been added to the global
658// PRODUCT_APEX_BOOT_JARS
659// This validation will only run on the apex which is active for this product/release_config
660func validateApexClasspathFragments(ctx android.ModuleContext) {
661 ctx.VisitDirectDeps(func(m android.Module) {
662 if info, exists := android.OtherModuleProvider(ctx, m, java.ClasspathFragmentValidationInfoProvider); exists {
663 ctx.ModuleErrorf("%s in contents of %s must also be declared in PRODUCT_APEX_BOOT_JARS", info.UnknownJars, info.ClasspathFragmentModuleName)
664 }
665 })
666}
667
Jiyong Park09d77522019-11-18 11:16:27 +0900668func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000669 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000670 if !p.IsHideFromMake() {
671 validateApexClasspathFragments(ctx)
672 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000673
Jooyung Han286957d2023-10-30 16:17:56 +0900674 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900675 // TODO(jungjw): Check the key validity.
Spandan Dase350e362024-09-21 01:49:34 +0000676 p.inputApex = android.PathForModuleSrc(ctx, p.properties.prebuiltApexSelector(ctx, ctx.Module()))
Jiyong Park09d77522019-11-18 11:16:27 +0900677 p.installDir = android.PathForModuleInstall(ctx, "apex")
678 p.installFilename = p.InstallFilename()
679 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
680 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
681 }
682 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
683 ctx.Build(pctx, android.BuildParams{
684 Rule: android.Cp,
685 Input: p.inputApex,
686 Output: p.outputApex,
687 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900688
689 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800690 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900691 return
692 }
693
Spandan Dase350e362024-09-21 01:49:34 +0000694 deapexerInfo := p.getDeapexerInfo(ctx, p.inputApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000695
Spandan Das2069c3f2023-12-06 19:40:24 +0000696 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000697 p.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000698
Spandan Dasda739a32023-12-13 00:06:32 +0000699 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000700 p.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000701
Spandan Dasa747d2e2024-03-11 21:37:25 +0000702 p.providePrebuiltInfo(ctx)
703
Paul Duffinc30aea22021-06-15 19:10:11 +0100704 // Save the files that need to be made available to Make.
705 p.initApexFilesForAndroidMk(ctx)
706
Colin Crossccba23d2021-11-12 19:01:29 +0000707 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900708 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000709 // or that prebuilt_apex overrides other apexes (using overrides: prop)
710 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900711 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700712 }
713
714 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800715 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700716 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900717 }
mrziwang1587b9c2024-06-13 11:26:04 -0700718
719 ctx.SetOutputFiles(android.Paths{p.outputApex}, "")
Jiyong Park09d77522019-11-18 11:16:27 +0900720}
721
Wei Li340ee8e2022-03-18 17:33:24 -0700722func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
723 return p.provenanceMetaDataFile
724}
725
Paul Duffin24704672021-04-06 16:09:30 +0100726// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
727// module. It extracts the correct apex to use and makes it available for use by apex_set.
728type prebuiltApexExtractorModule struct {
729 android.ModuleBase
730
731 properties ApexExtractorProperties
732
733 extractedApex android.WritablePath
734}
735
Spandan Das9d6e2092024-09-21 02:50:00 +0000736// extract registers the build actions to extract an apex from .apks file
737// returns the path of the extracted apex
738func extract(ctx android.ModuleContext, apexSet android.Path, prerelease *bool) android.Path {
Paul Duffin76fdd672022-12-12 18:00:47 +0000739 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Spandan Das9d6e2092024-09-21 02:50:00 +0000740 extractedApex := android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000741 // Filter out NativeBridge archs (b/260115309)
742 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100743 ctx.Build(pctx,
744 android.BuildParams{
745 Rule: extractMatchingApex,
746 Description: "Extract an apex from an apex set",
747 Inputs: android.Paths{apexSet},
Spandan Das9d6e2092024-09-21 02:50:00 +0000748 Output: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100749 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000750 "abis": strings.Join(abis, ","),
Spandan Das9d6e2092024-09-21 02:50:00 +0000751 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100752 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700753 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100754 },
Spandan Das9d6e2092024-09-21 02:50:00 +0000755 },
756 )
757 return extractedApex
Paul Duffin24704672021-04-06 16:09:30 +0100758}
759
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700760type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900761 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700762
763 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700764}
765
Paul Duffin24704672021-04-06 16:09:30 +0100766type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700767 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000768 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700769
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700770 Sanitized struct {
771 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000772 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700773 }
774 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000775 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700776 }
777 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000778 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700779 }
780 }
781
Paul Duffin24704672021-04-06 16:09:30 +0100782 // apexes in this set use prerelease SDK version
783 Prerelease *bool
784}
785
786func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
787 var srcs []string
788 if e.Set != nil {
789 srcs = append(srcs, *e.Set)
790 }
791
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900792 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100793
794 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
795 srcs = append(srcs, *e.Sanitized.Address.Set)
796 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
797 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
798 } else if e.Sanitized.None.Set != nil {
799 srcs = append(srcs, *e.Sanitized.None.Set)
800 }
801
802 return srcs
803}
804
805type ApexSetProperties struct {
806 ApexExtractorProperties
807
Paul Duffinef6b6952021-06-15 11:34:01 +0100808 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700809}
810
811func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
812 if sanitizer == "address" {
813 return a.properties.Sanitized.Address.Set != nil
814 }
815 if sanitizer == "hwaddress" {
816 return a.properties.Sanitized.Hwaddress.Set != nil
817 }
818
819 return false
820}
821
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700822// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
823func apexSetFactory() android.Module {
824 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100825 module.AddProperties(&module.properties)
Spandan Das9d6e2092024-09-21 02:50:00 +0000826 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
827
828 // init the module as a prebuilt
829 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
830 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
831 // The actual src will be evaluated in GenerateAndroidBuildActions.
832 android.InitPrebuiltModuleWithoutSrcs(module)
833 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin24704672021-04-06 16:09:30 +0100834
Paul Duffin24704672021-04-06 16:09:30 +0100835 return module
836}
837
Paul Duffin57f83592021-05-05 15:09:44 +0100838func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
839 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100840}
841
842var _ ApexInfoMutator = (*ApexSet)(nil)
843
844func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
845 a.apexInfoMutator(mctx)
846}
847
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700848func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000849 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000850 if !a.IsHideFromMake() {
851 validateApexClasspathFragments(ctx)
852 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000853
Jooyung Han286957d2023-10-30 16:17:56 +0900854 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700855 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100856 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
857 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700858 }
859
Spandan Das9d6e2092024-09-21 02:50:00 +0000860 var apexSet android.Path
861 if srcs := a.properties.prebuiltSrcs(ctx); len(srcs) == 1 {
862 apexSet = android.PathForModuleSrc(ctx, srcs[0])
863 } else {
864 ctx.ModuleErrorf("Expected exactly one source apex_set file, found %v\n", srcs)
865 }
866
867 extractedApex := extract(ctx, apexSet, a.properties.Prerelease)
868
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700869 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Jooyung Han26ec8482024-07-31 15:04:05 +0900870
871 // Build the output APEX. If compression is not enabled, make sure the output is not compressed even if the input is compressed
872 buildRule := android.Cp
873 if !ctx.Config().ApexCompressionEnabled() {
874 buildRule = decompressApex
875 }
Paul Duffin24704672021-04-06 16:09:30 +0100876 ctx.Build(pctx, android.BuildParams{
Jooyung Han26ec8482024-07-31 15:04:05 +0900877 Rule: buildRule,
Spandan Das9d6e2092024-09-21 02:50:00 +0000878 Input: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100879 Output: a.outputApex,
880 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900881
882 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800883 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900884 return
885 }
886
Spandan Das9d6e2092024-09-21 02:50:00 +0000887 deapexerInfo := a.getDeapexerInfo(ctx, extractedApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000888
Spandan Das2069c3f2023-12-06 19:40:24 +0000889 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000890 a.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000891
Spandan Dasda739a32023-12-13 00:06:32 +0000892 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000893 a.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000894
Spandan Dasa747d2e2024-03-11 21:37:25 +0000895 a.providePrebuiltInfo(ctx)
896
Paul Duffinc30aea22021-06-15 19:10:11 +0100897 // Save the files that need to be made available to Make.
898 a.initApexFilesForAndroidMk(ctx)
899
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700900 a.installDir = android.PathForModuleInstall(ctx, "apex")
901 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -0800902 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700903 }
904
905 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900906 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700907 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100908 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900909 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700910 }
mrziwang1587b9c2024-06-13 11:26:04 -0700911
912 ctx.SetOutputFiles(android.Paths{a.outputApex}, "")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700913}
914
Paul Duffinef6b6952021-06-15 11:34:01 +0100915type systemExtContext struct {
916 android.ModuleContext
917}
918
919func (*systemExtContext) SystemExtSpecific() bool {
920 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700921}