From db33bd9b403f9d8fbd4f8d7265cbd2ad4fb5e2fe Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Wed, 11 Jan 2023 19:22:07 +0100 Subject: [PATCH] cleaned up build process --- build.zig | 54 ++++++++++++++++++++++------------------ examples/hello/hello.zig | 43 ++++++++++++++++++++++++++++++++ src/deps/facilio.zig | 5 ++++ src/main.zig | 38 ++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 24 deletions(-) create mode 100644 examples/hello/hello.zig diff --git a/build.zig b/build.zig index 2b00704..a56382b 100644 --- a/build.zig +++ b/build.zig @@ -11,30 +11,41 @@ pub fn build(b: *std.build.Builder) !void { // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const mode = b.standardReleaseOptions(); - const lib = b.addStaticLibrary("zap", "src/main.zig"); - lib.setBuildMode(mode); - lib.addPackage(facilio); + var ensure_step = b.step("deps", "ensure external dependencies"); + ensure_step.makeFn = ensureDeps; - const lib_facilio = try addFacilioLib(lib); - lib.linkLibrary(lib_facilio); - lib.install(); + const example_run_step = b.step("run-example", "run the example"); + const example_step = b.step("example", "build the example"); - const main_tests = b.addTest("src/main.zig"); - main_tests.setBuildMode(mode); + var example = b.addExecutable("example", "examples/hello/hello.zig"); + example.setBuildMode(mode); + example.addPackage(facilio); + example.addIncludePath("src/deps/facilio/libdump/all"); + _ = try addFacilio(example); - const test_step = b.step("test", "Run library tests"); - test_step.dependOn(&main_tests.step); + const example_run = example.run(); + example_run_step.dependOn(&example_run.step); + + // install the artifact + const example_build_step = b.addInstallArtifact(example); + // only after the ensure step + example_build_step.step.dependOn(ensure_step); + // via `zig build example` invoked step depends on the installed exe + example_step.dependOn(&example_build_step.step); } -pub fn addFacilioLib(exe: *std.build.LibExeObjStep) !*std.build.LibExeObjStep { - ensureGit(exe.builder.allocator); - try ensureSubmodule(exe.builder.allocator, "src/deps/facilio"); - ensureMake(exe.builder.allocator); - try makeFacilioLibdump(exe.builder.allocator); +pub fn ensureDeps(step: *std.build.Step) !void { + _ = step; + const allocator = std.heap.page_allocator; + ensureGit(allocator); + try ensureSubmodule(allocator, "src/deps/facilio"); + ensureMake(allocator); + try makeFacilioLibdump(allocator); +} +pub fn addFacilio(exe: *std.build.LibExeObjStep) !void { var b = exe.builder; - var lib_facilio = b.addStaticLibrary("facilio", null); - lib_facilio.linkLibC(); + exe.linkLibC(); // Generate flags var flags = std.ArrayList([]const u8).init(std.heap.page_allocator); @@ -42,13 +53,10 @@ pub fn addFacilioLib(exe: *std.build.LibExeObjStep) !*std.build.LibExeObjStep { try flags.append("-Wno-return-type-c-linkage"); try flags.append("-fno-sanitize=undefined"); - lib_facilio.addIncludePath("./src/deps/facilio/libdump/all"); - - // legacy for fio_mem - lib_facilio.addIncludePath("src/deps/facilio/lib/facil/legacy"); + exe.addIncludePath("./src/deps/facilio/libdump/all"); // Add C - lib_facilio.addCSourceFiles(&.{ + exe.addCSourceFiles(&.{ "src/deps/facilio/libdump/all/http.c", "src/deps/facilio/libdump/all/fiobj_numbers.c", "src/deps/facilio/libdump/all/fio_siphash.c", @@ -64,8 +72,6 @@ pub fn addFacilioLib(exe: *std.build.LibExeObjStep) !*std.build.LibExeObjStep { "src/deps/facilio/libdump/all/http_internal.c", "src/deps/facilio/libdump/all/fiobj_mustache.c", }, flags.items); - - return lib_facilio; } fn sdkPath(comptime suffix: []const u8) []const u8 { diff --git a/examples/hello/hello.zig b/examples/hello/hello.zig new file mode 100644 index 0000000..5f3a75d --- /dev/null +++ b/examples/hello/hello.zig @@ -0,0 +1,43 @@ +const std = @import("std"); +const facilio = @import("facilio").Http; + +fn on_request(request: [*c]facilio.http_s) callconv(.C) void { + std.debug.print("REQUEST!\n", .{}); + var msg: []const u8 = "Hello from ZAP!"; + _ = facilio.http_send_body(request, @intToPtr( + *anyopaque, + @ptrToInt(msg.ptr), + ), msg.len); +} + +pub fn main() void { + if (facilio.http_listen("3000", null, .{ + .on_request = on_request, + .log = 1, + .on_upgrade = null, + .on_response = null, + .on_finish = null, + .udata = null, + .public_folder = null, + .public_folder_length = 0, + .max_header_size = 4096, + .max_body_size = 4096, + .max_clients = 42, + .tls = null, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + .ws_max_msg_size = 250 * 1024, + .timeout = 0, + .ws_timeout = 0, + .is_client = 0, + }) == -1) { + // listen failed + std.debug.print("Listening failed\n", .{}); + return; + } + facilio.fio_start(.{ + .threads = 4, + .workers = 4, + }); +} diff --git a/src/deps/facilio.zig b/src/deps/facilio.zig index 538f1d2..4b114b0 100644 --- a/src/deps/facilio.zig +++ b/src/deps/facilio.zig @@ -1 +1,6 @@ // zig type definitions for facilio lib + +pub const Http = @cImport({ + @cInclude("http.h"); + @cInclude("fio.h"); +}); diff --git a/src/main.zig b/src/main.zig index ecfeade..17fbaab 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,10 +1,48 @@ const std = @import("std"); const testing = std.testing; +const facilio = @import("facilio").Http; export fn add(a: i32, b: i32) i32 { return a + b; } +fn on_request(request: [*c]facilio.http_s) callconv(.C) void { + std.debug.print("REQUEST!\n", .{}); + var msg: []const u8 = "Hello from ZAP!"; + _ = facilio.http_send_body(request, @intToPtr( + *anyopaque, + @ptrToInt(msg.ptr), + ), msg.len); +} + test "basic add functionality" { try testing.expect(add(3, 7) == 10); } + +test "http" { + _ = facilio.http_listen("3000", null, .{ + .on_request = on_request, + .log = 1, + .on_upgrade = null, + .on_response = null, + .on_finish = null, + .udata = null, + .public_folder = null, + .public_folder_length = 0, + .max_header_size = 4096, + .max_body_size = 4096, + .max_clients = 42, + .tls = null, + .reserved1 = 0, + .reserved2 = 0, + .reserved3 = 0, + .ws_max_msg_size = 250 * 1024, + .timeout = 0, + .ws_timeout = 0, + .is_client = 0, + }); + _ = facilio.fio_start(.{ + .threads = 4, + .workers = 4, + }); +}