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