Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revise how modules imports are done to better support import public #1539

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ jobs:
- name: Test SPM plugin
working-directory: main
run: make test-spm-plugin PROTOC=../protobuf/cmake_build/protoc
- name: Compilation Tests
working-directory: main
run: make compile-tests PROTOC=../protobuf/cmake_build/protoc

sanitizer_testing:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ xcbaselines
/docs
/build
mined_words.txt
/CompileTests/MultiModule/.build
/CompileTests/MultiModule/.swiftpm
/*DescriptorTestData.bin
/Package.resolved

Expand Down
40 changes: 40 additions & 0 deletions CompileTests/MultiModule/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// swift-tools-version: 5.6

import PackageDescription

let package = Package(
name: "CompileTests",
dependencies: [
.package(name: "swift-protobuf", path: "../..")
],
targets: [
.testTarget(
name: "Test1",
dependencies: ["ImportsAPublicly"]
),
.testTarget(
name: "Test2",
dependencies: ["ImportsImportsAPublicly"]
),
.target(
name: "ModuleA",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
]
),
.target(
name: "ImportsAPublicly",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.target(name: "ModuleA"),
]
),
.target(
name: "ImportsImportsAPublicly",
dependencies: [
.product(name: "SwiftProtobuf", package: "swift-protobuf"),
.target(name: "ImportsAPublicly"),
]
),
]
)
7 changes: 7 additions & 0 deletions CompileTests/MultiModule/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# CompileTests/MultiModule

This is a test case that uses sources generated into multiple modules and
ensures the generated code compiles with the cross modules references.

This can't use the SwiftPM Plugin as that currently doesn't have support for the
module mappings.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// DO NOT EDIT.
// swift-format-ignore-file
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: Sources/ImportsAPublicly/imports_a_publicly.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/apple/swift-protobuf/

import Foundation
import SwiftProtobuf

// Use of 'import public' causes re-exports:
@_exported import enum ModuleA.E
@_exported import let ModuleA.Extensions_ext_str
@_exported import struct ModuleA.A

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
struct _3: SwiftProtobuf.ProtobufAPIVersion_3 {}
typealias Version = _3
}

public struct ImportsAPublicly: Sendable {
// SwiftProtobuf.Message conformance is added in an extension below. See the
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.

public var a: ModuleA.A {
get {return _a ?? ModuleA.A()}
set {_a = newValue}
}
/// Returns true if `a` has been explicitly set.
public var hasA: Bool {return self._a != nil}
/// Clears the value of `a`. Subsequent reads from it will return its default value.
public mutating func clearA() {self._a = nil}

public var e: ModuleA.E {
get {return _e ?? .unset}
set {_e = newValue}
}
/// Returns true if `e` has been explicitly set.
public var hasE: Bool {return self._e != nil}
/// Clears the value of `e`. Subsequent reads from it will return its default value.
public mutating func clearE() {self._e = nil}

public var unknownFields = SwiftProtobuf.UnknownStorage()

public init() {}

fileprivate var _a: ModuleA.A? = nil
fileprivate var _e: ModuleA.E? = nil
}

// MARK: - Code below here is support for the SwiftProtobuf runtime.

extension ImportsAPublicly: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
public static let protoMessageName: String = "ImportsAPublicly"
public static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
11: .same(proto: "a"),
12: .same(proto: "e"),
]

public var isInitialized: Bool {
if let v = self._a, !v.isInitialized {return false}
return true
}

public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
while let fieldNumber = try decoder.nextFieldNumber() {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every case branch when no optimizations are
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch fieldNumber {
case 11: try { try decoder.decodeSingularMessageField(value: &self._a) }()
case 12: try { try decoder.decodeSingularEnumField(value: &self._e) }()
default: break
}
}
}

public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every if/case branch local when no optimizations
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
// https://github.com/apple/swift-protobuf/issues/1182
try { if let v = self._a {
try visitor.visitSingularMessageField(value: v, fieldNumber: 11)
} }()
try { if let v = self._e {
try visitor.visitSingularEnumField(value: v, fieldNumber: 12)
} }()
try unknownFields.traverse(visitor: &visitor)
}

public static func ==(lhs: ImportsAPublicly, rhs: ImportsAPublicly) -> Bool {
if lhs._a != rhs._a {return false}
if lhs._e != rhs._e {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// DO NOT EDIT.
// swift-format-ignore-file
//
// Generated by the Swift generator plugin for the protocol buffer compiler.
// Source: Sources/ImportsImportsAPublicly/imports_imports_a_publicly.proto
//
// For information on using the generated types, please see the documentation:
// https://github.com/apple/swift-protobuf/

import Foundation
import SwiftProtobuf

// Use of 'import public' causes re-exports:
@_exported import enum ModuleA.E
@_exported import let ModuleA.Extensions_ext_str
@_exported import struct ImportsAPublicly.ImportsAPublicly
@_exported import struct ModuleA.A

// If the compiler emits an error on this type, it is because this file
// was generated by a version of the `protoc` Swift plug-in that is
// incompatible with the version of SwiftProtobuf to which you are linking.
// Please ensure that you are building against the same version of the API
// that was used to generate this file.
fileprivate struct _GeneratedWithProtocGenSwiftVersion: SwiftProtobuf.ProtobufAPIVersionCheck {
struct _3: SwiftProtobuf.ProtobufAPIVersion_3 {}
typealias Version = _3
}

public struct ImportsImportsAPublicly: Sendable {
// SwiftProtobuf.Message conformance is added in an extension below. See the
// `Message` and `Message+*Additions` files in the SwiftProtobuf library for
// methods supported on all messages.

public var a: ModuleA.A {
get {return _a ?? ModuleA.A()}
set {_a = newValue}
}
/// Returns true if `a` has been explicitly set.
public var hasA: Bool {return self._a != nil}
/// Clears the value of `a`. Subsequent reads from it will return its default value.
public mutating func clearA() {self._a = nil}

public var e: ModuleA.E {
get {return _e ?? .unset}
set {_e = newValue}
}
/// Returns true if `e` has been explicitly set.
public var hasE: Bool {return self._e != nil}
/// Clears the value of `e`. Subsequent reads from it will return its default value.
public mutating func clearE() {self._e = nil}

public var unknownFields = SwiftProtobuf.UnknownStorage()

public init() {}

fileprivate var _a: ModuleA.A? = nil
fileprivate var _e: ModuleA.E? = nil
}

// MARK: - Code below here is support for the SwiftProtobuf runtime.

extension ImportsImportsAPublicly: SwiftProtobuf.Message, SwiftProtobuf._MessageImplementationBase, SwiftProtobuf._ProtoNameProviding {
public static let protoMessageName: String = "ImportsImportsAPublicly"
public static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
21: .same(proto: "a"),
22: .same(proto: "e"),
]

public var isInitialized: Bool {
if let v = self._a, !v.isInitialized {return false}
return true
}

public mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
while let fieldNumber = try decoder.nextFieldNumber() {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every case branch when no optimizations are
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch fieldNumber {
case 21: try { try decoder.decodeSingularMessageField(value: &self._a) }()
case 22: try { try decoder.decodeSingularEnumField(value: &self._e) }()
default: break
}
}
}

public func traverse<V: SwiftProtobuf.Visitor>(visitor: inout V) throws {
// The use of inline closures is to circumvent an issue where the compiler
// allocates stack space for every if/case branch local when no optimizations
// are enabled. https://github.com/apple/swift-protobuf/issues/1034 and
// https://github.com/apple/swift-protobuf/issues/1182
try { if let v = self._a {
try visitor.visitSingularMessageField(value: v, fieldNumber: 21)
} }()
try { if let v = self._e {
try visitor.visitSingularEnumField(value: v, fieldNumber: 22)
} }()
try unknownFields.traverse(visitor: &visitor)
}

public static func ==(lhs: ImportsImportsAPublicly, rhs: ImportsImportsAPublicly) -> Bool {
if lhs._a != rhs._a {return false}
if lhs._e != rhs._e {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}
}
Loading