blob: 34dfc467b5e12b45b5fd58999f24d887915d4767 [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
Spandan Dasa747d2e2024-03-11 21:37:25 +0000124
125 // Path to the .prebuilt_info file of the prebuilt apex.
126 // In case of mainline modules, the .prebuilt_info file contains the build_id that was used to
127 // generate the prebuilt.
128 Prebuilt_info *string `android:"path"`
Jiyong Park10e926b2020-07-16 21:38:56 +0900129}
130
Paul Duffinef6b6952021-06-15 11:34:01 +0100131// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
132// module that is common to Prebuilt and ApexSet.
133func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
134 p.prebuiltCommonProperties = properties
135 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
136 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
137}
138
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100139func (p *prebuiltCommon) ApexVariationName() string {
Spandan Das3576e762024-01-03 18:57:03 +0000140 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.BaseModuleName())
141}
142
143func (p *prebuiltCommon) BaseModuleName() string {
144 return proptools.StringDefault(p.prebuiltCommonProperties.Source_apex_name, p.ModuleBase.BaseModuleName())
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100145}
146
Jiyong Park10e926b2020-07-16 21:38:56 +0900147func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
148 return &p.prebuilt
149}
150
151func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100152 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900153}
154
155func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900156 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900157
158 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
159 // to build the prebuilts themselves.
160 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
161
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700162 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
163 sanitized := ctx.Module().(sanitizedPrebuilt)
164 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
165 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900166
167 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100168 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900169 return true
170 }
171 return false
172}
173
Paul Duffinef6b6952021-06-15 11:34:01 +0100174func (p *prebuiltCommon) InstallFilename() string {
175 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
176}
177
178func (p *prebuiltCommon) Name() string {
179 return p.prebuilt.Name(p.ModuleBase.Name())
180}
181
182func (p *prebuiltCommon) Overrides() []string {
183 return p.prebuiltCommonProperties.Overrides
184}
185
186func (p *prebuiltCommon) installable() bool {
187 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
188}
189
Spandan Das2069c3f2023-12-06 19:40:24 +0000190// To satisfy java.DexpreopterInterface
191func (p *prebuiltCommon) IsInstallable() bool {
192 return p.installable()
193}
194
195// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100196func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000197 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
198 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
199 p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
200 }
201}
Paul Duffinc30aea22021-06-15 19:10:11 +0100202
Spandan Das2069c3f2023-12-06 19:40:24 +0000203// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
204func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext) {
205 // If this apex does not export anything, return
206 if !p.hasExportedDeps() {
207 return
208 }
Spandan Dase21a8d42024-01-23 23:56:29 +0000209 // If this prebuilt apex has not been selected, return
210 if p.IsHideFromMake() {
211 return
212 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000213 // Use apex_name to determine the api domain of this prebuilt apex
214 apexName := p.ApexVariationName()
Spandan Dasfae468e2023-12-12 23:23:53 +0000215 di, err := android.FindDeapexerProviderForModule(ctx)
216 if err != nil {
217 ctx.ModuleErrorf(err.Error())
218 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000219 dc := dexpreopt.GetGlobalConfig(ctx)
220 systemServerJarList := dc.AllApexSystemServerJars(ctx)
221
222 for i := 0; i < systemServerJarList.Len(); i++ {
223 sscpApex := systemServerJarList.Apex(i)
224 sscpJar := systemServerJarList.Jar(i)
225 if apexName != sscpApex {
226 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100227 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000228 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
229 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100230}
231
Jiakai Zhang204356f2021-09-09 08:12:46 +0000232func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
233 for _, fi := range p.apexFilesForAndroidMk {
234 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
235 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
236 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
237 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000238 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000239}
240
Paul Duffinef6b6952021-06-15 11:34:01 +0100241func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100242 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100243 {
244 Class: "ETC",
245 OutputFile: android.OptionalPathForPath(p.outputApex),
246 Include: "$(BUILD_PREBUILT)",
247 Host_required: p.hostRequired,
Spandan Das3576e762024-01-03 18:57:03 +0000248 OverrideName: p.BaseModuleName(),
Paul Duffinef6b6952021-06-15 11:34:01 +0100249 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
250 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800251 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100252 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700253 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
254 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700255 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100256 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
257 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900258 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000259 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100260 },
261 },
262 },
263 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100264
Spandan Das2069c3f2023-12-06 19:40:24 +0000265 // Add the dexpreopt artifacts to androidmk
266 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
267 entriesList = append(entriesList, install.ToMakeEntries())
268 }
269
Paul Duffinc30aea22021-06-15 19:10:11 +0100270 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
271 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
272 // apex specific variants of the exported java modules available for use from within make.
273 apexName := p.BaseModuleName()
274 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100275 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100276 entriesList = append(entriesList, entries)
277 }
278
279 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100280}
281
Paul Duffin9dc8c542021-06-17 13:33:09 +0100282// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
283func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
284 moduleName := fi.androidMkModuleName + "." + apexName
285 entries := android.AndroidMkEntries{
286 Class: fi.class.nameInMake(),
287 OverrideName: moduleName,
288 OutputFile: android.OptionalPathForPath(fi.builtFile),
289 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
290 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
291 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800292 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000293 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
294 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700295 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100296
297 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
298 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
299 // we will have foo.jar.jar
300 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100301 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
302 entries.SetString("LOCAL_DEX_PREOPT", "false")
303 },
304 },
305 ExtraFooters: []android.AndroidMkExtraFootersFunc{
306 func(w io.Writer, name, prefix, moduleDir string) {
307 // m <module_name> will build <module_name>.<apex_name> as well.
308 if fi.androidMkModuleName != moduleName {
309 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
310 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
311 }
312 },
313 },
314 }
315 return entries
316}
317
Paul Duffin5dda3e32021-05-05 14:13:27 +0100318// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
319// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
320type prebuiltApexModuleCreator interface {
321 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
322}
323
324// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
325// prebuiltApexModuleCreator's createPrebuiltApexModules method.
326//
327// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
328// will need to access dependencies added by that (exported modules) but must run before the
329// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
330// exported modules.
331func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
332 module := ctx.Module()
333 if creator, ok := module.(prebuiltApexModuleCreator); ok {
334 creator.createPrebuiltApexModules(ctx)
335 }
336}
337
Liz Kammer2dc72442023-04-20 10:10:48 -0400338func (p *prebuiltCommon) hasExportedDeps() bool {
339 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
340 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
341 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000342}
343
Paul Duffin57f83592021-05-05 15:09:44 +0100344// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
345func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
346 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100347
Liz Kammer2dc72442023-04-20 10:10:48 -0400348 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000349 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400350 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
351 }
352
353 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
354 prebuiltDep := android.PrebuiltNameFromSource(dep)
355 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
356 }
357
358 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
359 prebuiltDep := android.PrebuiltNameFromSource(dep)
360 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100361 }
Paul Duffindfd33262021-04-06 17:02:08 +0100362}
363
Paul Duffinb17d0442021-05-05 12:07:00 +0100364// Implements android.DepInInSameApex
365func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
366 tag := ctx.OtherModuleDependencyTag(dep)
367 _, ok := tag.(exportedDependencyTag)
368 return ok
369}
370
Paul Duffindfd33262021-04-06 17:02:08 +0100371// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
372// specific variant and checks that they are supported.
373//
374// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
375// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
376//
377// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
378// across prebuilt_apex modules. That is because there is no way to determine whether two
379// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
380// been built from different source at different times or they could have been built with different
381// build options that affect the libraries.
382//
383// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
384// modules were compatible it would be a lot of work and would not provide much benefit for a couple
385// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700386// - The number of prebuilt_apex modules that will be exporting files for the same module will be
387// low as the prebuilt_apex only exports files for the direct dependencies that require it and
388// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
389// few com.android.art* apex files that contain the same contents and could export files for the
390// same modules but only one of them needs to do so. Contrast that with source apex modules which
391// need apex specific variants for every module that contributes code to the apex, whether direct
392// or indirect.
393// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
394// extra copying of files. Contrast that with source apex modules that has to build each variant
395// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100396func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
397
398 // Collect direct dependencies into contents.
399 contents := make(map[string]android.ApexMembership)
400
401 // Collect the list of dependencies.
402 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100403 mctx.WalkDeps(func(child, parent android.Module) bool {
404 // If the child is not in the same apex as the parent then exit immediately and do not visit
405 // any of the child's dependencies.
406 if !android.IsDepInSameApex(mctx, parent, child) {
407 return false
408 }
409
410 tag := mctx.OtherModuleDependencyTag(child)
411 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100412 if exportedTag, ok := tag.(exportedDependencyTag); ok {
413 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100414
415 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100416 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100417 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100418 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100419 }
420
421 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100422 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100423 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100424 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100425 }
Paul Duffindfd33262021-04-06 17:02:08 +0100426 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100427
Paul Duffinfee8cf32021-06-29 18:38:38 +0100428 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
429 // variant.
430 if _, ok := child.(android.ApexModule); !ok {
431 return false
432 }
433
Paul Duffinb17d0442021-05-05 12:07:00 +0100434 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
435 // behavior whether there is a corresponding source module present or not.
436 depName = android.RemoveOptionalPrebuiltPrefix(depName)
437
438 // Remember if this module was added as a direct dependency.
439 direct := parent == mctx.Module()
440 contents[depName] = contents[depName].Add(direct)
441
442 // Add the module to the list of dependencies that need to have an APEX variant.
443 dependencies = append(dependencies, child.(android.ApexModule))
444
445 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100446 })
447
448 // Create contents for the prebuilt_apex and store it away for later use.
449 apexContents := android.NewApexContents(contents)
Spandan Dasf5e03f12024-01-25 19:25:42 +0000450 android.SetProvider(mctx, android.ApexBundleInfoProvider, android.ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100451 Contents: apexContents,
452 })
453
454 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100455 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100456 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000457 ApexVariationName: apexVariationName,
458 InApexVariants: []string{apexVariationName},
Spandan Das3576e762024-01-03 18:57:03 +0000459 InApexModules: []string{p.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100460 ApexContents: []*android.ApexContents{apexContents},
461 ForPrebuiltApex: true,
462 }
463
464 // Mark the dependencies of this module as requiring a variant for this module.
465 for _, am := range dependencies {
466 am.BuildForApex(apexInfo)
467 }
468}
469
Paul Duffin11216db2021-03-01 14:14:52 +0000470// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
471// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
472// deapexer.
473type prebuiltApexSelectorModule struct {
474 android.ModuleBase
475
476 apexFileProperties ApexFileProperties
477
478 inputApex android.Path
479}
480
481func privateApexSelectorModuleFactory() android.Module {
482 module := &prebuiltApexSelectorModule{}
483 module.AddProperties(
484 &module.apexFileProperties,
485 )
486 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
487 return module
488}
489
490func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
491 return android.Paths{p.inputApex}
492}
493
494func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
495 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
496}
497
Jiyong Park09d77522019-11-18 11:16:27 +0900498type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900499 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900500
Paul Duffinbb0dc132021-05-05 16:58:08 +0100501 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900502
Paul Duffinef6b6952021-06-15 11:34:01 +0100503 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700504
505 provenanceMetaDataFile android.OutputPath
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000506
507 // Single aconfig "cache file" merged from this module and all dependencies.
508 mergedAconfigFiles map[string]android.Paths
Jiyong Park09d77522019-11-18 11:16:27 +0900509}
510
Paul Duffin851f3992021-01-13 17:03:51 +0000511type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900512 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000513 //
514 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
515 // for android_common. That is so that it will have the same arch variant as, and so be compatible
516 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000517 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900518 Arch struct {
519 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000520 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900521 }
522 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000523 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900524 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800525 Riscv64 struct {
526 Src *string `android:"path"`
527 }
Jiyong Park09d77522019-11-18 11:16:27 +0900528 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000529 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900530 }
531 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000532 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900533 }
534 }
Paul Duffin851f3992021-01-13 17:03:51 +0000535}
536
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000537// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
538//
539// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
540// to use methods on it that are specific to the current module.
541//
542// See the ApexFileProperties.Src property.
543func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
544 multiTargets := prebuilt.MultiTargets()
545 if len(multiTargets) != 1 {
546 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
547 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000548 }
549 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000550 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000551 case android.Arm:
552 src = String(p.Arch.Arm.Src)
553 case android.Arm64:
554 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800555 case android.Riscv64:
556 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700557 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
558 if src == "" {
559 src = String(p.Arch.Arm64.Src)
560 }
Paul Duffin851f3992021-01-13 17:03:51 +0000561 case android.X86:
562 src = String(p.Arch.X86.Src)
563 case android.X86_64:
564 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000565 }
566 if src == "" {
567 src = String(p.Src)
568 }
Paul Duffin851f3992021-01-13 17:03:51 +0000569
Paul Duffinc0609c62021-03-01 17:27:16 +0000570 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700571 if ctx.Config().AllowMissingDependencies() {
572 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
573 } else {
574 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
575 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000576 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
577 // logic from reporting a more general, less useful message.
578 }
579
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000580 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000581}
582
583type PrebuiltProperties struct {
584 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900585
Paul Duffinef6b6952021-06-15 11:34:01 +0100586 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900587}
588
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700589func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
590 return false
591}
592
Jiyong Park09d77522019-11-18 11:16:27 +0900593func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
594 switch tag {
595 case "":
596 return android.Paths{p.outputApex}, nil
597 default:
598 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
599 }
600}
601
Jiyong Park09d77522019-11-18 11:16:27 +0900602// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
603func PrebuiltFactory() android.Module {
604 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100605 module.AddProperties(&module.properties)
606 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000607
Jiyong Park09d77522019-11-18 11:16:27 +0900608 return module
609}
610
Paul Duffin5dda3e32021-05-05 14:13:27 +0100611func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000612 props := struct {
613 Name *string
614 }{
615 Name: proptools.StringPtr(name),
616 }
617
618 ctx.CreateModule(privateApexSelectorModuleFactory,
619 &props,
620 apexFileProperties,
621 )
622}
623
Paul Duffin5dda3e32021-05-05 14:13:27 +0100624// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
625//
Paul Duffin57f83592021-05-05 15:09:44 +0100626// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
627// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
628// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000629func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100630 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400631 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100632 return
633 }
634
Paul Duffin57f83592021-05-05 15:09:44 +0100635 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100636 commonModules := []string{}
Spandan Das2ea84dd2024-01-25 22:12:50 +0000637 dexpreoptProfileGuidedModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100638 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100639 ctx.WalkDeps(func(child, parent android.Module) bool {
640 tag := ctx.OtherModuleDependencyTag(child)
641
Paul Duffin7db57e02021-06-17 14:56:05 +0100642 // If the child is not in the same apex as the parent then ignore it and all its children.
643 if !android.IsDepInSameApex(ctx, parent, child) {
644 return false
645 }
646
Spandan Das161e4682024-01-19 00:22:22 +0000647 name := java.ModuleStemForDeapexing(child)
Paul Duffin7db57e02021-06-17 14:56:05 +0100648 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100649 commonModules = append(commonModules, name)
650
Spandan Das2ea84dd2024-01-25 22:12:50 +0000651 extract := child.(android.RequiredFilesFromPrebuiltApex)
652 requiredFiles := extract.RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100653 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100654
Spandan Das2ea84dd2024-01-25 22:12:50 +0000655 if extract.UseProfileGuidedDexpreopt() {
656 dexpreoptProfileGuidedModules = append(dexpreoptProfileGuidedModules, name)
657 }
658
Paul Duffin7db57e02021-06-17 14:56:05 +0100659 // Visit the dependencies of this module just in case they also require files from the
660 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100661 return true
662 }
663
664 return false
665 })
666
Paul Duffin3bae0682021-05-05 18:03:47 +0100667 // Create properties for deapexer module.
668 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100669 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100670 // dependency as well as transitive ones.
Spandan Das2ea84dd2024-01-25 22:12:50 +0000671 CommonModules: android.SortedUniqueStrings(commonModules),
672 DexpreoptProfileGuidedModules: android.SortedUniqueStrings(dexpreoptProfileGuidedModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100673 }
674
675 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100676 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100677
Paul Duffin11216db2021-03-01 14:14:52 +0000678 props := struct {
679 Name *string
680 Selected_apex *string
681 }{
682 Name: proptools.StringPtr(deapexerName),
683 Selected_apex: proptools.StringPtr(apexFileSource),
684 }
685 ctx.CreateModule(privateDeapexerFactory,
686 &props,
687 deapexerProperties,
688 )
689}
690
Paul Duffin11216db2021-03-01 14:14:52 +0000691func apexSelectorModuleName(baseModuleName string) string {
692 return baseModuleName + ".apex.selector"
693}
694
Paul Duffin064b70c2020-11-02 17:32:38 +0000695func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
696 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
697 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
698 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
699 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
700 // and not a renamed prebuilt module then that will be detected and reported as an error when
701 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100702 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000703 if ctx.OtherModuleExists(prebuiltName) {
704 name = prebuiltName
705 }
706 return name
707}
708
Paul Duffina7139422021-02-08 11:01:58 +0000709type exportedDependencyTag struct {
710 blueprint.BaseDependencyTag
711 name string
712}
713
714// Mark this tag so dependencies that use it are excluded from visibility enforcement.
715//
716// This does allow any prebuilt_apex to reference any module which does open up a small window for
717// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
718// avoids opening up a much bigger window by widening the visibility of modules that need files
719// provided by the prebuilt_apex to include all the possible locations they may be defined, which
720// could include everything below vendor/.
721//
722// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
723// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
724// the .apex file. It will also have to be included in the module's apex_available property too.
725// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
726// incorrectly.
727func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
728
Paul Duffin7db57e02021-06-17 14:56:05 +0100729func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
730
731var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
732
Paul Duffina7139422021-02-08 11:01:58 +0000733var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000734 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
735 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
736 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000737)
738
Paul Duffin5dda3e32021-05-05 14:13:27 +0100739var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
740
741// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
742// build.
743//
744// If this needs to make files from within a `.apex` file available for use by other Soong modules,
745// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
746// it does so as follows:
747//
Colin Crossd079e0b2022-08-16 10:27:33 -0700748// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
749// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100750//
Colin Crossd079e0b2022-08-16 10:27:33 -0700751// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
752// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
753// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100754//
Colin Crossd079e0b2022-08-16 10:27:33 -0700755// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
756// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100757//
758// It also creates a child module `selector` that is responsible for selecting the appropriate
759// 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 +0100760//
Colin Crossd079e0b2022-08-16 10:27:33 -0700761// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100762//
Colin Crossd079e0b2022-08-16 10:27:33 -0700763// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
764// `apex_set`.
765//
766// prebuilt_apex
767// / | \
768// / | \
769// V V V
770// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100771func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +0000772 apexSelectorModuleName := apexSelectorModuleName(p.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +0100773 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
774
775 apexFileSource := ":" + apexSelectorModuleName
Spandan Das3576e762024-01-03 18:57:03 +0000776 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(p.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100777
778 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100779 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100780}
781
Paul Duffin57f83592021-05-05 15:09:44 +0100782func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
783 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000784}
785
Spandan Das2069c3f2023-12-06 19:40:24 +0000786func (p *prebuiltCommon) DepsMutator(ctx android.BottomUpMutatorContext) {
787 if p.hasExportedDeps() {
788 // Create a dependency from the prebuilt apex (prebuilt_apex/apex_set) to the internal deapexer module
789 // 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 +0000790 ctx.AddDependency(ctx.Module(), android.DeapexerTag, deapexerModuleName(p.Name()))
Spandan Das2069c3f2023-12-06 19:40:24 +0000791 }
792}
793
Paul Duffin064b70c2020-11-02 17:32:38 +0000794var _ ApexInfoMutator = (*Prebuilt)(nil)
795
Paul Duffin064b70c2020-11-02 17:32:38 +0000796func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100797 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900798}
799
Spandan Dasda739a32023-12-13 00:06:32 +0000800// Set a provider containing information about the jars and .prof provided by the apex
801// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
802// Used by dex_bootjars to generate the boot image
803func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext) {
804 if !p.hasExportedDeps() {
805 // nothing to do
806 return
807 }
808 if di, err := android.FindDeapexerProviderForModule(ctx); err == nil {
Spandan Das5be63332023-12-13 00:06:32 +0000809 javaModuleToDexPath := map[string]android.Path{}
810 for _, commonModule := range di.GetExportedModuleNames() {
811 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
812 javaModuleToDexPath[commonModule] = dex
813 }
814 }
815
Spandan Dasda739a32023-12-13 00:06:32 +0000816 exports := android.ApexExportsInfo{
Spandan Das5be63332023-12-13 00:06:32 +0000817 ApexName: p.ApexVariationName(),
818 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
819 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
Spandan Dasda739a32023-12-13 00:06:32 +0000820 }
Spandan Dasa41b8ec2023-12-22 00:05:04 +0000821 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000822 } else {
823 ctx.ModuleErrorf(err.Error())
824 }
825}
826
Spandan Dasa747d2e2024-03-11 21:37:25 +0000827// Set prebuiltInfoProvider. This will be used by `apex_prebuiltinfo_singleton` to print out a metadata file
828// with information about whether source or prebuilt of an apex was used during the build.
829func (p *prebuiltCommon) providePrebuiltInfo(ctx android.ModuleContext) {
830 info := prebuiltInfo{
831 Name: p.BaseModuleName(), // BaseModuleName ensures that this will not contain the prebuilt_ prefix.
832 Is_prebuilt: true,
833 }
834 // If Prebuilt_info information is available in the soong module definition, add it to prebuilt_info.json.
835 if p.prebuiltCommonProperties.Prebuilt_info != nil {
836 info.Prebuilt_info_file_path = android.PathForModuleSrc(ctx, *p.prebuiltCommonProperties.Prebuilt_info).String()
837 }
838 android.SetProvider(ctx, prebuiltInfoProvider, info)
839}
840
Jiyong Park09d77522019-11-18 11:16:27 +0900841func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900842 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900843 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100844 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900845 p.installDir = android.PathForModuleInstall(ctx, "apex")
846 p.installFilename = p.InstallFilename()
847 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
848 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
849 }
850 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
851 ctx.Build(pctx, android.BuildParams{
852 Rule: android.Cp,
853 Input: p.inputApex,
854 Output: p.outputApex,
855 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900856
857 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800858 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900859 return
860 }
861
Spandan Das2069c3f2023-12-06 19:40:24 +0000862 // dexpreopt any system server jars if present
863 p.dexpreoptSystemServerJars(ctx)
864
Spandan Dasda739a32023-12-13 00:06:32 +0000865 // provide info used for generating the boot image
866 p.provideApexExportsInfo(ctx)
867
Spandan Dasa747d2e2024-03-11 21:37:25 +0000868 p.providePrebuiltInfo(ctx)
869
Paul Duffinc30aea22021-06-15 19:10:11 +0100870 // Save the files that need to be made available to Make.
871 p.initApexFilesForAndroidMk(ctx)
872
Colin Crossccba23d2021-11-12 19:01:29 +0000873 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900874 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000875 // or that prebuilt_apex overrides other apexes (using overrides: prop)
876 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900877 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700878 }
879
880 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800881 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700882 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900883 }
LaMont Jonesafe7baf2024-01-09 22:47:39 +0000884
885 android.CollectDependencyAconfigFiles(ctx, &p.mergedAconfigFiles)
Jiyong Park09d77522019-11-18 11:16:27 +0900886}
887
Wei Li340ee8e2022-03-18 17:33:24 -0700888func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
889 return p.provenanceMetaDataFile
890}
891
Paul Duffin24704672021-04-06 16:09:30 +0100892// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
893// module. It extracts the correct apex to use and makes it available for use by apex_set.
894type prebuiltApexExtractorModule struct {
895 android.ModuleBase
896
897 properties ApexExtractorProperties
898
899 extractedApex android.WritablePath
900}
901
902func privateApexExtractorModuleFactory() android.Module {
903 module := &prebuiltApexExtractorModule{}
904 module.AddProperties(
905 &module.properties,
906 )
907 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
908 return module
909}
910
911func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
912 return android.Paths{p.extractedApex}
913}
914
915func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
916 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
917 return p.properties.prebuiltSrcs(ctx)
918 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000919 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100920 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
921 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000922 // Filter out NativeBridge archs (b/260115309)
923 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100924 ctx.Build(pctx,
925 android.BuildParams{
926 Rule: extractMatchingApex,
927 Description: "Extract an apex from an apex set",
928 Inputs: android.Paths{apexSet},
929 Output: p.extractedApex,
930 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000931 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000932 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100933 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700934 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100935 },
936 })
937}
938
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700939type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900940 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700941
942 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700943}
944
Paul Duffin24704672021-04-06 16:09:30 +0100945type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700946 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000947 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700948
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700949 Sanitized struct {
950 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000951 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700952 }
953 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000954 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700955 }
956 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000957 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700958 }
959 }
960
Paul Duffin24704672021-04-06 16:09:30 +0100961 // apexes in this set use prerelease SDK version
962 Prerelease *bool
963}
964
965func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
966 var srcs []string
967 if e.Set != nil {
968 srcs = append(srcs, *e.Set)
969 }
970
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900971 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100972
973 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
974 srcs = append(srcs, *e.Sanitized.Address.Set)
975 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
976 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
977 } else if e.Sanitized.None.Set != nil {
978 srcs = append(srcs, *e.Sanitized.None.Set)
979 }
980
981 return srcs
982}
983
984type ApexSetProperties struct {
985 ApexExtractorProperties
986
Paul Duffinef6b6952021-06-15 11:34:01 +0100987 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700988}
989
990func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
991 if sanitizer == "address" {
992 return a.properties.Sanitized.Address.Set != nil
993 }
994 if sanitizer == "hwaddress" {
995 return a.properties.Sanitized.Hwaddress.Set != nil
996 }
997
998 return false
999}
1000
Paul Duffin4f2282c2022-12-12 17:44:33 +00001001func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
1002 switch tag {
1003 case "":
1004 return android.Paths{a.outputApex}, nil
1005 default:
1006 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
1007 }
1008}
1009
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001010// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
1011func apexSetFactory() android.Module {
1012 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +01001013 module.AddProperties(&module.properties)
1014 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +01001015
Paul Duffin24704672021-04-06 16:09:30 +01001016 return module
1017}
1018
Paul Duffin5dda3e32021-05-05 14:13:27 +01001019func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +01001020 props := struct {
1021 Name *string
1022 }{
1023 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -07001024 }
1025
Paul Duffin24704672021-04-06 16:09:30 +01001026 ctx.CreateModule(privateApexExtractorModuleFactory,
1027 &props,
1028 apexExtractorProperties,
1029 )
1030}
1031
1032func apexExtractorModuleName(baseModuleName string) string {
1033 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001034}
1035
Paul Duffin5dda3e32021-05-05 14:13:27 +01001036var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
1037
1038// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
1039// modules.
1040//
1041// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
1042// prebuilt_apex except that instead of creating a selector module which selects one .apex file
1043// from those provided this creates an extractor module which extracts the appropriate .apex file
1044// from the zip file containing them.
1045func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +00001046 apexExtractorModuleName := apexExtractorModuleName(a.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +01001047 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
1048
1049 apexFileSource := ":" + apexExtractorModuleName
Spandan Das3576e762024-01-03 18:57:03 +00001050 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(a.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001051
1052 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +01001053 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001054}
1055
Paul Duffin57f83592021-05-05 15:09:44 +01001056func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
1057 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01001058}
1059
1060var _ ApexInfoMutator = (*ApexSet)(nil)
1061
1062func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
1063 a.apexInfoMutator(mctx)
1064}
1065
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001066func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +09001067 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001068 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +01001069 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
1070 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001071 }
1072
Paul Duffinbb0dc132021-05-05 16:58:08 +01001073 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001074 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +01001075 ctx.Build(pctx, android.BuildParams{
1076 Rule: android.Cp,
1077 Input: inputApex,
1078 Output: a.outputApex,
1079 })
Jiyong Park10e926b2020-07-16 21:38:56 +09001080
1081 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001082 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +09001083 return
1084 }
1085
Spandan Das2069c3f2023-12-06 19:40:24 +00001086 // dexpreopt any system server jars if present
1087 a.dexpreoptSystemServerJars(ctx)
1088
Spandan Dasda739a32023-12-13 00:06:32 +00001089 // provide info used for generating the boot image
1090 a.provideApexExportsInfo(ctx)
1091
Spandan Dasa747d2e2024-03-11 21:37:25 +00001092 a.providePrebuiltInfo(ctx)
1093
Paul Duffinc30aea22021-06-15 19:10:11 +01001094 // Save the files that need to be made available to Make.
1095 a.initApexFilesForAndroidMk(ctx)
1096
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001097 a.installDir = android.PathForModuleInstall(ctx, "apex")
1098 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001099 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001100 }
1101
1102 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001103 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001104 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001105 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001106 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001107 }
1108}
1109
Paul Duffinef6b6952021-06-15 11:34:01 +01001110type systemExtContext struct {
1111 android.ModuleContext
1112}
1113
1114func (*systemExtContext) SystemExtSpecific() bool {
1115 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001116}