blob: 158c8046f4de7f001049fa720d890bcc77aaa638 [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"
Colin Cross6340ea52021-11-04 12:01:18 -070026
Jaewoong Jungfa00c062020-05-14 14:15:24 -070027 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090028 "github.com/google/blueprint/proptools"
29)
30
Jaewoong Jungfa00c062020-05-14 14:15:24 -070031var (
32 extractMatchingApex = pctx.StaticRule(
33 "extractMatchingApex",
34 blueprint.RuleParams{
35 Command: `rm -rf "$out" && ` +
36 `${extract_apks} -o "${out}" -allow-prereleased=${allow-prereleased} ` +
37 `-sdk-version=${sdk-version} -abis=${abis} -screen-densities=all -extract-single ` +
38 `${in}`,
39 CommandDeps: []string{"${extract_apks}"},
40 },
41 "abis", "allow-prereleased", "sdk-version")
42)
43
Jiyong Park10e926b2020-07-16 21:38:56 +090044type prebuilt interface {
45 isForceDisabled() bool
46 InstallFilename() string
47}
48
49type prebuiltCommon struct {
Paul Duffinef6b6952021-06-15 11:34:01 +010050 android.ModuleBase
Paul Duffinbb0dc132021-05-05 16:58:08 +010051 prebuilt android.Prebuilt
Paul Duffindfd33262021-04-06 17:02:08 +010052
Paul Duffinbb0dc132021-05-05 16:58:08 +010053 // Properties common to both prebuilt_apex and apex_set.
Paul Duffinef6b6952021-06-15 11:34:01 +010054 prebuiltCommonProperties *PrebuiltCommonProperties
55
56 installDir android.InstallPath
57 installFilename string
Colin Cross6340ea52021-11-04 12:01:18 -070058 installedFile android.InstallPath
Paul Duffinef6b6952021-06-15 11:34:01 +010059 outputApex android.WritablePath
60
Paul Duffinc30aea22021-06-15 19:10:11 +010061 // A list of apexFile objects created in prebuiltCommon.initApexFilesForAndroidMk which are used
62 // to create make modules in prebuiltCommon.AndroidMkEntries.
63 apexFilesForAndroidMk []apexFile
64
Colin Cross6340ea52021-11-04 12:01:18 -070065 // Installed locations of symlinks for backward compatibility.
66 compatSymlinks android.InstallPaths
Paul Duffinef6b6952021-06-15 11:34:01 +010067
Jiakai Zhange6e90db2022-01-28 14:58:56 +000068 hostRequired []string
69 requiredModuleNames []string
Jiyong Park10e926b2020-07-16 21:38:56 +090070}
71
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070072type sanitizedPrebuilt interface {
73 hasSanitizedSource(sanitizer string) bool
74}
75
Paul Duffinef6b6952021-06-15 11:34:01 +010076type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010077 SelectedApexProperties
78
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010079 // Canonical name of this APEX. Used to determine the path to the activated APEX on
80 // device (/apex/<apex_name>). If unspecified, follows the name property.
81 Apex_name *string
82
Jiyong Park10e926b2020-07-16 21:38:56 +090083 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010084
Paul Duffinef6b6952021-06-15 11:34:01 +010085 // whether the extracted apex file is installable.
86 Installable *bool
87
88 // optional name for the installed apex. If unspecified, name of the
89 // module is used as the file name
90 Filename *string
91
92 // names of modules to be overridden. Listed modules can only be other binaries
93 // (in Make or Soong).
94 // This does not completely prevent installation of the overridden binaries, but if both
95 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
96 // from PRODUCT_PACKAGES.
97 Overrides []string
98
Paul Duffin3bae0682021-05-05 18:03:47 +010099 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
100 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
101 // dexpreopt and boot jars package check.
102 Exported_java_libs []string
103
104 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
105 // bundle will create an APEX variant.
106 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000107
108 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
109 // APEX bundle will create an APEX variant.
110 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900111}
112
Paul Duffinef6b6952021-06-15 11:34:01 +0100113// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
114// module that is common to Prebuilt and ApexSet.
115func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
116 p.prebuiltCommonProperties = properties
117 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
118 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
119}
120
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100121func (p *prebuiltCommon) ApexVariationName() string {
122 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
123}
124
Jiyong Park10e926b2020-07-16 21:38:56 +0900125func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
126 return &p.prebuilt
127}
128
129func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100130 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900131}
132
133func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
134 // If the device is configured to use flattened APEX, force disable the prebuilt because
135 // the prebuilt is a non-flattened one.
136 forceDisable := ctx.Config().FlattenApex()
137
138 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
139 // to build the prebuilts themselves.
140 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
141
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700142 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
143 sanitized := ctx.Module().(sanitizedPrebuilt)
144 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
145 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900146
147 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100148 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900149 return true
150 }
151 return false
152}
153
Paul Duffinef6b6952021-06-15 11:34:01 +0100154func (p *prebuiltCommon) InstallFilename() string {
155 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
156}
157
158func (p *prebuiltCommon) Name() string {
159 return p.prebuilt.Name(p.ModuleBase.Name())
160}
161
162func (p *prebuiltCommon) Overrides() []string {
163 return p.prebuiltCommonProperties.Overrides
164}
165
166func (p *prebuiltCommon) installable() bool {
167 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
168}
169
Paul Duffinc30aea22021-06-15 19:10:11 +0100170// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
171// modules that this depends upon.
172func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
173 // Walk the dependencies of this module looking for the java modules that it exports.
174 ctx.WalkDeps(func(child, parent android.Module) bool {
175 tag := ctx.OtherModuleDependencyTag(child)
176
177 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000178 if java.IsBootclasspathFragmentContentDepTag(tag) ||
179 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100180 // If the exported java module provides a dex jar path then add it to the list of apexFiles.
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100181 path := child.(interface {
182 DexJarBuildPath() java.OptionalDexJarPath
183 }).DexJarBuildPath()
184 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000185 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100186 module: child,
187 moduleDir: ctx.OtherModuleDir(child),
188 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100189 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100190 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000191 }
192 if module, ok := child.(java.DexpreopterInterface); ok {
193 for _, install := range module.DexpreoptBuiltInstalledForApex() {
194 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
195 }
196 }
197 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100198 }
Jiakai Zhange6e90db2022-01-28 14:58:56 +0000199 } else if tag == exportedBootclasspathFragmentTag {
200 bcpfModule, ok := child.(*java.PrebuiltBootclasspathFragmentModule)
201 if !ok {
202 ctx.PropertyErrorf("exported_bootclasspath_fragments", "%q is not a prebuilt_bootclasspath_fragment module", name)
203 return false
204 }
205 for _, makeModuleName := range bcpfModule.BootImageDeviceInstallMakeModules() {
206 p.requiredModuleNames = append(p.requiredModuleNames, makeModuleName)
207 }
208 // 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())
Paul Duffinef6b6952021-06-15 11:34:01 +0100241 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
242 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000243 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100244 },
245 },
246 },
247 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100248
249 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
250 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
251 // apex specific variants of the exported java modules available for use from within make.
252 apexName := p.BaseModuleName()
253 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100254 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100255 entriesList = append(entriesList, entries)
256 }
257
258 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100259}
260
Paul Duffin9dc8c542021-06-17 13:33:09 +0100261// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
262func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
263 moduleName := fi.androidMkModuleName + "." + apexName
264 entries := android.AndroidMkEntries{
265 Class: fi.class.nameInMake(),
266 OverrideName: moduleName,
267 OutputFile: android.OptionalPathForPath(fi.builtFile),
268 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
269 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
270 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
Colin Crossc68db4b2021-11-11 18:59:15 -0800271 entries.SetString("LOCAL_MODULE_PATH", p.installDir.String())
Martin Stjernholmae44fd82021-11-23 23:17:33 +0000272 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE", filepath.Join(p.installDir.String(), fi.stem()))
273 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS",
Colin Cross6340ea52021-11-04 12:01:18 -0700274 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100275
276 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
277 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
278 // we will have foo.jar.jar
279 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100280 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
281 entries.SetString("LOCAL_DEX_PREOPT", "false")
282 },
283 },
284 ExtraFooters: []android.AndroidMkExtraFootersFunc{
285 func(w io.Writer, name, prefix, moduleDir string) {
286 // m <module_name> will build <module_name>.<apex_name> as well.
287 if fi.androidMkModuleName != moduleName {
288 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
289 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
290 }
291 },
292 },
293 }
294 return entries
295}
296
Paul Duffin5dda3e32021-05-05 14:13:27 +0100297// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
298// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
299type prebuiltApexModuleCreator interface {
300 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
301}
302
303// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
304// prebuiltApexModuleCreator's createPrebuiltApexModules method.
305//
306// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
307// will need to access dependencies added by that (exported modules) but must run before the
308// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
309// exported modules.
310func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
311 module := ctx.Module()
312 if creator, ok := module.(prebuiltApexModuleCreator); ok {
313 creator.createPrebuiltApexModules(ctx)
314 }
315}
316
Jiakai Zhang774dd302021-09-26 03:54:25 +0000317func (p *prebuiltCommon) getExportedDependencies() map[string]exportedDependencyTag {
318 dependencies := make(map[string]exportedDependencyTag)
319
320 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
321 dependencies[dep] = exportedJavaLibTag
322 }
323
324 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
325 dependencies[dep] = exportedBootclasspathFragmentTag
326 }
327
328 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
329 dependencies[dep] = exportedSystemserverclasspathFragmentTag
330 }
331
332 return dependencies
333}
334
Paul Duffin57f83592021-05-05 15:09:44 +0100335// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
336func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
337 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100338
Jiakai Zhang774dd302021-09-26 03:54:25 +0000339 for dep, tag := range p.getExportedDependencies() {
340 prebuiltDep := android.PrebuiltNameFromSource(dep)
341 ctx.AddDependency(module, tag, 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:
367// * 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.
377func (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
Jiyong Park09d77522019-11-18 11:16:27 +0900485}
486
Paul Duffin851f3992021-01-13 17:03:51 +0000487type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900488 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000489 //
490 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
491 // for android_common. That is so that it will have the same arch variant as, and so be compatible
492 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000493 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900494 Arch struct {
495 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000496 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900497 }
498 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000499 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900500 }
501 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000502 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900503 }
504 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000505 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900506 }
507 }
Paul Duffin851f3992021-01-13 17:03:51 +0000508}
509
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000510// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
511//
512// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
513// to use methods on it that are specific to the current module.
514//
515// See the ApexFileProperties.Src property.
516func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
517 multiTargets := prebuilt.MultiTargets()
518 if len(multiTargets) != 1 {
519 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
520 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000521 }
522 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000523 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000524 case android.Arm:
525 src = String(p.Arch.Arm.Src)
526 case android.Arm64:
527 src = String(p.Arch.Arm64.Src)
528 case android.X86:
529 src = String(p.Arch.X86.Src)
530 case android.X86_64:
531 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000532 }
533 if src == "" {
534 src = String(p.Src)
535 }
Paul Duffin851f3992021-01-13 17:03:51 +0000536
Paul Duffinc0609c62021-03-01 17:27:16 +0000537 if src == "" {
538 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
539 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
540 // logic from reporting a more general, less useful message.
541 }
542
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000543 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000544}
545
546type PrebuiltProperties struct {
547 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900548
Paul Duffinef6b6952021-06-15 11:34:01 +0100549 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900550}
551
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700552func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
553 return false
554}
555
Jiyong Park09d77522019-11-18 11:16:27 +0900556func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
557 switch tag {
558 case "":
559 return android.Paths{p.outputApex}, nil
560 default:
561 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
562 }
563}
564
Jiyong Park09d77522019-11-18 11:16:27 +0900565// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
566func PrebuiltFactory() android.Module {
567 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100568 module.AddProperties(&module.properties)
569 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000570
Jiyong Park09d77522019-11-18 11:16:27 +0900571 return module
572}
573
Paul Duffin5dda3e32021-05-05 14:13:27 +0100574func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000575 props := struct {
576 Name *string
577 }{
578 Name: proptools.StringPtr(name),
579 }
580
581 ctx.CreateModule(privateApexSelectorModuleFactory,
582 &props,
583 apexFileProperties,
584 )
585}
586
Paul Duffin5dda3e32021-05-05 14:13:27 +0100587// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
588//
Paul Duffin57f83592021-05-05 15:09:44 +0100589// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
590// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
591// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000592func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100593 // Only create the deapexer module if it is needed.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000594 if len(p.getExportedDependencies()) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100595 return
596 }
597
Paul Duffin57f83592021-05-05 15:09:44 +0100598 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100599 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100600 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100601 ctx.WalkDeps(func(child, parent android.Module) bool {
602 tag := ctx.OtherModuleDependencyTag(child)
603
Paul Duffin7db57e02021-06-17 14:56:05 +0100604 // If the child is not in the same apex as the parent then ignore it and all its children.
605 if !android.IsDepInSameApex(ctx, parent, child) {
606 return false
607 }
608
Paul Duffin57f83592021-05-05 15:09:44 +0100609 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100610 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100611 commonModules = append(commonModules, name)
612
613 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100614 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100615
Paul Duffin7db57e02021-06-17 14:56:05 +0100616 // Visit the dependencies of this module just in case they also require files from the
617 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100618 return true
619 }
620
621 return false
622 })
623
Paul Duffin3bae0682021-05-05 18:03:47 +0100624 // Create properties for deapexer module.
625 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100626 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100627 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100628 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100629 }
630
631 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100632 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100633
Paul Duffin11216db2021-03-01 14:14:52 +0000634 props := struct {
635 Name *string
636 Selected_apex *string
637 }{
638 Name: proptools.StringPtr(deapexerName),
639 Selected_apex: proptools.StringPtr(apexFileSource),
640 }
641 ctx.CreateModule(privateDeapexerFactory,
642 &props,
643 deapexerProperties,
644 )
645}
646
Paul Duffin11216db2021-03-01 14:14:52 +0000647func apexSelectorModuleName(baseModuleName string) string {
648 return baseModuleName + ".apex.selector"
649}
650
Paul Duffin064b70c2020-11-02 17:32:38 +0000651func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
652 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
653 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
654 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
655 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
656 // and not a renamed prebuilt module then that will be detected and reported as an error when
657 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100658 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000659 if ctx.OtherModuleExists(prebuiltName) {
660 name = prebuiltName
661 }
662 return name
663}
664
Paul Duffina7139422021-02-08 11:01:58 +0000665type exportedDependencyTag struct {
666 blueprint.BaseDependencyTag
667 name string
668}
669
670// Mark this tag so dependencies that use it are excluded from visibility enforcement.
671//
672// This does allow any prebuilt_apex to reference any module which does open up a small window for
673// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
674// avoids opening up a much bigger window by widening the visibility of modules that need files
675// provided by the prebuilt_apex to include all the possible locations they may be defined, which
676// could include everything below vendor/.
677//
678// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
679// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
680// the .apex file. It will also have to be included in the module's apex_available property too.
681// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
682// incorrectly.
683func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
684
Paul Duffin7db57e02021-06-17 14:56:05 +0100685func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
686
687var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
688
Paul Duffina7139422021-02-08 11:01:58 +0000689var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000690 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
691 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
692 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000693)
694
Paul Duffin5dda3e32021-05-05 14:13:27 +0100695var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
696
697// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
698// build.
699//
700// If this needs to make files from within a `.apex` file available for use by other Soong modules,
701// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
702// it does so as follows:
703//
704// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
705// makes them available for use by other modules, at both Soong and ninja levels.
706//
707// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
708// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
709// dexpreopt, will work the same way from source and prebuilt.
710//
711// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
712// itself so that they can retrieve the file paths to those files.
713//
714// It also creates a child module `selector` that is responsible for selecting the appropriate
715// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
716// 1. To dedup the selection logic so it only runs in one module.
717// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
718// `apex_set`.
719//
720// prebuilt_apex
721// / | \
722// / | \
723// V V V
724// selector <--- deapexer <--- exported java lib
725//
726func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
727 baseModuleName := p.BaseModuleName()
728
729 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
730 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
731
732 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000733 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100734
735 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100736 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100737}
738
Paul Duffin57f83592021-05-05 15:09:44 +0100739func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
740 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000741}
742
743var _ ApexInfoMutator = (*Prebuilt)(nil)
744
Paul Duffin064b70c2020-11-02 17:32:38 +0000745func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100746 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900747}
748
749func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900750 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100751 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900752 p.installDir = android.PathForModuleInstall(ctx, "apex")
753 p.installFilename = p.InstallFilename()
754 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
755 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
756 }
757 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
758 ctx.Build(pctx, android.BuildParams{
759 Rule: android.Cp,
760 Input: p.inputApex,
761 Output: p.outputApex,
762 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900763
764 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800765 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900766 return
767 }
768
Paul Duffinc30aea22021-06-15 19:10:11 +0100769 // Save the files that need to be made available to Make.
770 p.initApexFilesForAndroidMk(ctx)
771
Colin Crossccba23d2021-11-12 19:01:29 +0000772 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700773 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
Colin Crossccba23d2021-11-12 19:01:29 +0000774 // or that prebuilt_apex overrides other apexes (using overrides: prop)
775 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700776 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
777 }
778
779 if p.installable() {
780 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
Jooyung Han002ab682020-01-08 01:57:58 +0900781 }
Jiyong Park09d77522019-11-18 11:16:27 +0900782}
783
Paul Duffin24704672021-04-06 16:09:30 +0100784// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
785// module. It extracts the correct apex to use and makes it available for use by apex_set.
786type prebuiltApexExtractorModule struct {
787 android.ModuleBase
788
789 properties ApexExtractorProperties
790
791 extractedApex android.WritablePath
792}
793
794func privateApexExtractorModuleFactory() android.Module {
795 module := &prebuiltApexExtractorModule{}
796 module.AddProperties(
797 &module.properties,
798 )
799 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
800 return module
801}
802
803func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
804 return android.Paths{p.extractedApex}
805}
806
807func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
808 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
809 return p.properties.prebuiltSrcs(ctx)
810 }
811 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
812 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
813 ctx.Build(pctx,
814 android.BuildParams{
815 Rule: extractMatchingApex,
816 Description: "Extract an apex from an apex set",
817 Inputs: android.Paths{apexSet},
818 Output: p.extractedApex,
819 Args: map[string]string{
820 "abis": strings.Join(java.SupportedAbis(ctx), ","),
821 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
822 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
823 },
824 })
825}
826
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700827type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900828 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700829
830 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700831}
832
Paul Duffin24704672021-04-06 16:09:30 +0100833type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700834 // the .apks file path that contains prebuilt apex files to be extracted.
835 Set *string
836
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700837 Sanitized struct {
838 None struct {
839 Set *string
840 }
841 Address struct {
842 Set *string
843 }
844 Hwaddress struct {
845 Set *string
846 }
847 }
848
Paul Duffin24704672021-04-06 16:09:30 +0100849 // apexes in this set use prerelease SDK version
850 Prerelease *bool
851}
852
853func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
854 var srcs []string
855 if e.Set != nil {
856 srcs = append(srcs, *e.Set)
857 }
858
859 var sanitizers []string
860 if ctx.Host() {
861 sanitizers = ctx.Config().SanitizeHost()
862 } else {
863 sanitizers = ctx.Config().SanitizeDevice()
864 }
865
866 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
867 srcs = append(srcs, *e.Sanitized.Address.Set)
868 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
869 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
870 } else if e.Sanitized.None.Set != nil {
871 srcs = append(srcs, *e.Sanitized.None.Set)
872 }
873
874 return srcs
875}
876
877type ApexSetProperties struct {
878 ApexExtractorProperties
879
Paul Duffinef6b6952021-06-15 11:34:01 +0100880 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700881}
882
883func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
884 if sanitizer == "address" {
885 return a.properties.Sanitized.Address.Set != nil
886 }
887 if sanitizer == "hwaddress" {
888 return a.properties.Sanitized.Hwaddress.Set != nil
889 }
890
891 return false
892}
893
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700894// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
895func apexSetFactory() android.Module {
896 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100897 module.AddProperties(&module.properties)
898 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100899
Paul Duffin24704672021-04-06 16:09:30 +0100900 return module
901}
902
Paul Duffin5dda3e32021-05-05 14:13:27 +0100903func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100904 props := struct {
905 Name *string
906 }{
907 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700908 }
909
Paul Duffin24704672021-04-06 16:09:30 +0100910 ctx.CreateModule(privateApexExtractorModuleFactory,
911 &props,
912 apexExtractorProperties,
913 )
914}
915
916func apexExtractorModuleName(baseModuleName string) string {
917 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700918}
919
Paul Duffin5dda3e32021-05-05 14:13:27 +0100920var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
921
922// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
923// modules.
924//
925// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
926// prebuilt_apex except that instead of creating a selector module which selects one .apex file
927// from those provided this creates an extractor module which extracts the appropriate .apex file
928// from the zip file containing them.
929func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
930 baseModuleName := a.BaseModuleName()
931
932 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
933 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
934
935 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000936 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100937
938 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100939 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100940}
941
Paul Duffin57f83592021-05-05 15:09:44 +0100942func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
943 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100944}
945
946var _ ApexInfoMutator = (*ApexSet)(nil)
947
948func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
949 a.apexInfoMutator(mctx)
950}
951
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700952func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
953 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100954 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
955 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700956 }
957
Paul Duffinbb0dc132021-05-05 16:58:08 +0100958 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700959 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100960 ctx.Build(pctx, android.BuildParams{
961 Rule: android.Cp,
962 Input: inputApex,
963 Output: a.outputApex,
964 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900965
966 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800967 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900968 return
969 }
970
Paul Duffinc30aea22021-06-15 19:10:11 +0100971 // Save the files that need to be made available to Make.
972 a.initApexFilesForAndroidMk(ctx)
973
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700974 a.installDir = android.PathForModuleInstall(ctx, "apex")
975 if a.installable() {
Colin Cross730e3f62021-12-08 21:09:04 -0800976 a.installedFile = ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700977 }
978
979 // in case that apex_set replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700980 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700981 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100982 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700983 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700984 }
985}
986
Paul Duffinef6b6952021-06-15 11:34:01 +0100987type systemExtContext struct {
988 android.ModuleContext
989}
990
991func (*systemExtContext) SystemExtSpecific() bool {
992 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700993}