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