| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 18 | "github.com/google/blueprint/pathtools" | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 19 | "github.com/google/blueprint/proptools" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | "android/soong/android" | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 22 | "android/soong/bazel" | 
|  | 23 | ) | 
|  | 24 |  | 
|  | 25 | const ( | 
|  | 26 | protoTypeDefault = "lite" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 27 | ) | 
|  | 28 |  | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 29 | // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns | 
|  | 30 | // the paths to the generated files. | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 31 | func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) { | 
|  | 32 | var ccFile, headerFile android.ModuleGenPath | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | srcSuffix := ".cc" | 
|  | 35 | if flags.protoC { | 
|  | 36 | srcSuffix = ".c" | 
|  | 37 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 38 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 39 | if flags.proto.CanonicalPathFromRoot { | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 40 | ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 41 | headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h") | 
|  | 42 | } else { | 
|  | 43 | rel := protoFile.Rel() | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 44 | ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix)) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 45 | headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h")) | 
|  | 46 | } | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 47 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 48 | protoDeps := flags.proto.Deps | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 49 | if flags.protoOptionsFile { | 
|  | 50 | optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options") | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 51 | optionsPath := android.PathForSource(ctx, optionsFile) | 
|  | 52 | protoDeps = append(android.Paths{optionsPath}, protoDeps...) | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 53 | } | 
|  | 54 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 55 | outDir := flags.proto.Dir | 
|  | 56 | depFile := ccFile.ReplaceExtension(ctx, "d") | 
|  | 57 | outputs := android.WritablePaths{ccFile, headerFile} | 
|  | 58 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 59 | rule := android.NewRuleBuilder(pctx, ctx) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 60 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 61 | android.ProtoRule(rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs) | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 62 |  | 
| Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 63 | rule.Build("protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel()) | 
| Colin Cross | 6af17aa | 2017-09-20 12:59:05 -0700 | [diff] [blame] | 64 |  | 
|  | 65 | return ccFile, headerFile | 
|  | 66 | } | 
|  | 67 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 68 | func protoDeps(ctx DepsContext, deps Deps, p *android.ProtoProperties, static bool) Deps { | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 69 | var lib string | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 70 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 71 | if String(p.Proto.Plugin) == "" { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 72 | switch proptools.StringDefault(p.Proto.Type, protoTypeDefault) { | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 73 | case "full": | 
|  | 74 | if ctx.useSdk() { | 
|  | 75 | lib = "libprotobuf-cpp-full-ndk" | 
|  | 76 | static = true | 
|  | 77 | } else { | 
|  | 78 | lib = "libprotobuf-cpp-full" | 
|  | 79 | } | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 80 | case "lite": | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 81 | if ctx.useSdk() { | 
|  | 82 | lib = "libprotobuf-cpp-lite-ndk" | 
|  | 83 | static = true | 
|  | 84 | } else { | 
|  | 85 | lib = "libprotobuf-cpp-lite" | 
|  | 86 | } | 
|  | 87 | case "nanopb-c": | 
|  | 88 | lib = "libprotobuf-c-nano" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 89 | static = true | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 90 | case "nanopb-c-enable_malloc": | 
|  | 91 | lib = "libprotobuf-c-nano-enable_malloc" | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 92 | static = true | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 93 | case "nanopb-c-16bit": | 
|  | 94 | lib = "libprotobuf-c-nano-16bit" | 
|  | 95 | static = true | 
|  | 96 | case "nanopb-c-enable_malloc-16bit": | 
|  | 97 | lib = "libprotobuf-c-nano-enable_malloc-16bit" | 
|  | 98 | static = true | 
|  | 99 | case "nanopb-c-32bit": | 
|  | 100 | lib = "libprotobuf-c-nano-32bit" | 
|  | 101 | static = true | 
|  | 102 | case "nanopb-c-enable_malloc-32bit": | 
|  | 103 | lib = "libprotobuf-c-nano-enable_malloc-32bit" | 
|  | 104 | static = true | 
|  | 105 | default: | 
|  | 106 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 107 | String(p.Proto.Type)) | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 108 | } | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 109 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 110 | if static { | 
|  | 111 | deps.StaticLibs = append(deps.StaticLibs, lib) | 
|  | 112 | deps.ReexportStaticLibHeaders = append(deps.ReexportStaticLibHeaders, lib) | 
|  | 113 | } else { | 
|  | 114 | deps.SharedLibs = append(deps.SharedLibs, lib) | 
|  | 115 | deps.ReexportSharedLibHeaders = append(deps.ReexportSharedLibHeaders, lib) | 
|  | 116 | } | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
|  | 119 | return deps | 
|  | 120 | } | 
|  | 121 |  | 
| Colin Cross | 38f794e | 2017-09-07 10:53:07 -0700 | [diff] [blame] | 122 | func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 123 | flags.Local.CFlags = append(flags.Local.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI") | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 124 |  | 
| Colin Cross | 19878da | 2019-03-28 14:45:07 -0700 | [diff] [blame] | 125 | flags.proto = android.GetProtoFlags(ctx, p) | 
|  | 126 | if flags.proto.CanonicalPathFromRoot { | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 127 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.SubDir.String()) | 
| Dan Willemsen | ab9f426 | 2018-02-14 13:58:34 -0800 | [diff] [blame] | 128 | } | 
| Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 129 | flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+flags.proto.Dir.String()) | 
| Colin Cross | 5ff51b5 | 2017-05-02 13:34:32 -0700 | [diff] [blame] | 130 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 131 | if String(p.Proto.Plugin) == "" { | 
|  | 132 | var plugin string | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 133 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 134 | switch String(p.Proto.Type) { | 
|  | 135 | case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit": | 
|  | 136 | flags.protoC = true | 
|  | 137 | flags.protoOptionsFile = true | 
|  | 138 | flags.proto.OutTypeFlag = "--nanopb_out" | 
| Ulf Adams | 82fd89b | 2020-11-25 22:37:22 +0100 | [diff] [blame] | 139 | // Disable nanopb timestamps to support remote caching. | 
|  | 140 | flags.proto.OutParams = append(flags.proto.OutParams, "-T") | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 141 | plugin = "protoc-gen-nanopb" | 
|  | 142 | case "full": | 
|  | 143 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 144 | case "lite": | 
|  | 145 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 146 | flags.proto.OutParams = append(flags.proto.OutParams, "lite") | 
|  | 147 | case "": | 
|  | 148 | // TODO(b/119714316): this should be equivalent to "lite" in | 
|  | 149 | // order to match protoDeps, but some modules are depending on | 
|  | 150 | // this behavior | 
|  | 151 | flags.proto.OutTypeFlag = "--cpp_out" | 
|  | 152 | default: | 
|  | 153 | ctx.PropertyErrorf("proto.type", "unknown proto type %q", | 
|  | 154 | String(p.Proto.Type)) | 
|  | 155 | } | 
| Dan Willemsen | 60e62f0 | 2018-11-16 21:05:32 -0800 | [diff] [blame] | 156 |  | 
| Colin Cross | fe17f6f | 2019-03-28 19:30:56 -0700 | [diff] [blame] | 157 | if plugin != "" { | 
|  | 158 | path := ctx.Config().HostToolPath(ctx, plugin) | 
|  | 159 | flags.proto.Deps = append(flags.proto.Deps, path) | 
|  | 160 | flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String()) | 
|  | 161 | } | 
| Joe Onorato | 09e94ab | 2017-11-18 18:23:14 -0800 | [diff] [blame] | 162 | } | 
|  | 163 |  | 
| Colin Cross | 0c461f1 | 2016-10-20 16:11:43 -0700 | [diff] [blame] | 164 | return flags | 
|  | 165 | } | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 166 |  | 
|  | 167 | type protoAttributes struct { | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 168 | Deps bazel.LabelListAttribute | 
|  | 169 |  | 
|  | 170 | // A list of proto_library targets that that the proto_library in `deps` depends on | 
|  | 171 | // This list is overestimation. | 
|  | 172 | // Overestimation is necessary since Soong includes other protos via proto.include_dirs and not | 
|  | 173 | // a specific .proto file module explicitly. | 
|  | 174 | Transitive_deps bazel.LabelListAttribute | 
|  | 175 |  | 
|  | 176 | // A list of cc_library_* targets that the generated cpp code depends on | 
|  | 177 | Cc_deps bazel.LabelListAttribute | 
|  | 178 |  | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 179 | Min_sdk_version *string | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
|  | 182 | type bp2buildProtoDeps struct { | 
|  | 183 | wholeStaticLib               *bazel.LabelAttribute | 
|  | 184 | implementationWholeStaticLib *bazel.LabelAttribute | 
|  | 185 | protoDep                     *bazel.LabelAttribute | 
|  | 186 | } | 
|  | 187 |  | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 188 | func bp2buildProto(ctx android.Bp2buildMutatorContext, m *Module, protoSrcs bazel.LabelListAttribute, la linkerAttributes) bp2buildProtoDeps { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 189 | var ret bp2buildProtoDeps | 
|  | 190 |  | 
| Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 191 | protoInfo, ok := android.Bp2buildProtoProperties(ctx, &m.ModuleBase, protoSrcs) | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 192 | if !ok || protoInfo.Proto_libs.IsEmpty() { | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 193 | return ret | 
|  | 194 | } | 
|  | 195 |  | 
|  | 196 | var depName string | 
|  | 197 | typ := proptools.StringDefault(protoInfo.Type, protoTypeDefault) | 
|  | 198 | var rule_class string | 
|  | 199 | suffix := "_cc_proto" | 
|  | 200 | switch typ { | 
|  | 201 | case "lite": | 
|  | 202 | suffix += "_lite" | 
|  | 203 | rule_class = "cc_lite_proto_library" | 
|  | 204 | depName = "libprotobuf-cpp-lite" | 
|  | 205 | case "full": | 
|  | 206 | rule_class = "cc_proto_library" | 
|  | 207 | depName = "libprotobuf-cpp-full" | 
|  | 208 | default: | 
|  | 209 | ctx.PropertyErrorf("proto.type", "cannot handle conversion at this time: %q", typ) | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | dep := android.BazelLabelForModuleDepSingle(ctx, depName) | 
|  | 213 | ret.protoDep = &bazel.LabelAttribute{Value: &dep} | 
|  | 214 |  | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 215 | var protoAttrs protoAttributes | 
| Yu Liu | 2aa806b | 2022-09-01 11:54:47 -0700 | [diff] [blame] | 216 | protoAttrs.Deps.SetValue(protoInfo.Proto_libs) | 
| Spandan Das | ec39d51 | 2023-08-15 22:08:18 +0000 | [diff] [blame] | 217 | protoAttrs.Transitive_deps.SetValue(protoInfo.Transitive_proto_libs) | 
|  | 218 |  | 
|  | 219 | // Add the implementation deps of the top-level cc_library_static | 
|  | 220 | // This is necessary to compile the internal root of cc_proto_library. | 
|  | 221 | // Without this, clang might not be able to find .h files that the generated cpp files depends on | 
|  | 222 | protoAttrs.Cc_deps = *la.implementationDeps.Clone() | 
|  | 223 | protoAttrs.Cc_deps.Append(la.implementationDynamicDeps) | 
|  | 224 | protoAttrs.Cc_deps.Append(la.implementationWholeArchiveDeps) | 
|  | 225 | protoAttrs.Cc_deps.Append(la.wholeArchiveDeps) | 
|  | 226 | // Subtract myself to prevent possible circular dep | 
|  | 227 | protoAttrs.Cc_deps = bazel.SubtractBazelLabelListAttribute( | 
|  | 228 | protoAttrs.Cc_deps, | 
|  | 229 | bazel.MakeLabelListAttribute( | 
|  | 230 | bazel.MakeLabelList([]bazel.Label{ | 
|  | 231 | bazel.Label{Label: ":" + m.Name() + suffix}, | 
|  | 232 | }), | 
|  | 233 | ), | 
|  | 234 | ) | 
|  | 235 | // Subtract the protobuf libraries since cc_proto_library implicitly adds them | 
|  | 236 | protoAttrs.Cc_deps = bazel.SubtractBazelLabelListAttribute( | 
|  | 237 | protoAttrs.Cc_deps, | 
|  | 238 | bazel.MakeLabelListAttribute( | 
|  | 239 | bazel.MakeLabelList([]bazel.Label{ | 
|  | 240 | bazel.Label{Label: "//external/protobuf:libprotobuf-cpp-full", OriginalModuleName: "libprotobuf-cpp-full"}, | 
|  | 241 | bazel.Label{Label: "//external/protobuf:libprotobuf-cpp-lite", OriginalModuleName: "libprotobuf-cpp-lite"}, | 
|  | 242 | }), | 
|  | 243 | ), | 
|  | 244 | ) | 
|  | 245 |  | 
| Sam Delmerico | eddd3c0 | 2022-12-02 17:31:58 -0500 | [diff] [blame] | 246 | protoAttrs.Min_sdk_version = m.Properties.Min_sdk_version | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 247 |  | 
|  | 248 | name := m.Name() + suffix | 
| Chris Parsons | 6666d0f | 2023-09-22 16:21:53 +0000 | [diff] [blame] | 249 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, m) | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 250 | ctx.CreateBazelTargetModule( | 
|  | 251 | bazel.BazelTargetModuleProperties{ | 
|  | 252 | Rule_class:        rule_class, | 
| Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 253 | Bzl_load_location: "//build/bazel/rules/cc:cc_proto.bzl", | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 254 | }, | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 255 | android.CommonAttributes{Name: name, Tags: tags}, | 
| Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 256 | &protoAttrs) | 
|  | 257 |  | 
|  | 258 | var privateHdrs bool | 
|  | 259 | if lib, ok := m.linker.(*libraryDecorator); ok { | 
|  | 260 | privateHdrs = !proptools.Bool(lib.Properties.Proto.Export_proto_headers) | 
|  | 261 | } | 
|  | 262 |  | 
|  | 263 | labelAttr := &bazel.LabelAttribute{Value: &bazel.Label{Label: ":" + name}} | 
|  | 264 | if privateHdrs { | 
|  | 265 | ret.implementationWholeStaticLib = labelAttr | 
|  | 266 | } else { | 
|  | 267 | ret.wholeStaticLib = labelAttr | 
|  | 268 | } | 
|  | 269 |  | 
|  | 270 | return ret | 
|  | 271 | } |