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
21// Contains 'deapexer' a private module type used by 'prebuilt_apex' to make dex files contained
22// within a .apex file referenced by `prebuilt_apex` available for use by their associated
23// `java_import` modules.
24//
25// An 'apex' module references `java_library` modules from which .dex files are obtained that are
26// stored in the resulting `.apex` file. The resulting `.apex` file is then made available as a
27// prebuilt by referencing it from a `prebuilt_apex`. For each such `java_library` that is used by
28// modules outside the `.apex` file a `java_import` prebuilt is made available referencing a jar
29// that contains the Java classes.
30//
31// When building a Java module type, e.g. `java_module` or `android_app` against such prebuilts the
32// `java_import` provides the classes jar  (jar containing `.class` files) against which the
33// module's `.java` files are compiled. That classes jar usually contains only stub classes. The
34// resulting classes jar is converted into a dex jar (jar containing `.dex` files). Then if
35// necessary the dex jar is further processed by `dexpreopt` to produce an optimized form of the
36// library specific to the current Android version. This process requires access to implementation
37// dex jars for each `java_import`. The `java_import` will obtain the implementation dex jar from
38// the `.apex` file in the associated `prebuilt_apex`.
39//
40// This is intentionally not registered by name as it is not intended to be used from within an
41// `Android.bp` file.
42
43// DeapexerProperties specifies the properties supported by the deapexer module.
44//
45// As these are never intended to be supplied in a .bp file they use a different naming convention
46// to make it clear that they are different.
47type DeapexerProperties struct {
48	// List of common modules that may need access to files exported by this module.
49	//
50	// A common module in this sense is one that is not arch specific but uses a common variant for
51	// all architectures, e.g. java.
52	CommonModules []string
53
54	// List of files exported from the .apex file by this module
55	//
56	// Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
57	ExportedFiles []string
58}
59
60type SelectedApexProperties struct {
61	// The path to the apex selected for use by this module.
62	//
63	// Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
64	// and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
65	// Android.bp file.
66	Selected_apex *string `android:"path" blueprint:"mutated"`
67}
68
69type Deapexer struct {
70	android.ModuleBase
71
72	properties             DeapexerProperties
73	selectedApexProperties SelectedApexProperties
74
75	inputApex android.Path
76}
77
78func privateDeapexerFactory() android.Module {
79	module := &Deapexer{}
80	module.AddProperties(&module.properties, &module.selectedApexProperties)
81	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
82	return module
83}
84
85func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
86	// Add dependencies from the java modules to which this exports files from the `.apex` file onto
87	// this module so that they can access the `DeapexerInfo` object that this provides.
88	for _, lib := range p.properties.CommonModules {
89		dep := prebuiltApexExportedModuleName(ctx, lib)
90		ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
91	}
92}
93
94func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
95	p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
96
97	// Create and remember the directory into which the .apex file's contents will be unpacked.
98	deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
99
100	exports := make(map[string]android.Path)
101
102	// Create mappings from apex relative path to the extracted file's path.
103	exportedPaths := make(android.Paths, 0, len(exports))
104	for _, path := range p.properties.ExportedFiles {
105		// Populate the exports that this makes available.
106		extractedPath := deapexerOutput.Join(ctx, path)
107		exports[path] = extractedPath
108		exportedPaths = append(exportedPaths, extractedPath)
109	}
110
111	// If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
112	// deapexer and verifies that all the required files were created. Also, make the mapping from
113	// apex relative path to extracted file path available for other modules.
114	if len(exports) > 0 {
115		// Make the information available for other modules.
116		ctx.SetProvider(android.DeapexerProvider, android.NewDeapexerInfo(exports))
117
118		// Create a sorted list of the files that this exports.
119		exportedPaths = android.SortedUniquePaths(exportedPaths)
120
121		// The apex needs to export some files so create a ninja rule to unpack the apex and check that
122		// the required files are present.
123		builder := android.NewRuleBuilder(pctx, ctx)
124		command := builder.Command()
125		command.
126			Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
127			BuiltTool("deapexer").
128			BuiltTool("debugfs").
129			Input(p.inputApex).
130			Text(deapexerOutput.String())
131		for _, p := range exportedPaths {
132			command.Output(p.(android.WritablePath))
133		}
134		builder.Build("deapexer", "deapex "+ctx.ModuleName())
135	}
136}
137