blob: 3b8233d33ae3bab8c404c6efe14bdd112cf719f9 [file] [log] [blame]
Paul Duffin064b70c2020-11-02 17:32:38 +00001// 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 "android/soong/android"
19)
20
Paul Duffin3bae0682021-05-05 18:03:47 +010021type 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 Das2ea84dd2024-01-25 22:12:50 +000028 // 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 Duffin3bae0682021-05-05 18:03:47 +010032 // List of files exported from the .apex file by this module
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010033 //
34 // Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
35 ExportedFiles []string
Paul Duffin064b70c2020-11-02 17:32:38 +000036}
37
Paul Duffin11216db2021-03-01 14:14:52 +000038type 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 Das52c01a12024-09-20 01:09:48 +000047// deapex creates the build rules to deapex a prebuilt .apex file
48// it returns a pointer to a DeapexerInfo object
49func deapex(ctx android.ModuleContext, apexFile android.Path, deapexerProps DeapexerProperties) *android.DeapexerInfo {
Paul Duffin064b70c2020-11-02 17:32:38 +000050 // Create and remember the directory into which the .apex file's contents will be unpacked.
51 deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
52
Jiakai Zhang204356f2021-09-09 08:12:46 +000053 exports := make(map[string]android.WritablePath)
Paul Duffin064b70c2020-11-02 17:32:38 +000054
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010055 // Create mappings from apex relative path to the extracted file's path.
56 exportedPaths := make(android.Paths, 0, len(exports))
Spandan Das52c01a12024-09-20 01:09:48 +000057 for _, path := range deapexerProps.ExportedFiles {
Paul Duffin3bae0682021-05-05 18:03:47 +010058 // Populate the exports that this makes available.
Paul Duffinb4bbf2c2021-06-17 15:59:07 +010059 extractedPath := deapexerOutput.Join(ctx, path)
60 exports[path] = extractedPath
61 exportedPaths = append(exportedPaths, extractedPath)
Paul Duffin064b70c2020-11-02 17:32:38 +000062 }
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 Duffinb4bbf2c2021-06-17 15:59:07 +010066 // apex relative path to extracted file path available for other modules.
Paul Duffin064b70c2020-11-02 17:32:38 +000067 if len(exports) > 0 {
68 // Make the information available for other modules.
Spandan Das52c01a12024-09-20 01:09:48 +000069 di := android.NewDeapexerInfo(ctx.ModuleName(), exports, deapexerProps.CommonModules)
70 di.AddDexpreoptProfileGuidedExportedModuleNames(deapexerProps.DexpreoptProfileGuidedModules...)
Paul Duffin064b70c2020-11-02 17:32:38 +000071
72 // Create a sorted list of the files that this exports.
Paul Duffin064b70c2020-11-02 17:32:38 +000073 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 Duffine17c3162022-12-12 17:37:20 +000083 BuiltTool("fsck.erofs").
Spandan Das52c01a12024-09-20 01:09:48 +000084 Input(apexFile).
Paul Duffin064b70c2020-11-02 17:32:38 +000085 Text(deapexerOutput.String())
86 for _, p := range exportedPaths {
87 command.Output(p.(android.WritablePath))
88 }
Spandan Das52c01a12024-09-20 01:09:48 +000089 builder.Build("deapexer", "deapex "+ctx.ModuleName())
90 return &di
Paul Duffin064b70c2020-11-02 17:32:38 +000091 }
Spandan Das52c01a12024-09-20 01:09:48 +000092 return nil
Paul Duffin064b70c2020-11-02 17:32:38 +000093}