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