2020-11-02 18:32:38 +01:00
|
|
|
// Copyright (C) 2021 The Android Open Source Project
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package apex
|
|
|
|
|
|
|
|
import (
|
2021-09-17 02:44:12 +02:00
|
|
|
"strings"
|
|
|
|
|
2020-11-02 18:32:38 +01:00
|
|
|
"android/soong/android"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Contains 'deapexer' a private module type used by 'prebuilt_apex' to make dex files contained
|
|
|
|
// within a .apex file referenced by `prebuilt_apex` available for use by their associated
|
|
|
|
// `java_import` modules.
|
|
|
|
//
|
|
|
|
// An 'apex' module references `java_library` modules from which .dex files are obtained that are
|
|
|
|
// stored in the resulting `.apex` file. The resulting `.apex` file is then made available as a
|
|
|
|
// prebuilt by referencing it from a `prebuilt_apex`. For each such `java_library` that is used by
|
|
|
|
// modules outside the `.apex` file a `java_import` prebuilt is made available referencing a jar
|
|
|
|
// that contains the Java classes.
|
|
|
|
//
|
|
|
|
// When building a Java module type, e.g. `java_module` or `android_app` against such prebuilts the
|
|
|
|
// `java_import` provides the classes jar (jar containing `.class` files) against which the
|
|
|
|
// module's `.java` files are compiled. That classes jar usually contains only stub classes. The
|
|
|
|
// resulting classes jar is converted into a dex jar (jar containing `.dex` files). Then if
|
|
|
|
// necessary the dex jar is further processed by `dexpreopt` to produce an optimized form of the
|
|
|
|
// library specific to the current Android version. This process requires access to implementation
|
|
|
|
// dex jars for each `java_import`. The `java_import` will obtain the implementation dex jar from
|
|
|
|
// the `.apex` file in the associated `prebuilt_apex`.
|
|
|
|
//
|
|
|
|
// This is intentionally not registered by name as it is not intended to be used from within an
|
|
|
|
// `Android.bp` file.
|
|
|
|
|
2021-05-05 19:03:47 +02:00
|
|
|
// DeapexerProperties specifies the properties supported by the deapexer module.
|
|
|
|
//
|
|
|
|
// As these are never intended to be supplied in a .bp file they use a different naming convention
|
|
|
|
// to make it clear that they are different.
|
2020-11-02 18:32:38 +01:00
|
|
|
type DeapexerProperties struct {
|
2021-05-05 19:03:47 +02:00
|
|
|
// List of common modules that may need access to files exported by this module.
|
|
|
|
//
|
|
|
|
// A common module in this sense is one that is not arch specific but uses a common variant for
|
|
|
|
// all architectures, e.g. java.
|
|
|
|
CommonModules []string
|
|
|
|
|
|
|
|
// List of files exported from the .apex file by this module
|
2021-06-17 16:59:07 +02:00
|
|
|
//
|
|
|
|
// Each entry is a path from the apex root, e.g. javalib/core-libart.jar.
|
|
|
|
ExportedFiles []string
|
2020-11-02 18:32:38 +01:00
|
|
|
}
|
|
|
|
|
2021-03-01 15:14:52 +01:00
|
|
|
type SelectedApexProperties struct {
|
|
|
|
// The path to the apex selected for use by this module.
|
|
|
|
//
|
|
|
|
// Is tagged as `android:"path"` because it will usually contain a string of the form ":<module>"
|
|
|
|
// and is tagged as "`blueprint:"mutate"` because it is only initialized in a LoadHook not an
|
|
|
|
// Android.bp file.
|
|
|
|
Selected_apex *string `android:"path" blueprint:"mutated"`
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:32:38 +01:00
|
|
|
type Deapexer struct {
|
|
|
|
android.ModuleBase
|
|
|
|
|
2021-03-01 15:14:52 +01:00
|
|
|
properties DeapexerProperties
|
|
|
|
selectedApexProperties SelectedApexProperties
|
2020-11-02 18:32:38 +01:00
|
|
|
|
|
|
|
inputApex android.Path
|
|
|
|
}
|
|
|
|
|
2021-09-17 02:44:12 +02:00
|
|
|
// Returns the name of the deapexer module corresponding to an APEX module with the given name.
|
|
|
|
func deapexerModuleName(apexModuleName string) string {
|
|
|
|
return apexModuleName + ".deapexer"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the name of the APEX module corresponding to an deapexer module with
|
|
|
|
// the given name. This reverses deapexerModuleName.
|
|
|
|
func apexModuleName(deapexerModuleName string) string {
|
|
|
|
return strings.TrimSuffix(deapexerModuleName, ".deapexer")
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:32:38 +01:00
|
|
|
func privateDeapexerFactory() android.Module {
|
|
|
|
module := &Deapexer{}
|
2021-03-01 15:14:52 +01:00
|
|
|
module.AddProperties(&module.properties, &module.selectedApexProperties)
|
2020-11-02 18:32:38 +01:00
|
|
|
android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
2021-01-26 15:18:53 +01:00
|
|
|
func (p *Deapexer) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2020-11-02 18:32:38 +01:00
|
|
|
// Add dependencies from the java modules to which this exports files from the `.apex` file onto
|
|
|
|
// this module so that they can access the `DeapexerInfo` object that this provides.
|
2021-05-05 19:03:47 +02:00
|
|
|
for _, lib := range p.properties.CommonModules {
|
2020-11-02 18:32:38 +01:00
|
|
|
dep := prebuiltApexExportedModuleName(ctx, lib)
|
|
|
|
ctx.AddReverseDependency(ctx.Module(), android.DeapexerTag, dep)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Deapexer) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
2021-03-01 15:14:52 +01:00
|
|
|
p.inputApex = android.OptionalPathForModuleSrc(ctx, p.selectedApexProperties.Selected_apex).Path()
|
2020-11-02 18:32:38 +01:00
|
|
|
|
|
|
|
// Create and remember the directory into which the .apex file's contents will be unpacked.
|
|
|
|
deapexerOutput := android.PathForModuleOut(ctx, "deapexer")
|
|
|
|
|
2021-09-09 10:12:46 +02:00
|
|
|
exports := make(map[string]android.WritablePath)
|
2020-11-02 18:32:38 +01:00
|
|
|
|
2021-06-17 16:59:07 +02:00
|
|
|
// Create mappings from apex relative path to the extracted file's path.
|
|
|
|
exportedPaths := make(android.Paths, 0, len(exports))
|
|
|
|
for _, path := range p.properties.ExportedFiles {
|
2021-05-05 19:03:47 +02:00
|
|
|
// Populate the exports that this makes available.
|
2021-06-17 16:59:07 +02:00
|
|
|
extractedPath := deapexerOutput.Join(ctx, path)
|
|
|
|
exports[path] = extractedPath
|
|
|
|
exportedPaths = append(exportedPaths, extractedPath)
|
2020-11-02 18:32:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the prebuilt_apex exports any files then create a build rule that unpacks the apex using
|
|
|
|
// deapexer and verifies that all the required files were created. Also, make the mapping from
|
2021-06-17 16:59:07 +02:00
|
|
|
// apex relative path to extracted file path available for other modules.
|
2020-11-02 18:32:38 +01:00
|
|
|
if len(exports) > 0 {
|
|
|
|
// Make the information available for other modules.
|
2021-09-17 02:44:12 +02:00
|
|
|
di := android.NewDeapexerInfo(apexModuleName(ctx.ModuleName()), exports)
|
|
|
|
ctx.SetProvider(android.DeapexerProvider, di)
|
2020-11-02 18:32:38 +01:00
|
|
|
|
|
|
|
// Create a sorted list of the files that this exports.
|
|
|
|
exportedPaths = android.SortedUniquePaths(exportedPaths)
|
|
|
|
|
|
|
|
// The apex needs to export some files so create a ninja rule to unpack the apex and check that
|
|
|
|
// the required files are present.
|
|
|
|
builder := android.NewRuleBuilder(pctx, ctx)
|
|
|
|
command := builder.Command()
|
|
|
|
command.
|
|
|
|
Tool(android.PathForSource(ctx, "build/soong/scripts/unpack-prebuilt-apex.sh")).
|
|
|
|
BuiltTool("deapexer").
|
|
|
|
BuiltTool("debugfs").
|
2022-12-12 18:37:20 +01:00
|
|
|
BuiltTool("fsck.erofs").
|
2020-11-02 18:32:38 +01:00
|
|
|
Input(p.inputApex).
|
|
|
|
Text(deapexerOutput.String())
|
|
|
|
for _, p := range exportedPaths {
|
|
|
|
command.Output(p.(android.WritablePath))
|
|
|
|
}
|
2021-09-17 02:44:12 +02:00
|
|
|
builder.Build("deapexer", "deapex "+apexModuleName(ctx.ModuleName()))
|
2020-11-02 18:32:38 +01:00
|
|
|
}
|
|
|
|
}
|