xref: /aosp_15_r20/build/soong/apex/deapexer.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
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	"android/soong/android"
19)
20
21type 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
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
32	// List of files exported from the .apex file by this module
33	//
34	// Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
35	ExportedFiles []string
36}
37
38type 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
47// 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 {
50	// Create and remember the directory into which the .apex file's contents will be unpacked.
51	deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
52
53	exports := make(map[string]android.WritablePath)
54
55	// Create mappings from apex relative path to the extracted file's path.
56	exportedPaths := make(android.Paths, 0, len(exports))
57	for _, path := range deapexerProps.ExportedFiles {
58		// Populate the exports that this makes available.
59		extractedPath := deapexerOutput.Join(ctx, path)
60		exports[path] = extractedPath
61		exportedPaths = append(exportedPaths, extractedPath)
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
66	// apex relative path to extracted file path available for other modules.
67	if len(exports) > 0 {
68		// Make the information available for other modules.
69		di := android.NewDeapexerInfo(ctx.ModuleName(), exports, deapexerProps.CommonModules)
70		di.AddDexpreoptProfileGuidedExportedModuleNames(deapexerProps.DexpreoptProfileGuidedModules...)
71
72		// Create a sorted list of the files that this exports.
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").
83			BuiltTool("fsck.erofs").
84			Input(apexFile).
85			Text(deapexerOutput.String())
86		for _, p := range exportedPaths {
87			command.Output(p.(android.WritablePath))
88		}
89		builder.Build("deapexer", "deapex "+ctx.ModuleName())
90		return &di
91	}
92	return nil
93}
94