| Jihoon Kang | 2a43e56 | 2024-02-12 19:05:12 +0000 | [diff] [blame] | 1 | // Copyright 2024 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 |  | 
|  | 15 | package codegen | 
|  | 16 |  | 
|  | 17 | import ( | 
|  | 18 | "android/soong/aconfig" | 
|  | 19 | "android/soong/android" | 
|  | 20 | "fmt" | 
|  | 21 |  | 
|  | 22 | "github.com/google/blueprint" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | type dependencyTag struct { | 
|  | 26 | blueprint.BaseDependencyTag | 
|  | 27 | name string | 
|  | 28 | } | 
|  | 29 |  | 
|  | 30 | var ( | 
|  | 31 | aconfigDeclarationsGroupTag = dependencyTag{name: "aconfigDeclarationsGroup"} | 
|  | 32 | javaAconfigLibraryTag       = dependencyTag{name: "javaAconfigLibrary"} | 
|  | 33 | ccAconfigLibraryTag         = dependencyTag{name: "ccAconfigLibrary"} | 
|  | 34 | rustAconfigLibraryTag       = dependencyTag{name: "rustAconfigLibrary"} | 
|  | 35 | ) | 
|  | 36 |  | 
|  | 37 | type AconfigDeclarationsGroup struct { | 
|  | 38 | android.ModuleBase | 
|  | 39 | android.DefaultableModuleBase | 
|  | 40 |  | 
|  | 41 | properties AconfigDeclarationsGroupProperties | 
|  | 42 |  | 
|  | 43 | aconfigDeclarationNames      []string | 
|  | 44 | intermediateCacheOutputPaths android.Paths | 
|  | 45 | javaSrcjars                  android.Paths | 
|  | 46 | } | 
|  | 47 |  | 
|  | 48 | type AconfigDeclarationsGroupProperties struct { | 
|  | 49 |  | 
|  | 50 | // Name of the aconfig_declarations_group modules | 
|  | 51 | Aconfig_declarations_groups []string | 
|  | 52 |  | 
|  | 53 | // Name of the java_aconfig_library modules | 
|  | 54 | Java_aconfig_libraries []string | 
|  | 55 |  | 
|  | 56 | // Name of the cc_aconfig_library modules | 
|  | 57 | Cc_aconfig_libraries []string | 
|  | 58 |  | 
|  | 59 | // Name of the rust_aconfig_library modules | 
|  | 60 | Rust_aconfig_libraries []string | 
|  | 61 | } | 
|  | 62 |  | 
|  | 63 | func AconfigDeclarationsGroupFactory() android.Module { | 
|  | 64 | module := &AconfigDeclarationsGroup{} | 
|  | 65 | module.AddProperties(&module.properties) | 
|  | 66 | android.InitAndroidModule(module) | 
|  | 67 | android.InitDefaultableModule(module) | 
|  | 68 | return module | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | func (adg *AconfigDeclarationsGroup) DepsMutator(ctx android.BottomUpMutatorContext) { | 
|  | 72 | ctx.AddDependency(ctx.Module(), aconfigDeclarationsGroupTag, adg.properties.Aconfig_declarations_groups...) | 
|  | 73 | ctx.AddDependency(ctx.Module(), javaAconfigLibraryTag, adg.properties.Java_aconfig_libraries...) | 
|  | 74 | ctx.AddDependency(ctx.Module(), ccAconfigLibraryTag, adg.properties.Cc_aconfig_libraries...) | 
|  | 75 | ctx.AddDependency(ctx.Module(), rustAconfigLibraryTag, adg.properties.Rust_aconfig_libraries...) | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | func (adg *AconfigDeclarationsGroup) VisitDeps(ctx android.ModuleContext) { | 
|  | 79 | ctx.VisitDirectDeps(func(dep android.Module) { | 
|  | 80 | tag := ctx.OtherModuleDependencyTag(dep) | 
|  | 81 | if provider, ok := android.OtherModuleProvider(ctx, dep, aconfig.CodegenInfoProvider); ok { | 
|  | 82 |  | 
|  | 83 | // aconfig declaration names and cache files are collected for all aconfig library dependencies | 
|  | 84 | adg.aconfigDeclarationNames = append(adg.aconfigDeclarationNames, provider.AconfigDeclarations...) | 
|  | 85 | adg.intermediateCacheOutputPaths = append(adg.intermediateCacheOutputPaths, provider.IntermediateCacheOutputPaths...) | 
|  | 86 |  | 
|  | 87 | switch tag { | 
|  | 88 | case aconfigDeclarationsGroupTag: | 
|  | 89 | // Will retrieve outputs from another language codegen modules when support is added | 
|  | 90 | adg.javaSrcjars = append(adg.javaSrcjars, provider.Srcjars...) | 
|  | 91 | case javaAconfigLibraryTag: | 
|  | 92 | adg.javaSrcjars = append(adg.javaSrcjars, provider.Srcjars...) | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | }) | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | func (adg *AconfigDeclarationsGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) { | 
|  | 99 | adg.VisitDeps(ctx) | 
|  | 100 | adg.aconfigDeclarationNames = android.FirstUniqueStrings(adg.aconfigDeclarationNames) | 
|  | 101 | adg.intermediateCacheOutputPaths = android.FirstUniquePaths(adg.intermediateCacheOutputPaths) | 
|  | 102 |  | 
|  | 103 | android.SetProvider(ctx, aconfig.CodegenInfoProvider, aconfig.CodegenInfo{ | 
|  | 104 | AconfigDeclarations:          adg.aconfigDeclarationNames, | 
|  | 105 | IntermediateCacheOutputPaths: adg.intermediateCacheOutputPaths, | 
|  | 106 | Srcjars:                      adg.javaSrcjars, | 
|  | 107 | }) | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | var _ android.OutputFileProducer = (*AconfigDeclarationsGroup)(nil) | 
|  | 111 |  | 
|  | 112 | func (adg *AconfigDeclarationsGroup) OutputFiles(tag string) (android.Paths, error) { | 
|  | 113 | switch tag { | 
|  | 114 | case "": | 
|  | 115 | return adg.intermediateCacheOutputPaths, nil | 
|  | 116 | case ".srcjars": | 
|  | 117 | return adg.javaSrcjars, nil | 
|  | 118 | default: | 
|  | 119 | return nil, fmt.Errorf("unsupported module reference tag %s", tag) | 
|  | 120 | } | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | func (adg *AconfigDeclarationsGroup) Srcjars() android.Paths { | 
|  | 124 | return adg.javaSrcjars | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | func (adg *AconfigDeclarationsGroup) AconfigDeclarations() []string { | 
|  | 128 | return adg.aconfigDeclarationNames | 
|  | 129 | } |