Skip to content

Commit 28cbe87

Browse files
flxoFelix Obenhuber
andauthored
nng-sys: upgrade to NNG 2.0.0-alpha.6 (#34)
* nng-sys: upgrade to NNG 2.0.0-alpha.6 BREAKING CHANGES: - nng_init() must be called before using any NNG functions - nng_close() renamed to nng_socket_close() - nng_send_aio()/nng_recv_aio() renamed to nng_socket_send()/nng_socket_recv() - Many functions now return nng_err enum instead of c_int - ZeroTier transport removed (NNG_AF_ZT, nng_sockaddr_zt, nng_zt_*) - nanomsg compatibility layer removed (compat feature and headers) - NNG_FLAG_ALLOC removed - Many option getter/setter functions removed or renamed - TLS must now be configured via nng_tls_config_* functions - URL structure is now opaque with accessor functions For the complete migration guide, see: https://github.com/nanomsg/nng/blob/master/docs/ref/migrate/nng1.md Changes: - Bump nng-sys version to 0.4.0+v2.0.0-alpha.6 - Bump nng-sys/nng submodule to v2.0.0-alpha.6 - Regenerate bindings for NNG 2.0 API - Simplify wrapper.h (now just nng.h and http.h) - Remove compat.h and supplemental.h - Update build.rs: remove compat feature handling, bump pkg-config to 2.0.0 - Add NNG_AF_ABSTRACT to nng_sockaddr_family conversion - Update README with NNG 2.0 warning, migration guide, and fixed examples - Update tests for NNG 2.0 API (nng_init, nng_socket_close, nng_err) Note: The `links = "nng"` attribute is temporarily commented out because anng and nng crates still depend on nng-sys 0.3.0 from crates.io. * Update CI to build NNG 2.0 from submodule for non-vendored tests - Build NNG from nng-sys/nng submodule instead of Debian packages (v1.x) - Update linux-sys 'system + bindgen' job to build NNG from source - Update linux-nng job to build NNG from source - Update macOS jobs to build NNG from submodule (consolidate to 2 configs) - Remove compat and supplemental test jobs (features don't exist in NNG 2.0) * Fix Windows MSVC build: always enable stats WORKAROUND: MSVC doesn't allow empty structs in C (error C2016), and NNG has a bug where nng/src/core/stats.h defines an empty nni_stat_item struct when NNG_ENABLE_STATS is OFF. Until this is fixed upstream, we must always enable stats when building with MSVC to avoid the empty struct compilation error. * Fix macOS CI: add library search path for /usr/local/lib On macOS, /usr/local/lib is not in the default library search path. When we install NNG to /usr/local, the linker can't find libnng unless we explicitly tell Rust where to look. Add RUSTFLAGS=-L native=/usr/local/lib to the macOS system nng job. * Refactor CI: extract NNG build script Create .github/scripts/build-nng.sh to handle OS-specific NNG build from the submodule. This reduces duplication between Linux and macOS jobs - the script auto-detects the OS and handles: - Install prefix (/usr on Linux, /usr/local on macOS) - Parallel job count (nproc vs sysctl -n hw.ncpu) - ldconfig on Linux only * rename install script * ci: build NNG from source in feature-powerset check Replace apt-installed libnng1/libnng-dev packages with building NNG from the nng-sys/nng submodule. This ensures the hack job tests against the exact NNG version tracked in the repository when checking all feature combinations. * fix comment * extend link comment * add comment wht nng v1 is used for testing the nng crate * ci: run cargo hack per-package to handle NNG v1/v2 split nng-sys v0.4 targets NNG v2, while nng and anng still depend on nng-sys v0.3 (NNG v1). Running feature-powerset on the entire workspace fails since these packages require incompatible NNG versions. This workaround runs cargo hack on each package individually: - nng-sys: Install NNG v2 from submodule, set NNG_DIR=/usr - nng/anng: Use vendored NNG v1 (builds from source, no NNG_DIR needed) * remove dbg! * improve version parsing * add link to upstream issue * add TODO for path dependencies * fix manifest formatting * rename build_nng to install_nng and remove false cases * remove "from submodule" comments * add CFLAGS * improve TODO comment for removing install_nng flag and action * remove support for abstract sockets from socket familiy conversion * update version of nng-sys to 0.4.0-v2pre.1+v2.0.0-alpha.6 * update tls link * revert version example * remove migration notes and just reference the release notes and the migration guide * remove invalid comment * fix readme * revert version change * add a bold warning about the use of alpha nng v2 * fix version in readme * add CFLAGS and RUSTFLAGS to v2 cargo hack run --------- Co-authored-by: Felix Obenhuber <felix.obenhuber@helsing.ai>
1 parent c399535 commit 28cbe87

File tree

16 files changed

+1010
-1278
lines changed

16 files changed

+1010
-1278
lines changed

.github/scripts/install-nng.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
# Build and install NNG from the nng-sys/nng submodule source.
3+
# This script handles OS-specific differences between Linux and macOS.
4+
set -ex
5+
6+
cd nng-sys/nng
7+
mkdir -p build && cd build
8+
9+
# OS-specific settings
10+
if [[ "$OSTYPE" == "darwin"* ]]; then
11+
PREFIX="/usr/local"
12+
JOBS=$(sysctl -n hw.ncpu)
13+
else
14+
PREFIX="/usr"
15+
JOBS=$(nproc)
16+
fi
17+
18+
cmake -DCMAKE_INSTALL_PREFIX="$PREFIX" -DBUILD_SHARED_LIBS=ON \
19+
-DNNG_TESTS=OFF -DNNG_TOOLS=OFF ..
20+
make -j"$JOBS"
21+
sudo make install
22+
23+
# Linux needs ldconfig to update the shared library cache
24+
if [[ "$OSTYPE" != "darwin"* ]]; then
25+
sudo ldconfig
26+
fi

.github/workflows/build-configurations.yml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,15 @@ jobs:
1818
include:
1919
- name: system + bindgen
2020
features: # no-default-features, so this won't vendor, and system implies bindgen
21-
# system nng, clang-dev for bindgen
22-
apt: libnng1 libnng-dev libclang-dev
21+
# clang and cmake for installing NNG, clang-dev for bindgen
22+
apt: clang cmake libclang-dev
23+
# until nng v2 is available via apt, we need to manually install nng
24+
# for the checks that expect nng to be installed in the system
25+
#
26+
# TODO(flxo): remove the manual installation of nng and replace with
27+
# a `apt install libnng2 libnng-dev` or similar, once the apt
28+
# packages are available. Same applies for `brew` and others.
29+
install_nng: true
2330
env: {}
2431
- name: vendored + bindgen
2532
features: --features vendored,bindgen
@@ -48,24 +55,16 @@ jobs:
4855
# link against mbedtls.
4956
apt: clang cmake libmbedtls-dev
5057
env: {}
51-
- name: vendored with compat (bindgen implied)
52-
features: --features compat
53-
# to generate compat, we need to run bindgen since bundled do not
54-
# include compat. bindgen feature is implied by compat.
55-
apt: libnng1 libnng-dev libclang-dev
56-
env: {}
57-
- name: vendored with supplemental (bindgen implied)
58-
features: --features supplemental
59-
# ditto for supplemental
60-
apt: libnng1 libnng-dev libclang-dev
61-
env: {}
6258
steps:
6359
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # tag=v6.0.1
6460
with:
6561
submodules: true
6662
- name: Install dependencies
6763
run: |
6864
sudo apt-get install -y ${{ matrix.apt }}
65+
- name: Build and install NNG
66+
if: matrix.install_nng
67+
run: .github/scripts/install-nng.sh
6968
- name: Install stable
7069
uses: dtolnay/rust-toolchain@f7ccc83f9ed1e5b9c81d8a67d7ad1a747e22a561 # branch=master
7170
with:
@@ -80,6 +79,7 @@ jobs:
8079
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # tag=v6.0.1
8180
with:
8281
submodules: true
82+
# the nng crate is not yet upgraded to nng v2. install nng v1.x (libnng1 and libnng-dev)
8383
- name: Install dependencies
8484
run: |
8585
sudo apt-get install -y libnng1 libnng-dev
@@ -119,16 +119,15 @@ jobs:
119119
fail-fast: false
120120
matrix:
121121
include:
122-
- name: brew-provided nng + CFLAGS
122+
- name: system nng
123123
features:
124-
brew: nng
124+
brew: cmake
125+
install_nng: true
125126
env:
126-
CFLAGS: -I/opt/homebrew/include
127-
RUSTFLAGS: -L native=/opt/homebrew/lib
128-
- name: brew-provided nng
129-
features:
130-
brew: nng
131-
env: {}
127+
# /usr/local/include is not a standard C compiler search path
128+
CFLAGS: -I/usr/local/include
129+
# /usr/local/lib is not in the default library search path on macOS
130+
RUSTFLAGS: -L native=/usr/local/lib
132131
- name: vendored
133132
features: --features vendored
134133
brew: cmake
@@ -140,6 +139,9 @@ jobs:
140139
- name: Install dependencies
141140
run: |
142141
brew install ${{ matrix.brew }}
142+
- name: Build and install NNG
143+
if: matrix.install_nng
144+
run: .github/scripts/install-nng.sh
143145
- name: Install stable
144146
uses: dtolnay/rust-toolchain@f7ccc83f9ed1e5b9c81d8a67d7ad1a747e22a561 # branch=master
145147
with:

.github/workflows/check.yml

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,35 @@ jobs:
120120
# since we're trying every combination here, we need to ensure we have
121121
# all the various tools installed already.
122122
- name: install tools
123-
run: sudo apt-get install -y libclang-dev libnng1 libnng-dev clang cmake ninja-build libmbedtls-dev
123+
run: sudo apt-get install -y libclang-dev clang cmake ninja-build libmbedtls-dev libnng1 libnng-dev
124+
# Run cargo hack per-package because nng-sys v0.4 targets NNG v2, while
125+
# nng and anng still depend on nng-sys v0.3 (NNG v1). Running feature-powerset
126+
# on the entire workspace fails since these require incompatible NNG versions.
127+
# The effect of the failure (when running `hack` on the workspace) is here:
128+
# https://github.com/nanomsg/nng-rs/actions/runs/20988550343/job/60328143824
129+
#
130+
# TODO: Remove this workaround once nng and anng are migrated to nng-sys v0.4/NNG v2
131+
# and revert to:
132+
# - name: cargo hack
133+
# run: cargo hack --feature-powerset check --skip source-update-bindings
134+
#
124135
# intentionally no target specifier; see https://github.com/jonhoo/rust-ci-conf/pull/4
125136
# --feature-powerset runs for every combination of features
126-
- name: cargo hack
127-
run: cargo hack --feature-powerset check --skip source-update-bindings
137+
- name: cargo hack nng
138+
run: cargo hack -p nng --feature-powerset check
139+
- name: cargo hack anng
140+
run: cargo hack -p anng --feature-powerset check
141+
- name: remove NNG v1
142+
run: sudo apt-get remove -y libnng1 libnng-dev
143+
- name: build and install NNG v2 for nng-sys
144+
run: .github/scripts/install-nng.sh
145+
- name: cargo hack nng-sys
146+
run: cargo hack -p nng-sys --feature-powerset check --skip source-update-bindings
147+
env:
148+
# add /usr/local/include to the C compiler search path for NNG headers
149+
CFLAGS: -I/usr/local/include
150+
# add /usr/local/lib to the linker search path for NNG libraries
151+
RUSTFLAGS: -L native=/usr/local/lib
128152
msrv:
129153
# check that we can build using the minimal rust version that is specified by this crate
130154
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 16 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

anng/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ lazy_static = { version = "1.5.0", optional = true }
2828

2929
[dependencies.nng-sys]
3030
version = "0.3.0"
31-
path = "../nng-sys"
31+
# TODO(flxo): use path dependency once anng is updated to use nng-sys v0.4
32+
# path = "../nng-sys"
3233
default-features = false
3334

3435
[features]

nng-sys/Cargo.toml

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nng-sys"
3-
version = "0.3.0+1.11.0"
3+
version = "0.4.0-v2pre.1+v2.0.0-alpha.6"
44
authors = [
55
"Nathan Kent <nate@nkent.net>",
66
"Jake W <jeikabu@gmail.com>",
@@ -15,7 +15,18 @@ repository = "https://github.com/nanomsg/nng-rs"
1515
readme = "README.md"
1616
edition = "2018"
1717
rust-version = "1.82.0" # unsafe extern blocks in generated bindings
18-
links = "nng"
18+
# TODO(flxo): temporary comment the `links` attribute because `anng` and `nng`
19+
# are not yet upgraded to nng-sys v0.4.0. cargo doesn't allow multiple crates in
20+
# the ws dependency tree that link the same lib:
21+
#
22+
# error: failed to select a version for `nng-sys`.
23+
# ... required by package `anng v0.1.3 (.../nng-rs/anng)`
24+
# versions that meet the requirements `^0.3.0` (locked to 0.3.0+1.11.0) are: 0.3.0+1.11.0
25+
#
26+
# package `nng-sys` links to the native library `nng`, but it conflicts with a previous package which links to `nng` as well:
27+
# package `nng-sys v0.4.0+v2.0.0-alpha.6 (.../nng-rs/nng-sys)`
28+
# Only one package in the dependency graph may specify the same links value. This helps ensure that only one copy of a native library is linked in the final binary. Try to adjust your dependencies so that only one package uses the `links = "nng"` value. For more information, see https://doc.rust-lang.org/cargo/reference/resolver.html#links.
29+
# links = "nng"
1930

2031
[features]
2132
default = ["vendored", "vendored-stats", "std"]
@@ -46,14 +57,6 @@ vendored-stats = []
4657
# Emits additional cargo directives for linking against mbedtls.
4758
tls = []
4859

49-
# nanomsg compatibility layer
50-
# NOTE: this changes (adds to) the bindings as well
51-
compat = ["bindgen"]
52-
53-
# supplemental utilities
54-
# NOTE: this changes (adds to) the bindings as well
55-
supplemental = ["bindgen"]
56-
5760
## Development features (for working with the source code)
5861
# Update `src/bindings.rs` with all features enabled
5962
source-update-bindings = ["bindgen"]

nng-sys/README.md

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ Rust FFI bindings to [NNG](https://github.com/nanomsg/nng):
66
[![crates.io](http://img.shields.io/crates/v/nng-sys.svg)](http://crates.io/crates/nng-sys)
77
![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)
88

9+
> [!WARNING]
10+
> This crate wraps [NNG v2](https://github.com/nanomsg/nng), which is still in alpha.
11+
> For [NNG v1 bindings](https://github.com/nanomsg/nng/tree/stable), see the [v1.xx branch](https://github.com/nanomsg/nng-rs/tree/v1.xx).
12+
913
## Usage
1014

1115
To use the latest version of this crate, add the following to your `Cargo.toml`:
1216

1317
```toml
1418
[dependencies]
15-
nng-sys = "0.3.0"
19+
nng-sys = "0.4.0-v2pre"
1620
```
1721

1822
If you need TLS support:
1923

2024
```toml
2125
[dependencies]
22-
nng-sys = { version = "0.3.0", features = ["tls"] }
26+
nng-sys = { version = "0.4.0-v2pre", features = ["tls"] }
2327
```
2428

25-
This will bundle [mbedTLS](https://tls.mbed.org/). See the [NNG TLS documentation](https://nng.nanomsg.org/man/v1.10.0/nng_tls.7.html) for details.
29+
This will bundle [mbedTLS](https://tls.mbed.org/). See the [NNG TLS documentation](https://github.com/nanomsg/nng/blob/main/docs/man/nng_tls.7.adoc) for details.
2630

2731
### Library Discovery
2832

@@ -82,7 +86,7 @@ This crate uses the format `<crate version>+<nng version>` following [Semantic V
8286
| Patch fix in crate bindings | `0.3.1+1.11.0` | Bug fixes, no API changes |
8387
| Update to newer NNG version | `0.3.2+1.12.0` | Compatible update to the NNG library |
8488
| Breaking change in bindings | `0.4.0+1.12.0` | Breaking API changes introduced by e.g. a `bindgen` upgrade (pre-1.0, minor acts as major) |
85-
| NNG major version update | `0.5.0+2.0.0` | A backwards-incompatible NNG release also results in a major version bump |
89+
| NNG major version update | `0.5.0+2.0.0` | A backwards-incompatible NNG release also results in a major version bump |
8690

8791
**Note:** Cargo ignores the `+<nng version>` build metadata suffix when resolving dependencies,
8892
so version `0.3.0+1.11.0` and `0.3.0+1.12.0` are considered equivalent by Cargo and cannot coexist.
@@ -92,16 +96,14 @@ This legacy format has been replaced to allow for proper semantic versioning of
9296

9397
## Features
9498

95-
| Feature | Default | Description |
96-
|---------|---------|-------------|
97-
| `vendored` || Build NNG from bundled sources using CMake |
98-
| `vendored-stats` || Enable NNG stats (`NNG_ENABLE_STATS`) when building vendored NNG |
99-
| `std` || Enable standard library support |
100-
| `static` | | Force static linking (default: static for vendored, dynamic for system) |
101-
| `bindgen` | | Generate bindings at build time (uses pre-generated by default) |
102-
| `tls` | | Enable TLS support (`NNG_ENABLE_TLS`, requires mbedTLS) |
103-
| `compat` | | Bindings for nanomsg-compatible API (implies `bindgen`) |
104-
| `supplemental` | | Bindings for supplemental utilities (implies `bindgen`) |
99+
| Feature | Default | Description |
100+
| ---------------- | ------- | ----------------------------------------------------------------------- |
101+
| `vendored` || Build NNG from bundled sources using CMake |
102+
| `vendored-stats` || Enable NNG stats (`NNG_ENABLE_STATS`) when building vendored NNG |
103+
| `std` || Enable standard library support |
104+
| `static` | | Force static linking (default: static for vendored, dynamic for system) |
105+
| `bindgen` | | Generate bindings at build time (uses pre-generated by default) |
106+
| `tls` | | Enable TLS support (`NNG_ENABLE_TLS`, requires mbedTLS) |
105107

106108
**Note:** Features that control NNG build options (`vendored-stats`, `tls`) only affect vendored builds.
107109
When using a system-provided NNG library, ensure it was built with the features you need.
@@ -110,13 +112,13 @@ When using a system-provided NNG library, ensure it was built with the features
110112

111113
### Quick Reference
112114

113-
| Variable | Purpose | Example |
114-
|----------|---------|---------|
115-
| `NNG_DIR` | Root NNG installation directory | `/usr/local` |
116-
| `NNG_LIB_DIR` + `NNG_INCLUDE_DIR` | Separate library and header paths | `/opt/nng/lib`, `/opt/nng/include` |
117-
| `NNG_STATIC` | Force static (`1`) or dynamic (`0`) linking | `1` |
118-
| `NNG_NO_VENDOR` | Prohibit vendored builds (error if system lib not found) | `1` |
119-
| `{TARGET}_NNG_*` | Target-specific override (cross-compilation) | `AARCH64_UNKNOWN_LINUX_GNU_NNG_DIR` |
115+
| Variable | Purpose | Example |
116+
| --------------------------------- | -------------------------------------------------------- | ----------------------------------- |
117+
| `NNG_DIR` | Root NNG installation directory | `/usr/local` |
118+
| `NNG_LIB_DIR` + `NNG_INCLUDE_DIR` | Separate library and header paths | `/opt/nng/lib`, `/opt/nng/include` |
119+
| `NNG_STATIC` | Force static (`1`) or dynamic (`0`) linking | `1` |
120+
| `NNG_NO_VENDOR` | Prohibit vendored builds (error if system lib not found) | `1` |
121+
| `{TARGET}_NNG_*` | Target-specific override (cross-compilation) | `AARCH64_UNKNOWN_LINUX_GNU_NNG_DIR` |
120122

121123
### Build Control
122124

@@ -147,6 +149,9 @@ use std::{ffi::CString, os::raw::c_char, ptr::null_mut};
147149

148150
fn example() {
149151
unsafe {
152+
// Initialize NNG (required in NNG 2.0)
153+
nng_init(null_mut());
154+
150155
let url = CString::new("inproc://nng_sys/tests/example").unwrap();
151156
let url = url.as_bytes_with_nul().as_ptr() as *const c_char;
152157

@@ -178,8 +183,15 @@ fn example() {
178183
// Can't do this because nng uses network order (big-endian)
179184
//assert_eq!(val, *(nng_msg_body(recv_msg) as *const u32));
180185

181-
nng_close(req_socket);
182-
nng_close(rep_socket);
186+
nng_socket_close(req_socket);
187+
nng_socket_close(rep_socket);
183188
}
184189
}
185190
```
191+
192+
## Migration from NNG 1.x
193+
194+
This version wraps **NNG 2.0.0-alpha.6**, which is a major breaking release from
195+
NNG 1.x. See the [NNG 2.0 release
196+
notes](https://github.com/nanomsg/nng/releases) and the [NNG 1.x Migration
197+
Guide](https://github.com/nanomsg/nng/blob/master/docs/ref/migrate/nng1.md) for details.

0 commit comments

Comments
 (0)