| 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 apex | 
|  | 16 |  | 
|  | 17 | import ( | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 18 | "strings" | 
|  | 19 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 20 | "android/soong/android" | 
|  | 21 | ) | 
|  | 22 |  | 
|  | 23 | // Contains 'deapexer' a private module type used by 'prebuilt_apex' to make dex files contained | 
|  | 24 | // within a .apex file referenced by `prebuilt_apex` available for use by their associated | 
|  | 25 | // `java_import` modules. | 
|  | 26 | // | 
|  | 27 | // An 'apex' module references `java_library` modules from which .dex files are obtained that are | 
|  | 28 | // stored in the resulting `.apex` file. The resulting `.apex` file is then made available as a | 
|  | 29 | // prebuilt by referencing it from a `prebuilt_apex`. For each such `java_library` that is used by | 
|  | 30 | // modules outside the `.apex` file a `java_import` prebuilt is made available referencing a jar | 
|  | 31 | // that contains the Java classes. | 
|  | 32 | // | 
|  | 33 | // When building a Java module type, e.g. `java_module` or `android_app` against such prebuilts the | 
|  | 34 | // `java_import` provides the classes jar  (jar containing `.class` files) against which the | 
|  | 35 | // module's `.java` files are compiled. That classes jar usually contains only stub classes. The | 
|  | 36 | // resulting classes jar is converted into a dex jar (jar containing `.dex` files). Then if | 
|  | 37 | // necessary the dex jar is further processed by `dexpreopt` to produce an optimized form of the | 
|  | 38 | // library specific to the current Android version. This process requires access to implementation | 
|  | 39 | // dex jars for each `java_import`. The `java_import` will obtain the implementation dex jar from | 
|  | 40 | // the `.apex` file in the associated `prebuilt_apex`. | 
|  | 41 | // | 
|  | 42 | // This is intentionally not registered by name as it is not intended to be used from within an | 
|  | 43 | // `Android.bp` file. | 
|  | 44 |  | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 45 | // DeapexerProperties specifies the properties supported by the deapexer module. | 
|  | 46 | // | 
|  | 47 | // As these are never intended to be supplied in a .bp file they use a different naming convention | 
|  | 48 | // to make it clear that they are different. | 
|  | 49 | type DeapexerProperties struct { | 
|  | 50 | // List of common modules that may need access to files exported by this module. | 
|  | 51 | // | 
|  | 52 | // A common module in this sense is one that is not arch specific but uses a common variant for | 
|  | 53 | // all architectures, e.g. java. | 
|  | 54 | CommonModules []string | 
|  | 55 |  | 
| Spandan Das | 2ea84dd | 2024-01-25 22:12:50 +0000 | [diff] [blame] | 56 | // List of modules that use an embedded .prof to guide optimization of the equivalent dexpreopt artifact | 
|  | 57 | // This is a subset of CommonModules | 
|  | 58 | DexpreoptProfileGuidedModules []string | 
|  | 59 |  | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 60 | // List of files exported from the .apex file by this module | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 61 | // | 
|  | 62 | // Each entry is a path from the apex root, e.g. javalib/core-libart.jar. | 
|  | 63 | ExportedFiles []string | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 66 | type SelectedApexProperties struct { | 
|  | 67 | // The path to the apex selected for use by this module. | 
|  | 68 | // | 
|  | 69 | // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>" | 
|  | 70 | // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an | 
|  | 71 | // Android.bp file. | 
|  | 72 | Selected_apex *string `android:"path" blueprint:"mutated"` | 
|  | 73 | } | 
|  | 74 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 75 | type Deapexer struct { | 
|  | 76 | android.ModuleBase | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 77 |  | 
| Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 78 | properties             DeapexerProperties | 
|  | 79 | selectedApexProperties SelectedApexProperties | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 80 |  | 
|  | 81 | inputApex android.Path | 
|  | 82 | } | 
|  | 83 |  | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 84 | // Returns the name of the deapexer module corresponding to an APEX module with the given name. | 
|  | 85 | func deapexerModuleName(apexModuleName string) string { | 
|  | 86 | return apexModuleName + ".deapexer" | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | // Returns the name of the APEX module corresponding to an deapexer module with | 
|  | 90 | // the given name. This reverses deapexerModuleName. | 
|  | 91 | func apexModuleName(deapexerModuleName string) string { | 
|  | 92 | return strings.TrimSuffix(deapexerModuleName, ".deapexer") | 
|  | 93 | } | 
|  | 94 |  | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 95 | func privateDeapexerFactory() android.Module { | 
|  | 96 | module := &Deapexer{} | 
| Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 97 | module.AddProperties(&module.properties, &module.selectedApexProperties) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 98 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) | 
|  | 99 | return module | 
|  | 100 | } | 
|  | 101 |  | 
| Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 102 | func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 103 | // Add dependencies from the java modules to which this exports files from the `.apex` file onto | 
|  | 104 | // this module so that they can access the `DeapexerInfo` object that this provides. | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 105 | // TODO: b/308174306 - Once all the mainline modules have been flagged, drop this dependency edge | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 106 | for _, lib := range p.properties.CommonModules { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 107 | dep := prebuiltApexExportedModuleName(ctx, lib) | 
|  | 108 | ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep) | 
|  | 109 | } | 
|  | 110 | } | 
|  | 111 |  | 
|  | 112 | func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 113 | p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path() | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 114 |  | 
|  | 115 | // Create and remember the directory into which the .apex file's contents will be unpacked. | 
|  | 116 | deapexerOutput := android.PathForModuleOut(ctx, "deapexer") | 
|  | 117 |  | 
| Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 118 | exports := make(map[string]android.WritablePath) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 119 |  | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 120 | // Create mappings from apex relative path to the extracted file's path. | 
|  | 121 | exportedPaths := make(android.Paths, 0, len(exports)) | 
|  | 122 | for _, path := range p.properties.ExportedFiles { | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 123 | // Populate the exports that this makes available. | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 124 | extractedPath := deapexerOutput.Join(ctx, path) | 
|  | 125 | exports[path] = extractedPath | 
|  | 126 | exportedPaths = append(exportedPaths, extractedPath) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 127 | } | 
|  | 128 |  | 
|  | 129 | // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using | 
|  | 130 | // deapexer and verifies that all the required files were created. Also, make the mapping from | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 131 | // apex relative path to extracted file path available for other modules. | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 132 | if len(exports) > 0 { | 
|  | 133 | // Make the information available for other modules. | 
| Spandan Das | 5be6333 | 2023-12-13 00:06:32 +0000 | [diff] [blame] | 134 | di := android.NewDeapexerInfo(apexModuleName(ctx.ModuleName()), exports, p.properties.CommonModules) | 
| Spandan Das | 2ea84dd | 2024-01-25 22:12:50 +0000 | [diff] [blame] | 135 | di.AddDexpreoptProfileGuidedExportedModuleNames(p.properties.DexpreoptProfileGuidedModules...) | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 136 | android.SetProvider(ctx, android.DeapexerProvider, di) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 137 |  | 
|  | 138 | // Create a sorted list of the files that this exports. | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 139 | exportedPaths = android.SortedUniquePaths(exportedPaths) | 
|  | 140 |  | 
|  | 141 | // The apex needs to export some files so create a ninja rule to unpack the apex and check that | 
|  | 142 | // the required files are present. | 
|  | 143 | builder := android.NewRuleBuilder(pctx, ctx) | 
|  | 144 | command := builder.Command() | 
|  | 145 | command. | 
|  | 146 | Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")). | 
|  | 147 | BuiltTool("deapexer"). | 
|  | 148 | BuiltTool("debugfs"). | 
| Paul Duffin | e17c316 | 2022-12-12 17:37:20 +0000 | [diff] [blame] | 149 | BuiltTool("fsck.erofs"). | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 150 | Input(p.inputApex). | 
|  | 151 | Text(deapexerOutput.String()) | 
|  | 152 | for _, p := range exportedPaths { | 
|  | 153 | command.Output(p.(android.WritablePath)) | 
|  | 154 | } | 
| Martin Stjernholm | 4482560 | 2021-09-17 01:44:12 +0100 | [diff] [blame] | 155 | builder.Build("deapexer", "deapex "+apexModuleName(ctx.ModuleName())) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 156 | } | 
|  | 157 | } |