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

ZProject: Zig #1329

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ At least the following build environments are currently supported:
* Android
* iOS
* Visual Studio
* Zig

Thanks to the ZeroMQ community, you can do all the heavy lifting in C and then easily generate bindings in the following languages:

Expand All @@ -91,6 +92,7 @@ Thanks to the ZeroMQ community, you can do all the heavy lifting in C and then e
* QML
* Qt
* Ruby
* Ziglang

The language bindings are minimal, meant to be wrapped in a handwritten idiomatic layer later.

Expand Down Expand Up @@ -219,6 +221,7 @@ zproject's `project.xml` contains an extensive description of the available conf
vs2012 Microsoft Visual Studio 2012
vs2013 Microsoft Visual Studio 2013
vs2015 Microsoft Visual Studio 2015
zig Zig binding and build system

Classes are automatically added to all build environments. Further as you
add new classes to your project you can generate skeleton header and source
Expand Down Expand Up @@ -619,6 +622,7 @@ zproject's `project.xml` contains an extensive description of the available conf
<bin name = "zproject_vs2008.gsl" />
<bin name = "zproject_vs20xx.gsl" />
<bin name = "zproject_vs20xx_props.gsl" />
<bin name = "zproject_zig.gsl" />

<bin name = "zproject_known_projects.xml" />
</project>
Expand Down
2 changes: 2 additions & 0 deletions project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
vs2012 Microsoft Visual Studio 2012
vs2013 Microsoft Visual Studio 2013
vs2015 Microsoft Visual Studio 2015
zig Zig binding and build system

Classes are automatically added to all build environments. Further as you
add new classes to your project you can generate skeleton header and source
Expand Down Expand Up @@ -440,6 +441,7 @@
<bin name = "zproject_vs2008.gsl" />
<bin name = "zproject_vs20xx.gsl" />
<bin name = "zproject_vs20xx_props.gsl" />
<bin name = "zproject_zig.gsl" />

<bin name = "zproject_known_projects.xml" />
</project>
1 change: 1 addition & 0 deletions zproject.gsl
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ gsl from "zproject_vagrant.gsl"
gsl from "zproject_vs2008.gsl"
gsl from "zproject_vs20xx.gsl"
gsl from "zproject_vs20xx_props.gsl"
gsl from "zproject_zig.gsl"

# Build targets requested by project
if defined (switches.target)
Expand Down
152 changes: 152 additions & 0 deletions zproject_zig.gsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# Generate minimal Zig build.
#
# This is a code generator built using the iMatix GSL code generation
# language. See https://github.com/zeromq/gsl for details.
#
# Copyright (c) the Contributors as noted in the AUTHORS file.
# This file is part of zproject.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

register_target ("zig", "Zig build")

# Target provides name space isolation for its functions

.macro target_zig
.output "build.zig"
$(project.GENERATED_WARNING_HEADER:)

//! Works only Zig version: 0.11.0 or higher

const std = @import("std");

// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
// runner.
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});

// Standard optimization options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

// Generating "platform.h" on $PWD/zig-cache (local build cache)
const config_header = if (!target.isWindows()) b.addConfigHeader(.{
.style = .blank,
.include_path = "platform.h",
}, .{
.CZMQ_BUILD_DRAFT_API = {},
.HAVE_LINUX_WIRELESS_H = {},
.HAVE_NET_IF_H = {},
.HAVE_NET_IF_MEDIA_H = null,
.HAVE_GETIFADDRS = {},
.HAVE_FREEIFADDRS = {},
.HAVE_DECL_AI_V4MAPPED = 0,
}) else b.addConfigHeader(.{
.style = .blank,
.include_path = "platform.h",
}, .{});

const lib = b.addSharedLibrary(.{
.name = "zig_czmq",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
//.root_source_file = .{ .path = "bindings/zig/src/main.zig" }, // No need zig file to build library (C and/or C++ code only)
.version = .{
.major = 4,
.minor = 2,
.patch = 2,
},
.target = target,
.optimize = optimize,
});
lib.addConfigHeader(config_header);
lib.addIncludePath("include");
lib.addIncludePath(config_header.include_path);
lib.addCSourceFiles(lib_src, lib_flags);
if (target.isWindows()) {
lib.linkSystemLibraryName("ws2_32");
lib.linkSystemLibraryName("rpcrt4");
lib.linkSystemLibraryName("iphlpapi");
}
lib.linkSystemLibrary("zmq");
lib.linkLibC();
// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
// running `zig build`).
lib.install();

// Creates a step for unit testing.
const libtest = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.target = target,
.optimize = optimize,
});
libtest.addConfigHeader(config_header);
libtest.addIncludePath(config_header.include_path);
libtest.addIncludePath("include");
libtest.addCSourceFiles(lib_src, lib_flags);
if (target.isWindows()) {
libtest.linkSystemLibraryName("ws2_32");
libtest.linkSystemLibraryName("rpcrt4");
libtest.linkSystemLibraryName("iphlpapi");
}
libtest.linkSystemLibrary("zmq");
libtest.linkLibC();

// This creates a build step. It will be visible in the `zig build --help` menu,
// and can be selected like this: `zig build test`
// This will evaluate the `test` step rather than the default, which is "install".
const test_step = b.step("test", "Run library tests");
test_step.dependOn(&libtest.step);
}

const lib_flags: []const []const u8 = &.{
"-std=gnu99",
"-O3",
"-Wall",
"-fno-sanitize=undefined",
"-fno-sanitize-trap=undefined",
// disable undefined sanitizer (default on zig/clang wrapper)
};
const lib_src: []const []const u8 = &.{
"src/zactor.c",
"src/zarmour.c",
"src/zcert.c",
"src/zcertstore.c",
"src/zchunk.c",
"src/zclock.c",
"src/zconfig.c",
"src/zdigest.c",
"src/zdir.c",
"src/zdir_patch.c",
"src/zfile.c",
"src/zframe.c",
"src/zhash.c",
"src/zhashx.c",
"src/ziflist.c",
"src/zlist.c",
"src/zlistx.c",
"src/zloop.c",
"src/zmsg.c",
"src/zpoller.c",
"src/zsock.c",
"src/zstr.c",
"src/zsys.c",
"src/zuuid.c",
"src/zauth.c",
"src/zbeacon.c",
"src/zgossip.c",
"src/zmonitor.c",
"src/zproxy.c",
"src/zrex.c",
"src/zgossip_msg.c",
};
.endmacro