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" |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 19 | "android/soong/bazel" |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 20 | "android/soong/cc" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 21 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 24 | |
| 25 | "fmt" |
| 26 | "strings" |
| 27 | ) |
| 28 | |
| 29 | type ccDeclarationsTagType struct { |
| 30 | blueprint.BaseDependencyTag |
| 31 | } |
| 32 | |
| 33 | var ccDeclarationsTag = ccDeclarationsTagType{} |
| 34 | |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 35 | const baseLibDep = "server_configurable_flags" |
| 36 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 37 | type CcAconfigLibraryProperties struct { |
| 38 | // name of the aconfig_declarations module to generate a library for |
| 39 | Aconfig_declarations string |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 40 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 41 | // default mode is "production", the other accepted modes are: |
| 42 | // "test": to generate test mode version of the library |
| 43 | // "exported": to generate exported mode version of the library |
| 44 | // an error will be thrown if the mode is not supported |
| 45 | Mode *string |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | type CcAconfigLibraryCallbacks struct { |
| 49 | properties *CcAconfigLibraryProperties |
| 50 | |
| 51 | generatedDir android.WritablePath |
| 52 | headerDir android.WritablePath |
| 53 | generatedCpp android.WritablePath |
| 54 | generatedH android.WritablePath |
| 55 | } |
| 56 | |
| 57 | func CcAconfigLibraryFactory() android.Module { |
| 58 | callbacks := &CcAconfigLibraryCallbacks{ |
| 59 | properties: &CcAconfigLibraryProperties{}, |
| 60 | } |
| 61 | return cc.GeneratedCcLibraryModuleFactory("cc_aconfig_library", callbacks) |
| 62 | } |
| 63 | |
| 64 | func (this *CcAconfigLibraryCallbacks) GeneratorInit(ctx cc.BaseModuleContext) { |
| 65 | } |
| 66 | |
| 67 | func (this *CcAconfigLibraryCallbacks) GeneratorProps() []interface{} { |
| 68 | return []interface{}{this.properties} |
| 69 | } |
| 70 | |
| 71 | func (this *CcAconfigLibraryCallbacks) GeneratorDeps(ctx cc.DepsContext, deps cc.Deps) cc.Deps { |
| 72 | // Add a dependency for the declarations module |
| 73 | declarations := this.properties.Aconfig_declarations |
| 74 | if len(declarations) == 0 { |
| 75 | ctx.PropertyErrorf("aconfig_declarations", "aconfig_declarations property required") |
| 76 | } else { |
| 77 | ctx.AddDependency(ctx.Module(), ccDeclarationsTag, declarations) |
| 78 | } |
| 79 | |
| 80 | // Add a dependency for the aconfig flags base library |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 81 | deps.SharedLibs = append(deps.SharedLibs, baseLibDep) |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 82 | // TODO: It'd be really nice if we could reexport this library and not make everyone do it. |
| 83 | |
| 84 | return deps |
| 85 | } |
| 86 | |
| 87 | func (this *CcAconfigLibraryCallbacks) GeneratorSources(ctx cc.ModuleContext) cc.GeneratedSource { |
| 88 | result := cc.GeneratedSource{} |
| 89 | |
| 90 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 91 | declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag) |
| 92 | if len(declarationsModules) != 1 { |
| 93 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 94 | } |
| 95 | declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData) |
| 96 | |
| 97 | // Figure out the generated file paths. This has to match aconfig's codegen_cpp.rs. |
| 98 | this.generatedDir = android.PathForModuleGen(ctx) |
| 99 | |
| 100 | this.headerDir = android.PathForModuleGen(ctx, "include") |
| 101 | result.IncludeDirs = []android.Path{this.headerDir} |
| 102 | result.ReexportedDirs = []android.Path{this.headerDir} |
| 103 | |
| 104 | basename := strings.ReplaceAll(declarations.Package, ".", "_") |
| 105 | |
| 106 | this.generatedCpp = android.PathForModuleGen(ctx, basename+".cc") |
| 107 | result.Sources = []android.Path{this.generatedCpp} |
| 108 | |
| 109 | this.generatedH = android.PathForModuleGen(ctx, "include", basename+".h") |
| 110 | result.Headers = []android.Path{this.generatedH} |
| 111 | |
| 112 | return result |
| 113 | } |
| 114 | |
| 115 | func (this *CcAconfigLibraryCallbacks) GeneratorFlags(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) cc.Flags { |
| 116 | return flags |
| 117 | } |
| 118 | |
| 119 | func (this *CcAconfigLibraryCallbacks) GeneratorBuildActions(ctx cc.ModuleContext, flags cc.Flags, deps cc.PathDeps) { |
| 120 | // Get the values that came from the global RELEASE_ACONFIG_VALUE_SETS flag |
| 121 | declarationsModules := ctx.GetDirectDepsWithTag(ccDeclarationsTag) |
| 122 | if len(declarationsModules) != 1 { |
| 123 | panic(fmt.Errorf("Exactly one aconfig_declarations property required")) |
| 124 | } |
| 125 | declarations := ctx.OtherModuleProvider(declarationsModules[0], declarationsProviderKey).(declarationsProviderData) |
| 126 | |
Zi Wang | 275f654 | 2023-11-09 14:59:31 -0800 | [diff] [blame] | 127 | mode := proptools.StringDefault(this.properties.Mode, "production") |
| 128 | if !isModeSupported(mode) { |
| 129 | ctx.PropertyErrorf("mode", "%q is not a supported mode", mode) |
| 130 | } |
Zi Wang | d72e2db | 2023-11-13 16:01:13 -0800 | [diff] [blame^] | 131 | |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 132 | ctx.Build(pctx, android.BuildParams{ |
| 133 | Rule: cppRule, |
| 134 | Input: declarations.IntermediatePath, |
| 135 | Outputs: []android.WritablePath{ |
| 136 | this.generatedCpp, |
| 137 | this.generatedH, |
| 138 | }, |
| 139 | Description: "cc_aconfig_library", |
| 140 | Args: map[string]string{ |
| 141 | "gendir": this.generatedDir.String(), |
Dennis Shen | c5e39f5 | 2023-09-14 18:52:49 +0000 | [diff] [blame] | 142 | "mode": mode, |
Joe Onorato | 37f900c | 2023-07-18 16:58:16 -0700 | [diff] [blame] | 143 | }, |
| 144 | }) |
| 145 | } |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 146 | |
| 147 | type bazelCcAconfigLibraryAttributes struct { |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame] | 148 | cc.SdkAttributes |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 149 | Aconfig_declarations bazel.LabelAttribute |
| 150 | Dynamic_deps bazel.LabelListAttribute |
| 151 | } |
| 152 | |
| 153 | // Convert the cc_aconfig_library module to bazel. |
| 154 | // |
| 155 | // This method is called from cc.ConvertWithBp2build to actually convert the |
| 156 | // cc_aconfig_library module. This is necessary since the factory method of this |
| 157 | // module type returns a cc library and the bp2build conversion is called on the |
| 158 | // cc library type. |
| 159 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame] | 160 | func (this *CcAconfigLibraryCallbacks) GeneratorBp2build(ctx android.Bp2buildMutatorContext, module *cc.Module) bool { |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 161 | if ctx.ModuleType() != "cc_aconfig_library" { |
| 162 | return false |
| 163 | } |
| 164 | |
| 165 | attrs := bazelCcAconfigLibraryAttributes{ |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame] | 166 | SdkAttributes: cc.Bp2BuildParseSdkAttributes(module), |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 167 | Aconfig_declarations: *bazel.MakeLabelAttribute(android.BazelLabelForModuleDepSingle(ctx, this.properties.Aconfig_declarations).Label), |
| 168 | Dynamic_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, []string{baseLibDep})), |
| 169 | } |
| 170 | props := bazel.BazelTargetModuleProperties{ |
| 171 | Rule_class: "cc_aconfig_library", |
| 172 | Bzl_load_location: "//build/bazel/rules/cc:cc_aconfig_library.bzl", |
| 173 | } |
| 174 | |
Yu Liu | f11b7c3 | 2023-10-20 19:15:51 +0000 | [diff] [blame] | 175 | ctx.CreateBazelTargetModule( |
| 176 | props, |
| 177 | android.CommonAttributes{ |
| 178 | Name: ctx.ModuleName(), |
| 179 | Tags: android.ApexAvailableTagsWithoutTestApexes(ctx, module), |
| 180 | }, |
| 181 | &attrs) |
Yu Liu | 855cfc2 | 2023-09-14 15:10:03 -0700 | [diff] [blame] | 182 | return true |
| 183 | } |