Skip to content

Commit

Permalink
Exposes implementationOnlyImports option in SPM plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
gjcairo authored and thomasvl committed May 19, 2023
1 parent c69d459 commit 1fb8ee8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Plugins/SwiftProtobufPlugin/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct SwiftProtobufPlugin: BuildToolPlugin {
var visibility: Visibility?
/// The file naming strategy to use.
var fileNaming: FileNaming?
/// Whether internal imports should be annotated as `@_implementationOnly`.
var implementationOnlyImports: Bool?
}

/// The path to the `protoc` binary.
Expand Down Expand Up @@ -156,6 +158,10 @@ struct SwiftProtobufPlugin: BuildToolPlugin {
var inputFiles = [Path]()
var outputFiles = [Path]()

if let implementationOnlyImports = invocation.implementationOnlyImports {
protocArgs.append("--swift_opt=ImplementationOnlyImports=\(implementationOnlyImports)")
}

for var file in invocation.protoFiles {
// Append the file to the protoc args so that it is used for generating
protocArgs.append("\(file)")
Expand Down
24 changes: 24 additions & 0 deletions Sources/protoc-gen-swift/Docs.docc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,30 @@ mapping {
The `proto_file_path` values here should match the paths used in the proto file
`import` statements.


##### Generation Option: `ImplementationOnlyImports` - `@_implementationOnly`-annotated imports

By default, the code generator does not annotate any imports with `@_implementationOnly`.
However, in some scenarios, such as when distributing an `XCFramework`, imports
for types used only internally should be annotated as `@_implementationOnly` to
avoid exposing internal symbols to clients.
You can change this with the `ImplementationOnlyImports` option:

```
$ protoc --swift_opt=ImplementationOnlyImports=[value] --swift_out=. foo/bar/*.proto mumble/*.proto
```

The possible values for `ImplementationOnlyImports` are:

* `false` (default): The `@_implementationOnly` annotation will never be used.
* `true`: Imports of internal dependencies and any modules defined in the module
mappings will be annotated as `@_implementationOnly`.

**Important:** Modules cannot be imported as implementation-only if they're
exposed via public API, so even if `ImplementationOnlyImports` is set to `true`,
this will only work if the `Visibility` is set to `internal`.


### Building your project

After copying the `.pb.swift` files into your project, you will need
Expand Down
3 changes: 2 additions & 1 deletion Sources/protoc-gen-swift/Docs.docc/spm-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ to the root of your target's source folder. An example configuration file looks
"protoFiles": [
"Foo.proto",
],
"visibility": "internal"
"visibility": "internal",
"implementationOnlyImports": true
},
{
"protoFiles": [
Expand Down

0 comments on commit 1fb8ee8

Please sign in to comment.