blob: c7cdbfabbbc86b09e0a25d56f3e615d619da156e [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// DeapexerExportedFile defines the properties needed to expose a file from the deapexer module.
44type DeapexerExportedFile struct {
45 // The tag parameter which must be passed to android.OutputFileProducer OutputFiles(tag) method
46 // to retrieve the path to the unpacked file.
47 Tag string
Paul Duffin023dba02021-04-22 01:45:29 +010048
Paul Duffin3bae0682021-05-05 18:03:47 +010049 // The path within the APEX that needs to be exported.
50 Path string `android:"path"`
51}
52
53// DeapexerProperties specifies the properties supported by the deapexer module.
54//
55// As these are never intended to be supplied in a .bp file they use a different naming convention
56// to make it clear that they are different.
57type DeapexerProperties struct {
58 // List of common modules that may need access to files exported by this module.
59 //
60 // A common module in this sense is one that is not arch specific but uses a common variant for
61 // all architectures, e.g. java.
62 CommonModules []string
63
64 // List of files exported from the .apex file by this module
65 ExportedFiles []DeapexerExportedFile
Paul Duffin064b70c2020-11-02 17:32:38 +000066}
67
Paul Duffin11216db2021-03-01 14:14:52 +000068type SelectedApexProperties struct {
69 // The path to the apex selected for use by this module.
70 //
71 // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
72 // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
73 // Android.bp file.
74 Selected_apex *string `android:"path" blueprint:"mutated"`
75}
76
Paul Duffin064b70c2020-11-02 17:32:38 +000077type Deapexer struct {
78 android.ModuleBase
Paul Duffin064b70c2020-11-02 17:32:38 +000079
Paul Duffin11216db2021-03-01 14:14:52 +000080 properties DeapexerProperties
81 selectedApexProperties SelectedApexProperties
Paul Duffin064b70c2020-11-02 17:32:38 +000082
83 inputApex android.Path
84}
85
86func privateDeapexerFactory() android.Module {
87 module := &Deapexer{}
Paul Duffin11216db2021-03-01 14:14:52 +000088 module.AddProperties(&module.properties, &module.selectedApexProperties)
Paul Duffin064b70c2020-11-02 17:32:38 +000089 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
90 return module
91}
92
Liz Kammer356f7d42021-01-26 09:18:53 -050093func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
Paul Duffin064b70c2020-11-02 17:32:38 +000094 // Add dependencies from the java modules to which this exports files from the `.apex` file onto
95 // this module so that they can access the `DeapexerInfo` object that this provides.
Paul Duffin3bae0682021-05-05 18:03:47 +010096 for _, lib := range p.properties.CommonModules {
Paul Duffin064b70c2020-11-02 17:32:38 +000097 dep := prebuiltApexExportedModuleName(ctx, lib)
98 ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
99 }
100}
101
102func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Paul Duffin11216db2021-03-01 14:14:52 +0000103 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
Paul Duffin064b70c2020-11-02 17:32:38 +0000104
105 // Create and remember the directory into which the .apex file's contents will be unpacked.
106 deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
107
108 exports := make(map[string]android.Path)
109
110 // Create mappings from name+tag to all the required exported paths.
Paul Duffin3bae0682021-05-05 18:03:47 +0100111 for _, e := range p.properties.ExportedFiles {
112 tag := e.Tag
113 path := e.Path
114
115 // Populate the exports that this makes available.
116 exports[tag] = deapexerOutput.Join(ctx, path)
Paul Duffin064b70c2020-11-02 17:32:38 +0000117 }
118
119 // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
120 // deapexer and verifies that all the required files were created. Also, make the mapping from
121 // name+tag to path available for other modules.
122 if len(exports) > 0 {
123 // Make the information available for other modules.
124 ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports))
125
126 // Create a sorted list of the files that this exports.
127 exportedPaths := make(android.Paths, 0, len(exports))
128 for _, p := range exports {
129 exportedPaths = append(exportedPaths, p)
130 }
131 exportedPaths = android.SortedUniquePaths(exportedPaths)
132
133 // The apex needs to export some files so create a ninja rule to unpack the apex and check that
134 // the required files are present.
135 builder := android.NewRuleBuilder(pctx, ctx)
136 command := builder.Command()
137 command.
138 Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
139 BuiltTool("deapexer").
140 BuiltTool("debugfs").
141 Input(p.inputApex).
142 Text(deapexerOutput.String())
143 for _, p := range exportedPaths {
144 command.Output(p.(android.WritablePath))
145 }
146 builder.Build("deapexer", "deapex "+ctx.ModuleName())
147 }
148}