blob: 5b070c6fb509ef550897364fbcfcc7bcdd32cbbe [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
Colin Cross6340ea52021-11-04 12:01:18 -070068 hostRequired []string
Jiyong Park10e926b2020-07-16 21:38:56 +090069}
70
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070071type sanitizedPrebuilt interface {
72 hasSanitizedSource(sanitizer string) bool
73}
74
Paul Duffinef6b6952021-06-15 11:34:01 +010075type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010076 SelectedApexProperties
77
Martin Stjernholmd8da28e2021-06-24 14:37:13 +010078 // Canonical name of this APEX. Used to determine the path to the activated APEX on
79 // device (/apex/<apex_name>). If unspecified, follows the name property.
80 Apex_name *string
81
Jiyong Park10e926b2020-07-16 21:38:56 +090082 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010083
Paul Duffinef6b6952021-06-15 11:34:01 +010084 // whether the extracted apex file is installable.
85 Installable *bool
86
87 // optional name for the installed apex. If unspecified, name of the
88 // module is used as the file name
89 Filename *string
90
91 // names of modules to be overridden. Listed modules can only be other binaries
92 // (in Make or Soong).
93 // This does not completely prevent installation of the overridden binaries, but if both
94 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
95 // from PRODUCT_PACKAGES.
96 Overrides []string
97
Paul Duffin3bae0682021-05-05 18:03:47 +010098 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
99 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
100 // dexpreopt and boot jars package check.
101 Exported_java_libs []string
102
103 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
104 // bundle will create an APEX variant.
105 Exported_bootclasspath_fragments []string
Jiakai Zhang774dd302021-09-26 03:54:25 +0000106
107 // List of systemserverclasspath fragments inside this prebuilt APEX bundle and for which this
108 // APEX bundle will create an APEX variant.
109 Exported_systemserverclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +0900110}
111
Paul Duffinef6b6952021-06-15 11:34:01 +0100112// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
113// module that is common to Prebuilt and ApexSet.
114func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
115 p.prebuiltCommonProperties = properties
116 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
117 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
118}
119
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100120func (p *prebuiltCommon) ApexVariationName() string {
121 return proptools.StringDefault(p.prebuiltCommonProperties.Apex_name, p.ModuleBase.BaseModuleName())
122}
123
Jiyong Park10e926b2020-07-16 21:38:56 +0900124func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
125 return &p.prebuilt
126}
127
128func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100129 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900130}
131
132func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
133 // If the device is configured to use flattened APEX, force disable the prebuilt because
134 // the prebuilt is a non-flattened one.
135 forceDisable := ctx.Config().FlattenApex()
136
137 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
138 // to build the prebuilts themselves.
139 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
140
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700141 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
142 sanitized := ctx.Module().(sanitizedPrebuilt)
143 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
144 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900145
146 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100147 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900148 return true
149 }
150 return false
151}
152
Paul Duffinef6b6952021-06-15 11:34:01 +0100153func (p *prebuiltCommon) InstallFilename() string {
154 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
155}
156
157func (p *prebuiltCommon) Name() string {
158 return p.prebuilt.Name(p.ModuleBase.Name())
159}
160
161func (p *prebuiltCommon) Overrides() []string {
162 return p.prebuiltCommonProperties.Overrides
163}
164
165func (p *prebuiltCommon) installable() bool {
166 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
167}
168
Paul Duffinc30aea22021-06-15 19:10:11 +0100169// initApexFilesForAndroidMk initializes the prebuiltCommon.apexFilesForAndroidMk field from the
170// modules that this depends upon.
171func (p *prebuiltCommon) initApexFilesForAndroidMk(ctx android.ModuleContext) {
172 // Walk the dependencies of this module looking for the java modules that it exports.
173 ctx.WalkDeps(func(child, parent android.Module) bool {
174 tag := ctx.OtherModuleDependencyTag(child)
175
176 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Jiakai Zhang774dd302021-09-26 03:54:25 +0000177 if java.IsBootclasspathFragmentContentDepTag(tag) ||
178 java.IsSystemServerClasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffinc30aea22021-06-15 19:10:11 +0100179 // 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 +0100180 path := child.(interface {
181 DexJarBuildPath() java.OptionalDexJarPath
182 }).DexJarBuildPath()
183 if path.IsSet() {
Jiakai Zhang204356f2021-09-09 08:12:46 +0000184 af := apexFile{
Paul Duffinc30aea22021-06-15 19:10:11 +0100185 module: child,
186 moduleDir: ctx.OtherModuleDir(child),
187 androidMkModuleName: name,
Martin Stjernholm8be1e6d2021-09-15 03:34:04 +0100188 builtFile: path.Path(),
Paul Duffinc30aea22021-06-15 19:10:11 +0100189 class: javaSharedLib,
Jiakai Zhang204356f2021-09-09 08:12:46 +0000190 }
191 if module, ok := child.(java.DexpreopterInterface); ok {
192 for _, install := range module.DexpreoptBuiltInstalledForApex() {
193 af.requiredModuleNames = append(af.requiredModuleNames, install.FullModuleName())
194 }
195 }
196 p.apexFilesForAndroidMk = append(p.apexFilesForAndroidMk, af)
Paul Duffinc30aea22021-06-15 19:10:11 +0100197 }
Jiakai Zhang774dd302021-09-26 03:54:25 +0000198 } else if tag == exportedBootclasspathFragmentTag ||
199 tag == exportedSystemserverclasspathFragmentTag {
200 // Visit the children of the bootclasspath_fragment and systemserver_fragment.
Paul Duffinc30aea22021-06-15 19:10:11 +0100201 return true
202 }
203
204 return false
205 })
206}
207
Jiakai Zhang204356f2021-09-09 08:12:46 +0000208func (p *prebuiltCommon) addRequiredModules(entries *android.AndroidMkEntries) {
209 for _, fi := range p.apexFilesForAndroidMk {
210 entries.AddStrings("LOCAL_REQUIRED_MODULES", fi.requiredModuleNames...)
211 entries.AddStrings("LOCAL_TARGET_REQUIRED_MODULES", fi.targetRequiredModuleNames...)
212 entries.AddStrings("LOCAL_HOST_REQUIRED_MODULES", fi.hostRequiredModuleNames...)
213 }
214}
215
Paul Duffinef6b6952021-06-15 11:34:01 +0100216func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
Paul Duffinc30aea22021-06-15 19:10:11 +0100217 entriesList := []android.AndroidMkEntries{
Paul Duffinef6b6952021-06-15 11:34:01 +0100218 {
219 Class: "ETC",
220 OutputFile: android.OptionalPathForPath(p.outputApex),
221 Include: "$(BUILD_PREBUILT)",
222 Host_required: p.hostRequired,
223 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
224 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
225 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
226 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
Colin Cross6340ea52021-11-04 12:01:18 -0700227 entries.SetPath("LOCAL_SOONG_INSTALLED_MODULE", p.installedFile)
228 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS", p.outputApex.String()+":"+p.installedFile.String())
Paul Duffinef6b6952021-06-15 11:34:01 +0100229 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
230 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
Jiakai Zhang204356f2021-09-09 08:12:46 +0000231 p.addRequiredModules(entries)
Paul Duffinef6b6952021-06-15 11:34:01 +0100232 },
233 },
234 },
235 }
Paul Duffinc30aea22021-06-15 19:10:11 +0100236
237 // Iterate over the apexFilesForAndroidMk list and create an AndroidMkEntries struct for each
238 // file. This provides similar behavior to that provided in apexBundle.AndroidMk() as it makes the
239 // apex specific variants of the exported java modules available for use from within make.
240 apexName := p.BaseModuleName()
241 for _, fi := range p.apexFilesForAndroidMk {
Paul Duffin9dc8c542021-06-17 13:33:09 +0100242 entries := p.createEntriesForApexFile(fi, apexName)
Paul Duffinc30aea22021-06-15 19:10:11 +0100243 entriesList = append(entriesList, entries)
244 }
245
246 return entriesList
Paul Duffinef6b6952021-06-15 11:34:01 +0100247}
248
Paul Duffin9dc8c542021-06-17 13:33:09 +0100249// createEntriesForApexFile creates an AndroidMkEntries for the supplied apexFile
250func (p *prebuiltCommon) createEntriesForApexFile(fi apexFile, apexName string) android.AndroidMkEntries {
251 moduleName := fi.androidMkModuleName + "." + apexName
252 entries := android.AndroidMkEntries{
253 Class: fi.class.nameInMake(),
254 OverrideName: moduleName,
255 OutputFile: android.OptionalPathForPath(fi.builtFile),
256 Include: "$(BUILD_SYSTEM)/soong_java_prebuilt.mk",
257 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
258 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
259 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
Colin Cross6340ea52021-11-04 12:01:18 -0700260 entries.SetString("LOCAL_SOONG_INSTALLED_MODULE :=", filepath.Join(p.installDir.String(), fi.stem()))
261 entries.SetString("LOCAL_SOONG_INSTALL_PAIRS :=",
262 fi.builtFile.String()+":"+filepath.Join(p.installDir.String(), fi.stem()))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100263
264 // soong_java_prebuilt.mk sets LOCAL_MODULE_SUFFIX := .jar Therefore
265 // we need to remove the suffix from LOCAL_MODULE_STEM, otherwise
266 // we will have foo.jar.jar
267 entries.SetString("LOCAL_MODULE_STEM", strings.TrimSuffix(fi.stem(), ".jar"))
Paul Duffin9dc8c542021-06-17 13:33:09 +0100268 entries.SetString("LOCAL_SOONG_DEX_JAR", fi.builtFile.String())
269 entries.SetString("LOCAL_DEX_PREOPT", "false")
270 },
271 },
272 ExtraFooters: []android.AndroidMkExtraFootersFunc{
273 func(w io.Writer, name, prefix, moduleDir string) {
274 // m <module_name> will build <module_name>.<apex_name> as well.
275 if fi.androidMkModuleName != moduleName {
276 fmt.Fprintf(w, ".PHONY: %s\n", fi.androidMkModuleName)
277 fmt.Fprintf(w, "%s: %s\n", fi.androidMkModuleName, moduleName)
278 }
279 },
280 },
281 }
282 return entries
283}
284
Paul Duffin5dda3e32021-05-05 14:13:27 +0100285// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
286// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
287type prebuiltApexModuleCreator interface {
288 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
289}
290
291// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
292// prebuiltApexModuleCreator's createPrebuiltApexModules method.
293//
294// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
295// will need to access dependencies added by that (exported modules) but must run before the
296// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
297// exported modules.
298func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
299 module := ctx.Module()
300 if creator, ok := module.(prebuiltApexModuleCreator); ok {
301 creator.createPrebuiltApexModules(ctx)
302 }
303}
304
Jiakai Zhang774dd302021-09-26 03:54:25 +0000305func (p *prebuiltCommon) getExportedDependencies() map[string]exportedDependencyTag {
306 dependencies := make(map[string]exportedDependencyTag)
307
308 for _, dep := range p.prebuiltCommonProperties.Exported_java_libs {
309 dependencies[dep] = exportedJavaLibTag
310 }
311
312 for _, dep := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
313 dependencies[dep] = exportedBootclasspathFragmentTag
314 }
315
316 for _, dep := range p.prebuiltCommonProperties.Exported_systemserverclasspath_fragments {
317 dependencies[dep] = exportedSystemserverclasspathFragmentTag
318 }
319
320 return dependencies
321}
322
Paul Duffin57f83592021-05-05 15:09:44 +0100323// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
324func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
325 module := ctx.Module()
Paul Duffin023dba02021-04-22 01:45:29 +0100326
Jiakai Zhang774dd302021-09-26 03:54:25 +0000327 for dep, tag := range p.getExportedDependencies() {
328 prebuiltDep := android.PrebuiltNameFromSource(dep)
329 ctx.AddDependency(module, tag, prebuiltDep)
Paul Duffin023dba02021-04-22 01:45:29 +0100330 }
Paul Duffindfd33262021-04-06 17:02:08 +0100331}
332
Paul Duffinb17d0442021-05-05 12:07:00 +0100333// Implements android.DepInInSameApex
334func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
335 tag := ctx.OtherModuleDependencyTag(dep)
336 _, ok := tag.(exportedDependencyTag)
337 return ok
338}
339
Paul Duffindfd33262021-04-06 17:02:08 +0100340// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
341// specific variant and checks that they are supported.
342//
343// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
344// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
345//
346// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
347// across prebuilt_apex modules. That is because there is no way to determine whether two
348// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
349// been built from different source at different times or they could have been built with different
350// build options that affect the libraries.
351//
352// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
353// modules were compatible it would be a lot of work and would not provide much benefit for a couple
354// of reasons:
355// * The number of prebuilt_apex modules that will be exporting files for the same module will be
356// low as the prebuilt_apex only exports files for the direct dependencies that require it and
357// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
358// few com.android.art* apex files that contain the same contents and could export files for the
359// same modules but only one of them needs to do so. Contrast that with source apex modules which
360// need apex specific variants for every module that contributes code to the apex, whether direct
361// or indirect.
362// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
363// extra copying of files. Contrast that with source apex modules that has to build each variant
364// from source.
365func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
366
367 // Collect direct dependencies into contents.
368 contents := make(map[string]android.ApexMembership)
369
370 // Collect the list of dependencies.
371 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100372 mctx.WalkDeps(func(child, parent android.Module) bool {
373 // If the child is not in the same apex as the parent then exit immediately and do not visit
374 // any of the child's dependencies.
375 if !android.IsDepInSameApex(mctx, parent, child) {
376 return false
377 }
378
379 tag := mctx.OtherModuleDependencyTag(child)
380 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100381 if exportedTag, ok := tag.(exportedDependencyTag); ok {
382 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100383
384 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100385 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100386 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100387 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100388 }
389
390 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100391 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100392 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100393 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100394 }
Paul Duffindfd33262021-04-06 17:02:08 +0100395 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100396
Paul Duffinfee8cf32021-06-29 18:38:38 +0100397 // Ignore any modules that do not implement ApexModule as they cannot have an APEX specific
398 // variant.
399 if _, ok := child.(android.ApexModule); !ok {
400 return false
401 }
402
Paul Duffinb17d0442021-05-05 12:07:00 +0100403 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
404 // behavior whether there is a corresponding source module present or not.
405 depName = android.RemoveOptionalPrebuiltPrefix(depName)
406
407 // Remember if this module was added as a direct dependency.
408 direct := parent == mctx.Module()
409 contents[depName] = contents[depName].Add(direct)
410
411 // Add the module to the list of dependencies that need to have an APEX variant.
412 dependencies = append(dependencies, child.(android.ApexModule))
413
414 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100415 })
416
417 // Create contents for the prebuilt_apex and store it away for later use.
418 apexContents := android.NewApexContents(contents)
419 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
420 Contents: apexContents,
421 })
422
423 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100424 apexVariationName := p.ApexVariationName()
Paul Duffindfd33262021-04-06 17:02:08 +0100425 apexInfo := android.ApexInfo{
Martin Stjernholmc4f4ced2021-05-27 11:17:00 +0000426 ApexVariationName: apexVariationName,
427 InApexVariants: []string{apexVariationName},
Martin Stjernholmd8da28e2021-06-24 14:37:13 +0100428 InApexModules: []string{p.ModuleBase.BaseModuleName()}, // BaseModuleName() to avoid the prebuilt_ prefix.
Paul Duffindfd33262021-04-06 17:02:08 +0100429 ApexContents: []*android.ApexContents{apexContents},
430 ForPrebuiltApex: true,
431 }
432
433 // Mark the dependencies of this module as requiring a variant for this module.
434 for _, am := range dependencies {
435 am.BuildForApex(apexInfo)
436 }
437}
438
Paul Duffin11216db2021-03-01 14:14:52 +0000439// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
440// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
441// deapexer.
442type prebuiltApexSelectorModule struct {
443 android.ModuleBase
444
445 apexFileProperties ApexFileProperties
446
447 inputApex android.Path
448}
449
450func privateApexSelectorModuleFactory() android.Module {
451 module := &prebuiltApexSelectorModule{}
452 module.AddProperties(
453 &module.apexFileProperties,
454 )
455 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
456 return module
457}
458
459func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
460 return android.Paths{p.inputApex}
461}
462
463func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
464 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
465}
466
Jiyong Park09d77522019-11-18 11:16:27 +0900467type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900468 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900469
Paul Duffinbb0dc132021-05-05 16:58:08 +0100470 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900471
Paul Duffinef6b6952021-06-15 11:34:01 +0100472 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900473}
474
Colin Cross6340ea52021-11-04 12:01:18 -0700475func (p *Prebuilt) InstallBypassMake() bool {
476 return true
477}
478
Paul Duffin851f3992021-01-13 17:03:51 +0000479type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900480 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000481 //
482 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
483 // for android_common. That is so that it will have the same arch variant as, and so be compatible
484 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000485 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900486 Arch struct {
487 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000488 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900489 }
490 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000491 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900492 }
493 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000494 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900495 }
496 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000497 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900498 }
499 }
Paul Duffin851f3992021-01-13 17:03:51 +0000500}
501
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000502// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
503//
504// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
505// to use methods on it that are specific to the current module.
506//
507// See the ApexFileProperties.Src property.
508func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
509 multiTargets := prebuilt.MultiTargets()
510 if len(multiTargets) != 1 {
511 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
512 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000513 }
514 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000515 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000516 case android.Arm:
517 src = String(p.Arch.Arm.Src)
518 case android.Arm64:
519 src = String(p.Arch.Arm64.Src)
520 case android.X86:
521 src = String(p.Arch.X86.Src)
522 case android.X86_64:
523 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000524 }
525 if src == "" {
526 src = String(p.Src)
527 }
Paul Duffin851f3992021-01-13 17:03:51 +0000528
Paul Duffinc0609c62021-03-01 17:27:16 +0000529 if src == "" {
530 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
531 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
532 // logic from reporting a more general, less useful message.
533 }
534
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000535 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000536}
537
538type PrebuiltProperties struct {
539 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900540
Paul Duffinef6b6952021-06-15 11:34:01 +0100541 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900542}
543
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700544func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
545 return false
546}
547
Jiyong Park09d77522019-11-18 11:16:27 +0900548func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
549 switch tag {
550 case "":
551 return android.Paths{p.outputApex}, nil
552 default:
553 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
554 }
555}
556
Jiyong Park09d77522019-11-18 11:16:27 +0900557// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
558func PrebuiltFactory() android.Module {
559 module := &Prebuilt{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100560 module.AddProperties(&module.properties)
561 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000562
Jiyong Park09d77522019-11-18 11:16:27 +0900563 return module
564}
565
Paul Duffin5dda3e32021-05-05 14:13:27 +0100566func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000567 props := struct {
568 Name *string
569 }{
570 Name: proptools.StringPtr(name),
571 }
572
573 ctx.CreateModule(privateApexSelectorModuleFactory,
574 &props,
575 apexFileProperties,
576 )
577}
578
Paul Duffin5dda3e32021-05-05 14:13:27 +0100579// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
580//
Paul Duffin57f83592021-05-05 15:09:44 +0100581// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
582// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
583// the listed modules need access to files from within the prebuilt .apex file.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000584func (p *prebuiltCommon) createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100585 // Only create the deapexer module if it is needed.
Jiakai Zhang774dd302021-09-26 03:54:25 +0000586 if len(p.getExportedDependencies()) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100587 return
588 }
589
Paul Duffin57f83592021-05-05 15:09:44 +0100590 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffinb5084052021-06-07 10:25:31 +0100591 commonModules := []string{}
Paul Duffin034196d2021-06-17 15:59:07 +0100592 exportedFiles := []string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100593 ctx.WalkDeps(func(child, parent android.Module) bool {
594 tag := ctx.OtherModuleDependencyTag(child)
595
Paul Duffin7db57e02021-06-17 14:56:05 +0100596 // If the child is not in the same apex as the parent then ignore it and all its children.
597 if !android.IsDepInSameApex(ctx, parent, child) {
598 return false
599 }
600
Paul Duffin57f83592021-05-05 15:09:44 +0100601 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
Paul Duffin7db57e02021-06-17 14:56:05 +0100602 if _, ok := tag.(android.RequiresFilesFromPrebuiltApexTag); ok {
Paul Duffinb5084052021-06-07 10:25:31 +0100603 commonModules = append(commonModules, name)
604
605 requiredFiles := child.(android.RequiredFilesFromPrebuiltApex).RequiredFilesFromPrebuiltApex(ctx)
Paul Duffin034196d2021-06-17 15:59:07 +0100606 exportedFiles = append(exportedFiles, requiredFiles...)
Paul Duffinb5084052021-06-07 10:25:31 +0100607
Paul Duffin7db57e02021-06-17 14:56:05 +0100608 // Visit the dependencies of this module just in case they also require files from the
609 // prebuilt apex.
Paul Duffin57f83592021-05-05 15:09:44 +0100610 return true
611 }
612
613 return false
614 })
615
Paul Duffin3bae0682021-05-05 18:03:47 +0100616 // Create properties for deapexer module.
617 deapexerProperties := &DeapexerProperties{
Paul Duffinb5084052021-06-07 10:25:31 +0100618 // Remove any duplicates from the common modules lists as a module may be included via a direct
Paul Duffin3bae0682021-05-05 18:03:47 +0100619 // dependency as well as transitive ones.
Paul Duffinb5084052021-06-07 10:25:31 +0100620 CommonModules: android.SortedUniqueStrings(commonModules),
Paul Duffin3bae0682021-05-05 18:03:47 +0100621 }
622
623 // Populate the exported files property in a fixed order.
Paul Duffin034196d2021-06-17 15:59:07 +0100624 deapexerProperties.ExportedFiles = android.SortedUniqueStrings(exportedFiles)
Paul Duffin57f83592021-05-05 15:09:44 +0100625
Paul Duffin11216db2021-03-01 14:14:52 +0000626 props := struct {
627 Name *string
628 Selected_apex *string
629 }{
630 Name: proptools.StringPtr(deapexerName),
631 Selected_apex: proptools.StringPtr(apexFileSource),
632 }
633 ctx.CreateModule(privateDeapexerFactory,
634 &props,
635 deapexerProperties,
636 )
637}
638
Paul Duffin11216db2021-03-01 14:14:52 +0000639func apexSelectorModuleName(baseModuleName string) string {
640 return baseModuleName + ".apex.selector"
641}
642
Paul Duffin064b70c2020-11-02 17:32:38 +0000643func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
644 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
645 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
646 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
647 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
648 // and not a renamed prebuilt module then that will be detected and reported as an error when
649 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100650 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000651 if ctx.OtherModuleExists(prebuiltName) {
652 name = prebuiltName
653 }
654 return name
655}
656
Paul Duffina7139422021-02-08 11:01:58 +0000657type exportedDependencyTag struct {
658 blueprint.BaseDependencyTag
659 name string
660}
661
662// Mark this tag so dependencies that use it are excluded from visibility enforcement.
663//
664// This does allow any prebuilt_apex to reference any module which does open up a small window for
665// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
666// avoids opening up a much bigger window by widening the visibility of modules that need files
667// provided by the prebuilt_apex to include all the possible locations they may be defined, which
668// could include everything below vendor/.
669//
670// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
671// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
672// the .apex file. It will also have to be included in the module's apex_available property too.
673// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
674// incorrectly.
675func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
676
Paul Duffin7db57e02021-06-17 14:56:05 +0100677func (t exportedDependencyTag) RequiresFilesFromPrebuiltApex() {}
678
679var _ android.RequiresFilesFromPrebuiltApexTag = exportedDependencyTag{}
680
Paul Duffina7139422021-02-08 11:01:58 +0000681var (
Jiakai Zhang774dd302021-09-26 03:54:25 +0000682 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
683 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
684 exportedSystemserverclasspathFragmentTag = exportedDependencyTag{name: "exported_systemserverclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000685)
686
Paul Duffin5dda3e32021-05-05 14:13:27 +0100687var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
688
689// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
690// build.
691//
692// If this needs to make files from within a `.apex` file available for use by other Soong modules,
693// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
694// it does so as follows:
695//
696// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
697// makes them available for use by other modules, at both Soong and ninja levels.
698//
699// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
700// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
701// dexpreopt, will work the same way from source and prebuilt.
702//
703// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
704// itself so that they can retrieve the file paths to those files.
705//
706// It also creates a child module `selector` that is responsible for selecting the appropriate
707// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
708// 1. To dedup the selection logic so it only runs in one module.
709// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
710// `apex_set`.
711//
712// prebuilt_apex
713// / | \
714// / | \
715// V V V
716// selector <--- deapexer <--- exported java lib
717//
718func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
719 baseModuleName := p.BaseModuleName()
720
721 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
722 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
723
724 apexFileSource := ":" + apexSelectorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000725 p.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100726
727 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100728 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100729}
730
Paul Duffin57f83592021-05-05 15:09:44 +0100731func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
732 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000733}
734
735var _ ApexInfoMutator = (*Prebuilt)(nil)
736
Paul Duffin064b70c2020-11-02 17:32:38 +0000737func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100738 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900739}
740
741func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900742 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100743 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900744 p.installDir = android.PathForModuleInstall(ctx, "apex")
745 p.installFilename = p.InstallFilename()
746 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
747 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
748 }
749 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
750 ctx.Build(pctx, android.BuildParams{
751 Rule: android.Cp,
752 Input: p.inputApex,
753 Output: p.outputApex,
754 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900755
756 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800757 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900758 return
759 }
760
Paul Duffinc30aea22021-06-15 19:10:11 +0100761 // Save the files that need to be made available to Make.
762 p.initApexFilesForAndroidMk(ctx)
763
Colin Crossccba23d2021-11-12 19:01:29 +0000764 // in case that prebuilt_apex replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700765 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx, true)
Colin Crossccba23d2021-11-12 19:01:29 +0000766 // or that prebuilt_apex overrides other apexes (using overrides: prop)
767 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700768 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
769 }
770
771 if p.installable() {
772 p.installedFile = ctx.InstallFile(p.installDir, p.installFilename, p.inputApex, p.compatSymlinks.Paths()...)
Jooyung Han002ab682020-01-08 01:57:58 +0900773 }
Jiyong Park09d77522019-11-18 11:16:27 +0900774}
775
Paul Duffin24704672021-04-06 16:09:30 +0100776// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
777// module. It extracts the correct apex to use and makes it available for use by apex_set.
778type prebuiltApexExtractorModule struct {
779 android.ModuleBase
780
781 properties ApexExtractorProperties
782
783 extractedApex android.WritablePath
784}
785
786func privateApexExtractorModuleFactory() android.Module {
787 module := &prebuiltApexExtractorModule{}
788 module.AddProperties(
789 &module.properties,
790 )
791 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
792 return module
793}
794
795func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
796 return android.Paths{p.extractedApex}
797}
798
799func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
800 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
801 return p.properties.prebuiltSrcs(ctx)
802 }
803 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
804 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
805 ctx.Build(pctx,
806 android.BuildParams{
807 Rule: extractMatchingApex,
808 Description: "Extract an apex from an apex set",
809 Inputs: android.Paths{apexSet},
810 Output: p.extractedApex,
811 Args: map[string]string{
812 "abis": strings.Join(java.SupportedAbis(ctx), ","),
813 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
814 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
815 },
816 })
817}
818
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700819type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900820 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700821
822 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700823}
824
Paul Duffin24704672021-04-06 16:09:30 +0100825type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700826 // the .apks file path that contains prebuilt apex files to be extracted.
827 Set *string
828
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700829 Sanitized struct {
830 None struct {
831 Set *string
832 }
833 Address struct {
834 Set *string
835 }
836 Hwaddress struct {
837 Set *string
838 }
839 }
840
Paul Duffin24704672021-04-06 16:09:30 +0100841 // apexes in this set use prerelease SDK version
842 Prerelease *bool
843}
844
845func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
846 var srcs []string
847 if e.Set != nil {
848 srcs = append(srcs, *e.Set)
849 }
850
851 var sanitizers []string
852 if ctx.Host() {
853 sanitizers = ctx.Config().SanitizeHost()
854 } else {
855 sanitizers = ctx.Config().SanitizeDevice()
856 }
857
858 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
859 srcs = append(srcs, *e.Sanitized.Address.Set)
860 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
861 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
862 } else if e.Sanitized.None.Set != nil {
863 srcs = append(srcs, *e.Sanitized.None.Set)
864 }
865
866 return srcs
867}
868
869type ApexSetProperties struct {
870 ApexExtractorProperties
871
Paul Duffinef6b6952021-06-15 11:34:01 +0100872 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700873}
874
875func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
876 if sanitizer == "address" {
877 return a.properties.Sanitized.Address.Set != nil
878 }
879 if sanitizer == "hwaddress" {
880 return a.properties.Sanitized.Hwaddress.Set != nil
881 }
882
883 return false
884}
885
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700886// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
887func apexSetFactory() android.Module {
888 module := &ApexSet{}
Paul Duffinef6b6952021-06-15 11:34:01 +0100889 module.AddProperties(&module.properties)
890 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100891
Paul Duffin24704672021-04-06 16:09:30 +0100892 return module
893}
894
Paul Duffin5dda3e32021-05-05 14:13:27 +0100895func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100896 props := struct {
897 Name *string
898 }{
899 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700900 }
901
Paul Duffin24704672021-04-06 16:09:30 +0100902 ctx.CreateModule(privateApexExtractorModuleFactory,
903 &props,
904 apexExtractorProperties,
905 )
906}
907
908func apexExtractorModuleName(baseModuleName string) string {
909 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700910}
911
Paul Duffin5dda3e32021-05-05 14:13:27 +0100912var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
913
914// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
915// modules.
916//
917// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
918// prebuilt_apex except that instead of creating a selector module which selects one .apex file
919// from those provided this creates an extractor module which extracts the appropriate .apex file
920// from the zip file containing them.
921func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
922 baseModuleName := a.BaseModuleName()
923
924 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
925 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
926
927 apexFileSource := ":" + apexExtractorModuleName
Jiakai Zhang774dd302021-09-26 03:54:25 +0000928 a.createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100929
930 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100931 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100932}
933
Paul Duffin57f83592021-05-05 15:09:44 +0100934func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
935 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100936}
937
938var _ ApexInfoMutator = (*ApexSet)(nil)
939
940func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
941 a.apexInfoMutator(mctx)
942}
943
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700944func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
945 a.installFilename = a.InstallFilename()
Samiul Islam7c02e262021-09-08 17:48:28 +0100946 if !strings.HasSuffix(a.installFilename, imageApexSuffix) && !strings.HasSuffix(a.installFilename, imageCapexSuffix) {
947 ctx.ModuleErrorf("filename should end in %s or %s for apex_set", imageApexSuffix, imageCapexSuffix)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700948 }
949
Paul Duffinbb0dc132021-05-05 16:58:08 +0100950 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700951 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100952 ctx.Build(pctx, android.BuildParams{
953 Rule: android.Cp,
954 Input: inputApex,
955 Output: a.outputApex,
956 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900957
958 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800959 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900960 return
961 }
962
Paul Duffinc30aea22021-06-15 19:10:11 +0100963 // Save the files that need to be made available to Make.
964 a.initApexFilesForAndroidMk(ctx)
965
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700966 a.installDir = android.PathForModuleInstall(ctx, "apex")
967 if a.installable() {
968 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
969 }
970
971 // in case that apex_set replaces source apex (using prefer: prop)
Colin Cross6340ea52021-11-04 12:01:18 -0700972 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx, true)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700973 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffinef6b6952021-06-15 11:34:01 +0100974 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Colin Cross6340ea52021-11-04 12:01:18 -0700975 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx, true)...)
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700976 }
977}
978
Paul Duffinef6b6952021-06-15 11:34:01 +0100979type systemExtContext struct {
980 android.ModuleContext
981}
982
983func (*systemExtContext) SystemExtSpecific() bool {
984 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700985}