Skip to content

Commit

Permalink
feat:make build and test works with zig master
Browse files Browse the repository at this point in the history
Signed-off-by: Chen Kai <[email protected]>
  • Loading branch information
GrapeBaBa committed Nov 24, 2024
1 parent b514e36 commit c1542ee
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 244 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
zig-cache/
zig-out/
.zig-cache
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
<hr>

**⚠️ Project Archived ⚠️.** This project is archived. I used this for a few
real projects since 2021 and it was stable, but I've since moved onto using
[libxev](https://github.com/mitchellh/libxev) so I'm no longer maintaining
the libuv bindings.
Forked this repo to upgrade the zig version to master. The original repo is [here](https://github.com/mitchellh/zig-libuv).

<hr>

Expand Down
190 changes: 105 additions & 85 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const std = @import("std");
const root = thisDir() ++ "/vendor/libuv/";
const include_path = root ++ "include";

pub const pkg = std.build.Pkg{
pub const pkg = .{
.name = "libuv",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
};
Expand All @@ -13,57 +13,56 @@ fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}

pub fn build(b: *std.build.Builder) !void {
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const tests = b.addTest(.{
.name = "pixman-test",
.kind = .test_exe,
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = .{ .cwd_relative = "src/main.zig" },
.target = target,
.optimize = optimize,
});
_ = try link(b, tests);
tests.install();
b.installArtifact(tests);

const test_step = b.step("test", "Run tests");
const tests_run = tests.run();
const tests_run = b.addRunArtifact(tests);
test_step.dependOn(&tests_run.step);
}

pub fn link(b: *std.build.Builder, step: *std.build.LibExeObjStep) !*std.build.LibExeObjStep {
pub fn link(b: *std.Build, step: *std.Build.Step.Compile) !*std.Build.Step.Compile {
const libuv = try buildLibuv(b, step);
step.linkLibrary(libuv);
step.addIncludePath(include_path);
step.addIncludePath(.{ .cwd_relative = include_path });
return libuv;
}

pub fn buildLibuv(
b: *std.build.Builder,
step: *std.build.LibExeObjStep,
) !*std.build.LibExeObjStep {
const target = step.target;
b: *std.Build,
step: *std.Build.Step.Compile,
) !*std.Build.Step.Compile {
const target = step.root_module.resolved_target.?;
const lib = b.addStaticLibrary(.{
.name = "uv",
.target = target,
.optimize = step.optimize,
.optimize = step.root_module.optimize.?,
});

// Include dirs
lib.addIncludePath(include_path);
lib.addIncludePath(root ++ "src");
lib.addIncludePath(.{ .cwd_relative = include_path });
lib.addIncludePath(.{ .cwd_relative = root ++ "src" });

// Links
if (target.isWindows()) {
if (target.result.os.tag == .windows) {
lib.linkSystemLibrary("psapi");
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("advapi32");
lib.linkSystemLibrary("iphlpapi");
lib.linkSystemLibrary("userenv");
lib.linkSystemLibrary("ws2_32");
}
if (target.isLinux()) {
if (target.result.os.tag == .linux) {
lib.linkSystemLibrary("pthread");
}
lib.linkLibC();
Expand All @@ -73,104 +72,125 @@ pub fn buildLibuv(
defer flags.deinit();
// try flags.appendSlice(&.{});

if (!target.isWindows()) {
if (target.result.os.tag != .windows) {
try flags.appendSlice(&.{
"-D_FILE_OFFSET_BITS=64",
"-D_LARGEFILE_SOURCE",
});
}

if (target.isLinux()) {
if (target.result.os.tag == .linux) {
try flags.appendSlice(&.{
"-D_GNU_SOURCE",
"-D_POSIX_C_SOURCE=200112",
});
}

if (target.isDarwin()) {
if (target.result.os.tag == .macos) {
try flags.appendSlice(&.{
"-D_DARWIN_UNLIMITED_SELECT=1",
"-D_DARWIN_USE_64_BIT_INODE=1",
});
}

// C files common to all platforms
lib.addCSourceFiles(&.{
root ++ "src/fs-poll.c",
root ++ "src/idna.c",
root ++ "src/inet.c",
root ++ "src/random.c",
root ++ "src/strscpy.c",
root ++ "src/strtok.c",
root ++ "src/threadpool.c",
root ++ "src/timer.c",
root ++ "src/uv-common.c",
root ++ "src/uv-data-getter-setters.c",
root ++ "src/version.c",
}, flags.items);

if (!target.isWindows()) {
lib.addCSourceFiles(&.{
root ++ "src/unix/async.c",
root ++ "src/unix/core.c",
root ++ "src/unix/dl.c",
root ++ "src/unix/fs.c",
root ++ "src/unix/getaddrinfo.c",
root ++ "src/unix/getnameinfo.c",
root ++ "src/unix/loop-watcher.c",
root ++ "src/unix/loop.c",
root ++ "src/unix/pipe.c",
root ++ "src/unix/poll.c",
root ++ "src/unix/process.c",
root ++ "src/unix/random-devurandom.c",
root ++ "src/unix/signal.c",
root ++ "src/unix/stream.c",
root ++ "src/unix/tcp.c",
root ++ "src/unix/thread.c",
root ++ "src/unix/tty.c",
root ++ "src/unix/udp.c",
}, flags.items);
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/fs-poll.c",
root ++ "src/idna.c",
root ++ "src/inet.c",
root ++ "src/random.c",
root ++ "src/strscpy.c",
root ++ "src/strtok.c",
root ++ "src/threadpool.c",
root ++ "src/timer.c",
root ++ "src/uv-common.c",
root ++ "src/uv-data-getter-setters.c",
root ++ "src/version.c",
},
.flags = flags.items,
});

if (target.result.os.tag != .windows) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/async.c",
root ++ "src/unix/core.c",
root ++ "src/unix/dl.c",
root ++ "src/unix/fs.c",
root ++ "src/unix/getaddrinfo.c",
root ++ "src/unix/getnameinfo.c",
root ++ "src/unix/loop-watcher.c",
root ++ "src/unix/loop.c",
root ++ "src/unix/pipe.c",
root ++ "src/unix/poll.c",
root ++ "src/unix/process.c",
root ++ "src/unix/random-devurandom.c",
root ++ "src/unix/signal.c",
root ++ "src/unix/stream.c",
root ++ "src/unix/tcp.c",
root ++ "src/unix/thread.c",
root ++ "src/unix/tty.c",
root ++ "src/unix/udp.c",
},
.flags = flags.items,
});
}

if (target.isLinux() or target.isDarwin()) {
lib.addCSourceFiles(&.{
root ++ "src/unix/proctitle.c",
}, flags.items);
if (target.result.os.tag == .linux or target.result.os.tag == .macos) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/proctitle.c",
},
.flags = flags.items,
});
}

if (target.isLinux()) {
lib.addCSourceFiles(&.{
root ++ "src/unix/linux.c",
root ++ "src/unix/procfs-exepath.c",
root ++ "src/unix/random-getrandom.c",
root ++ "src/unix/random-sysctl-linux.c",
}, flags.items);
if (target.result.os.tag == .linux) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/linux.c",
root ++ "src/unix/procfs-exepath.c",
root ++ "src/unix/random-getrandom.c",
root ++ "src/unix/random-sysctl-linux.c",
},
.flags = flags.items,
});
}

if (target.isDarwin() or
target.isOpenBSD() or
target.isNetBSD() or
target.isFreeBSD() or
target.isDragonFlyBSD())
if (target.result.os.tag == .macos or
target.result.os.tag == .openbsd or
target.result.os.tag == .netbsd or
target.result.os.tag == .freebsd or
target.result.os.tag == .dragonfly)
{
lib.addCSourceFiles(&.{
root ++ "src/unix/bsd-ifaddrs.c",
root ++ "src/unix/kqueue.c",
}, flags.items);
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/bsd-ifaddrs.c",
root ++ "src/unix/kqueue.c",
},
.flags = flags.items,
});
}

if (target.isDarwin() or target.isOpenBSD()) {
lib.addCSourceFiles(&.{
root ++ "src/unix/random-getentropy.c",
}, flags.items);
if (target.result.os.tag == .macos or target.result.os.tag == .openbsd) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/random-getentropy.c",
},
.flags = flags.items,
});
}

if (target.isDarwin()) {
lib.addCSourceFiles(&.{
root ++ "src/unix/darwin-proctitle.c",
root ++ "src/unix/darwin.c",
root ++ "src/unix/fsevents.c",
}, flags.items);
if (target.result.os.tag == .macos) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/darwin-proctitle.c",
root ++ "src/unix/darwin.c",
root ++ "src/unix/fsevents.c",
},
.flags = flags.items,
});
}

return lib;
Expand Down
2 changes: 1 addition & 1 deletion src/Async.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ handle: *c.uv_async_t,
pub usingnamespace Handle(Async);

pub fn init(alloc: Allocator, loop: Loop, comptime cb: fn (*Async) void) !Async {
var handle = try alloc.create(c.uv_async_t);
const handle = try alloc.create(c.uv_async_t);
errdefer alloc.destroy(handle);

const Wrapper = struct {
Expand Down
2 changes: 1 addition & 1 deletion src/Idle.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ handle: *c.uv_idle_t,
pub usingnamespace Handle(Idle);

pub fn init(alloc: Allocator, loop: Loop) !Idle {
var handle = try alloc.create(c.uv_idle_t);
const handle = try alloc.create(c.uv_idle_t);
errdefer alloc.destroy(handle);

try errors.convertError(c.uv_idle_init(loop.loop, handle));
Expand Down
6 changes: 3 additions & 3 deletions src/Loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ pub fn alive(self: Loop) !bool {
///
/// This is not reentrant. It must not be called from a callback.
pub fn run(self: Loop, mode: RunMode) !u32 {
const res = c.uv_run(self.loop, @enumToInt(mode));
const res = c.uv_run(self.loop, @intFromEnum(mode));
try errors.convertError(res);
return @intCast(u32, res);
return @as(u32, @intCast(res));
}

/// Stop the event loop, causing uv_run() to end as soon as possible. This
Expand Down Expand Up @@ -98,7 +98,7 @@ pub fn setData(self: Loop, pointer: ?*anyopaque) void {
/// Returns loop->data.
pub fn getData(self: Loop, comptime DT: type) ?*DT {
return if (c.uv_loop_get_data(self.loop)) |ptr|
@ptrCast(?*DT, @alignCast(@alignOf(DT), ptr))
@as(?*DT, @ptrCast(@alignCast(ptr)))
else
null;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pipe.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub const Flags = packed struct {
_ignore_high: u1 = 0,

pub inline fn toInt(self: Flags, comptime IntType: type) IntType {
return @intCast(IntType, @bitCast(u8, self));
return @as(IntType, @intCast(@as(u8, @bitCast(self))));
}

test "Flags: expected value" {
Expand Down Expand Up @@ -55,9 +55,9 @@ pub fn pipe(read_flags: Flags, write_flags: Flags) !Pair {
}

pub fn init(alloc: Allocator, loop: Loop, ipc: bool) !Pipe {
var handle = try alloc.create(c.uv_pipe_t);
const handle = try alloc.create(c.uv_pipe_t);
errdefer alloc.destroy(handle);
try errors.convertError(c.uv_pipe_init(loop.loop, handle, @boolToInt(ipc)));
try errors.convertError(c.uv_pipe_init(loop.loop, handle, @intFromBool(ipc)));
return Pipe{ .handle = handle };
}

Expand Down Expand Up @@ -167,7 +167,7 @@ const TestData = struct {
var data = h.getData(TestData).?;
data.data.appendSlice(
testing.allocator,
buf[0..@intCast(usize, n)],
buf[0..@as(usize, @intCast(n))],
) catch unreachable;
testing.allocator.free(buf);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Prepare.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ handle: *c.uv_prepare_t,
pub usingnamespace Handle(Prepare);

pub fn init(alloc: Allocator, loop: Loop) !Prepare {
var handle = try alloc.create(c.uv_prepare_t);
const handle = try alloc.create(c.uv_prepare_t);
errdefer alloc.destroy(handle);

try errors.convertError(c.uv_prepare_init(loop.loop, handle));
Expand Down
4 changes: 2 additions & 2 deletions src/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ pub fn initData(
// to do the proper casts for the callback.
const Data = @TypeOf(data);
const dataInfo = @typeInfo(Data);
if (dataInfo != .Pointer) @compileError("data must be a pointer type");
if (dataInfo != .pointer) @compileError("data must be a pointer type");

const CWrapper = struct {
pub fn wrapper(arg: ?*anyopaque) callconv(.C) void {
@call(.always_inline, callback, .{
@ptrCast(Data, @alignCast(@alignOf(dataInfo.Pointer.child), arg)),
@as(Data, @ptrCast(@alignCast(arg))),
});
}
};
Expand Down
Loading

0 comments on commit c1542ee

Please sign in to comment.