Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 1 | // Copyright 2020 The Android Open Source Project |
| 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 rust |
| 16 | |
| 17 | import ( |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 18 | "fmt" |
| 19 | "strings" |
| 20 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 21 | "android/soong/android" |
| 22 | ) |
| 23 | |
| 24 | var ( |
| 25 | defaultProtobufFlags = []string{""} |
| 26 | ) |
| 27 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 28 | const ( |
| 29 | grpcSuffix = "_grpc" |
| 30 | ) |
| 31 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 32 | type PluginType int |
| 33 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 34 | func init() { |
| 35 | android.RegisterModuleType("rust_protobuf", RustProtobufFactory) |
| 36 | android.RegisterModuleType("rust_protobuf_host", RustProtobufHostFactory) |
| 37 | } |
| 38 | |
| 39 | var _ SourceProvider = (*protobufDecorator)(nil) |
| 40 | |
| 41 | type ProtobufProperties struct { |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 42 | // List of relative paths to proto files that will be used to generate the source. |
| 43 | // Either this or grpc_protos must be defined. |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 44 | Protos []string `android:"path,arch_variant"` |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 45 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 46 | // List of relative paths to GRPC-containing proto files that will be used to generate the source. |
| 47 | // Either this or protos must be defined. |
| 48 | Grpc_protos []string `android:"path,arch_variant"` |
| 49 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 50 | // List of additional flags to pass to aprotoc |
| 51 | Proto_flags []string `android:"arch_variant"` |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 52 | |
| 53 | // List of libraries which export include paths required for this module |
Ivan Lozano | 9b44383 | 2020-11-17 13:39:30 -0500 | [diff] [blame] | 54 | Header_libs []string `android:"arch_variant,variant_prepend"` |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 55 | |
| 56 | // Use protobuf version 3.x. This will be deleted once we migrate all current users |
| 57 | // of protobuf off of 2.x. |
| 58 | Use_protobuf3 *bool |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | type protobufDecorator struct { |
| 62 | *BaseSourceProvider |
| 63 | |
| 64 | Properties ProtobufProperties |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 65 | protoNames []string |
| 66 | grpcNames []string |
| 67 | |
| 68 | grpcProtoFlags android.ProtoFlags |
| 69 | protoFlags android.ProtoFlags |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 72 | func (proto *protobufDecorator) useProtobuf3() bool { |
| 73 | return Bool(proto.Properties.Use_protobuf3) |
| 74 | } |
| 75 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 76 | func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 77 | var protoFlags android.ProtoFlags |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 78 | var grpcProtoFlags android.ProtoFlags |
| 79 | var commonProtoFlags []string |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 80 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 81 | outDir := android.PathForModuleOut(ctx) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 82 | protoFiles := android.PathsForModuleSrc(ctx, proto.Properties.Protos) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 83 | grpcFiles := android.PathsForModuleSrc(ctx, proto.Properties.Grpc_protos) |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 84 | |
| 85 | // For now protobuf2 (the deprecated version) remains the default. This will change in the |
| 86 | // future as we update the various users. |
Jeff Vander Stoep | 91c0466 | 2023-03-22 15:09:00 +0100 | [diff] [blame] | 87 | protoPluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust-deprecated") |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 88 | if proto.useProtobuf3() == true { |
| 89 | protoPluginPath = ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
| 90 | } |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 91 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 92 | commonProtoFlags = append(commonProtoFlags, defaultProtobufFlags...) |
| 93 | commonProtoFlags = append(commonProtoFlags, proto.Properties.Proto_flags...) |
| 94 | commonProtoFlags = append(commonProtoFlags, "--plugin=protoc-gen-rust="+protoPluginPath.String()) |
| 95 | |
| 96 | if len(protoFiles) > 0 { |
| 97 | protoFlags.OutTypeFlag = "--rust_out" |
| 98 | protoFlags.Flags = append(protoFlags.Flags, commonProtoFlags...) |
| 99 | |
| 100 | protoFlags.Deps = append(protoFlags.Deps, protoPluginPath) |
| 101 | } |
| 102 | |
| 103 | if len(grpcFiles) > 0 { |
| 104 | grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin") |
| 105 | |
| 106 | grpcProtoFlags.OutTypeFlag = "--rust_out" |
| 107 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--grpc_out="+outDir.String()) |
| 108 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String()) |
| 109 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, commonProtoFlags...) |
| 110 | |
| 111 | grpcProtoFlags.Deps = append(grpcProtoFlags.Deps, grpcPath, protoPluginPath) |
| 112 | } |
| 113 | |
| 114 | if len(protoFiles) == 0 && len(grpcFiles) == 0 { |
| 115 | ctx.PropertyErrorf("protos", |
| 116 | "at least one protobuf must be defined in either protos or grpc_protos.") |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 119 | // Add exported dependency include paths |
| 120 | for _, include := range deps.depIncludePaths { |
| 121 | protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String()) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 122 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "-I"+include.String()) |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 125 | stem := proto.BaseSourceProvider.getStem(ctx) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 126 | |
| 127 | // The mod_stem.rs file is used to avoid collisions if this is not included as a crate. |
| 128 | stemFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs") |
| 129 | |
| 130 | // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point. |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 131 | var outputs android.WritablePaths |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 132 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 133 | rule := android.NewRuleBuilder(pctx, ctx) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 134 | |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 135 | for _, protoFile := range protoFiles { |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 136 | // Since we're iterating over the protoFiles already, make sure they're not redeclared in grpcFiles |
| 137 | if android.InList(protoFile.String(), grpcFiles.Strings()) { |
| 138 | ctx.PropertyErrorf("protos", |
| 139 | "A proto can only be added once to either grpc_protos or protos. %q is declared in both properties", |
| 140 | protoFile.String()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 143 | protoName := strings.TrimSuffix(protoFile.Base(), ".proto") |
| 144 | proto.protoNames = append(proto.protoNames, protoName) |
| 145 | |
| 146 | protoOut := android.PathForModuleOut(ctx, protoName+".rs") |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 147 | depFile := android.PathForModuleOut(ctx, protoName+".d") |
| 148 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 149 | ruleOutputs := android.WritablePaths{protoOut, depFile} |
| 150 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 151 | android.ProtoRule(rule, protoFile, protoFlags, protoFlags.Deps, outDir, depFile, ruleOutputs) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 152 | outputs = append(outputs, ruleOutputs...) |
| 153 | } |
| 154 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 155 | for _, grpcFile := range grpcFiles { |
| 156 | grpcName := strings.TrimSuffix(grpcFile.Base(), ".proto") |
| 157 | proto.grpcNames = append(proto.grpcNames, grpcName) |
| 158 | |
| 159 | // GRPC protos produce two files, a proto.rs and a proto_grpc.rs |
| 160 | protoOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+".rs")) |
| 161 | grpcOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+grpcSuffix+".rs")) |
| 162 | depFile := android.PathForModuleOut(ctx, grpcName+".d") |
| 163 | |
| 164 | ruleOutputs := android.WritablePaths{protoOut, grpcOut, depFile} |
| 165 | |
| 166 | android.ProtoRule(rule, grpcFile, grpcProtoFlags, grpcProtoFlags.Deps, outDir, depFile, ruleOutputs) |
| 167 | outputs = append(outputs, ruleOutputs...) |
| 168 | } |
| 169 | |
| 170 | // Check that all proto base filenames are unique as outputs are written to the same directory. |
| 171 | baseFilenames := append(proto.protoNames, proto.grpcNames...) |
| 172 | if len(baseFilenames) != len(android.FirstUniqueStrings(baseFilenames)) { |
| 173 | ctx.PropertyErrorf("protos", "proto filenames must be unique across 'protos' and 'grpc_protos' "+ |
| 174 | "to be used in the same rust_protobuf module. For example, foo.proto and src/foo.proto will conflict.") |
| 175 | } |
| 176 | |
| 177 | android.WriteFileRule(ctx, stemFile, proto.genModFileContents()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 178 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 179 | rule.Build("protoc_"+ctx.ModuleName(), "protoc "+ctx.ModuleName()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 180 | |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 181 | // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point. |
| 182 | proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 183 | |
| 184 | // mod_stem.rs is the entry-point for our library modules, so this is what we return. |
| 185 | return stemFile |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 188 | func (proto *protobufDecorator) genModFileContents() string { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 189 | lines := []string{ |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 190 | "// @Soong generated Source", |
| 191 | } |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 192 | for _, protoName := range proto.protoNames { |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 193 | lines = append(lines, fmt.Sprintf("pub mod %s;", protoName)) |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 196 | for _, grpcName := range proto.grpcNames { |
| 197 | lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName)) |
| 198 | lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix)) |
| 199 | } |
| 200 | if len(proto.grpcNames) > 0 { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 201 | lines = append( |
| 202 | lines, |
| 203 | "pub mod empty {", |
| 204 | " pub use protobuf::well_known_types::Empty;", |
David Duarte | b6be48d | 2022-01-04 08:51:21 +0000 | [diff] [blame] | 205 | "}", |
| 206 | "pub mod wrappers {", |
| 207 | " pub use protobuf::well_known_types::{", |
| 208 | " DoubleValue, FloatValue, Int64Value, UInt64Value, Int32Value, UInt32Value,", |
| 209 | " BoolValue, StringValue, BytesValue", |
| 210 | " };", |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 211 | "}") |
| 212 | } |
| 213 | |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 214 | return strings.Join(lines, "\n") |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 217 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 218 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 219 | } |
| 220 | |
| 221 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 222 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 223 | useProtobuf3 := proto.useProtobuf3() |
| 224 | if useProtobuf3 == true { |
| 225 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
| 226 | } else { |
| 227 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf_deprecated") |
| 228 | } |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 229 | deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...) |
| 230 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 231 | if len(proto.Properties.Grpc_protos) > 0 { |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame^] | 232 | if useProtobuf3 == true { |
| 233 | ctx.PropertyErrorf("protos", "rust_protobuf with grpc_protos defined must currently use "+ |
| 234 | "`use_protobuf3: false,` in the Android.bp file. This is temporary until the "+ |
| 235 | "grpcio crate is updated to use the current version of the protobuf crate.") |
| 236 | } |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 237 | deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures") |
| 238 | deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full") |
| 239 | } |
| 240 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 241 | return deps |
| 242 | } |
| 243 | |
| 244 | // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for |
| 245 | // protoc. Additional flags to the protoc command can be passed via the proto_flags property. This module type will |
| 246 | // create library variants that can be used as a crate dependency by adding it to the rlibs, dylibs, and rustlibs |
| 247 | // properties of other modules. |
| 248 | func RustProtobufFactory() android.Module { |
| 249 | module, _ := NewRustProtobuf(android.HostAndDeviceSupported) |
| 250 | return module.Init() |
| 251 | } |
| 252 | |
| 253 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 254 | func RustProtobufHostFactory() android.Module { |
| 255 | module, _ := NewRustProtobuf(android.HostSupported) |
| 256 | return module.Init() |
| 257 | } |
| 258 | |
| 259 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 260 | protobuf := &protobufDecorator{ |
| 261 | BaseSourceProvider: NewSourceProvider(), |
| 262 | Properties: ProtobufProperties{}, |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Matthew Maurer | e94f3e7 | 2022-08-10 20:25:50 +0000 | [diff] [blame] | 265 | module := NewSourceProviderModule(hod, protobuf, false, false) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 266 | |
| 267 | return module, protobuf |
| 268 | } |