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 ( |
| 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 | |
| 43 | // Properties that are specific to `deapexer` but which need to be provided on the `prebuilt_apex` |
| 44 | // module.` |
| 45 | type DeapexerProperties struct { |
| 46 | // List of java libraries that are embedded inside this prebuilt APEX bundle and for which this |
| 47 | // APEX bundle will provide dex implementation jars for use by dexpreopt and boot jars package |
| 48 | // check. |
| 49 | Exported_java_libs []string |
| 50 | } |
| 51 | |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame^] | 52 | type SelectedApexProperties struct { |
| 53 | // The path to the apex selected for use by this module. |
| 54 | // |
| 55 | // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>" |
| 56 | // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an |
| 57 | // Android.bp file. |
| 58 | Selected_apex *string `android:"path" blueprint:"mutated"` |
| 59 | } |
| 60 | |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 61 | type Deapexer struct { |
| 62 | android.ModuleBase |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 63 | |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame^] | 64 | properties DeapexerProperties |
| 65 | selectedApexProperties SelectedApexProperties |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 66 | |
| 67 | inputApex android.Path |
| 68 | } |
| 69 | |
| 70 | func privateDeapexerFactory() android.Module { |
| 71 | module := &Deapexer{} |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame^] | 72 | module.AddProperties(&module.properties, &module.selectedApexProperties) |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 73 | android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) |
| 74 | return module |
| 75 | } |
| 76 | |
Liz Kammer | 356f7d4 | 2021-01-26 09:18:53 -0500 | [diff] [blame] | 77 | func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) { |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 78 | // Add dependencies from the java modules to which this exports files from the `.apex` file onto |
| 79 | // this module so that they can access the `DeapexerInfo` object that this provides. |
| 80 | for _, lib := range p.properties.Exported_java_libs { |
| 81 | dep := prebuiltApexExportedModuleName(ctx, lib) |
| 82 | ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame^] | 87 | p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path() |
Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 88 | |
| 89 | // Create and remember the directory into which the .apex file's contents will be unpacked. |
| 90 | deapexerOutput := android.PathForModuleOut(ctx, "deapexer") |
| 91 | |
| 92 | exports := make(map[string]android.Path) |
| 93 | |
| 94 | // Create mappings from name+tag to all the required exported paths. |
| 95 | for _, l := range p.properties.Exported_java_libs { |
| 96 | // Populate the exports that this makes available. The path here must match the path of the |
| 97 | // file in the APEX created by apexFileForJavaModule(...). |
| 98 | exports[l+"{.dexjar}"] = deapexerOutput.Join(ctx, "javalib", l+".jar") |
| 99 | } |
| 100 | |
| 101 | // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using |
| 102 | // deapexer and verifies that all the required files were created. Also, make the mapping from |
| 103 | // name+tag to path available for other modules. |
| 104 | if len(exports) > 0 { |
| 105 | // Make the information available for other modules. |
| 106 | ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports)) |
| 107 | |
| 108 | // Create a sorted list of the files that this exports. |
| 109 | exportedPaths := make(android.Paths, 0, len(exports)) |
| 110 | for _, p := range exports { |
| 111 | exportedPaths = append(exportedPaths, p) |
| 112 | } |
| 113 | exportedPaths = android.SortedUniquePaths(exportedPaths) |
| 114 | |
| 115 | // The apex needs to export some files so create a ninja rule to unpack the apex and check that |
| 116 | // the required files are present. |
| 117 | builder := android.NewRuleBuilder(pctx, ctx) |
| 118 | command := builder.Command() |
| 119 | command. |
| 120 | Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")). |
| 121 | BuiltTool("deapexer"). |
| 122 | BuiltTool("debugfs"). |
| 123 | Input(p.inputApex). |
| 124 | Text(deapexerOutput.String()) |
| 125 | for _, p := range exportedPaths { |
| 126 | command.Output(p.(android.WritablePath)) |
| 127 | } |
| 128 | builder.Build("deapexer", "deapex "+ctx.ModuleName()) |
| 129 | } |
| 130 | } |