Skip to content

Commit

Permalink
v0.0.14 - lazy dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
FObersteiner committed Jun 26, 2024
1 parent 06abf49 commit ac4b202
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -24,7 +40,7 @@ Options:
## Demo output
```shell
```sh
zig build run -- -z Europe/Berlin
```
Expand All @@ -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
Expand Down
70 changes: 42 additions & 28 deletions build.zig
Original file line number Diff line number Diff line change
@@ -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(.{
Expand Down
4 changes: 3 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -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,
},
},

Expand Down
4 changes: 4 additions & 0 deletions docs/change.log
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit ac4b202

Please sign in to comment.