blob: 9db3b7a9df36a14e7841487e5811a254f8a56c38 [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 }
204 // Use apex_name to determine the api domain of this prebuilt apex
205 apexName := p.ApexVariationName()
Spandan Dasfae468e2023-12-12 23:23:53 +0000206 di, err := android.FindDeapexerProviderForModule(ctx)
207 if err != nil {
208 ctx.ModuleErrorf(err.Error())
209 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000210 dc := dexpreopt.GetGlobalConfig(ctx)
211 systemServerJarList := dc.AllApexSystemServerJars(ctx)
212
213 for i := 0; i < systemServerJarList.Len(); i++ {
214 sscpApex := systemServerJarList.Apex(i)
215 sscpJar := systemServerJarList.Jar(i)
216 if apexName != sscpApex {
217 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100218 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000219 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
220 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100221}
222
Jiakai Zhang204356f2021-09-09 08:12:46 +0000223func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
224 for _, fi := range p.apexFilesForAndroidMk {
225 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
226 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
227 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
228 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000229 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000230}
231
Paul Duffinef6b6952021-06-15 11:34:01 +0100232func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100233 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100234 {
235 Class: "ETC",
236 OutputFile: android.OptionalPathForPath(p.outputApex),
237 Include: "$(BUILD_PREBUILT)",
238 Host_required: p.hostRequired,
Spandan Das3576e762024-01-03 18:57:03 +0000239 OverrideName: p.BaseModuleName(),
Paul Duffinef6b6952021-06-15 11:34:01 +0100240 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
241 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800242 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100243 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700244 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
245 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700246 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100247 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
248 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900249 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000250 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100251 },
252 },
253 },
254 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100255
Spandan Das2069c3f2023-12-06 19:40:24 +0000256 // Add the dexpreopt artifacts to androidmk
257 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
258 entriesList = append(entriesList, install.ToMakeEntries())
259 }
260
Paul Duffinc30aea22021-06-15 19:10:11 +0100261 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
262 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
263 // apex specific variants of the exported java modules available for use from within make.
264 apexName := p.BaseModuleName()
265 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100266 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100267 entriesList = append(entriesList, entries)
268 }
269
270 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100271}
272
Paul Duffin9dc8c542021-06-17 13:33:09 +0100273// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
274func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
275 moduleName := fi.androidMkModuleName + "." + apexName
276 entries := android.AndroidMkEntries{
277 Class: fi.class.nameInMake(),
278 OverrideName: moduleName,
279 OutputFile: android.OptionalPathForPath(fi.builtFile),
280 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
281 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
282 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800283 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000284 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
285 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700286 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100287
288 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
289 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
290 // we will have foo.jar.jar
291 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100292 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
293 entries.SetString("LOCAL_DEX_PREOPT", "false")
294 },
295 },
296 ExtraFooters: []android.AndroidMkExtraFootersFunc{
297 func(w io.Writer, name, prefix, moduleDir string) {
298 // m <module_name> will build <module_name>.<apex_name> as well.
299 if fi.androidMkModuleName != moduleName {
300 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
301 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
302 }
303 },
304 },
305 }
306 return entries
307}
308
Paul Duffin5dda3e32021-05-05 14:13:27 +0100309// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
310// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
311type prebuiltApexModuleCreator interface {
312 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
313}
314
315// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
316// prebuiltApexModuleCreator's createPrebuiltApexModules method.
317//
318// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
319// will need to access dependencies added by that (exported modules) but must run before the
320// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
321// exported modules.
322func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
323 module := ctx.Module()
324 if creator, ok := module.(prebuiltApexModuleCreator); ok {
325 creator.createPrebuiltApexModules(ctx)
326 }
327}
328
Liz Kammer2dc72442023-04-20 10:10:48 -0400329func (p *prebuiltCommon) hasExportedDeps() bool {
330 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
331 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
332 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000333}
334
Paul Duffin57f83592021-05-05 15:09:44 +0100335// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
336func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
337 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100338
Liz Kammer2dc72442023-04-20 10:10:48 -0400339 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000340 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400341 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
342 }
343
344 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
345 prebuiltDep := android.PrebuiltNameFromSource(dep)
346 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
347 }
348
349 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
350 prebuiltDep := android.PrebuiltNameFromSource(dep)
351 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100352 }
Paul Duffindfd33262021-04-06 17:02:08 +0100353}
354
Paul Duffinb17d0442021-05-05 12:07:00 +0100355// Implements android.DepInInSameApex
356func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
357 tag := ctx.OtherModuleDependencyTag(dep)
358 _, ok := tag.(exportedDependencyTag)
359 return ok
360}
361
Paul Duffindfd33262021-04-06 17:02:08 +0100362// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
363// specific variant and checks that they are supported.
364//
365// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
366// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
367//
368// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
369// across prebuilt_apex modules. That is because there is no way to determine whether two
370// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
371// been built from different source at different times or they could have been built with different
372// build options that affect the libraries.
373//
374// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
375// modules were compatible it would be a lot of work and would not provide much benefit for a couple
376// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700377// - The number of prebuilt_apex modules that will be exporting files for the same module will be
378// low as the prebuilt_apex only exports files for the direct dependencies that require it and
379// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
380// few com.android.art* apex files that contain the same contents and could export files for the
381// same modules but only one of them needs to do so. Contrast that with source apex modules which
382// need apex specific variants for every module that contributes code to the apex, whether direct
383// or indirect.
384// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
385// extra copying of files. Contrast that with source apex modules that has to build each variant
386// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100387func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
388
389 // Collect direct dependencies into contents.
390 contents := make(map[string]android.ApexMembership)
391
392 // Collect the list of dependencies.
393 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100394 mctx.WalkDeps(func(child, parent android.Module) bool {
395 // If the child is not in the same apex as the parent then exit immediately and do not visit
396 // any of the child's dependencies.
397 if !android.IsDepInSameApex(mctx, parent, child) {
398 return false
399 }
400
401 tag := mctx.OtherModuleDependencyTag(child)
402 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100403 if exportedTag, ok := tag.(exportedDependencyTag); ok {
404 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100405
406 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100407 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100408 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100409 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100410 }
411
412 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100413 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100414 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100415 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100416 }
Paul Duffindfd33262021-04-06 17:02:08 +0100417 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100418
Paul Duffinfee8cf32021-06-29 18:38:38 +0100419 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
420 // variant.
421 if _, ok := child.(android.ApexModule); !ok {
422 return false
423 }
424
Paul Duffinb17d0442021-05-05 12:07:00 +0100425 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
426 // behavior whether there is a corresponding source module present or not.
427 depName = android.RemoveOptionalPrebuiltPrefix(depName)
428
429 // Remember if this module was added as a direct dependency.
430 direct := parent == mctx.Module()
431 contents[depName] = contents[depName].Add(direct)
432
433 // Add the module to the list of dependencies that need to have an APEX variant.
434 dependencies = append(dependencies, child.(android.ApexModule))
435
436 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100437 })
438
439 // Create contents for the prebuilt_apex and store it away for later use.
440 apexContents := android.NewApexContents(contents)
Colin Cross40213022023-12-13 15:19:49 -0800441 android.SetProvider(mctx, ApexBundleInfoProvider, ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100442 Contents: apexContents,
443 })
444
445 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100446 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100447 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000448 ApexVariationName: apexVariationName,
449 InApexVariants: []string{apexVariationName},
Spandan Das3576e762024-01-03 18:57:03 +0000450 InApexModules: []string{p.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100451 ApexContents: []*android.ApexContents{apexContents},
452 ForPrebuiltApex: true,
453 }
454
455 // Mark the dependencies of this module as requiring a variant for this module.
456 for _, am := range dependencies {
457 am.BuildForApex(apexInfo)
458 }
459}
460
Paul Duffin11216db2021-03-01 14:14:52 +0000461// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
462// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
463// deapexer.
464type prebuiltApexSelectorModule struct {
465 android.ModuleBase
466
467 apexFileProperties ApexFileProperties
468
469 inputApex android.Path
470}
471
472func privateApexSelectorModuleFactory() android.Module {
473 module := &prebuiltApexSelectorModule{}
474 module.AddProperties(
475 &module.apexFileProperties,
476 )
477 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
478 return module
479}
480
481func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
482 return android.Paths{p.inputApex}
483}
484
485func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
486 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
487}
488
Jiyong Park09d77522019-11-18 11:16:27 +0900489type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900490 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900491
Paul Duffinbb0dc132021-05-05 16:58:08 +0100492 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900493
Paul Duffinef6b6952021-06-15 11:34:01 +0100494 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700495
496 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900497}
498
Paul Duffin851f3992021-01-13 17:03:51 +0000499type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900500 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000501 //
502 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
503 // for android_common. That is so that it will have the same arch variant as, and so be compatible
504 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000505 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900506 Arch struct {
507 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000508 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900509 }
510 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000511 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900512 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800513 Riscv64 struct {
514 Src *string `android:"path"`
515 }
Jiyong Park09d77522019-11-18 11:16:27 +0900516 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000517 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900518 }
519 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000520 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900521 }
522 }
Paul Duffin851f3992021-01-13 17:03:51 +0000523}
524
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000525// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
526//
527// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
528// to use methods on it that are specific to the current module.
529//
530// See the ApexFileProperties.Src property.
531func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
532 multiTargets := prebuilt.MultiTargets()
533 if len(multiTargets) != 1 {
534 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
535 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000536 }
537 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000538 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000539 case android.Arm:
540 src = String(p.Arch.Arm.Src)
541 case android.Arm64:
542 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800543 case android.Riscv64:
544 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700545 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
546 if src == "" {
547 src = String(p.Arch.Arm64.Src)
548 }
Paul Duffin851f3992021-01-13 17:03:51 +0000549 case android.X86:
550 src = String(p.Arch.X86.Src)
551 case android.X86_64:
552 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000553 }
554 if src == "" {
555 src = String(p.Src)
556 }
Paul Duffin851f3992021-01-13 17:03:51 +0000557
Paul Duffinc0609c62021-03-01 17:27:16 +0000558 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700559 if ctx.Config().AllowMissingDependencies() {
560 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
561 } else {
562 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
563 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000564 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
565 // logic from reporting a more general, less useful message.
566 }
567
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000568 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000569}
570
571type PrebuiltProperties struct {
572 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900573
Paul Duffinef6b6952021-06-15 11:34:01 +0100574 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900575}
576
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700577func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
578 return false
579}
580
Jiyong Park09d77522019-11-18 11:16:27 +0900581func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
582 switch tag {
583 case "":
584 return android.Paths{p.outputApex}, nil
585 default:
586 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
587 }
588}
589
Jiyong Park09d77522019-11-18 11:16:27 +0900590// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
591func PrebuiltFactory() android.Module {
592 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100593 module.AddProperties(&module.properties)
594 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000595
Jiyong Park09d77522019-11-18 11:16:27 +0900596 return module
597}
598
Paul Duffin5dda3e32021-05-05 14:13:27 +0100599func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000600 props := struct {
601 Name *string
602 }{
603 Name: proptools.StringPtr(name),
604 }
605
606 ctx.CreateModule(privateApexSelectorModuleFactory,
607 &props,
608 apexFileProperties,
609 )
610}
611
Paul Duffin5dda3e32021-05-05 14:13:27 +0100612// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
613//
Paul Duffin57f83592021-05-05 15:09:44 +0100614// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
615// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
616// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000617func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100618 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400619 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100620 return
621 }
622
Paul Duffin57f83592021-05-05 15:09:44 +0100623 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100624 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100625 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100626 ctx.WalkDeps(func(child, parent android.Module) bool {
627 tag := ctx.OtherModuleDependencyTag(child)
628
Paul Duffin7db57e02021-06-17 14:56:05 +0100629 // If the child is not in the same apex as the parent then ignore it and all its children.
630 if !android.IsDepInSameApex(ctx, parent, child) {
631 return false
632 }
633
Paul Duffin57f83592021-05-05 15:09:44 +0100634 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100635 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100636 commonModules = append(commonModules, name)
637
638 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100639 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100640
Paul Duffin7db57e02021-06-17 14:56:05 +0100641 // Visit the dependencies of this module just in case they also require files from the
642 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100643 return true
644 }
645
646 return false
647 })
648
Paul Duffin3bae0682021-05-05 18:03:47 +0100649 // Create properties for deapexer module.
650 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100651 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100652 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100653 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100654 }
655
656 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100657 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100658
Paul Duffin11216db2021-03-01 14:14:52 +0000659 props := struct {
660 Name *string
661 Selected_apex *string
662 }{
663 Name: proptools.StringPtr(deapexerName),
664 Selected_apex: proptools.StringPtr(apexFileSource),
665 }
666 ctx.CreateModule(privateDeapexerFactory,
667 &props,
668 deapexerProperties,
669 )
670}
671
Paul Duffin11216db2021-03-01 14:14:52 +0000672func apexSelectorModuleName(baseModuleName string) string {
673 return baseModuleName + ".apex.selector"
674}
675
Paul Duffin064b70c2020-11-02 17:32:38 +0000676func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
677 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
678 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
679 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
680 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
681 // and not a renamed prebuilt module then that will be detected and reported as an error when
682 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100683 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000684 if ctx.OtherModuleExists(prebuiltName) {
685 name = prebuiltName
686 }
687 return name
688}
689
Paul Duffina7139422021-02-08 11:01:58 +0000690type exportedDependencyTag struct {
691 blueprint.BaseDependencyTag
692 name string
693}
694
695// Mark this tag so dependencies that use it are excluded from visibility enforcement.
696//
697// This does allow any prebuilt_apex to reference any module which does open up a small window for
698// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
699// avoids opening up a much bigger window by widening the visibility of modules that need files
700// provided by the prebuilt_apex to include all the possible locations they may be defined, which
701// could include everything below vendor/.
702//
703// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
704// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
705// the .apex file. It will also have to be included in the module's apex_available property too.
706// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
707// incorrectly.
708func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
709
Paul Duffin7db57e02021-06-17 14:56:05 +0100710func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
711
712var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
713
Paul Duffina7139422021-02-08 11:01:58 +0000714var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000715 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
716 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
717 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000718)
719
Paul Duffin5dda3e32021-05-05 14:13:27 +0100720var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
721
722// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
723// build.
724//
725// If this needs to make files from within a `.apex` file available for use by other Soong modules,
726// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
727// it does so as follows:
728//
Colin Crossd079e0b2022-08-16 10:27:33 -0700729// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
730// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100731//
Colin Crossd079e0b2022-08-16 10:27:33 -0700732// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
733// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
734// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100735//
Colin Crossd079e0b2022-08-16 10:27:33 -0700736// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
737// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100738//
739// It also creates a child module `selector` that is responsible for selecting the appropriate
740// 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 +0100741//
Colin Crossd079e0b2022-08-16 10:27:33 -0700742// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100743//
Colin Crossd079e0b2022-08-16 10:27:33 -0700744// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
745// `apex_set`.
746//
747// prebuilt_apex
748// / | \
749// / | \
750// V V V
751// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100752func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +0000753 apexSelectorModuleName := apexSelectorModuleName(p.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +0100754 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
755
756 apexFileSource := ":" + apexSelectorModuleName
Spandan Das3576e762024-01-03 18:57:03 +0000757 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(p.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100758
759 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100760 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100761}
762
Paul Duffin57f83592021-05-05 15:09:44 +0100763func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
764 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000765}
766
Spandan Das2069c3f2023-12-06 19:40:24 +0000767func (p *prebuiltCommon) DepsMutator(ctx android.BottomUpMutatorContext) {
768 if p.hasExportedDeps() {
769 // Create a dependency from the prebuilt apex (prebuilt_apex/apex_set) to the internal deapexer module
770 // 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 +0000771 ctx.AddDependency(ctx.Module(), android.DeapexerTag, deapexerModuleName(p.Name()))
Spandan Das2069c3f2023-12-06 19:40:24 +0000772 }
773}
774
Paul Duffin064b70c2020-11-02 17:32:38 +0000775var _ ApexInfoMutator = (*Prebuilt)(nil)
776
Paul Duffin064b70c2020-11-02 17:32:38 +0000777func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100778 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900779}
780
Spandan Dasda739a32023-12-13 00:06:32 +0000781// Set a provider containing information about the jars and .prof provided by the apex
782// Apexes built from prebuilts retrieve this information by visiting its internal deapexer module
783// Used by dex_bootjars to generate the boot image
784func (p *prebuiltCommon) provideApexExportsInfo(ctx android.ModuleContext) {
785 if !p.hasExportedDeps() {
786 // nothing to do
787 return
788 }
789 if di, err := android.FindDeapexerProviderForModule(ctx); err == nil {
Spandan Das5be63332023-12-13 00:06:32 +0000790 javaModuleToDexPath := map[string]android.Path{}
791 for _, commonModule := range di.GetExportedModuleNames() {
792 if dex := di.PrebuiltExportPath(java.ApexRootRelativePathToJavaLib(commonModule)); dex != nil {
793 javaModuleToDexPath[commonModule] = dex
794 }
795 }
796
Spandan Dasda739a32023-12-13 00:06:32 +0000797 exports := android.ApexExportsInfo{
Spandan Das5be63332023-12-13 00:06:32 +0000798 ApexName: p.ApexVariationName(),
799 ProfilePathOnHost: di.PrebuiltExportPath(java.ProfileInstallPathInApex),
800 LibraryNameToDexJarPathOnHost: javaModuleToDexPath,
Spandan Dasda739a32023-12-13 00:06:32 +0000801 }
Spandan Dasa41b8ec2023-12-22 00:05:04 +0000802 android.SetProvider(ctx, android.ApexExportsInfoProvider, exports)
Spandan Dasda739a32023-12-13 00:06:32 +0000803 } else {
804 ctx.ModuleErrorf(err.Error())
805 }
806}
807
Jiyong Park09d77522019-11-18 11:16:27 +0900808func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900809 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900810 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100811 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900812 p.installDir = android.PathForModuleInstall(ctx, "apex")
813 p.installFilename = p.InstallFilename()
814 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
815 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
816 }
817 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
818 ctx.Build(pctx, android.BuildParams{
819 Rule: android.Cp,
820 Input: p.inputApex,
821 Output: p.outputApex,
822 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900823
824 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800825 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900826 return
827 }
828
Spandan Das2069c3f2023-12-06 19:40:24 +0000829 // dexpreopt any system server jars if present
830 p.dexpreoptSystemServerJars(ctx)
831
Spandan Dasda739a32023-12-13 00:06:32 +0000832 // provide info used for generating the boot image
833 p.provideApexExportsInfo(ctx)
834
Paul Duffinc30aea22021-06-15 19:10:11 +0100835 // Save the files that need to be made available to Make.
836 p.initApexFilesForAndroidMk(ctx)
837
Colin Crossccba23d2021-11-12 19:01:29 +0000838 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900839 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000840 // or that prebuilt_apex overrides other apexes (using overrides: prop)
841 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900842 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700843 }
844
845 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800846 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700847 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900848 }
Jiyong Park09d77522019-11-18 11:16:27 +0900849}
850
Wei Li340ee8e2022-03-18 17:33:24 -0700851func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
852 return p.provenanceMetaDataFile
853}
854
Paul Duffin24704672021-04-06 16:09:30 +0100855// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
856// module. It extracts the correct apex to use and makes it available for use by apex_set.
857type prebuiltApexExtractorModule struct {
858 android.ModuleBase
859
860 properties ApexExtractorProperties
861
862 extractedApex android.WritablePath
863}
864
865func privateApexExtractorModuleFactory() android.Module {
866 module := &prebuiltApexExtractorModule{}
867 module.AddProperties(
868 &module.properties,
869 )
870 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
871 return module
872}
873
874func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
875 return android.Paths{p.extractedApex}
876}
877
878func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
879 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
880 return p.properties.prebuiltSrcs(ctx)
881 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000882 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100883 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
884 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000885 // Filter out NativeBridge archs (b/260115309)
886 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100887 ctx.Build(pctx,
888 android.BuildParams{
889 Rule: extractMatchingApex,
890 Description: "Extract an apex from an apex set",
891 Inputs: android.Paths{apexSet},
892 Output: p.extractedApex,
893 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000894 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000895 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100896 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700897 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100898 },
899 })
900}
901
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700902type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900903 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700904
905 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700906}
907
Paul Duffin24704672021-04-06 16:09:30 +0100908type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700909 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000910 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700911
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700912 Sanitized struct {
913 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000914 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700915 }
916 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000917 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700918 }
919 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000920 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700921 }
922 }
923
Paul Duffin24704672021-04-06 16:09:30 +0100924 // apexes in this set use prerelease SDK version
925 Prerelease *bool
926}
927
928func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
929 var srcs []string
930 if e.Set != nil {
931 srcs = append(srcs, *e.Set)
932 }
933
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900934 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100935
936 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
937 srcs = append(srcs, *e.Sanitized.Address.Set)
938 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
939 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
940 } else if e.Sanitized.None.Set != nil {
941 srcs = append(srcs, *e.Sanitized.None.Set)
942 }
943
944 return srcs
945}
946
947type ApexSetProperties struct {
948 ApexExtractorProperties
949
Paul Duffinef6b6952021-06-15 11:34:01 +0100950 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700951}
952
953func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
954 if sanitizer == "address" {
955 return a.properties.Sanitized.Address.Set != nil
956 }
957 if sanitizer == "hwaddress" {
958 return a.properties.Sanitized.Hwaddress.Set != nil
959 }
960
961 return false
962}
963
Paul Duffin4f2282c2022-12-12 17:44:33 +0000964func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
965 switch tag {
966 case "":
967 return android.Paths{a.outputApex}, nil
968 default:
969 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
970 }
971}
972
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700973// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
974func apexSetFactory() android.Module {
975 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100976 module.AddProperties(&module.properties)
977 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100978
Paul Duffin24704672021-04-06 16:09:30 +0100979 return module
980}
981
Paul Duffin5dda3e32021-05-05 14:13:27 +0100982func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100983 props := struct {
984 Name *string
985 }{
986 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700987 }
988
Paul Duffin24704672021-04-06 16:09:30 +0100989 ctx.CreateModule(privateApexExtractorModuleFactory,
990 &props,
991 apexExtractorProperties,
992 )
993}
994
995func apexExtractorModuleName(baseModuleName string) string {
996 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700997}
998
Paul Duffin5dda3e32021-05-05 14:13:27 +0100999var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
1000
1001// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
1002// modules.
1003//
1004// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
1005// prebuilt_apex except that instead of creating a selector module which selects one .apex file
1006// from those provided this creates an extractor module which extracts the appropriate .apex file
1007// from the zip file containing them.
1008func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
Spandan Das3576e762024-01-03 18:57:03 +00001009 apexExtractorModuleName := apexExtractorModuleName(a.Name())
Paul Duffin5dda3e32021-05-05 14:13:27 +01001010 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
1011
1012 apexFileSource := ":" + apexExtractorModuleName
Spandan Das3576e762024-01-03 18:57:03 +00001013 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(a.Name()), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001014
1015 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +01001016 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +01001017}
1018
Paul Duffin57f83592021-05-05 15:09:44 +01001019func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
1020 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +01001021}
1022
1023var _ ApexInfoMutator = (*ApexSet)(nil)
1024
1025func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
1026 a.apexInfoMutator(mctx)
1027}
1028
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001029func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +09001030 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001031 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +01001032 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
1033 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001034 }
1035
Paul Duffinbb0dc132021-05-05 16:58:08 +01001036 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001037 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +01001038 ctx.Build(pctx, android.BuildParams{
1039 Rule: android.Cp,
1040 Input: inputApex,
1041 Output: a.outputApex,
1042 })
Jiyong Park10e926b2020-07-16 21:38:56 +09001043
1044 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001045 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +09001046 return
1047 }
1048
Spandan Das2069c3f2023-12-06 19:40:24 +00001049 // dexpreopt any system server jars if present
1050 a.dexpreoptSystemServerJars(ctx)
1051
Spandan Dasda739a32023-12-13 00:06:32 +00001052 // provide info used for generating the boot image
1053 a.provideApexExportsInfo(ctx)
1054
Paul Duffinc30aea22021-06-15 19:10:11 +01001055 // Save the files that need to be made available to Make.
1056 a.initApexFilesForAndroidMk(ctx)
1057
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001058 a.installDir = android.PathForModuleInstall(ctx, "apex")
1059 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001060 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001061 }
1062
1063 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001064 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001065 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001066 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001067 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001068 }
1069}
1070
Paul Duffinef6b6952021-06-15 11:34:01 +01001071type systemExtContext struct {
1072 android.ModuleContext
1073}
1074
1075func (*systemExtContext) SystemExtSpecific() bool {
1076 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001077}