| 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 ( | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 18 | "fmt" | 
| Paul Duffin | 1aa5056 | 2022-06-09 17:32:21 +0000 | [diff] [blame] | 19 | "strings" | 
|  | 20 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 21 | "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 Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 26 | // | 
|  | 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 Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 61 | //     javalib/core-libart.jar -> javalib/core-libart.jar | 
| Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 62 | // it will return something like this: | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 63 | //     javalib/core-libart.jar -> out/soong/.....deapexer.../javalib/core-libart.jar | 
| Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 64 | // | 
|  | 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 Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 72 |  | 
|  | 73 | // The information exported by the `deapexer` module, access it using `DeapxerInfoProvider`. | 
|  | 74 | type DeapexerInfo struct { | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 75 | apexModuleName string | 
|  | 76 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 77 | // 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] | 78 | // 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] | 79 | // | 
|  | 80 | // See Prebuilt.ApexInfoMutator for more information. | 
| Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 81 | exports map[string]WritablePath | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 82 |  | 
|  | 83 | // name of the java libraries exported from the apex | 
|  | 84 | // e.g. core-libart | 
|  | 85 | exportedModuleNames []string | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 88 | // ApexModuleName returns the name of the APEX module that provided the info. | 
|  | 89 | func (i DeapexerInfo) ApexModuleName() string { | 
|  | 90 | return i.apexModuleName | 
|  | 91 | } | 
|  | 92 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 93 | // PrebuiltExportPath provides the path, or nil if not available, of a file exported from the | 
|  | 94 | // prebuilt_apex that created this ApexInfo. | 
|  | 95 | // | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 96 | // 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] | 97 | // | 
|  | 98 | // See apex/deapexer.go for more information. | 
| Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 99 | func (i DeapexerInfo) PrebuiltExportPath(apexRelativePath string) WritablePath { | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 100 | path := i.exports[apexRelativePath] | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 101 | return path | 
|  | 102 | } | 
|  | 103 |  | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 104 | func (i DeapexerInfo) GetExportedModuleNames() []string { | 
|  | 105 | return i.exportedModuleNames | 
|  | 106 | } | 
|  | 107 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 108 | // Provider that can be used from within the `GenerateAndroidBuildActions` of a module that depends | 
|  | 109 | // on a `deapexer` module to retrieve its `DeapexerInfo`. | 
| Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 110 | var DeapexerProvider = blueprint.NewProvider[DeapexerInfo]() | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 111 |  | 
|  | 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 Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 116 | func NewDeapexerInfo(apexModuleName string, exports map[string]WritablePath, moduleNames []string) DeapexerInfo { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 117 | return DeapexerInfo{ | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 118 | apexModuleName:      apexModuleName, | 
|  | 119 | exports:             exports, | 
|  | 120 | exportedModuleNames: moduleNames, | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 121 | } | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | type deapexerTagStruct struct { | 
|  | 125 | blueprint.BaseDependencyTag | 
|  | 126 | } | 
|  | 127 |  | 
| Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 128 | // Mark this tag so dependencies that use it are excluded from APEX contents. | 
|  | 129 | func (t deapexerTagStruct) ExcludeFromApexContents() {} | 
|  | 130 |  | 
|  | 131 | var _ ExcludeFromApexContentsTag = DeapexerTag | 
|  | 132 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 133 | // A tag that is used for dependencies on the `deapexer` module. | 
|  | 134 | var DeapexerTag = deapexerTagStruct{} | 
| Paul Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 135 |  | 
|  | 136 | // RequiredFilesFromPrebuiltApex must be implemented by modules that require files to be exported | 
|  | 137 | // from a prebuilt_apex/apex_set. | 
|  | 138 | type RequiredFilesFromPrebuiltApex interface { | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 139 | // 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 Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 141 | // | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 142 | // 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 Duffin | 5466a36 | 2021-06-07 10:25:31 +0100 | [diff] [blame] | 146 | } | 
| Paul Duffin | fef5500 | 2021-06-17 14:56:05 +0100 | [diff] [blame] | 147 |  | 
|  | 148 | // Marker interface that identifies dependencies on modules that may require files from a prebuilt | 
|  | 149 | // apex. | 
|  | 150 | type RequiresFilesFromPrebuiltApexTag interface { | 
|  | 151 | blueprint.DependencyTag | 
|  | 152 |  | 
|  | 153 | // Method that differentiates this interface from others. | 
|  | 154 | RequiresFilesFromPrebuiltApex() | 
|  | 155 | } | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 156 |  | 
|  | 157 | // FindDeapexerProviderForModule searches through the direct dependencies of the current context | 
| Martin Stjernholm | 43c44b0 | 2021-06-30 16:35:07 +0100 | [diff] [blame] | 158 | // module for a DeapexerTag dependency and returns its DeapexerInfo. If a single nonambiguous | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 159 | // 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 | 
|  | 161 | func FindDeapexerProviderForModule(ctx ModuleContext) (*DeapexerInfo, error) { | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 162 | var di *DeapexerInfo | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 163 | var err error | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 164 | ctx.VisitDirectDepsWithTag(DeapexerTag, func(m Module) { | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 165 | if err != nil { | 
|  | 166 | // An err has been found. Do not visit further. | 
|  | 167 | return | 
|  | 168 | } | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 169 | c, _ := OtherModuleProvider(ctx, m, DeapexerProvider) | 
| Paul Duffin | 1aa5056 | 2022-06-09 17:32:21 +0000 | [diff] [blame] | 170 | p := &c | 
| Martin Stjernholm | 43c44b0 | 2021-06-30 16:35:07 +0100 | [diff] [blame] | 171 | if di != nil { | 
| Paul Duffin | 1aa5056 | 2022-06-09 17:32:21 +0000 | [diff] [blame] | 172 | // 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 Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 178 | err = fmt.Errorf("Multiple installable prebuilt APEXes provide ambiguous deapexers: %s and %s", di.ApexModuleName(), p.ApexModuleName()) | 
| Martin Stjernholm | 43c44b0 | 2021-06-30 16:35:07 +0100 | [diff] [blame] | 179 | } | 
| Paul Duffin | 1aa5056 | 2022-06-09 17:32:21 +0000 | [diff] [blame] | 180 | di = p | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 181 | }) | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 182 | if err != nil { | 
|  | 183 | return nil, err | 
|  | 184 | } | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 185 | if di != nil { | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 186 | return di, nil | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 187 | } | 
| Colin Cross | ff694a8 | 2023-12-13 15:54:49 -0800 | [diff] [blame] | 188 | ai, _ := ModuleProvider(ctx, ApexInfoProvider) | 
| Spandan Das | fae468e | 2023-12-12 23:23:53 +0000 | [diff] [blame] | 189 | return nil, fmt.Errorf("No prebuilt APEX provides a deapexer module for APEX variant %s", ai.ApexVariationName) | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 190 | } | 
| Paul Duffin | 1aa5056 | 2022-06-09 17:32:21 +0000 | [diff] [blame] | 191 |  | 
|  | 192 | // removeCompressedApexSuffix removes the _compressed suffix from the name if present. | 
|  | 193 | func 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. | 
|  | 205 | func 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 | } |