Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 1 | // 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 Liu | eae7b36 | 2023-11-16 17:05:47 -0800 | [diff] [blame] | 15 | package codegen |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 20 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 21 | "github.com/google/blueprint" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 22 | "github.com/google/blueprint/proptools" |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 23 | |
| 24 | "fmt" |
| 25 | "strings" |
| 26 | ) |
| 27 | |
| 28 | type ccDeclarationsTagType struct { |
| 29 | blueprint.BaseDependencyTag |
| 30 | } |
| 31 | |
| 32 | var ccDeclarationsTag = ccDeclarationsTagType{} |
| 33 | |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 34 | const baseLibDep = "server_configurable_flags" |
| 35 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 36 | type CcAconfigLibraryProperties struct { |
| 37 | // name of the aconfig_declarations module to generate a library for |
| 38 | Aconfig_declarations string |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 39 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 40 | // 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 |
| 43 | // an error will be thrown if the mode is not supported |
| 44 | Mode *string |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | type CcAconfigLibraryCallbacks struct { |
| 48 | properties *CcAconfigLibraryProperties |
| 49 | |
| 50 | generatedDir android.WritablePath |
| 51 | headerDir android.WritablePath |
| 52 | generatedCpp android.WritablePath |
| 53 | generatedH android.WritablePath |
| 54 | } |
| 55 | |
| 56 | func CcAconfigLibraryFactory() android.Module { |
| 57 | callbacks := &CcAconfigLibraryCallbacks{ |
| 58 | properties: &CcAconfigLibraryProperties{}, |
| 59 | } |
| 60 | return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks) |
| 61 | } |
| 62 | |
| 63 | func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) { |
| 64 | } |
| 65 | |
| 66 | func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} { |
| 67 | return []interface{}{this.properties} |
| 68 | } |
| 69 | |
| 70 | func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc.Deps) cc.Deps { |
| 71 | // Add a dependency for the declarations module |
| 72 | declarations := this.properties.Aconfig_declarations |
| 73 | if len(declarations) == 0 { |
| 74 | ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required") |
| 75 | } else { |
| 76 | ctx.AddDependency(ctx.Module(), ccDeclarationsTag, declarations) |
| 77 | } |
| 78 | |
| 79 | // Add a dependency for the aconfig flags base library |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 80 | deps.SharedLibs = append(deps.SharedLibs, baseLibDep) |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 81 | // TODO: It'd be really nice if we could reexport this library and not make everyone do it. |
| 82 | |
| 83 | return deps |
| 84 | } |
| 85 | |
| 86 | func (this *CcAconfigLibraryCallbacks) GeneratorSources(ctx cc.ModuleContext) cc.GeneratedSource { |
| 87 | result := cc.GeneratedSource{} |
| 88 | |
| 89 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 90 | declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag) |
| 91 | if len(declarationsModules) != 1 { |
| 92 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 93 | } |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame^] | 94 | declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 95 | |
| 96 | // Figure out the generated file paths. This has to match aconfig's codegen_cpp.rs. |
| 97 | this.generatedDir = android.PathForModuleGen(ctx) |
| 98 | |
| 99 | this.headerDir = android.PathForModuleGen(ctx, "include") |
| 100 | result.IncludeDirs = []android.Path{this.headerDir} |
| 101 | result.ReexportedDirs = []android.Path{this.headerDir} |
| 102 | |
| 103 | basename := strings.ReplaceAll(declarations.Package, ".", "_") |
| 104 | |
| 105 | this.generatedCpp = android.PathForModuleGen(ctx, basename+".cc") |
| 106 | result.Sources = []android.Path{this.generatedCpp} |
| 107 | |
| 108 | this.generatedH = android.PathForModuleGen(ctx, "include", basename+".h") |
| 109 | result.Headers = []android.Path{this.generatedH} |
| 110 | |
| 111 | return result |
| 112 | } |
| 113 | |
| 114 | func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags { |
| 115 | return flags |
| 116 | } |
| 117 | |
| 118 | func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) { |
| 119 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 120 | declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag) |
| 121 | if len(declarationsModules) != 1 { |
| 122 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 123 | } |
LaMont Jones | aa005ae | 2023-12-19 19:01:57 +0000 | [diff] [blame^] | 124 | declarations, _ := android.OtherModuleProvider(ctx, declarationsModules[0], android.AconfigDeclarationsProviderKey) |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 125 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 126 | mode := proptools.StringDefault(this.properties.Mode, "production") |
| 127 | if !isModeSupported(mode) { |
| 128 | ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) |
| 129 | } |
Zi Wang | d72e2db | 2023-11-13 16:01:13 -0800 | [diff] [blame] | 130 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 131 | ctx.Build(pctx, android.BuildParams{ |
| 132 | Rule: cppRule, |
Jihoon Kang | cca3e0c | 2023-11-29 19:35:29 +0000 | [diff] [blame] | 133 | Input: declarations.IntermediateCacheOutputPath, |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 134 | Outputs: []android.WritablePath{ |
| 135 | this.generatedCpp, |
| 136 | this.generatedH, |
| 137 | }, |
| 138 | Description: "cc_aconfig_library", |
| 139 | Args: map[string]string{ |
| 140 | "gendir": this.generatedDir.String(), |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 141 | "mode": mode, |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 142 | }, |
| 143 | }) |
| 144 | } |