diff --git a/README.md b/README.md index 5abb6ec..e6f0e55 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,23 @@ Command line app to query an NTP server, to verify your OS clock setting. - [on Codeberg](https://codeberg.org/FObersteiner/ntp_client) - [on github](https://github.com/FObersteiner/ntp-client) -```text +## Usage + +### Building the binary + +```sh +zig build -Dexe [--release=[safe|small|fast]] +# build and run, debug: zig build -Dexe run +# library tests: zig build test +``` + +### NTP library + +NTP library (`src/ntp.zig`) can be used independently in other projects; it is exposed via this project's `build.zig` and `build.zig.zon` files. Other dependencies of the binary are lazy, i.e. they won't be fetched if you use only the library in another project. + +### Usage of the binary + +```sh Usage: ntp_client [options] Options: @@ -24,7 +40,7 @@ Options: ## Demo output -```shell +```sh zig build run -- -z Europe/Berlin ``` @@ -51,7 +67,7 @@ Round-trip delay: 0.077 s (76970 us) ## Compatibility and Requirements -Developed & tested on Linux (Debian, on an x86 machine). Windows should work (build.zig links libc for this), Mac OS might work (can't test this). +Developed & tested on Linux (Debian, on an x86 machine). Windows worked last time I tested (build.zig links libc for this), Mac OS might work (can't test this). ## Zig version diff --git a/build.zig b/build.zig index de47cb8..155f4fc 100644 --- a/build.zig +++ b/build.zig @@ -1,59 +1,73 @@ const std = @import("std"); const log = std.log.scoped(.ntp_client_build); -const client_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 13 }; +const client_version = std.SemanticVersion{ .major = 0, .minor = 0, .patch = 14 }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + // -Dexe option is required to build the executable. + // This avoids leaking dependencies, if another project wants to use + // ntp.zig as a library. + const build_exe = b.option(bool, "exe", "build executable"); + // expose ntp.zig as a library _ = b.addModule("ntp_client", .{ .root_source_file = b.path("src/ntp.zig"), }); - const flags = b.dependency("flags", .{}); - const flags_module = flags.module("flags"); - - const zdt = b.dependency("zdt", .{ - // use system zoneinfo: - // .prefix_tzdb = @as([]const u8, "/usr/share/zoneinfo"), - }); - const zdt_module = zdt.module("zdt"); - - const exe = b.addExecutable(.{ - .name = "ntp_client", - .root_source_file = b.path("src/main.zig"), - .target = target, - .optimize = optimize, - .version = client_version, - }); + if (build_exe) |_| { + const exe = b.addExecutable(.{ + .name = "ntp_client", + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .version = client_version, + }); - b.installArtifact(exe); + b.installArtifact(exe); - // for Windows compatibility, required by sockets functionality - exe.linkLibC(); + // for Windows compatibility, required by sockets functionality + exe.linkLibC(); - exe.root_module.addImport("flags", flags_module); - exe.root_module.addImport("zdt", zdt_module); + // using lazy dependencies here so that another project can + // use the NTP lib without having to fetch flags and zdt + if (b.lazyDependency("flags", .{ + .optimize = optimize, + .target = target, + })) |dep| { + exe.root_module.addImport("flags", dep.module("flags")); + } + if (b.lazyDependency("zdt", .{ + .optimize = optimize, + .target = target, + // use system zoneinfo: + // .prefix_tzdb = @as([]const u8, "/usr/share/zoneinfo"), + })) |dep| { + exe.root_module.addImport("zdt", dep.module("zdt")); + } - const run_cmd = b.addRunArtifact(exe); - run_cmd.step.dependOn(b.getInstallStep()); + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); - if (b.args) |args| run_cmd.addArgs(args); + if (b.args) |args| run_cmd.addArgs(args); - const run_step = b.step("run", "Run the app"); - run_step.dependOn(&run_cmd.step); + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); + } + // run unit tests from ntp.zig, which on its own has no dependencies const unit_tests = b.addTest(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, }); const run_unit_tests = b.addRunArtifact(unit_tests); - // run_unit_tests.has_side_effects = true; const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step); + // autodocs step excluded since not really useful as NTP lib is currently + // pretty small. // const docs_step = b.step("docs", "auto-generate documentation"); // { // const install_docs = b.addInstallDirectory(.{ diff --git a/build.zig.zon b/build.zig.zon index d9fc652..67a2475 100755 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,14 +1,16 @@ .{ .name = "ntp_client", - .version = "0.0.13", + .version = "0.0.14", .dependencies = .{ .zdt = .{ .url = "https://codeberg.org/FObersteiner/zdt/archive/v0.1.4.tar.gz", .hash = "122061d2677118a50e5692507e61e3a72189eb6ad08d7e90f699876f561627066677", + .lazy = true, }, .flags = .{ .url = "https://github.com/n0s4/flags/archive/refs/tags/v0.5.0.tar.gz", .hash = "12202e9d5de187569b77064c66a4972e8a824488295fab2f5c8cb48331eab9877257", + .lazy = true, }, }, diff --git a/docs/change.log b/docs/change.log index ee079bb..8086b4c 100644 --- a/docs/change.log +++ b/docs/change.log @@ -1,5 +1,9 @@ # Changelog +## 2024-06-26, v0.0.14 + +- use lazy dependencies so that another project can use ntp.zig without having to fetch the dependencies of this project + ## 2024-06-24, v0.0.13 - use parseIp instead of resolveIp, avoids "std.net.if_nametoindex unimplemented for this OS" error on specific OS (thanks @part1zano on codeberg)