blob: ec86af7f98114d557723dcb0d71664ab35c21ca3 [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
15package aconfig
16
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
34type CcAconfigLibraryProperties struct {
35 // name of the aconfig_declarations module to generate a library for
36 Aconfig_declarations string
Dennis Shenc5e39f52023-09-14 18:52:49 +000037
38 // whether to generate test mode version of the library
39 Test *bool
Joe Onorato37f900c2023-07-18 16:58:16 -070040}
41
42type CcAconfigLibraryCallbacks struct {
43 properties *CcAconfigLibraryProperties
44
45 generatedDir android.WritablePath
46 headerDir android.WritablePath
47 generatedCpp android.WritablePath
48 generatedH android.WritablePath
49}
50
51func CcAconfigLibraryFactory() android.Module {
52 callbacks := &CcAconfigLibraryCallbacks{
53 properties: &CcAconfigLibraryProperties{},
54 }
55 return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks)
56}
57
58func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) {
59}
60
61func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} {
62 return []interface{}{this.properties}
63}
64
65func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc.Deps) cc.Deps {
66 // Add a dependency for the declarations module
67 declarations := this.properties.Aconfig_declarations
68 if len(declarations) == 0 {
69 ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required")
70 } else {
71 ctx.AddDependency(ctx.Module(), ccDeclarationsTag, declarations)
72 }
73
74 // Add a dependency for the aconfig flags base library
75 deps.SharedLibs = append(deps.SharedLibs, "server_configurable_flags")
76 // TODO: It'd be really nice if we could reexport this library and not make everyone do it.
77
78 return deps
79}
80
81func (this *CcAconfigLibraryCallbacks) GeneratorSources(ctx cc.ModuleContext) cc.GeneratedSource {
82 result := cc.GeneratedSource{}
83
84 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
85 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
86 if len(declarationsModules) != 1 {
87 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
88 }
89 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
90
91 // Figure out the generated file paths. This has to match aconfig's codegen_cpp.rs.
92 this.generatedDir = android.PathForModuleGen(ctx)
93
94 this.headerDir = android.PathForModuleGen(ctx, "include")
95 result.IncludeDirs = []android.Path{this.headerDir}
96 result.ReexportedDirs = []android.Path{this.headerDir}
97
98 basename := strings.ReplaceAll(declarations.Package, ".", "_")
99
100 this.generatedCpp = android.PathForModuleGen(ctx, basename+".cc")
101 result.Sources = []android.Path{this.generatedCpp}
102
103 this.generatedH = android.PathForModuleGen(ctx, "include", basename+".h")
104 result.Headers = []android.Path{this.generatedH}
105
106 return result
107}
108
109func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags {
110 return flags
111}
112
113func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) {
114 // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag
115 declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag)
116 if len(declarationsModules) != 1 {
117 panic(fmt.Errorf("Exactly one aconfig_declarations property required"))
118 }
119 declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData)
120
Dennis Shenc5e39f52023-09-14 18:52:49 +0000121 var mode string
122 if proptools.Bool(this.properties.Test) {
123 mode = "test"
124 } else {
125 mode = "production"
126 }
Joe Onorato37f900c2023-07-18 16:58:16 -0700127 ctx.Build(pctx, android.BuildParams{
128 Rule: cppRule,
129 Input: declarations.IntermediatePath,
130 Outputs: []android.WritablePath{
131 this.generatedCpp,
132 this.generatedH,
133 },
134 Description: "cc_aconfig_library",
135 Args: map[string]string{
136 "gendir": this.generatedDir.String(),
Dennis Shenc5e39f52023-09-14 18:52:49 +0000137 "mode": mode,
Joe Onorato37f900c2023-07-18 16:58:16 -0700138 },
139 })
140}