|
| 1 | +# Code generation with protoc |
| 2 | + |
| 3 | +This article describes how to use the Swift Package Manager build plugin to |
| 4 | +generate gRPC Swift and Swift Protobuf code from your Protocol Buffers `.proto` files. |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +If you've used Protocol Buffers before then generating gRPC Swift stubs should be simple. If you're |
| 9 | +unfamiliar with Protocol Buffers then you should get comfortable with the concepts before |
| 10 | +continuing; the [Protocol Buffers website](https://protobuf.dev/) is a great place to start. |
| 11 | + |
| 12 | +If you haven't installed `protoc` yet refer to <doc:Installing-protoc> for |
| 13 | +instructions. |
| 14 | + |
| 15 | +## Using protoc |
| 16 | + |
| 17 | +The [`grpc-swift-protobuf`](https://github.com/grpc/grpc-swift-protobuf) package provides |
| 18 | +`protoc-gen-grpc-swift-2`, a program which is a plugin for the Protocol Buffers compiler, `protoc`. |
| 19 | +To generate gRPC stubs for your `.proto` files directly you must run the `protoc` command with |
| 20 | +the `--grpc-swift-2_out=<DIRECTORY>` option: |
| 21 | + |
| 22 | +```console |
| 23 | +protoc --grpc-swift-2_out=. my-service.proto |
| 24 | +``` |
| 25 | + |
| 26 | +> `protoc-gen-grpc-swift-2` only generates gRPC stubs, it doesn't generate messages. You must use |
| 27 | +> `protoc-gen-swift` to generate messages in addition to gRPC Stubs. |
| 28 | +
|
| 29 | +The presence of `--grpc-swift-2_out` tells `protoc` to use the `protoc-gen-grpc-swift-2` plugin. By |
| 30 | +default it'll look for the plugin in your `PATH`. You can also specify the path to the plugin |
| 31 | +explicitly: |
| 32 | + |
| 33 | +```console |
| 34 | +protoc --plugin=/path/to/protoc-gen-grpc-swift-2 --grpc-swift-2_out=. my-service.proto |
| 35 | +``` |
| 36 | + |
| 37 | +You can specify various options to `protoc-gen-grpc-swift-2` via `protoc` using |
| 38 | +the `--grpc-swift-2_opt` argument: |
| 39 | + |
| 40 | +```console |
| 41 | +protoc --grpc-swift-2_opt=<OPTION_NAME>=<OPTION_VALUE> --grpc-swift-2_out=. |
| 42 | +``` |
| 43 | + |
| 44 | +You can specify multiple options by repeating the `--grpc-swift-2_opt` argument: |
| 45 | + |
| 46 | +```console |
| 47 | +protoc \ |
| 48 | + --grpc-swift-2_opt=<OPTION_NAME1>=<OPTION_VALUE1> \ |
| 49 | + --grpc-swift-2_opt=<OPTION_NAME2>=<OPTION_VALUE2> \ |
| 50 | + --grpc-swift-2_out=. |
| 51 | +``` |
| 52 | + |
| 53 | +### Generator options |
| 54 | + |
| 55 | +| Name | Possible Values | Default | Description | |
| 56 | +|---------------------------|---------------------------------------------|-----------------|----------------------------------------------------------| |
| 57 | +| `Visibility` | `Public`, `Package`, `Internal` | `Internal` | Access level for generated stubs | |
| 58 | +| `Server` | `True`, `False` | `True` | Generate server stubs | |
| 59 | +| `Client` | `True`, `False` | `True` | Generate client stubs | |
| 60 | +| `FileNaming` | `FullPath`, `PathToUnderscores`, `DropPath` | `FullPath` | How generated source files should be named. † | |
| 61 | +| `ProtoPathModuleMappings` | | | Path to module map `.asciipb` file. ‡ | |
| 62 | +| `UseAccessLevelOnImports` | `True`, `False` | `False` | Whether imports should have explicit access levels. | |
| 63 | +| `GRPCModuleName` | | `GRPCCore` | The name of the `GRPCCore` module. | |
| 64 | +| `GRPCProtobufModuleName` | | `GRPCProtobuf` | The name of the `GRPCProtobuf` module. | |
| 65 | +| `SwiftProtobufModuleName` | | `SwiftProtobuf` | The name of the `SwiftProtobuf` module. | |
| 66 | +| `Availability` | String, in the form `OS Version` | | Platform availability to use in generated code. § | |
| 67 | + |
| 68 | +† The `FileNaming` option has three possible values, for an input of `foo/bar/baz.proto` the following |
| 69 | +output file will be generated: |
| 70 | +- `FullPath`: `foo/bar/baz.grpc.swift`. |
| 71 | +- `PathToUnderscores`: `foo_bar_baz.grpc.swift` |
| 72 | +- `DropPath`: `baz.grpc.swift` |
| 73 | + |
| 74 | +‡ The code generator assumes all inputs are generated into the same module, `ProtoPathModuleMappings` |
| 75 | +allows you to specify a mapping from `.proto` files to the Swift module they are generated in. This |
| 76 | +allows the code generator to add appropriate imports to your generated stubs. This is described in |
| 77 | +more detail in the [SwiftProtobuf documentation](https://github.com/apple/swift-protobuf/blob/main/Documentation/PLUGIN.md). |
| 78 | + |
| 79 | +§ If unspecified the following availability is used: macOS 15, iOS 18, tvOS 18, |
| 80 | +watchOS 11, visionOS 2. The `Availability` option may be specified multiple |
| 81 | +times, where each value is a space delimited pair of platform and version, e.g. |
| 82 | +`Availability=macOS 15.0`. |
| 83 | + |
| 84 | +### Building the protoc plugin |
| 85 | + |
| 86 | +> The version of `protoc-gen-grpc-swift-2` you use mustn't be newer than the version of |
| 87 | +> the `grpc-swift-protobuf` you're using. |
| 88 | +
|
| 89 | +If your package depends on `grpc-swift-protobuf` then you can get a copy of `protoc-gen-grpc-swift-2` |
| 90 | +by building it directly: |
| 91 | + |
| 92 | +```console |
| 93 | +swift build --product protoc-gen-grpc-swift-2 |
| 94 | +``` |
| 95 | + |
| 96 | +This command will build the plugin into `.build/debug` directory. You can get the full path using |
| 97 | +`swift build --show-bin-path`. |
0 commit comments