blob: e7aa5514a3a9e859c036c4c667e86472be11d6aa [file] [log] [blame]
Paul Duffin064b70c2020-11-02 17:32:38 +00001// Copyright (C) 2021 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 "android/soong/android"
19)
20
21// Contains 'deapexer' a private module type used by 'prebuilt_apex' to make dex files contained
22// within a .apex file referenced by `prebuilt_apex` available for use by their associated
23// `java_import` modules.
24//
25// An 'apex' module references `java_library` modules from which .dex files are obtained that are
26// stored in the resulting `.apex` file. The resulting `.apex` file is then made available as a
27// prebuilt by referencing it from a `prebuilt_apex`. For each such `java_library` that is used by
28// modules outside the `.apex` file a `java_import` prebuilt is made available referencing a jar
29// that contains the Java classes.
30//
31// When building a Java module type, e.g. `java_module` or `android_app` against such prebuilts the
32// `java_import` provides the classes jar (jar containing `.class` files) against which the
33// module's `.java` files are compiled. That classes jar usually contains only stub classes. The
34// resulting classes jar is converted into a dex jar (jar containing `.dex` files). Then if
35// necessary the dex jar is further processed by `dexpreopt` to produce an optimized form of the
36// library specific to the current Android version. This process requires access to implementation
37// dex jars for each `java_import`. The `java_import` will obtain the implementation dex jar from
38// the `.apex` file in the associated `prebuilt_apex`.
39//
40// This is intentionally not registered by name as it is not intended to be used from within an
41// `Android.bp` file.
42
43// Properties that are specific to `deapexer` but which need to be provided on the `prebuilt_apex`
44// module.`
45type DeapexerProperties struct {
46 // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this
47 // APEX bundle will provide dex implementation jars for use by dexpreopt and boot jars package
48 // check.
49 Exported_java_libs []string
50}
51
52type Deapexer struct {
53 android.ModuleBase
Paul Duffin064b70c2020-11-02 17:32:38 +000054
55 properties DeapexerProperties
56 apexFileProperties ApexFileProperties
57
58 inputApex android.Path
59}
60
61func privateDeapexerFactory() android.Module {
62 module := &Deapexer{}
63 module.AddProperties(
64 &module.properties,
65 &module.apexFileProperties,
66 )
Paul Duffin064b70c2020-11-02 17:32:38 +000067 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
68 return module
69}
70
Liz Kammer356f7d42021-01-26 09:18:53 -050071func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +000072 // Add dependencies from the java modules to which this exports files from the `.apex` file onto
73 // this module so that they can access the `DeapexerInfo` object that this provides.
74 for _, lib := range p.properties.Exported_java_libs {
75 dep := prebuiltApexExportedModuleName(ctx, lib)
76 ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
77 }
78}
79
80func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin56dc66e2021-03-01 13:18:44 +000081 p.inputApex = android.SingleSourcePathFromSupplier(ctx, p.apexFileProperties.prebuiltApexSelector, "src")
Paul Duffin064b70c2020-11-02 17:32:38 +000082
83 // Create and remember the directory into which the .apex file's contents will be unpacked.
84 deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
85
86 exports := make(map[string]android.Path)
87
88 // Create mappings from name+tag to all the required exported paths.
89 for _, l := range p.properties.Exported_java_libs {
90 // Populate the exports that this makes available. The path here must match the path of the
91 // file in the APEX created by apexFileForJavaModule(...).
92 exports[l+"{.dexjar}"] = deapexerOutput.Join(ctx, "javalib", l+".jar")
93 }
94
95 // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
96 // deapexer and verifies that all the required files were created. Also, make the mapping from
97 // name+tag to path available for other modules.
98 if len(exports) > 0 {
99 // Make the information available for other modules.
100 ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports))
101
102 // Create a sorted list of the files that this exports.
103 exportedPaths := make(android.Paths, 0, len(exports))
104 for _, p := range exports {
105 exportedPaths = append(exportedPaths, p)
106 }
107 exportedPaths = android.SortedUniquePaths(exportedPaths)
108
109 // The apex needs to export some files so create a ninja rule to unpack the apex and check that
110 // the required files are present.
111 builder := android.NewRuleBuilder(pctx, ctx)
112 command := builder.Command()
113 command.
114 Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
115 BuiltTool("deapexer").
116 BuiltTool("debugfs").
117 Input(p.inputApex).
118 Text(deapexerOutput.String())
119 for _, p := range exportedPaths {
120 command.Output(p.(android.WritablePath))
121 }
122 builder.Build("deapexer", "deapex "+ctx.ModuleName())
123 }
124}