xref: /aosp_15_r20/build/soong/apex/deapexer.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright (C) 2021 The Android Open Source Project
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage apex
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
19*333d2b36SAndroid Build Coastguard Worker)
20*333d2b36SAndroid Build Coastguard Worker
21*333d2b36SAndroid Build Coastguard Workertype DeapexerProperties struct {
22*333d2b36SAndroid Build Coastguard Worker	// List of common modules that may need access to files exported by this module.
23*333d2b36SAndroid Build Coastguard Worker	//
24*333d2b36SAndroid Build Coastguard Worker	// A common module in this sense is one that is not arch specific but uses a common variant for
25*333d2b36SAndroid Build Coastguard Worker	// all architectures, e.g. java.
26*333d2b36SAndroid Build Coastguard Worker	CommonModules []string
27*333d2b36SAndroid Build Coastguard Worker
28*333d2b36SAndroid Build Coastguard Worker	// List of modules that use an embedded .prof to guide optimization of the equivalent dexpreopt artifact
29*333d2b36SAndroid Build Coastguard Worker	// This is a subset of CommonModules
30*333d2b36SAndroid Build Coastguard Worker	DexpreoptProfileGuidedModules []string
31*333d2b36SAndroid Build Coastguard Worker
32*333d2b36SAndroid Build Coastguard Worker	// List of files exported from the .apex file by this module
33*333d2b36SAndroid Build Coastguard Worker	//
34*333d2b36SAndroid Build Coastguard Worker	// Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
35*333d2b36SAndroid Build Coastguard Worker	ExportedFiles []string
36*333d2b36SAndroid Build Coastguard Worker}
37*333d2b36SAndroid Build Coastguard Worker
38*333d2b36SAndroid Build Coastguard Workertype SelectedApexProperties struct {
39*333d2b36SAndroid Build Coastguard Worker	// The path to the apex selected for use by this module.
40*333d2b36SAndroid Build Coastguard Worker	//
41*333d2b36SAndroid Build Coastguard Worker	// Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
42*333d2b36SAndroid Build Coastguard Worker	// and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
43*333d2b36SAndroid Build Coastguard Worker	// Android.bp file.
44*333d2b36SAndroid Build Coastguard Worker	Selected_apex *string `android:"path" blueprint:"mutated"`
45*333d2b36SAndroid Build Coastguard Worker}
46*333d2b36SAndroid Build Coastguard Worker
47*333d2b36SAndroid Build Coastguard Worker// deapex creates the build rules to deapex a prebuilt .apex file
48*333d2b36SAndroid Build Coastguard Worker// it returns a pointer to a DeapexerInfo object
49*333d2b36SAndroid Build Coastguard Workerfunc deapex(ctx android.ModuleContext, apexFile android.Path, deapexerProps DeapexerProperties) *android.DeapexerInfo {
50*333d2b36SAndroid Build Coastguard Worker	// Create and remember the directory into which the .apex file's contents will be unpacked.
51*333d2b36SAndroid Build Coastguard Worker	deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Worker	exports := make(map[string]android.WritablePath)
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Worker	// Create mappings from apex relative path to the extracted file's path.
56*333d2b36SAndroid Build Coastguard Worker	exportedPaths := make(android.Paths, 0, len(exports))
57*333d2b36SAndroid Build Coastguard Worker	for _, path := range deapexerProps.ExportedFiles {
58*333d2b36SAndroid Build Coastguard Worker		// Populate the exports that this makes available.
59*333d2b36SAndroid Build Coastguard Worker		extractedPath := deapexerOutput.Join(ctx, path)
60*333d2b36SAndroid Build Coastguard Worker		exports[path] = extractedPath
61*333d2b36SAndroid Build Coastguard Worker		exportedPaths = append(exportedPaths, extractedPath)
62*333d2b36SAndroid Build Coastguard Worker	}
63*333d2b36SAndroid Build Coastguard Worker
64*333d2b36SAndroid Build Coastguard Worker	// If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
65*333d2b36SAndroid Build Coastguard Worker	// deapexer and verifies that all the required files were created. Also, make the mapping from
66*333d2b36SAndroid Build Coastguard Worker	// apex relative path to extracted file path available for other modules.
67*333d2b36SAndroid Build Coastguard Worker	if len(exports) > 0 {
68*333d2b36SAndroid Build Coastguard Worker		// Make the information available for other modules.
69*333d2b36SAndroid Build Coastguard Worker		di := android.NewDeapexerInfo(ctx.ModuleName(), exports, deapexerProps.CommonModules)
70*333d2b36SAndroid Build Coastguard Worker		di.AddDexpreoptProfileGuidedExportedModuleNames(deapexerProps.DexpreoptProfileGuidedModules...)
71*333d2b36SAndroid Build Coastguard Worker
72*333d2b36SAndroid Build Coastguard Worker		// Create a sorted list of the files that this exports.
73*333d2b36SAndroid Build Coastguard Worker		exportedPaths = android.SortedUniquePaths(exportedPaths)
74*333d2b36SAndroid Build Coastguard Worker
75*333d2b36SAndroid Build Coastguard Worker		// The apex needs to export some files so create a ninja rule to unpack the apex and check that
76*333d2b36SAndroid Build Coastguard Worker		// the required files are present.
77*333d2b36SAndroid Build Coastguard Worker		builder := android.NewRuleBuilder(pctx, ctx)
78*333d2b36SAndroid Build Coastguard Worker		command := builder.Command()
79*333d2b36SAndroid Build Coastguard Worker		command.
80*333d2b36SAndroid Build Coastguard Worker			Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
81*333d2b36SAndroid Build Coastguard Worker			BuiltTool("deapexer").
82*333d2b36SAndroid Build Coastguard Worker			BuiltTool("debugfs").
83*333d2b36SAndroid Build Coastguard Worker			BuiltTool("fsck.erofs").
84*333d2b36SAndroid Build Coastguard Worker			Input(apexFile).
85*333d2b36SAndroid Build Coastguard Worker			Text(deapexerOutput.String())
86*333d2b36SAndroid Build Coastguard Worker		for _, p := range exportedPaths {
87*333d2b36SAndroid Build Coastguard Worker			command.Output(p.(android.WritablePath))
88*333d2b36SAndroid Build Coastguard Worker		}
89*333d2b36SAndroid Build Coastguard Worker		builder.Build("deapexer", "deapex "+ctx.ModuleName())
90*333d2b36SAndroid Build Coastguard Worker		return &di
91*333d2b36SAndroid Build Coastguard Worker	}
92*333d2b36SAndroid Build Coastguard Worker	return nil
93*333d2b36SAndroid Build Coastguard Worker}
94