blob: 1c91beeef68c8636995f2c885376aaf55d6ae407 [file] [log] [blame]
Jihoon Kang2a43e562024-02-12 19:05:12 +00001// 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
15package codegen
16
17import (
Jihoon Kang2a43e562024-02-12 19:05:12 +000018 "fmt"
Yu Liu67a28422024-03-05 00:36:31 +000019 "maps"
20
21 "android/soong/android"
Jihoon Kang2a43e562024-02-12 19:05:12 +000022
23 "github.com/google/blueprint"
24)
25
26type dependencyTag struct {
27 blueprint.BaseDependencyTag
28 name string
29}
30
31var (
32 aconfigDeclarationsGroupTag = dependencyTag{name: "aconfigDeclarationsGroup"}
33 javaAconfigLibraryTag = dependencyTag{name: "javaAconfigLibrary"}
34 ccAconfigLibraryTag = dependencyTag{name: "ccAconfigLibrary"}
35 rustAconfigLibraryTag = dependencyTag{name: "rustAconfigLibrary"}
36)
37
38type 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 Liu67a28422024-03-05 00:36:31 +000047 modeInfos map[string]android.ModeInfo
Jihoon Kang2a43e562024-02-12 19:05:12 +000048}
49
50type 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
65func AconfigDeclarationsGroupFactory() android.Module {
66 module := &AconfigDeclarationsGroup{}
67 module.AddProperties(&module.properties)
68 android.InitAndroidModule(module)
69 android.InitDefaultableModule(module)
70 return module
71}
72
73func (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
80func (adg *AconfigDeclarationsGroup) VisitDeps(ctx android.ModuleContext) {
Yu Liu67a28422024-03-05 00:36:31 +000081 adg.modeInfos = make(map[string]android.ModeInfo)
Jihoon Kang2a43e562024-02-12 19:05:12 +000082 ctx.VisitDirectDeps(func(dep android.Module) {
83 tag := ctx.OtherModuleDependencyTag(dep)
Yu Liu67a28422024-03-05 00:36:31 +000084 if provider, ok := android.OtherModuleProvider(ctx, dep, android.CodegenInfoProvider); ok {
Jihoon Kang2a43e562024-02-12 19:05:12 +000085
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 Liu67a28422024-03-05 00:36:31 +000094 maps.Copy(adg.modeInfos, provider.ModeInfos)
Jihoon Kang2a43e562024-02-12 19:05:12 +000095 case javaAconfigLibraryTag:
96 adg.javaSrcjars = append(adg.javaSrcjars, provider.Srcjars...)
Yu Liu67a28422024-03-05 00:36:31 +000097 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 Kang2a43e562024-02-12 19:05:12 +0000102 }
103 }
104 })
105}
106
107func (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 Liu67a28422024-03-05 00:36:31 +0000112 android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{
Jihoon Kang2a43e562024-02-12 19:05:12 +0000113 AconfigDeclarations: adg.aconfigDeclarationNames,
114 IntermediateCacheOutputPaths: adg.intermediateCacheOutputPaths,
115 Srcjars: adg.javaSrcjars,
Yu Liu67a28422024-03-05 00:36:31 +0000116 ModeInfos: adg.modeInfos,
Jihoon Kang2a43e562024-02-12 19:05:12 +0000117 })
118}
119
120var _ android.OutputFileProducer = (*AconfigDeclarationsGroup)(nil)
121
122func (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
133func (adg *AconfigDeclarationsGroup) Srcjars() android.Paths {
134 return adg.javaSrcjars
135}
136
137func (adg *AconfigDeclarationsGroup) AconfigDeclarations() []string {
138 return adg.aconfigDeclarationNames
139}