mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 23:24:09 +00:00
cleaned up build process
This commit is contained in:
parent
68e197cc21
commit
db33bd9b40
4 changed files with 116 additions and 24 deletions
54
build.zig
54
build.zig
|
@ -11,30 +11,41 @@ pub fn build(b: *std.build.Builder) !void {
|
||||||
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
|
||||||
const mode = b.standardReleaseOptions();
|
const mode = b.standardReleaseOptions();
|
||||||
|
|
||||||
const lib = b.addStaticLibrary("zap", "src/main.zig");
|
var ensure_step = b.step("deps", "ensure external dependencies");
|
||||||
lib.setBuildMode(mode);
|
ensure_step.makeFn = ensureDeps;
|
||||||
lib.addPackage(facilio);
|
|
||||||
|
|
||||||
const lib_facilio = try addFacilioLib(lib);
|
const example_run_step = b.step("run-example", "run the example");
|
||||||
lib.linkLibrary(lib_facilio);
|
const example_step = b.step("example", "build the example");
|
||||||
lib.install();
|
|
||||||
|
|
||||||
const main_tests = b.addTest("src/main.zig");
|
var example = b.addExecutable("example", "examples/hello/hello.zig");
|
||||||
main_tests.setBuildMode(mode);
|
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");
|
const example_run = example.run();
|
||||||
test_step.dependOn(&main_tests.step);
|
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 {
|
pub fn ensureDeps(step: *std.build.Step) !void {
|
||||||
ensureGit(exe.builder.allocator);
|
_ = step;
|
||||||
try ensureSubmodule(exe.builder.allocator, "src/deps/facilio");
|
const allocator = std.heap.page_allocator;
|
||||||
ensureMake(exe.builder.allocator);
|
ensureGit(allocator);
|
||||||
try makeFacilioLibdump(exe.builder.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 b = exe.builder;
|
||||||
var lib_facilio = b.addStaticLibrary("facilio", null);
|
exe.linkLibC();
|
||||||
lib_facilio.linkLibC();
|
|
||||||
|
|
||||||
// Generate flags
|
// Generate flags
|
||||||
var flags = std.ArrayList([]const u8).init(std.heap.page_allocator);
|
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("-Wno-return-type-c-linkage");
|
||||||
try flags.append("-fno-sanitize=undefined");
|
try flags.append("-fno-sanitize=undefined");
|
||||||
|
|
||||||
lib_facilio.addIncludePath("./src/deps/facilio/libdump/all");
|
exe.addIncludePath("./src/deps/facilio/libdump/all");
|
||||||
|
|
||||||
// legacy for fio_mem
|
|
||||||
lib_facilio.addIncludePath("src/deps/facilio/lib/facil/legacy");
|
|
||||||
|
|
||||||
// Add C
|
// Add C
|
||||||
lib_facilio.addCSourceFiles(&.{
|
exe.addCSourceFiles(&.{
|
||||||
"src/deps/facilio/libdump/all/http.c",
|
"src/deps/facilio/libdump/all/http.c",
|
||||||
"src/deps/facilio/libdump/all/fiobj_numbers.c",
|
"src/deps/facilio/libdump/all/fiobj_numbers.c",
|
||||||
"src/deps/facilio/libdump/all/fio_siphash.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/http_internal.c",
|
||||||
"src/deps/facilio/libdump/all/fiobj_mustache.c",
|
"src/deps/facilio/libdump/all/fiobj_mustache.c",
|
||||||
}, flags.items);
|
}, flags.items);
|
||||||
|
|
||||||
return lib_facilio;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sdkPath(comptime suffix: []const u8) []const u8 {
|
fn sdkPath(comptime suffix: []const u8) []const u8 {
|
||||||
|
|
43
examples/hello/hello.zig
Normal file
43
examples/hello/hello.zig
Normal file
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
|
@ -1 +1,6 @@
|
||||||
// zig type definitions for facilio lib
|
// zig type definitions for facilio lib
|
||||||
|
|
||||||
|
pub const Http = @cImport({
|
||||||
|
@cInclude("http.h");
|
||||||
|
@cInclude("fio.h");
|
||||||
|
});
|
||||||
|
|
38
src/main.zig
38
src/main.zig
|
@ -1,10 +1,48 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const testing = std.testing;
|
const testing = std.testing;
|
||||||
|
const facilio = @import("facilio").Http;
|
||||||
|
|
||||||
export fn add(a: i32, b: i32) i32 {
|
export fn add(a: i32, b: i32) i32 {
|
||||||
return a + b;
|
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" {
|
test "basic add functionality" {
|
||||||
try testing.expect(add(3, 7) == 10);
|
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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue