blob: 85b64e743467e814f7a41ff62f5ec2cb25b11318 [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 (
Colin Cross388c6612025-01-28 14:00:12 -080018 "slices"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070019 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090020 "strings"
21
22 "android/soong/android"
Spandan Das2069c3f2023-12-06 19:40:24 +000023 "android/soong/dexpreopt"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070024 "android/soong/java"
Wei Li340ee8e2022-03-18 17:33:24 -070025 "android/soong/provenance"
Anton Hansson805e0a52022-11-25 14:06:46 +000026
Jaewoong Jungfa00c062020-05-14 14:15:24 -070027 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090028 "github.com/google/blueprint/proptools"
29)
30
Jaewoong Jungfa00c062020-05-14 14:15:24 -070031var (
32 extractMatchingApex = pctx.StaticRule(
33 "extractMatchingApex",
34 blueprint.RuleParams{
35 Command: `rm -rf "$out" && ` +
36 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -070037 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
38 `-screen-densities=all -extract-single ` +
Jaewoong Jungfa00c062020-05-14 14:15:24 -070039 `${in}`,
40 CommandDeps: []string{"${extract_apks}"},
41 },
Pranav Gupta51645ff2023-03-20 16:19:53 -070042 "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
Jooyung Han26ec8482024-07-31 15:04:05 +090043 decompressApex = pctx.StaticRule("decompressApex", blueprint.RuleParams{
Jooyung Han8fa61162024-08-08 10:42:47 +090044 Command: `rm -rf $out && ${deapexer} decompress --copy-if-uncompressed --input ${in} --output ${out}`,
Jooyung Han26ec8482024-07-31 15:04:05 +090045 CommandDeps: []string{"${deapexer}"},
Jooyung Han8fa61162024-08-08 10:42:47 +090046 Description: "decompress $out",
Jooyung Han26ec8482024-07-31 15:04:05 +090047 })
Jaewoong Jungfa00c062020-05-14 14:15:24 -070048)
49
Jiyong Park10e926b2020-07-16 21:38:56 +090050type prebuilt interface {
51 isForceDisabled() bool
52 InstallFilename() string
53}
54
55type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010056 android.ModuleBase
Spandan Das2069c3f2023-12-06 19:40:24 +000057 java.Dexpreopter
Paul Duffinbb0dc132021-05-05 16:58:08 +010058 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010059
Paul Duffinbb0dc132021-05-05 16:58:08 +010060 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010061 prebuiltCommonProperties *PrebuiltCommonProperties
62
Colin Cross388c6612025-01-28 14:00:12 -080063 installDir android.InstallPath
64 installFilename string
65 installedFile android.InstallPath
66 extraInstalledFiles android.InstallPaths
67 extraInstalledPairs installPairs
68 outputApex android.WritablePath
Paul Duffinef6b6952021-06-15 11:34:01 +010069
Jooyung Han286957d2023-10-30 16:17:56 +090070 // fragment for this apex for apexkeys.txt
71 apexKeysPath android.WritablePath
72
Colin Cross6340ea52021-11-04 12:01:18 -070073 // Installed locations of symlinks for backward compatibility.
74 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010075
Colin Cross388c6612025-01-28 14:00:12 -080076 // systemServerDexpreoptInstalls stores the list of dexpreopt artifacts for a system server jar.
77 systemServerDexpreoptInstalls []java.DexpreopterInstall
78
79 // systemServerDexJars stores the list of dexjars for system server jars in the prebuilt for use when
80 // dexpreopting system server jars that are later in the system server classpath.
81 systemServerDexJars android.Paths
Jiyong Park10e926b2020-07-16 21:38:56 +090082}
83
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070084type sanitizedPrebuilt interface {
85 hasSanitizedSource(sanitizer string) bool
86}
87
Paul Duffinef6b6952021-06-15 11:34:01 +010088type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010089 SelectedApexProperties
90
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010091 // Canonical name of this APEX. Used to determine the path to the activated APEX on
92 // device (/apex/<apex_name>). If unspecified, follows the name property.
93 Apex_name *string
94
Spandan Das3576e762024-01-03 18:57:03 +000095 // Name of the source APEX that gets shadowed by this prebuilt
96 // e.g. com.mycompany.android.myapex
97 // If unspecified, follows the naming convention that the source apex of
98 // the prebuilt is Name() without "prebuilt_" prefix
99 Source_apex_name *string
100
Jiyong Park10e926b2020-07-16 21:38:56 +0900101 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +0100102
Paul Duffinef6b6952021-06-15 11:34:01 +0100103 // whether the extracted apex file is installable.
104 Installable *bool
105
106 // optional name for the installed apex. If unspecified, name of the
107 // module is used as the file name
108 Filename *string
109
110 // names of modules to be overridden. Listed modules can only be other binaries
111 // (in Make or Soong).
112 // This does not completely prevent installation of the overridden binaries, but if both
113 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
114 // from PRODUCT_PACKAGES.
115 Overrides []string
116
Paul Duffin3bae0682021-05-05 18:03:47 +0100117 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
118 // bundle will create an APEX variant.
119 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000120
121 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
122 // APEX bundle will create an APEX variant.
123 Exported_systemserverclasspath_fragments []string
Spandan Dasa747d2e2024-03-11 21:37:25 +0000124
125 // Path to the .prebuilt_info file of the prebuilt apex.
126 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used to
127 // generate the prebuilt.
128 Prebuilt_info *string `android:"path"`
Jiyong Park10e926b2020-07-16 21:38:56 +0900129}
130
Paul Duffinef6b6952021-06-15 11:34:01 +0100131// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
132// module that is common to Prebuilt and ApexSet.
133func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
134 p.prebuiltCommonProperties = properties
135 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
136 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
137}
138
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100139func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000140 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
141}
142
143func (p *prebuiltCommon) BaseModuleName() string {
144 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100145}
146
Jiyong Park10e926b2020-07-16 21:38:56 +0900147func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
148 return &p.prebuilt
149}
150
151func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100152 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900153}
154
155func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900156 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900157
158 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
159 // to build the prebuilts themselves.
160 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
161
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700162 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
163 sanitized := ctx.Module().(sanitizedPrebuilt)
164 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
165 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900166
167 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100168 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900169 return true
170 }
171 return false
172}
173
Paul Duffinef6b6952021-06-15 11:34:01 +0100174func (p *prebuiltCommon) InstallFilename() string {
175 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
176}
177
178func (p *prebuiltCommon) Name() string {
179 return p.prebuilt.Name(p.ModuleBase.Name())
180}
181
182func (p *prebuiltCommon) Overrides() []string {
183 return p.prebuiltCommonProperties.Overrides
184}
185
186func (p *prebuiltCommon) installable() bool {
187 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
188}
189
Spandan Das2069c3f2023-12-06 19:40:24 +0000190// To satisfy java.DexpreopterInterface
191func (p *prebuiltCommon) IsInstallable() bool {
192 return p.installable()
193}
194
195// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100196func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000197 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
Colin Cross388c6612025-01-28 14:00:12 -0800198 p.systemServerDexpreoptInstalls = append(p.systemServerDexpreoptInstalls, p.Dexpreopter.ApexSystemServerDexpreoptInstalls()...)
199 p.systemServerDexJars = append(p.systemServerDexJars, p.Dexpreopter.ApexSystemServerDexJars()...)
Spandan Das2069c3f2023-12-06 19:40:24 +0000200}
Paul Duffinc30aea22021-06-15 19:10:11 +0100201
Spandan Das2069c3f2023-12-06 19:40:24 +0000202// 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 +0000203func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext, di *android.DeapexerInfo) {
204 if di == nil {
Spandan Das2069c3f2023-12-06 19:40:24 +0000205 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 Das52c01a12024-09-20 01:09:48 +0000213 // TODO: do not compute twice
Spandan Das2069c3f2023-12-06 19:40:24 +0000214 dc := dexpreopt.GetGlobalConfig(ctx)
215 systemServerJarList := dc.AllApexSystemServerJars(ctx)
216
217 for i := 0; i < systemServerJarList.Len(); i++ {
218 sscpApex := systemServerJarList.Apex(i)
219 sscpJar := systemServerJarList.Jar(i)
220 if apexName != sscpApex {
221 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100222 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000223 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
224 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100225}
226
Colin Cross388c6612025-01-28 14:00:12 -0800227// installApexSystemServerFiles installs dexpreopt files for system server classpath entries
228// provided by the apex. They are installed here instead of in library module because there may be multiple
229// variants of the library, generally one for the "main" apex and another with a different min_sdk_version
230// for the Android Go version of the apex. Both variants would attempt to install to the same locations,
231// and the library variants cannot determine which one should. The apex module is better equipped to determine
232// if it is "selected".
233// This assumes that the jars produced by different min_sdk_version values are identical, which is currently
234// true but may not be true if the min_sdk_version difference between the variants spans version that changed
235// the dex format.
236func (p *prebuiltCommon) installApexSystemServerFiles(ctx android.ModuleContext) {
237 performInstalls := android.IsModulePreferred(ctx.Module())
238
239 for _, install := range p.systemServerDexpreoptInstalls {
240 var installedFile android.InstallPath
241 if performInstalls {
242 installedFile = ctx.InstallFile(install.InstallDirOnDevice, install.InstallFileOnDevice, install.OutputPathOnHost)
243 } else {
244 installedFile = install.InstallDirOnDevice.Join(ctx, install.InstallFileOnDevice)
245 }
246 p.extraInstalledFiles = append(p.extraInstalledFiles, installedFile)
247 p.extraInstalledPairs = append(p.extraInstalledPairs, installPair{install.OutputPathOnHost, installedFile})
248 }
249
250 for _, dexJar := range p.systemServerDexJars {
251 // Copy the system server dex jar to a predefined location where dex2oat will find it.
252 android.CopyFileRule(ctx, dexJar,
253 android.PathForOutput(ctx, dexpreopt.SystemServerDexjarsDir, dexJar.Base()))
254 }
Jiakai Zhang204356f2021-09-09 08:12:46 +0000255}
256
Paul Duffinef6b6952021-06-15 11:34:01 +0100257func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100258 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100259 {
Colin Cross388c6612025-01-28 14:00:12 -0800260 Class: "ETC",
261 OutputFile: android.OptionalPathForPath(p.outputApex),
262 Include: "$(BUILD_PREBUILT)",
Paul Duffinef6b6952021-06-15 11:34:01 +0100263 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
264 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800265 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100266 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700267 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
Colin Cross388c6612025-01-28 14:00:12 -0800268 installPairs := append(installPairs{{p.outputApex, p.installedFile}}, p.extraInstalledPairs...)
269 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", installPairs.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700270 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100271 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
272 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900273 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100274 },
275 },
276 },
277 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100278
Paul Duffinc30aea22021-06-15 19:10:11 +0100279 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100280}
281
Liz Kammer2dc72442023-04-20 10:10:48 -0400282func (p *prebuiltCommon) hasExportedDeps() bool {
Spandan Das1896fd62024-09-18 21:49:14 +0000283 return len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
Liz Kammer2dc72442023-04-20 10:10:48 -0400284 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000285}
286
Paul Duffin57f83592021-05-05 15:09:44 +0100287// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
288func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
289 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100290
Liz Kammer2dc72442023-04-20 10:10:48 -0400291 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
292 prebuiltDep := android.PrebuiltNameFromSource(dep)
293 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
Colin Crossbdd344b2025-01-14 16:01:03 -0800294 ctx.AddDependency(module, fragmentInApexTag, prebuiltDep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400295 }
296
297 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
298 prebuiltDep := android.PrebuiltNameFromSource(dep)
299 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100300 }
Paul Duffindfd33262021-04-06 17:02:08 +0100301}
302
Paul Duffinb17d0442021-05-05 12:07:00 +0100303// Implements android.DepInInSameApex
Colin Crossf7bbd2f2024-12-05 13:57:10 -0800304func (p *prebuiltCommon) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool {
Paul Duffinb17d0442021-05-05 12:07:00 +0100305 _, ok := tag.(exportedDependencyTag)
306 return ok
307}
308
Colin Crossf7bbd2f2024-12-05 13:57:10 -0800309func (p *prebuiltCommon) IncomingDepIsInSameApex(tag blueprint.DependencyTag) bool {
310 return true
311}
312
Paul Duffindfd33262021-04-06 17:02:08 +0100313// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
314// specific variant and checks that they are supported.
315//
316// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
317// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
318//
319// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
320// across prebuilt_apex modules. That is because there is no way to determine whether two
321// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
322// been built from different source at different times or they could have been built with different
323// build options that affect the libraries.
324//
325// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
326// modules were compatible it would be a lot of work and would not provide much benefit for a couple
327// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700328// - The number of prebuilt_apex modules that will be exporting files for the same module will be
329// low as the prebuilt_apex only exports files for the direct dependencies that require it and
330// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
331// few com.android.art* apex files that contain the same contents and could export files for the
332// same modules but only one of them needs to do so. Contrast that with source apex modules which
333// need apex specific variants for every module that contributes code to the apex, whether direct
334// or indirect.
335// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
336// extra copying of files. Contrast that with source apex modules that has to build each variant
337// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100338func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100339 // Collect the list of dependencies.
340 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100341 mctx.WalkDeps(func(child, parent android.Module) bool {
342 // If the child is not in the same apex as the parent then exit immediately and do not visit
343 // any of the child's dependencies.
344 if !android.IsDepInSameApex(mctx, parent, child) {
345 return false
346 }
347
348 tag := mctx.OtherModuleDependencyTag(child)
349 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100350 if exportedTag, ok := tag.(exportedDependencyTag); ok {
351 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100352
353 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100354 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100355 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100356 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100357 }
358
359 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100360 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100361 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100362 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100363 }
Paul Duffindfd33262021-04-06 17:02:08 +0100364 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100365
Paul Duffinfee8cf32021-06-29 18:38:38 +0100366 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
367 // variant.
368 if _, ok := child.(android.ApexModule); !ok {
369 return false
370 }
371
Paul Duffinb17d0442021-05-05 12:07:00 +0100372 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
373 // behavior whether there is a corresponding source module present or not.
374 depName = android.RemoveOptionalPrebuiltPrefix(depName)
375
Paul Duffinb17d0442021-05-05 12:07:00 +0100376 // 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
Colin Crossbb674a12024-11-18 14:24:23 -0800382 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{})
Paul Duffindfd33262021-04-06 17:02:08 +0100383
384 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100385 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100386 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000387 ApexVariationName: apexVariationName,
388 InApexVariants: []string{apexVariationName},
Paul Duffindfd33262021-04-06 17:02:08 +0100389 ForPrebuiltApex: true,
390 }
391
392 // Mark the dependencies of this module as requiring a variant for this module.
393 for _, am := range dependencies {
394 am.BuildForApex(apexInfo)
395 }
396}
397
Jiyong Park09d77522019-11-18 11:16:27 +0900398type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900399 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900400
Paul Duffinbb0dc132021-05-05 16:58:08 +0100401 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900402
Paul Duffinef6b6952021-06-15 11:34:01 +0100403 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700404
Cole Faust4e9f5922024-11-13 16:09:23 -0800405 provenanceMetaDataFile android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900406}
407
Paul Duffin851f3992021-01-13 17:03:51 +0000408type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900409 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000410 //
411 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
412 // for android_common. That is so that it will have the same arch variant as, and so be compatible
413 // with, the source `apex` module type that it replaces.
Cole Faust642e7202024-08-14 17:46:12 -0700414 Src proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park09d77522019-11-18 11:16:27 +0900415 Arch struct {
416 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000417 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900418 }
419 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000420 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900421 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800422 Riscv64 struct {
423 Src *string `android:"path"`
424 }
Jiyong Park09d77522019-11-18 11:16:27 +0900425 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000426 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900427 }
428 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000429 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900430 }
431 }
Paul Duffin851f3992021-01-13 17:03:51 +0000432}
433
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000434// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
435//
436// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
437// to use methods on it that are specific to the current module.
438//
439// See the ApexFileProperties.Src property.
Spandan Dase350e362024-09-21 01:49:34 +0000440func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) string {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000441 multiTargets := prebuilt.MultiTargets()
442 if len(multiTargets) != 1 {
443 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
Spandan Dase350e362024-09-21 01:49:34 +0000444 return ""
Paul Duffin851f3992021-01-13 17:03:51 +0000445 }
446 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000447 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000448 case android.Arm:
449 src = String(p.Arch.Arm.Src)
450 case android.Arm64:
451 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800452 case android.Riscv64:
453 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700454 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
455 if src == "" {
456 src = String(p.Arch.Arm64.Src)
457 }
Paul Duffin851f3992021-01-13 17:03:51 +0000458 case android.X86:
459 src = String(p.Arch.X86.Src)
460 case android.X86_64:
461 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000462 }
463 if src == "" {
Cole Faust642e7202024-08-14 17:46:12 -0700464 src = p.Src.GetOrDefault(ctx, "")
Paul Duffin851f3992021-01-13 17:03:51 +0000465 }
Paul Duffin851f3992021-01-13 17:03:51 +0000466
Paul Duffinc0609c62021-03-01 17:27:16 +0000467 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700468 if ctx.Config().AllowMissingDependencies() {
469 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
470 } else {
471 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
472 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000473 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
474 // logic from reporting a more general, less useful message.
475 }
476
Spandan Dase350e362024-09-21 01:49:34 +0000477 return src
Paul Duffin851f3992021-01-13 17:03:51 +0000478}
479
480type PrebuiltProperties struct {
481 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900482
Paul Duffinef6b6952021-06-15 11:34:01 +0100483 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900484}
485
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700486func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
487 return false
488}
489
Jiyong Park09d77522019-11-18 11:16:27 +0900490// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
491func PrebuiltFactory() android.Module {
492 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100493 module.AddProperties(&module.properties)
Spandan Dase350e362024-09-21 01:49:34 +0000494 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
495
496 // init the module as a prebuilt
497 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
498 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
499 // The actual src will be evaluated in GenerateAndroidBuildActions.
500 android.InitPrebuiltModuleWithoutSrcs(module)
501 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000502
Jiyong Park09d77522019-11-18 11:16:27 +0900503 return module
504}
505
Spandan Das52c01a12024-09-20 01:09:48 +0000506func (p *prebuiltCommon) getDeapexerPropertiesIfNeeded(ctx android.ModuleContext) DeapexerProperties {
Paul Duffin57f83592021-05-05 15:09:44 +0100507 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100508 commonModules := []string{}
Spandan Das2ea84dd2024-01-25 22:12:50 +0000509 dexpreoptProfileGuidedModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100510 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100511 ctx.WalkDeps(func(child, parent android.Module) bool {
512 tag := ctx.OtherModuleDependencyTag(child)
513
Paul Duffin7db57e02021-06-17 14:56:05 +0100514 // If the child is not in the same apex as the parent then ignore it and all its children.
515 if !android.IsDepInSameApex(ctx, parent, child) {
516 return false
517 }
518
Spandan Das161e4682024-01-19 00:22:22 +0000519 name := java.ModuleStemForDeapexing(child)
Paul Duffin7db57e02021-06-17 14:56:05 +0100520 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100521 commonModules = append(commonModules, name)
522
Spandan Das2ea84dd2024-01-25 22:12:50 +0000523 extract := child.(android.RequiredFilesFromPrebuiltApex)
524 requiredFiles := extract.RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100525 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100526
Spandan Das2ea84dd2024-01-25 22:12:50 +0000527 if extract.UseProfileGuidedDexpreopt() {
528 dexpreoptProfileGuidedModules = append(dexpreoptProfileGuidedModules, name)
529 }
530
Paul Duffin7db57e02021-06-17 14:56:05 +0100531 // Visit the dependencies of this module just in case they also require files from the
532 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100533 return true
534 }
535
536 return false
537 })
538
Paul Duffin3bae0682021-05-05 18:03:47 +0100539 // Create properties for deapexer module.
Spandan Das52c01a12024-09-20 01:09:48 +0000540 deapexerProperties := DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100541 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100542 // dependency as well as transitive ones.
Spandan Das2ea84dd2024-01-25 22:12:50 +0000543 CommonModules: android.SortedUniqueStrings(commonModules),
544 DexpreoptProfileGuidedModules: android.SortedUniqueStrings(dexpreoptProfileGuidedModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100545 }
546
547 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100548 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Spandan Das52c01a12024-09-20 01:09:48 +0000549 return deapexerProperties
Paul Duffin11216db2021-03-01 14:14:52 +0000550}
551
Paul Duffin064b70c2020-11-02 17:32:38 +0000552func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
553 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
554 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
555 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
556 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
557 // and not a renamed prebuilt module then that will be detected and reported as an error when
558 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100559 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000560 if ctx.OtherModuleExists(prebuiltName) {
561 name = prebuiltName
562 }
563 return name
564}
565
Paul Duffina7139422021-02-08 11:01:58 +0000566type exportedDependencyTag struct {
567 blueprint.BaseDependencyTag
568 name string
569}
570
571// Mark this tag so dependencies that use it are excluded from visibility enforcement.
572//
573// This does allow any prebuilt_apex to reference any module which does open up a small window for
574// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
575// avoids opening up a much bigger window by widening the visibility of modules that need files
576// provided by the prebuilt_apex to include all the possible locations they may be defined, which
577// could include everything below vendor/.
578//
579// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
580// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
581// the .apex file. It will also have to be included in the module's apex_available property too.
582// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
583// incorrectly.
584func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
585
Paul Duffin7db57e02021-06-17 14:56:05 +0100586func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
587
588var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
589
Paul Duffina7139422021-02-08 11:01:58 +0000590var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000591 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
592 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000593)
594
Paul Duffin57f83592021-05-05 15:09:44 +0100595func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
596 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000597}
598
599var _ ApexInfoMutator = (*Prebuilt)(nil)
600
Paul Duffin064b70c2020-11-02 17:32:38 +0000601func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100602 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900603}
604
Spandan Das52c01a12024-09-20 01:09:48 +0000605// creates the build rules to deapex the prebuilt, and returns a deapexerInfo
Spandan Dase350e362024-09-21 01:49:34 +0000606func (p *prebuiltCommon) getDeapexerInfo(ctx android.ModuleContext, apexFile android.Path) *android.DeapexerInfo {
Spandan Das52c01a12024-09-20 01:09:48 +0000607 if !p.hasExportedDeps() {
608 // nothing to do
609 return nil
610 }
Spandan Das52c01a12024-09-20 01:09:48 +0000611 deapexerProps := p.getDeapexerPropertiesIfNeeded(ctx)
612 return deapex(ctx, apexFile, deapexerProps)
613}
614
Spandan Dasda739a32023-12-13 00:06:32 +0000615// Set a provider containing information about the jars and .prof provided by the apex
616// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
617// Used by dex_bootjars to generate the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000618func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext, di *android.DeapexerInfo) {
619 if di == nil {
Spandan Dasda739a32023-12-13 00:06:32 +0000620 return
621 }
Spandan Das52c01a12024-09-20 01:09:48 +0000622 javaModuleToDexPath := map[string]android.Path{}
623 for _, commonModule := range di.GetExportedModuleNames() {
624 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
625 javaModuleToDexPath[commonModule] = dex
Spandan Das5be63332023-12-13 00:06:32 +0000626 }
Spandan Dasda739a32023-12-13 00:06:32 +0000627 }
Spandan Das52c01a12024-09-20 01:09:48 +0000628
629 exports := android.ApexExportsInfo{
630 ApexName: p.ApexVariationName(),
631 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
632 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
633 }
634 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000635}
636
Spandan Dasa747d2e2024-03-11 21:37:25 +0000637// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
638// with information about whether source or prebuilt of an apex was used during the build.
639func (p *prebuiltCommon) providePrebuiltInfo(ctx android.ModuleContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +0000640 info := android.PrebuiltInfo{
641 Name: p.BaseModuleName(),
Spandan Dasa747d2e2024-03-11 21:37:25 +0000642 Is_prebuilt: true,
643 }
644 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
645 if p.prebuiltCommonProperties.Prebuilt_info != nil {
646 info.Prebuilt_info_file_path = android.PathForModuleSrc(ctx, *p.prebuiltCommonProperties.Prebuilt_info).String()
647 }
Spandan Das3490dfd2024-03-11 21:37:25 +0000648 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
Spandan Dasa747d2e2024-03-11 21:37:25 +0000649}
650
Spandan Das3d0d31a2024-05-03 21:36:48 +0000651// Uses an object provided by its deps to validate that the contents of bcpf have been added to the global
652// PRODUCT_APEX_BOOT_JARS
653// This validation will only run on the apex which is active for this product/release_config
654func validateApexClasspathFragments(ctx android.ModuleContext) {
655 ctx.VisitDirectDeps(func(m android.Module) {
656 if info, exists := android.OtherModuleProvider(ctx, m, java.ClasspathFragmentValidationInfoProvider); exists {
657 ctx.ModuleErrorf("%s in contents of %s must also be declared in PRODUCT_APEX_BOOT_JARS", info.UnknownJars, info.ClasspathFragmentModuleName)
658 }
659 })
660}
661
Jiyong Park09d77522019-11-18 11:16:27 +0900662func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000663 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000664 if !p.IsHideFromMake() {
665 validateApexClasspathFragments(ctx)
666 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000667
Jooyung Han286957d2023-10-30 16:17:56 +0900668 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900669 // TODO(jungjw): Check the key validity.
Spandan Dase350e362024-09-21 01:49:34 +0000670 p.inputApex = android.PathForModuleSrc(ctx, p.properties.prebuiltApexSelector(ctx, ctx.Module()))
Jiyong Park09d77522019-11-18 11:16:27 +0900671 p.installDir = android.PathForModuleInstall(ctx, "apex")
672 p.installFilename = p.InstallFilename()
673 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
674 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
675 }
676 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
677 ctx.Build(pctx, android.BuildParams{
678 Rule: android.Cp,
679 Input: p.inputApex,
680 Output: p.outputApex,
681 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900682
683 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800684 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900685 return
686 }
687
Spandan Dase350e362024-09-21 01:49:34 +0000688 deapexerInfo := p.getDeapexerInfo(ctx, p.inputApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000689
Spandan Das2069c3f2023-12-06 19:40:24 +0000690 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000691 p.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000692
Spandan Dasda739a32023-12-13 00:06:32 +0000693 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000694 p.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000695
Spandan Dasa747d2e2024-03-11 21:37:25 +0000696 p.providePrebuiltInfo(ctx)
697
Paul Duffinc30aea22021-06-15 19:10:11 +0100698 // Save the files that need to be made available to Make.
699 p.initApexFilesForAndroidMk(ctx)
700
Colin Crossccba23d2021-11-12 19:01:29 +0000701 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900702 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000703 // or that prebuilt_apex overrides other apexes (using overrides: prop)
704 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900705 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700706 }
707
708 if p.installable() {
Colin Cross388c6612025-01-28 14:00:12 -0800709 p.installApexSystemServerFiles(ctx)
710 installDeps := slices.Concat(p.compatSymlinks, p.extraInstalledFiles)
711 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, installDeps...)
Wei Li340ee8e2022-03-18 17:33:24 -0700712 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900713 }
mrziwang1587b9c2024-06-13 11:26:04 -0700714
715 ctx.SetOutputFiles(android.Paths{p.outputApex}, "")
Jiyong Park09d77522019-11-18 11:16:27 +0900716}
717
Cole Faust4e9f5922024-11-13 16:09:23 -0800718func (p *Prebuilt) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700719 return p.provenanceMetaDataFile
720}
721
Spandan Das9d6e2092024-09-21 02:50:00 +0000722// extract registers the build actions to extract an apex from .apks file
723// returns the path of the extracted apex
724func extract(ctx android.ModuleContext, apexSet android.Path, prerelease *bool) android.Path {
Paul Duffin76fdd672022-12-12 18:00:47 +0000725 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Spandan Das9d6e2092024-09-21 02:50:00 +0000726 extractedApex := android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000727 // Filter out NativeBridge archs (b/260115309)
728 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100729 ctx.Build(pctx,
730 android.BuildParams{
731 Rule: extractMatchingApex,
732 Description: "Extract an apex from an apex set",
733 Inputs: android.Paths{apexSet},
Spandan Das9d6e2092024-09-21 02:50:00 +0000734 Output: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100735 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000736 "abis": strings.Join(abis, ","),
Spandan Das9d6e2092024-09-21 02:50:00 +0000737 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100738 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700739 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100740 },
Spandan Das9d6e2092024-09-21 02:50:00 +0000741 },
742 )
743 return extractedApex
Paul Duffin24704672021-04-06 16:09:30 +0100744}
745
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700746type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900747 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700748
749 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700750}
751
Paul Duffin24704672021-04-06 16:09:30 +0100752type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700753 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000754 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700755
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700756 Sanitized struct {
757 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000758 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700759 }
760 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000761 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700762 }
763 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000764 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700765 }
766 }
767
Paul Duffin24704672021-04-06 16:09:30 +0100768 // apexes in this set use prerelease SDK version
769 Prerelease *bool
770}
771
772func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
773 var srcs []string
774 if e.Set != nil {
775 srcs = append(srcs, *e.Set)
776 }
777
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900778 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100779
780 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
781 srcs = append(srcs, *e.Sanitized.Address.Set)
782 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
783 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
784 } else if e.Sanitized.None.Set != nil {
785 srcs = append(srcs, *e.Sanitized.None.Set)
786 }
787
788 return srcs
789}
790
791type ApexSetProperties struct {
792 ApexExtractorProperties
793
Paul Duffinef6b6952021-06-15 11:34:01 +0100794 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700795}
796
797func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
798 if sanitizer == "address" {
799 return a.properties.Sanitized.Address.Set != nil
800 }
801 if sanitizer == "hwaddress" {
802 return a.properties.Sanitized.Hwaddress.Set != nil
803 }
804
805 return false
806}
807
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700808// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
809func apexSetFactory() android.Module {
810 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100811 module.AddProperties(&module.properties)
Spandan Das9d6e2092024-09-21 02:50:00 +0000812 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
813
814 // init the module as a prebuilt
815 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
816 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
817 // The actual src will be evaluated in GenerateAndroidBuildActions.
818 android.InitPrebuiltModuleWithoutSrcs(module)
819 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin24704672021-04-06 16:09:30 +0100820
Paul Duffin24704672021-04-06 16:09:30 +0100821 return module
822}
823
Paul Duffin57f83592021-05-05 15:09:44 +0100824func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
825 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100826}
827
828var _ ApexInfoMutator = (*ApexSet)(nil)
829
830func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
831 a.apexInfoMutator(mctx)
832}
833
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700834func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000835 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000836 if !a.IsHideFromMake() {
837 validateApexClasspathFragments(ctx)
838 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000839
Jooyung Han286957d2023-10-30 16:17:56 +0900840 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700841 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100842 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
843 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700844 }
845
Spandan Das9d6e2092024-09-21 02:50:00 +0000846 var apexSet android.Path
847 if srcs := a.properties.prebuiltSrcs(ctx); len(srcs) == 1 {
848 apexSet = android.PathForModuleSrc(ctx, srcs[0])
849 } else {
850 ctx.ModuleErrorf("Expected exactly one source apex_set file, found %v\n", srcs)
851 }
852
853 extractedApex := extract(ctx, apexSet, a.properties.Prerelease)
854
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700855 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Jooyung Han26ec8482024-07-31 15:04:05 +0900856
857 // Build the output APEX. If compression is not enabled, make sure the output is not compressed even if the input is compressed
858 buildRule := android.Cp
859 if !ctx.Config().ApexCompressionEnabled() {
860 buildRule = decompressApex
861 }
Paul Duffin24704672021-04-06 16:09:30 +0100862 ctx.Build(pctx, android.BuildParams{
Jooyung Han26ec8482024-07-31 15:04:05 +0900863 Rule: buildRule,
Spandan Das9d6e2092024-09-21 02:50:00 +0000864 Input: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100865 Output: a.outputApex,
866 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900867
868 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800869 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900870 return
871 }
872
Spandan Das9d6e2092024-09-21 02:50:00 +0000873 deapexerInfo := a.getDeapexerInfo(ctx, extractedApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000874
Spandan Das2069c3f2023-12-06 19:40:24 +0000875 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000876 a.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000877
Spandan Dasda739a32023-12-13 00:06:32 +0000878 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000879 a.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000880
Spandan Dasa747d2e2024-03-11 21:37:25 +0000881 a.providePrebuiltInfo(ctx)
882
Paul Duffinc30aea22021-06-15 19:10:11 +0100883 // Save the files that need to be made available to Make.
884 a.initApexFilesForAndroidMk(ctx)
885
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700886 a.installDir = android.PathForModuleInstall(ctx, "apex")
887 if a.installable() {
Colin Cross388c6612025-01-28 14:00:12 -0800888 a.installApexSystemServerFiles(ctx)
889 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex, a.extraInstalledFiles...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700890 }
891
892 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900893 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700894 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100895 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900896 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700897 }
mrziwang1587b9c2024-06-13 11:26:04 -0700898
899 ctx.SetOutputFiles(android.Paths{a.outputApex}, "")
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700900}