Skip to content
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
17 changes: 15 additions & 2 deletions .github/workflows/build-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ on:

jobs:
build-and-test:
runs-on: macOS-15
strategy:
matrix:
include:
- os: macOS-15
swift: '6.2'
- os: ubuntu-24.04
swift: '6.2'
runs-on: ${{ matrix.os }}

steps:
- uses: SwiftyLab/setup-swift@v1
with:
swift-version: '6.1'
swift-version: ${{ matrix.swift }}

- name: Install ZeroMQ dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libzmq3-dev libsodium-dev

- name: Checkout
uses: actions/checkout@v4
Expand Down
36 changes: 27 additions & 9 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription

#if os(Linux)
let zmqDependency: Target.Dependency = .target(name: "ZeroMQ")
let zmqTargets: [Target] = [
.systemLibrary(
name: "ZeroMQ",
pkgConfig: "libzmq",
providers: [
.apt(["libzmq3-dev", "libsodium-dev"])
]
)
]
#else
let zmqDependency: Target.Dependency = .target(name: "zmq")
let zmqTargets: [Target] = [
.binaryTarget(
name: "zmq",
url:
"https://github.com/a4z/libzmq-xcf/releases/download/v4.3.5-250103_1/libzmq.xcframework.zip",
checksum: "34bf6c91c7151bfd9e0bea70fdea3b375246520677e0b6aa9b36184315aa0ec9"
)
]
#endif

let package = Package(
name: "szq",
// other/older might be possible, but not tested
Expand All @@ -16,19 +39,14 @@ let package = Package(
name: "szq",
targets: ["szq"])
],
targets: [
.binaryTarget(
name: "zmq",
url:
"https://github.com/a4z/libzmq-xcf/releases/download/v4.3.5-250103_1/libzmq.xcframework.zip",
checksum: "34bf6c91c7151bfd9e0bea70fdea3b375246520677e0b6aa9b36184315aa0ec9"
),
targets: zmqTargets + [
.target(
name: "szq",
dependencies: ["zmq"],
dependencies: [zmqDependency],
cxxSettings: [],
linkerSettings: [
.linkedLibrary("c++")
.linkedLibrary("c++", .when(platforms: [.macOS, .iOS, .watchOS, .tvOS])),
.linkedLibrary("stdc++", .when(platforms: [.linux])),
]
),
.testTarget(
Expand Down
5 changes: 5 additions & 0 deletions Sources/ZeroMQ/module.modulemap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ZeroMQ [system] {
header "shim.h"
link "zmq"
export *
}
1 change: 1 addition & 0 deletions Sources/ZeroMQ/shim.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include <zmq.h>
28 changes: 20 additions & 8 deletions Sources/szq/ZmqStreamable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ extension String: ZmqStreamable {

public static func pack(value: String) throws -> zmq_msg_t {
var msg = zmq_msg_t()
let zrc = zmq_msg_init_size(&msg, value.utf8.count)
let count = value.utf8.count
let zrc = zmq_msg_init_size(&msg, count)
if zrc != 0 {
throw currentZmqError()
}
let data = zmq_msg_data(&msg)
_ = value.withCString { cStr in
memcpy(data, cStr, value.utf8.count)
if count > 0 {
guard let data = zmq_msg_data(&msg) else {
throw currentZmqError()
}
_ = value.withCString { cStr in
memcpy(data, cStr, count)
}
}
return msg
}
Expand All @@ -41,7 +46,7 @@ extension ZmqStreamable where Self: BitwiseCopyable {
return dataPtr.withMemoryRebound(to: Self.self, capacity: 1) { pointer in
return pointer.pointee
}
}
}

public static func pack(value: Self) throws -> zmq_msg_t {
var msg = zmq_msg_t()
Expand All @@ -50,9 +55,16 @@ extension ZmqStreamable where Self: BitwiseCopyable {
if zrc != 0 {
throw currentZmqError()
}
let data = zmq_msg_data(&msg)
_ = withUnsafeBytes(of: value) { bytes in
memcpy(data, bytes.baseAddress, size)
if size > 0 {
guard let data = zmq_msg_data(&msg) else {
throw currentZmqError()
}
withUnsafeBytes(of: value) { bytes in
guard let base = bytes.baseAddress else {
return
}
memcpy(data, base, size)
}
}
return msg
}
Expand Down