blob: 46ce41f4d2ee1d65793296e8e45b8ab41e28fa2e [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
54 prebuilt android.Prebuilt
55
56 properties DeapexerProperties
57 apexFileProperties ApexFileProperties
58
59 inputApex android.Path
60}
61
62func privateDeapexerFactory() android.Module {
63 module := &Deapexer{}
64 module.AddProperties(
65 &module.properties,
66 &module.apexFileProperties,
67 )
Paul Duffinc04fb9e2021-03-01 12:25:10 +000068 android.InitPrebuiltModuleWithSrcSupplier(module, module.apexFileProperties.prebuiltApexSelector, "src")
Paul Duffin064b70c2020-11-02 17:32:38 +000069 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
70 return module
71}
72
73func (p *Deapexer) Prebuilt() *android.Prebuilt {
74 return &p.prebuilt
75}
76
77func (p *Deapexer) Name() string {
78 return p.prebuilt.Name(p.ModuleBase.Name())
79}
80
Liz Kammer356f7d42021-01-26 09:18:53 -050081func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +000082 // Add dependencies from the java modules to which this exports files from the `.apex` file onto
83 // this module so that they can access the `DeapexerInfo` object that this provides.
84 for _, lib := range p.properties.Exported_java_libs {
85 dep := prebuiltApexExportedModuleName(ctx, lib)
86 ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
87 }
88}
89
90func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
91 p.inputApex = p.Prebuilt().SingleSourcePath(ctx)
92
93 // Create and remember the directory into which the .apex file's contents will be unpacked.
94 deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
95
96 exports := make(map[string]android.Path)
97
98 // Create mappings from name+tag to all the required exported paths.
99 for _, l := range p.properties.Exported_java_libs {
100 // Populate the exports that this makes available. The path here must match the path of the
101 // file in the APEX created by apexFileForJavaModule(...).
102 exports[l+"{.dexjar}"] = deapexerOutput.Join(ctx, "javalib", l+".jar")
103 }
104
105 // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
106 // deapexer and verifies that all the required files were created. Also, make the mapping from
107 // name+tag to path available for other modules.
108 if len(exports) > 0 {
109 // Make the information available for other modules.
110 ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports))
111
112 // Create a sorted list of the files that this exports.
113 exportedPaths := make(android.Paths, 0, len(exports))
114 for _, p := range exports {
115 exportedPaths = append(exportedPaths, p)
116 }
117 exportedPaths = android.SortedUniquePaths(exportedPaths)
118
119 // The apex needs to export some files so create a ninja rule to unpack the apex and check that
120 // the required files are present.
121 builder := android.NewRuleBuilder(pctx, ctx)
122 command := builder.Command()
123 command.
124 Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
125 BuiltTool("deapexer").
126 BuiltTool("debugfs").
127 Input(p.inputApex).
128 Text(deapexerOutput.String())
129 for _, p := range exportedPaths {
130 command.Output(p.(android.WritablePath))
131 }
132 builder.Build("deapexer", "deapex "+ctx.ModuleName())
133 }
134}