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. |
Ludovic Barman | 19739bf | 2023-11-04 15:13:49 +0000 | [diff] [blame^] | 59 | // ludovicb@: DEPRECATED, to be removed |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame] | 60 | Use_protobuf3 *bool |
Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 61 | |
| 62 | // List of exported include paths containing proto files for dependent rust_protobuf modules. |
| 63 | Exported_include_dirs []string |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | type protobufDecorator struct { |
| 67 | *BaseSourceProvider |
| 68 | |
Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 69 | Properties ProtobufProperties |
| 70 | protoNames []string |
| 71 | additionalCrates []string |
| 72 | grpcNames []string |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 73 | |
| 74 | grpcProtoFlags android.ProtoFlags |
| 75 | protoFlags android.ProtoFlags |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | func (proto *protobufDecorator) GenerateSource(ctx ModuleContext, deps PathDeps) android.Path { |
| 79 | var protoFlags android.ProtoFlags |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 80 | var grpcProtoFlags android.ProtoFlags |
| 81 | var commonProtoFlags []string |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 82 | |
Ivan Lozano | 6a3d1e9 | 2020-11-02 15:06:26 -0500 | [diff] [blame] | 83 | outDir := android.PathForModuleOut(ctx) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 84 | protoFiles := android.PathsForModuleSrc(ctx, proto.Properties.Protos) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 85 | grpcFiles := android.PathsForModuleSrc(ctx, proto.Properties.Grpc_protos) |
Jeff Vander Stoep | c1490ec | 2023-04-24 11:28:25 +0200 | [diff] [blame] | 86 | |
Ludovic Barman | 19739bf | 2023-11-04 15:13:49 +0000 | [diff] [blame^] | 87 | protoPluginPath := ctx.Config().HostToolPath(ctx, "protoc-gen-rust") |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 88 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 89 | commonProtoFlags = append(commonProtoFlags, defaultProtobufFlags...) |
| 90 | commonProtoFlags = append(commonProtoFlags, proto.Properties.Proto_flags...) |
| 91 | commonProtoFlags = append(commonProtoFlags, "--plugin=protoc-gen-rust="+protoPluginPath.String()) |
| 92 | |
| 93 | if len(protoFiles) > 0 { |
| 94 | protoFlags.OutTypeFlag = "--rust_out" |
| 95 | protoFlags.Flags = append(protoFlags.Flags, commonProtoFlags...) |
| 96 | |
| 97 | protoFlags.Deps = append(protoFlags.Deps, protoPluginPath) |
| 98 | } |
| 99 | |
| 100 | if len(grpcFiles) > 0 { |
| 101 | grpcPath := ctx.Config().HostToolPath(ctx, "grpc_rust_plugin") |
| 102 | |
| 103 | grpcProtoFlags.OutTypeFlag = "--rust_out" |
| 104 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--grpc_out="+outDir.String()) |
| 105 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "--plugin=protoc-gen-grpc="+grpcPath.String()) |
| 106 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, commonProtoFlags...) |
| 107 | |
| 108 | grpcProtoFlags.Deps = append(grpcProtoFlags.Deps, grpcPath, protoPluginPath) |
| 109 | } |
| 110 | |
| 111 | if len(protoFiles) == 0 && len(grpcFiles) == 0 { |
| 112 | ctx.PropertyErrorf("protos", |
| 113 | "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] | 114 | } |
| 115 | |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 116 | // Add exported dependency include paths |
| 117 | for _, include := range deps.depIncludePaths { |
| 118 | protoFlags.Flags = append(protoFlags.Flags, "-I"+include.String()) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 119 | grpcProtoFlags.Flags = append(grpcProtoFlags.Flags, "-I"+include.String()) |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Chih-Hung Hsieh | c49649c | 2020-10-01 21:25:05 -0700 | [diff] [blame] | 122 | stem := proto.BaseSourceProvider.getStem(ctx) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 123 | |
| 124 | // The mod_stem.rs file is used to avoid collisions if this is not included as a crate. |
| 125 | stemFile := android.PathForModuleOut(ctx, "mod_"+stem+".rs") |
| 126 | |
| 127 | // 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] | 128 | var outputs android.WritablePaths |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 129 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 130 | rule := android.NewRuleBuilder(pctx, ctx) |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 131 | |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 132 | for _, protoFile := range protoFiles { |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 133 | // Since we're iterating over the protoFiles already, make sure they're not redeclared in grpcFiles |
| 134 | if android.InList(protoFile.String(), grpcFiles.Strings()) { |
| 135 | ctx.PropertyErrorf("protos", |
| 136 | "A proto can only be added once to either grpc_protos or protos. %q is declared in both properties", |
| 137 | protoFile.String()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 138 | } |
| 139 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 140 | protoName := strings.TrimSuffix(protoFile.Base(), ".proto") |
| 141 | proto.protoNames = append(proto.protoNames, protoName) |
| 142 | |
| 143 | protoOut := android.PathForModuleOut(ctx, protoName+".rs") |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 144 | depFile := android.PathForModuleOut(ctx, protoName+".d") |
| 145 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 146 | ruleOutputs := android.WritablePaths{protoOut, depFile} |
| 147 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 148 | android.ProtoRule(rule, protoFile, protoFlags, protoFlags.Deps, outDir, depFile, ruleOutputs) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 149 | outputs = append(outputs, ruleOutputs...) |
| 150 | } |
| 151 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 152 | for _, grpcFile := range grpcFiles { |
| 153 | grpcName := strings.TrimSuffix(grpcFile.Base(), ".proto") |
| 154 | proto.grpcNames = append(proto.grpcNames, grpcName) |
| 155 | |
| 156 | // GRPC protos produce two files, a proto.rs and a proto_grpc.rs |
| 157 | protoOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+".rs")) |
| 158 | grpcOut := android.WritablePath(android.PathForModuleOut(ctx, grpcName+grpcSuffix+".rs")) |
| 159 | depFile := android.PathForModuleOut(ctx, grpcName+".d") |
| 160 | |
| 161 | ruleOutputs := android.WritablePaths{protoOut, grpcOut, depFile} |
| 162 | |
| 163 | android.ProtoRule(rule, grpcFile, grpcProtoFlags, grpcProtoFlags.Deps, outDir, depFile, ruleOutputs) |
| 164 | outputs = append(outputs, ruleOutputs...) |
| 165 | } |
| 166 | |
| 167 | // Check that all proto base filenames are unique as outputs are written to the same directory. |
| 168 | baseFilenames := append(proto.protoNames, proto.grpcNames...) |
| 169 | if len(baseFilenames) != len(android.FirstUniqueStrings(baseFilenames)) { |
| 170 | ctx.PropertyErrorf("protos", "proto filenames must be unique across 'protos' and 'grpc_protos' "+ |
| 171 | "to be used in the same rust_protobuf module. For example, foo.proto and src/foo.proto will conflict.") |
| 172 | } |
| 173 | |
| 174 | android.WriteFileRule(ctx, stemFile, proto.genModFileContents()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 175 | |
Colin Cross | f1a035e | 2020-11-16 17:32:30 -0800 | [diff] [blame] | 176 | rule.Build("protoc_"+ctx.ModuleName(), "protoc "+ctx.ModuleName()) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 177 | |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 178 | // stemFile must be first here as the first path in BaseSourceProvider.OutputFiles is the library entry-point. |
| 179 | proto.BaseSourceProvider.OutputFiles = append(android.Paths{stemFile}, outputs.Paths()...) |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 180 | |
Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 181 | ctx.SetProvider(cc.FlagExporterInfoProvider, cc.FlagExporterInfo{ |
| 182 | IncludeDirs: android.PathsForModuleSrc(ctx, proto.Properties.Exported_include_dirs), |
| 183 | }) |
| 184 | |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 185 | // mod_stem.rs is the entry-point for our library modules, so this is what we return. |
| 186 | return stemFile |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 189 | func (proto *protobufDecorator) genModFileContents() string { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 190 | lines := []string{ |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 191 | "// @Soong generated Source", |
| 192 | } |
Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 193 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 194 | for _, protoName := range proto.protoNames { |
Ivan Lozano | 57f434e | 2020-10-28 09:32:10 -0400 | [diff] [blame] | 195 | lines = append(lines, fmt.Sprintf("pub mod %s;", protoName)) |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Ivan Lozano | d106efe | 2023-09-21 23:30:26 -0400 | [diff] [blame] | 198 | for _, crate := range proto.additionalCrates { |
| 199 | lines = append(lines, fmt.Sprintf("pub use %s::*;", crate)) |
| 200 | |
| 201 | } |
| 202 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 203 | for _, grpcName := range proto.grpcNames { |
| 204 | lines = append(lines, fmt.Sprintf("pub mod %s;", grpcName)) |
| 205 | lines = append(lines, fmt.Sprintf("pub mod %s%s;", grpcName, grpcSuffix)) |
| 206 | } |
| 207 | if len(proto.grpcNames) > 0 { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 208 | lines = append( |
| 209 | lines, |
| 210 | "pub mod empty {", |
Ludovic Barman | 19739bf | 2023-11-04 15:13:49 +0000 | [diff] [blame^] | 211 | " pub use protobuf::well_known_types::empty::Empty;", |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 212 | "}") |
| 213 | } |
| 214 | |
Ivan Lozano | 9d74a52 | 2020-12-01 09:25:22 -0500 | [diff] [blame] | 215 | return strings.Join(lines, "\n") |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 218 | func (proto *protobufDecorator) SourceProviderProps() []interface{} { |
| 219 | return append(proto.BaseSourceProvider.SourceProviderProps(), &proto.Properties) |
| 220 | } |
| 221 | |
| 222 | func (proto *protobufDecorator) SourceProviderDeps(ctx DepsContext, deps Deps) Deps { |
| 223 | deps = proto.BaseSourceProvider.SourceProviderDeps(ctx, deps) |
Ludovic Barman | 19739bf | 2023-11-04 15:13:49 +0000 | [diff] [blame^] | 224 | deps.Rustlibs = append(deps.Rustlibs, "libprotobuf") |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 225 | deps.HeaderLibs = append(deps.SharedLibs, proto.Properties.Header_libs...) |
| 226 | |
Ivan Lozano | 6eff900 | 2020-12-11 15:25:59 -0500 | [diff] [blame] | 227 | if len(proto.Properties.Grpc_protos) > 0 { |
Zach Johnson | 3df4e63 | 2020-11-06 11:56:27 -0800 | [diff] [blame] | 228 | deps.Rustlibs = append(deps.Rustlibs, "libgrpcio", "libfutures") |
| 229 | deps.HeaderLibs = append(deps.HeaderLibs, "libprotobuf-cpp-full") |
| 230 | } |
| 231 | |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 232 | return deps |
| 233 | } |
| 234 | |
| 235 | // rust_protobuf generates protobuf rust code from the provided proto file. This uses the protoc-gen-rust plugin for |
| 236 | // 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] | 237 | // 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] | 238 | // properties of other modules. |
| 239 | func RustProtobufFactory() android.Module { |
| 240 | module, _ := NewRustProtobuf(android.HostAndDeviceSupported) |
| 241 | return module.Init() |
| 242 | } |
| 243 | |
| 244 | // A host-only variant of rust_protobuf. Refer to rust_protobuf for more details. |
| 245 | func RustProtobufHostFactory() android.Module { |
| 246 | module, _ := NewRustProtobuf(android.HostSupported) |
| 247 | return module.Init() |
| 248 | } |
| 249 | |
| 250 | func NewRustProtobuf(hod android.HostOrDeviceSupported) (*Module, *protobufDecorator) { |
| 251 | protobuf := &protobufDecorator{ |
| 252 | BaseSourceProvider: NewSourceProvider(), |
| 253 | Properties: ProtobufProperties{}, |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Matthew Maurer | e94f3e7 | 2022-08-10 20:25:50 +0000 | [diff] [blame] | 256 | module := NewSourceProviderModule(hod, protobuf, false, false) |
Treehugger Robot | 588aae7 | 2020-08-21 10:01:58 +0000 | [diff] [blame] | 257 | |
| 258 | return module, protobuf |
| 259 | } |