mod: remove dependency to c std library and replace with ztime dependency for a pure zig implementation
All checks were successful
Zig Project Action / Lint, Spell-check and test zig project (push) Successful in 57s

This commit is contained in:
2024-11-30 14:45:57 +01:00
parent 06752299be
commit df515b8cad
3 changed files with 17 additions and 19 deletions

View File

@@ -27,14 +27,19 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize. // set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{}); const optimize = b.standardOptimizeOption(.{});
const ztime_dependency = b.dependency("ztime", .{
.target = target,
.optimize = optimize,
});
const zlog_module = b.addModule("zlog", .{ const zlog_module = b.addModule("zlog", .{
// In this case the main source file is merely a path, however, in more // In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file. // complicated build scripts, this could be a generated file.
.root_source_file = b.path("src/zlog.zig"), .root_source_file = b.path("src/zlog.zig"),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
.link_libc = include_timestamp, // uses c std library <time.h>
}); });
zlog_module.addImport("ztime", ztime_dependency.module("ztime"));
zlog_module.addImport("build_options", options_module); zlog_module.addImport("build_options", options_module);
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
@@ -43,6 +48,7 @@ pub fn build(b: *std.Build) void {
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.root_module.addImport("ztime", ztime_dependency.module("ztime"));
exe.root_module.addImport("zlog", zlog_module); exe.root_module.addImport("zlog", zlog_module);
// This declares intent for the executable to be installed into the // 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`). // step when running `zig build`).
b.installArtifact(exe); 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 // 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 // step is evaluated that depends on it. The next line below will establish
// such a dependency. // such a dependency.
@@ -91,6 +86,8 @@ pub fn build(b: *std.Build) void {
.target = target, .target = target,
.optimize = optimize, .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); const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

View File

@@ -3,6 +3,12 @@
// version name should match the zig version except for the last number, // version name should match the zig version except for the last number,
// which stands for the version inside a given zig version // which stands for the version inside a given zig version
.version = "0.13.0", .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", .minimum_zig_version = "0.13.0",
.paths = .{ .paths = .{
"build.zig", "build.zig",

View File

@@ -1,5 +1,5 @@
const build_options = @import("build_options"); 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"); const std = @import("std");
pub const std_options: std.Options = .{ 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 { fn log_timestamp(writer: anytype) void {
const curtime = c_time.time(null); writer.print("[{any}] ", .{ztime.DateTime.now()}) catch return;
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;
} }
pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { pub fn pretty_format(object: anytype, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {