blob: 6fdd50a5e6042c5260833581ead75dc904223bfb [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} ` +
38 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
39 `${in}`,
40 CommandDeps: []string{"${extract_apks}"},
41 },
42 "abis", "allow-prereleased", "sdk-version")
43)
44
Jiyong Park10e926b2020-07-16 21:38:56 +090045type prebuilt interface {
46 isForceDisabled() bool
47 InstallFilename() string
48}
49
50type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010051 android.ModuleBase
Paul Duffinbb0dc132021-05-05 16:58:08 +010052 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010053
Paul Duffinbb0dc132021-05-05 16:58:08 +010054 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010055 prebuiltCommonProperties *PrebuiltCommonProperties
56
57 installDir android.InstallPath
58 installFilename string
Colin Cross6340ea52021-11-04 12:01:18 -070059 installedFile android.InstallPath
Paul Duffinef6b6952021-06-15 11:34:01 +010060 outputApex android.WritablePath
61
Paul Duffinc30aea22021-06-15 19:10:11 +010062 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
63 // to create make modules in prebuiltCommon.AndroidMkEntries.
64 apexFilesForAndroidMk []apexFile
65
Colin Cross6340ea52021-11-04 12:01:18 -070066 // Installed locations of symlinks for backward compatibility.
67 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010068
Jiakai Zhange6e90db2022-01-28 14:58:56 +000069 hostRequired []string
70 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090071}
72
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070073type sanitizedPrebuilt interface {
74 hasSanitizedSource(sanitizer string) bool
75}
76
Paul Duffinef6b6952021-06-15 11:34:01 +010077type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010078 SelectedApexProperties
79
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010080 // Canonical name of this APEX. Used to determine the path to the activated APEX on
81 // device (/apex/<apex_name>). If unspecified, follows the name property.
82 Apex_name *string
83
Jiyong Park10e926b2020-07-16 21:38:56 +090084 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010085
Paul Duffinef6b6952021-06-15 11:34:01 +010086 // whether the extracted apex file is installable.
87 Installable *bool
88
89 // optional name for the installed apex. If unspecified, name of the
90 // module is used as the file name
91 Filename *string
92
93 // names of modules to be overridden. Listed modules can only be other binaries
94 // (in Make or Soong).
95 // This does not completely prevent installation of the overridden binaries, but if both
96 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
97 // from PRODUCT_PACKAGES.
98 Overrides []string
99
Paul Duffin3bae0682021-05-05 18:03:47 +0100100 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
101 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
102 // dexpreopt and boot jars package check.
103 Exported_java_libs []string
104
105 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
106 // bundle will create an APEX variant.
107 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000108
109 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
110 // APEX bundle will create an APEX variant.
111 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900112}
113
Paul Duffinef6b6952021-06-15 11:34:01 +0100114// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
115// module that is common to Prebuilt and ApexSet.
116func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
117 p.prebuiltCommonProperties = properties
118 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
119 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
120}
121
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100122func (p *prebuiltCommon) ApexVariationName() string {
123 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
124}
125
Jiyong Park10e926b2020-07-16 21:38:56 +0900126func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
127 return &p.prebuilt
128}
129
130func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100131 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900132}
133
134func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
135 // If the device is configured to use flattened APEX, force disable the prebuilt because
136 // the prebuilt is a non-flattened one.
137 forceDisable := ctx.Config().FlattenApex()
138
139 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
140 // to build the prebuilts themselves.
141 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
142
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700143 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
144 sanitized := ctx.Module().(sanitizedPrebuilt)
145 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
146 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900147
148 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100149 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900150 return true
151 }
152 return false
153}
154
Paul Duffinef6b6952021-06-15 11:34:01 +0100155func (p *prebuiltCommon) InstallFilename() string {
156 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
157}
158
159func (p *prebuiltCommon) Name() string {
160 return p.prebuilt.Name(p.ModuleBase.Name())
161}
162
163func (p *prebuiltCommon) Overrides() []string {
164 return p.prebuiltCommonProperties.Overrides
165}
166
167func (p *prebuiltCommon) installable() bool {
168 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
169}
170
Paul Duffinc30aea22021-06-15 19:10:11 +0100171// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
172// modules that this depends upon.
173func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
174 // Walk the dependencies of this module looking for the java modules that it exports.
175 ctx.WalkDeps(func(child, parent android.Module) bool {
176 tag := ctx.OtherModuleDependencyTag(child)
177
178 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000179 if java.IsBootclasspathFragmentContentDepTag(tag) ||
180 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100181 // 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 +0100182 path := child.(interface {
183 DexJarBuildPath() java.OptionalDexJarPath
184 }).DexJarBuildPath()
185 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000186 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100187 module: child,
188 moduleDir: ctx.OtherModuleDir(child),
189 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100190 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100191 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000192 }
193 if module, ok := child.(java.DexpreopterInterface); ok {
194 for _, install := range module.DexpreoptBuiltInstalledForApex() {
195 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
196 }
197 }
198 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100199 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000200 } else if tag == exportedBootclasspathFragmentTag {
201 bcpfModule, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
202 if !ok {
203 ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
204 return false
205 }
206 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
207 p.requiredModuleNames = append(p.requiredModuleNames, makeModuleName)
208 }
209 // Visit the children of the bootclasspath_fragment.
210 return true
211 } else if tag == exportedSystemserverclasspathFragmentTag {
212 // Visit the children of the systemserver_fragment.
Paul Duffinc30aea22021-06-15 19:10:11 +0100213 return true
214 }
215
216 return false
217 })
218}
219
Jiakai Zhang204356f2021-09-09 08:12:46 +0000220func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
221 for _, fi := range p.apexFilesForAndroidMk {
222 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
223 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
224 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
225 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000226 entries.AddStrings("LOCAL_REQUIRED_MODULES", p.requiredModuleNames...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000227}
228
Paul Duffinef6b6952021-06-15 11:34:01 +0100229func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100230 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100231 {
232 Class: "ETC",
233 OutputFile: android.OptionalPathForPath(p.outputApex),
234 Include: "$(BUILD_PREBUILT)",
235 Host_required: p.hostRequired,
236 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
237 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800238 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100239 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700240 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
241 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100242 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
243 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000244 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100245 },
246 },
247 },
248 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100249
250 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
251 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
252 // apex specific variants of the exported java modules available for use from within make.
253 apexName := p.BaseModuleName()
254 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100255 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100256 entriesList = append(entriesList, entries)
257 }
258
259 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100260}
261
Paul Duffin9dc8c542021-06-17 13:33:09 +0100262// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
263func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
264 moduleName := fi.androidMkModuleName + "." + apexName
265 entries := android.AndroidMkEntries{
266 Class: fi.class.nameInMake(),
267 OverrideName: moduleName,
268 OutputFile: android.OptionalPathForPath(fi.builtFile),
269 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
270 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
271 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800272 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000273 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
274 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700275 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100276
277 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
278 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
279 // we will have foo.jar.jar
280 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100281 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
282 entries.SetString("LOCAL_DEX_PREOPT", "false")
283 },
284 },
285 ExtraFooters: []android.AndroidMkExtraFootersFunc{
286 func(w io.Writer, name, prefix, moduleDir string) {
287 // m <module_name> will build <module_name>.<apex_name> as well.
288 if fi.androidMkModuleName != moduleName {
289 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
290 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
291 }
292 },
293 },
294 }
295 return entries
296}
297
Paul Duffin5dda3e32021-05-05 14:13:27 +0100298// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
299// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
300type prebuiltApexModuleCreator interface {
301 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
302}
303
304// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
305// prebuiltApexModuleCreator's createPrebuiltApexModules method.
306//
307// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
308// will need to access dependencies added by that (exported modules) but must run before the
309// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
310// exported modules.
311func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
312 module := ctx.Module()
313 if creator, ok := module.(prebuiltApexModuleCreator); ok {
314 creator.createPrebuiltApexModules(ctx)
315 }
316}
317
Jiakai Zhang774dd302021-09-26 03:54:25 +0000318func (p *prebuiltCommon) getExportedDependencies() map[string]exportedDependencyTag {
319 dependencies := make(map[string]exportedDependencyTag)
320
321 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
322 dependencies[dep] = exportedJavaLibTag
323 }
324
325 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
326 dependencies[dep] = exportedBootclasspathFragmentTag
327 }
328
329 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
330 dependencies[dep] = exportedSystemserverclasspathFragmentTag
331 }
332
333 return dependencies
334}
335
Paul Duffin57f83592021-05-05 15:09:44 +0100336// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
337func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
338 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100339
Jiakai Zhang774dd302021-09-26 03:54:25 +0000340 for dep, tag := range p.getExportedDependencies() {
341 prebuiltDep := android.PrebuiltNameFromSource(dep)
342 ctx.AddDependency(module, tag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100343 }
Paul Duffindfd33262021-04-06 17:02:08 +0100344}
345
Paul Duffinb17d0442021-05-05 12:07:00 +0100346// Implements android.DepInInSameApex
347func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
348 tag := ctx.OtherModuleDependencyTag(dep)
349 _, ok := tag.(exportedDependencyTag)
350 return ok
351}
352
Paul Duffindfd33262021-04-06 17:02:08 +0100353// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
354// specific variant and checks that they are supported.
355//
356// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
357// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
358//
359// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
360// across prebuilt_apex modules. That is because there is no way to determine whether two
361// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
362// been built from different source at different times or they could have been built with different
363// build options that affect the libraries.
364//
365// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
366// modules were compatible it would be a lot of work and would not provide much benefit for a couple
367// of reasons:
Colin Crossd079e0b2022-08-16 10:27:33 -0700368// - The number of prebuilt_apex modules that will be exporting files for the same module will be
369// low as the prebuilt_apex only exports files for the direct dependencies that require it and
370// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
371// few com.android.art* apex files that contain the same contents and could export files for the
372// same modules but only one of them needs to do so. Contrast that with source apex modules which
373// need apex specific variants for every module that contributes code to the apex, whether direct
374// or indirect.
375// - The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
376// extra copying of files. Contrast that with source apex modules that has to build each variant
377// from source.
Paul Duffindfd33262021-04-06 17:02:08 +0100378func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
379
380 // Collect direct dependencies into contents.
381 contents := make(map[string]android.ApexMembership)
382
383 // Collect the list of dependencies.
384 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100385 mctx.WalkDeps(func(child, parent android.Module) bool {
386 // If the child is not in the same apex as the parent then exit immediately and do not visit
387 // any of the child's dependencies.
388 if !android.IsDepInSameApex(mctx, parent, child) {
389 return false
390 }
391
392 tag := mctx.OtherModuleDependencyTag(child)
393 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100394 if exportedTag, ok := tag.(exportedDependencyTag); ok {
395 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100396
397 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100398 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100399 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100400 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100401 }
402
403 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100404 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100405 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100406 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100407 }
Paul Duffindfd33262021-04-06 17:02:08 +0100408 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100409
Paul Duffinfee8cf32021-06-29 18:38:38 +0100410 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
411 // variant.
412 if _, ok := child.(android.ApexModule); !ok {
413 return false
414 }
415
Paul Duffinb17d0442021-05-05 12:07:00 +0100416 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
417 // behavior whether there is a corresponding source module present or not.
418 depName = android.RemoveOptionalPrebuiltPrefix(depName)
419
420 // Remember if this module was added as a direct dependency.
421 direct := parent == mctx.Module()
422 contents[depName] = contents[depName].Add(direct)
423
424 // Add the module to the list of dependencies that need to have an APEX variant.
425 dependencies = append(dependencies, child.(android.ApexModule))
426
427 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100428 })
429
430 // Create contents for the prebuilt_apex and store it away for later use.
431 apexContents := android.NewApexContents(contents)
432 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
433 Contents: apexContents,
434 })
435
436 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100437 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100438 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000439 ApexVariationName: apexVariationName,
440 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100441 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100442 ApexContents: []*android.ApexContents{apexContents},
443 ForPrebuiltApex: true,
444 }
445
446 // Mark the dependencies of this module as requiring a variant for this module.
447 for _, am := range dependencies {
448 am.BuildForApex(apexInfo)
449 }
450}
451
Paul Duffin11216db2021-03-01 14:14:52 +0000452// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
453// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
454// deapexer.
455type prebuiltApexSelectorModule struct {
456 android.ModuleBase
457
458 apexFileProperties ApexFileProperties
459
460 inputApex android.Path
461}
462
463func privateApexSelectorModuleFactory() android.Module {
464 module := &prebuiltApexSelectorModule{}
465 module.AddProperties(
466 &module.apexFileProperties,
467 )
468 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
469 return module
470}
471
472func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
473 return android.Paths{p.inputApex}
474}
475
476func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
477 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
478}
479
Jiyong Park09d77522019-11-18 11:16:27 +0900480type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900481 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900482
Paul Duffinbb0dc132021-05-05 16:58:08 +0100483 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900484
Paul Duffinef6b6952021-06-15 11:34:01 +0100485 inputApex android.Path
Wei Li340ee8e2022-03-18 17:33:24 -0700486
487 provenanceMetaDataFile android.OutputPath
Jiyong Park09d77522019-11-18 11:16:27 +0900488}
489
Paul Duffin851f3992021-01-13 17:03:51 +0000490type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900491 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000492 //
493 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
494 // for android_common. That is so that it will have the same arch variant as, and so be compatible
495 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000496 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900497 Arch struct {
498 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000499 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900500 }
501 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000502 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900503 }
Chen Guoyin401f2982022-10-12 19:28:48 +0800504 Riscv64 struct {
505 Src *string `android:"path"`
506 }
Jiyong Park09d77522019-11-18 11:16:27 +0900507 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000508 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900509 }
510 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000511 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900512 }
513 }
Paul Duffin851f3992021-01-13 17:03:51 +0000514}
515
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000516// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
517//
518// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
519// to use methods on it that are specific to the current module.
520//
521// See the ApexFileProperties.Src property.
522func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
523 multiTargets := prebuilt.MultiTargets()
524 if len(multiTargets) != 1 {
525 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
526 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000527 }
528 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000529 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000530 case android.Arm:
531 src = String(p.Arch.Arm.Src)
532 case android.Arm64:
533 src = String(p.Arch.Arm64.Src)
Chen Guoyin401f2982022-10-12 19:28:48 +0800534 case android.Riscv64:
535 src = String(p.Arch.Riscv64.Src)
Colin Crossabacbe82022-11-01 09:26:51 -0700536 // HACK: fall back to arm64 prebuilts, the riscv64 ones don't exist yet.
537 if src == "" {
538 src = String(p.Arch.Arm64.Src)
539 }
Paul Duffin851f3992021-01-13 17:03:51 +0000540 case android.X86:
541 src = String(p.Arch.X86.Src)
542 case android.X86_64:
543 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000544 }
545 if src == "" {
546 src = String(p.Src)
547 }
Paul Duffin851f3992021-01-13 17:03:51 +0000548
Paul Duffinc0609c62021-03-01 17:27:16 +0000549 if src == "" {
Colin Cross553a31b2022-10-03 22:02:09 -0700550 if ctx.Config().AllowMissingDependencies() {
551 ctx.AddMissingDependencies([]string{ctx.OtherModuleName(prebuilt)})
552 } else {
553 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
554 }
Paul Duffinc0609c62021-03-01 17:27:16 +0000555 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
556 // logic from reporting a more general, less useful message.
557 }
558
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000559 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000560}
561
562type PrebuiltProperties struct {
563 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900564
Paul Duffinef6b6952021-06-15 11:34:01 +0100565 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900566}
567
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700568func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
569 return false
570}
571
Jiyong Park09d77522019-11-18 11:16:27 +0900572func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
573 switch tag {
574 case "":
575 return android.Paths{p.outputApex}, nil
576 default:
577 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
578 }
579}
580
Jiyong Park09d77522019-11-18 11:16:27 +0900581// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
582func PrebuiltFactory() android.Module {
583 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100584 module.AddProperties(&module.properties)
585 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000586
Jiyong Park09d77522019-11-18 11:16:27 +0900587 return module
588}
589
Paul Duffin5dda3e32021-05-05 14:13:27 +0100590func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000591 props := struct {
592 Name *string
593 }{
594 Name: proptools.StringPtr(name),
595 }
596
597 ctx.CreateModule(privateApexSelectorModuleFactory,
598 &props,
599 apexFileProperties,
600 )
601}
602
Paul Duffin5dda3e32021-05-05 14:13:27 +0100603// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
604//
Paul Duffin57f83592021-05-05 15:09:44 +0100605// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
606// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
607// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000608func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100609 // Only create the deapexer module if it is needed.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000610 if len(p.getExportedDependencies()) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100611 return
612 }
613
Paul Duffin57f83592021-05-05 15:09:44 +0100614 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100615 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100616 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100617 ctx.WalkDeps(func(child, parent android.Module) bool {
618 tag := ctx.OtherModuleDependencyTag(child)
619
Paul Duffin7db57e02021-06-17 14:56:05 +0100620 // If the child is not in the same apex as the parent then ignore it and all its children.
621 if !android.IsDepInSameApex(ctx, parent, child) {
622 return false
623 }
624
Paul Duffin57f83592021-05-05 15:09:44 +0100625 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100626 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100627 commonModules = append(commonModules, name)
628
629 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100630 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100631
Paul Duffin7db57e02021-06-17 14:56:05 +0100632 // Visit the dependencies of this module just in case they also require files from the
633 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100634 return true
635 }
636
637 return false
638 })
639
Paul Duffin3bae0682021-05-05 18:03:47 +0100640 // Create properties for deapexer module.
641 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100642 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100643 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100644 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100645 }
646
647 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100648 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100649
Paul Duffin11216db2021-03-01 14:14:52 +0000650 props := struct {
651 Name *string
652 Selected_apex *string
653 }{
654 Name: proptools.StringPtr(deapexerName),
655 Selected_apex: proptools.StringPtr(apexFileSource),
656 }
657 ctx.CreateModule(privateDeapexerFactory,
658 &props,
659 deapexerProperties,
660 )
661}
662
Paul Duffin11216db2021-03-01 14:14:52 +0000663func apexSelectorModuleName(baseModuleName string) string {
664 return baseModuleName + ".apex.selector"
665}
666
Paul Duffin064b70c2020-11-02 17:32:38 +0000667func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
668 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
669 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
670 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
671 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
672 // and not a renamed prebuilt module then that will be detected and reported as an error when
673 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100674 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000675 if ctx.OtherModuleExists(prebuiltName) {
676 name = prebuiltName
677 }
678 return name
679}
680
Paul Duffina7139422021-02-08 11:01:58 +0000681type exportedDependencyTag struct {
682 blueprint.BaseDependencyTag
683 name string
684}
685
686// Mark this tag so dependencies that use it are excluded from visibility enforcement.
687//
688// This does allow any prebuilt_apex to reference any module which does open up a small window for
689// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
690// avoids opening up a much bigger window by widening the visibility of modules that need files
691// provided by the prebuilt_apex to include all the possible locations they may be defined, which
692// could include everything below vendor/.
693//
694// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
695// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
696// the .apex file. It will also have to be included in the module's apex_available property too.
697// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
698// incorrectly.
699func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
700
Paul Duffin7db57e02021-06-17 14:56:05 +0100701func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
702
703var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
704
Paul Duffina7139422021-02-08 11:01:58 +0000705var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000706 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
707 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
708 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000709)
710
Paul Duffin5dda3e32021-05-05 14:13:27 +0100711var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
712
713// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
714// build.
715//
716// If this needs to make files from within a `.apex` file available for use by other Soong modules,
717// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
718// it does so as follows:
719//
Colin Crossd079e0b2022-08-16 10:27:33 -0700720// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
721// makes them available for use by other modules, at both Soong and ninja levels.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100722//
Colin Crossd079e0b2022-08-16 10:27:33 -0700723// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
724// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
725// dexpreopt, will work the same way from source and prebuilt.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100726//
Colin Crossd079e0b2022-08-16 10:27:33 -0700727// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
728// itself so that they can retrieve the file paths to those files.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100729//
730// It also creates a child module `selector` that is responsible for selecting the appropriate
731// 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 +0100732//
Colin Crossd079e0b2022-08-16 10:27:33 -0700733// 1. To dedup the selection logic so it only runs in one module.
Paul Duffin5dda3e32021-05-05 14:13:27 +0100734//
Colin Crossd079e0b2022-08-16 10:27:33 -0700735// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
736// `apex_set`.
737//
738// prebuilt_apex
739// / | \
740// / | \
741// V V V
742// selector <--- deapexer <--- exported java lib
Paul Duffin5dda3e32021-05-05 14:13:27 +0100743func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
744 baseModuleName := p.BaseModuleName()
745
746 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
747 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
748
749 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000750 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100751
752 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100753 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100754}
755
Paul Duffin57f83592021-05-05 15:09:44 +0100756func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
757 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000758}
759
760var _ ApexInfoMutator = (*Prebuilt)(nil)
761
Paul Duffin064b70c2020-11-02 17:32:38 +0000762func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100763 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900764}
765
766func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
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)
Colin Cross6340ea52021-11-04 12:01:18 -0700790 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
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 {
Colin Cross6340ea52021-11-04 12:01:18 -0700793 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
794 }
795
796 if p.installable() {
797 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
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 }
833 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
834 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
Anton Hansson805e0a52022-11-25 14:06:46 +0000835 // Filter out NativeBridge archs (b/260115309)
836 abis := java.SupportedAbis(ctx, true)
Paul Duffin24704672021-04-06 16:09:30 +0100837 ctx.Build(pctx,
838 android.BuildParams{
839 Rule: extractMatchingApex,
840 Description: "Extract an apex from an apex set",
841 Inputs: android.Paths{apexSet},
842 Output: p.extractedApex,
843 Args: map[string]string{
Anton Hansson805e0a52022-11-25 14:06:46 +0000844 "abis": strings.Join(abis, ","),
Paul Duffin24704672021-04-06 16:09:30 +0100845 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
846 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
847 },
848 })
849}
850
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700851type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900852 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700853
854 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700855}
856
Paul Duffin24704672021-04-06 16:09:30 +0100857type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700858 // the .apks file path that contains prebuilt apex files to be extracted.
Pranav Guptaeba03b02022-09-27 00:27:08 +0000859 Set *string `android:"path"`
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700860
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700861 Sanitized struct {
862 None struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000863 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700864 }
865 Address struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000866 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700867 }
868 Hwaddress struct {
Pranav Guptaeba03b02022-09-27 00:27:08 +0000869 Set *string `android:"path"`
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700870 }
871 }
872
Paul Duffin24704672021-04-06 16:09:30 +0100873 // apexes in this set use prerelease SDK version
874 Prerelease *bool
875}
876
877func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
878 var srcs []string
879 if e.Set != nil {
880 srcs = append(srcs, *e.Set)
881 }
882
883 var sanitizers []string
884 if ctx.Host() {
885 sanitizers = ctx.Config().SanitizeHost()
886 } else {
887 sanitizers = ctx.Config().SanitizeDevice()
888 }
889
890 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
891 srcs = append(srcs, *e.Sanitized.Address.Set)
892 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
893 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
894 } else if e.Sanitized.None.Set != nil {
895 srcs = append(srcs, *e.Sanitized.None.Set)
896 }
897
898 return srcs
899}
900
901type ApexSetProperties struct {
902 ApexExtractorProperties
903
Paul Duffinef6b6952021-06-15 11:34:01 +0100904 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700905}
906
907func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
908 if sanitizer == "address" {
909 return a.properties.Sanitized.Address.Set != nil
910 }
911 if sanitizer == "hwaddress" {
912 return a.properties.Sanitized.Hwaddress.Set != nil
913 }
914
915 return false
916}
917
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700918// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
919func apexSetFactory() android.Module {
920 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100921 module.AddProperties(&module.properties)
922 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100923
Paul Duffin24704672021-04-06 16:09:30 +0100924 return module
925}
926
Paul Duffin5dda3e32021-05-05 14:13:27 +0100927func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100928 props := struct {
929 Name *string
930 }{
931 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700932 }
933
Paul Duffin24704672021-04-06 16:09:30 +0100934 ctx.CreateModule(privateApexExtractorModuleFactory,
935 &props,
936 apexExtractorProperties,
937 )
938}
939
940func apexExtractorModuleName(baseModuleName string) string {
941 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700942}
943
Paul Duffin5dda3e32021-05-05 14:13:27 +0100944var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
945
946// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
947// modules.
948//
949// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
950// prebuilt_apex except that instead of creating a selector module which selects one .apex file
951// from those provided this creates an extractor module which extracts the appropriate .apex file
952// from the zip file containing them.
953func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
954 baseModuleName := a.BaseModuleName()
955
956 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
957 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
958
959 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000960 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100961
962 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100963 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100964}
965
Paul Duffin57f83592021-05-05 15:09:44 +0100966func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
967 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100968}
969
970var _ ApexInfoMutator = (*ApexSet)(nil)
971
972func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
973 a.apexInfoMutator(mctx)
974}
975
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700976func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
977 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100978 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
979 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700980 }
981
Paul Duffinbb0dc132021-05-05 16:58:08 +0100982 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700983 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100984 ctx.Build(pctx, android.BuildParams{
985 Rule: android.Cp,
986 Input: inputApex,
987 Output: a.outputApex,
988 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900989
990 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800991 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900992 return
993 }
994
Paul Duffinc30aea22021-06-15 19:10:11 +0100995 // Save the files that need to be made available to Make.
996 a.initApexFilesForAndroidMk(ctx)
997
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700998 a.installDir = android.PathForModuleInstall(ctx, "apex")
999 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -08001000 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001001 }
1002
1003 // in case that apex_set replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -07001004 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001005 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +01001006 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -07001007 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001008 }
1009}
1010
Paul Duffinef6b6952021-06-15 11:34:01 +01001011type systemExtContext struct {
1012 android.ModuleContext
1013}
1014
1015func (*systemExtContext) SystemExtSpecific() bool {
1016 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -07001017}