blob: 3509e6cbe19cc24139813a832ba2bde2dc846fee [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"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070025 "android/soong/java"
Wei Li340ee8e2022-03-18 17:33:24 -070026 "android/soong/provenance"
Anton Hansson805e0a52022-11-25 14:06:46 +000027
Jaewoong Jungfa00c062020-05-14 14:15:24 -070028 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090029 "github.com/google/blueprint/proptools"
30)
31
Jaewoong Jungfa00c062020-05-14 14:15:24 -070032var (
33 extractMatchingApex = pctx.StaticRule(
34 "extractMatchingApex",
35 blueprint.RuleParams{
36 Command: `rm -rf "$out" && ` +
37 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
Pranav Gupta51645ff2023-03-20 16:19:53 -070038 `-sdk-version=${sdk-version} -skip-sdk-check=${skip-sdk-check} -abis=${abis} ` +
39 `-screen-densities=all -extract-single ` +
Jaewoong Jungfa00c062020-05-14 14:15:24 -070040 `${in}`,
41 CommandDeps: []string{"${extract_apks}"},
42 },
Pranav Gupta51645ff2023-03-20 16:19:53 -070043 "abis", "allow-prereleased", "sdk-version", "skip-sdk-check")
Jaewoong Jungfa00c062020-05-14 14:15:24 -070044)
45
Jiyong Park10e926b2020-07-16 21:38:56 +090046type prebuilt interface {
47 isForceDisabled() bool
48 InstallFilename() string
49}
50
51type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010052 android.ModuleBase
Paul Duffinbb0dc132021-05-05 16:58:08 +010053 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010054
Paul Duffinbb0dc132021-05-05 16:58:08 +010055 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010056 prebuiltCommonProperties *PrebuiltCommonProperties
57
58 installDir android.InstallPath
59 installFilename string
Colin Cross6340ea52021-11-04 12:01:18 -070060 installedFile android.InstallPath
Paul Duffinef6b6952021-06-15 11:34:01 +010061 outputApex android.WritablePath
62
Paul Duffinc30aea22021-06-15 19:10:11 +010063 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
64 // to create make modules in prebuiltCommon.AndroidMkEntries.
65 apexFilesForAndroidMk []apexFile
66
Colin Cross6340ea52021-11-04 12:01:18 -070067 // Installed locations of symlinks for backward compatibility.
68 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010069
Jiakai Zhange6e90db2022-01-28 14:58:56 +000070 hostRequired []string
71 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090072}
73
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070074type sanitizedPrebuilt interface {
75 hasSanitizedSource(sanitizer string) bool
76}
77
Paul Duffinef6b6952021-06-15 11:34:01 +010078type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010079 SelectedApexProperties
80
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010081 // Canonical name of this APEX. Used to determine the path to the activated APEX on
82 // device (/apex/<apex_name>). If unspecified, follows the name property.
83 Apex_name *string
84
Jiyong Park10e926b2020-07-16 21:38:56 +090085 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010086
Paul Duffinef6b6952021-06-15 11:34:01 +010087 // whether the extracted apex file is installable.
88 Installable *bool
89
90 // optional name for the installed apex. If unspecified, name of the
91 // module is used as the file name
92 Filename *string
93
94 // names of modules to be overridden. Listed modules can only be other binaries
95 // (in Make or Soong).
96 // This does not completely prevent installation of the overridden binaries, but if both
97 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
98 // from PRODUCT_PACKAGES.
99 Overrides []string
100
Paul Duffin3bae0682021-05-05 18:03:47 +0100101 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
102 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
103 // dexpreopt and boot jars package check.
104 Exported_java_libs []string
105
106 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
107 // bundle will create an APEX variant.
108 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000109
110 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
111 // APEX bundle will create an APEX variant.
112 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900113}
114
Paul Duffinef6b6952021-06-15 11:34:01 +0100115// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
116// module that is common to Prebuilt and ApexSet.
117func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
118 p.prebuiltCommonProperties = properties
119 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
120 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
121}
122
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100123func (p *prebuiltCommon) ApexVariationName() string {
124 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
125}
126
Jiyong Park10e926b2020-07-16 21:38:56 +0900127func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
128 return &p.prebuilt
129}
130
131func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100132 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900133}
134
135func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900136 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900137
138 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
139 // to build the prebuilts themselves.
140 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
141
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700142 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
143 sanitized := ctx.Module().(sanitizedPrebuilt)
144 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
145 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900146
147 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100148 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900149 return true
150 }
151 return false
152}
153
Paul Duffinef6b6952021-06-15 11:34:01 +0100154func (p *prebuiltCommon) InstallFilename() string {
155 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
156}
157
158func (p *prebuiltCommon) Name() string {
159 return p.prebuilt.Name(p.ModuleBase.Name())
160}
161
162func (p *prebuiltCommon) Overrides() []string {
163 return p.prebuiltCommonProperties.Overrides
164}
165
166func (p *prebuiltCommon) installable() bool {
167 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
168}
169
Paul Duffinc30aea22021-06-15 19:10:11 +0100170// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
171// modules that this depends upon.
172func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
173 // Walk the dependencies of this module looking for the java modules that it exports.
174 ctx.WalkDeps(func(child, parent android.Module) bool {
175 tag := ctx.OtherModuleDependencyTag(child)
176
177 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000178 if java.IsBootclasspathFragmentContentDepTag(tag) ||
179 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100180 // If the exported java module provides a dex jar path then add it to the list of apexFiles.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100181 path := child.(interface {
182 DexJarBuildPath() java.OptionalDexJarPath
183 }).DexJarBuildPath()
184 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000185 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100186 module: child,
187 moduleDir: ctx.OtherModuleDir(child),
188 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100189 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100190 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000191 }
192 if module, ok := child.(java.DexpreopterInterface); ok {
193 for _, install := range module.DexpreoptBuiltInstalledForApex() {
194 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
195 }
196 }
197 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100198 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000199 } else if tag == exportedBootclasspathFragmentTag {
Jiakai Zhangb47cacc2023-05-10 16:40:18 +0100200 _, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000201 if !ok {
202 ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
203 return false
204 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000205 // Visit the children of the bootclasspath_fragment.
206 return true
207 } else if tag == exportedSystemserverclasspathFragmentTag {
208 // Visit the children of the systemserver_fragment.
Paul Duffinc30aea22021-06-15 19:10:11 +0100209 return true
210 }
211
212 return false
213 })
214}
215
Jiakai Zhang204356f2021-09-09 08:12:46 +0000216func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
217 for _, fi := range p.apexFilesForAndroidMk {
218 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
219 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
220 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
221 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000222 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000223}
224
Paul Duffinef6b6952021-06-15 11:34:01 +0100225func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100226 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100227 {
228 Class: "ETC",
229 OutputFile: android.OptionalPathForPath(p.outputApex),
230 Include: "$(BUILD_PREBUILT)",
231 Host_required: p.hostRequired,
232 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
233 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800234 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100235 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700236 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
237 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100238 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
239 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000240 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100241 },
242 },
243 },
244 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100245
246 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
247 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
248 // apex specific variants of the exported java modules available for use from within make.
249 apexName := p.BaseModuleName()
250 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100251 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100252 entriesList = append(entriesList, entries)
253 }
254
255 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100256}
257
Paul Duffin9dc8c542021-06-17 13:33:09 +0100258// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
259func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
260 moduleName := fi.androidMkModuleName + "." + apexName
261 entries := android.AndroidMkEntries{
262 Class: fi.class.nameInMake(),
263 OverrideName: moduleName,
264 OutputFile: android.OptionalPathForPath(fi.builtFile),
265 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
266 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
267 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800268 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000269 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
270 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700271 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100272
273 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
274 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
275 // we will have foo.jar.jar
276 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100277 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
278 entries.SetString("LOCAL_DEX_PREOPT", "false")
279 },
280 },
281 ExtraFooters: []android.AndroidMkExtraFootersFunc{
282 func(w io.Writer, name, prefix, moduleDir string) {
283 // m <module_name> will build <module_name>.<apex_name> as well.
284 if fi.androidMkModuleName != moduleName {
285 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
286 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
287 }
288 },
289 },
290 }
291 return entries
292}
293
Paul Duffin5dda3e32021-05-05 14:13:27 +0100294// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
295// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
296type prebuiltApexModuleCreator interface {
297 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
298}
299
300// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
301// prebuiltApexModuleCreator's createPrebuiltApexModules method.
302//
303// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
304// will need to access dependencies added by that (exported modules) but must run before the
305// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
306// exported modules.
307func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
308 module := ctx.Module()
309 if creator, ok := module.(prebuiltApexModuleCreator); ok {
310 creator.createPrebuiltApexModules(ctx)
311 }
312}
313
Liz Kammer2dc72442023-04-20 10:10:48 -0400314func (p *prebuiltCommon) hasExportedDeps() bool {
315 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
316 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
317 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000318}
319
Paul Duffin57f83592021-05-05 15:09:44 +0100320// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
321func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
322 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100323
Liz Kammer2dc72442023-04-20 10:10:48 -0400324 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000325 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400326 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
327 }
328
329 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
330 prebuiltDep := android.PrebuiltNameFromSource(dep)
331 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
332 }
333
334 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
335 prebuiltDep := android.PrebuiltNameFromSource(dep)
336 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100337 }
Paul Duffindfd33262021-04-06 17:02:08 +0100338}
339
Paul Duffinb17d0442021-05-05 12:07:00 +0100340// Implements android.DepInInSameApex
341func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
342 tag := ctx.OtherModuleDependencyTag(dep)
343 _, ok := tag.(exportedDependencyTag)
344 return ok
345}
346
Paul Duffindfd33262021-04-06 17:02:08 +0100347// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
348// specific variant and checks that they are supported.
349//
350// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
351// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
352//
353// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
354// across prebuilt_apex modules. That is because there is no way to determine whether two
355// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
356// been built from different source at different times or they could have been built with different
357// build options that affect the libraries.
358//
359// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
360// modules were compatible it would be a lot of work and would not provide much benefit for a couple
361// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700362// - The number of prebuilt_apex modules that will be exporting files for the same module will be
363// low as the prebuilt_apex only exports files for the direct dependencies that require it and
364// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
365// few com.android.art* apex files that contain the same contents and could export files for the
366// same modules but only one of them needs to do so. Contrast that with source apex modules which
367// need apex specific variants for every module that contributes code to the apex, whether direct
368// or indirect.
369// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
370// extra copying of files. Contrast that with source apex modules that has to build each variant
371// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100372func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
373
374 // Collect direct dependencies into contents.
375 contents := make(map[string]android.ApexMembership)
376
377 // Collect the list of dependencies.
378 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100379 mctx.WalkDeps(func(child, parent android.Module) bool {
380 // If the child is not in the same apex as the parent then exit immediately and do not visit
381 // any of the child's dependencies.
382 if !android.IsDepInSameApex(mctx, parent, child) {
383 return false
384 }
385
386 tag := mctx.OtherModuleDependencyTag(child)
387 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100388 if exportedTag, ok := tag.(exportedDependencyTag); ok {
389 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100390
391 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100392 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100393 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100394 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100395 }
396
397 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100398 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100399 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100400 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100401 }
Paul Duffindfd33262021-04-06 17:02:08 +0100402 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100403
Paul Duffinfee8cf32021-06-29 18:38:38 +0100404 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
405 // variant.
406 if _, ok := child.(android.ApexModule); !ok {
407 return false
408 }
409
Paul Duffinb17d0442021-05-05 12:07:00 +0100410 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
411 // behavior whether there is a corresponding source module present or not.
412 depName = android.RemoveOptionalPrebuiltPrefix(depName)
413
414 // Remember if this module was added as a direct dependency.
415 direct := parent == mctx.Module()
416 contents[depName] = contents[depName].Add(direct)
417
418 // Add the module to the list of dependencies that need to have an APEX variant.
419 dependencies = append(dependencies, child.(android.ApexModule))
420
421 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100422 })
423
424 // Create contents for the prebuilt_apex and store it away for later use.
425 apexContents := android.NewApexContents(contents)
426 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
427 Contents: apexContents,
428 })
429
430 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100431 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100432 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000433 ApexVariationName: apexVariationName,
434 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100435 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100436 ApexContents: []*android.ApexContents{apexContents},
437 ForPrebuiltApex: true,
438 }
439
440 // Mark the dependencies of this module as requiring a variant for this module.
441 for _, am := range dependencies {
442 am.BuildForApex(apexInfo)
443 }
444}
445
Paul Duffin11216db2021-03-01 14:14:52 +0000446// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
447// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
448// deapexer.
449type prebuiltApexSelectorModule struct {
450 android.ModuleBase
451
452 apexFileProperties ApexFileProperties
453
454 inputApex android.Path
455}
456
457func privateApexSelectorModuleFactory() android.Module {
458 module := &prebuiltApexSelectorModule{}
459 module.AddProperties(
460 &module.apexFileProperties,
461 )
462 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
463 return module
464}
465
466func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
467 return android.Paths{p.inputApex}
468}
469
470func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
471 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
472}
473
Jiyong Park09d77522019-11-18 11:16:27 +0900474type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900475 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900476
Paul Duffinbb0dc132021-05-05 16:58:08 +0100477 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900478
Paul Duffinef6b6952021-06-15 11:34:01 +0100479 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700480
481 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900482}
483
Paul Duffin851f3992021-01-13 17:03:51 +0000484type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900485 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000486 //
487 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
488 // for android_common. That is so that it will have the same arch variant as, and so be compatible
489 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000490 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900491 Arch struct {
492 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000493 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900494 }
495 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000496 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900497 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800498 Riscv64 struct {
499 Src *string `android:"path"`
500 }
Jiyong Park09d77522019-11-18 11:16:27 +0900501 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000502 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900503 }
504 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000505 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900506 }
507 }
Paul Duffin851f3992021-01-13 17:03:51 +0000508}
509
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000510// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
511//
512// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
513// to use methods on it that are specific to the current module.
514//
515// See the ApexFileProperties.Src property.
516func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
517 multiTargets := prebuilt.MultiTargets()
518 if len(multiTargets) != 1 {
519 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
520 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000521 }
522 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000523 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000524 case android.Arm:
525 src = String(p.Arch.Arm.Src)
526 case android.Arm64:
527 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800528 case android.Riscv64:
529 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700530 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
531 if src == "" {
532 src = String(p.Arch.Arm64.Src)
533 }
Paul Duffin851f3992021-01-13 17:03:51 +0000534 case android.X86:
535 src = String(p.Arch.X86.Src)
536 case android.X86_64:
537 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000538 }
539 if src == "" {
540 src = String(p.Src)
541 }
Paul Duffin851f3992021-01-13 17:03:51 +0000542
Paul Duffinc0609c62021-03-01 17:27:16 +0000543 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700544 if ctx.Config().AllowMissingDependencies() {
545 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
546 } else {
547 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
548 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000549 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
550 // logic from reporting a more general, less useful message.
551 }
552
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000553 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000554}
555
556type PrebuiltProperties struct {
557 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900558
Paul Duffinef6b6952021-06-15 11:34:01 +0100559 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900560}
561
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700562func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
563 return false
564}
565
Jiyong Park09d77522019-11-18 11:16:27 +0900566func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
567 switch tag {
568 case "":
569 return android.Paths{p.outputApex}, nil
570 default:
571 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
572 }
573}
574
Jiyong Park09d77522019-11-18 11:16:27 +0900575// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
576func PrebuiltFactory() android.Module {
577 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100578 module.AddProperties(&module.properties)
579 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000580
Jiyong Park09d77522019-11-18 11:16:27 +0900581 return module
582}
583
Paul Duffin5dda3e32021-05-05 14:13:27 +0100584func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000585 props := struct {
586 Name *string
587 }{
588 Name: proptools.StringPtr(name),
589 }
590
591 ctx.CreateModule(privateApexSelectorModuleFactory,
592 &props,
593 apexFileProperties,
594 )
595}
596
Paul Duffin5dda3e32021-05-05 14:13:27 +0100597// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
598//
Paul Duffin57f83592021-05-05 15:09:44 +0100599// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
600// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
601// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000602func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100603 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400604 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100605 return
606 }
607
Paul Duffin57f83592021-05-05 15:09:44 +0100608 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100609 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100610 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100611 ctx.WalkDeps(func(child, parent android.Module) bool {
612 tag := ctx.OtherModuleDependencyTag(child)
613
Paul Duffin7db57e02021-06-17 14:56:05 +0100614 // If the child is not in the same apex as the parent then ignore it and all its children.
615 if !android.IsDepInSameApex(ctx, parent, child) {
616 return false
617 }
618
Paul Duffin57f83592021-05-05 15:09:44 +0100619 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100620 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100621 commonModules = append(commonModules, name)
622
623 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100624 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100625
Paul Duffin7db57e02021-06-17 14:56:05 +0100626 // Visit the dependencies of this module just in case they also require files from the
627 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100628 return true
629 }
630
631 return false
632 })
633
Paul Duffin3bae0682021-05-05 18:03:47 +0100634 // Create properties for deapexer module.
635 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100636 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100637 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100638 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100639 }
640
641 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100642 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100643
Paul Duffin11216db2021-03-01 14:14:52 +0000644 props := struct {
645 Name *string
646 Selected_apex *string
647 }{
648 Name: proptools.StringPtr(deapexerName),
649 Selected_apex: proptools.StringPtr(apexFileSource),
650 }
651 ctx.CreateModule(privateDeapexerFactory,
652 &props,
653 deapexerProperties,
654 )
655}
656
Paul Duffin11216db2021-03-01 14:14:52 +0000657func apexSelectorModuleName(baseModuleName string) string {
658 return baseModuleName + ".apex.selector"
659}
660
Paul Duffin064b70c2020-11-02 17:32:38 +0000661func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
662 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
663 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
664 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
665 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
666 // and not a renamed prebuilt module then that will be detected and reported as an error when
667 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100668 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000669 if ctx.OtherModuleExists(prebuiltName) {
670 name = prebuiltName
671 }
672 return name
673}
674
Paul Duffina7139422021-02-08 11:01:58 +0000675type exportedDependencyTag struct {
676 blueprint.BaseDependencyTag
677 name string
678}
679
680// Mark this tag so dependencies that use it are excluded from visibility enforcement.
681//
682// This does allow any prebuilt_apex to reference any module which does open up a small window for
683// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
684// avoids opening up a much bigger window by widening the visibility of modules that need files
685// provided by the prebuilt_apex to include all the possible locations they may be defined, which
686// could include everything below vendor/.
687//
688// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
689// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
690// the .apex file. It will also have to be included in the module's apex_available property too.
691// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
692// incorrectly.
693func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
694
Paul Duffin7db57e02021-06-17 14:56:05 +0100695func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
696
697var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
698
Paul Duffina7139422021-02-08 11:01:58 +0000699var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000700 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
701 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
702 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000703)
704
Paul Duffin5dda3e32021-05-05 14:13:27 +0100705var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
706
707// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
708// build.
709//
710// If this needs to make files from within a `.apex` file available for use by other Soong modules,
711// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
712// it does so as follows:
713//
Colin Crossd079e0b2022-08-16 10:27:33 -0700714// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
715// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100716//
Colin Crossd079e0b2022-08-16 10:27:33 -0700717// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
718// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
719// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100720//
Colin Crossd079e0b2022-08-16 10:27:33 -0700721// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
722// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100723//
724// It also creates a child module `selector` that is responsible for selecting the appropriate
725// 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 +0100726//
Colin Crossd079e0b2022-08-16 10:27:33 -0700727// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100728//
Colin Crossd079e0b2022-08-16 10:27:33 -0700729// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
730// `apex_set`.
731//
732// prebuilt_apex
733// / | \
734// / | \
735// V V V
736// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100737func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
738 baseModuleName := p.BaseModuleName()
739
740 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
741 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
742
743 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000744 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100745
746 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100747 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100748}
749
Paul Duffin57f83592021-05-05 15:09:44 +0100750func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
751 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000752}
753
754var _ ApexInfoMutator = (*Prebuilt)(nil)
755
Paul Duffin064b70c2020-11-02 17:32:38 +0000756func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100757 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900758}
759
760func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900761 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100762 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900763 p.installDir = android.PathForModuleInstall(ctx, "apex")
764 p.installFilename = p.InstallFilename()
765 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
766 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
767 }
768 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
769 ctx.Build(pctx, android.BuildParams{
770 Rule: android.Cp,
771 Input: p.inputApex,
772 Output: p.outputApex,
773 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900774
775 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800776 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900777 return
778 }
779
Paul Duffinc30aea22021-06-15 19:10:11 +0100780 // Save the files that need to be made available to Make.
781 p.initApexFilesForAndroidMk(ctx)
782
Colin Crossccba23d2021-11-12 19:01:29 +0000783 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700784 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
Colin Crossccba23d2021-11-12 19:01:29 +0000785 // or that prebuilt_apex overrides other apexes (using overrides: prop)
786 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700787 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
788 }
789
790 if p.installable() {
791 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
Wei Li340ee8e2022-03-18 17:33:24 -0700792 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900793 }
Jiyong Park09d77522019-11-18 11:16:27 +0900794}
795
Wei Li340ee8e2022-03-18 17:33:24 -0700796func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
797 return p.provenanceMetaDataFile
798}
799
Paul Duffin24704672021-04-06 16:09:30 +0100800// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
801// module. It extracts the correct apex to use and makes it available for use by apex_set.
802type prebuiltApexExtractorModule struct {
803 android.ModuleBase
804
805 properties ApexExtractorProperties
806
807 extractedApex android.WritablePath
808}
809
810func privateApexExtractorModuleFactory() android.Module {
811 module := &prebuiltApexExtractorModule{}
812 module.AddProperties(
813 &module.properties,
814 )
815 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
816 return module
817}
818
819func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
820 return android.Paths{p.extractedApex}
821}
822
823func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
824 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
825 return p.properties.prebuiltSrcs(ctx)
826 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000827 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100828 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
829 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000830 // Filter out NativeBridge archs (b/260115309)
831 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100832 ctx.Build(pctx,
833 android.BuildParams{
834 Rule: extractMatchingApex,
835 Description: "Extract an apex from an apex set",
836 Inputs: android.Paths{apexSet},
837 Output: p.extractedApex,
838 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000839 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000840 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100841 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700842 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100843 },
844 })
845}
846
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700847type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900848 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700849
850 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700851}
852
Paul Duffin24704672021-04-06 16:09:30 +0100853type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700854 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000855 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700856
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700857 Sanitized struct {
858 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000859 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700860 }
861 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000862 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700863 }
864 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000865 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700866 }
867 }
868
Paul Duffin24704672021-04-06 16:09:30 +0100869 // apexes in this set use prerelease SDK version
870 Prerelease *bool
871}
872
873func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
874 var srcs []string
875 if e.Set != nil {
876 srcs = append(srcs, *e.Set)
877 }
878
879 var sanitizers []string
880 if ctx.Host() {
881 sanitizers = ctx.Config().SanitizeHost()
882 } else {
883 sanitizers = ctx.Config().SanitizeDevice()
884 }
885
886 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
887 srcs = append(srcs, *e.Sanitized.Address.Set)
888 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
889 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
890 } else if e.Sanitized.None.Set != nil {
891 srcs = append(srcs, *e.Sanitized.None.Set)
892 }
893
894 return srcs
895}
896
897type ApexSetProperties struct {
898 ApexExtractorProperties
899
Paul Duffinef6b6952021-06-15 11:34:01 +0100900 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700901}
902
903func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
904 if sanitizer == "address" {
905 return a.properties.Sanitized.Address.Set != nil
906 }
907 if sanitizer == "hwaddress" {
908 return a.properties.Sanitized.Hwaddress.Set != nil
909 }
910
911 return false
912}
913
Paul Duffin4f2282c2022-12-12 17:44:33 +0000914func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
915 switch tag {
916 case "":
917 return android.Paths{a.outputApex}, nil
918 default:
919 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
920 }
921}
922
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700923// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
924func apexSetFactory() android.Module {
925 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100926 module.AddProperties(&module.properties)
927 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100928
Paul Duffin24704672021-04-06 16:09:30 +0100929 return module
930}
931
Paul Duffin5dda3e32021-05-05 14:13:27 +0100932func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100933 props := struct {
934 Name *string
935 }{
936 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700937 }
938
Paul Duffin24704672021-04-06 16:09:30 +0100939 ctx.CreateModule(privateApexExtractorModuleFactory,
940 &props,
941 apexExtractorProperties,
942 )
943}
944
945func apexExtractorModuleName(baseModuleName string) string {
946 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700947}
948
Paul Duffin5dda3e32021-05-05 14:13:27 +0100949var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
950
951// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
952// modules.
953//
954// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
955// prebuilt_apex except that instead of creating a selector module which selects one .apex file
956// from those provided this creates an extractor module which extracts the appropriate .apex file
957// from the zip file containing them.
958func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
959 baseModuleName := a.BaseModuleName()
960
961 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
962 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
963
964 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000965 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100966
967 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100968 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100969}
970
Paul Duffin57f83592021-05-05 15:09:44 +0100971func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
972 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100973}
974
975var _ ApexInfoMutator = (*ApexSet)(nil)
976
977func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
978 a.apexInfoMutator(mctx)
979}
980
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700981func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
982 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100983 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
984 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700985 }
986
Paul Duffinbb0dc132021-05-05 16:58:08 +0100987 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700988 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100989 ctx.Build(pctx, android.BuildParams{
990 Rule: android.Cp,
991 Input: inputApex,
992 Output: a.outputApex,
993 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900994
995 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800996 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900997 return
998 }
999
Paul Duffinc30aea22021-06-15 19:10:11 +01001000 // Save the files that need to be made available to Make.
1001 a.initApexFilesForAndroidMk(ctx)
1002
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001003 a.installDir = android.PathForModuleInstall(ctx, "apex")
1004 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001005 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001006 }
1007
1008 // in case that apex_set replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -07001009 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001010 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001011 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -07001012 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001013 }
1014}
1015
Paul Duffinef6b6952021-06-15 11:34:01 +01001016type systemExtContext struct {
1017 android.ModuleContext
1018}
1019
1020func (*systemExtContext) SystemExtSpecific() bool {
1021 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001022}