22
33Learn how to generate stubs for gRPC Swift from a service defined using the Protocol Buffers IDL.
44
5- ## Overview
6-
7- There are two approaches to generating stubs from Protocol Buffers:
8-
9- 1 . With the Swift Package Manager build plugin, or
10- 2 . With the Protocol Buffers compiler (` protoc ` ).
11-
12- The following sections describe how and when to use each.
13-
14- ### Using the Swift Package Manager build plugin
15-
16- You can generate stubs at build time by using ` GRPCSwiftPlugin ` which is a build plugin for the
17- Swift Package Manager. Using it means that you don't have to manage the generation of
18- stubs with separate tooling, or check the generated stubs into your source repository.
19-
20- The build plugin will generate gRPC stubs for you by building ` protoc-gen-grpc-swift ` (more details
21- in the following section) for you and invoking ` protoc ` . Because of the implicit
22- dependency on ` protoc ` being made available by the system ` GRPCSwiftPlugin ` isn't suitable for use
23- in:
24-
25- - Library packages, or
26- - Environments where ` protoc ` isn't available.
27-
28- > ` GRPCSwiftPlugin ` _ only_ generates gRPC stubs, it doesn't generate messages. You must generate
29- > messages in addition to the gRPC Stubs. The [ Swift Protobuf] ( https://github.com/apple/swift-protobuf )
30- > project provides an equivalent build plugin, ` SwiftProtobufPlugin ` , for this.
31-
32- #### Configuring the build plugin
33-
34- You can configure which stubs ` GRPCSwiftPlugin ` generates and how via a configuration file. This
35- must be called ` grpc-swift-config.json ` and can be placed anywhere in the source directory for your
36- target.
37-
38- A config file for the plugin is made up of a number of ` protoc ` invocations. Each invocation
39- describes the inputs to ` protoc ` as well as any options.
40-
41- The following is a list of options which can be applied to each invocation object:
42- - ` protoFiles ` , an array of strings where each string is the path to an input ` .proto ` file
43- _ relative to ` grpc-swift-config.json ` _ .
44- - ` visibility ` , a string describing the access level of the generated stub (must be one
45- of ` "public" ` , ` "internal" ` , or ` "package" ` ). If not specified then stubs are generated as
46- ` internal ` .
47- - ` server ` , a boolean indicating whether server stubs should be generated. Defaults to ` true ` if
48- not specified.
49- - ` client ` , a boolean indicating whether client stubs should be generated. Defaults to ` true ` if
50- not specified.
51- - ` _V2 ` , a boolean indicated whether the generated stubs should be for v2.x. Defaults to ` false ` if
52- not specified.
53-
54- > The ` GRPCSwiftPlugin ` build plugin is currently shared between gRPC Swift v1.x and v2.x. To
55- > generate stubs for v2.x you _ must_ set ` _V2 ` to ` true ` in your config.
56- >
57- > This option will be deprecated and removed once v2.x has been released.
58-
59- #### Finding protoc
60-
61- The build plugin requires a copy of the ` protoc ` binary to be available. To resolve which copy of
62- the binary to use, ` GRPCSwiftPlugin ` will look at the following in order:
63-
64- 1 . The exact path specified in the ` protocPath ` property in ` grpc-swift-config.json ` , if present.
65- 2 . The exact path specified in the ` PROTOC_PATH ` environment variable, if set.
66- 3 . The first ` protoc ` binary found in your ` PATH ` environment variable.
67-
68- #### Using the build plugin from Xcode
69-
70- Xcode doesn't have access to your ` PATH ` so in order to use ` GRPCSwiftPlugin ` with Xcode you must
71- either set ` protocPath ` in your ` grpc-swift-config.json ` or explicitly set ` PROTOC_PATH ` when
72- opening Xcode.
73-
74- You can do this by running:
75-
76- ``` sh
77- env PROTOC_PATH=/path/to/protoc xed /path/to/your-project
78- ```
79-
80- Note that Xcode must _ not_ be open before running this command.
81-
82- #### Example configuration
83-
84- We recommend putting your config and ` .proto ` files in a directory called ` Protos ` within your
85- target. Here's an example package structure:
86-
87- ```
88- MyPackage
89- ├── Package.swift
90- └── Sources
91- └── MyTarget
92- └── Protos
93- ├── foo
94- │ └── bar
95- │ ├── baz.proto
96- │ └── buzz.proto
97- └── grpc-swift-config.json
98- ```
99-
100- If you wanted the generated stubs from ` baz.proto ` to be ` public ` , and to only generate a client
101- for ` buzz.proto ` then the ` grpc-swift-config ` could look like this:
102-
103- ``` json
104- {
105- "invocations" : [
106- {
107- "_V2" : true ,
108- "protoFiles" : [" foo/bar/baz.proto" ],
109- "visibility" : " public"
110- },
111- {
112- "_V2" : true ,
113- "protoFiles" : [" foo/bar/buzz.proto" ],
114- "server" : false
115- }
116- ]
117- }
118- ```
119-
120- ### Using protoc
5+ ## Using protoc
1216
1227If you've used Protocol Buffers before then generating gRPC Swift stubs should be simple. If you're
1238unfamiliar with Protocol Buffers then you should get comfortable with the concepts before
1249continuing; the [ Protocol Buffers website] ( https://protobuf.dev/ ) is a great place to start.
12510
126- gRPC Swift provides ` protoc-gen- grpc-swift` , a program which is a plugin for the Protocol Buffers
127- compiler, ` protoc ` .
11+ The [ ` grpc-swift-protobuf ` ] ( https://github.com/ grpc/grpc -swift-protobuf ) package provides
12+ ` protoc-gen-grpc-swift ` , a program which is a plugin for the Protocol Buffers compiler, ` protoc ` .
12813
12914> ` protoc-gen-grpc-swift ` only generates gRPC stubs, it doesn't generate messages. You must use
13015> ` protoc-gen-swift ` to generate messages in addition to gRPC Stubs.
@@ -160,22 +45,16 @@ protoc \
16045 --grpc-swift_out=.
16146```
16247
163- #### Generator options
48+ ### Generator options
16449
16550| Name | Possible Values | Default | Description |
16651| ---------------------------| --------------------------------------------| ------------| ----------------------------------------------------------|
167- | ` _V2 ` | ` True ` , ` False ` | ` False ` | Whether stubs are generated for gRPC Swift v2.x |
16852| ` Visibility ` | ` Public ` , ` Package ` , ` Internal ` | ` Internal ` | Access level for generated stubs |
16953| ` Server ` | ` True ` , ` False ` | ` True ` | Generate server stubs |
17054| ` Client ` | ` True ` , ` False ` | ` True ` | Generate client stubs |
17155| ` FileNaming ` | ` FullPath ` , ` PathToUnderscore ` , ` DropPath ` | ` FullPath ` | How generated source files should be named. (See below.) |
17256| ` ProtoPathModuleMappings ` | | | Path to module map ` .asciipb ` file. (See below.) |
173- | ` AccessLevelOnImports ` | ` True ` , ` False ` | ` True ` | Whether imports should have explicit access levels. |
174-
175- > The ` protoc-gen-grpc-swift ` binary is currently shared between gRPC Swift v1.x and v2.x. To
176- > generate stubs for v2.x you _ must_ specify ` _V2=True ` .
177- >
178- > This option will be deprecated and removed once v2.x has been released.
57+ | ` UseAccessLevelOnImports ` | ` True ` , ` False ` | ` False ` | Whether imports should have explicit access levels. |
17958
18059The ` FileNaming ` option has three possible values, for an input of ` foo/bar/baz.proto ` the following
18160output file will be generated:
@@ -188,12 +67,12 @@ allows you to specify a mapping from `.proto` files to the Swift module they are
18867allows the code generator to add appropriate imports to your generated stubs. This is described in
18968more detail in the [ SwiftProtobuf documentation] ( https://github.com/apple/swift-protobuf/blob/main/Documentation/PLUGIN.md ) .
19069
191- #### Building the plugin
70+ ### Building the plugin
19271
19372> The version of ` protoc-gen-grpc-swift ` you use mustn't be newer than the version of
194- > the ` grpc-swift ` you're using.
73+ > the ` grpc-swift-protobuf ` you're using.
19574
196- If your package depends on ` grpc-swift ` then you can get a copy of ` protoc-gen-grpc-swift `
75+ If your package depends on ` grpc-swift-protobuf ` then you can get a copy of ` protoc-gen-grpc-swift `
19776by building it directly:
19877
19978``` console
0 commit comments