blob: 4c44678e7c332a70a9d31bca7c70c9c77a2a3c1b [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
Jiyong Park10e926b2020-07-16 21:38:56 +090090 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010091
Paul Duffinef6b6952021-06-15 11:34:01 +010092 // whether the extracted apex file is installable.
93 Installable *bool
94
95 // optional name for the installed apex. If unspecified, name of the
96 // module is used as the file name
97 Filename *string
98
99 // names of modules to be overridden. Listed modules can only be other binaries
100 // (in Make or Soong).
101 // This does not completely prevent installation of the overridden binaries, but if both
102 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
103 // from PRODUCT_PACKAGES.
104 Overrides []string
105
Paul Duffin3bae0682021-05-05 18:03:47 +0100106 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
107 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
108 // dexpreopt and boot jars package check.
109 Exported_java_libs []string
110
111 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
112 // bundle will create an APEX variant.
113 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000114
115 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
116 // APEX bundle will create an APEX variant.
117 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900118}
119
Paul Duffinef6b6952021-06-15 11:34:01 +0100120// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
121// module that is common to Prebuilt and ApexSet.
122func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
123 p.prebuiltCommonProperties = properties
124 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
125 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
126}
127
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100128func (p *prebuiltCommon) ApexVariationName() string {
129 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
130}
131
Jiyong Park10e926b2020-07-16 21:38:56 +0900132func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
133 return &p.prebuilt
134}
135
136func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100137 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900138}
139
140func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900141 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900142
143 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
144 // to build the prebuilts themselves.
145 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
146
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700147 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
148 sanitized := ctx.Module().(sanitizedPrebuilt)
149 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
150 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900151
152 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100153 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900154 return true
155 }
156 return false
157}
158
Paul Duffinef6b6952021-06-15 11:34:01 +0100159func (p *prebuiltCommon) InstallFilename() string {
160 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
161}
162
163func (p *prebuiltCommon) Name() string {
164 return p.prebuilt.Name(p.ModuleBase.Name())
165}
166
167func (p *prebuiltCommon) Overrides() []string {
168 return p.prebuiltCommonProperties.Overrides
169}
170
171func (p *prebuiltCommon) installable() bool {
172 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
173}
174
Spandan Das2069c3f2023-12-06 19:40:24 +0000175// To satisfy java.DexpreopterInterface
176func (p *prebuiltCommon) IsInstallable() bool {
177 return p.installable()
178}
179
180// initApexFilesForAndroidMk initializes the prebuiltCommon.requiredModuleNames field with the install only deps of the prebuilt apex
Paul Duffinc30aea22021-06-15 19:10:11 +0100181func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
Spandan Das2069c3f2023-12-06 19:40:24 +0000182 // If this apex contains a system server jar, then the dexpreopt artifacts should be added as required
183 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
184 p.requiredModuleNames = append(p.requiredModuleNames, install.FullModuleName())
185 }
186}
Paul Duffinc30aea22021-06-15 19:10:11 +0100187
Spandan Das2069c3f2023-12-06 19:40:24 +0000188// If this prebuilt has system server jar, create the rules to dexpreopt it and install it alongside the prebuilt apex
189func (p *prebuiltCommon) dexpreoptSystemServerJars(ctx android.ModuleContext) {
190 // If this apex does not export anything, return
191 if !p.hasExportedDeps() {
192 return
193 }
194 // Use apex_name to determine the api domain of this prebuilt apex
195 apexName := p.ApexVariationName()
196 di := android.FindDeapexerProviderForModule(ctx)
197 dc := dexpreopt.GetGlobalConfig(ctx)
198 systemServerJarList := dc.AllApexSystemServerJars(ctx)
199
200 for i := 0; i < systemServerJarList.Len(); i++ {
201 sscpApex := systemServerJarList.Apex(i)
202 sscpJar := systemServerJarList.Jar(i)
203 if apexName != sscpApex {
204 continue
Paul Duffinc30aea22021-06-15 19:10:11 +0100205 }
Spandan Das2069c3f2023-12-06 19:40:24 +0000206 p.Dexpreopter.DexpreoptPrebuiltApexSystemServerJars(ctx, sscpJar, di)
207 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100208}
209
Jiakai Zhang204356f2021-09-09 08:12:46 +0000210func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
211 for _, fi := range p.apexFilesForAndroidMk {
212 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
213 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
214 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
215 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000216 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000217}
218
Paul Duffinef6b6952021-06-15 11:34:01 +0100219func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100220 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100221 {
222 Class: "ETC",
223 OutputFile: android.OptionalPathForPath(p.outputApex),
224 Include: "$(BUILD_PREBUILT)",
225 Host_required: p.hostRequired,
226 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
227 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800228 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100229 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700230 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
231 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700232 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100233 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
234 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900235 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000236 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100237 },
238 },
239 },
240 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100241
Spandan Das2069c3f2023-12-06 19:40:24 +0000242 // Add the dexpreopt artifacts to androidmk
243 for _, install := range p.Dexpreopter.DexpreoptBuiltInstalledForApex() {
244 entriesList = append(entriesList, install.ToMakeEntries())
245 }
246
Paul Duffinc30aea22021-06-15 19:10:11 +0100247 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
248 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
249 // apex specific variants of the exported java modules available for use from within make.
250 apexName := p.BaseModuleName()
251 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100252 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100253 entriesList = append(entriesList, entries)
254 }
255
256 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100257}
258
Paul Duffin9dc8c542021-06-17 13:33:09 +0100259// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
260func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
261 moduleName := fi.androidMkModuleName + "." + apexName
262 entries := android.AndroidMkEntries{
263 Class: fi.class.nameInMake(),
264 OverrideName: moduleName,
265 OutputFile: android.OptionalPathForPath(fi.builtFile),
266 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
267 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
268 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800269 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000270 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
271 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700272 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100273
274 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
275 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
276 // we will have foo.jar.jar
277 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100278 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
279 entries.SetString("LOCAL_DEX_PREOPT", "false")
280 },
281 },
282 ExtraFooters: []android.AndroidMkExtraFootersFunc{
283 func(w io.Writer, name, prefix, moduleDir string) {
284 // m <module_name> will build <module_name>.<apex_name> as well.
285 if fi.androidMkModuleName != moduleName {
286 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
287 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
288 }
289 },
290 },
291 }
292 return entries
293}
294
Paul Duffin5dda3e32021-05-05 14:13:27 +0100295// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
296// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
297type prebuiltApexModuleCreator interface {
298 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
299}
300
301// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
302// prebuiltApexModuleCreator's createPrebuiltApexModules method.
303//
304// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
305// will need to access dependencies added by that (exported modules) but must run before the
306// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
307// exported modules.
308func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
309 module := ctx.Module()
310 if creator, ok := module.(prebuiltApexModuleCreator); ok {
311 creator.createPrebuiltApexModules(ctx)
312 }
313}
314
Liz Kammer2dc72442023-04-20 10:10:48 -0400315func (p *prebuiltCommon) hasExportedDeps() bool {
316 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
317 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
318 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000319}
320
Paul Duffin57f83592021-05-05 15:09:44 +0100321// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
322func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
323 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100324
Liz Kammer2dc72442023-04-20 10:10:48 -0400325 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000326 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400327 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
328 }
329
330 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
331 prebuiltDep := android.PrebuiltNameFromSource(dep)
332 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
333 }
334
335 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
336 prebuiltDep := android.PrebuiltNameFromSource(dep)
337 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100338 }
Paul Duffindfd33262021-04-06 17:02:08 +0100339}
340
Paul Duffinb17d0442021-05-05 12:07:00 +0100341// Implements android.DepInInSameApex
342func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
343 tag := ctx.OtherModuleDependencyTag(dep)
344 _, ok := tag.(exportedDependencyTag)
345 return ok
346}
347
Paul Duffindfd33262021-04-06 17:02:08 +0100348// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
349// specific variant and checks that they are supported.
350//
351// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
352// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
353//
354// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
355// across prebuilt_apex modules. That is because there is no way to determine whether two
356// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
357// been built from different source at different times or they could have been built with different
358// build options that affect the libraries.
359//
360// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
361// modules were compatible it would be a lot of work and would not provide much benefit for a couple
362// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700363// - The number of prebuilt_apex modules that will be exporting files for the same module will be
364// low as the prebuilt_apex only exports files for the direct dependencies that require it and
365// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
366// few com.android.art* apex files that contain the same contents and could export files for the
367// same modules but only one of them needs to do so. Contrast that with source apex modules which
368// need apex specific variants for every module that contributes code to the apex, whether direct
369// or indirect.
370// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
371// extra copying of files. Contrast that with source apex modules that has to build each variant
372// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100373func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
374
375 // Collect direct dependencies into contents.
376 contents := make(map[string]android.ApexMembership)
377
378 // Collect the list of dependencies.
379 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100380 mctx.WalkDeps(func(child, parent android.Module) bool {
381 // If the child is not in the same apex as the parent then exit immediately and do not visit
382 // any of the child's dependencies.
383 if !android.IsDepInSameApex(mctx, parent, child) {
384 return false
385 }
386
387 tag := mctx.OtherModuleDependencyTag(child)
388 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100389 if exportedTag, ok := tag.(exportedDependencyTag); ok {
390 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100391
392 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100393 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100394 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100395 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100396 }
397
398 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100399 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100400 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100401 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100402 }
Paul Duffindfd33262021-04-06 17:02:08 +0100403 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100404
Paul Duffinfee8cf32021-06-29 18:38:38 +0100405 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
406 // variant.
407 if _, ok := child.(android.ApexModule); !ok {
408 return false
409 }
410
Paul Duffinb17d0442021-05-05 12:07:00 +0100411 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
412 // behavior whether there is a corresponding source module present or not.
413 depName = android.RemoveOptionalPrebuiltPrefix(depName)
414
415 // Remember if this module was added as a direct dependency.
416 direct := parent == mctx.Module()
417 contents[depName] = contents[depName].Add(direct)
418
419 // Add the module to the list of dependencies that need to have an APEX variant.
420 dependencies = append(dependencies, child.(android.ApexModule))
421
422 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100423 })
424
425 // Create contents for the prebuilt_apex and store it away for later use.
426 apexContents := android.NewApexContents(contents)
Colin Cross40213022023-12-13 15:19:49 -0800427 android.SetProvider(mctx, ApexBundleInfoProvider, ApexBundleInfo{
Paul Duffindfd33262021-04-06 17:02:08 +0100428 Contents: apexContents,
429 })
430
431 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100432 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100433 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000434 ApexVariationName: apexVariationName,
435 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100436 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100437 ApexContents: []*android.ApexContents{apexContents},
438 ForPrebuiltApex: true,
439 }
440
441 // Mark the dependencies of this module as requiring a variant for this module.
442 for _, am := range dependencies {
443 am.BuildForApex(apexInfo)
444 }
445}
446
Paul Duffin11216db2021-03-01 14:14:52 +0000447// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
448// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
449// deapexer.
450type prebuiltApexSelectorModule struct {
451 android.ModuleBase
452
453 apexFileProperties ApexFileProperties
454
455 inputApex android.Path
456}
457
458func privateApexSelectorModuleFactory() android.Module {
459 module := &prebuiltApexSelectorModule{}
460 module.AddProperties(
461 &module.apexFileProperties,
462 )
463 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
464 return module
465}
466
467func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
468 return android.Paths{p.inputApex}
469}
470
471func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
472 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
473}
474
Jiyong Park09d77522019-11-18 11:16:27 +0900475type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900476 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900477
Paul Duffinbb0dc132021-05-05 16:58:08 +0100478 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900479
Paul Duffinef6b6952021-06-15 11:34:01 +0100480 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700481
482 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900483}
484
Paul Duffin851f3992021-01-13 17:03:51 +0000485type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900486 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000487 //
488 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
489 // for android_common. That is so that it will have the same arch variant as, and so be compatible
490 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000491 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900492 Arch struct {
493 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000494 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900495 }
496 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000497 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900498 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800499 Riscv64 struct {
500 Src *string `android:"path"`
501 }
Jiyong Park09d77522019-11-18 11:16:27 +0900502 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000503 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900504 }
505 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000506 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900507 }
508 }
Paul Duffin851f3992021-01-13 17:03:51 +0000509}
510
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000511// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
512//
513// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
514// to use methods on it that are specific to the current module.
515//
516// See the ApexFileProperties.Src property.
517func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
518 multiTargets := prebuilt.MultiTargets()
519 if len(multiTargets) != 1 {
520 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
521 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000522 }
523 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000524 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000525 case android.Arm:
526 src = String(p.Arch.Arm.Src)
527 case android.Arm64:
528 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800529 case android.Riscv64:
530 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700531 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
532 if src == "" {
533 src = String(p.Arch.Arm64.Src)
534 }
Paul Duffin851f3992021-01-13 17:03:51 +0000535 case android.X86:
536 src = String(p.Arch.X86.Src)
537 case android.X86_64:
538 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000539 }
540 if src == "" {
541 src = String(p.Src)
542 }
Paul Duffin851f3992021-01-13 17:03:51 +0000543
Paul Duffinc0609c62021-03-01 17:27:16 +0000544 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700545 if ctx.Config().AllowMissingDependencies() {
546 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
547 } else {
548 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
549 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000550 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
551 // logic from reporting a more general, less useful message.
552 }
553
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000554 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000555}
556
557type PrebuiltProperties struct {
558 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900559
Paul Duffinef6b6952021-06-15 11:34:01 +0100560 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900561}
562
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700563func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
564 return false
565}
566
Jiyong Park09d77522019-11-18 11:16:27 +0900567func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
568 switch tag {
569 case "":
570 return android.Paths{p.outputApex}, nil
571 default:
572 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
573 }
574}
575
Jiyong Park09d77522019-11-18 11:16:27 +0900576// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
577func PrebuiltFactory() android.Module {
578 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100579 module.AddProperties(&module.properties)
580 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000581
Jiyong Park09d77522019-11-18 11:16:27 +0900582 return module
583}
584
Paul Duffin5dda3e32021-05-05 14:13:27 +0100585func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000586 props := struct {
587 Name *string
588 }{
589 Name: proptools.StringPtr(name),
590 }
591
592 ctx.CreateModule(privateApexSelectorModuleFactory,
593 &props,
594 apexFileProperties,
595 )
596}
597
Paul Duffin5dda3e32021-05-05 14:13:27 +0100598// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
599//
Paul Duffin57f83592021-05-05 15:09:44 +0100600// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
601// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
602// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000603func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100604 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400605 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100606 return
607 }
608
Paul Duffin57f83592021-05-05 15:09:44 +0100609 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100610 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100611 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100612 ctx.WalkDeps(func(child, parent android.Module) bool {
613 tag := ctx.OtherModuleDependencyTag(child)
614
Paul Duffin7db57e02021-06-17 14:56:05 +0100615 // If the child is not in the same apex as the parent then ignore it and all its children.
616 if !android.IsDepInSameApex(ctx, parent, child) {
617 return false
618 }
619
Paul Duffin57f83592021-05-05 15:09:44 +0100620 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100621 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100622 commonModules = append(commonModules, name)
623
624 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100625 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100626
Paul Duffin7db57e02021-06-17 14:56:05 +0100627 // Visit the dependencies of this module just in case they also require files from the
628 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100629 return true
630 }
631
632 return false
633 })
634
Paul Duffin3bae0682021-05-05 18:03:47 +0100635 // Create properties for deapexer module.
636 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100637 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100638 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100639 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100640 }
641
642 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100643 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100644
Paul Duffin11216db2021-03-01 14:14:52 +0000645 props := struct {
646 Name *string
647 Selected_apex *string
648 }{
649 Name: proptools.StringPtr(deapexerName),
650 Selected_apex: proptools.StringPtr(apexFileSource),
651 }
652 ctx.CreateModule(privateDeapexerFactory,
653 &props,
654 deapexerProperties,
655 )
656}
657
Paul Duffin11216db2021-03-01 14:14:52 +0000658func apexSelectorModuleName(baseModuleName string) string {
659 return baseModuleName + ".apex.selector"
660}
661
Paul Duffin064b70c2020-11-02 17:32:38 +0000662func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
663 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
664 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
665 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
666 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
667 // and not a renamed prebuilt module then that will be detected and reported as an error when
668 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100669 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000670 if ctx.OtherModuleExists(prebuiltName) {
671 name = prebuiltName
672 }
673 return name
674}
675
Paul Duffina7139422021-02-08 11:01:58 +0000676type exportedDependencyTag struct {
677 blueprint.BaseDependencyTag
678 name string
679}
680
681// Mark this tag so dependencies that use it are excluded from visibility enforcement.
682//
683// This does allow any prebuilt_apex to reference any module which does open up a small window for
684// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
685// avoids opening up a much bigger window by widening the visibility of modules that need files
686// provided by the prebuilt_apex to include all the possible locations they may be defined, which
687// could include everything below vendor/.
688//
689// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
690// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
691// the .apex file. It will also have to be included in the module's apex_available property too.
692// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
693// incorrectly.
694func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
695
Paul Duffin7db57e02021-06-17 14:56:05 +0100696func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
697
698var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
699
Paul Duffina7139422021-02-08 11:01:58 +0000700var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000701 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
702 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
703 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000704)
705
Paul Duffin5dda3e32021-05-05 14:13:27 +0100706var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
707
708// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
709// build.
710//
711// If this needs to make files from within a `.apex` file available for use by other Soong modules,
712// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
713// it does so as follows:
714//
Colin Crossd079e0b2022-08-16 10:27:33 -0700715// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
716// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100717//
Colin Crossd079e0b2022-08-16 10:27:33 -0700718// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
719// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
720// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100721//
Colin Crossd079e0b2022-08-16 10:27:33 -0700722// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
723// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100724//
725// It also creates a child module `selector` that is responsible for selecting the appropriate
726// 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 +0100727//
Colin Crossd079e0b2022-08-16 10:27:33 -0700728// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100729//
Colin Crossd079e0b2022-08-16 10:27:33 -0700730// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
731// `apex_set`.
732//
733// prebuilt_apex
734// / | \
735// / | \
736// V V V
737// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100738func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
739 baseModuleName := p.BaseModuleName()
740
741 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
742 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
743
744 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000745 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100746
747 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100748 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100749}
750
Paul Duffin57f83592021-05-05 15:09:44 +0100751func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
752 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000753}
754
Spandan Das2069c3f2023-12-06 19:40:24 +0000755func (p *prebuiltCommon) DepsMutator(ctx android.BottomUpMutatorContext) {
756 if p.hasExportedDeps() {
757 // Create a dependency from the prebuilt apex (prebuilt_apex/apex_set) to the internal deapexer module
758 // The deapexer will return a provider that will be bubbled up to the rdeps of apexes (e.g. dex_bootjars)
759 ctx.AddDependency(ctx.Module(), android.DeapexerTag, deapexerModuleName(p.BaseModuleName()))
760 }
761}
762
Paul Duffin064b70c2020-11-02 17:32:38 +0000763var _ ApexInfoMutator = (*Prebuilt)(nil)
764
Paul Duffin064b70c2020-11-02 17:32:38 +0000765func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100766 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900767}
768
769func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900770 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900771 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100772 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900773 p.installDir = android.PathForModuleInstall(ctx, "apex")
774 p.installFilename = p.InstallFilename()
775 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
776 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
777 }
778 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
779 ctx.Build(pctx, android.BuildParams{
780 Rule: android.Cp,
781 Input: p.inputApex,
782 Output: p.outputApex,
783 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900784
785 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800786 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900787 return
788 }
789
Spandan Das2069c3f2023-12-06 19:40:24 +0000790 // dexpreopt any system server jars if present
791 p.dexpreoptSystemServerJars(ctx)
792
Paul Duffinc30aea22021-06-15 19:10:11 +0100793 // Save the files that need to be made available to Make.
794 p.initApexFilesForAndroidMk(ctx)
795
Colin Crossccba23d2021-11-12 19:01:29 +0000796 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900797 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000798 // or that prebuilt_apex overrides other apexes (using overrides: prop)
799 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900800 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700801 }
802
803 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800804 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700805 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900806 }
Jiyong Park09d77522019-11-18 11:16:27 +0900807}
808
Wei Li340ee8e2022-03-18 17:33:24 -0700809func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
810 return p.provenanceMetaDataFile
811}
812
Paul Duffin24704672021-04-06 16:09:30 +0100813// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
814// module. It extracts the correct apex to use and makes it available for use by apex_set.
815type prebuiltApexExtractorModule struct {
816 android.ModuleBase
817
818 properties ApexExtractorProperties
819
820 extractedApex android.WritablePath
821}
822
823func privateApexExtractorModuleFactory() android.Module {
824 module := &prebuiltApexExtractorModule{}
825 module.AddProperties(
826 &module.properties,
827 )
828 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
829 return module
830}
831
832func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
833 return android.Paths{p.extractedApex}
834}
835
836func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
837 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
838 return p.properties.prebuiltSrcs(ctx)
839 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000840 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100841 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
842 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000843 // Filter out NativeBridge archs (b/260115309)
844 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100845 ctx.Build(pctx,
846 android.BuildParams{
847 Rule: extractMatchingApex,
848 Description: "Extract an apex from an apex set",
849 Inputs: android.Paths{apexSet},
850 Output: p.extractedApex,
851 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000852 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000853 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100854 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700855 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100856 },
857 })
858}
859
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700860type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900861 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700862
863 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700864}
865
Paul Duffin24704672021-04-06 16:09:30 +0100866type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700867 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000868 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700869
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700870 Sanitized struct {
871 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000872 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700873 }
874 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000875 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700876 }
877 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000878 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700879 }
880 }
881
Paul Duffin24704672021-04-06 16:09:30 +0100882 // apexes in this set use prerelease SDK version
883 Prerelease *bool
884}
885
886func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
887 var srcs []string
888 if e.Set != nil {
889 srcs = append(srcs, *e.Set)
890 }
891
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900892 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100893
894 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
895 srcs = append(srcs, *e.Sanitized.Address.Set)
896 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
897 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
898 } else if e.Sanitized.None.Set != nil {
899 srcs = append(srcs, *e.Sanitized.None.Set)
900 }
901
902 return srcs
903}
904
905type ApexSetProperties struct {
906 ApexExtractorProperties
907
Paul Duffinef6b6952021-06-15 11:34:01 +0100908 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700909}
910
911func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
912 if sanitizer == "address" {
913 return a.properties.Sanitized.Address.Set != nil
914 }
915 if sanitizer == "hwaddress" {
916 return a.properties.Sanitized.Hwaddress.Set != nil
917 }
918
919 return false
920}
921
Paul Duffin4f2282c2022-12-12 17:44:33 +0000922func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
923 switch tag {
924 case "":
925 return android.Paths{a.outputApex}, nil
926 default:
927 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
928 }
929}
930
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700931// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
932func apexSetFactory() android.Module {
933 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100934 module.AddProperties(&module.properties)
935 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100936
Paul Duffin24704672021-04-06 16:09:30 +0100937 return module
938}
939
Paul Duffin5dda3e32021-05-05 14:13:27 +0100940func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100941 props := struct {
942 Name *string
943 }{
944 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700945 }
946
Paul Duffin24704672021-04-06 16:09:30 +0100947 ctx.CreateModule(privateApexExtractorModuleFactory,
948 &props,
949 apexExtractorProperties,
950 )
951}
952
953func apexExtractorModuleName(baseModuleName string) string {
954 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700955}
956
Paul Duffin5dda3e32021-05-05 14:13:27 +0100957var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
958
959// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
960// modules.
961//
962// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
963// prebuilt_apex except that instead of creating a selector module which selects one .apex file
964// from those provided this creates an extractor module which extracts the appropriate .apex file
965// from the zip file containing them.
966func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
967 baseModuleName := a.BaseModuleName()
968
969 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
970 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
971
972 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000973 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100974
975 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100976 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100977}
978
Paul Duffin57f83592021-05-05 15:09:44 +0100979func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
980 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100981}
982
983var _ ApexInfoMutator = (*ApexSet)(nil)
984
985func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
986 a.apexInfoMutator(mctx)
987}
988
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700989func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900990 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700991 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100992 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
993 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700994 }
995
Paul Duffinbb0dc132021-05-05 16:58:08 +0100996 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700997 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100998 ctx.Build(pctx, android.BuildParams{
999 Rule: android.Cp,
1000 Input: inputApex,
1001 Output: a.outputApex,
1002 })
Jiyong Park10e926b2020-07-16 21:38:56 +09001003
1004 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -08001005 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +09001006 return
1007 }
1008
Spandan Das2069c3f2023-12-06 19:40:24 +00001009 // dexpreopt any system server jars if present
1010 a.dexpreoptSystemServerJars(ctx)
1011
Paul Duffinc30aea22021-06-15 19:10:11 +01001012 // Save the files that need to be made available to Make.
1013 a.initApexFilesForAndroidMk(ctx)
1014
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001015 a.installDir = android.PathForModuleInstall(ctx, "apex")
1016 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001017 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001018 }
1019
1020 // in case that apex_set replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001021 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001022 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001023 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001024 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001025 }
1026}
1027
Paul Duffinef6b6952021-06-15 11:34:01 +01001028type systemExtContext struct {
1029 android.ModuleContext
1030}
1031
1032func (*systemExtContext) SystemExtSpecific() bool {
1033 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001034}