| 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 ( | 
 | 18 | 	"android/soong/android" | 
 | 19 | 	"github.com/google/blueprint" | 
 | 20 | ) | 
 | 21 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 22 | // Properties for "aconfig_value_set" | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 23 | type ValueSetModule struct { | 
 | 24 | 	android.ModuleBase | 
 | 25 | 	android.DefaultableModuleBase | 
 | 26 |  | 
 | 27 | 	properties struct { | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 28 | 		// aconfig_values modules | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 29 | 		Values []string | 
 | 30 | 	} | 
 | 31 | } | 
 | 32 |  | 
 | 33 | func ValueSetFactory() android.Module { | 
 | 34 | 	module := &ValueSetModule{} | 
 | 35 |  | 
 | 36 | 	android.InitAndroidModule(module) | 
 | 37 | 	android.InitDefaultableModule(module) | 
 | 38 | 	module.AddProperties(&module.properties) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 39 |  | 
 | 40 | 	return module | 
 | 41 | } | 
 | 42 |  | 
 | 43 | // Dependency tag for values property | 
 | 44 | type valueSetType struct { | 
 | 45 | 	blueprint.BaseDependencyTag | 
 | 46 | } | 
 | 47 |  | 
 | 48 | var valueSetTag = valueSetType{} | 
 | 49 |  | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 50 | // Provider published by aconfig_value_set | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 51 | type valueSetProviderData struct { | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 52 | 	// The package of each of the | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 53 | 	// (map of package --> aconfig_module) | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 54 | 	AvailablePackages map[string]android.Paths | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 55 | } | 
 | 56 |  | 
| Colin Cross | bc7d76c | 2023-12-12 16:39:03 -0800 | [diff] [blame] | 57 | var valueSetProviderKey = blueprint.NewProvider[valueSetProviderData]() | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 58 |  | 
 | 59 | func (module *ValueSetModule) DepsMutator(ctx android.BottomUpMutatorContext) { | 
 | 60 | 	deps := ctx.AddDependency(ctx.Module(), valueSetTag, module.properties.Values...) | 
 | 61 | 	for _, dep := range deps { | 
 | 62 | 		_, ok := dep.(*ValuesModule) | 
 | 63 | 		if !ok { | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 64 | 			ctx.PropertyErrorf("values", "values must be a aconfig_values module") | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 65 | 			return | 
 | 66 | 		} | 
 | 67 | 	} | 
 | 68 | } | 
 | 69 |  | 
 | 70 | func (module *ValueSetModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 71 | 	// Accumulate the packages of the values modules listed, and set that as an | 
| Joe Onorato | 981c926 | 2023-06-21 15:16:23 -0700 | [diff] [blame] | 72 | 	// valueSetProviderKey provider that aconfig modules can read and use | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 73 | 	// to append values to their aconfig actions. | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 74 | 	packages := make(map[string]android.Paths) | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 75 | 	ctx.VisitDirectDeps(func(dep android.Module) { | 
| Colin Cross | 313aa54 | 2023-12-13 13:47:44 -0800 | [diff] [blame] | 76 | 		if depData, ok := android.OtherModuleProvider(ctx, dep, valuesProviderKey); ok { | 
 | 77 | 			srcs := make([]android.Path, len(depData.Values)) | 
 | 78 | 			copy(srcs, depData.Values) | 
 | 79 | 			packages[depData.Package] = srcs | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 80 | 		} | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 81 |  | 
 | 82 | 	}) | 
| Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 83 | 	android.SetProvider(ctx, valueSetProviderKey, valueSetProviderData{ | 
| Joe Onorato | 81b25ed | 2023-06-21 13:49:37 -0700 | [diff] [blame] | 84 | 		AvailablePackages: packages, | 
| Joe Onorato | fee845a | 2023-05-09 08:14:14 -0700 | [diff] [blame] | 85 | 	}) | 
 | 86 | } |