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