blob: 4e89883154af9e75f1bde3012771796efbe1a148 [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 Duffin3bae0682021-05-05 18:03:47 +010019 "path/filepath"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070020 "strconv"
Jiyong Park09d77522019-11-18 11:16:27 +090021 "strings"
22
23 "android/soong/android"
Jaewoong Jungfa00c062020-05-14 14:15:24 -070024 "android/soong/java"
Jiyong Park10e926b2020-07-16 21:38:56 +090025
Jaewoong Jungfa00c062020-05-14 14:15:24 -070026 "github.com/google/blueprint"
Jiyong Park09d77522019-11-18 11:16:27 +090027
28 "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 Duffina9c81102021-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 Duffina9c81102021-06-15 11:34:01 +010054 prebuiltCommonProperties *PrebuiltCommonProperties
55
56 installDir android.InstallPath
57 installFilename string
58 outputApex android.WritablePath
59
60 // list of commands to create symlinks for backward compatibility.
61 // these commands will be attached as LOCAL_POST_INSTALL_CMD
62 compatSymlinks []string
63
64 hostRequired []string
65 postInstallCommands []string
Jiyong Park10e926b2020-07-16 21:38:56 +090066}
67
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -070068type sanitizedPrebuilt interface {
69 hasSanitizedSource(sanitizer string) bool
70}
71
Paul Duffina9c81102021-06-15 11:34:01 +010072type PrebuiltCommonProperties struct {
Paul Duffinbb0dc132021-05-05 16:58:08 +010073 SelectedApexProperties
74
Jiyong Park10e926b2020-07-16 21:38:56 +090075 ForceDisable bool `blueprint:"mutated"`
Paul Duffin3bae0682021-05-05 18:03:47 +010076
Paul Duffina9c81102021-06-15 11:34:01 +010077 // whether the extracted apex file is installable.
78 Installable *bool
79
80 // optional name for the installed apex. If unspecified, name of the
81 // module is used as the file name
82 Filename *string
83
84 // names of modules to be overridden. Listed modules can only be other binaries
85 // (in Make or Soong).
86 // This does not completely prevent installation of the overridden binaries, but if both
87 // binaries would be installed by default (in PRODUCT_PACKAGES) the other binary will be removed
88 // from PRODUCT_PACKAGES.
89 Overrides []string
90
Paul Duffin3bae0682021-05-05 18:03:47 +010091 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
92 // APEX bundle will create an APEX variant and provide dex implementation jars for use by
93 // dexpreopt and boot jars package check.
94 Exported_java_libs []string
95
96 // List of bootclasspath fragments inside this prebuilt APEX bundle and for which this APEX
97 // bundle will create an APEX variant.
98 Exported_bootclasspath_fragments []string
Jiyong Park10e926b2020-07-16 21:38:56 +090099}
100
Paul Duffina9c81102021-06-15 11:34:01 +0100101// initPrebuiltCommon initializes the prebuiltCommon structure and performs initialization of the
102// module that is common to Prebuilt and ApexSet.
103func (p *prebuiltCommon) initPrebuiltCommon(module android.Module, properties *PrebuiltCommonProperties) {
104 p.prebuiltCommonProperties = properties
105 android.InitSingleSourcePrebuiltModule(module.(android.PrebuiltInterface), properties, "Selected_apex")
106 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
107}
108
Jiyong Park10e926b2020-07-16 21:38:56 +0900109func (p *prebuiltCommon) Prebuilt() *android.Prebuilt {
110 return &p.prebuilt
111}
112
113func (p *prebuiltCommon) isForceDisabled() bool {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100114 return p.prebuiltCommonProperties.ForceDisable
Jiyong Park10e926b2020-07-16 21:38:56 +0900115}
116
117func (p *prebuiltCommon) checkForceDisable(ctx android.ModuleContext) bool {
118 // If the device is configured to use flattened APEX, force disable the prebuilt because
119 // the prebuilt is a non-flattened one.
120 forceDisable := ctx.Config().FlattenApex()
121
122 // Force disable the prebuilts when we are doing unbundled build. We do unbundled build
123 // to build the prebuilts themselves.
124 forceDisable = forceDisable || ctx.Config().UnbundledBuild()
125
126 // Force disable the prebuilts when coverage is enabled.
127 forceDisable = forceDisable || ctx.DeviceConfig().NativeCoverageEnabled()
128 forceDisable = forceDisable || ctx.Config().IsEnvTrue("EMMA_INSTRUMENT")
129
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700130 // b/137216042 don't use prebuilts when address sanitizer is on, unless the prebuilt has a sanitized source
131 sanitized := ctx.Module().(sanitizedPrebuilt)
132 forceDisable = forceDisable || (android.InList("address", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("address"))
133 forceDisable = forceDisable || (android.InList("hwaddress", ctx.Config().SanitizeDevice()) && !sanitized.hasSanitizedSource("hwaddress"))
Jiyong Park10e926b2020-07-16 21:38:56 +0900134
135 if forceDisable && p.prebuilt.SourceExists() {
Paul Duffinbb0dc132021-05-05 16:58:08 +0100136 p.prebuiltCommonProperties.ForceDisable = true
Jiyong Park10e926b2020-07-16 21:38:56 +0900137 return true
138 }
139 return false
140}
141
Paul Duffina9c81102021-06-15 11:34:01 +0100142func (p *prebuiltCommon) InstallFilename() string {
143 return proptools.StringDefault(p.prebuiltCommonProperties.Filename, p.BaseModuleName()+imageApexSuffix)
144}
145
146func (p *prebuiltCommon) Name() string {
147 return p.prebuilt.Name(p.ModuleBase.Name())
148}
149
150func (p *prebuiltCommon) Overrides() []string {
151 return p.prebuiltCommonProperties.Overrides
152}
153
154func (p *prebuiltCommon) installable() bool {
155 return proptools.BoolDefault(p.prebuiltCommonProperties.Installable, true)
156}
157
158func (p *prebuiltCommon) AndroidMkEntries() []android.AndroidMkEntries {
159 return []android.AndroidMkEntries{
160 {
161 Class: "ETC",
162 OutputFile: android.OptionalPathForPath(p.outputApex),
163 Include: "$(BUILD_PREBUILT)",
164 Host_required: p.hostRequired,
165 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
166 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
167 entries.SetString("LOCAL_MODULE_PATH", p.installDir.ToMakePath().String())
168 entries.SetString("LOCAL_MODULE_STEM", p.installFilename)
169 entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable())
170 entries.AddStrings("LOCAL_OVERRIDES_MODULES", p.prebuiltCommonProperties.Overrides...)
171 postInstallCommands := append([]string{}, p.postInstallCommands...)
172 postInstallCommands = append(postInstallCommands, p.compatSymlinks...)
173 if len(postInstallCommands) > 0 {
174 entries.SetString("LOCAL_POST_INSTALL_CMD", strings.Join(postInstallCommands, " && "))
175 }
176 },
177 },
178 },
179 }
180}
181
Paul Duffin5dda3e32021-05-05 14:13:27 +0100182// prebuiltApexModuleCreator defines the methods that need to be implemented by prebuilt_apex and
183// apex_set in order to create the modules needed to provide access to the prebuilt .apex file.
184type prebuiltApexModuleCreator interface {
185 createPrebuiltApexModules(ctx android.TopDownMutatorContext)
186}
187
188// prebuiltApexModuleCreatorMutator is the mutator responsible for invoking the
189// prebuiltApexModuleCreator's createPrebuiltApexModules method.
190//
191// It is registered as a pre-arch mutator as it must run after the ComponentDepsMutator because it
192// will need to access dependencies added by that (exported modules) but must run before the
193// DepsMutator so that the deapexer module it creates can add dependencies onto itself from the
194// exported modules.
195func prebuiltApexModuleCreatorMutator(ctx android.TopDownMutatorContext) {
196 module := ctx.Module()
197 if creator, ok := module.(prebuiltApexModuleCreator); ok {
198 creator.createPrebuiltApexModules(ctx)
199 }
200}
201
Paul Duffin57f83592021-05-05 15:09:44 +0100202// prebuiltApexContentsDeps adds dependencies onto the prebuilt apex module's contents.
203func (p *prebuiltCommon) prebuiltApexContentsDeps(ctx android.BottomUpMutatorContext) {
204 module := ctx.Module()
Paul Duffindfd33262021-04-06 17:02:08 +0100205 // Add dependencies onto the java modules that represent the java libraries that are provided by
206 // and exported from this prebuilt apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100207 for _, exported := range p.prebuiltCommonProperties.Exported_java_libs {
Paul Duffin57f83592021-05-05 15:09:44 +0100208 dep := android.PrebuiltNameFromSource(exported)
209 ctx.AddDependency(module, exportedJavaLibTag, dep)
Paul Duffindfd33262021-04-06 17:02:08 +0100210 }
Paul Duffin023dba02021-04-22 01:45:29 +0100211
212 // Add dependencies onto the bootclasspath fragment modules that are exported from this prebuilt
213 // apex.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100214 for _, exported := range p.prebuiltCommonProperties.Exported_bootclasspath_fragments {
Paul Duffin57f83592021-05-05 15:09:44 +0100215 dep := android.PrebuiltNameFromSource(exported)
216 ctx.AddDependency(module, exportedBootclasspathFragmentTag, dep)
Paul Duffin023dba02021-04-22 01:45:29 +0100217 }
Paul Duffindfd33262021-04-06 17:02:08 +0100218}
219
Paul Duffinb17d0442021-05-05 12:07:00 +0100220// Implements android.DepInInSameApex
221func (p *prebuiltCommon) DepIsInSameApex(ctx android.BaseModuleContext, dep android.Module) bool {
222 tag := ctx.OtherModuleDependencyTag(dep)
223 _, ok := tag.(exportedDependencyTag)
224 return ok
225}
226
Paul Duffindfd33262021-04-06 17:02:08 +0100227// apexInfoMutator marks any modules for which this apex exports a file as requiring an apex
228// specific variant and checks that they are supported.
229//
230// The apexMutator will ensure that the ApexInfo objects passed to BuildForApex(ApexInfo) are
231// associated with the apex specific variant using the ApexInfoProvider for later retrieval.
232//
233// Unlike the source apex module type the prebuilt_apex module type cannot share compatible variants
234// across prebuilt_apex modules. That is because there is no way to determine whether two
235// prebuilt_apex modules that export files for the same module are compatible. e.g. they could have
236// been built from different source at different times or they could have been built with different
237// build options that affect the libraries.
238//
239// While it may be possible to provide sufficient information to determine whether two prebuilt_apex
240// modules were compatible it would be a lot of work and would not provide much benefit for a couple
241// of reasons:
242// * The number of prebuilt_apex modules that will be exporting files for the same module will be
243// low as the prebuilt_apex only exports files for the direct dependencies that require it and
244// very few modules are direct dependencies of multiple prebuilt_apex modules, e.g. there are a
245// few com.android.art* apex files that contain the same contents and could export files for the
246// same modules but only one of them needs to do so. Contrast that with source apex modules which
247// need apex specific variants for every module that contributes code to the apex, whether direct
248// or indirect.
249// * The build cost of a prebuilt_apex variant is generally low as at worst it will involve some
250// extra copying of files. Contrast that with source apex modules that has to build each variant
251// from source.
252func (p *prebuiltCommon) apexInfoMutator(mctx android.TopDownMutatorContext) {
253
254 // Collect direct dependencies into contents.
255 contents := make(map[string]android.ApexMembership)
256
257 // Collect the list of dependencies.
258 var dependencies []android.ApexModule
Paul Duffinb17d0442021-05-05 12:07:00 +0100259 mctx.WalkDeps(func(child, parent android.Module) bool {
260 // If the child is not in the same apex as the parent then exit immediately and do not visit
261 // any of the child's dependencies.
262 if !android.IsDepInSameApex(mctx, parent, child) {
263 return false
264 }
265
266 tag := mctx.OtherModuleDependencyTag(child)
267 depName := mctx.OtherModuleName(child)
Paul Duffin023dba02021-04-22 01:45:29 +0100268 if exportedTag, ok := tag.(exportedDependencyTag); ok {
269 propertyName := exportedTag.name
Paul Duffindfd33262021-04-06 17:02:08 +0100270
271 // It is an error if the other module is not a prebuilt.
Paul Duffinb17d0442021-05-05 12:07:00 +0100272 if !android.IsModulePrebuilt(child) {
Paul Duffin023dba02021-04-22 01:45:29 +0100273 mctx.PropertyErrorf(propertyName, "%q is not a prebuilt module", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100274 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100275 }
276
277 // It is an error if the other module is not an ApexModule.
Paul Duffinb17d0442021-05-05 12:07:00 +0100278 if _, ok := child.(android.ApexModule); !ok {
Paul Duffin023dba02021-04-22 01:45:29 +0100279 mctx.PropertyErrorf(propertyName, "%q is not usable within an apex", depName)
Paul Duffinb17d0442021-05-05 12:07:00 +0100280 return false
Paul Duffindfd33262021-04-06 17:02:08 +0100281 }
Paul Duffindfd33262021-04-06 17:02:08 +0100282 }
Paul Duffinb17d0442021-05-05 12:07:00 +0100283
284 // Strip off the prebuilt_ prefix if present before storing content to ensure consistent
285 // behavior whether there is a corresponding source module present or not.
286 depName = android.RemoveOptionalPrebuiltPrefix(depName)
287
288 // Remember if this module was added as a direct dependency.
289 direct := parent == mctx.Module()
290 contents[depName] = contents[depName].Add(direct)
291
292 // Add the module to the list of dependencies that need to have an APEX variant.
293 dependencies = append(dependencies, child.(android.ApexModule))
294
295 return true
Paul Duffindfd33262021-04-06 17:02:08 +0100296 })
297
298 // Create contents for the prebuilt_apex and store it away for later use.
299 apexContents := android.NewApexContents(contents)
300 mctx.SetProvider(ApexBundleInfoProvider, ApexBundleInfo{
301 Contents: apexContents,
302 })
303
304 // Create an ApexInfo for the prebuilt_apex.
Martin Stjernholmbe105032021-05-26 16:57:39 +0100305 apexVariationName := android.RemoveOptionalPrebuiltPrefix(mctx.ModuleName())
Paul Duffindfd33262021-04-06 17:02:08 +0100306 apexInfo := android.ApexInfo{
Martin Stjernholmbe105032021-05-26 16:57:39 +0100307 ApexVariationName: apexVariationName,
308 InApexVariants: []string{apexVariationName},
309 InApexModules: []string{apexVariationName},
Paul Duffindfd33262021-04-06 17:02:08 +0100310 ApexContents: []*android.ApexContents{apexContents},
311 ForPrebuiltApex: true,
312 }
313
314 // Mark the dependencies of this module as requiring a variant for this module.
315 for _, am := range dependencies {
316 am.BuildForApex(apexInfo)
317 }
318}
319
Paul Duffin11216db2021-03-01 14:14:52 +0000320// prebuiltApexSelectorModule is a private module type that is only created by the prebuilt_apex
321// module. It selects the apex to use and makes it available for use by prebuilt_apex and the
322// deapexer.
323type prebuiltApexSelectorModule struct {
324 android.ModuleBase
325
326 apexFileProperties ApexFileProperties
327
328 inputApex android.Path
329}
330
331func privateApexSelectorModuleFactory() android.Module {
332 module := &prebuiltApexSelectorModule{}
333 module.AddProperties(
334 &module.apexFileProperties,
335 )
336 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
337 return module
338}
339
340func (p *prebuiltApexSelectorModule) Srcs() android.Paths {
341 return android.Paths{p.inputApex}
342}
343
344func (p *prebuiltApexSelectorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
345 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
346}
347
Jiyong Park09d77522019-11-18 11:16:27 +0900348type Prebuilt struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900349 prebuiltCommon
Jiyong Park09d77522019-11-18 11:16:27 +0900350
Paul Duffinbb0dc132021-05-05 16:58:08 +0100351 properties PrebuiltProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900352
Paul Duffina9c81102021-06-15 11:34:01 +0100353 inputApex android.Path
Jiyong Park09d77522019-11-18 11:16:27 +0900354}
355
Paul Duffin851f3992021-01-13 17:03:51 +0000356type ApexFileProperties struct {
Jiyong Park09d77522019-11-18 11:16:27 +0900357 // the path to the prebuilt .apex file to import.
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000358 //
359 // This cannot be marked as `android:"arch_variant"` because the `prebuilt_apex` is only mutated
360 // for android_common. That is so that it will have the same arch variant as, and so be compatible
361 // with, the source `apex` module type that it replaces.
Paul Duffin11216db2021-03-01 14:14:52 +0000362 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900363 Arch struct {
364 Arm struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000365 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900366 }
367 Arm64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000368 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900369 }
370 X86 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000371 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900372 }
373 X86_64 struct {
Paul Duffin11216db2021-03-01 14:14:52 +0000374 Src *string `android:"path"`
Jiyong Park09d77522019-11-18 11:16:27 +0900375 }
376 }
Paul Duffin851f3992021-01-13 17:03:51 +0000377}
378
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000379// prebuiltApexSelector selects the correct prebuilt APEX file for the build target.
380//
381// The ctx parameter can be for any module not just the prebuilt module so care must be taken not
382// to use methods on it that are specific to the current module.
383//
384// See the ApexFileProperties.Src property.
385func (p *ApexFileProperties) prebuiltApexSelector(ctx android.BaseModuleContext, prebuilt android.Module) []string {
386 multiTargets := prebuilt.MultiTargets()
387 if len(multiTargets) != 1 {
388 ctx.OtherModuleErrorf(prebuilt, "compile_multilib shouldn't be \"both\" for prebuilt_apex")
389 return nil
Paul Duffin851f3992021-01-13 17:03:51 +0000390 }
391 var src string
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000392 switch multiTargets[0].Arch.ArchType {
Paul Duffin851f3992021-01-13 17:03:51 +0000393 case android.Arm:
394 src = String(p.Arch.Arm.Src)
395 case android.Arm64:
396 src = String(p.Arch.Arm64.Src)
397 case android.X86:
398 src = String(p.Arch.X86.Src)
399 case android.X86_64:
400 src = String(p.Arch.X86_64.Src)
Paul Duffin851f3992021-01-13 17:03:51 +0000401 }
402 if src == "" {
403 src = String(p.Src)
404 }
Paul Duffin851f3992021-01-13 17:03:51 +0000405
Paul Duffinc0609c62021-03-01 17:27:16 +0000406 if src == "" {
407 ctx.OtherModuleErrorf(prebuilt, "prebuilt_apex does not support %q", multiTargets[0].Arch.String())
408 // Drop through to return an empty string as the src (instead of nil) to avoid the prebuilt
409 // logic from reporting a more general, less useful message.
410 }
411
Paul Duffinc04fb9e2021-03-01 12:25:10 +0000412 return []string{src}
Paul Duffin851f3992021-01-13 17:03:51 +0000413}
414
415type PrebuiltProperties struct {
416 ApexFileProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900417
Paul Duffina9c81102021-06-15 11:34:01 +0100418 PrebuiltCommonProperties
Jiyong Park09d77522019-11-18 11:16:27 +0900419}
420
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700421func (a *Prebuilt) hasSanitizedSource(sanitizer string) bool {
422 return false
423}
424
Jiyong Park09d77522019-11-18 11:16:27 +0900425func (p *Prebuilt) OutputFiles(tag string) (android.Paths, error) {
426 switch tag {
427 case "":
428 return android.Paths{p.outputApex}, nil
429 default:
430 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
431 }
432}
433
Jiyong Park09d77522019-11-18 11:16:27 +0900434// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
435func PrebuiltFactory() android.Module {
436 module := &Prebuilt{}
Paul Duffina9c81102021-06-15 11:34:01 +0100437 module.AddProperties(&module.properties)
438 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +0000439
Jiyong Park09d77522019-11-18 11:16:27 +0900440 return module
441}
442
Paul Duffin5dda3e32021-05-05 14:13:27 +0100443func createApexSelectorModule(ctx android.TopDownMutatorContext, name string, apexFileProperties *ApexFileProperties) {
Paul Duffin11216db2021-03-01 14:14:52 +0000444 props := struct {
445 Name *string
446 }{
447 Name: proptools.StringPtr(name),
448 }
449
450 ctx.CreateModule(privateApexSelectorModuleFactory,
451 &props,
452 apexFileProperties,
453 )
454}
455
Paul Duffin5dda3e32021-05-05 14:13:27 +0100456// createDeapexerModuleIfNeeded will create a deapexer module if it is needed.
457//
Paul Duffin57f83592021-05-05 15:09:44 +0100458// A deapexer module is only needed when the prebuilt apex specifies one or more modules in either
459// the `exported_java_libs` or `exported_bootclasspath_fragments` properties as that indicates that
460// the listed modules need access to files from within the prebuilt .apex file.
Paul Duffina9c81102021-06-15 11:34:01 +0100461func createDeapexerModuleIfNeeded(ctx android.TopDownMutatorContext, deapexerName string, apexFileSource string, properties *PrebuiltCommonProperties) {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100462 // Only create the deapexer module if it is needed.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100463 if len(properties.Exported_java_libs)+len(properties.Exported_bootclasspath_fragments) == 0 {
Paul Duffin5dda3e32021-05-05 14:13:27 +0100464 return
465 }
466
Paul Duffin57f83592021-05-05 15:09:44 +0100467 // Compute the deapexer properties from the transitive dependencies of this module.
Paul Duffin3bae0682021-05-05 18:03:47 +0100468 javaModules := []string{}
469 exportedFiles := map[string]string{}
Paul Duffin57f83592021-05-05 15:09:44 +0100470 ctx.WalkDeps(func(child, parent android.Module) bool {
471 tag := ctx.OtherModuleDependencyTag(child)
472
473 name := android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child))
474 if java.IsBootclasspathFragmentContentDepTag(tag) || tag == exportedJavaLibTag {
Paul Duffin3bae0682021-05-05 18:03:47 +0100475 javaModules = append(javaModules, name)
476
477 // Add the dex implementation jar to the set of exported files. The path here must match the
478 // path of the file in the APEX created by apexFileForJavaModule(...).
479 exportedFiles[name+"{.dexjar}"] = filepath.Join("javalib", name+".jar")
480
Paul Duffin57f83592021-05-05 15:09:44 +0100481 } else if tag == exportedBootclasspathFragmentTag {
Paul Duffin57f83592021-05-05 15:09:44 +0100482 // Only visit the children of the bootclasspath_fragment for now.
483 return true
484 }
485
486 return false
487 })
488
Paul Duffin3bae0682021-05-05 18:03:47 +0100489 // Create properties for deapexer module.
490 deapexerProperties := &DeapexerProperties{
491 // Remove any duplicates from the java modules lists as a module may be included via a direct
492 // dependency as well as transitive ones.
493 CommonModules: android.SortedUniqueStrings(javaModules),
494 }
495
496 // Populate the exported files property in a fixed order.
497 for _, tag := range android.SortedStringKeys(exportedFiles) {
498 deapexerProperties.ExportedFiles = append(deapexerProperties.ExportedFiles, DeapexerExportedFile{
499 Tag: tag,
500 Path: exportedFiles[tag],
501 })
502 }
Paul Duffin57f83592021-05-05 15:09:44 +0100503
Paul Duffin11216db2021-03-01 14:14:52 +0000504 props := struct {
505 Name *string
506 Selected_apex *string
507 }{
508 Name: proptools.StringPtr(deapexerName),
509 Selected_apex: proptools.StringPtr(apexFileSource),
510 }
511 ctx.CreateModule(privateDeapexerFactory,
512 &props,
513 deapexerProperties,
514 )
515}
516
517func deapexerModuleName(baseModuleName string) string {
518 return baseModuleName + ".deapexer"
519}
520
521func apexSelectorModuleName(baseModuleName string) string {
522 return baseModuleName + ".apex.selector"
523}
524
Paul Duffin064b70c2020-11-02 17:32:38 +0000525func prebuiltApexExportedModuleName(ctx android.BottomUpMutatorContext, name string) string {
526 // The prebuilt_apex should be depending on prebuilt modules but as this runs after
527 // prebuilt_rename the prebuilt module may or may not be using the prebuilt_ prefixed named. So,
528 // check to see if the prefixed name is in use first, if it is then use that, otherwise assume
529 // the unprefixed name is the one to use. If the unprefixed one turns out to be a source module
530 // and not a renamed prebuilt module then that will be detected and reported as an error when
531 // processing the dependency in ApexInfoMutator().
Paul Duffin864116c2021-04-02 10:24:13 +0100532 prebuiltName := android.PrebuiltNameFromSource(name)
Paul Duffin064b70c2020-11-02 17:32:38 +0000533 if ctx.OtherModuleExists(prebuiltName) {
534 name = prebuiltName
535 }
536 return name
537}
538
Paul Duffina7139422021-02-08 11:01:58 +0000539type exportedDependencyTag struct {
540 blueprint.BaseDependencyTag
541 name string
542}
543
544// Mark this tag so dependencies that use it are excluded from visibility enforcement.
545//
546// This does allow any prebuilt_apex to reference any module which does open up a small window for
547// restricted visibility modules to be referenced from the wrong prebuilt_apex. However, doing so
548// avoids opening up a much bigger window by widening the visibility of modules that need files
549// provided by the prebuilt_apex to include all the possible locations they may be defined, which
550// could include everything below vendor/.
551//
552// A prebuilt_apex that references a module via this tag will have to contain the appropriate files
553// corresponding to that module, otherwise it will fail when attempting to retrieve the files from
554// the .apex file. It will also have to be included in the module's apex_available property too.
555// That makes it highly unlikely that a prebuilt_apex would reference a restricted module
556// incorrectly.
557func (t exportedDependencyTag) ExcludeFromVisibilityEnforcement() {}
558
559var (
Paul Duffin023dba02021-04-22 01:45:29 +0100560 exportedJavaLibTag = exportedDependencyTag{name: "exported_java_libs"}
561 exportedBootclasspathFragmentTag = exportedDependencyTag{name: "exported_bootclasspath_fragments"}
Paul Duffina7139422021-02-08 11:01:58 +0000562)
563
Paul Duffin5dda3e32021-05-05 14:13:27 +0100564var _ prebuiltApexModuleCreator = (*Prebuilt)(nil)
565
566// createPrebuiltApexModules creates modules necessary to export files from the prebuilt apex to the
567// build.
568//
569// If this needs to make files from within a `.apex` file available for use by other Soong modules,
570// e.g. make dex implementation jars available for java_import modules listed in exported_java_libs,
571// it does so as follows:
572//
573// 1. It creates a `deapexer` module that actually extracts the files from the `.apex` file and
574// makes them available for use by other modules, at both Soong and ninja levels.
575//
576// 2. It adds a dependency onto those modules and creates an apex specific variant similar to what
577// an `apex` module does. That ensures that code which looks for specific apex variant, e.g.
578// dexpreopt, will work the same way from source and prebuilt.
579//
580// 3. The `deapexer` module adds a dependency from the modules that require the exported files onto
581// itself so that they can retrieve the file paths to those files.
582//
583// It also creates a child module `selector` that is responsible for selecting the appropriate
584// input apex for both the prebuilt_apex and the deapexer. That is needed for a couple of reasons:
585// 1. To dedup the selection logic so it only runs in one module.
586// 2. To allow the deapexer to be wired up to a different source for the input apex, e.g. an
587// `apex_set`.
588//
589// prebuilt_apex
590// / | \
591// / | \
592// V V V
593// selector <--- deapexer <--- exported java lib
594//
595func (p *Prebuilt) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
596 baseModuleName := p.BaseModuleName()
597
598 apexSelectorModuleName := apexSelectorModuleName(baseModuleName)
599 createApexSelectorModule(ctx, apexSelectorModuleName, &p.properties.ApexFileProperties)
600
601 apexFileSource := ":" + apexSelectorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100602 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, p.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100603
604 // Add a source reference to retrieve the selected apex from the selector module.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100605 p.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100606}
607
Paul Duffin57f83592021-05-05 15:09:44 +0100608func (p *Prebuilt) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
609 p.prebuiltApexContentsDeps(ctx)
Paul Duffin064b70c2020-11-02 17:32:38 +0000610}
611
612var _ ApexInfoMutator = (*Prebuilt)(nil)
613
Paul Duffin064b70c2020-11-02 17:32:38 +0000614func (p *Prebuilt) ApexInfoMutator(mctx android.TopDownMutatorContext) {
Paul Duffindfd33262021-04-06 17:02:08 +0100615 p.apexInfoMutator(mctx)
Jiyong Park09d77522019-11-18 11:16:27 +0900616}
617
618func (p *Prebuilt) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Jiyong Park09d77522019-11-18 11:16:27 +0900619 // TODO(jungjw): Check the key validity.
Paul Duffinbb0dc132021-05-05 16:58:08 +0100620 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.prebuiltCommonProperties.Selected_apex).Path()
Jiyong Park09d77522019-11-18 11:16:27 +0900621 p.installDir = android.PathForModuleInstall(ctx, "apex")
622 p.installFilename = p.InstallFilename()
623 if !strings.HasSuffix(p.installFilename, imageApexSuffix) {
624 ctx.ModuleErrorf("filename should end in %s for prebuilt_apex", imageApexSuffix)
625 }
626 p.outputApex = android.PathForModuleOut(ctx, p.installFilename)
627 ctx.Build(pctx, android.BuildParams{
628 Rule: android.Cp,
629 Input: p.inputApex,
630 Output: p.outputApex,
631 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900632
633 if p.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800634 p.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900635 return
636 }
637
Jiyong Park09d77522019-11-18 11:16:27 +0900638 if p.installable() {
639 ctx.InstallFile(p.installDir, p.installFilename, p.inputApex)
640 }
641
Jooyung Han002ab682020-01-08 01:57:58 +0900642 // in case that prebuilt_apex replaces source apex (using prefer: prop)
643 p.compatSymlinks = makeCompatSymlinks(p.BaseModuleName(), ctx)
644 // or that prebuilt_apex overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100645 for _, overridden := range p.prebuiltCommonProperties.Overrides {
Jooyung Han002ab682020-01-08 01:57:58 +0900646 p.compatSymlinks = append(p.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
647 }
Jiyong Park09d77522019-11-18 11:16:27 +0900648}
649
Paul Duffin24704672021-04-06 16:09:30 +0100650// prebuiltApexExtractorModule is a private module type that is only created by the prebuilt_apex
651// module. It extracts the correct apex to use and makes it available for use by apex_set.
652type prebuiltApexExtractorModule struct {
653 android.ModuleBase
654
655 properties ApexExtractorProperties
656
657 extractedApex android.WritablePath
658}
659
660func privateApexExtractorModuleFactory() android.Module {
661 module := &prebuiltApexExtractorModule{}
662 module.AddProperties(
663 &module.properties,
664 )
665 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
666 return module
667}
668
669func (p *prebuiltApexExtractorModule) Srcs() android.Paths {
670 return android.Paths{p.extractedApex}
671}
672
673func (p *prebuiltApexExtractorModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
674 srcsSupplier := func(ctx android.BaseModuleContext, prebuilt android.Module) []string {
675 return p.properties.prebuiltSrcs(ctx)
676 }
677 apexSet := android.SingleSourcePathFromSupplier(ctx, srcsSupplier, "set")
678 p.extractedApex = android.PathForModuleOut(ctx, "extracted", apexSet.Base())
679 ctx.Build(pctx,
680 android.BuildParams{
681 Rule: extractMatchingApex,
682 Description: "Extract an apex from an apex set",
683 Inputs: android.Paths{apexSet},
684 Output: p.extractedApex,
685 Args: map[string]string{
686 "abis": strings.Join(java.SupportedAbis(ctx), ","),
687 "allow-prereleased": strconv.FormatBool(proptools.Bool(p.properties.Prerelease)),
688 "sdk-version": ctx.Config().PlatformSdkVersion().String(),
689 },
690 })
691}
692
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700693type ApexSet struct {
Jiyong Park10e926b2020-07-16 21:38:56 +0900694 prebuiltCommon
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700695
696 properties ApexSetProperties
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700697}
698
Paul Duffin24704672021-04-06 16:09:30 +0100699type ApexExtractorProperties struct {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700700 // the .apks file path that contains prebuilt apex files to be extracted.
701 Set *string
702
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700703 Sanitized struct {
704 None struct {
705 Set *string
706 }
707 Address struct {
708 Set *string
709 }
710 Hwaddress struct {
711 Set *string
712 }
713 }
714
Paul Duffin24704672021-04-06 16:09:30 +0100715 // apexes in this set use prerelease SDK version
716 Prerelease *bool
717}
718
719func (e *ApexExtractorProperties) prebuiltSrcs(ctx android.BaseModuleContext) []string {
720 var srcs []string
721 if e.Set != nil {
722 srcs = append(srcs, *e.Set)
723 }
724
725 var sanitizers []string
726 if ctx.Host() {
727 sanitizers = ctx.Config().SanitizeHost()
728 } else {
729 sanitizers = ctx.Config().SanitizeDevice()
730 }
731
732 if android.InList("address", sanitizers) && e.Sanitized.Address.Set != nil {
733 srcs = append(srcs, *e.Sanitized.Address.Set)
734 } else if android.InList("hwaddress", sanitizers) && e.Sanitized.Hwaddress.Set != nil {
735 srcs = append(srcs, *e.Sanitized.Hwaddress.Set)
736 } else if e.Sanitized.None.Set != nil {
737 srcs = append(srcs, *e.Sanitized.None.Set)
738 }
739
740 return srcs
741}
742
743type ApexSetProperties struct {
744 ApexExtractorProperties
745
Paul Duffina9c81102021-06-15 11:34:01 +0100746 PrebuiltCommonProperties
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700747}
748
749func (a *ApexSet) hasSanitizedSource(sanitizer string) bool {
750 if sanitizer == "address" {
751 return a.properties.Sanitized.Address.Set != nil
752 }
753 if sanitizer == "hwaddress" {
754 return a.properties.Sanitized.Hwaddress.Set != nil
755 }
756
757 return false
758}
759
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700760// prebuilt_apex imports an `.apex` file into the build graph as if it was built with apex.
761func apexSetFactory() android.Module {
762 module := &ApexSet{}
Paul Duffina9c81102021-06-15 11:34:01 +0100763 module.AddProperties(&module.properties)
764 module.initPrebuiltCommon(module, &module.properties.PrebuiltCommonProperties)
Paul Duffin24704672021-04-06 16:09:30 +0100765
Paul Duffin24704672021-04-06 16:09:30 +0100766 return module
767}
768
Paul Duffin5dda3e32021-05-05 14:13:27 +0100769func createApexExtractorModule(ctx android.TopDownMutatorContext, name string, apexExtractorProperties *ApexExtractorProperties) {
Paul Duffin24704672021-04-06 16:09:30 +0100770 props := struct {
771 Name *string
772 }{
773 Name: proptools.StringPtr(name),
Evgenii Stepanov2080bfe2020-07-24 15:35:40 -0700774 }
775
Paul Duffin24704672021-04-06 16:09:30 +0100776 ctx.CreateModule(privateApexExtractorModuleFactory,
777 &props,
778 apexExtractorProperties,
779 )
780}
781
782func apexExtractorModuleName(baseModuleName string) string {
783 return baseModuleName + ".apex.extractor"
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700784}
785
Paul Duffin5dda3e32021-05-05 14:13:27 +0100786var _ prebuiltApexModuleCreator = (*ApexSet)(nil)
787
788// createPrebuiltApexModules creates modules necessary to export files from the apex set to other
789// modules.
790//
791// This effectively does for apex_set what Prebuilt.createPrebuiltApexModules does for a
792// prebuilt_apex except that instead of creating a selector module which selects one .apex file
793// from those provided this creates an extractor module which extracts the appropriate .apex file
794// from the zip file containing them.
795func (a *ApexSet) createPrebuiltApexModules(ctx android.TopDownMutatorContext) {
796 baseModuleName := a.BaseModuleName()
797
798 apexExtractorModuleName := apexExtractorModuleName(baseModuleName)
799 createApexExtractorModule(ctx, apexExtractorModuleName, &a.properties.ApexExtractorProperties)
800
801 apexFileSource := ":" + apexExtractorModuleName
Paul Duffina9c81102021-06-15 11:34:01 +0100802 createDeapexerModuleIfNeeded(ctx, deapexerModuleName(baseModuleName), apexFileSource, a.prebuiltCommonProperties)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100803
804 // After passing the arch specific src properties to the creating the apex selector module
Paul Duffinbb0dc132021-05-05 16:58:08 +0100805 a.prebuiltCommonProperties.Selected_apex = proptools.StringPtr(apexFileSource)
Paul Duffin5dda3e32021-05-05 14:13:27 +0100806}
807
Paul Duffin57f83592021-05-05 15:09:44 +0100808func (a *ApexSet) ComponentDepsMutator(ctx android.BottomUpMutatorContext) {
809 a.prebuiltApexContentsDeps(ctx)
Paul Duffinf58fd9a2021-04-06 16:00:22 +0100810}
811
812var _ ApexInfoMutator = (*ApexSet)(nil)
813
814func (a *ApexSet) ApexInfoMutator(mctx android.TopDownMutatorContext) {
815 a.apexInfoMutator(mctx)
816}
817
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700818func (a *ApexSet) GenerateAndroidBuildActions(ctx android.ModuleContext) {
819 a.installFilename = a.InstallFilename()
820 if !strings.HasSuffix(a.installFilename, imageApexSuffix) {
821 ctx.ModuleErrorf("filename should end in %s for apex_set", imageApexSuffix)
822 }
823
Paul Duffinbb0dc132021-05-05 16:58:08 +0100824 inputApex := android.OptionalPathForModuleSrc(ctx, a.prebuiltCommonProperties.Selected_apex).Path()
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700825 a.outputApex = android.PathForModuleOut(ctx, a.installFilename)
Paul Duffin24704672021-04-06 16:09:30 +0100826 ctx.Build(pctx, android.BuildParams{
827 Rule: android.Cp,
828 Input: inputApex,
829 Output: a.outputApex,
830 })
Jiyong Park10e926b2020-07-16 21:38:56 +0900831
832 if a.prebuiltCommon.checkForceDisable(ctx) {
Colin Crossa9c8c9f2020-12-16 10:20:23 -0800833 a.HideFromMake()
Jiyong Park10e926b2020-07-16 21:38:56 +0900834 return
835 }
836
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700837 a.installDir = android.PathForModuleInstall(ctx, "apex")
838 if a.installable() {
839 ctx.InstallFile(a.installDir, a.installFilename, a.outputApex)
840 }
841
842 // in case that apex_set replaces source apex (using prefer: prop)
843 a.compatSymlinks = makeCompatSymlinks(a.BaseModuleName(), ctx)
844 // or that apex_set overrides other apexes (using overrides: prop)
Paul Duffina9c81102021-06-15 11:34:01 +0100845 for _, overridden := range a.prebuiltCommonProperties.Overrides {
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700846 a.compatSymlinks = append(a.compatSymlinks, makeCompatSymlinks(overridden, ctx)...)
847 }
Jooyung Han29637162020-06-30 06:34:23 +0900848
849 if ctx.Config().InstallExtraFlattenedApexes() {
850 // flattened apex should be in /system_ext/apex
851 flattenedApexDir := android.PathForModuleInstall(&systemExtContext{ctx}, "apex", a.BaseModuleName())
852 a.postInstallCommands = append(a.postInstallCommands,
853 fmt.Sprintf("$(HOST_OUT_EXECUTABLES)/deapexer --debugfs_path $(HOST_OUT_EXECUTABLES)/debugfs extract %s %s",
854 a.outputApex.String(),
855 flattenedApexDir.ToMakePath().String(),
856 ))
857 a.hostRequired = []string{"deapexer", "debugfs"}
858 }
859}
860
861type systemExtContext struct {
862 android.ModuleContext
863}
864
865func (*systemExtContext) SystemExtSpecific() bool {
866 return true
Jaewoong Jungfa00c062020-05-14 14:15:24 -0700867}