blob: 33c20cdb2fc9727d48ff816a8c2da0789162ddef [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 (
Yu Liu67a28422024-03-05 00:36:31 +000018 "maps"
19
20 "android/soong/android"
Jihoon Kang2a43e562024-02-12 19:05:12 +000021
22 "github.com/google/blueprint"
23)
24
25type dependencyTag struct {
26 blueprint.BaseDependencyTag
27 name string
28}
29
30var (
31 aconfigDeclarationsGroupTag = dependencyTag{name: "aconfigDeclarationsGroup"}
32 javaAconfigLibraryTag = dependencyTag{name: "javaAconfigLibrary"}
33 ccAconfigLibraryTag = dependencyTag{name: "ccAconfigLibrary"}
34 rustAconfigLibraryTag = dependencyTag{name: "rustAconfigLibrary"}
35)
36
37type AconfigDeclarationsGroup struct {
38 android.ModuleBase
39 android.DefaultableModuleBase
40
41 properties AconfigDeclarationsGroupProperties
42
43 aconfigDeclarationNames []string
44 intermediateCacheOutputPaths android.Paths
45 javaSrcjars android.Paths
Yu Liu67a28422024-03-05 00:36:31 +000046 modeInfos map[string]android.ModeInfo
Jihoon Kang2a43e562024-02-12 19:05:12 +000047}
48
49type AconfigDeclarationsGroupProperties struct {
50
51 // Name of the aconfig_declarations_group modules
52 Aconfig_declarations_groups []string
53
54 // Name of the java_aconfig_library modules
55 Java_aconfig_libraries []string
56
57 // Name of the cc_aconfig_library modules
58 Cc_aconfig_libraries []string
59
60 // Name of the rust_aconfig_library modules
61 Rust_aconfig_libraries []string
62}
63
64func AconfigDeclarationsGroupFactory() android.Module {
65 module := &AconfigDeclarationsGroup{}
66 module.AddProperties(&module.properties)
67 android.InitAndroidModule(module)
68 android.InitDefaultableModule(module)
69 return module
70}
71
72func (adg *AconfigDeclarationsGroup) DepsMutator(ctx android.BottomUpMutatorContext) {
73 ctx.AddDependency(ctx.Module(), aconfigDeclarationsGroupTag, adg.properties.Aconfig_declarations_groups...)
74 ctx.AddDependency(ctx.Module(), javaAconfigLibraryTag, adg.properties.Java_aconfig_libraries...)
75 ctx.AddDependency(ctx.Module(), ccAconfigLibraryTag, adg.properties.Cc_aconfig_libraries...)
76 ctx.AddDependency(ctx.Module(), rustAconfigLibraryTag, adg.properties.Rust_aconfig_libraries...)
77}
78
79func (adg *AconfigDeclarationsGroup) VisitDeps(ctx android.ModuleContext) {
Yu Liu67a28422024-03-05 00:36:31 +000080 adg.modeInfos = make(map[string]android.ModeInfo)
Jihoon Kang2a43e562024-02-12 19:05:12 +000081 ctx.VisitDirectDeps(func(dep android.Module) {
82 tag := ctx.OtherModuleDependencyTag(dep)
Yu Liu67a28422024-03-05 00:36:31 +000083 if provider, ok := android.OtherModuleProvider(ctx, dep, android.CodegenInfoProvider); ok {
Jihoon Kang2a43e562024-02-12 19:05:12 +000084
85 // aconfig declaration names and cache files are collected for all aconfig library dependencies
86 adg.aconfigDeclarationNames = append(adg.aconfigDeclarationNames, provider.AconfigDeclarations...)
87 adg.intermediateCacheOutputPaths = append(adg.intermediateCacheOutputPaths, provider.IntermediateCacheOutputPaths...)
88
89 switch tag {
90 case aconfigDeclarationsGroupTag:
91 // Will retrieve outputs from another language codegen modules when support is added
92 adg.javaSrcjars = append(adg.javaSrcjars, provider.Srcjars...)
Yu Liu67a28422024-03-05 00:36:31 +000093 maps.Copy(adg.modeInfos, provider.ModeInfos)
Jihoon Kang2a43e562024-02-12 19:05:12 +000094 case javaAconfigLibraryTag:
95 adg.javaSrcjars = append(adg.javaSrcjars, provider.Srcjars...)
Yu Liu67a28422024-03-05 00:36:31 +000096 maps.Copy(adg.modeInfos, provider.ModeInfos)
97 case ccAconfigLibraryTag:
98 maps.Copy(adg.modeInfos, provider.ModeInfos)
99 case rustAconfigLibraryTag:
100 maps.Copy(adg.modeInfos, provider.ModeInfos)
Jihoon Kang2a43e562024-02-12 19:05:12 +0000101 }
102 }
103 })
104}
105
106func (adg *AconfigDeclarationsGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
107 adg.VisitDeps(ctx)
108 adg.aconfigDeclarationNames = android.FirstUniqueStrings(adg.aconfigDeclarationNames)
109 adg.intermediateCacheOutputPaths = android.FirstUniquePaths(adg.intermediateCacheOutputPaths)
110
Yu Liu67a28422024-03-05 00:36:31 +0000111 android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{
Jihoon Kang2a43e562024-02-12 19:05:12 +0000112 AconfigDeclarations: adg.aconfigDeclarationNames,
113 IntermediateCacheOutputPaths: adg.intermediateCacheOutputPaths,
114 Srcjars: adg.javaSrcjars,
Yu Liu67a28422024-03-05 00:36:31 +0000115 ModeInfos: adg.modeInfos,
Jihoon Kang2a43e562024-02-12 19:05:12 +0000116 })
Jihoon Kang2a43e562024-02-12 19:05:12 +0000117
mrziwang937f35f2024-06-11 14:01:41 -0700118 ctx.SetOutputFiles(adg.intermediateCacheOutputPaths, "")
119 ctx.SetOutputFiles(adg.javaSrcjars, ".srcjars")
Jihoon Kang2a43e562024-02-12 19:05:12 +0000120}
121
122func (adg *AconfigDeclarationsGroup) Srcjars() android.Paths {
123 return adg.javaSrcjars
124}
125
126func (adg *AconfigDeclarationsGroup) AconfigDeclarations() []string {
127 return adg.aconfigDeclarationNames
128}