blob: 0d83830f16dcbb154fd92a604e8fd007956f06ee [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 {
136 // If the device is configured to use flattened APEX, force disable the prebuilt because
137 // the prebuilt is a non-flattened one.
138 forceDisable := ctx.Config().FlattenApex()
139
140 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
141 // to build the prebuilts themselves.
142 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
143
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700144 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
145 sanitized := ctx.Module().(sanitizedPrebuilt)
146 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
147 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900148
149 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100150 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900151 return true
152 }
153 return false
154}
155
Paul Duffinef6b6952021-06-15 11:34:01 +0100156func (p *prebuiltCommon) InstallFilename() string {
157 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
158}
159
160func (p *prebuiltCommon) Name() string {
161 return p.prebuilt.Name(p.ModuleBase.Name())
162}
163
164func (p *prebuiltCommon) Overrides() []string {
165 return p.prebuiltCommonProperties.Overrides
166}
167
168func (p *prebuiltCommon) installable() bool {
169 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
170}
171
Paul Duffinc30aea22021-06-15 19:10:11 +0100172// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
173// modules that this depends upon.
174func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
175 // Walk the dependencies of this module looking for the java modules that it exports.
176 ctx.WalkDeps(func(child, parent android.Module) bool {
177 tag := ctx.OtherModuleDependencyTag(child)
178
179 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000180 if java.IsBootclasspathFragmentContentDepTag(tag) ||
181 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100182 // 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 +0100183 path := child.(interface {
184 DexJarBuildPath() java.OptionalDexJarPath
185 }).DexJarBuildPath()
186 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000187 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100188 module: child,
189 moduleDir: ctx.OtherModuleDir(child),
190 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100191 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100192 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000193 }
194 if module, ok := child.(java.DexpreopterInterface); ok {
195 for _, install := range module.DexpreoptBuiltInstalledForApex() {
196 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
197 }
198 }
199 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100200 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000201 } else if tag == exportedBootclasspathFragmentTag {
Jiakai Zhangb47cacc2023-05-10 16:40:18 +0100202 _, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000203 if !ok {
204 ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
205 return false
206 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000207 // Visit the children of the bootclasspath_fragment.
208 return true
209 } else if tag == exportedSystemserverclasspathFragmentTag {
210 // Visit the children of the systemserver_fragment.
Paul Duffinc30aea22021-06-15 19:10:11 +0100211 return true
212 }
213
214 return false
215 })
216}
217
Jiakai Zhang204356f2021-09-09 08:12:46 +0000218func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
219 for _, fi := range p.apexFilesForAndroidMk {
220 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
221 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
222 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
223 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000224 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000225}
226
Paul Duffinef6b6952021-06-15 11:34:01 +0100227func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100228 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100229 {
230 Class: "ETC",
231 OutputFile: android.OptionalPathForPath(p.outputApex),
232 Include: "$(BUILD_PREBUILT)",
233 Host_required: p.hostRequired,
234 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
235 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800236 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100237 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700238 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
239 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100240 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
241 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000242 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100243 },
244 },
245 },
246 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100247
248 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
249 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
250 // apex specific variants of the exported java modules available for use from within make.
251 apexName := p.BaseModuleName()
252 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100253 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100254 entriesList = append(entriesList, entries)
255 }
256
257 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100258}
259
Paul Duffin9dc8c542021-06-17 13:33:09 +0100260// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
261func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
262 moduleName := fi.androidMkModuleName + "." + apexName
263 entries := android.AndroidMkEntries{
264 Class: fi.class.nameInMake(),
265 OverrideName: moduleName,
266 OutputFile: android.OptionalPathForPath(fi.builtFile),
267 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
268 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
269 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800270 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000271 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
272 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700273 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100274
275 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
276 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
277 // we will have foo.jar.jar
278 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100279 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
280 entries.SetString("LOCAL_DEX_PREOPT", "false")
281 },
282 },
283 ExtraFooters: []android.AndroidMkExtraFootersFunc{
284 func(w io.Writer, name, prefix, moduleDir string) {
285 // m <module_name> will build <module_name>.<apex_name> as well.
286 if fi.androidMkModuleName != moduleName {
287 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
288 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
289 }
290 },
291 },
292 }
293 return entries
294}
295
Paul Duffin5dda3e32021-05-05 14:13:27 +0100296// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
297// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
298type prebuiltApexModuleCreator interface {
299 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
300}
301
302// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
303// prebuiltApexModuleCreator's createPrebuiltApexModules method.
304//
305// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
306// will need to access dependencies added by that (exported modules) but must run before the
307// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
308// exported modules.
309func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
310 module := ctx.Module()
311 if creator, ok := module.(prebuiltApexModuleCreator); ok {
312 creator.createPrebuiltApexModules(ctx)
313 }
314}
315
Liz Kammer2dc72442023-04-20 10:10:48 -0400316func (p *prebuiltCommon) hasExportedDeps() bool {
317 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
318 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
319 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000320}
321
Paul Duffin57f83592021-05-05 15:09:44 +0100322// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
323func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
324 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100325
Liz Kammer2dc72442023-04-20 10:10:48 -0400326 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000327 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400328 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
329 }
330
331 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
332 prebuiltDep := android.PrebuiltNameFromSource(dep)
333 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
334 }
335
336 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
337 prebuiltDep := android.PrebuiltNameFromSource(dep)
338 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100339 }
Paul Duffindfd33262021-04-06 17:02:08 +0100340}
341
Paul Duffinb17d0442021-05-05 12:07:00 +0100342// Implements android.DepInInSameApex
343func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
344 tag := ctx.OtherModuleDependencyTag(dep)
345 _, ok := tag.(exportedDependencyTag)
346 return ok
347}
348
Paul Duffindfd33262021-04-06 17:02:08 +0100349// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
350// specific variant and checks that they are supported.
351//
352// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
353// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
354//
355// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
356// across prebuilt_apex modules. That is because there is no way to determine whether two
357// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
358// been built from different source at different times or they could have been built with different
359// build options that affect the libraries.
360//
361// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
362// modules were compatible it would be a lot of work and would not provide much benefit for a couple
363// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700364// - The number of prebuilt_apex modules that will be exporting files for the same module will be
365// low as the prebuilt_apex only exports files for the direct dependencies that require it and
366// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
367// few com.android.art* apex files that contain the same contents and could export files for the
368// same modules but only one of them needs to do so. Contrast that with source apex modules which
369// need apex specific variants for every module that contributes code to the apex, whether direct
370// or indirect.
371// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
372// extra copying of files. Contrast that with source apex modules that has to build each variant
373// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100374func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
375
376 // Collect direct dependencies into contents.
377 contents := make(map[string]android.ApexMembership)
378
379 // Collect the list of dependencies.
380 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100381 mctx.WalkDeps(func(child, parent android.Module) bool {
382 // If the child is not in the same apex as the parent then exit immediately and do not visit
383 // any of the child's dependencies.
384 if !android.IsDepInSameApex(mctx, parent, child) {
385 return false
386 }
387
388 tag := mctx.OtherModuleDependencyTag(child)
389 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100390 if exportedTag, ok := tag.(exportedDependencyTag); ok {
391 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100392
393 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100394 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100395 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100396 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100397 }
398
399 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100400 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100401 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100402 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100403 }
Paul Duffindfd33262021-04-06 17:02:08 +0100404 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100405
Paul Duffinfee8cf32021-06-29 18:38:38 +0100406 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
407 // variant.
408 if _, ok := child.(android.ApexModule); !ok {
409 return false
410 }
411
Paul Duffinb17d0442021-05-05 12:07:00 +0100412 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
413 // behavior whether there is a corresponding source module present or not.
414 depName = android.RemoveOptionalPrebuiltPrefix(depName)
415
416 // Remember if this module was added as a direct dependency.
417 direct := parent == mctx.Module()
418 contents[depName] = contents[depName].Add(direct)
419
420 // Add the module to the list of dependencies that need to have an APEX variant.
421 dependencies = append(dependencies, child.(android.ApexModule))
422
423 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100424 })
425
426 // Create contents for the prebuilt_apex and store it away for later use.
427 apexContents := android.NewApexContents(contents)
428 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
429 Contents: apexContents,
430 })
431
432 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100433 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100434 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000435 ApexVariationName: apexVariationName,
436 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100437 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100438 ApexContents: []*android.ApexContents{apexContents},
439 ForPrebuiltApex: true,
440 }
441
442 // Mark the dependencies of this module as requiring a variant for this module.
443 for _, am := range dependencies {
444 am.BuildForApex(apexInfo)
445 }
446}
447
Paul Duffin11216db2021-03-01 14:14:52 +0000448// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
449// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
450// deapexer.
451type prebuiltApexSelectorModule struct {
452 android.ModuleBase
453
454 apexFileProperties ApexFileProperties
455
456 inputApex android.Path
457}
458
459func privateApexSelectorModuleFactory() android.Module {
460 module := &prebuiltApexSelectorModule{}
461 module.AddProperties(
462 &module.apexFileProperties,
463 )
464 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
465 return module
466}
467
468func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
469 return android.Paths{p.inputApex}
470}
471
472func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
473 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
474}
475
Jiyong Park09d77522019-11-18 11:16:27 +0900476type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900477 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900478
Paul Duffinbb0dc132021-05-05 16:58:08 +0100479 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900480
Paul Duffinef6b6952021-06-15 11:34:01 +0100481 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700482
483 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900484}
485
Paul Duffin851f3992021-01-13 17:03:51 +0000486type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900487 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000488 //
489 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
490 // for android_common. That is so that it will have the same arch variant as, and so be compatible
491 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000492 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900493 Arch struct {
494 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000495 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900496 }
497 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000498 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900499 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800500 Riscv64 struct {
501 Src *string `android:"path"`
502 }
Jiyong Park09d77522019-11-18 11:16:27 +0900503 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000504 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900505 }
506 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000507 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900508 }
509 }
Paul Duffin851f3992021-01-13 17:03:51 +0000510}
511
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000512// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
513//
514// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
515// to use methods on it that are specific to the current module.
516//
517// See the ApexFileProperties.Src property.
518func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
519 multiTargets := prebuilt.MultiTargets()
520 if len(multiTargets) != 1 {
521 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
522 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000523 }
524 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000525 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000526 case android.Arm:
527 src = String(p.Arch.Arm.Src)
528 case android.Arm64:
529 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800530 case android.Riscv64:
531 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700532 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
533 if src == "" {
534 src = String(p.Arch.Arm64.Src)
535 }
Paul Duffin851f3992021-01-13 17:03:51 +0000536 case android.X86:
537 src = String(p.Arch.X86.Src)
538 case android.X86_64:
539 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000540 }
541 if src == "" {
542 src = String(p.Src)
543 }
Paul Duffin851f3992021-01-13 17:03:51 +0000544
Paul Duffinc0609c62021-03-01 17:27:16 +0000545 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700546 if ctx.Config().AllowMissingDependencies() {
547 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
548 } else {
549 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
550 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000551 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
552 // logic from reporting a more general, less useful message.
553 }
554
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000555 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000556}
557
558type PrebuiltProperties struct {
559 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900560
Paul Duffinef6b6952021-06-15 11:34:01 +0100561 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900562}
563
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700564func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
565 return false
566}
567
Jiyong Park09d77522019-11-18 11:16:27 +0900568func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
569 switch tag {
570 case "":
571 return android.Paths{p.outputApex}, nil
572 default:
573 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
574 }
575}
576
Jiyong Park09d77522019-11-18 11:16:27 +0900577// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
578func PrebuiltFactory() android.Module {
579 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100580 module.AddProperties(&module.properties)
581 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000582
Jiyong Park09d77522019-11-18 11:16:27 +0900583 return module
584}
585
Paul Duffin5dda3e32021-05-05 14:13:27 +0100586func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000587 props := struct {
588 Name *string
589 }{
590 Name: proptools.StringPtr(name),
591 }
592
593 ctx.CreateModule(privateApexSelectorModuleFactory,
594 &props,
595 apexFileProperties,
596 )
597}
598
Paul Duffin5dda3e32021-05-05 14:13:27 +0100599// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
600//
Paul Duffin57f83592021-05-05 15:09:44 +0100601// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
602// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
603// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000604func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100605 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400606 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100607 return
608 }
609
Paul Duffin57f83592021-05-05 15:09:44 +0100610 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100611 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100612 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100613 ctx.WalkDeps(func(child, parent android.Module) bool {
614 tag := ctx.OtherModuleDependencyTag(child)
615
Paul Duffin7db57e02021-06-17 14:56:05 +0100616 // If the child is not in the same apex as the parent then ignore it and all its children.
617 if !android.IsDepInSameApex(ctx, parent, child) {
618 return false
619 }
620
Paul Duffin57f83592021-05-05 15:09:44 +0100621 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100622 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100623 commonModules = append(commonModules, name)
624
625 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100626 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100627
Paul Duffin7db57e02021-06-17 14:56:05 +0100628 // Visit the dependencies of this module just in case they also require files from the
629 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100630 return true
631 }
632
633 return false
634 })
635
Paul Duffin3bae0682021-05-05 18:03:47 +0100636 // Create properties for deapexer module.
637 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100638 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100639 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100640 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100641 }
642
643 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100644 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100645
Paul Duffin11216db2021-03-01 14:14:52 +0000646 props := struct {
647 Name *string
648 Selected_apex *string
649 }{
650 Name: proptools.StringPtr(deapexerName),
651 Selected_apex: proptools.StringPtr(apexFileSource),
652 }
653 ctx.CreateModule(privateDeapexerFactory,
654 &props,
655 deapexerProperties,
656 )
657}
658
Paul Duffin11216db2021-03-01 14:14:52 +0000659func apexSelectorModuleName(baseModuleName string) string {
660 return baseModuleName + ".apex.selector"
661}
662
Paul Duffin064b70c2020-11-02 17:32:38 +0000663func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
664 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
665 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
666 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
667 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
668 // and not a renamed prebuilt module then that will be detected and reported as an error when
669 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100670 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000671 if ctx.OtherModuleExists(prebuiltName) {
672 name = prebuiltName
673 }
674 return name
675}
676
Paul Duffina7139422021-02-08 11:01:58 +0000677type exportedDependencyTag struct {
678 blueprint.BaseDependencyTag
679 name string
680}
681
682// Mark this tag so dependencies that use it are excluded from visibility enforcement.
683//
684// This does allow any prebuilt_apex to reference any module which does open up a small window for
685// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
686// avoids opening up a much bigger window by widening the visibility of modules that need files
687// provided by the prebuilt_apex to include all the possible locations they may be defined, which
688// could include everything below vendor/.
689//
690// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
691// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
692// the .apex file. It will also have to be included in the module's apex_available property too.
693// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
694// incorrectly.
695func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
696
Paul Duffin7db57e02021-06-17 14:56:05 +0100697func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
698
699var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
700
Paul Duffina7139422021-02-08 11:01:58 +0000701var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000702 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
703 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
704 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000705)
706
Paul Duffin5dda3e32021-05-05 14:13:27 +0100707var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
708
709// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
710// build.
711//
712// If this needs to make files from within a `.apex` file available for use by other Soong modules,
713// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
714// it does so as follows:
715//
Colin Crossd079e0b2022-08-16 10:27:33 -0700716// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
717// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100718//
Colin Crossd079e0b2022-08-16 10:27:33 -0700719// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
720// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
721// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100722//
Colin Crossd079e0b2022-08-16 10:27:33 -0700723// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
724// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100725//
726// It also creates a child module `selector` that is responsible for selecting the appropriate
727// 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 +0100728//
Colin Crossd079e0b2022-08-16 10:27:33 -0700729// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100730//
Colin Crossd079e0b2022-08-16 10:27:33 -0700731// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
732// `apex_set`.
733//
734// prebuilt_apex
735// / | \
736// / | \
737// V V V
738// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100739func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
740 baseModuleName := p.BaseModuleName()
741
742 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
743 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
744
745 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000746 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100747
748 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100749 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100750}
751
Paul Duffin57f83592021-05-05 15:09:44 +0100752func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
753 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000754}
755
756var _ ApexInfoMutator = (*Prebuilt)(nil)
757
Paul Duffin064b70c2020-11-02 17:32:38 +0000758func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100759 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900760}
761
762func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900763 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100764 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900765 p.installDir = android.PathForModuleInstall(ctx, "apex")
766 p.installFilename = p.InstallFilename()
767 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
768 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
769 }
770 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
771 ctx.Build(pctx, android.BuildParams{
772 Rule: android.Cp,
773 Input: p.inputApex,
774 Output: p.outputApex,
775 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900776
777 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800778 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900779 return
780 }
781
Paul Duffinc30aea22021-06-15 19:10:11 +0100782 // Save the files that need to be made available to Make.
783 p.initApexFilesForAndroidMk(ctx)
784
Colin Crossccba23d2021-11-12 19:01:29 +0000785 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700786 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
Colin Crossccba23d2021-11-12 19:01:29 +0000787 // or that prebuilt_apex overrides other apexes (using overrides: prop)
788 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700789 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
790 }
791
792 if p.installable() {
793 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
Wei Li340ee8e2022-03-18 17:33:24 -0700794 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900795 }
Jiyong Park09d77522019-11-18 11:16:27 +0900796}
797
Wei Li340ee8e2022-03-18 17:33:24 -0700798func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
799 return p.provenanceMetaDataFile
800}
801
Paul Duffin24704672021-04-06 16:09:30 +0100802// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
803// module. It extracts the correct apex to use and makes it available for use by apex_set.
804type prebuiltApexExtractorModule struct {
805 android.ModuleBase
806
807 properties ApexExtractorProperties
808
809 extractedApex android.WritablePath
810}
811
812func privateApexExtractorModuleFactory() android.Module {
813 module := &prebuiltApexExtractorModule{}
814 module.AddProperties(
815 &module.properties,
816 )
817 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
818 return module
819}
820
821func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
822 return android.Paths{p.extractedApex}
823}
824
825func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
826 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
827 return p.properties.prebuiltSrcs(ctx)
828 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000829 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100830 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
831 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000832 // Filter out NativeBridge archs (b/260115309)
833 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100834 ctx.Build(pctx,
835 android.BuildParams{
836 Rule: extractMatchingApex,
837 Description: "Extract an apex from an apex set",
838 Inputs: android.Paths{apexSet},
839 Output: p.extractedApex,
840 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000841 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000842 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100843 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700844 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100845 },
846 })
847}
848
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700849type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900850 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700851
852 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700853}
854
Paul Duffin24704672021-04-06 16:09:30 +0100855type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700856 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000857 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700858
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700859 Sanitized struct {
860 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000861 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700862 }
863 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000864 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700865 }
866 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000867 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700868 }
869 }
870
Paul Duffin24704672021-04-06 16:09:30 +0100871 // apexes in this set use prerelease SDK version
872 Prerelease *bool
873}
874
875func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
876 var srcs []string
877 if e.Set != nil {
878 srcs = append(srcs, *e.Set)
879 }
880
881 var sanitizers []string
882 if ctx.Host() {
883 sanitizers = ctx.Config().SanitizeHost()
884 } else {
885 sanitizers = ctx.Config().SanitizeDevice()
886 }
887
888 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
889 srcs = append(srcs, *e.Sanitized.Address.Set)
890 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
891 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
892 } else if e.Sanitized.None.Set != nil {
893 srcs = append(srcs, *e.Sanitized.None.Set)
894 }
895
896 return srcs
897}
898
899type ApexSetProperties struct {
900 ApexExtractorProperties
901
Paul Duffinef6b6952021-06-15 11:34:01 +0100902 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700903}
904
905func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
906 if sanitizer == "address" {
907 return a.properties.Sanitized.Address.Set != nil
908 }
909 if sanitizer == "hwaddress" {
910 return a.properties.Sanitized.Hwaddress.Set != nil
911 }
912
913 return false
914}
915
Paul Duffin4f2282c2022-12-12 17:44:33 +0000916func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
917 switch tag {
918 case "":
919 return android.Paths{a.outputApex}, nil
920 default:
921 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
922 }
923}
924
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700925// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
926func apexSetFactory() android.Module {
927 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100928 module.AddProperties(&module.properties)
929 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100930
Paul Duffin24704672021-04-06 16:09:30 +0100931 return module
932}
933
Paul Duffin5dda3e32021-05-05 14:13:27 +0100934func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100935 props := struct {
936 Name *string
937 }{
938 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700939 }
940
Paul Duffin24704672021-04-06 16:09:30 +0100941 ctx.CreateModule(privateApexExtractorModuleFactory,
942 &props,
943 apexExtractorProperties,
944 )
945}
946
947func apexExtractorModuleName(baseModuleName string) string {
948 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700949}
950
Paul Duffin5dda3e32021-05-05 14:13:27 +0100951var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
952
953// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
954// modules.
955//
956// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
957// prebuilt_apex except that instead of creating a selector module which selects one .apex file
958// from those provided this creates an extractor module which extracts the appropriate .apex file
959// from the zip file containing them.
960func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
961 baseModuleName := a.BaseModuleName()
962
963 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
964 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
965
966 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000967 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100968
969 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100970 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100971}
972
Paul Duffin57f83592021-05-05 15:09:44 +0100973func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
974 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100975}
976
977var _ ApexInfoMutator = (*ApexSet)(nil)
978
979func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
980 a.apexInfoMutator(mctx)
981}
982
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700983func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
984 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100985 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
986 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700987 }
988
Paul Duffinbb0dc132021-05-05 16:58:08 +0100989 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700990 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100991 ctx.Build(pctx, android.BuildParams{
992 Rule: android.Cp,
993 Input: inputApex,
994 Output: a.outputApex,
995 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900996
997 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800998 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900999 return
1000 }
1001
Paul Duffinc30aea22021-06-15 19:10:11 +01001002 // Save the files that need to be made available to Make.
1003 a.initApexFilesForAndroidMk(ctx)
1004
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001005 a.installDir = android.PathForModuleInstall(ctx, "apex")
1006 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001007 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001008 }
1009
1010 // in case that apex_set replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -07001011 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001012 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001013 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -07001014 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001015 }
1016}
1017
Paul Duffinef6b6952021-06-15 11:34:01 +01001018type systemExtContext struct {
1019 android.ModuleContext
1020}
1021
1022func (*systemExtContext) SystemExtSpecific() bool {
1023 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001024}