2023-05-09 17:14:14 +02:00
|
|
|
// Copyright 2023 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
package aconfig
|
2023-05-09 17:14:14 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-08-02 19:50:26 +02:00
|
|
|
|
2023-09-06 02:19:45 +02:00
|
|
|
"android/soong/android"
|
2023-11-29 20:35:29 +01:00
|
|
|
|
2023-08-02 19:50:26 +02:00
|
|
|
"github.com/google/blueprint"
|
2023-05-09 17:14:14 +02:00
|
|
|
)
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
type DeclarationsModule struct {
|
2023-05-09 17:14:14 +02:00
|
|
|
android.ModuleBase
|
|
|
|
android.DefaultableModuleBase
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
// Properties for "aconfig_declarations"
|
2023-05-09 17:14:14 +02:00
|
|
|
properties struct {
|
|
|
|
// aconfig files, relative to this Android.bp file
|
|
|
|
Srcs []string `android:"path"`
|
|
|
|
|
2023-06-21 22:49:37 +02:00
|
|
|
// Release config flag package
|
|
|
|
Package string
|
2023-05-09 17:14:14 +02:00
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
// Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS
|
2023-05-09 17:14:14 +02:00
|
|
|
Values []string `blueprint:"mutated"`
|
2023-11-17 02:05:47 +01:00
|
|
|
|
|
|
|
// Container(system/vendor/apex) that this module belongs to
|
|
|
|
Container string
|
2023-05-09 17:14:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
intermediatePath android.WritablePath
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
func DeclarationsFactory() android.Module {
|
|
|
|
module := &DeclarationsModule{}
|
2023-05-09 17:14:14 +02:00
|
|
|
|
|
|
|
android.InitAndroidModule(module)
|
|
|
|
android.InitDefaultableModule(module)
|
|
|
|
module.AddProperties(&module.properties)
|
|
|
|
|
|
|
|
return module
|
|
|
|
}
|
|
|
|
|
|
|
|
type implicitValuesTagType struct {
|
|
|
|
blueprint.BaseDependencyTag
|
|
|
|
}
|
|
|
|
|
|
|
|
var implicitValuesTag = implicitValuesTagType{}
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) {
|
2023-05-09 17:14:14 +02:00
|
|
|
// Validate Properties
|
|
|
|
if len(module.properties.Srcs) == 0 {
|
|
|
|
ctx.PropertyErrorf("srcs", "missing source files")
|
|
|
|
return
|
|
|
|
}
|
2023-06-21 22:49:37 +02:00
|
|
|
if len(module.properties.Package) == 0 {
|
|
|
|
ctx.PropertyErrorf("package", "missing package property")
|
2023-05-09 17:14:14 +02:00
|
|
|
}
|
2023-11-17 02:05:47 +01:00
|
|
|
// TODO(b/311155208): Add mandatory check for container after all pre-existing
|
|
|
|
// ones are changed.
|
2023-05-09 17:14:14 +02:00
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
// Add a dependency on the aconfig_value_sets defined in
|
|
|
|
// RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that
|
2023-06-21 22:49:37 +02:00
|
|
|
// match our package.
|
2023-06-22 00:16:23 +02:00
|
|
|
valuesFromConfig := ctx.Config().ReleaseAconfigValueSets()
|
2023-10-13 05:31:27 +02:00
|
|
|
if len(valuesFromConfig) > 0 {
|
|
|
|
ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...)
|
2023-09-06 02:19:45 +02:00
|
|
|
}
|
2023-05-09 17:14:14 +02:00
|
|
|
}
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) {
|
2023-05-09 17:14:14 +02:00
|
|
|
switch tag {
|
|
|
|
case "":
|
|
|
|
// The default output of this module is the intermediates format, which is
|
|
|
|
// not installable and in a private format that no other rules can handle
|
|
|
|
// correctly.
|
|
|
|
return []android.Path{module.intermediatePath}, nil
|
|
|
|
default:
|
2023-06-22 00:16:23 +02:00
|
|
|
return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag)
|
2023-05-09 17:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinAndPrefix(prefix string, values []string) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
for _, v := range values {
|
|
|
|
sb.WriteString(prefix)
|
|
|
|
sb.WriteString(v)
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2023-08-10 23:47:40 +02:00
|
|
|
func optionalVariable(prefix string, value string) string {
|
|
|
|
var sb strings.Builder
|
|
|
|
if value != "" {
|
|
|
|
sb.WriteString(prefix)
|
|
|
|
sb.WriteString(value)
|
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
// Provider published by aconfig_value_set
|
2023-11-17 02:05:47 +01:00
|
|
|
type DeclarationsProviderData struct {
|
2023-11-29 20:35:29 +01:00
|
|
|
Package string
|
|
|
|
Container string
|
|
|
|
IntermediateCacheOutputPath android.WritablePath
|
|
|
|
IntermediateDumpOutputPath android.WritablePath
|
2023-06-01 23:42:59 +02:00
|
|
|
}
|
|
|
|
|
2023-12-13 01:39:03 +01:00
|
|
|
var DeclarationsProviderKey = blueprint.NewProvider[DeclarationsProviderData]()
|
2023-11-17 02:05:47 +01:00
|
|
|
|
|
|
|
// This is used to collect the aconfig declarations info on the transitive closure,
|
|
|
|
// the data is keyed on the container.
|
|
|
|
type TransitiveDeclarationsInfo struct {
|
2023-11-28 22:14:56 +01:00
|
|
|
AconfigFiles map[string]android.Paths
|
2023-11-17 02:05:47 +01:00
|
|
|
}
|
|
|
|
|
2023-12-13 01:39:03 +01:00
|
|
|
var TransitiveDeclarationsInfoProvider = blueprint.NewProvider[TransitiveDeclarationsInfo]()
|
2023-06-01 23:42:59 +02:00
|
|
|
|
2023-06-22 00:16:23 +02:00
|
|
|
func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
|
|
|
|
// Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
|
2023-08-20 04:02:15 +02:00
|
|
|
valuesFiles := make([]android.Path, 0)
|
2023-05-09 17:14:14 +02:00
|
|
|
ctx.VisitDirectDeps(func(dep android.Module) {
|
2023-12-13 22:47:44 +01:00
|
|
|
if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok {
|
|
|
|
paths, ok := depData.AvailablePackages[module.properties.Package]
|
|
|
|
if ok {
|
|
|
|
valuesFiles = append(valuesFiles, paths...)
|
|
|
|
for _, path := range paths {
|
|
|
|
module.properties.Values = append(module.properties.Values, path.String())
|
|
|
|
}
|
2023-05-09 17:14:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Intermediate format
|
2023-08-20 04:02:15 +02:00
|
|
|
declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs)
|
2023-11-29 20:35:29 +01:00
|
|
|
intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb")
|
2023-08-10 23:47:40 +02:00
|
|
|
defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission()
|
2023-08-20 04:02:15 +02:00
|
|
|
inputFiles := make([]android.Path, len(declarationFiles))
|
|
|
|
copy(inputFiles, declarationFiles)
|
|
|
|
inputFiles = append(inputFiles, valuesFiles...)
|
2023-05-09 17:14:14 +02:00
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aconfigRule,
|
2023-11-29 20:35:29 +01:00
|
|
|
Output: intermediateCacheFilePath,
|
2023-08-20 04:02:15 +02:00
|
|
|
Inputs: inputFiles,
|
2023-06-22 00:16:23 +02:00
|
|
|
Description: "aconfig_declarations",
|
2023-05-09 17:14:14 +02:00
|
|
|
Args: map[string]string{
|
2023-08-10 23:47:40 +02:00
|
|
|
"release_version": ctx.Config().ReleaseVersion(),
|
|
|
|
"package": module.properties.Package,
|
2023-08-20 04:02:15 +02:00
|
|
|
"declarations": android.JoinPathsWithPrefix(declarationFiles, "--declarations "),
|
2023-08-10 23:47:40 +02:00
|
|
|
"values": joinAndPrefix(" --values ", module.properties.Values),
|
|
|
|
"default-permission": optionalVariable(" --default-permission ", defaultPermission),
|
2023-05-09 17:14:14 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2023-11-29 20:35:29 +01:00
|
|
|
intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt")
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: aconfigTextRule,
|
|
|
|
Output: intermediateDumpFilePath,
|
|
|
|
Inputs: android.Paths{intermediateCacheFilePath},
|
|
|
|
Description: "aconfig_text",
|
|
|
|
})
|
|
|
|
|
2023-12-14 00:19:49 +01:00
|
|
|
android.SetProvider(ctx, DeclarationsProviderKey, DeclarationsProviderData{
|
2023-11-29 20:35:29 +01:00
|
|
|
Package: module.properties.Package,
|
|
|
|
Container: module.properties.Container,
|
|
|
|
IntermediateCacheOutputPath: intermediateCacheFilePath,
|
|
|
|
IntermediateDumpOutputPath: intermediateDumpFilePath,
|
2023-05-09 17:14:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
}
|
2023-11-28 22:14:56 +01:00
|
|
|
func CollectDependencyAconfigFiles(ctx android.ModuleContext, mergedAconfigFiles *map[string]android.Paths) {
|
|
|
|
if *mergedAconfigFiles == nil {
|
|
|
|
*mergedAconfigFiles = make(map[string]android.Paths)
|
2023-11-17 02:05:47 +01:00
|
|
|
}
|
|
|
|
ctx.VisitDirectDeps(func(module android.Module) {
|
2023-12-13 22:47:44 +01:00
|
|
|
if dep, _ := android.OtherModuleProvider(ctx, module, DeclarationsProviderKey); dep.IntermediateCacheOutputPath != nil {
|
2023-11-29 20:35:29 +01:00
|
|
|
(*mergedAconfigFiles)[dep.Container] = append((*mergedAconfigFiles)[dep.Container], dep.IntermediateCacheOutputPath)
|
2023-11-17 02:05:47 +01:00
|
|
|
return
|
|
|
|
}
|
2023-12-13 22:47:44 +01:00
|
|
|
if dep, _ := android.OtherModuleProvider(ctx, module, TransitiveDeclarationsInfoProvider); len(dep.AconfigFiles) > 0 {
|
2023-11-28 22:14:56 +01:00
|
|
|
for container, v := range dep.AconfigFiles {
|
|
|
|
(*mergedAconfigFiles)[container] = append((*mergedAconfigFiles)[container], v...)
|
|
|
|
}
|
2023-11-17 02:05:47 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-11-28 22:14:56 +01:00
|
|
|
for container, aconfigFiles := range *mergedAconfigFiles {
|
|
|
|
(*mergedAconfigFiles)[container] = mergeAconfigFiles(ctx, aconfigFiles)
|
|
|
|
}
|
|
|
|
|
2023-12-14 00:19:49 +01:00
|
|
|
android.SetProvider(ctx, TransitiveDeclarationsInfoProvider, TransitiveDeclarationsInfo{
|
2023-11-28 22:14:56 +01:00
|
|
|
AconfigFiles: *mergedAconfigFiles,
|
2023-11-17 02:05:47 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-28 22:14:56 +01:00
|
|
|
func mergeAconfigFiles(ctx android.ModuleContext, inputs android.Paths) android.Paths {
|
2023-12-01 01:45:50 +01:00
|
|
|
inputs = android.LastUniquePaths(inputs)
|
2023-11-28 22:14:56 +01:00
|
|
|
if len(inputs) == 1 {
|
|
|
|
return android.Paths{inputs[0]}
|
2023-11-17 02:05:47 +01:00
|
|
|
}
|
2023-11-28 22:14:56 +01:00
|
|
|
|
|
|
|
output := android.PathForModuleOut(ctx, "aconfig_merged.pb")
|
|
|
|
|
|
|
|
ctx.Build(pctx, android.BuildParams{
|
|
|
|
Rule: mergeAconfigFilesRule,
|
|
|
|
Description: "merge aconfig files",
|
|
|
|
Inputs: inputs,
|
|
|
|
Output: output,
|
|
|
|
Args: map[string]string{
|
|
|
|
"flags": android.JoinWithPrefix(inputs.Strings(), "--cache "),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return android.Paths{output}
|
2023-11-17 02:05:47 +01:00
|
|
|
}
|
2023-12-14 02:19:35 +01:00
|
|
|
|
|
|
|
func SetAconfigFileMkEntries(m *android.ModuleBase, entries *android.AndroidMkEntries, aconfigFiles map[string]android.Paths) {
|
2023-12-18 23:44:34 +01:00
|
|
|
// TODO(b/311155208): The default container here should be system.
|
|
|
|
container := ""
|
|
|
|
|
|
|
|
if m.SocSpecific() {
|
|
|
|
container = "vendor"
|
|
|
|
} else if m.ProductSpecific() {
|
|
|
|
container = "product"
|
|
|
|
} else if m.SystemExtSpecific() {
|
|
|
|
container = "system_ext"
|
2023-12-14 02:19:35 +01:00
|
|
|
}
|
2023-12-18 23:44:34 +01:00
|
|
|
|
|
|
|
entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[container])
|
2023-12-14 02:19:35 +01:00
|
|
|
}
|