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