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 15package apex 16 17import ( 18 "strings" 19 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 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. 49type 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 56 // List of files exported from the .apex file by this module 57 // 58 // Each entry is a path from the apex root, e.g. javalib/core-libart.jar. 59 ExportedFiles []string 60} 61 62type SelectedApexProperties struct { 63 // The path to the apex selected for use by this module. 64 // 65 // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>" 66 // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an 67 // Android.bp file. 68 Selected_apex *string `android:"path" blueprint:"mutated"` 69} 70 71type Deapexer struct { 72 android.ModuleBase 73 74 properties DeapexerProperties 75 selectedApexProperties SelectedApexProperties 76 77 inputApex android.Path 78} 79 80// Returns the name of the deapexer module corresponding to an APEX module with the given name. 81func deapexerModuleName(apexModuleName string) string { 82 return apexModuleName + ".deapexer" 83} 84 85// Returns the name of the APEX module corresponding to an deapexer module with 86// the given name. This reverses deapexerModuleName. 87func apexModuleName(deapexerModuleName string) string { 88 return strings.TrimSuffix(deapexerModuleName, ".deapexer") 89} 90 91func privateDeapexerFactory() android.Module { 92 module := &Deapexer{} 93 module.AddProperties(&module.properties, &module.selectedApexProperties) 94 android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon) 95 return module 96} 97 98func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) { 99 // Add dependencies from the java modules to which this exports files from the `.apex` file onto 100 // this module so that they can access the `DeapexerInfo` object that this provides. 101 for _, lib := range p.properties.CommonModules { 102 dep := prebuiltApexExportedModuleName(ctx, lib) 103 ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep) 104 } 105} 106 107func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) { 108 p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path() 109 110 // Create and remember the directory into which the .apex file's contents will be unpacked. 111 deapexerOutput := android.PathForModuleOut(ctx, "deapexer") 112 113 exports := make(map[string]android.WritablePath) 114 115 // Create mappings from apex relative path to the extracted file's path. 116 exportedPaths := make(android.Paths, 0, len(exports)) 117 for _, path := range p.properties.ExportedFiles { 118 // Populate the exports that this makes available. 119 extractedPath := deapexerOutput.Join(ctx, path) 120 exports[path] = extractedPath 121 exportedPaths = append(exportedPaths, extractedPath) 122 } 123 124 // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using 125 // deapexer and verifies that all the required files were created. Also, make the mapping from 126 // apex relative path to extracted file path available for other modules. 127 if len(exports) > 0 { 128 // Make the information available for other modules. 129 di := android.NewDeapexerInfo(apexModuleName(ctx.ModuleName()), exports) 130 ctx.SetProvider(android.DeapexerProvider, di) 131 132 // Create a sorted list of the files that this exports. 133 exportedPaths = android.SortedUniquePaths(exportedPaths) 134 135 // The apex needs to export some files so create a ninja rule to unpack the apex and check that 136 // the required files are present. 137 builder := android.NewRuleBuilder(pctx, ctx) 138 command := builder.Command() 139 command. 140 Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")). 141 BuiltTool("deapexer"). 142 BuiltTool("debugfs"). 143 Input(p.inputApex). 144 Text(deapexerOutput.String()) 145 for _, p := range exportedPaths { 146 command.Output(p.(android.WritablePath)) 147 } 148 builder.Build("deapexer", "deapex "+apexModuleName(ctx.ModuleName())) 149 } 150} 151