blob: 7d339d54eac74ba1a0cd8ab82ac412cf3f1aed50 [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
Jooyung Han286957d2023-10-30 16:17:56 +090063 // fragment for this apex for apexkeys.txt
64 apexKeysPath android.WritablePath
65
Paul Duffinc30aea22021-06-15 19:10:11 +010066 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
67 // to create make modules in prebuiltCommon.AndroidMkEntries.
68 apexFilesForAndroidMk []apexFile
69
Colin Cross6340ea52021-11-04 12:01:18 -070070 // Installed locations of symlinks for backward compatibility.
71 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010072
Jiakai Zhange6e90db2022-01-28 14:58:56 +000073 hostRequired []string
74 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090075}
76
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070077type sanitizedPrebuilt interface {
78 hasSanitizedSource(sanitizer string) bool
79}
80
Paul Duffinef6b6952021-06-15 11:34:01 +010081type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010082 SelectedApexProperties
83
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010084 // Canonical name of this APEX. Used to determine the path to the activated APEX on
85 // device (/apex/<apex_name>). If unspecified, follows the name property.
86 Apex_name *string
87
Jiyong Park10e926b2020-07-16 21:38:56 +090088 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010089
Paul Duffinef6b6952021-06-15 11:34:01 +010090 // whether the extracted apex file is installable.
91 Installable *bool
92
93 // optional name for the installed apex. If unspecified, name of the
94 // module is used as the file name
95 Filename *string
96
97 // names of modules to be overridden. Listed modules can only be other binaries
98 // (in Make or Soong).
99 // This does not completely prevent installation of the overridden binaries, but if both
100 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
101 // from PRODUCT_PACKAGES.
102 Overrides []string
103
Paul Duffin3bae0682021-05-05 18:03:47 +0100104 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
105 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
106 // dexpreopt and boot jars package check.
107 Exported_java_libs []string
108
109 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
110 // bundle will create an APEX variant.
111 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000112
113 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
114 // APEX bundle will create an APEX variant.
115 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900116}
117
Paul Duffinef6b6952021-06-15 11:34:01 +0100118// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
119// module that is common to Prebuilt and ApexSet.
120func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
121 p.prebuiltCommonProperties = properties
122 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
123 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
124}
125
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100126func (p *prebuiltCommon) ApexVariationName() string {
127 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
128}
129
Jiyong Park10e926b2020-07-16 21:38:56 +0900130func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
131 return &p.prebuilt
132}
133
134func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100135 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900136}
137
138func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
Jooyung Haneec1b3f2023-06-20 16:25:59 +0900139 forceDisable := false
Jiyong Park10e926b2020-07-16 21:38:56 +0900140
141 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
142 // to build the prebuilts themselves.
143 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
144
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700145 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
146 sanitized := ctx.Module().(sanitizedPrebuilt)
147 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
148 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900149
150 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100151 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900152 return true
153 }
154 return false
155}
156
Paul Duffinef6b6952021-06-15 11:34:01 +0100157func (p *prebuiltCommon) InstallFilename() string {
158 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
159}
160
161func (p *prebuiltCommon) Name() string {
162 return p.prebuilt.Name(p.ModuleBase.Name())
163}
164
165func (p *prebuiltCommon) Overrides() []string {
166 return p.prebuiltCommonProperties.Overrides
167}
168
169func (p *prebuiltCommon) installable() bool {
170 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
171}
172
Paul Duffinc30aea22021-06-15 19:10:11 +0100173// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
174// modules that this depends upon.
175func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
176 // Walk the dependencies of this module looking for the java modules that it exports.
177 ctx.WalkDeps(func(child, parent android.Module) bool {
178 tag := ctx.OtherModuleDependencyTag(child)
179
180 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000181 if java.IsBootclasspathFragmentContentDepTag(tag) ||
182 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100183 // 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 +0100184 path := child.(interface {
185 DexJarBuildPath() java.OptionalDexJarPath
186 }).DexJarBuildPath()
187 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000188 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100189 module: child,
190 moduleDir: ctx.OtherModuleDir(child),
191 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100192 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100193 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000194 }
195 if module, ok := child.(java.DexpreopterInterface); ok {
196 for _, install := range module.DexpreoptBuiltInstalledForApex() {
197 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
198 }
199 }
200 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100201 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000202 } else if tag == exportedBootclasspathFragmentTag {
Jiakai Zhangb47cacc2023-05-10 16:40:18 +0100203 _, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000204 if !ok {
205 ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
206 return false
207 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000208 // Visit the children of the bootclasspath_fragment.
209 return true
210 } else if tag == exportedSystemserverclasspathFragmentTag {
211 // Visit the children of the systemserver_fragment.
Paul Duffinc30aea22021-06-15 19:10:11 +0100212 return true
213 }
214
215 return false
216 })
217}
218
Jiakai Zhang204356f2021-09-09 08:12:46 +0000219func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
220 for _, fi := range p.apexFilesForAndroidMk {
221 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
222 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
223 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
224 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000225 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000226}
227
Paul Duffinef6b6952021-06-15 11:34:01 +0100228func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100229 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100230 {
231 Class: "ETC",
232 OutputFile: android.OptionalPathForPath(p.outputApex),
233 Include: "$(BUILD_PREBUILT)",
234 Host_required: p.hostRequired,
235 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
236 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800237 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100238 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700239 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
240 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Cole Faustd22afe92023-08-18 16:05:44 -0700241 entries.AddStrings("LOCAL_SOONG_INSTALL_SYMLINKS", p.compatSymlinks.Strings()...)
Paul Duffinef6b6952021-06-15 11:34:01 +0100242 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
243 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jooyung Han286957d2023-10-30 16:17:56 +0900244 entries.SetString("LOCAL_APEX_KEY_PATH", p.apexKeysPath.String())
Jiakai Zhang204356f2021-09-09 08:12:46 +0000245 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100246 },
247 },
248 },
249 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100250
251 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
252 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
253 // apex specific variants of the exported java modules available for use from within make.
254 apexName := p.BaseModuleName()
255 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100256 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100257 entriesList = append(entriesList, entries)
258 }
259
260 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100261}
262
Paul Duffin9dc8c542021-06-17 13:33:09 +0100263// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
264func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
265 moduleName := fi.androidMkModuleName + "." + apexName
266 entries := android.AndroidMkEntries{
267 Class: fi.class.nameInMake(),
268 OverrideName: moduleName,
269 OutputFile: android.OptionalPathForPath(fi.builtFile),
270 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
271 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
272 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800273 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000274 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
275 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700276 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100277
278 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
279 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
280 // we will have foo.jar.jar
281 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100282 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
283 entries.SetString("LOCAL_DEX_PREOPT", "false")
284 },
285 },
286 ExtraFooters: []android.AndroidMkExtraFootersFunc{
287 func(w io.Writer, name, prefix, moduleDir string) {
288 // m <module_name> will build <module_name>.<apex_name> as well.
289 if fi.androidMkModuleName != moduleName {
290 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
291 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
292 }
293 },
294 },
295 }
296 return entries
297}
298
Paul Duffin5dda3e32021-05-05 14:13:27 +0100299// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
300// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
301type prebuiltApexModuleCreator interface {
302 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
303}
304
305// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
306// prebuiltApexModuleCreator's createPrebuiltApexModules method.
307//
308// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
309// will need to access dependencies added by that (exported modules) but must run before the
310// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
311// exported modules.
312func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
313 module := ctx.Module()
314 if creator, ok := module.(prebuiltApexModuleCreator); ok {
315 creator.createPrebuiltApexModules(ctx)
316 }
317}
318
Liz Kammer2dc72442023-04-20 10:10:48 -0400319func (p *prebuiltCommon) hasExportedDeps() bool {
320 return len(p.prebuiltCommonProperties.Exported_java_libs) > 0 ||
321 len(p.prebuiltCommonProperties.Exported_bootclasspath_fragments) > 0 ||
322 len(p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments) > 0
Jiakai Zhang774dd302021-09-26 03:54:25 +0000323}
324
Paul Duffin57f83592021-05-05 15:09:44 +0100325// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
326func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
327 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100328
Liz Kammer2dc72442023-04-20 10:10:48 -0400329 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
Jiakai Zhang774dd302021-09-26 03:54:25 +0000330 prebuiltDep := android.PrebuiltNameFromSource(dep)
Liz Kammer2dc72442023-04-20 10:10:48 -0400331 ctx.AddDependency(module, exportedJavaLibTag, prebuiltDep)
332 }
333
334 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
335 prebuiltDep := android.PrebuiltNameFromSource(dep)
336 ctx.AddDependency(module, exportedBootclasspathFragmentTag, prebuiltDep)
337 }
338
339 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
340 prebuiltDep := android.PrebuiltNameFromSource(dep)
341 ctx.AddDependency(module, exportedSystemserverclasspathFragmentTag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100342 }
Paul Duffindfd33262021-04-06 17:02:08 +0100343}
344
Paul Duffinb17d0442021-05-05 12:07:00 +0100345// Implements android.DepInInSameApex
346func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
347 tag := ctx.OtherModuleDependencyTag(dep)
348 _, ok := tag.(exportedDependencyTag)
349 return ok
350}
351
Paul Duffindfd33262021-04-06 17:02:08 +0100352// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
353// specific variant and checks that they are supported.
354//
355// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
356// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
357//
358// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
359// across prebuilt_apex modules. That is because there is no way to determine whether two
360// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
361// been built from different source at different times or they could have been built with different
362// build options that affect the libraries.
363//
364// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
365// modules were compatible it would be a lot of work and would not provide much benefit for a couple
366// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700367// - The number of prebuilt_apex modules that will be exporting files for the same module will be
368// low as the prebuilt_apex only exports files for the direct dependencies that require it and
369// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
370// few com.android.art* apex files that contain the same contents and could export files for the
371// same modules but only one of them needs to do so. Contrast that with source apex modules which
372// need apex specific variants for every module that contributes code to the apex, whether direct
373// or indirect.
374// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
375// extra copying of files. Contrast that with source apex modules that has to build each variant
376// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100377func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
378
379 // Collect direct dependencies into contents.
380 contents := make(map[string]android.ApexMembership)
381
382 // Collect the list of dependencies.
383 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100384 mctx.WalkDeps(func(child, parent android.Module) bool {
385 // If the child is not in the same apex as the parent then exit immediately and do not visit
386 // any of the child's dependencies.
387 if !android.IsDepInSameApex(mctx, parent, child) {
388 return false
389 }
390
391 tag := mctx.OtherModuleDependencyTag(child)
392 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100393 if exportedTag, ok := tag.(exportedDependencyTag); ok {
394 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100395
396 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100397 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100398 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100399 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100400 }
401
402 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100403 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100404 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100405 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100406 }
Paul Duffindfd33262021-04-06 17:02:08 +0100407 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100408
Paul Duffinfee8cf32021-06-29 18:38:38 +0100409 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
410 // variant.
411 if _, ok := child.(android.ApexModule); !ok {
412 return false
413 }
414
Paul Duffinb17d0442021-05-05 12:07:00 +0100415 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
416 // behavior whether there is a corresponding source module present or not.
417 depName = android.RemoveOptionalPrebuiltPrefix(depName)
418
419 // Remember if this module was added as a direct dependency.
420 direct := parent == mctx.Module()
421 contents[depName] = contents[depName].Add(direct)
422
423 // Add the module to the list of dependencies that need to have an APEX variant.
424 dependencies = append(dependencies, child.(android.ApexModule))
425
426 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100427 })
428
429 // Create contents for the prebuilt_apex and store it away for later use.
430 apexContents := android.NewApexContents(contents)
431 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
432 Contents: apexContents,
433 })
434
435 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100436 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100437 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000438 ApexVariationName: apexVariationName,
439 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100440 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100441 ApexContents: []*android.ApexContents{apexContents},
442 ForPrebuiltApex: true,
443 }
444
445 // Mark the dependencies of this module as requiring a variant for this module.
446 for _, am := range dependencies {
447 am.BuildForApex(apexInfo)
448 }
449}
450
Paul Duffin11216db2021-03-01 14:14:52 +0000451// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
452// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
453// deapexer.
454type prebuiltApexSelectorModule struct {
455 android.ModuleBase
456
457 apexFileProperties ApexFileProperties
458
459 inputApex android.Path
460}
461
462func privateApexSelectorModuleFactory() android.Module {
463 module := &prebuiltApexSelectorModule{}
464 module.AddProperties(
465 &module.apexFileProperties,
466 )
467 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
468 return module
469}
470
471func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
472 return android.Paths{p.inputApex}
473}
474
475func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
476 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
477}
478
Jiyong Park09d77522019-11-18 11:16:27 +0900479type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900480 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900481
Paul Duffinbb0dc132021-05-05 16:58:08 +0100482 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900483
Paul Duffinef6b6952021-06-15 11:34:01 +0100484 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700485
486 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900487}
488
Paul Duffin851f3992021-01-13 17:03:51 +0000489type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900490 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000491 //
492 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
493 // for android_common. That is so that it will have the same arch variant as, and so be compatible
494 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000495 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900496 Arch struct {
497 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000498 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900499 }
500 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000501 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900502 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800503 Riscv64 struct {
504 Src *string `android:"path"`
505 }
Jiyong Park09d77522019-11-18 11:16:27 +0900506 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000507 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900508 }
509 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000510 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900511 }
512 }
Paul Duffin851f3992021-01-13 17:03:51 +0000513}
514
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000515// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
516//
517// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
518// to use methods on it that are specific to the current module.
519//
520// See the ApexFileProperties.Src property.
521func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
522 multiTargets := prebuilt.MultiTargets()
523 if len(multiTargets) != 1 {
524 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
525 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000526 }
527 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000528 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000529 case android.Arm:
530 src = String(p.Arch.Arm.Src)
531 case android.Arm64:
532 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800533 case android.Riscv64:
534 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700535 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
536 if src == "" {
537 src = String(p.Arch.Arm64.Src)
538 }
Paul Duffin851f3992021-01-13 17:03:51 +0000539 case android.X86:
540 src = String(p.Arch.X86.Src)
541 case android.X86_64:
542 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000543 }
544 if src == "" {
545 src = String(p.Src)
546 }
Paul Duffin851f3992021-01-13 17:03:51 +0000547
Paul Duffinc0609c62021-03-01 17:27:16 +0000548 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700549 if ctx.Config().AllowMissingDependencies() {
550 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
551 } else {
552 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
553 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000554 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
555 // logic from reporting a more general, less useful message.
556 }
557
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000558 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000559}
560
561type PrebuiltProperties struct {
562 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900563
Paul Duffinef6b6952021-06-15 11:34:01 +0100564 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900565}
566
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700567func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
568 return false
569}
570
Jiyong Park09d77522019-11-18 11:16:27 +0900571func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
572 switch tag {
573 case "":
574 return android.Paths{p.outputApex}, nil
575 default:
576 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
577 }
578}
579
Jiyong Park09d77522019-11-18 11:16:27 +0900580// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
581func PrebuiltFactory() android.Module {
582 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100583 module.AddProperties(&module.properties)
584 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000585
Jiyong Park09d77522019-11-18 11:16:27 +0900586 return module
587}
588
Paul Duffin5dda3e32021-05-05 14:13:27 +0100589func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000590 props := struct {
591 Name *string
592 }{
593 Name: proptools.StringPtr(name),
594 }
595
596 ctx.CreateModule(privateApexSelectorModuleFactory,
597 &props,
598 apexFileProperties,
599 )
600}
601
Paul Duffin5dda3e32021-05-05 14:13:27 +0100602// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
603//
Paul Duffin57f83592021-05-05 15:09:44 +0100604// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
605// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
606// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000607func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100608 // Only create the deapexer module if it is needed.
Liz Kammer2dc72442023-04-20 10:10:48 -0400609 if !p.hasExportedDeps() {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100610 return
611 }
612
Paul Duffin57f83592021-05-05 15:09:44 +0100613 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100614 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100615 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100616 ctx.WalkDeps(func(child, parent android.Module) bool {
617 tag := ctx.OtherModuleDependencyTag(child)
618
Paul Duffin7db57e02021-06-17 14:56:05 +0100619 // If the child is not in the same apex as the parent then ignore it and all its children.
620 if !android.IsDepInSameApex(ctx, parent, child) {
621 return false
622 }
623
Paul Duffin57f83592021-05-05 15:09:44 +0100624 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100625 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100626 commonModules = append(commonModules, name)
627
628 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100629 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100630
Paul Duffin7db57e02021-06-17 14:56:05 +0100631 // Visit the dependencies of this module just in case they also require files from the
632 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100633 return true
634 }
635
636 return false
637 })
638
Paul Duffin3bae0682021-05-05 18:03:47 +0100639 // Create properties for deapexer module.
640 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100641 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100642 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100643 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100644 }
645
646 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100647 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100648
Paul Duffin11216db2021-03-01 14:14:52 +0000649 props := struct {
650 Name *string
651 Selected_apex *string
652 }{
653 Name: proptools.StringPtr(deapexerName),
654 Selected_apex: proptools.StringPtr(apexFileSource),
655 }
656 ctx.CreateModule(privateDeapexerFactory,
657 &props,
658 deapexerProperties,
659 )
660}
661
Paul Duffin11216db2021-03-01 14:14:52 +0000662func apexSelectorModuleName(baseModuleName string) string {
663 return baseModuleName + ".apex.selector"
664}
665
Paul Duffin064b70c2020-11-02 17:32:38 +0000666func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
667 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
668 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
669 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
670 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
671 // and not a renamed prebuilt module then that will be detected and reported as an error when
672 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100673 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000674 if ctx.OtherModuleExists(prebuiltName) {
675 name = prebuiltName
676 }
677 return name
678}
679
Paul Duffina7139422021-02-08 11:01:58 +0000680type exportedDependencyTag struct {
681 blueprint.BaseDependencyTag
682 name string
683}
684
685// Mark this tag so dependencies that use it are excluded from visibility enforcement.
686//
687// This does allow any prebuilt_apex to reference any module which does open up a small window for
688// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
689// avoids opening up a much bigger window by widening the visibility of modules that need files
690// provided by the prebuilt_apex to include all the possible locations they may be defined, which
691// could include everything below vendor/.
692//
693// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
694// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
695// the .apex file. It will also have to be included in the module's apex_available property too.
696// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
697// incorrectly.
698func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
699
Paul Duffin7db57e02021-06-17 14:56:05 +0100700func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
701
702var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
703
Paul Duffina7139422021-02-08 11:01:58 +0000704var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000705 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
706 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
707 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000708)
709
Paul Duffin5dda3e32021-05-05 14:13:27 +0100710var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
711
712// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
713// build.
714//
715// If this needs to make files from within a `.apex` file available for use by other Soong modules,
716// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
717// it does so as follows:
718//
Colin Crossd079e0b2022-08-16 10:27:33 -0700719// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
720// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100721//
Colin Crossd079e0b2022-08-16 10:27:33 -0700722// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
723// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
724// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100725//
Colin Crossd079e0b2022-08-16 10:27:33 -0700726// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
727// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100728//
729// It also creates a child module `selector` that is responsible for selecting the appropriate
730// 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 +0100731//
Colin Crossd079e0b2022-08-16 10:27:33 -0700732// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100733//
Colin Crossd079e0b2022-08-16 10:27:33 -0700734// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
735// `apex_set`.
736//
737// prebuilt_apex
738// / | \
739// / | \
740// V V V
741// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100742func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
743 baseModuleName := p.BaseModuleName()
744
745 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
746 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
747
748 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000749 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100750
751 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100752 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100753}
754
Paul Duffin57f83592021-05-05 15:09:44 +0100755func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
756 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000757}
758
759var _ ApexInfoMutator = (*Prebuilt)(nil)
760
Paul Duffin064b70c2020-11-02 17:32:38 +0000761func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100762 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900763}
764
765func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900766 p.apexKeysPath = writeApexKeys(ctx, p)
Jiyong Park09d77522019-11-18 11:16:27 +0900767 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100768 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900769 p.installDir = android.PathForModuleInstall(ctx, "apex")
770 p.installFilename = p.InstallFilename()
771 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
772 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
773 }
774 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
775 ctx.Build(pctx, android.BuildParams{
776 Rule: android.Cp,
777 Input: p.inputApex,
778 Output: p.outputApex,
779 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900780
781 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800782 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900783 return
784 }
785
Paul Duffinc30aea22021-06-15 19:10:11 +0100786 // Save the files that need to be made available to Make.
787 p.initApexFilesForAndroidMk(ctx)
788
Colin Crossccba23d2021-11-12 19:01:29 +0000789 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900790 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
Colin Crossccba23d2021-11-12 19:01:29 +0000791 // or that prebuilt_apex overrides other apexes (using overrides: prop)
792 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han06a8a1c2023-08-23 11:11:43 +0900793 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
Colin Cross6340ea52021-11-04 12:01:18 -0700794 }
795
796 if p.installable() {
Colin Cross09ad3a62023-11-15 12:29:33 -0800797 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks...)
Wei Li340ee8e2022-03-18 17:33:24 -0700798 p.provenanceMetaDataFile = provenance.GenerateArtifactProvenanceMetaData(ctx, p.inputApex, p.installedFile)
Jooyung Han002ab682020-01-08 01:57:58 +0900799 }
Jiyong Park09d77522019-11-18 11:16:27 +0900800}
801
Wei Li340ee8e2022-03-18 17:33:24 -0700802func (p *Prebuilt) ProvenanceMetaDataFile() android.OutputPath {
803 return p.provenanceMetaDataFile
804}
805
Paul Duffin24704672021-04-06 16:09:30 +0100806// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
807// module. It extracts the correct apex to use and makes it available for use by apex_set.
808type prebuiltApexExtractorModule struct {
809 android.ModuleBase
810
811 properties ApexExtractorProperties
812
813 extractedApex android.WritablePath
814}
815
816func privateApexExtractorModuleFactory() android.Module {
817 module := &prebuiltApexExtractorModule{}
818 module.AddProperties(
819 &module.properties,
820 )
821 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
822 return module
823}
824
825func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
826 return android.Paths{p.extractedApex}
827}
828
829func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
830 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
831 return p.properties.prebuiltSrcs(ctx)
832 }
Paul Duffin76fdd672022-12-12 18:00:47 +0000833 defaultAllowPrerelease := ctx.Config().IsEnvTrue("SOONG_ALLOW_PRERELEASE_APEXES")
Paul Duffin24704672021-04-06 16:09:30 +0100834 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
835 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000836 // Filter out NativeBridge archs (b/260115309)
837 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100838 ctx.Build(pctx,
839 android.BuildParams{
840 Rule: extractMatchingApex,
841 Description: "Extract an apex from an apex set",
842 Inputs: android.Paths{apexSet},
843 Output: p.extractedApex,
844 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000845 "abis": strings.Join(abis, ","),
Paul Duffin76fdd672022-12-12 18:00:47 +0000846 "allow-prereleased": strconv.FormatBool(proptools.BoolDefault(p.properties.Prerelease, defaultAllowPrerelease)),
Paul Duffin24704672021-04-06 16:09:30 +0100847 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
Pranav Gupta51645ff2023-03-20 16:19:53 -0700848 "skip-sdk-check": strconv.FormatBool(ctx.Config().IsEnvTrue("SOONG_SKIP_APPSET_SDK_CHECK")),
Paul Duffin24704672021-04-06 16:09:30 +0100849 },
850 })
851}
852
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700853type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900854 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700855
856 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700857}
858
Paul Duffin24704672021-04-06 16:09:30 +0100859type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700860 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000861 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700862
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700863 Sanitized struct {
864 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000865 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700866 }
867 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000868 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700869 }
870 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000871 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700872 }
873 }
874
Paul Duffin24704672021-04-06 16:09:30 +0100875 // apexes in this set use prerelease SDK version
876 Prerelease *bool
877}
878
879func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
880 var srcs []string
881 if e.Set != nil {
882 srcs = append(srcs, *e.Set)
883 }
884
Jooyung Han8d4a1f02023-08-23 13:54:08 +0900885 sanitizers := ctx.Config().SanitizeDevice()
Paul Duffin24704672021-04-06 16:09:30 +0100886
887 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
888 srcs = append(srcs, *e.Sanitized.Address.Set)
889 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
890 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
891 } else if e.Sanitized.None.Set != nil {
892 srcs = append(srcs, *e.Sanitized.None.Set)
893 }
894
895 return srcs
896}
897
898type ApexSetProperties struct {
899 ApexExtractorProperties
900
Paul Duffinef6b6952021-06-15 11:34:01 +0100901 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700902}
903
904func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
905 if sanitizer == "address" {
906 return a.properties.Sanitized.Address.Set != nil
907 }
908 if sanitizer == "hwaddress" {
909 return a.properties.Sanitized.Hwaddress.Set != nil
910 }
911
912 return false
913}
914
Paul Duffin4f2282c2022-12-12 17:44:33 +0000915func (a *ApexSet) OutputFiles(tag string) (android.Paths, error) {
916 switch tag {
917 case "":
918 return android.Paths{a.outputApex}, nil
919 default:
920 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
921 }
922}
923
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700924// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
925func apexSetFactory() android.Module {
926 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100927 module.AddProperties(&module.properties)
928 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100929
Paul Duffin24704672021-04-06 16:09:30 +0100930 return module
931}
932
Paul Duffin5dda3e32021-05-05 14:13:27 +0100933func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100934 props := struct {
935 Name *string
936 }{
937 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700938 }
939
Paul Duffin24704672021-04-06 16:09:30 +0100940 ctx.CreateModule(privateApexExtractorModuleFactory,
941 &props,
942 apexExtractorProperties,
943 )
944}
945
946func apexExtractorModuleName(baseModuleName string) string {
947 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700948}
949
Paul Duffin5dda3e32021-05-05 14:13:27 +0100950var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
951
952// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
953// modules.
954//
955// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
956// prebuilt_apex except that instead of creating a selector module which selects one .apex file
957// from those provided this creates an extractor module which extracts the appropriate .apex file
958// from the zip file containing them.
959func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
960 baseModuleName := a.BaseModuleName()
961
962 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
963 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
964
965 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000966 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100967
968 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100969 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100970}
971
Paul Duffin57f83592021-05-05 15:09:44 +0100972func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
973 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100974}
975
976var _ ApexInfoMutator = (*ApexSet)(nil)
977
978func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
979 a.apexInfoMutator(mctx)
980}
981
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700982func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jooyung Han286957d2023-10-30 16:17:56 +0900983 a.apexKeysPath = writeApexKeys(ctx, a)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700984 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)
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001011 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
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 {
Jooyung Han06a8a1c2023-08-23 11:11:43 +09001014 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
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}