From cd32fa6bc99bc6f42504a2f148396218e337bf25 Mon Sep 17 00:00:00 2001 From: Quetzal Bradley Date: Sun, 12 Feb 2023 07:48:53 +0000 Subject: [PATCH 1/4] export zap module --- build.zig | 177 +++++--------------------------------------------- build.zig.zon | 11 ++++ 2 files changed, 28 insertions(+), 160 deletions(-) create mode 100644 build.zig.zon diff --git a/build.zig b/build.zig index e1941d0..aa49e5d 100644 --- a/build.zig +++ b/build.zig @@ -6,8 +6,21 @@ pub fn build(b: *std.build.Builder) !void { // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. const optimize = b.standardOptimizeOption(.{}); - var ensure_step = b.step("deps", "ensure external dependencies"); - ensure_step.makeFn = ensureDeps; + const facil_dep = b.dependency("facil.io", .{ + .target = target, + .optimize = optimize, + }); + + // create a module to be used internally. + var zap_module = b.createModule(.{ + .source_file = .{ .path = "src/zap.zig" }, + }); + + // register the module so it can be referenced + // using the package manager. + // TODO: How to automatically integrate the + // facil.io dependency with the module? + try b.modules.put(b.dupe("zap"), zap_module); inline for ([_]struct { name: []const u8, @@ -47,9 +60,9 @@ pub fn build(b: *std.build.Builder) !void { .target = target, .optimize = optimize, }); - const zap_module = b.createModule(.{ .source_file = .{ .path = "src/zap.zig" } }); + + example.linkLibrary(facil_dep.artifact("facil.io")); example.addModule("zap", zap_module); - try addFacilio(example, "./"); const example_run = example.run(); example_run_step.dependOn(&example_run.step); @@ -59,162 +72,6 @@ pub fn build(b: *std.build.Builder) !void { // the step invoked via `zig build example` on the installed exe which // itself depends on the "ensure" step const example_build_step = b.addInstallArtifact(example); - example.step.dependOn(ensure_step); example_step.dependOn(&example_build_step.step); } } - -pub fn ensureDeps(step: *std.build.Step) !void { - _ = step; - const allocator = std.heap.page_allocator; - ensureGit(allocator); - ensureGitHook(allocator); - try ensureSubmodule(allocator, "src/deps/facilio"); - try ensurePatch(allocator, "src/deps/facilio", "../0001-microsecond-logging.patch"); - ensureMake(allocator); - try makeFacilioLibdump(allocator); -} - -pub fn addFacilio(exe: *std.build.CompileStep, comptime p: [*]const u8) !void { - exe.linkLibC(); - - // Generate flags - var flags = std.ArrayList([]const u8).init(std.heap.page_allocator); - if (exe.optimize != .Debug) try flags.append("-Os"); - try flags.append("-Wno-return-type-c-linkage"); - try flags.append("-fno-sanitize=undefined"); - try flags.append("-DFIO_OVERRIDE_MALLOC"); - try flags.append("-DFIO_HTTP_EXACT_LOGGING"); - exe.addIncludePath(p ++ "src/deps/facilio/libdump/all"); - - // Add C - exe.addCSourceFiles(&.{ - p ++ "src/deps/facilio/libdump/all/http.c", - p ++ "src/deps/facilio/libdump/all/fiobj_numbers.c", - p ++ "src/deps/facilio/libdump/all/fio_siphash.c", - p ++ "src/deps/facilio/libdump/all/fiobj_str.c", - p ++ "src/deps/facilio/libdump/all/http1.c", - p ++ "src/deps/facilio/libdump/all/fiobj_ary.c", - p ++ "src/deps/facilio/libdump/all/fiobj_data.c", - p ++ "src/deps/facilio/libdump/all/fiobj_hash.c", - p ++ "src/deps/facilio/libdump/all/websockets.c", - p ++ "src/deps/facilio/libdump/all/fiobj_json.c", - p ++ "src/deps/facilio/libdump/all/fio.c", - p ++ "src/deps/facilio/libdump/all/fiobject.c", - p ++ "src/deps/facilio/libdump/all/http_internal.c", - p ++ "src/deps/facilio/libdump/all/fiobj_mustache.c", - }, flags.items); -} - -pub fn addZap(exe: *std.build.CompileStep, comptime p: [*]const u8) !void { - try addFacilio(exe, p); - var b = exe.builder; - var ensure_step = b.step("zap-deps", "ensure zap's dependencies"); - ensure_step.makeFn = ensureDeps; - exe.step.dependOn(ensure_step); -} - -fn sdkPath(comptime suffix: []const u8) []const u8 { - if (suffix[0] != '/') @compileError("suffix must be an absolute path"); - return comptime blk: { - const root_dir = std.fs.path.dirname(@src().file) orelse "."; - break :blk root_dir ++ suffix; - }; -} - -fn ensureGit(allocator: std.mem.Allocator) void { - const result = std.ChildProcess.exec(.{ - .allocator = allocator, - .argv = &.{ "git", "--version" }, - }) catch { // e.g. FileNotFound - std.log.err("error: 'git --version' failed. Is git not installed?", .{}); - std.process.exit(1); - }; - defer { - allocator.free(result.stderr); - allocator.free(result.stdout); - } - if (result.term.Exited != 0) { - std.log.err("error: 'git --version' failed. Is git not installed?", .{}); - std.process.exit(1); - } -} - -pub fn ensureGitHook(allocator: std.mem.Allocator) void { - // only check if we ourselves are not part of a submodule - var cwd = std.fs.cwd().openDir(sdkPath("/"), .{}) catch return; - defer cwd.close(); - const dotgit = cwd.statFile(".git") catch return; - if (dotgit.kind == .File) { - // we are checked out as a submodule. No point in trying to install - // a hook - return; - } - - var child = std.ChildProcess.init( - &.{ "cp", "precommit-hook.sh", ".git/hooks/pre-commit" }, - allocator, - ); - child.cwd = sdkPath("/"); - child.stderr = std.io.getStdErr(); - child.stdout = std.io.getStdOut(); - _ = child.spawnAndWait() catch { - std.log.err("Warning: unable to install git precommit-hook ", .{}); - return; - }; -} - -fn ensureSubmodule(allocator: std.mem.Allocator, path: []const u8) !void { - if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| { - defer allocator.free(no_ensure_submodules); - if (std.mem.eql(u8, no_ensure_submodules, "true")) return; - } else |_| {} - var child = std.ChildProcess.init(&.{ "git", "submodule", "update", "--init", path }, allocator); - child.cwd = sdkPath("/"); - child.stderr = std.io.getStdErr(); - child.stdout = std.io.getStdOut(); - _ = try child.spawnAndWait(); -} - -fn ensurePatch(allocator: std.mem.Allocator, path: []const u8, patch: []const u8) !void { - if (std.process.getEnvVarOwned(allocator, "NO_ENSURE_SUBMODULES")) |no_ensure_submodules| { - defer allocator.free(no_ensure_submodules); - if (std.mem.eql(u8, no_ensure_submodules, "true")) return; - } else |_| {} - var child = std.ChildProcess.init(&.{ "git", "-C", path, "am", "-3", patch }, allocator); - child.cwd = sdkPath("/"); - child.stderr = std.io.getStdErr(); - child.stdout = std.io.getStdOut(); - _ = try child.spawnAndWait(); -} - -fn ensureMake(allocator: std.mem.Allocator) void { - const result = std.ChildProcess.exec(.{ - .allocator = allocator, - .argv = &.{ "make", "--version" }, - }) catch { // e.g. FileNotFound - std.log.err("error: 'make --version' failed. Is make not installed?", .{}); - std.process.exit(1); - }; - defer { - allocator.free(result.stderr); - allocator.free(result.stdout); - } - if (result.term.Exited != 0) { - std.log.err("error: 'make --version' failed. Is make not installed?", .{}); - std.process.exit(1); - } -} - -fn makeFacilioLibdump(allocator: std.mem.Allocator) !void { - var child = std.ChildProcess.init(&.{ - "make", - "-C", - "./src/deps/facilio", - "libdump", - }, allocator); - child.cwd = sdkPath("/"); - child.stderr = std.io.getStdErr(); - child.stdout = std.io.getStdOut(); - _ = try child.spawnAndWait(); -} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..e04ce3b --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,11 @@ +.{ + .name = "zap", + .version = "0.0.1", + + .dependencies = .{ + .@"facil.io" = .{ + .url = "https://github.com/qbradley/facil.io/archive/f5a5a0fa28950f78d0bae251e146ae7a6b0ab869.tar.gz", + .hash = "1220c0ce15fbedd7b5fb3de54f467473585ef5ba9b2db0c37e547a5c1ebc9ce80372", + } + } +} From 3141ecc90f9794284717897cefc94ed5aed60175 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sun, 12 Feb 2023 14:34:42 +0100 Subject: [PATCH 2/4] updated facil.io dependency to our fork --- build.zig.zon | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index e04ce3b..42e7068 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,11 +1,11 @@ .{ - .name = "zap", - .version = "0.0.1", + .name = "zap", + .version = "0.0.1", .dependencies = .{ - .@"facil.io" = .{ - .url = "https://github.com/qbradley/facil.io/archive/f5a5a0fa28950f78d0bae251e146ae7a6b0ab869.tar.gz", - .hash = "1220c0ce15fbedd7b5fb3de54f467473585ef5ba9b2db0c37e547a5c1ebc9ce80372", - } - } + .@"facil.io" = .{ + .url = "https://github.com/renerocksai/facil.io/archive/1b2fce7e46128c526880bce28e9ce68cd1e23ce2.tar.gz", + .hash = "1220b1eb9d016ad6c82ea2840acce19726152eec1193f615399170528f160c8c1003", + } + } } From 013b1ac3ff797d38f1d2a0c6d1a3ea2d743682c3 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sun, 12 Feb 2023 15:07:12 +0100 Subject: [PATCH 3/4] Added revision of our facil.io fork that has a build.zig file --- build.zig.zon | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 42e7068..ff5fe63 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,8 +4,8 @@ .dependencies = .{ .@"facil.io" = .{ - .url = "https://github.com/renerocksai/facil.io/archive/1b2fce7e46128c526880bce28e9ce68cd1e23ce2.tar.gz", - .hash = "1220b1eb9d016ad6c82ea2840acce19726152eec1193f615399170528f160c8c1003", + .url = "https://github.com/renerocksai/facil.io/archive/e49f2bb79bcfaabebc80df770bc9295b808f4d22.tar.gz", + .hash = "1220542d21851b6ff0decaa664f2df25251cb8f65a20a2d1ea5d8b039096c3ff641b", } } } From ae06356c78c9890a0dc6b9095dfd3afe39af60d6 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sun, 12 Feb 2023 16:58:33 +0100 Subject: [PATCH 4/4] depend on zig pkg man friendly fork of facil.io --- build.zig | 4 +--- build.zig.zon | 4 ++-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/build.zig b/build.zig index aa49e5d..49799f0 100644 --- a/build.zig +++ b/build.zig @@ -62,15 +62,13 @@ pub fn build(b: *std.build.Builder) !void { }); example.linkLibrary(facil_dep.artifact("facil.io")); + example.addModule("zap", zap_module); const example_run = example.run(); example_run_step.dependOn(&example_run.step); // install the artifact - depending on the "example" - // only after the ensure step - // the step invoked via `zig build example` on the installed exe which - // itself depends on the "ensure" step const example_build_step = b.addInstallArtifact(example); example_step.dependOn(&example_build_step.step); } diff --git a/build.zig.zon b/build.zig.zon index ff5fe63..a791b64 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -4,8 +4,8 @@ .dependencies = .{ .@"facil.io" = .{ - .url = "https://github.com/renerocksai/facil.io/archive/e49f2bb79bcfaabebc80df770bc9295b808f4d22.tar.gz", - .hash = "1220542d21851b6ff0decaa664f2df25251cb8f65a20a2d1ea5d8b039096c3ff641b", + .url = "https://github.com/renerocksai/facil.io/archive/2c04cd1949328dd62fe5d262b9cc930e54392ab8.tar.gz", + .hash = "12209d3b552145f24431e5a2e6a4ad59ceaa9656f7fba8af7a8aa704a8784a79f55d", } } }