| 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 |  | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 21 | type DeapexerProperties struct { | 
|  | 22 | // List of common modules that may need access to files exported by this module. | 
|  | 23 | // | 
|  | 24 | // A common module in this sense is one that is not arch specific but uses a common variant for | 
|  | 25 | // all architectures, e.g. java. | 
|  | 26 | CommonModules []string | 
|  | 27 |  | 
| Spandan Das | 2ea84dd | 2024-01-25 22:12:50 +0000 | [diff] [blame] | 28 | // List of modules that use an embedded .prof to guide optimization of the equivalent dexpreopt artifact | 
|  | 29 | // This is a subset of CommonModules | 
|  | 30 | DexpreoptProfileGuidedModules []string | 
|  | 31 |  | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 32 | // List of files exported from the .apex file by this module | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 33 | // | 
|  | 34 | // Each entry is a path from the apex root, e.g. javalib/core-libart.jar. | 
|  | 35 | ExportedFiles []string | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 36 | } | 
|  | 37 |  | 
| Paul Duffin | 11216db | 2021-03-01 14:14:52 +0000 | [diff] [blame] | 38 | type SelectedApexProperties struct { | 
|  | 39 | // The path to the apex selected for use by this module. | 
|  | 40 | // | 
|  | 41 | // Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>" | 
|  | 42 | // and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an | 
|  | 43 | // Android.bp file. | 
|  | 44 | Selected_apex *string `android:"path" blueprint:"mutated"` | 
|  | 45 | } | 
|  | 46 |  | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 47 | // deapex creates the build rules to deapex a prebuilt .apex file | 
|  | 48 | // it returns a pointer to a DeapexerInfo object | 
|  | 49 | func deapex(ctx android.ModuleContext, apexFile android.Path, deapexerProps DeapexerProperties) *android.DeapexerInfo { | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 50 | // Create and remember the directory into which the .apex file's contents will be unpacked. | 
|  | 51 | deapexerOutput := android.PathForModuleOut(ctx, "deapexer") | 
|  | 52 |  | 
| Jiakai Zhang | 204356f | 2021-09-09 08:12:46 +0000 | [diff] [blame] | 53 | exports := make(map[string]android.WritablePath) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 54 |  | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 55 | // Create mappings from apex relative path to the extracted file's path. | 
|  | 56 | exportedPaths := make(android.Paths, 0, len(exports)) | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 57 | for _, path := range deapexerProps.ExportedFiles { | 
| Paul Duffin | 3bae068 | 2021-05-05 18:03:47 +0100 | [diff] [blame] | 58 | // Populate the exports that this makes available. | 
| Paul Duffin | b4bbf2c | 2021-06-17 15:59:07 +0100 | [diff] [blame] | 59 | extractedPath := deapexerOutput.Join(ctx, path) | 
|  | 60 | exports[path] = extractedPath | 
|  | 61 | exportedPaths = append(exportedPaths, extractedPath) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
|  | 64 | // If the prebuilt_apex exports any files then create a build rule that unpacks the apex using | 
|  | 65 | // 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] | 66 | // apex relative path to extracted file path available for other modules. | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 67 | if len(exports) > 0 { | 
|  | 68 | // Make the information available for other modules. | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 69 | di := android.NewDeapexerInfo(ctx.ModuleName(), exports, deapexerProps.CommonModules) | 
|  | 70 | di.AddDexpreoptProfileGuidedExportedModuleNames(deapexerProps.DexpreoptProfileGuidedModules...) | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 71 |  | 
|  | 72 | // Create a sorted list of the files that this exports. | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 73 | exportedPaths = android.SortedUniquePaths(exportedPaths) | 
|  | 74 |  | 
|  | 75 | // The apex needs to export some files so create a ninja rule to unpack the apex and check that | 
|  | 76 | // the required files are present. | 
|  | 77 | builder := android.NewRuleBuilder(pctx, ctx) | 
|  | 78 | command := builder.Command() | 
|  | 79 | command. | 
|  | 80 | Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")). | 
|  | 81 | BuiltTool("deapexer"). | 
|  | 82 | BuiltTool("debugfs"). | 
| Paul Duffin | e17c316 | 2022-12-12 17:37:20 +0000 | [diff] [blame] | 83 | BuiltTool("fsck.erofs"). | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 84 | Input(apexFile). | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 85 | Text(deapexerOutput.String()) | 
|  | 86 | for _, p := range exportedPaths { | 
|  | 87 | command.Output(p.(android.WritablePath)) | 
|  | 88 | } | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 89 | builder.Build("deapexer", "deapex "+ctx.ModuleName()) | 
|  | 90 | return &di | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 91 | } | 
| Spandan Das | 52c01a1 | 2024-09-20 01:09:48 +0000 | [diff] [blame] | 92 | return nil | 
| Paul Duffin | 064b70c | 2020-11-02 17:32:38 +0000 | [diff] [blame] | 93 | } |