zeromq / czmq

High-level C binding for ØMQ
czmq.zeromq.org
Mozilla Public License 2.0
1.16k stars 523 forks source link

New binding language: Zig #2258

Open kassane opened 1 year ago

kassane commented 1 year ago

Currently the has only been tested on Linux (wsl2). The purpose to use version 0.11 because of initial support for zig pkg manager.

TODO: add libzmq module to build all deps, on single build.

Current test

https://github.com/zeromq/czmq/blob/6c3217e2393fac6c8bbee8e15dc3438849dbf87b/bindings/zig/src/czmq.zig#L15-L21

$> zig build test
Test [2/2] test.Hello 0MQ...
Hello, Curves!
All 2 tests passed.

Steps

Note: This contribution is not intended to rewrite or deconstruct the main project. It only seeks to expand the working tooling for better use and build alternative.

References

bluca commented 1 year ago

create a new class in https://github.com/zeromq/zproject please

kassane commented 1 year ago

create a new class in https://github.com/zeromq/zproject please

[WIP] 😅

Although I briefly used zeroMQ, I never used zproject. So, I'm making some tests to better adapt zig. In addition to binding, it will serve as alternative build-system.

kassane commented 1 year ago

Apparently the zig package manager needs to detect in the build.zig and zon dependencies and then manually generate the multihash of each dep (e.g. sha2-256).

The libzmq was detected and built, but czmq requires the libzmq headers (zmq.h) and was not included by the package manager, only the library.

Update: all files (libzmq) has extracted on user global cache (Linux e.g.: $HOME/.cache/zig) in p (package) folder.

# local cache (project folder)
$>  tree zig-cache/
zig-cache/
[...]
├── o
│   ├── 85e23b1f3f84cb9cce35d523ffcab725
│   │   └── dependencies.zig
│   ├── fcb53a609b902f91e32497a436feaa8e
│   │   ├── libzmq.so -> libzmq.so.4
│   │   ├── libzmq.so.4 -> libzmq.so.4.3.5
│   │   ├── libzmq.so.4.3.5
│   │   └── lld.id -> 9cb6dff55fdda61620c10687321419e0
│   ├── fd7a3ca3b826d6e15d5b1601c8057f2f
│   │   └── raw_decoder.o
│   └── fe4274462516bd2dd182c8d86ed329b3
│       └── mailbox_safe.o
[...]
90 directories, 197 files

Output:

$> /zig build
error(compilation): clang failed with stderr: In file included from /home/kassane/czmq/src/zgossip_msg.c:32:
In file included from /home/kassane/czmq/src/czmq_classes.h:35:
In file included from /home/kassane/czmq/src/../include/czmq.h:37:
In file included from /home/kassane/czmq/include/czmq_library.h:23:
/home/kassane/czmq/include/czmq_prelude.h:217:10: fatal error: 'zmq.h' file not found

error(compilation): clang failed with stderr: In file included from /home/kassane/czmq/src/zarmour.c:39:
In file included from /home/kassane/czmq/src/czmq_classes.h:35:
In file included from /home/kassane/czmq/src/../include/czmq.h:37:
In file included from /home/kassane/czmq/include/czmq_library.h:23:
/home/kassane/czmq/include/czmq_prelude.h:217:10: fatal error: 'zmq.h' file not found
kassane commented 1 year ago

Output:

$> /zig build
error(compilation): clang failed with stderr: In file included from /home/kassane/czmq/src/zgossip_msg.c:32:
In file included from /home/kassane/czmq/src/czmq_classes.h:35:
In file included from /home/kassane/czmq/src/../include/czmq.h:37:
In file included from /home/kassane/czmq/include/czmq_library.h:23:
/home/kassane/czmq/include/czmq_prelude.h:217:10: fatal error: 'zmq.h' file not found

error(compilation): clang failed with stderr: In file included from /home/kassane/czmq/src/zarmour.c:39:
In file included from /home/kassane/czmq/src/czmq_classes.h:35:
In file included from /home/kassane/czmq/src/../include/czmq.h:37:
In file included from /home/kassane/czmq/include/czmq_library.h:23:
/home/kassane/czmq/include/czmq_prelude.h:217:10: fatal error: 'zmq.h' file not found

Fixed: Missing .installHeadersDirectory on libzmq_dep. zig-czmq's local cache receives the built library and includes together.

Result

Size: 5.8M - libzig_czmq.so.4.2.2 - (includes libzmq.a)

Output:

$> tree zig-out/
zig-out/
├── include
│   ├── Makefile.am
│   ├── czmq.h
│   ├── czmq_library.h
│   ├── czmq_prelude.h
│   ├── test_zgossip.h
│   ├── zactor.h
│   ├── zargs.h
│   ├── zarmour.h
│   ├── zauth.h
│   ├── zbeacon.h
│   ├── zcert.h
│   ├── zcertstore.h
│   ├── zchunk.h
│   ├── zclock.h
│   ├── zconfig.h
│   ├── zdigest.h
│   ├── zdir.h
│   ├── zdir_patch.h
│   ├── zfile.h
│   ├── zframe.h
│   ├── zgossip.h
│   ├── zhash.h
│   ├── zhashx.h
│   ├── zhttp_client.h
│   ├── zhttp_request.h
│   ├── zhttp_response.h
│   ├── zhttp_server.h
│   ├── zhttp_server_options.h
│   ├── ziflist.h
│   ├── zlist.h
│   ├── zlistx.h
│   ├── zloop.h
│   ├── zmonitor.h
│   ├── zmq.h            <-- libzmq (dep) header
│   ├── zmq_utils.h      <-- libzmq (dep) header
│   ├── zmsg.h
│   ├── zosc.h
│   ├── zpoller.h
│   ├── zproc.h
│   ├── zproxy.h
│   ├── zrex.h
│   ├── zsock.h
│   ├── zstr.h
│   ├── zsys.h
│   ├── ztimerset.h
│   ├── ztrie.h
│   └── zuuid.h
└── lib
    ├── libzig_czmq.so -> libzig_czmq.so.4
    ├── libzig_czmq.so.4 -> libzig_czmq.so.4.2.2
    └── libzig_czmq.so.4.2.2
2 directories, 50 files
kassane commented 1 year ago

@bluca, a doubt:

Have you ever thought about using a package manager in the project?

Type there is already support for CMake but an old minimal version instead of trying to use FetchContent (zig-pkg similar) to libzmq.

bluca commented 1 year ago

No, this library is provided by distributions

kassane commented 1 year ago

It is now possible to use zig-binding to compile even C projects listed in examples/security/.

BHare1985 commented 3 months ago

Is there any update to this? Zig bindings would be nice to have!