blob: f3e9599bcc570e2b3a24b8d232d8f24ba39ca868 [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"
Ted Bauer10fff942024-04-29 19:30:23 +000025 "strconv"
Joe Onorato37f900c2023-07-18 16:58:16 -070026 "strings"
27)
28
29type ccDeclarationsTagType struct {
30 blueprint.BaseDependencyTag
31}
32
33var ccDeclarationsTag = ccDeclarationsTagType{}
34
Yu Liu855cfc22023-09-14 15:10:03 -070035const baseLibDep = "server_configurable_flags"
36
Ted Bauerf0f18592024-04-23 18:25:26 +000037const libBaseDep = "libbase"
38const libLogDep = "liblog"
39const libAconfigStorageReadApiCcDep = "libaconfig_storage_read_api_cc"
Ted Bauerf0f18592024-04-23 18:25:26 +000040
Joe Onorato37f900c2023-07-18 16:58:16 -070041type CcAconfigLibraryProperties struct {
42 // name of the aconfig_declarations module to generate a library for
43 Aconfig_declarations string
Dennis Shenc5e39f52023-09-14 18:52:49 +000044
Zi Wang275f6542023-11-09 14:59:31 -080045 // default mode is "production", the other accepted modes are:
46 // "test": to generate test mode version of the library
47 // "exported": to generate exported mode version of the library
Zhi Dou70e21242023-12-20 23:14:34 +000048 // "force-read-only": to generate force-read-only mode version of the library
Zi Wang275f6542023-11-09 14:59:31 -080049 // an error will be thrown if the mode is not supported
50 Mode *string
Joe Onorato37f900c2023-07-18 16:58:16 -070051}
52
53type CcAconfigLibraryCallbacks struct {
54 properties *CcAconfigLibraryProperties
55
56 generatedDir android.WritablePath
57 headerDir android.WritablePath
58 generatedCpp android.WritablePath
59 generatedH android.WritablePath
60}
61
62func CcAconfigLibraryFactory() android.Module {
63 callbacks := &CcAconfigLibraryCallbacks{
64 properties: &CcAconfigLibraryProperties{},
65 }
66 return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks)
67}
68
69func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) {
70}
71
72func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} {
73 return []interface{}{this.properties}
74}
75
76func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc.Deps) cc.Deps {
77 // Add a dependency for the declarations module
78 declarations := this.properties.Aconfig_declarations
79 if len(declarations) == 0 {
80 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
81 } else {
82 ctx.AddDependency(ctx.Module(), ccDeclarationsTag, declarations)
83 }
84
Dennis Shenc6dc5512024-01-10 14:07:35 +000085 mode := proptools.StringDefault(this.properties.Mode, "production")
86
87 // Add a dependency for the aconfig flags base library if it is not forced read only
88 if mode != "force-read-only" {
89 deps.SharedLibs = append(deps.SharedLibs, baseLibDep)
Ted Bauerf0f18592024-04-23 18:25:26 +000090
Ted Bauerf0f18592024-04-23 18:25:26 +000091 deps.SharedLibs = append(deps.SharedLibs, libAconfigStorageReadApiCcDep)
Ted Bauer1e96f8c2024-04-25 19:50:01 +000092 deps.SharedLibs = append(deps.SharedLibs, libLogDep)
Ted Bauer6c69edf2024-04-29 14:56:55 +000093 deps.SharedLibs = append(deps.SharedLibs, libBaseDep)
Dennis Shenc6dc5512024-01-10 14:07:35 +000094 }
Joe Onorato37f900c2023-07-18 16:58:16 -070095 // TODO: It'd be really nice if we could reexport this library and not make everyone do it.
96
97 return deps
98}
99
100func (this *CcAconfigLibraryCallbacks) GeneratorSources(ctx cc.ModuleContext) cc.GeneratedSource {
101 result := cc.GeneratedSource{}
102
103 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
104 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
105 if len(declarationsModules) != 1 {
106 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
107 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000108 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato37f900c2023-07-18 16:58:16 -0700109
110 // Figure out the generated file paths. This has to match aconfig's codegen_cpp.rs.
111 this.generatedDir = android.PathForModuleGen(ctx)
112
113 this.headerDir = android.PathForModuleGen(ctx, "include")
114 result.IncludeDirs = []android.Path{this.headerDir}
115 result.ReexportedDirs = []android.Path{this.headerDir}
116
117 basename := strings.ReplaceAll(declarations.Package, ".", "_")
118
119 this.generatedCpp = android.PathForModuleGen(ctx, basename+".cc")
120 result.Sources = []android.Path{this.generatedCpp}
121
122 this.generatedH = android.PathForModuleGen(ctx, "include", basename+".h")
123 result.Headers = []android.Path{this.generatedH}
124
125 return result
126}
127
128func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags {
129 return flags
130}
131
132func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) {
133 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
134 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
135 if len(declarationsModules) != 1 {
136 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
137 }
LaMont Jonesaa005ae2023-12-19 19:01:57 +0000138 declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey)
Joe Onorato37f900c2023-07-18 16:58:16 -0700139
Zi Wang275f6542023-11-09 14:59:31 -0800140 mode := proptools.StringDefault(this.properties.Mode, "production")
141 if !isModeSupported(mode) {
142 ctx.PropertyErrorf("mode", "%q is not a supported mode", mode)
143 }
Zi Wangd72e2db2023-11-13 16:01:13 -0800144
Joe Onorato37f900c2023-07-18 16:58:16 -0700145 ctx.Build(pctx, android.BuildParams{
146 Rule: cppRule,
Jihoon Kangcca3e0c2023-11-29 19:35:29 +0000147 Input: declarations.IntermediateCacheOutputPath,
Joe Onorato37f900c2023-07-18 16:58:16 -0700148 Outputs: []android.WritablePath{
149 this.generatedCpp,
150 this.generatedH,
151 },
152 Description: "cc_aconfig_library",
153 Args: map[string]string{
154 "gendir": this.generatedDir.String(),
Dennis Shenc5e39f52023-09-14 18:52:49 +0000155 "mode": mode,
Ted Bauer10fff942024-04-29 19:30:23 +0000156 "debug": strconv.FormatBool(ctx.Config().ReleaseReadFromNewStorageCc()),
Joe Onorato37f900c2023-07-18 16:58:16 -0700157 },
158 })
Yu Liu67a28422024-03-05 00:36:31 +0000159
160 android.SetProvider(ctx, android.CodegenInfoProvider, android.CodegenInfo{
161 ModeInfos: map[string]android.ModeInfo{
162 ctx.ModuleName(): {
163 Container: declarations.Container,
164 Mode: mode,
165 }},
166 })
Joe Onorato37f900c2023-07-18 16:58:16 -0700167}