blob: fdd9a75d70bd182e9869bc530858a05cdff3a727 [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 (
Spandan Das37eecea2025-03-19 20:15:23 +000018 "fmt"
Colin Cross388c6612025-01-28 14:00:12 -080019 "slices"
Spandan Das37eecea2025-03-19 20:15:23 +000020 "sort"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070021 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090022 "strings"
23
24 "android/soong/android"
Spandan Das2069c3f2023-12-06 19:40:24 +000025 "android/soong/dexpreopt"
Spandan Dasdad98702025-02-26 14:32:28 +000026 "android/soong/filesystem"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070027 "android/soong/java"
Wei Li340ee8e2022-03-18 17:33:24 -070028 "android/soong/provenance"
Anton Hansson805e0a52022-11-25 14:06:46 +000029
Jaewoong Jungfa00c062020-05-14 14:15:24 -070030 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090031 "github.com/google/blueprint/proptools"
32)
33
Jaewoong Jungfa00c062020-05-14 14:15:24 -070034var (
35 extractMatchingApex = pctx.StaticRule(
36 "extractMatchingApex",
37 blueprint.RuleParams{
38 Command: `rm -rf "$out" && ` +
39 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -070040 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
41 `-screen-densities=all -extract-single ` +
Jaewoong Jungfa00c062020-05-14 14:15:24 -070042 `${in}`,
43 CommandDeps: []string{"${extract_apks}"},
44 },
Pranav Gupta51645ff2023-03-20 16:19:53 -070045 "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
Jooyung Han26ec8482024-07-31 15:04:05 +090046 decompressApex = pctx.StaticRule("decompressApex", blueprint.RuleParams{
Jooyung Han8fa61162024-08-08 10:42:47 +090047 Command: `rm -rf $out && ${deapexer} decompress --copy-if-uncompressed --input ${in} --output ${out}`,
Jooyung Han26ec8482024-07-31 15:04:05 +090048 CommandDeps: []string{"${deapexer}"},
Jooyung Han8fa61162024-08-08 10:42:47 +090049 Description: "decompress $out",
Jooyung Han26ec8482024-07-31 15:04:05 +090050 })
Jaewoong Jungfa00c062020-05-14 14:15:24 -070051)
52
Jiyong Park10e926b2020-07-16 21:38:56 +090053type prebuilt interface {
54 isForceDisabled() bool
55 InstallFilename() string
56}
57
58type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010059 android.ModuleBase
Spandan Das2069c3f2023-12-06 19:40:24 +000060 java.Dexpreopter
Paul Duffinbb0dc132021-05-05 16:58:08 +010061 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010062
Paul Duffinbb0dc132021-05-05 16:58:08 +010063 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010064 prebuiltCommonProperties *PrebuiltCommonProperties
65
Colin Cross388c6612025-01-28 14:00:12 -080066 installDir android.InstallPath
67 installFilename string
68 installedFile android.InstallPath
69 extraInstalledFiles android.InstallPaths
70 extraInstalledPairs installPairs
71 outputApex android.WritablePath
Paul Duffinef6b6952021-06-15 11:34:01 +010072
Jooyung Han286957d2023-10-30 16:17:56 +090073 // fragment for this apex for apexkeys.txt
74 apexKeysPath android.WritablePath
75
Colin Cross6340ea52021-11-04 12:01:18 -070076 // Installed locations of symlinks for backward compatibility.
77 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010078
Colin Cross388c6612025-01-28 14:00:12 -080079 // systemServerDexpreoptInstalls stores the list of dexpreopt artifacts for a system server jar.
80 systemServerDexpreoptInstalls []java.DexpreopterInstall
81
82 // systemServerDexJars stores the list of dexjars for system server jars in the prebuilt for use when
83 // dexpreopting system server jars that are later in the system server classpath.
84 systemServerDexJars android.Paths
Spandan Das37eecea2025-03-19 20:15:23 +000085
86 // Certificate information of any apk packaged inside the prebuilt apex.
87 // This will be nil if the prebuilt apex does not contain any apk.
88 apkCertsFile android.WritablePath
Jiyong Park10e926b2020-07-16 21:38:56 +090089}
90
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070091type sanitizedPrebuilt interface {
92 hasSanitizedSource(sanitizer string) bool
93}
94
Paul Duffinef6b6952021-06-15 11:34:01 +010095type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010096 SelectedApexProperties
97
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010098 // Canonical name of this APEX. Used to determine the path to the activated APEX on
99 // device (/apex/<apex_name>). If unspecified, follows the name property.
100 Apex_name *string
101
Spandan Das3576e762024-01-03 18:57:03 +0000102 // Name of the source APEX that gets shadowed by this prebuilt
103 // e.g. com.mycompany.android.myapex
104 // If unspecified, follows the naming convention that the source apex of
105 // the prebuilt is Name() without "prebuilt_" prefix
106 Source_apex_name *string
107
Jiyong Park10e926b2020-07-16 21:38:56 +0900108 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +0100109
Paul Duffinef6b6952021-06-15 11:34:01 +0100110 // whether the extracted apex file is installable.
111 Installable *bool
112
113 // optional name for the installed apex. If unspecified, name of the
114 // module is used as the file name
115 Filename *string
116
117 // names of modules to be overridden. Listed modules can only be other binaries
118 // (in Make or Soong).
119 // This does not completely prevent installation of the overridden binaries, but if both
120 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
121 // from PRODUCT_PACKAGES.
122 Overrides []string
123
Paul Duffin3bae0682021-05-05 18:03:47 +0100124 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
125 // bundle will create an APEX variant.
126 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000127
128 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
129 // APEX bundle will create an APEX variant.
130 Exported_systemserverclasspath_fragments []string
Spandan Dasa747d2e2024-03-11 21:37:25 +0000131
132 // Path to the .prebuilt_info file of the prebuilt apex.
133 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used to
134 // generate the prebuilt.
135 Prebuilt_info *string `android:"path"`
Jiyong Park10e926b2020-07-16 21:38:56 +0900136}
137
Paul Duffinef6b6952021-06-15 11:34:01 +0100138// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
139// module that is common to Prebuilt and ApexSet.
140func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
141 p.prebuiltCommonProperties = properties
142 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
143 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
144}
145
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100146func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000147 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
148}
149
150func (p *prebuiltCommon) BaseModuleName() string {
151 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100152}
153
Jiyong Park10e926b2020-07-16 21:38:56 +0900154func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
155 return &p.prebuilt
156}
157
158func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100159 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900160}
161
162func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900163 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900164
165 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
166 // to build the prebuilts themselves.
167 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
168
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700169 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
170 sanitized := ctx.Module().(sanitizedPrebuilt)
171 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
172 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900173
174 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100175 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900176 return true
177 }
178 return false
179}
180
Paul Duffinef6b6952021-06-15 11:34:01 +0100181func (p *prebuiltCommon) InstallFilename() string {
182 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
183}
184
185func (p *prebuiltCommon) Name() string {
186 return p.prebuilt.Name(p.ModuleBase.Name())
187}
188
189func (p *prebuiltCommon) Overrides() []string {
190 return p.prebuiltCommonProperties.Overrides
191}
192
193func (p *prebuiltCommon) installable() bool {
194 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
195}
196
Spandan Das2069c3f2023-12-06 19:40:24 +0000197// To satisfy java.DexpreopterInterface
198func (p *prebuiltCommon) IsInstallable() bool {
199 return p.installable()
200}
201
202// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100203func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000204 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
Colin Cross388c6612025-01-28 14:00:12 -0800205 p.systemServerDexpreoptInstalls = append(p.systemServerDexpreoptInstalls, p.Dexpreopter.ApexSystemServerDexpreoptInstalls()...)
206 p.systemServerDexJars = append(p.systemServerDexJars, p.Dexpreopter.ApexSystemServerDexJars()...)
Spandan Das2069c3f2023-12-06 19:40:24 +0000207}
Paul Duffinc30aea22021-06-15 19:10:11 +0100208
Spandan Das2069c3f2023-12-06 19:40:24 +0000209// 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 +0000210func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext, di *android.DeapexerInfo) {
211 if di == nil {
Spandan Das2069c3f2023-12-06 19:40:24 +0000212 return
213 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000214 // If this prebuilt apex has not been selected, return
215 if p.IsHideFromMake() {
216 return
217 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000218 // Use apex_name to determine the api domain of this prebuilt apex
219 apexName := p.ApexVariationName()
Spandan Das52c01a12024-09-20 01:09:48 +0000220 // TODO: do not compute twice
Spandan Das2069c3f2023-12-06 19:40:24 +0000221 dc := dexpreopt.GetGlobalConfig(ctx)
222 systemServerJarList := dc.AllApexSystemServerJars(ctx)
223
224 for i := 0; i < systemServerJarList.Len(); i++ {
225 sscpApex := systemServerJarList.Apex(i)
226 sscpJar := systemServerJarList.Jar(i)
227 if apexName != sscpApex {
228 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100229 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000230 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
231 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100232}
233
Colin Cross388c6612025-01-28 14:00:12 -0800234// installApexSystemServerFiles installs dexpreopt files for system server classpath entries
235// provided by the apex. They are installed here instead of in library module because there may be multiple
236// variants of the library, generally one for the "main" apex and another with a different min_sdk_version
237// for the Android Go version of the apex. Both variants would attempt to install to the same locations,
238// and the library variants cannot determine which one should. The apex module is better equipped to determine
239// if it is "selected".
240// This assumes that the jars produced by different min_sdk_version values are identical, which is currently
241// true but may not be true if the min_sdk_version difference between the variants spans version that changed
242// the dex format.
243func (p *prebuiltCommon) installApexSystemServerFiles(ctx android.ModuleContext) {
244 performInstalls := android.IsModulePreferred(ctx.Module())
245
246 for _, install := range p.systemServerDexpreoptInstalls {
247 var installedFile android.InstallPath
248 if performInstalls {
249 installedFile = ctx.InstallFile(install.InstallDirOnDevice, install.InstallFileOnDevice, install.OutputPathOnHost)
250 } else {
251 installedFile = install.InstallDirOnDevice.Join(ctx, install.InstallFileOnDevice)
252 }
253 p.extraInstalledFiles = append(p.extraInstalledFiles, installedFile)
254 p.extraInstalledPairs = append(p.extraInstalledPairs, installPair{install.OutputPathOnHost, installedFile})
Kiyoung Kima3904c82025-02-24 10:30:42 +0900255 ctx.PackageFile(install.InstallDirOnDevice, install.InstallFileOnDevice, install.OutputPathOnHost)
Colin Cross388c6612025-01-28 14:00:12 -0800256 }
257
258 for _, dexJar := range p.systemServerDexJars {
259 // Copy the system server dex jar to a predefined location where dex2oat will find it.
260 android.CopyFileRule(ctx, dexJar,
261 android.PathForOutput(ctx, dexpreopt.SystemServerDexjarsDir, dexJar.Base()))
262 }
Jiakai Zhang204356f2021-09-09 08:12:46 +0000263}
264
Paul Duffinef6b6952021-06-15 11:34:01 +0100265func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100266 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100267 {
Colin Cross388c6612025-01-28 14:00:12 -0800268 Class: "ETC",
269 OutputFile: android.OptionalPathForPath(p.outputApex),
270 Include: "$(BUILD_PREBUILT)",
Paul Duffinef6b6952021-06-15 11:34:01 +0100271 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
272 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800273 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100274 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700275 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
Colin Cross388c6612025-01-28 14:00:12 -0800276 installPairs := append(installPairs{{p.outputApex, p.installedFile}}, p.extraInstalledPairs...)
277 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", installPairs.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700278 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100279 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
280 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900281 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Spandan Das37eecea2025-03-19 20:15:23 +0000282 if p.apkCertsFile != nil {
283 entries.SetString("LOCAL_APKCERTS_FILE", p.apkCertsFile.String())
284 }
285
Paul Duffinef6b6952021-06-15 11:34:01 +0100286 },
287 },
288 },
289 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100290
Paul Duffinc30aea22021-06-15 19:10:11 +0100291 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100292}
293
Liz Kammer2dc72442023-04-20 10:10:48 -0400294func (p *prebuiltCommon) hasExportedDeps() bool {
Spandan Das1896fd62024-09-18 21:49:14 +0000295 return len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
Liz Kammer2dc72442023-04-20 10:10:48 -0400296 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000297}
298
Spandan Das37eecea2025-03-19 20:15:23 +0000299type appInPrebuiltApexDepTag struct {
300 blueprint.BaseDependencyTag
301}
302
303func (appInPrebuiltApexDepTag) ExcludeFromVisibilityEnforcement() {}
304
305var appInPrebuiltApexTag = appInPrebuiltApexDepTag{}
306
Paul Duffin57f83592021-05-05 15:09:44 +0100307// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
308func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
309 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100310
Liz Kammer2dc72442023-04-20 10:10:48 -0400311 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
312 prebuiltDep := android.PrebuiltNameFromSource(dep)
313 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
Colin Crossbdd344b2025-01-14 16:01:03 -0800314 ctx.AddDependency(module, fragmentInApexTag, prebuiltDep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400315 }
316
317 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
318 prebuiltDep := android.PrebuiltNameFromSource(dep)
319 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100320 }
Paul Duffindfd33262021-04-06 17:02:08 +0100321}
322
Paul Duffinb17d0442021-05-05 12:07:00 +0100323// Implements android.DepInInSameApex
Yu Liuf1806032025-02-07 00:23:34 +0000324func (m *prebuiltCommon) GetDepInSameApexChecker() android.DepInSameApexChecker {
325 return ApexPrebuiltDepInSameApexChecker{}
Paul Duffinb17d0442021-05-05 12:07:00 +0100326}
327
Yu Liuf1806032025-02-07 00:23:34 +0000328type ApexPrebuiltDepInSameApexChecker struct {
329 android.BaseDepInSameApexChecker
330}
331
332func (m ApexPrebuiltDepInSameApexChecker) OutgoingDepIsInSameApex(tag blueprint.DependencyTag) bool {
333 _, ok := tag.(exportedDependencyTag)
334 return ok
Colin Crossf7bbd2f2024-12-05 13:57:10 -0800335}
336
Colin Cross1cea5302024-12-03 16:40:08 -0800337func (p *prebuiltCommon) checkExportedDependenciesArePrebuilts(ctx android.ModuleContext) {
338 ctx.VisitDirectDeps(func(dep android.Module) {
339 tag := ctx.OtherModuleDependencyTag(dep)
340 depName := ctx.OtherModuleName(dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100341 if exportedTag, ok := tag.(exportedDependencyTag); ok {
342 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100343
344 // It is an error if the other module is not a prebuilt.
Colin Cross1cea5302024-12-03 16:40:08 -0800345 if !android.IsModulePrebuilt(dep) {
346 ctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffindfd33262021-04-06 17:02:08 +0100347 }
348
349 // It is an error if the other module is not an ApexModule.
Colin Cross1cea5302024-12-03 16:40:08 -0800350 if _, ok := dep.(android.ApexModule); !ok {
351 ctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffindfd33262021-04-06 17:02:08 +0100352 }
Paul Duffindfd33262021-04-06 17:02:08 +0100353 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100354
Paul Duffindfd33262021-04-06 17:02:08 +0100355 })
Colin Cross1cea5302024-12-03 16:40:08 -0800356}
Paul Duffindfd33262021-04-06 17:02:08 +0100357
Colin Cross1cea5302024-12-03 16:40:08 -0800358// generateApexInfo returns an android.ApexInfo configuration suitable for dependencies of this apex.
359func (p *prebuiltCommon) generateApexInfo(ctx generateApexInfoContext) android.ApexInfo {
360 return android.ApexInfo{
361 ApexVariationName: "prebuilt_" + p.ApexVariationName(),
362 BaseApexName: p.ApexVariationName(),
Paul Duffindfd33262021-04-06 17:02:08 +0100363 ForPrebuiltApex: true,
364 }
Paul Duffindfd33262021-04-06 17:02:08 +0100365}
366
Jiyong Park09d77522019-11-18 11:16:27 +0900367type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900368 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900369
Paul Duffinbb0dc132021-05-05 16:58:08 +0100370 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900371
Paul Duffinef6b6952021-06-15 11:34:01 +0100372 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700373
Cole Faust4e9f5922024-11-13 16:09:23 -0800374 provenanceMetaDataFile android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900375}
376
Paul Duffin851f3992021-01-13 17:03:51 +0000377type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900378 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000379 //
380 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
381 // for android_common. That is so that it will have the same arch variant as, and so be compatible
382 // with, the source `apex` module type that it replaces.
Cole Faust642e7202024-08-14 17:46:12 -0700383 Src proptools.Configurable[string] `android:"path,replace_instead_of_append"`
Jiyong Park09d77522019-11-18 11:16:27 +0900384 Arch struct {
385 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000386 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900387 }
388 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000389 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900390 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800391 Riscv64 struct {
392 Src *string `android:"path"`
393 }
Jiyong Park09d77522019-11-18 11:16:27 +0900394 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000395 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900396 }
397 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000398 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900399 }
400 }
Paul Duffin851f3992021-01-13 17:03:51 +0000401}
402
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000403// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
404//
405// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
406// to use methods on it that are specific to the current module.
407//
408// See the ApexFileProperties.Src property.
Spandan Dase350e362024-09-21 01:49:34 +0000409func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) string {
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000410 multiTargets := prebuilt.MultiTargets()
411 if len(multiTargets) != 1 {
412 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
Spandan Dase350e362024-09-21 01:49:34 +0000413 return ""
Paul Duffin851f3992021-01-13 17:03:51 +0000414 }
415 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000416 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000417 case android.Arm:
418 src = String(p.Arch.Arm.Src)
419 case android.Arm64:
420 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800421 case android.Riscv64:
422 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700423 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
424 if src == "" {
425 src = String(p.Arch.Arm64.Src)
426 }
Paul Duffin851f3992021-01-13 17:03:51 +0000427 case android.X86:
428 src = String(p.Arch.X86.Src)
429 case android.X86_64:
430 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000431 }
432 if src == "" {
Cole Faust642e7202024-08-14 17:46:12 -0700433 src = p.Src.GetOrDefault(ctx, "")
Paul Duffin851f3992021-01-13 17:03:51 +0000434 }
Paul Duffin851f3992021-01-13 17:03:51 +0000435
Paul Duffinc0609c62021-03-01 17:27:16 +0000436 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700437 if ctx.Config().AllowMissingDependencies() {
438 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
439 } else {
440 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
441 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000442 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
443 // logic from reporting a more general, less useful message.
444 }
445
Spandan Dase350e362024-09-21 01:49:34 +0000446 return src
Paul Duffin851f3992021-01-13 17:03:51 +0000447}
448
449type PrebuiltProperties struct {
450 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900451
Paul Duffinef6b6952021-06-15 11:34:01 +0100452 PrebuiltCommonProperties
Spandan Das37eecea2025-03-19 20:15:23 +0000453
454 // List of apps that are bundled inside this prebuilt apex.
455 // This will be used to create the certificate info of those apps for apkcerts.txt
456 // This dependency will only be used for apkcerts.txt processing.
457 // Notably, building the prebuilt apex will not build the source app.
458 Apps []string
Jiyong Park09d77522019-11-18 11:16:27 +0900459}
460
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700461func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
462 return false
463}
464
Jiyong Park09d77522019-11-18 11:16:27 +0900465// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
466func PrebuiltFactory() android.Module {
467 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100468 module.AddProperties(&module.properties)
Spandan Dase350e362024-09-21 01:49:34 +0000469 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
470
471 // init the module as a prebuilt
472 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
473 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
474 // The actual src will be evaluated in GenerateAndroidBuildActions.
475 android.InitPrebuiltModuleWithoutSrcs(module)
476 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin064b70c2020-11-02 17:32:38 +0000477
Jiyong Park09d77522019-11-18 11:16:27 +0900478 return module
479}
480
Spandan Das52c01a12024-09-20 01:09:48 +0000481func (p *prebuiltCommon) getDeapexerPropertiesIfNeeded(ctx android.ModuleContext) DeapexerProperties {
Paul Duffin57f83592021-05-05 15:09:44 +0100482 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100483 commonModules := []string{}
Spandan Das2ea84dd2024-01-25 22:12:50 +0000484 dexpreoptProfileGuidedModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100485 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100486 ctx.WalkDeps(func(child, parent android.Module) bool {
487 tag := ctx.OtherModuleDependencyTag(child)
488
Paul Duffin7db57e02021-06-17 14:56:05 +0100489 // If the child is not in the same apex as the parent then ignore it and all its children.
490 if !android.IsDepInSameApex(ctx, parent, child) {
491 return false
492 }
493
Spandan Das161e4682024-01-19 00:22:22 +0000494 name := java.ModuleStemForDeapexing(child)
Paul Duffin7db57e02021-06-17 14:56:05 +0100495 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100496 commonModules = append(commonModules, name)
497
Spandan Das2ea84dd2024-01-25 22:12:50 +0000498 extract := child.(android.RequiredFilesFromPrebuiltApex)
499 requiredFiles := extract.RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100500 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100501
Spandan Das2ea84dd2024-01-25 22:12:50 +0000502 if extract.UseProfileGuidedDexpreopt() {
503 dexpreoptProfileGuidedModules = append(dexpreoptProfileGuidedModules, name)
504 }
505
Paul Duffin7db57e02021-06-17 14:56:05 +0100506 // Visit the dependencies of this module just in case they also require files from the
507 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100508 return true
509 }
510
511 return false
512 })
513
Paul Duffin3bae0682021-05-05 18:03:47 +0100514 // Create properties for deapexer module.
Spandan Das52c01a12024-09-20 01:09:48 +0000515 deapexerProperties := DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100516 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100517 // dependency as well as transitive ones.
Spandan Das2ea84dd2024-01-25 22:12:50 +0000518 CommonModules: android.SortedUniqueStrings(commonModules),
519 DexpreoptProfileGuidedModules: android.SortedUniqueStrings(dexpreoptProfileGuidedModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100520 }
521
522 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100523 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Spandan Das52c01a12024-09-20 01:09:48 +0000524 return deapexerProperties
Paul Duffin11216db2021-03-01 14:14:52 +0000525}
526
Paul Duffin064b70c2020-11-02 17:32:38 +0000527func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
528 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
529 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
530 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
531 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
532 // and not a renamed prebuilt module then that will be detected and reported as an error when
533 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100534 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000535 if ctx.OtherModuleExists(prebuiltName) {
536 name = prebuiltName
537 }
538 return name
539}
540
Paul Duffina7139422021-02-08 11:01:58 +0000541type exportedDependencyTag struct {
542 blueprint.BaseDependencyTag
543 name string
544}
545
546// Mark this tag so dependencies that use it are excluded from visibility enforcement.
547//
548// This does allow any prebuilt_apex to reference any module which does open up a small window for
549// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
550// avoids opening up a much bigger window by widening the visibility of modules that need files
551// provided by the prebuilt_apex to include all the possible locations they may be defined, which
552// could include everything below vendor/.
553//
554// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
555// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
556// the .apex file. It will also have to be included in the module's apex_available property too.
557// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
558// incorrectly.
559func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
560
Paul Duffin7db57e02021-06-17 14:56:05 +0100561func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
562
563var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
564
Paul Duffina7139422021-02-08 11:01:58 +0000565var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000566 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
567 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000568)
569
Paul Duffin57f83592021-05-05 15:09:44 +0100570func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
571 p.prebuiltApexContentsDeps(ctx)
Spandan Das37eecea2025-03-19 20:15:23 +0000572 for _, app := range p.properties.Apps {
573 ctx.AddDependency(p, appInPrebuiltApexTag, app)
574 }
Paul Duffin064b70c2020-11-02 17:32:38 +0000575}
576
Colin Cross1cea5302024-12-03 16:40:08 -0800577var _ ApexTransitionMutator = (*Prebuilt)(nil)
Paul Duffin064b70c2020-11-02 17:32:38 +0000578
Colin Cross1cea5302024-12-03 16:40:08 -0800579func (p *Prebuilt) ApexTransitionMutatorSplit(ctx android.BaseModuleContext) []android.ApexInfo {
580 return []android.ApexInfo{p.generateApexInfo(ctx)}
581}
582
583func (p *Prebuilt) ApexTransitionMutatorOutgoing(ctx android.OutgoingTransitionContext, sourceInfo android.ApexInfo) android.ApexInfo {
584 return sourceInfo
585}
586
587func (p *Prebuilt) ApexTransitionMutatorIncoming(ctx android.IncomingTransitionContext, outgoingInfo android.ApexInfo) android.ApexInfo {
588 return p.generateApexInfo(ctx)
589}
590
591func (p *Prebuilt) ApexTransitionMutatorMutate(ctx android.BottomUpMutatorContext, info android.ApexInfo) {
592 android.SetProvider(ctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{})
Jiyong Park09d77522019-11-18 11:16:27 +0900593}
594
Spandan Das52c01a12024-09-20 01:09:48 +0000595// creates the build rules to deapex the prebuilt, and returns a deapexerInfo
Spandan Dase350e362024-09-21 01:49:34 +0000596func (p *prebuiltCommon) getDeapexerInfo(ctx android.ModuleContext, apexFile android.Path) *android.DeapexerInfo {
Spandan Das52c01a12024-09-20 01:09:48 +0000597 if !p.hasExportedDeps() {
598 // nothing to do
599 return nil
600 }
Spandan Das52c01a12024-09-20 01:09:48 +0000601 deapexerProps := p.getDeapexerPropertiesIfNeeded(ctx)
602 return deapex(ctx, apexFile, deapexerProps)
603}
604
Spandan Dasda739a32023-12-13 00:06:32 +0000605// Set a provider containing information about the jars and .prof provided by the apex
606// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
607// Used by dex_bootjars to generate the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000608func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext, di *android.DeapexerInfo) {
609 if di == nil {
Spandan Dasda739a32023-12-13 00:06:32 +0000610 return
611 }
Spandan Das52c01a12024-09-20 01:09:48 +0000612 javaModuleToDexPath := map[string]android.Path{}
613 for _, commonModule := range di.GetExportedModuleNames() {
614 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
615 javaModuleToDexPath[commonModule] = dex
Spandan Das5be63332023-12-13 00:06:32 +0000616 }
Spandan Dasda739a32023-12-13 00:06:32 +0000617 }
Spandan Das52c01a12024-09-20 01:09:48 +0000618
619 exports := android.ApexExportsInfo{
620 ApexName: p.ApexVariationName(),
621 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
622 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
623 }
624 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000625}
626
Spandan Dasa747d2e2024-03-11 21:37:25 +0000627// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
628// with information about whether source or prebuilt of an apex was used during the build.
629func (p *prebuiltCommon) providePrebuiltInfo(ctx android.ModuleContext) {
Spandan Das3490dfd2024-03-11 21:37:25 +0000630 info := android.PrebuiltInfo{
631 Name: p.BaseModuleName(),
Spandan Dasa747d2e2024-03-11 21:37:25 +0000632 Is_prebuilt: true,
633 }
634 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
635 if p.prebuiltCommonProperties.Prebuilt_info != nil {
636 info.Prebuilt_info_file_path = android.PathForModuleSrc(ctx, *p.prebuiltCommonProperties.Prebuilt_info).String()
637 }
Spandan Das3490dfd2024-03-11 21:37:25 +0000638 android.SetProvider(ctx, android.PrebuiltInfoProvider, info)
Spandan Dasa747d2e2024-03-11 21:37:25 +0000639}
640
Spandan Das3d0d31a2024-05-03 21:36:48 +0000641// Uses an object provided by its deps to validate that the contents of bcpf have been added to the global
642// PRODUCT_APEX_BOOT_JARS
643// This validation will only run on the apex which is active for this product/release_config
644func validateApexClasspathFragments(ctx android.ModuleContext) {
645 ctx.VisitDirectDeps(func(m android.Module) {
646 if info, exists := android.OtherModuleProvider(ctx, m, java.ClasspathFragmentValidationInfoProvider); exists {
647 ctx.ModuleErrorf("%s in contents of %s must also be declared in PRODUCT_APEX_BOOT_JARS", info.UnknownJars, info.ClasspathFragmentModuleName)
648 }
649 })
650}
651
Jiyong Park09d77522019-11-18 11:16:27 +0900652func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000653 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000654 if !p.IsHideFromMake() {
655 validateApexClasspathFragments(ctx)
656 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000657
Colin Cross1cea5302024-12-03 16:40:08 -0800658 p.checkExportedDependenciesArePrebuilts(ctx)
659
Jooyung Han286957d2023-10-30 16:17:56 +0900660 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900661 // TODO(jungjw): Check the key validity.
Spandan Dase350e362024-09-21 01:49:34 +0000662 p.inputApex = android.PathForModuleSrc(ctx, p.properties.prebuiltApexSelector(ctx, ctx.Module()))
Jiyong Park09d77522019-11-18 11:16:27 +0900663 p.installDir = android.PathForModuleInstall(ctx, "apex")
664 p.installFilename = p.InstallFilename()
665 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
666 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
667 }
668 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
669 ctx.Build(pctx, android.BuildParams{
670 Rule: android.Cp,
671 Input: p.inputApex,
672 Output: p.outputApex,
673 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900674
675 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800676 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900677 return
678 }
679
Spandan Dase350e362024-09-21 01:49:34 +0000680 deapexerInfo := p.getDeapexerInfo(ctx, p.inputApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000681
Spandan Das2069c3f2023-12-06 19:40:24 +0000682 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000683 p.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000684
Spandan Dasda739a32023-12-13 00:06:32 +0000685 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000686 p.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000687
Spandan Dasa747d2e2024-03-11 21:37:25 +0000688 p.providePrebuiltInfo(ctx)
689
Paul Duffinc30aea22021-06-15 19:10:11 +0100690 // Save the files that need to be made available to Make.
691 p.initApexFilesForAndroidMk(ctx)
692
Colin Crossccba23d2021-11-12 19:01:29 +0000693 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900694 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000695 // or that prebuilt_apex overrides other apexes (using overrides: prop)
696 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900697 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700698 }
699
700 if p.installable() {
Colin Cross388c6612025-01-28 14:00:12 -0800701 p.installApexSystemServerFiles(ctx)
702 installDeps := slices.Concat(p.compatSymlinks, p.extraInstalledFiles)
703 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, installDeps...)
Wei Li340ee8e2022-03-18 17:33:24 -0700704 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900705 }
mrziwang1587b9c2024-06-13 11:26:04 -0700706
Spandan Das37eecea2025-03-19 20:15:23 +0000707 p.addApkCertsInfo(ctx)
708
mrziwang1587b9c2024-06-13 11:26:04 -0700709 ctx.SetOutputFiles(android.Paths{p.outputApex}, "")
Spandan Dasdad98702025-02-26 14:32:28 +0000710
711 android.SetProvider(ctx, filesystem.ApexKeyPathInfoProvider, filesystem.ApexKeyPathInfo{p.apexKeysPath})
Jiyong Park09d77522019-11-18 11:16:27 +0900712}
713
Spandan Das37eecea2025-03-19 20:15:23 +0000714// `addApkCertsInfo` sets a provider that will be used to create apkcerts.txt
715func (p *Prebuilt) addApkCertsInfo(ctx android.ModuleContext) {
716 formatLine := func(cert java.Certificate, name, partition string) string {
717 pem := cert.AndroidMkString()
718 var key string
719 if cert.Key == nil {
720 key = ""
721 } else {
722 key = cert.Key.String()
723 }
724 return fmt.Sprintf(`name="%s" certificate="%s" private_key="%s" partition="%s"`, name, pem, key, partition)
725 }
726
727 // Determine if this prebuilt_apex contains any .apks
728 var appInfos java.AppInfos
729 ctx.VisitDirectDepsProxyWithTag(appInPrebuiltApexTag, func(app android.ModuleProxy) {
730 if appInfo, ok := android.OtherModuleProvider(ctx, app, java.AppInfoProvider); ok {
731 appInfos = append(appInfos, *appInfo)
732 } else {
733 ctx.ModuleErrorf("App %s does not set AppInfoProvider\n", app.Name())
734 }
735 })
736 sort.Slice(appInfos, func(i, j int) bool {
737 return appInfos[i].InstallApkName < appInfos[j].InstallApkName
738 })
739
740 if len(appInfos) == 0 {
741 return
742 }
743
744 // Set a provider for use by `android_device`.
745 // `android_device` will create an apkcerts.txt with the list of installed apps for that device.
746 android.SetProvider(ctx, java.AppInfosProvider, appInfos)
747
748 // Set a Make variable for legacy apkcerts.txt creation
749 // p.apkCertsFile will become `LOCAL_APKCERTS_FILE`
750 var lines []string
751 for _, appInfo := range appInfos {
752 lines = append(lines, formatLine(appInfo.Certificate, appInfo.InstallApkName+".apk", p.PartitionTag(ctx.DeviceConfig())))
753 }
754 if len(lines) > 0 {
755 p.apkCertsFile = android.PathForModuleOut(ctx, "apkcerts.txt")
756 android.WriteFileRule(ctx, p.apkCertsFile, strings.Join(lines, "\n"))
757 }
758}
759
Cole Faust4e9f5922024-11-13 16:09:23 -0800760func (p *Prebuilt) ProvenanceMetaDataFile() android.Path {
Wei Li340ee8e2022-03-18 17:33:24 -0700761 return p.provenanceMetaDataFile
762}
763
Spandan Das9d6e2092024-09-21 02:50:00 +0000764// extract registers the build actions to extract an apex from .apks file
765// returns the path of the extracted apex
766func extract(ctx android.ModuleContext, apexSet android.Path, prerelease *bool) android.Path {
Paul Duffin76fdd672022-12-12 18:00:47 +0000767 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Spandan Das9d6e2092024-09-21 02:50:00 +0000768 extractedApex := android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000769 // Filter out NativeBridge archs (b/260115309)
770 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100771 ctx.Build(pctx,
772 android.BuildParams{
773 Rule: extractMatchingApex,
774 Description: "Extract an apex from an apex set",
775 Inputs: android.Paths{apexSet},
Spandan Das9d6e2092024-09-21 02:50:00 +0000776 Output: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100777 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000778 "abis": strings.Join(abis, ","),
Spandan Das9d6e2092024-09-21 02:50:00 +0000779 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100780 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700781 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100782 },
Spandan Das9d6e2092024-09-21 02:50:00 +0000783 },
784 )
785 return extractedApex
Paul Duffin24704672021-04-06 16:09:30 +0100786}
787
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700788type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900789 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700790
791 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700792}
793
Paul Duffin24704672021-04-06 16:09:30 +0100794type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700795 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000796 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700797
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700798 Sanitized struct {
799 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000800 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700801 }
802 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000803 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700804 }
805 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000806 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700807 }
808 }
809
Paul Duffin24704672021-04-06 16:09:30 +0100810 // apexes in this set use prerelease SDK version
811 Prerelease *bool
812}
813
814func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
815 var srcs []string
816 if e.Set != nil {
817 srcs = append(srcs, *e.Set)
818 }
819
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900820 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100821
822 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
823 srcs = append(srcs, *e.Sanitized.Address.Set)
824 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
825 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
826 } else if e.Sanitized.None.Set != nil {
827 srcs = append(srcs, *e.Sanitized.None.Set)
828 }
829
830 return srcs
831}
832
833type ApexSetProperties struct {
834 ApexExtractorProperties
835
Paul Duffinef6b6952021-06-15 11:34:01 +0100836 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700837}
838
839func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
840 if sanitizer == "address" {
841 return a.properties.Sanitized.Address.Set != nil
842 }
843 if sanitizer == "hwaddress" {
844 return a.properties.Sanitized.Hwaddress.Set != nil
845 }
846
847 return false
848}
849
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700850// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
851func apexSetFactory() android.Module {
852 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100853 module.AddProperties(&module.properties)
Spandan Das9d6e2092024-09-21 02:50:00 +0000854 module.prebuiltCommon.prebuiltCommonProperties = &module.properties.PrebuiltCommonProperties
855
856 // init the module as a prebuilt
857 // even though this module type has srcs, use `InitPrebuiltModuleWithoutSrcs`, since the existing
858 // InitPrebuiltModule* are not friendly with Sources of Configurable type.
859 // The actual src will be evaluated in GenerateAndroidBuildActions.
860 android.InitPrebuiltModuleWithoutSrcs(module)
861 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
Paul Duffin24704672021-04-06 16:09:30 +0100862
Paul Duffin24704672021-04-06 16:09:30 +0100863 return module
864}
865
Paul Duffin57f83592021-05-05 15:09:44 +0100866func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
867 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100868}
869
Colin Cross1cea5302024-12-03 16:40:08 -0800870var _ ApexTransitionMutator = (*ApexSet)(nil)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100871
Colin Cross1cea5302024-12-03 16:40:08 -0800872func (a *ApexSet) ApexTransitionMutatorSplit(ctx android.BaseModuleContext) []android.ApexInfo {
873 return []android.ApexInfo{a.generateApexInfo(ctx)}
874}
875
876func (a *ApexSet) ApexTransitionMutatorOutgoing(ctx android.OutgoingTransitionContext, sourceInfo android.ApexInfo) android.ApexInfo {
877 return sourceInfo
878}
879
880func (a *ApexSet) ApexTransitionMutatorIncoming(ctx android.IncomingTransitionContext, outgoingInfo android.ApexInfo) android.ApexInfo {
881 return a.generateApexInfo(ctx)
882}
883
884func (a *ApexSet) ApexTransitionMutatorMutate(ctx android.BottomUpMutatorContext, info android.ApexInfo) {
885 android.SetProvider(ctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{})
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100886}
887
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700888func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Spandan Das3d0d31a2024-05-03 21:36:48 +0000889 // Validate contents of classpath fragments
Spandan Das5f1f9402024-05-21 18:59:23 +0000890 if !a.IsHideFromMake() {
891 validateApexClasspathFragments(ctx)
892 }
Spandan Das3d0d31a2024-05-03 21:36:48 +0000893
Jooyung Han286957d2023-10-30 16:17:56 +0900894 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700895 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100896 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
897 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700898 }
899
Spandan Das9d6e2092024-09-21 02:50:00 +0000900 var apexSet android.Path
901 if srcs := a.properties.prebuiltSrcs(ctx); len(srcs) == 1 {
902 apexSet = android.PathForModuleSrc(ctx, srcs[0])
903 } else {
904 ctx.ModuleErrorf("Expected exactly one source apex_set file, found %v\n", srcs)
905 }
906
907 extractedApex := extract(ctx, apexSet, a.properties.Prerelease)
908
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700909 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Jooyung Han26ec8482024-07-31 15:04:05 +0900910
911 // Build the output APEX. If compression is not enabled, make sure the output is not compressed even if the input is compressed
912 buildRule := android.Cp
913 if !ctx.Config().ApexCompressionEnabled() {
914 buildRule = decompressApex
915 }
Paul Duffin24704672021-04-06 16:09:30 +0100916 ctx.Build(pctx, android.BuildParams{
Jooyung Han26ec8482024-07-31 15:04:05 +0900917 Rule: buildRule,
Spandan Das9d6e2092024-09-21 02:50:00 +0000918 Input: extractedApex,
Paul Duffin24704672021-04-06 16:09:30 +0100919 Output: a.outputApex,
920 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900921
922 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800923 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900924 return
925 }
926
Spandan Das9d6e2092024-09-21 02:50:00 +0000927 deapexerInfo := a.getDeapexerInfo(ctx, extractedApex)
Spandan Das52c01a12024-09-20 01:09:48 +0000928
Spandan Das2069c3f2023-12-06 19:40:24 +0000929 // dexpreopt any system server jars if present
Spandan Das52c01a12024-09-20 01:09:48 +0000930 a.dexpreoptSystemServerJars(ctx, deapexerInfo)
Spandan Das2069c3f2023-12-06 19:40:24 +0000931
Spandan Dasda739a32023-12-13 00:06:32 +0000932 // provide info used for generating the boot image
Spandan Das52c01a12024-09-20 01:09:48 +0000933 a.provideApexExportsInfo(ctx, deapexerInfo)
Spandan Dasda739a32023-12-13 00:06:32 +0000934
Spandan Dasa747d2e2024-03-11 21:37:25 +0000935 a.providePrebuiltInfo(ctx)
936
Paul Duffinc30aea22021-06-15 19:10:11 +0100937 // Save the files that need to be made available to Make.
938 a.initApexFilesForAndroidMk(ctx)
939
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700940 a.installDir = android.PathForModuleInstall(ctx, "apex")
941 if a.installable() {
Colin Cross388c6612025-01-28 14:00:12 -0800942 a.installApexSystemServerFiles(ctx)
943 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex, a.extraInstalledFiles...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700944 }
945
946 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900947 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700948 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100949 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900950 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700951 }
mrziwang1587b9c2024-06-13 11:26:04 -0700952
953 ctx.SetOutputFiles(android.Paths{a.outputApex}, "")
Spandan Dasdad98702025-02-26 14:32:28 +0000954
955 android.SetProvider(ctx, filesystem.ApexKeyPathInfoProvider, filesystem.ApexKeyPathInfo{a.apexKeysPath})
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700956}