blob: c70da1542620ec8c2d69a1f5f3128173c0bb491b [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
Paul Duffin3bae0682021-05-05 18:03:47 +010043// DeapexerProperties specifies the properties supported by the deapexer module.
44//
45// As these are never intended to be supplied in a .bp file they use a different naming convention
46// to make it clear that they are different.
47type DeapexerProperties struct {
48 // List of common modules that may need access to files exported by this module.
49 //
50 // A common module in this sense is one that is not arch specific but uses a common variant for
51 // all architectures, e.g. java.
52 CommonModules []string
53
54 // List of files exported from the .apex file by this module
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010055 //
56 // Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
57 ExportedFiles []string
Paul Duffin064b70c2020-11-02 17:32:38 +000058}
59
Paul Duffin11216db2021-03-01 14:14:52 +000060type SelectedApexProperties struct {
61 // The path to the apex selected for use by this module.
62 //
63 // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
64 // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
65 // Android.bp file.
66 Selected_apex *string `android:"path" blueprint:"mutated"`
67}
68
Paul Duffin064b70c2020-11-02 17:32:38 +000069type Deapexer struct {
70 android.ModuleBase
Paul Duffin064b70c2020-11-02 17:32:38 +000071
Paul Duffin11216db2021-03-01 14:14:52 +000072 properties DeapexerProperties
73 selectedApexProperties SelectedApexProperties
Paul Duffin064b70c2020-11-02 17:32:38 +000074
75 inputApex android.Path
76}
77
78func privateDeapexerFactory() android.Module {
79 module := &Deapexer{}
Paul Duffin11216db2021-03-01 14:14:52 +000080 module.AddProperties(&module.properties, &module.selectedApexProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +000081 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
82 return module
83}
84
Liz Kammer356f7d42021-01-26 09:18:53 -050085func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +000086 // Add dependencies from the java modules to which this exports files from the `.apex` file onto
87 // this module so that they can access the `DeapexerInfo` object that this provides.
Paul Duffin3bae0682021-05-05 18:03:47 +010088 for _, lib := range p.properties.CommonModules {
Paul Duffin064b70c2020-11-02 17:32:38 +000089 dep := prebuiltApexExportedModuleName(ctx, lib)
90 ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
91 }
92}
93
94func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin11216db2021-03-01 14:14:52 +000095 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Paul Duffin064b70c2020-11-02 17:32:38 +000096
97 // Create and remember the directory into which the .apex file's contents will be unpacked.
98 deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
99
100 exports := make(map[string]android.Path)
101
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100102 // Create mappings from apex relative path to the extracted file's path.
103 exportedPaths := make(android.Paths, 0, len(exports))
104 for _, path := range p.properties.ExportedFiles {
Paul Duffin3bae0682021-05-05 18:03:47 +0100105 // Populate the exports that this makes available.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100106 extractedPath := deapexerOutput.Join(ctx, path)
107 exports[path] = extractedPath
108 exportedPaths = append(exportedPaths, extractedPath)
Paul Duffin064b70c2020-11-02 17:32:38 +0000109 }
110
111 // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
112 // deapexer and verifies that all the required files were created. Also, make the mapping from
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100113 // apex relative path to extracted file path available for other modules.
Paul Duffin064b70c2020-11-02 17:32:38 +0000114 if len(exports) > 0 {
115 // Make the information available for other modules.
116 ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports))
117
118 // Create a sorted list of the files that this exports.
Paul Duffin064b70c2020-11-02 17:32:38 +0000119 exportedPaths = android.SortedUniquePaths(exportedPaths)
120
121 // The apex needs to export some files so create a ninja rule to unpack the apex and check that
122 // the required files are present.
123 builder := android.NewRuleBuilder(pctx, ctx)
124 command := builder.Command()
125 command.
126 Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
127 BuiltTool("deapexer").
128 BuiltTool("debugfs").
129 Input(p.inputApex).
130 Text(deapexerOutput.String())
131 for _, p := range exportedPaths {
132 command.Output(p.(android.WritablePath))
133 }
134 builder.Build("deapexer", "deapex "+ctx.ModuleName())
135 }
136}