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 | |
| 15 | package aconfig |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | "android/soong/cc" |
| 20 | "github.com/google/blueprint" |
| 21 | |
| 22 | "fmt" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | type ccDeclarationsTagType struct { |
| 27 | blueprint.BaseDependencyTag |
| 28 | } |
| 29 | |
| 30 | var ccDeclarationsTag = ccDeclarationsTagType{} |
| 31 | |
| 32 | type CcAconfigLibraryProperties struct { |
| 33 | // name of the aconfig_declarations module to generate a library for |
| 34 | Aconfig_declarations string |
| 35 | } |
| 36 | |
| 37 | type CcAconfigLibraryCallbacks struct { |
| 38 | properties *CcAconfigLibraryProperties |
| 39 | |
| 40 | generatedDir android.WritablePath |
| 41 | headerDir android.WritablePath |
| 42 | generatedCpp android.WritablePath |
| 43 | generatedH android.WritablePath |
| 44 | } |
| 45 | |
| 46 | func CcAconfigLibraryFactory() android.Module { |
| 47 | callbacks := &CcAconfigLibraryCallbacks{ |
| 48 | properties: &CcAconfigLibraryProperties{}, |
| 49 | } |
| 50 | return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks) |
| 51 | } |
| 52 | |
| 53 | func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) { |
| 54 | } |
| 55 | |
| 56 | func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} { |
| 57 | return []interface{}{this.properties} |
| 58 | } |
| 59 | |
| 60 | func (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 | |
| 76 | func (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 | |
| 104 | func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags { |
| 105 | return flags |
| 106 | } |
| 107 | |
| 108 | func (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 | } |