blob: 80e492620c35296c339c5822119e9196b0178778 [file] [log] [blame]
Joe Onorato37f900c2023-07-18 16:58:16 -07001// 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
Yu Liueae7b362023-11-16 17:05:47 -080015package codegen
Joe Onorato37f900c2023-07-18 16:58:16 -070016
17import (
18 "android/soong/android"
19 "android/soong/cc"
Dennis Shenc5e39f52023-09-14 18:52:49 +000020
Joe Onorato37f900c2023-07-18 16:58:16 -070021 "github.com/google/blueprint"
Dennis Shenc5e39f52023-09-14 18:52:49 +000022 "github.com/google/blueprint/proptools"
Joe Onorato37f900c2023-07-18 16:58:16 -070023
24 "fmt"
25 "strings"
26)
27
28type ccDeclarationsTagType struct {
29 blueprint.BaseDependencyTag
30}
31
32var ccDeclarationsTag = ccDeclarationsTagType{}
33
Yu Liu855cfc22023-09-14 15:10:03 -070034const baseLibDep = "server_configurable_flags"
35
Joe Onorato37f900c2023-07-18 16:58:16 -070036type CcAconfigLibraryProperties struct {
37 // name of the aconfig_declarations module to generate a library for
38 Aconfig_declarations string
Dennis Shenc5e39f52023-09-14 18:52:49 +000039
Zi Wang275f6542023-11-09 14:59:31 -080040 // default mode is "production", the other accepted modes are:
41 // "test": to generate test mode version of the library
42 // "exported": to generate exported mode version of the library
Zhi Dou70e21242023-12-20 23:14:34 +000043 // "force-read-only": to generate force-read-only mode version of the library
Zi Wang275f6542023-11-09 14:59:31 -080044 // an error will be thrown if the mode is not supported
45 Mode *string
Joe Onorato37f900c2023-07-18 16:58:16 -070046}
47
48type CcAconfigLibraryCallbacks struct {
49 properties *CcAconfigLibraryProperties
50
51 generatedDir android.WritablePath
52 headerDir android.WritablePath
53 generatedCpp android.WritablePath
54 generatedH android.WritablePath
55}
56
57func CcAconfigLibraryFactory() android.Module {
58 callbacks := &CcAconfigLibraryCallbacks{
59 properties: &CcAconfigLibraryProperties{},
60 }
61 return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks)
62}
63
64func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) {
65}
66
67func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} {
68 return []interface{}{this.properties}
69}
70
71func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc.Deps) cc.Deps {
72 // Add a dependency for the declarations module
73 declarations := this.properties.Aconfig_declarations
74 if len(declarations) == 0 {
75 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
76 } else {
77 ctx.AddDependency(ctx.Module(), ccDeclarationsTag, declarations)
78 }
79
Dennis Shenc6dc5512024-01-10 14:07:35 +000080 mode := proptools.StringDefault(this.properties.Mode, "production")
81
82 // Add a dependency for the aconfig flags base library if it is not forced read only
83 if mode != "force-read-only" {
84 deps.SharedLibs = append(deps.SharedLibs, baseLibDep)
85 }
Joe Onorato37f900c2023-07-18 16:58:16 -070086 // TODO: It'd be really nice if we could reexport this library and not make everyone do it.
87
88 return deps
89}
90
91func (this *CcAconfigLibraryCallbacks) GeneratorSources(ctx cc.ModuleContext) cc.GeneratedSource {
92 result := cc.GeneratedSource{}
93
94 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
95 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
96 if len(declarationsModules) != 1 {
97 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
98 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +000099 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato37f900c2023-07-18 16:58:16 -0700100
101 // Figure out the generated file paths. This has to match aconfig's codegen_cpp.rs.
102 this.generatedDir = android.PathForModuleGen(ctx)
103
104 this.headerDir = android.PathForModuleGen(ctx, "include")
105 result.IncludeDirs = []android.Path{this.headerDir}
106 result.ReexportedDirs = []android.Path{this.headerDir}
107
108 basename := strings.ReplaceAll(declarations.Package, ".", "_")
109
110 this.generatedCpp = android.PathForModuleGen(ctx, basename+".cc")
111 result.Sources = []android.Path{this.generatedCpp}
112
113 this.generatedH = android.PathForModuleGen(ctx, "include", basename+".h")
114 result.Headers = []android.Path{this.generatedH}
115
116 return result
117}
118
119func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags {
120 return flags
121}
122
123func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) {
124 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
125 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
126 if len(declarationsModules) != 1 {
127 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
128 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000129 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato37f900c2023-07-18 16:58:16 -0700130
Zi Wang275f6542023-11-09 14:59:31 -0800131 mode := proptools.StringDefault(this.properties.Mode, "production")
132 if !isModeSupported(mode) {
133 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
134 }
Zi Wangd72e2db2023-11-13 16:01:13 -0800135
Joe Onorato37f900c2023-07-18 16:58:16 -0700136 ctx.Build(pctx, android.BuildParams{
137 Rule: cppRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000138 Input: declarations.IntermediateCacheOutputPath,
Joe Onorato37f900c2023-07-18 16:58:16 -0700139 Outputs: []android.WritablePath{
140 this.generatedCpp,
141 this.generatedH,
142 },
143 Description: "cc_aconfig_library",
144 Args: map[string]string{
145 "gendir": this.generatedDir.String(),
Dennis Shenc5e39f52023-09-14 18:52:49 +0000146 "mode": mode,
Joe Onorato37f900c2023-07-18 16:58:16 -0700147 },
148 })
Yu Liu67a28422024-03-05 00:36:31 +0000149
150 android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{
151 ModeInfos: map[string]android.ModeInfo{
152 ctx.ModuleName(): {
153 Container: declarations.Container,
154 Mode: mode,
155 }},
156 })
Joe Onorato37f900c2023-07-18 16:58:16 -0700157}