From df515b8cadee5b98feef748e7d5093465ee6f17b Mon Sep 17 00:00:00 2001 From: Yves Biener Date: Sat, 30 Nov 2024 14:45:57 +0100 Subject: [PATCH] mod: remove dependency to c std library and replace with ztime dependency for a pure zig implementation --- build.zig | 21 +++++++++------------ build.zig.zon | 6 ++++++ src/zlog.zig | 9 ++------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/build.zig b/build.zig index 629fd6e..40477d7 100644 --- a/build.zig +++ b/build.zig @@ -27,14 +27,19 @@ pub fn build(b: *std.Build) void { // set a preferred release mode, allowing the user to decide how to optimize. const optimize = b.standardOptimizeOption(.{}); + const ztime_dependency = b.dependency("ztime", .{ + .target = target, + .optimize = optimize, + }); + const zlog_module = b.addModule("zlog", .{ // 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 = b.path("src/zlog.zig"), .target = target, .optimize = optimize, - .link_libc = include_timestamp, // uses c std library }); + zlog_module.addImport("ztime", ztime_dependency.module("ztime")); zlog_module.addImport("build_options", options_module); const exe = b.addExecutable(.{ @@ -43,6 +48,7 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + exe.root_module.addImport("ztime", ztime_dependency.module("ztime")); exe.root_module.addImport("zlog", zlog_module); // This declares intent for the executable to be installed into the @@ -50,17 +56,6 @@ pub fn build(b: *std.Build) void { // step when running `zig build`). b.installArtifact(exe); - const exe_check = b.addExecutable(.{ - .name = "check", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - }); - exe_check.root_module.addImport("zlog", zlog_module); - - const check = b.step("check", "Check if project compiles"); - check.dependOn(&exe_check.step); - // This *creates* a Run step in the build graph, to be executed when another // step is evaluated that depends on it. The next line below will establish // such a dependency. @@ -91,6 +86,8 @@ pub fn build(b: *std.Build) void { .target = target, .optimize = optimize, }); + lib_unit_tests.root_module.addImport("ztime", ztime_dependency.module("ztime")); + lib_unit_tests.root_module.addImport("build_options", options_module); const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests); diff --git a/build.zig.zon b/build.zig.zon index 47fe730..d873fbd 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,6 +3,12 @@ // version name should match the zig version except for the last number, // which stands for the version inside a given zig version .version = "0.13.0", + .dependencies = .{ + .ztime = .{ + .url = "git+https://gitea.yves-biener.de/yves-biener/ztime#c0f370c1c4673bb5f1fc1a1266e9819bafec0082", + .hash = "1220f094aa3a4e38ed4140da594e9eae3a3d66e7ba5eb3ea3f6fb7ed561d6981c6be", + }, + }, .minimum_zig_version = "0.13.0", .paths = .{ "build.zig", diff --git a/src/zlog.zig b/src/zlog.zig index 03cae86..443eba4 100644 --- a/src/zlog.zig +++ b/src/zlog.zig @@ -1,5 +1,5 @@ const build_options = @import("build_options"); -const c_time = if (build_options.timestamp) @cImport(@cInclude("time.h")) else null; +const ztime = if (build_options.timestamp) @import("ztime") else null; const std = @import("std"); pub const std_options: std.Options = .{ @@ -55,12 +55,7 @@ fn log_writing(writer: anytype, comptime fmt: []const u8, args: anytype) void { } fn log_timestamp(writer: anytype) void { - const curtime = c_time.time(null); - const tm = c_time.localtime(&curtime); - - var buffer: [16]u8 = undefined; - _ = c_time.strftime(@ptrCast(&buffer), 32, "%F %R", tm); - writer.print("[{s}] ", .{buffer}) catch return; + writer.print("[{any}] ", .{ztime.DateTime.now()}) catch return; } pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {