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

New binding language: Zig #2258

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft

New binding language: Zig #2258

wants to merge 9 commits into from

Conversation

kassane
Copy link

@kassane kassane commented Feb 17, 2023

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

test "Hello 0MQ" {
const msg: cstr = "Hello, Curves!\n";
var publisher: ?*czmq.zsock_t = czmq.zsock_new(czmq.ZMQ_PUB);
czmq.zsock_set_curve_server(publisher, @boolToInt(true));
debug.print("\n{s}", .{msg});
czmq.zsock_destroy(&publisher);
}

$> 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

zig binding test (need zig version: 0.11)
Replace cmake/autoconf to zig build.
This build example need libzmq only
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 (ref.: ziglang/zig#14307)
@kassane kassane marked this pull request as draft February 17, 2023 16:42
@bluca
Copy link
Member

bluca commented Feb 17, 2023

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

@kassane
Copy link
Author

kassane commented Feb 18, 2023

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
Copy link
Author

kassane commented Feb 20, 2023

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
Copy link
Author

kassane commented Feb 21, 2023

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
Copy link
Author

kassane commented Feb 21, 2023

@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
Copy link
Member

bluca commented Feb 21, 2023

No, this library is provided by distributions

@kassane
Copy link
Author

kassane commented Feb 23, 2023

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

@BHare1985
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants