Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 1 | // 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 18 | "github.com/google/blueprint" |
| 19 | ) |
| 20 | |
| 21 | // Provides support for interacting with the `deapexer` module to which a `prebuilt_apex` module |
| 22 | // will delegate the work to export files from a prebuilt '.apex` file. |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 23 | // |
| 24 | // The actual processing that is done is quite convoluted but it is all about combining information |
| 25 | // from multiple different sources in order to allow a prebuilt module to use a file extracted from |
| 26 | // an apex file. As follows: |
| 27 | // |
| 28 | // 1. A prebuilt module, e.g. prebuilt_bootclasspath_fragment or java_import needs to use a file |
| 29 | // from a prebuilt_apex/apex_set. It knows the path of the file within the apex but does not know |
| 30 | // where the apex file is or what apex to use. |
| 31 | // |
| 32 | // 2. The connection between the prebuilt module and the prebuilt_apex/apex_set is created through |
| 33 | // use of an exported_... property on the latter. That causes four things to occur: |
| 34 | // a. A `deapexer` mopdule is created by the prebuilt_apex/apex_set to extract files from the |
| 35 | // apex file. |
| 36 | // b. A dependency is added from the prebuilt_apex/apex_set modules onto the prebuilt modules |
| 37 | // listed in those properties. |
| 38 | // c. An APEX variant is created for each of those prebuilt modules. |
| 39 | // d. A dependency is added from the prebuilt modules to the `deapexer` module. |
| 40 | // |
| 41 | // 3. The prebuilt_apex/apex_set modules do not know which files are available in the apex file. |
| 42 | // That information could be specified on the prebuilt_apex/apex_set modules but without |
| 43 | // automated generation of those modules it would be expensive to maintain. So, instead they |
| 44 | // obtain that information from the prebuilt modules. They do not know what files are actually in |
| 45 | // the apex file either but they know what files they need from it. So, the |
| 46 | // prebuilt_apex/apex_set modules obtain the files that should be in the apex file from those |
| 47 | // modules and then pass those onto the `deapexer` module. |
| 48 | // |
| 49 | // 4. The `deapexer` module's ninja rule extracts all the files from the apex file into an output |
| 50 | // directory and checks that all the expected files are there. The expected files are declared as |
| 51 | // the outputs of the ninja rule so they are available to other modules. |
| 52 | // |
| 53 | // 5. The prebuilt modules then retrieve the paths to the files that they needed from the `deapexer` |
| 54 | // module. |
| 55 | // |
| 56 | // The files that are passed to `deapexer` and those that are passed back have a unique identifier |
| 57 | // that links them together. e.g. If the `deapexer` is passed something like this: |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 58 | // javalib/core-libart.jar -> javalib/core-libart.jar |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 59 | // it will return something like this: |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 60 | // javalib/core-libart.jar -> out/soong/.....deapexer.../javalib/core-libart.jar |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 61 | // |
| 62 | // The reason why the `deapexer` module is separate from the prebuilt_apex/apex_set is to avoid |
| 63 | // cycles. e.g. |
| 64 | // prebuilt_apex "com.android.art" depends upon java_import "core-libart": |
| 65 | // This is so it can create an APEX variant of the latter and obtain information about the |
| 66 | // files that it needs from the apex file. |
| 67 | // java_import "core-libart" depends upon `deapexer` module: |
| 68 | // This is so it can retrieve the paths to the files it needs. |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 69 | |
| 70 | // The information exported by the `deapexer` module, access it using `DeapxerInfoProvider`. |
| 71 | type DeapexerInfo struct { |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 72 | apexModuleName string |
| 73 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 74 | // map from the name of an exported file from a prebuilt_apex to the path to that file. The |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 75 | // exported file name is the apex relative path, e.g. javalib/core-libart.jar. |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 76 | // |
| 77 | // See Prebuilt.ApexInfoMutator for more information. |
Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 78 | exports map[string]WritablePath |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 81 | // ApexModuleName returns the name of the APEX module that provided the info. |
| 82 | func (i DeapexerInfo) ApexModuleName() string { |
| 83 | return i.apexModuleName |
| 84 | } |
| 85 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 86 | // PrebuiltExportPath provides the path, or nil if not available, of a file exported from the |
| 87 | // prebuilt_apex that created this ApexInfo. |
| 88 | // |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 89 | // The exported file is identified by the apex relative path, e.g. "javalib/core-libart.jar". |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 90 | // |
| 91 | // See apex/deapexer.go for more information. |
Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 92 | func (i DeapexerInfo) PrebuiltExportPath(apexRelativePath string) WritablePath { |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 93 | path := i.exports[apexRelativePath] |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 94 | return path |
| 95 | } |
| 96 | |
| 97 | // Provider that can be used from within the `GenerateAndroidBuildActions` of a module that depends |
| 98 | // on a `deapexer` module to retrieve its `DeapexerInfo`. |
| 99 | var DeapexerProvider = blueprint.NewProvider(DeapexerInfo{}) |
| 100 | |
| 101 | // NewDeapexerInfo creates and initializes a DeapexerInfo that is suitable |
| 102 | // for use with a prebuilt_apex module. |
| 103 | // |
| 104 | // See apex/deapexer.go for more information. |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 105 | func NewDeapexerInfo(apexModuleName string, exports map[string]WritablePath) DeapexerInfo { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 106 | return DeapexerInfo{ |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 107 | apexModuleName: apexModuleName, |
| 108 | exports: exports, |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | type deapexerTagStruct struct { |
| 113 | blueprint.BaseDependencyTag |
| 114 | } |
| 115 | |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 116 | // Mark this tag so dependencies that use it are excluded from APEX contents. |
| 117 | func (t deapexerTagStruct) ExcludeFromApexContents() {} |
| 118 | |
| 119 | var _ ExcludeFromApexContentsTag = DeapexerTag |
| 120 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 121 | // A tag that is used for dependencies on the `deapexer` module. |
| 122 | var DeapexerTag = deapexerTagStruct{} |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 123 | |
| 124 | // RequiredFilesFromPrebuiltApex must be implemented by modules that require files to be exported |
| 125 | // from a prebuilt_apex/apex_set. |
| 126 | type RequiredFilesFromPrebuiltApex interface { |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 127 | // RequiredFilesFromPrebuiltApex returns a list of the file paths (relative to the root of the |
| 128 | // APEX's contents) that the implementing module requires from within a prebuilt .apex file. |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 129 | // |
Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 130 | // For each file path this will cause the file to be extracted out of the prebuilt .apex file, and |
| 131 | // the path to the extracted file will be stored in the DeapexerInfo using the APEX relative file |
| 132 | // path as the key, The path can then be retrieved using the PrebuiltExportPath(key) method. |
| 133 | RequiredFilesFromPrebuiltApex(ctx BaseModuleContext) []string |
Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 134 | } |
Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 135 | |
| 136 | // Marker interface that identifies dependencies on modules that may require files from a prebuilt |
| 137 | // apex. |
| 138 | type RequiresFilesFromPrebuiltApexTag interface { |
| 139 | blueprint.DependencyTag |
| 140 | |
| 141 | // Method that differentiates this interface from others. |
| 142 | RequiresFilesFromPrebuiltApex() |
| 143 | } |
Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 144 | |
| 145 | // FindDeapexerProviderForModule searches through the direct dependencies of the current context |
| 146 | // module for a DeapexerTag dependency and returns its DeapexerInfo. If there is an error then it is |
| 147 | // reported with ctx.ModuleErrorf and nil is returned. |
| 148 | func FindDeapexerProviderForModule(ctx ModuleContext) *DeapexerInfo { |
| 149 | var di *DeapexerInfo |
| 150 | ctx.VisitDirectDepsWithTag(DeapexerTag, func(m Module) { |
| 151 | p := ctx.OtherModuleProvider(m, DeapexerProvider).(DeapexerInfo) |
| 152 | di = &p |
| 153 | }) |
| 154 | if di != nil { |
| 155 | return di |
| 156 | } |
| 157 | ai := ctx.Provider(ApexInfoProvider).(ApexInfo) |
| 158 | ctx.ModuleErrorf("No prebuilt APEX provides a deapexer module for APEX variant %s", ai.ApexVariationName) |
| 159 | return nil |
| 160 | } |