| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 1 | // Copyright 2023 Google Inc. All rights reserved. | 
 | 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 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 15 | package aconfig | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 16 |  | 
 | 17 | import ( | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 18 | 	"fmt" | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 19 | 	"strings" | 
| Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 20 |  | 
| Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 21 | 	"android/soong/android" | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 22 |  | 
| Vinh Tran | 457ddef | 2023-08-02 13:50:26 -0400 | [diff] [blame] | 23 | 	"github.com/google/blueprint" | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 24 | ) | 
 | 25 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 26 | type DeclarationsModule struct { | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 27 | 	android.ModuleBase | 
 | 28 | 	android.DefaultableModuleBase | 
 | 29 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 30 | 	// Properties for "aconfig_declarations" | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 31 | 	properties struct { | 
 | 32 | 		// aconfig files, relative to this Android.bp file | 
 | 33 | 		Srcs []string `android:"path"` | 
 | 34 |  | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 35 | 		// Release config flag package | 
 | 36 | 		Package string | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 37 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 38 | 		// Values from TARGET_RELEASE / RELEASE_ACONFIG_VALUE_SETS | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 39 | 		Values []string `blueprint:"mutated"` | 
| Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 40 |  | 
 | 41 | 		// Container(system/vendor/apex) that this module belongs to | 
 | 42 | 		Container string | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 43 | 	} | 
 | 44 |  | 
 | 45 | 	intermediatePath android.WritablePath | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 46 | } | 
 | 47 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 48 | func DeclarationsFactory() android.Module { | 
 | 49 | 	module := &DeclarationsModule{} | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 50 |  | 
 | 51 | 	android.InitAndroidModule(module) | 
 | 52 | 	android.InitDefaultableModule(module) | 
 | 53 | 	module.AddProperties(&module.properties) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 54 |  | 
 | 55 | 	return module | 
 | 56 | } | 
 | 57 |  | 
 | 58 | type implicitValuesTagType struct { | 
 | 59 | 	blueprint.BaseDependencyTag | 
 | 60 | } | 
 | 61 |  | 
 | 62 | var implicitValuesTag = implicitValuesTagType{} | 
 | 63 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 64 | func (module *DeclarationsModule) DepsMutator(ctx android.BottomUpMutatorContext) { | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 65 | 	// Validate Properties | 
 | 66 | 	if len(module.properties.Srcs) == 0 { | 
 | 67 | 		ctx.PropertyErrorf("srcs", "missing source files") | 
 | 68 | 		return | 
 | 69 | 	} | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 70 | 	if len(module.properties.Package) == 0 { | 
 | 71 | 		ctx.PropertyErrorf("package", "missing package property") | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 72 | 	} | 
| Yu Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 73 | 	// TODO(b/311155208): Add mandatory check for container after all pre-existing | 
 | 74 | 	// ones are changed. | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 75 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 76 | 	// Add a dependency on the aconfig_value_sets defined in | 
 | 77 | 	// RELEASE_ACONFIG_VALUE_SETS, and add any aconfig_values that | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 78 | 	// match our package. | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 79 | 	valuesFromConfig := ctx.Config().ReleaseAconfigValueSets() | 
| Yu Liu | eebb259 | 2023-10-12 20:31:27 -0700 | [diff] [blame] | 80 | 	if len(valuesFromConfig) > 0 { | 
 | 81 | 		ctx.AddDependency(ctx.Module(), implicitValuesTag, valuesFromConfig...) | 
| Yu Liu | 2cc802a | 2023-09-05 17:19:45 -0700 | [diff] [blame] | 82 | 	} | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 83 | } | 
 | 84 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 85 | func (module *DeclarationsModule) OutputFiles(tag string) (android.Paths, error) { | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 86 | 	switch tag { | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 87 | 	case "": | 
 | 88 | 		// The default output of this module is the intermediates format, which is | 
 | 89 | 		// not installable and in a private format that no other rules can handle | 
 | 90 | 		// correctly. | 
 | 91 | 		return []android.Path{module.intermediatePath}, nil | 
 | 92 | 	default: | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 93 | 		return nil, fmt.Errorf("unsupported aconfig_declarations module reference tag %q", tag) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 94 | 	} | 
 | 95 | } | 
 | 96 |  | 
 | 97 | func joinAndPrefix(prefix string, values []string) string { | 
 | 98 | 	var sb strings.Builder | 
 | 99 | 	for _, v := range values { | 
 | 100 | 		sb.WriteString(prefix) | 
 | 101 | 		sb.WriteString(v) | 
 | 102 | 	} | 
 | 103 | 	return sb.String() | 
 | 104 | } | 
 | 105 |  | 
| Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 106 | func optionalVariable(prefix string, value string) string { | 
 | 107 | 	var sb strings.Builder | 
 | 108 | 	if value != "" { | 
 | 109 | 		sb.WriteString(prefix) | 
 | 110 | 		sb.WriteString(value) | 
 | 111 | 	} | 
 | 112 | 	return sb.String() | 
 | 113 | } | 
 | 114 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 115 | func (module *DeclarationsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
 | 116 | 	// Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag | 
| Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 117 | 	valuesFiles := make([]android.Path, 0) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 118 | 	ctx.VisitDirectDeps(func(dep android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 119 | 		if depData, ok := android.OtherModuleProvider(ctx, dep, valueSetProviderKey); ok { | 
 | 120 | 			paths, ok := depData.AvailablePackages[module.properties.Package] | 
 | 121 | 			if ok { | 
 | 122 | 				valuesFiles = append(valuesFiles, paths...) | 
 | 123 | 				for _, path := range paths { | 
 | 124 | 					module.properties.Values = append(module.properties.Values, path.String()) | 
 | 125 | 				} | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 126 | 			} | 
 | 127 | 		} | 
 | 128 | 	}) | 
 | 129 |  | 
 | 130 | 	// Intermediate format | 
| Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 131 | 	declarationFiles := android.PathsForModuleSrc(ctx, module.properties.Srcs) | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 132 | 	intermediateCacheFilePath := android.PathForModuleOut(ctx, "intermediate.pb") | 
| Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 133 | 	defaultPermission := ctx.Config().ReleaseAconfigFlagDefaultPermission() | 
| Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 134 | 	inputFiles := make([]android.Path, len(declarationFiles)) | 
 | 135 | 	copy(inputFiles, declarationFiles) | 
 | 136 | 	inputFiles = append(inputFiles, valuesFiles...) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 137 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 138 | 		Rule:        aconfigRule, | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 139 | 		Output:      intermediateCacheFilePath, | 
| Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 140 | 		Inputs:      inputFiles, | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 141 | 		Description: "aconfig_declarations", | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 142 | 		Args: map[string]string{ | 
| Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 143 | 			"release_version":    ctx.Config().ReleaseVersion(), | 
 | 144 | 			"package":            module.properties.Package, | 
| Joe Onorato | 4551ea1 | 2023-08-19 19:02:15 -0700 | [diff] [blame] | 145 | 			"declarations":       android.JoinPathsWithPrefix(declarationFiles, "--declarations "), | 
| Zhi Dou | 3f65a41 | 2023-08-10 21:47:40 +0000 | [diff] [blame] | 146 | 			"values":             joinAndPrefix(" --values ", module.properties.Values), | 
 | 147 | 			"default-permission": optionalVariable(" --default-permission ", defaultPermission), | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 148 | 		}, | 
 | 149 | 	}) | 
 | 150 |  | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 151 | 	intermediateDumpFilePath := android.PathForModuleOut(ctx, "intermediate.txt") | 
 | 152 | 	ctx.Build(pctx, android.BuildParams{ | 
 | 153 | 		Rule:        aconfigTextRule, | 
 | 154 | 		Output:      intermediateDumpFilePath, | 
 | 155 | 		Inputs:      android.Paths{intermediateCacheFilePath}, | 
 | 156 | 		Description: "aconfig_text", | 
 | 157 | 	}) | 
 | 158 |  | 
| LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame] | 159 | 	android.SetProvider(ctx, android.AconfigDeclarationsProviderKey, android.AconfigDeclarationsProviderData{ | 
| Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 160 | 		Package:                     module.properties.Package, | 
 | 161 | 		Container:                   module.properties.Container, | 
 | 162 | 		IntermediateCacheOutputPath: intermediateCacheFilePath, | 
 | 163 | 		IntermediateDumpOutputPath:  intermediateDumpFilePath, | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 164 | 	}) | 
 | 165 |  | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 166 | } | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 167 |  | 
 | 168 | func SetAconfigFileMkEntries(m *android.ModuleBase, entries *android.AndroidMkEntries, aconfigFiles map[string]android.Paths) { | 
| Yu Liu | 22e32f1 | 2023-12-18 14:44:34 -0800 | [diff] [blame] | 169 | 	// TODO(b/311155208): The default container here should be system. | 
 | 170 | 	container := "" | 
 | 171 |  | 
 | 172 | 	if m.SocSpecific() { | 
 | 173 | 		container = "vendor" | 
 | 174 | 	} else if m.ProductSpecific() { | 
 | 175 | 		container = "product" | 
 | 176 | 	} else if m.SystemExtSpecific() { | 
 | 177 | 		container = "system_ext" | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 178 | 	} | 
| Yu Liu | 22e32f1 | 2023-12-18 14:44:34 -0800 | [diff] [blame] | 179 |  | 
 | 180 | 	entries.SetPaths("LOCAL_ACONFIG_FILES", aconfigFiles[container]) | 
| Yu Liu | 6dc93f9 | 2023-12-14 01:19:35 +0000 | [diff] [blame] | 181 | } |