blob: 2704b3ef71a9e84bdd493765388d3da9bd8aaba5 [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 android
16
17import (
Spandan Dasfae468e2023-12-12 23:23:53 +000018 "fmt"
Paul Duffin1aa50562022-06-09 17:32:21 +000019 "strings"
20
Paul Duffin064b70c2020-11-02 17:32:38 +000021 "github.com/google/blueprint"
22)
23
24// Provides support for interacting with the `deapexer` module to which a `prebuilt_apex` module
25// will delegate the work to export files from a prebuilt '.apex` file.
Paul Duffin5466a362021-06-07 10:25:31 +010026//
27// The actual processing that is done is quite convoluted but it is all about combining information
28// from multiple different sources in order to allow a prebuilt module to use a file extracted from
29// an apex file. As follows:
30//
31// 1. A prebuilt module, e.g. prebuilt_bootclasspath_fragment or java_import needs to use a file
32// from a prebuilt_apex/apex_set. It knows the path of the file within the apex but does not know
33// where the apex file is or what apex to use.
34//
35// 2. The connection between the prebuilt module and the prebuilt_apex/apex_set is created through
36// use of an exported_... property on the latter. That causes four things to occur:
37// a. A `deapexer` mopdule is created by the prebuilt_apex/apex_set to extract files from the
38// apex file.
39// b. A dependency is added from the prebuilt_apex/apex_set modules onto the prebuilt modules
40// listed in those properties.
41// c. An APEX variant is created for each of those prebuilt modules.
42// d. A dependency is added from the prebuilt modules to the `deapexer` module.
43//
44// 3. The prebuilt_apex/apex_set modules do not know which files are available in the apex file.
45// That information could be specified on the prebuilt_apex/apex_set modules but without
46// automated generation of those modules it would be expensive to maintain. So, instead they
47// obtain that information from the prebuilt modules. They do not know what files are actually in
48// the apex file either but they know what files they need from it. So, the
49// prebuilt_apex/apex_set modules obtain the files that should be in the apex file from those
50// modules and then pass those onto the `deapexer` module.
51//
52// 4. The `deapexer` module's ninja rule extracts all the files from the apex file into an output
53// directory and checks that all the expected files are there. The expected files are declared as
54// the outputs of the ninja rule so they are available to other modules.
55//
56// 5. The prebuilt modules then retrieve the paths to the files that they needed from the `deapexer`
57// module.
58//
59// The files that are passed to `deapexer` and those that are passed back have a unique identifier
60// that links them together. e.g. If the `deapexer` is passed something like this:
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010061// javalib/core-libart.jar -> javalib/core-libart.jar
Paul Duffin5466a362021-06-07 10:25:31 +010062// it will return something like this:
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010063// javalib/core-libart.jar -> out/soong/.....deapexer.../javalib/core-libart.jar
Paul Duffin5466a362021-06-07 10:25:31 +010064//
65// The reason why the `deapexer` module is separate from the prebuilt_apex/apex_set is to avoid
66// cycles. e.g.
67// prebuilt_apex "com.android.art" depends upon java_import "core-libart":
68// This is so it can create an APEX variant of the latter and obtain information about the
69// files that it needs from the apex file.
70// java_import "core-libart" depends upon `deapexer` module:
71// This is so it can retrieve the paths to the files it needs.
Paul Duffin064b70c2020-11-02 17:32:38 +000072
73// The information exported by the `deapexer` module, access it using `DeapxerInfoProvider`.
74type DeapexerInfo struct {
Martin Stjernholm44825602021-09-17 01:44:12 +010075 apexModuleName string
76
Paul Duffin064b70c2020-11-02 17:32:38 +000077 // map from the name of an exported file from a prebuilt_apex to the path to that file. The
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010078 // exported file name is the apex relative path, e.g. javalib/core-libart.jar.
Paul Duffin064b70c2020-11-02 17:32:38 +000079 //
80 // See Prebuilt.ApexInfoMutator for more information.
Jiakai Zhang204356f2021-09-09 08:12:46 +000081 exports map[string]WritablePath
Spandan Das5be63332023-12-13 00:06:32 +000082
83 // name of the java libraries exported from the apex
84 // e.g. core-libart
85 exportedModuleNames []string
Paul Duffin064b70c2020-11-02 17:32:38 +000086}
87
Martin Stjernholm44825602021-09-17 01:44:12 +010088// ApexModuleName returns the name of the APEX module that provided the info.
89func (i DeapexerInfo) ApexModuleName() string {
90 return i.apexModuleName
91}
92
Paul Duffin064b70c2020-11-02 17:32:38 +000093// PrebuiltExportPath provides the path, or nil if not available, of a file exported from the
94// prebuilt_apex that created this ApexInfo.
95//
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010096// The exported file is identified by the apex relative path, e.g. "javalib/core-libart.jar".
Paul Duffin064b70c2020-11-02 17:32:38 +000097//
98// See apex/deapexer.go for more information.
Jiakai Zhang204356f2021-09-09 08:12:46 +000099func (i DeapexerInfo) PrebuiltExportPath(apexRelativePath string) WritablePath {
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100100 path := i.exports[apexRelativePath]
Paul Duffin064b70c2020-11-02 17:32:38 +0000101 return path
102}
103
Spandan Das5be63332023-12-13 00:06:32 +0000104func (i DeapexerInfo) GetExportedModuleNames() []string {
105 return i.exportedModuleNames
106}
107
Paul Duffin064b70c2020-11-02 17:32:38 +0000108// Provider that can be used from within the `GenerateAndroidBuildActions` of a module that depends
109// on a `deapexer` module to retrieve its `DeapexerInfo`.
Colin Crossbc7d76c2023-12-12 16:39:03 -0800110var DeapexerProvider = blueprint.NewProvider[DeapexerInfo]()
Paul Duffin064b70c2020-11-02 17:32:38 +0000111
112// NewDeapexerInfo creates and initializes a DeapexerInfo that is suitable
113// for use with a prebuilt_apex module.
114//
115// See apex/deapexer.go for more information.
Spandan Das5be63332023-12-13 00:06:32 +0000116func NewDeapexerInfo(apexModuleName string, exports map[string]WritablePath, moduleNames []string) DeapexerInfo {
Paul Duffin064b70c2020-11-02 17:32:38 +0000117 return DeapexerInfo{
Spandan Das5be63332023-12-13 00:06:32 +0000118 apexModuleName: apexModuleName,
119 exports: exports,
120 exportedModuleNames: moduleNames,
Paul Duffin064b70c2020-11-02 17:32:38 +0000121 }
122}
123
124type deapexerTagStruct struct {
125 blueprint.BaseDependencyTag
126}
127
Paul Duffin5466a362021-06-07 10:25:31 +0100128// Mark this tag so dependencies that use it are excluded from APEX contents.
129func (t deapexerTagStruct) ExcludeFromApexContents() {}
130
131var _ ExcludeFromApexContentsTag = DeapexerTag
132
Paul Duffin064b70c2020-11-02 17:32:38 +0000133// A tag that is used for dependencies on the `deapexer` module.
134var DeapexerTag = deapexerTagStruct{}
Paul Duffin5466a362021-06-07 10:25:31 +0100135
136// RequiredFilesFromPrebuiltApex must be implemented by modules that require files to be exported
137// from a prebuilt_apex/apex_set.
138type RequiredFilesFromPrebuiltApex interface {
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100139 // RequiredFilesFromPrebuiltApex returns a list of the file paths (relative to the root of the
140 // APEX's contents) that the implementing module requires from within a prebuilt .apex file.
Paul Duffin5466a362021-06-07 10:25:31 +0100141 //
Paul Duffinb4bbf2c2021-06-17 15:59:07 +0100142 // For each file path this will cause the file to be extracted out of the prebuilt .apex file, and
143 // the path to the extracted file will be stored in the DeapexerInfo using the APEX relative file
144 // path as the key, The path can then be retrieved using the PrebuiltExportPath(key) method.
145 RequiredFilesFromPrebuiltApex(ctx BaseModuleContext) []string
Paul Duffin5466a362021-06-07 10:25:31 +0100146}
Paul Duffinfef55002021-06-17 14:56:05 +0100147
148// Marker interface that identifies dependencies on modules that may require files from a prebuilt
149// apex.
150type RequiresFilesFromPrebuiltApexTag interface {
151 blueprint.DependencyTag
152
153 // Method that differentiates this interface from others.
154 RequiresFilesFromPrebuiltApex()
155}
Martin Stjernholm44825602021-09-17 01:44:12 +0100156
157// FindDeapexerProviderForModule searches through the direct dependencies of the current context
Martin Stjernholm43c44b02021-06-30 16:35:07 +0100158// module for a DeapexerTag dependency and returns its DeapexerInfo. If a single nonambiguous
Spandan Dasfae468e2023-12-12 23:23:53 +0000159// deapexer module isn't found then it returns it an error
160// clients should check the value of error and call ctx.ModuleErrof if a non nil error is received
161func FindDeapexerProviderForModule(ctx ModuleContext) (*DeapexerInfo, error) {
Martin Stjernholm44825602021-09-17 01:44:12 +0100162 var di *DeapexerInfo
Spandan Dasfae468e2023-12-12 23:23:53 +0000163 var err error
Martin Stjernholm44825602021-09-17 01:44:12 +0100164 ctx.VisitDirectDepsWithTag(DeapexerTag, func(m Module) {
Spandan Dasfae468e2023-12-12 23:23:53 +0000165 if err != nil {
166 // An err has been found. Do not visit further.
167 return
168 }
Colin Cross313aa542023-12-13 13:47:44 -0800169 c, _ := OtherModuleProvider(ctx, m, DeapexerProvider)
Paul Duffin1aa50562022-06-09 17:32:21 +0000170 p := &c
Martin Stjernholm43c44b02021-06-30 16:35:07 +0100171 if di != nil {
Paul Duffin1aa50562022-06-09 17:32:21 +0000172 // If two DeapexerInfo providers have been found then check if they are
173 // equivalent. If they are then use the selected one, otherwise fail.
174 if selected := equivalentDeapexerInfoProviders(di, p); selected != nil {
175 di = selected
176 return
177 }
Spandan Dasfae468e2023-12-12 23:23:53 +0000178 err = fmt.Errorf("Multiple installable prebuilt APEXes provide ambiguous deapexers: %s and %s", di.ApexModuleName(), p.ApexModuleName())
Martin Stjernholm43c44b02021-06-30 16:35:07 +0100179 }
Paul Duffin1aa50562022-06-09 17:32:21 +0000180 di = p
Martin Stjernholm44825602021-09-17 01:44:12 +0100181 })
Spandan Dasfae468e2023-12-12 23:23:53 +0000182 if err != nil {
183 return nil, err
184 }
Martin Stjernholm44825602021-09-17 01:44:12 +0100185 if di != nil {
Spandan Dasfae468e2023-12-12 23:23:53 +0000186 return di, nil
Martin Stjernholm44825602021-09-17 01:44:12 +0100187 }
Colin Crossff694a82023-12-13 15:54:49 -0800188 ai, _ := ModuleProvider(ctx, ApexInfoProvider)
Spandan Dasfae468e2023-12-12 23:23:53 +0000189 return nil, fmt.Errorf("No prebuilt APEX provides a deapexer module for APEX variant %s", ai.ApexVariationName)
Martin Stjernholm44825602021-09-17 01:44:12 +0100190}
Paul Duffin1aa50562022-06-09 17:32:21 +0000191
192// removeCompressedApexSuffix removes the _compressed suffix from the name if present.
193func removeCompressedApexSuffix(name string) string {
194 return strings.TrimSuffix(name, "_compressed")
195}
196
197// equivalentDeapexerInfoProviders checks to make sure that the two DeapexerInfo structures are
198// equivalent.
199//
200// At the moment <x> and <x>_compressed APEXes are treated as being equivalent.
201//
202// If they are not equivalent then this returns nil, otherwise, this returns the DeapexerInfo that
203// should be used by the build, which is always the uncompressed one. That ensures that the behavior
204// of the build is not dependent on which prebuilt APEX is visited first.
205func equivalentDeapexerInfoProviders(p1 *DeapexerInfo, p2 *DeapexerInfo) *DeapexerInfo {
206 n1 := removeCompressedApexSuffix(p1.ApexModuleName())
207 n2 := removeCompressedApexSuffix(p2.ApexModuleName())
208
209 // If the names don't match then they are not equivalent.
210 if n1 != n2 {
211 return nil
212 }
213
214 // Select the uncompressed APEX.
215 if n1 == removeCompressedApexSuffix(n1) {
216 return p1
217 } else {
218 return p2
219 }
220}