blob: 551942d313837453efc33ef284bbcdb0d45d68a8 [file] [log] [blame]
Jiyong Park09d77522019-11-18 11:16:27 +09001// Copyright (C) 2019 The Android Open Source Project
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package apex
16
17import (
18 "fmt"
Paul Duffinc30aea22021-06-15 19:10:11 +010019 "io"
Colin Cross6340ea52021-11-04 12:01:18 -070020 "path/filepath"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070021 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090022 "strings"
23
24 "android/soong/android"
Spandan Das2069c3f2023-12-06 19:40:24 +000025 "android/soong/dexpreopt"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070026 "android/soong/java"
Wei Li340ee8e2022-03-18 17:33:24 -070027 "android/soong/provenance"
Anton Hansson805e0a52022-11-25 14:06:46 +000028
Jaewoong Jungfa00c062020-05-14 14:15:24 -070029 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090030 "github.com/google/blueprint/proptools"
31)
32
Jaewoong Jungfa00c062020-05-14 14:15:24 -070033var (
34 extractMatchingApex = pctx.StaticRule(
35 "extractMatchingApex",
36 blueprint.RuleParams{
37 Command: `rm -rf "$out" && ` +
38 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -070039 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
40 `-screen-densities=all -extract-single ` +
Jaewoong Jungfa00c062020-05-14 14:15:24 -070041 `${in}`,
42 CommandDeps: []string{"${extract_apks}"},
43 },
Pranav Gupta51645ff2023-03-20 16:19:53 -070044 "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
Jaewoong Jungfa00c062020-05-14 14:15:24 -070045)
46
Jiyong Park10e926b2020-07-16 21:38:56 +090047type prebuilt interface {
48 isForceDisabled() bool
49 InstallFilename() string
50}
51
52type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010053 android.ModuleBase
Spandan Das2069c3f2023-12-06 19:40:24 +000054 java.Dexpreopter
Paul Duffinbb0dc132021-05-05 16:58:08 +010055 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010056
Paul Duffinbb0dc132021-05-05 16:58:08 +010057 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010058 prebuiltCommonProperties *PrebuiltCommonProperties
59
60 installDir android.InstallPath
61 installFilename string
Colin Cross6340ea52021-11-04 12:01:18 -070062 installedFile android.InstallPath
Paul Duffinef6b6952021-06-15 11:34:01 +010063 outputApex android.WritablePath
64
Jooyung Han286957d2023-10-30 16:17:56 +090065 // fragment for this apex for apexkeys.txt
66 apexKeysPath android.WritablePath
67
Paul Duffinc30aea22021-06-15 19:10:11 +010068 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
69 // to create make modules in prebuiltCommon.AndroidMkEntries.
70 apexFilesForAndroidMk []apexFile
71
Colin Cross6340ea52021-11-04 12:01:18 -070072 // Installed locations of symlinks for backward compatibility.
73 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010074
Jiakai Zhange6e90db2022-01-28 14:58:56 +000075 hostRequired []string
76 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090077}
78
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070079type sanitizedPrebuilt interface {
80 hasSanitizedSource(sanitizer string) bool
81}
82
Paul Duffinef6b6952021-06-15 11:34:01 +010083type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010084 SelectedApexProperties
85
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010086 // Canonical name of this APEX. Used to determine the path to the activated APEX on
87 // device (/apex/<apex_name>). If unspecified, follows the name property.
88 Apex_name *string
89
Spandan Das3576e762024-01-03 18:57:03 +000090 // Name of the source APEX that gets shadowed by this prebuilt
91 // e.g. com.mycompany.android.myapex
92 // If unspecified, follows the naming convention that the source apex of
93 // the prebuilt is Name() without "prebuilt_" prefix
94 Source_apex_name *string
95
Jiyong Park10e926b2020-07-16 21:38:56 +090096 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010097
Paul Duffinef6b6952021-06-15 11:34:01 +010098 // whether the extracted apex file is installable.
99 Installable *bool
100
101 // optional name for the installed apex. If unspecified, name of the
102 // module is used as the file name
103 Filename *string
104
105 // names of modules to be overridden. Listed modules can only be other binaries
106 // (in Make or Soong).
107 // This does not completely prevent installation of the overridden binaries, but if both
108 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
109 // from PRODUCT_PACKAGES.
110 Overrides []string
111
Paul Duffin3bae0682021-05-05 18:03:47 +0100112 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
113 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
114 // dexpreopt and boot jars package check.
115 Exported_java_libs []string
116
117 // 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
Jiyong Park10e926b2020-07-16 21:38:56 +0900124}
125
Paul Duffinef6b6952021-06-15 11:34:01 +0100126// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
127// module that is common to Prebuilt and ApexSet.
128func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
129 p.prebuiltCommonProperties = properties
130 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
131 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
132}
133
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100134func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000135 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
136}
137
138func (p *prebuiltCommon) BaseModuleName() string {
139 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100140}
141
Jiyong Park10e926b2020-07-16 21:38:56 +0900142func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
143 return &p.prebuilt
144}
145
146func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100147 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900148}
149
150func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900151 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900152
153 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
154 // to build the prebuilts themselves.
155 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
156
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700157 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
158 sanitized := ctx.Module().(sanitizedPrebuilt)
159 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
160 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900161
162 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100163 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900164 return true
165 }
166 return false
167}
168
Paul Duffinef6b6952021-06-15 11:34:01 +0100169func (p *prebuiltCommon) InstallFilename() string {
170 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
171}
172
173func (p *prebuiltCommon) Name() string {
174 return p.prebuilt.Name(p.ModuleBase.Name())
175}
176
177func (p *prebuiltCommon) Overrides() []string {
178 return p.prebuiltCommonProperties.Overrides
179}
180
181func (p *prebuiltCommon) installable() bool {
182 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
183}
184
Spandan Das2069c3f2023-12-06 19:40:24 +0000185// To satisfy java.DexpreopterInterface
186func (p *prebuiltCommon) IsInstallable() bool {
187 return p.installable()
188}
189
190// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100191func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000192 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
193 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
194 p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
195 }
196}
Paul Duffinc30aea22021-06-15 19:10:11 +0100197
Spandan Das2069c3f2023-12-06 19:40:24 +0000198// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
199func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext) {
200 // If this apex does not export anything, return
201 if !p.hasExportedDeps() {
202 return
203 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000204 // If this prebuilt apex has not been selected, return
205 if p.IsHideFromMake() {
206 return
207 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000208 // Use apex_name to determine the api domain of this prebuilt apex
209 apexName := p.ApexVariationName()
Spandan Dasfae468e2023-12-12 23:23:53 +0000210 di, err := android.FindDeapexerProviderForModule(ctx)
211 if err != nil {
212 ctx.ModuleErrorf(err.Error())
213 }
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
Jiakai Zhang204356f2021-09-09 08:12:46 +0000227func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
228 for _, fi := range p.apexFilesForAndroidMk {
229 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
230 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
231 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
232 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000233 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000234}
235
Paul Duffinef6b6952021-06-15 11:34:01 +0100236func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100237 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100238 {
239 Class: "ETC",
240 OutputFile: android.OptionalPathForPath(p.outputApex),
241 Include: "$(BUILD_PREBUILT)",
242 Host_required: p.hostRequired,
Spandan Das3576e762024-01-03 18:57:03 +0000243 OverrideName: p.BaseModuleName(),
Paul Duffinef6b6952021-06-15 11:34:01 +0100244 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
245 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800246 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100247 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700248 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
249 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700250 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100251 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
252 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900253 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000254 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100255 },
256 },
257 },
258 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100259
Spandan Das2069c3f2023-12-06 19:40:24 +0000260 // Add the dexpreopt artifacts to androidmk
261 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
262 entriesList = append(entriesList, install.ToMakeEntries())
263 }
264
Paul Duffinc30aea22021-06-15 19:10:11 +0100265 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
266 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
267 // apex specific variants of the exported java modules available for use from within make.
268 apexName := p.BaseModuleName()
269 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100270 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100271 entriesList = append(entriesList, entries)
272 }
273
274 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100275}
276
Paul Duffin9dc8c542021-06-17 13:33:09 +0100277// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
278func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
279 moduleName := fi.androidMkModuleName + "." + apexName
280 entries := android.AndroidMkEntries{
281 Class: fi.class.nameInMake(),
282 OverrideName: moduleName,
283 OutputFile: android.OptionalPathForPath(fi.builtFile),
284 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
285 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
286 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800287 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000288 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
289 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700290 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100291
292 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
293 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
294 // we will have foo.jar.jar
295 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100296 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
297 entries.SetString("LOCAL_DEX_PREOPT", "false")
298 },
299 },
300 ExtraFooters: []android.AndroidMkExtraFootersFunc{
301 func(w io.Writer, name, prefix, moduleDir string) {
302 // m <module_name> will build <module_name>.<apex_name> as well.
303 if fi.androidMkModuleName != moduleName {
304 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
305 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
306 }
307 },
308 },
309 }
310 return entries
311}
312
Paul Duffin5dda3e32021-05-05 14:13:27 +0100313// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
314// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
315type prebuiltApexModuleCreator interface {
316 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
317}
318
319// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
320// prebuiltApexModuleCreator's createPrebuiltApexModules method.
321//
322// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
323// will need to access dependencies added by that (exported modules) but must run before the
324// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
325// exported modules.
326func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
327 module := ctx.Module()
328 if creator, ok := module.(prebuiltApexModuleCreator); ok {
329 creator.createPrebuiltApexModules(ctx)
330 }
331}
332
Liz Kammer2dc72442023-04-20 10:10:48 -0400333func (p *prebuiltCommon) hasExportedDeps() bool {
334 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
335 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
336 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000337}
338
Paul Duffin57f83592021-05-05 15:09:44 +0100339// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
340func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
341 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100342
Liz Kammer2dc72442023-04-20 10:10:48 -0400343 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000344 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400345 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
346 }
347
348 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
349 prebuiltDep := android.PrebuiltNameFromSource(dep)
350 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
351 }
352
353 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
354 prebuiltDep := android.PrebuiltNameFromSource(dep)
355 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100356 }
Paul Duffindfd33262021-04-06 17:02:08 +0100357}
358
Paul Duffinb17d0442021-05-05 12:07:00 +0100359// Implements android.DepInInSameApex
360func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
361 tag := ctx.OtherModuleDependencyTag(dep)
362 _, ok := tag.(exportedDependencyTag)
363 return ok
364}
365
Paul Duffindfd33262021-04-06 17:02:08 +0100366// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
367// specific variant and checks that they are supported.
368//
369// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
370// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
371//
372// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
373// across prebuilt_apex modules. That is because there is no way to determine whether two
374// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
375// been built from different source at different times or they could have been built with different
376// build options that affect the libraries.
377//
378// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
379// modules were compatible it would be a lot of work and would not provide much benefit for a couple
380// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700381// - The number of prebuilt_apex modules that will be exporting files for the same module will be
382// low as the prebuilt_apex only exports files for the direct dependencies that require it and
383// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
384// few com.android.art* apex files that contain the same contents and could export files for the
385// same modules but only one of them needs to do so. Contrast that with source apex modules which
386// need apex specific variants for every module that contributes code to the apex, whether direct
387// or indirect.
388// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
389// extra copying of files. Contrast that with source apex modules that has to build each variant
390// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100391func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
392
393 // Collect direct dependencies into contents.
394 contents := make(map[string]android.ApexMembership)
395
396 // Collect the list of dependencies.
397 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100398 mctx.WalkDeps(func(child, parent android.Module) bool {
399 // If the child is not in the same apex as the parent then exit immediately and do not visit
400 // any of the child's dependencies.
401 if !android.IsDepInSameApex(mctx, parent, child) {
402 return false
403 }
404
405 tag := mctx.OtherModuleDependencyTag(child)
406 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100407 if exportedTag, ok := tag.(exportedDependencyTag); ok {
408 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100409
410 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100411 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100412 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100413 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100414 }
415
416 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100417 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100418 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100419 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100420 }
Paul Duffindfd33262021-04-06 17:02:08 +0100421 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100422
Paul Duffinfee8cf32021-06-29 18:38:38 +0100423 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
424 // variant.
425 if _, ok := child.(android.ApexModule); !ok {
426 return false
427 }
428
Paul Duffinb17d0442021-05-05 12:07:00 +0100429 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
430 // behavior whether there is a corresponding source module present or not.
431 depName = android.RemoveOptionalPrebuiltPrefix(depName)
432
433 // Remember if this module was added as a direct dependency.
434 direct := parent == mctx.Module()
435 contents[depName] = contents[depName].Add(direct)
436
437 // Add the module to the list of dependencies that need to have an APEX variant.
438 dependencies = append(dependencies, child.(android.ApexModule))
439
440 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100441 })
442
443 // Create contents for the prebuilt_apex and store it away for later use.
444 apexContents := android.NewApexContents(contents)
Spandan Dasf5e03f12024-01-25 19:25:42 +0000445 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100446 Contents: apexContents,
447 })
448
449 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100450 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100451 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000452 ApexVariationName: apexVariationName,
453 InApexVariants: []string{apexVariationName},
Spandan Das3576e762024-01-03 18:57:03 +0000454 InApexModules: []string{p.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100455 ApexContents: []*android.ApexContents{apexContents},
456 ForPrebuiltApex: true,
457 }
458
459 // Mark the dependencies of this module as requiring a variant for this module.
460 for _, am := range dependencies {
461 am.BuildForApex(apexInfo)
462 }
463}
464
Paul Duffin11216db2021-03-01 14:14:52 +0000465// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
466// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
467// deapexer.
468type prebuiltApexSelectorModule struct {
469 android.ModuleBase
470
471 apexFileProperties ApexFileProperties
472
473 inputApex android.Path
474}
475
476func privateApexSelectorModuleFactory() android.Module {
477 module := &prebuiltApexSelectorModule{}
478 module.AddProperties(
479 &module.apexFileProperties,
480 )
481 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
482 return module
483}
484
485func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
486 return android.Paths{p.inputApex}
487}
488
489func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
490 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
491}
492
Jiyong Park09d77522019-11-18 11:16:27 +0900493type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900494 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900495
Paul Duffinbb0dc132021-05-05 16:58:08 +0100496 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900497
Paul Duffinef6b6952021-06-15 11:34:01 +0100498 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700499
500 provenanceMetaDataFile android.OutputPath
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000501
502 // Single aconfig "cache file" merged from this module and all dependencies.
503 mergedAconfigFiles map[string]android.Paths
Jiyong Park09d77522019-11-18 11:16:27 +0900504}
505
Paul Duffin851f3992021-01-13 17:03:51 +0000506type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900507 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000508 //
509 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
510 // for android_common. That is so that it will have the same arch variant as, and so be compatible
511 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000512 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900513 Arch struct {
514 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000515 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900516 }
517 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000518 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900519 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800520 Riscv64 struct {
521 Src *string `android:"path"`
522 }
Jiyong Park09d77522019-11-18 11:16:27 +0900523 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000524 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900525 }
526 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000527 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900528 }
529 }
Paul Duffin851f3992021-01-13 17:03:51 +0000530}
531
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000532// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
533//
534// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
535// to use methods on it that are specific to the current module.
536//
537// See the ApexFileProperties.Src property.
538func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
539 multiTargets := prebuilt.MultiTargets()
540 if len(multiTargets) != 1 {
541 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
542 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000543 }
544 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000545 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000546 case android.Arm:
547 src = String(p.Arch.Arm.Src)
548 case android.Arm64:
549 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800550 case android.Riscv64:
551 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700552 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
553 if src == "" {
554 src = String(p.Arch.Arm64.Src)
555 }
Paul Duffin851f3992021-01-13 17:03:51 +0000556 case android.X86:
557 src = String(p.Arch.X86.Src)
558 case android.X86_64:
559 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000560 }
561 if src == "" {
562 src = String(p.Src)
563 }
Paul Duffin851f3992021-01-13 17:03:51 +0000564
Paul Duffinc0609c62021-03-01 17:27:16 +0000565 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700566 if ctx.Config().AllowMissingDependencies() {
567 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
568 } else {
569 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
570 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000571 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
572 // logic from reporting a more general, less useful message.
573 }
574
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000575 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000576}
577
578type PrebuiltProperties struct {
579 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900580
Paul Duffinef6b6952021-06-15 11:34:01 +0100581 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900582}
583
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700584func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
585 return false
586}
587
Jiyong Park09d77522019-11-18 11:16:27 +0900588func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
589 switch tag {
590 case "":
591 return android.Paths{p.outputApex}, nil
592 default:
593 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
594 }
595}
596
Jiyong Park09d77522019-11-18 11:16:27 +0900597// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
598func PrebuiltFactory() android.Module {
599 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100600 module.AddProperties(&module.properties)
601 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000602
Jiyong Park09d77522019-11-18 11:16:27 +0900603 return module
604}
605
Paul Duffin5dda3e32021-05-05 14:13:27 +0100606func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000607 props := struct {
608 Name *string
609 }{
610 Name: proptools.StringPtr(name),
611 }
612
613 ctx.CreateModule(privateApexSelectorModuleFactory,
614 &props,
615 apexFileProperties,
616 )
617}
618
Paul Duffin5dda3e32021-05-05 14:13:27 +0100619// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
620//
Paul Duffin57f83592021-05-05 15:09:44 +0100621// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
622// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
623// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000624func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100625 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400626 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100627 return
628 }
629
Paul Duffin57f83592021-05-05 15:09:44 +0100630 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100631 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100632 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100633 ctx.WalkDeps(func(child, parent android.Module) bool {
634 tag := ctx.OtherModuleDependencyTag(child)
635
Paul Duffin7db57e02021-06-17 14:56:05 +0100636 // If the child is not in the same apex as the parent then ignore it and all its children.
637 if !android.IsDepInSameApex(ctx, parent, child) {
638 return false
639 }
640
Paul Duffin57f83592021-05-05 15:09:44 +0100641 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100642 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100643 commonModules = append(commonModules, name)
644
645 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100646 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100647
Paul Duffin7db57e02021-06-17 14:56:05 +0100648 // Visit the dependencies of this module just in case they also require files from the
649 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100650 return true
651 }
652
653 return false
654 })
655
Paul Duffin3bae0682021-05-05 18:03:47 +0100656 // Create properties for deapexer module.
657 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100658 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100659 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100660 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100661 }
662
663 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100664 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100665
Paul Duffin11216db2021-03-01 14:14:52 +0000666 props := struct {
667 Name *string
668 Selected_apex *string
669 }{
670 Name: proptools.StringPtr(deapexerName),
671 Selected_apex: proptools.StringPtr(apexFileSource),
672 }
673 ctx.CreateModule(privateDeapexerFactory,
674 &props,
675 deapexerProperties,
676 )
677}
678
Paul Duffin11216db2021-03-01 14:14:52 +0000679func apexSelectorModuleName(baseModuleName string) string {
680 return baseModuleName + ".apex.selector"
681}
682
Paul Duffin064b70c2020-11-02 17:32:38 +0000683func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
684 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
685 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
686 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
687 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
688 // and not a renamed prebuilt module then that will be detected and reported as an error when
689 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100690 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000691 if ctx.OtherModuleExists(prebuiltName) {
692 name = prebuiltName
693 }
694 return name
695}
696
Paul Duffina7139422021-02-08 11:01:58 +0000697type exportedDependencyTag struct {
698 blueprint.BaseDependencyTag
699 name string
700}
701
702// Mark this tag so dependencies that use it are excluded from visibility enforcement.
703//
704// This does allow any prebuilt_apex to reference any module which does open up a small window for
705// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
706// avoids opening up a much bigger window by widening the visibility of modules that need files
707// provided by the prebuilt_apex to include all the possible locations they may be defined, which
708// could include everything below vendor/.
709//
710// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
711// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
712// the .apex file. It will also have to be included in the module's apex_available property too.
713// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
714// incorrectly.
715func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
716
Paul Duffin7db57e02021-06-17 14:56:05 +0100717func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
718
719var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
720
Paul Duffina7139422021-02-08 11:01:58 +0000721var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000722 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
723 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
724 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000725)
726
Paul Duffin5dda3e32021-05-05 14:13:27 +0100727var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
728
729// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
730// build.
731//
732// If this needs to make files from within a `.apex` file available for use by other Soong modules,
733// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
734// it does so as follows:
735//
Colin Crossd079e0b2022-08-16 10:27:33 -0700736// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
737// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100738//
Colin Crossd079e0b2022-08-16 10:27:33 -0700739// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
740// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
741// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100742//
Colin Crossd079e0b2022-08-16 10:27:33 -0700743// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
744// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100745//
746// It also creates a child module `selector` that is responsible for selecting the appropriate
747// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
Paul Duffin5dda3e32021-05-05 14:13:27 +0100748//
Colin Crossd079e0b2022-08-16 10:27:33 -0700749// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100750//
Colin Crossd079e0b2022-08-16 10:27:33 -0700751// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
752// `apex_set`.
753//
754// prebuilt_apex
755// / | \
756// / | \
757// V V V
758// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100759func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +0000760 apexSelectorModuleName := apexSelectorModuleName(p.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +0100761 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
762
763 apexFileSource := ":" + apexSelectorModuleName
Spandan Das3576e762024-01-03 18:57:03 +0000764 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(p.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100765
766 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100767 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100768}
769
Paul Duffin57f83592021-05-05 15:09:44 +0100770func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
771 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000772}
773
Spandan Das2069c3f2023-12-06 19:40:24 +0000774func (p *prebuiltCommon) DepsMutator(ctx android.BottomUpMutatorContext) {
775 if p.hasExportedDeps() {
776 // Create a dependency from the prebuilt apex (prebuilt_apex/apex_set) to the internal deapexer module
777 // The deapexer will return a provider that will be bubbled up to the rdeps of apexes (e.g. dex_bootjars)
Spandan Das3576e762024-01-03 18:57:03 +0000778 ctx.AddDependency(ctx.Module(), android.DeapexerTag, deapexerModuleName(p.Name()))
Spandan Das2069c3f2023-12-06 19:40:24 +0000779 }
780}
781
Paul Duffin064b70c2020-11-02 17:32:38 +0000782var _ ApexInfoMutator = (*Prebuilt)(nil)
783
Paul Duffin064b70c2020-11-02 17:32:38 +0000784func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100785 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900786}
787
Spandan Dasda739a32023-12-13 00:06:32 +0000788// Set a provider containing information about the jars and .prof provided by the apex
789// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
790// Used by dex_bootjars to generate the boot image
791func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext) {
792 if !p.hasExportedDeps() {
793 // nothing to do
794 return
795 }
796 if di, err := android.FindDeapexerProviderForModule(ctx); err == nil {
Spandan Das5be63332023-12-13 00:06:32 +0000797 javaModuleToDexPath := map[string]android.Path{}
798 for _, commonModule := range di.GetExportedModuleNames() {
799 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
800 javaModuleToDexPath[commonModule] = dex
801 }
802 }
803
Spandan Dasda739a32023-12-13 00:06:32 +0000804 exports := android.ApexExportsInfo{
Spandan Das5be63332023-12-13 00:06:32 +0000805 ApexName: p.ApexVariationName(),
806 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
807 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
Spandan Dasda739a32023-12-13 00:06:32 +0000808 }
Spandan Dasa41b8ec2023-12-22 00:05:04 +0000809 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000810 } else {
811 ctx.ModuleErrorf(err.Error())
812 }
813}
814
Jiyong Park09d77522019-11-18 11:16:27 +0900815func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900816 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900817 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100818 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900819 p.installDir = android.PathForModuleInstall(ctx, "apex")
820 p.installFilename = p.InstallFilename()
821 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
822 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
823 }
824 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
825 ctx.Build(pctx, android.BuildParams{
826 Rule: android.Cp,
827 Input: p.inputApex,
828 Output: p.outputApex,
829 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900830
831 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800832 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900833 return
834 }
835
Spandan Das2069c3f2023-12-06 19:40:24 +0000836 // dexpreopt any system server jars if present
837 p.dexpreoptSystemServerJars(ctx)
838
Spandan Dasda739a32023-12-13 00:06:32 +0000839 // provide info used for generating the boot image
840 p.provideApexExportsInfo(ctx)
841
Paul Duffinc30aea22021-06-15 19:10:11 +0100842 // Save the files that need to be made available to Make.
843 p.initApexFilesForAndroidMk(ctx)
844
Colin Crossccba23d2021-11-12 19:01:29 +0000845 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900846 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000847 // or that prebuilt_apex overrides other apexes (using overrides: prop)
848 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900849 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700850 }
851
852 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800853 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700854 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900855 }
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000856
857 android.CollectDependencyAconfigFiles(ctx, &p.mergedAconfigFiles)
Jiyong Park09d77522019-11-18 11:16:27 +0900858}
859
Wei Li340ee8e2022-03-18 17:33:24 -0700860func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
861 return p.provenanceMetaDataFile
862}
863
Paul Duffin24704672021-04-06 16:09:30 +0100864// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
865// module. It extracts the correct apex to use and makes it available for use by apex_set.
866type prebuiltApexExtractorModule struct {
867 android.ModuleBase
868
869 properties ApexExtractorProperties
870
871 extractedApex android.WritablePath
872}
873
874func privateApexExtractorModuleFactory() android.Module {
875 module := &prebuiltApexExtractorModule{}
876 module.AddProperties(
877 &module.properties,
878 )
879 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
880 return module
881}
882
883func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
884 return android.Paths{p.extractedApex}
885}
886
887func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
888 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
889 return p.properties.prebuiltSrcs(ctx)
890 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000891 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100892 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
893 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000894 // Filter out NativeBridge archs (b/260115309)
895 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100896 ctx.Build(pctx,
897 android.BuildParams{
898 Rule: extractMatchingApex,
899 Description: "Extract an apex from an apex set",
900 Inputs: android.Paths{apexSet},
901 Output: p.extractedApex,
902 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000903 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000904 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100905 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700906 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100907 },
908 })
909}
910
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700911type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900912 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700913
914 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700915}
916
Paul Duffin24704672021-04-06 16:09:30 +0100917type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700918 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000919 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700920
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700921 Sanitized struct {
922 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000923 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700924 }
925 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000926 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700927 }
928 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000929 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700930 }
931 }
932
Paul Duffin24704672021-04-06 16:09:30 +0100933 // apexes in this set use prerelease SDK version
934 Prerelease *bool
935}
936
937func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
938 var srcs []string
939 if e.Set != nil {
940 srcs = append(srcs, *e.Set)
941 }
942
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900943 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100944
945 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
946 srcs = append(srcs, *e.Sanitized.Address.Set)
947 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
948 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
949 } else if e.Sanitized.None.Set != nil {
950 srcs = append(srcs, *e.Sanitized.None.Set)
951 }
952
953 return srcs
954}
955
956type ApexSetProperties struct {
957 ApexExtractorProperties
958
Paul Duffinef6b6952021-06-15 11:34:01 +0100959 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700960}
961
962func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
963 if sanitizer == "address" {
964 return a.properties.Sanitized.Address.Set != nil
965 }
966 if sanitizer == "hwaddress" {
967 return a.properties.Sanitized.Hwaddress.Set != nil
968 }
969
970 return false
971}
972
Paul Duffin4f2282c2022-12-12 17:44:33 +0000973func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
974 switch tag {
975 case "":
976 return android.Paths{a.outputApex}, nil
977 default:
978 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
979 }
980}
981
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700982// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
983func apexSetFactory() android.Module {
984 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100985 module.AddProperties(&module.properties)
986 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100987
Paul Duffin24704672021-04-06 16:09:30 +0100988 return module
989}
990
Paul Duffin5dda3e32021-05-05 14:13:27 +0100991func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100992 props := struct {
993 Name *string
994 }{
995 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700996 }
997
Paul Duffin24704672021-04-06 16:09:30 +0100998 ctx.CreateModule(privateApexExtractorModuleFactory,
999 &props,
1000 apexExtractorProperties,
1001 )
1002}
1003
1004func apexExtractorModuleName(baseModuleName string) string {
1005 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001006}
1007
Paul Duffin5dda3e32021-05-05 14:13:27 +01001008var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
1009
1010// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
1011// modules.
1012//
1013// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
1014// prebuilt_apex except that instead of creating a selector module which selects one .apex file
1015// from those provided this creates an extractor module which extracts the appropriate .apex file
1016// from the zip file containing them.
1017func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +00001018 apexExtractorModuleName := apexExtractorModuleName(a.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +01001019 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
1020
1021 apexFileSource := ":" + apexExtractorModuleName
Spandan Das3576e762024-01-03 18:57:03 +00001022 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(a.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001023
1024 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +01001025 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001026}
1027
Paul Duffin57f83592021-05-05 15:09:44 +01001028func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
1029 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01001030}
1031
1032var _ ApexInfoMutator = (*ApexSet)(nil)
1033
1034func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
1035 a.apexInfoMutator(mctx)
1036}
1037
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001038func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +09001039 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001040 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +01001041 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
1042 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001043 }
1044
Paul Duffinbb0dc132021-05-05 16:58:08 +01001045 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001046 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +01001047 ctx.Build(pctx, android.BuildParams{
1048 Rule: android.Cp,
1049 Input: inputApex,
1050 Output: a.outputApex,
1051 })
Jiyong Park10e926b2020-07-16 21:38:56 +09001052
1053 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001054 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +09001055 return
1056 }
1057
Spandan Das2069c3f2023-12-06 19:40:24 +00001058 // dexpreopt any system server jars if present
1059 a.dexpreoptSystemServerJars(ctx)
1060
Spandan Dasda739a32023-12-13 00:06:32 +00001061 // provide info used for generating the boot image
1062 a.provideApexExportsInfo(ctx)
1063
Paul Duffinc30aea22021-06-15 19:10:11 +01001064 // Save the files that need to be made available to Make.
1065 a.initApexFilesForAndroidMk(ctx)
1066
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001067 a.installDir = android.PathForModuleInstall(ctx, "apex")
1068 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001069 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001070 }
1071
1072 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001073 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001074 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001075 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001076 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001077 }
1078}
1079
Paul Duffinef6b6952021-06-15 11:34:01 +01001080type systemExtContext struct {
1081 android.ModuleContext
1082}
1083
1084func (*systemExtContext) SystemExtSpecific() bool {
1085 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001086}