IO-as-an-interface refactor across the codebase. The big shifts: - std.io → std.Io, std.fs → std.Io.Dir/File, std.process.Child → spawn/run. - Juicy Main: pub fn main(init: std.process.Init) gives gpa, io, arena, environ_map up front. main.zig + the build/ scripts use it directly. - Threading io through everywhere that touches the outside world (HTTP, files, stderr, sleep, terminal detection). Functions taking `io` now announce side effects at the call site — the smell is the feature. - date math takes `as_of: Date`, not `today: Date`. Caller resolves `--as-of` flag vs wall-clock at the boundary; the function operates on whatever date it's given. Every "today" parameter renamed and the as_of: ?Date + today: Date pattern collapsed. - now_s: i64 (or before_s/after_s pairs) for sub-second metadata fields like snapshot captured_at, audit cadence, formatAge/fmtTimeAgo. Also pure and testable. - legitimate Timestamp.now callers (cache TTL math, FetchResult timestamps, rate limiter, per-frame TUI "now" captures) gain `// wall-clock required: ...` comments justifying the read. Test discovery: replaced the local refAllDeclsRecursive with bare std.testing.refAllDecls(@This()). Sema-pulling main.zig's top-level decls reaches every test file transitively through the import graph; no explicit _ = @import(...) lines needed. Cleanup along the way: - Dropped DataService.allocator()/io() accessor methods; renamed the fields to drop the base_ prefix. Callers use self.allocator and self.io directly. - Dropped now-vestigial io parameters from buildSnapshot, analyzePortfolio, compareSchwabSummary, compareAccounts, buildPortfolioData, divs.display, quote.display, parsePortfolioOpts, aggregateLiveStocks, renderEarningsLines, capitalGainsIndicator, aggregateDripLots, printLotRow, portfolio.display, printSnapNote. - Dropped the unused contributions.computeAttribution date-form wrapper (only computeAttributionSpec is called). - formatAge/fmtTimeAgo take (before_s, after_s) instead of io and reading the clock internally. - parseProjectionsConfig uses an internal stack-buffer FixedBufferAllocator instead of an allocator parameter. - ThreadSafeAllocator wrappers in cache concurrency tests dropped (0.16's DebugAllocator is thread-safe by default). - analyzePortfolio bug surfaced by the rename: snapshot.zig was passing wall-clock today instead of as_of, mis-valuing cash/CDs for historical backfills. 83 new unit tests added due to removal of IO, bringing coverage from 58% -> 64%
227 lines
8.7 KiB
Zig
227 lines
8.7 KiB
Zig
const std = @import("std");
|
|
const Coverage = @import("build/Coverage.zig");
|
|
|
|
pub fn build(b: *std.Build) void {
|
|
const target = b.standardTargetOptions(.{});
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
// External dependencies
|
|
const srf_dep = b.dependency("srf", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
|
|
const vaxis_dep = b.dependency("vaxis", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
|
|
const z2d_dep = b.dependency("z2d", .{
|
|
.target = target,
|
|
.optimize = optimize,
|
|
});
|
|
|
|
const srf_mod = srf_dep.module("srf");
|
|
|
|
const shiller_mod = b.addModule("shiller_year", .{
|
|
.root_source_file = b.path("src/models/shiller_year.zig"),
|
|
});
|
|
|
|
// Build-time info: version string (from git describe) and build timestamp.
|
|
// Exposed to application code as `@import("build_info")`.
|
|
//
|
|
// The version string is derived from `git describe --tags --always --dirty`
|
|
// so dev builds show the nearest tag plus commit hash + dirty flag. If git
|
|
// is unavailable (e.g. building from a source tarball), falls back to the
|
|
// `.version` in build.zig.zon.
|
|
const build_info = buildInfoOptions(b);
|
|
|
|
// Library module -- the public API for downstream consumers of zfin.
|
|
// Internal code (CLI, TUI) uses file-path imports instead.
|
|
_ = b.addModule("zfin", .{
|
|
.root_source_file = b.path("src/root.zig"),
|
|
.target = target,
|
|
.imports = &.{
|
|
.{ .name = "srf", .module = srf_mod },
|
|
.{ .name = "build_info", .module = build_info },
|
|
},
|
|
});
|
|
|
|
// Shared imports for the unified module (CLI + TUI + lib in one module).
|
|
// Only external deps -- internal imports use file paths so that Zig's
|
|
// test runner can discover tests across the entire source tree.
|
|
const imports: []const std.Build.Module.Import = &.{
|
|
.{ .name = "srf", .module = srf_mod },
|
|
.{ .name = "vaxis", .module = vaxis_dep.module("vaxis") },
|
|
.{ .name = "z2d", .module = z2d_dep.module("z2d") },
|
|
.{ .name = "build_info", .module = build_info },
|
|
.{ .name = "shiller_year", .module = shiller_mod },
|
|
};
|
|
|
|
// Generate Shiller annual returns data from ie_data.csv.
|
|
// Runs build/gen_shiller.zig as a native tool; outputs a .zig file
|
|
// that shiller.zig imports as a zero-cost const array.
|
|
const gen_shiller = b.addExecutable(.{
|
|
.name = "gen_shiller",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("build/gen_shiller.zig"),
|
|
.target = b.graph.host,
|
|
}),
|
|
});
|
|
gen_shiller.root_module.addImport("shiller", shiller_mod);
|
|
|
|
const gen_shiller_run = b.addRunArtifact(gen_shiller);
|
|
gen_shiller_run.addFileArg(b.path("src/data/ie_data.csv"));
|
|
const shiller_generated = gen_shiller_run.addOutputFileArg("shiller_generated.zig");
|
|
const shiller_generated_mod = b.addModule("shiller_generated", .{
|
|
.root_source_file = shiller_generated,
|
|
});
|
|
shiller_generated_mod.addImport("shiller", shiller_mod);
|
|
|
|
// Unified executable (CLI + TUI in one binary)
|
|
const exe = b.addExecutable(.{
|
|
.name = "zfin",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.imports = imports,
|
|
}),
|
|
});
|
|
exe.root_module.addImport("shiller_generated", shiller_generated_mod);
|
|
b.installArtifact(exe);
|
|
|
|
// Run step: `zig build run -- <args>`
|
|
const run_step = b.step("run", "Run the zfin CLI");
|
|
const run_cmd = b.addRunArtifact(exe);
|
|
run_step.dependOn(&run_cmd.step);
|
|
run_cmd.step.dependOn(b.getInstallStep());
|
|
if (b.args) |args| {
|
|
run_cmd.addArgs(args);
|
|
}
|
|
|
|
// Tests: single binary, single module. refAllDeclsRecursive in
|
|
// main.zig discovers all tests via file imports.
|
|
const test_step = b.step("test", "Run all tests");
|
|
const tests = b.addTest(.{ .root_module = b.createModule(.{
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.imports = imports,
|
|
}) });
|
|
tests.root_module.addImport("shiller_generated", shiller_generated_mod);
|
|
test_step.dependOn(&b.addRunArtifact(tests).step);
|
|
|
|
// Docs (still uses the library module for clean public API docs)
|
|
const lib = b.addLibrary(.{
|
|
.name = "zfin",
|
|
.root_module = b.createModule(.{
|
|
.root_source_file = b.path("src/root.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.imports = &.{
|
|
.{ .name = "srf", .module = srf_mod },
|
|
.{ .name = "build_info", .module = build_info },
|
|
},
|
|
}),
|
|
});
|
|
const docs_step = b.step("docs", "Generate documentation");
|
|
docs_step.dependOn(&b.addInstallDirectory(.{
|
|
.source_dir = lib.getEmittedDocs(),
|
|
.install_dir = .prefix,
|
|
.install_subdir = "docs",
|
|
}).step);
|
|
|
|
// Coverage: `zig build coverage` (uses kcov, Linux x86_64/aarch64 only)
|
|
{
|
|
var cov = Coverage.init(b);
|
|
const cov_mod = b.createModule(.{
|
|
.root_source_file = b.path("src/main.zig"),
|
|
.target = target,
|
|
.optimize = optimize,
|
|
.imports = imports,
|
|
});
|
|
cov_mod.addImport("shiller_generated", shiller_generated_mod);
|
|
_ = cov.addModule(cov_mod, "zfin");
|
|
}
|
|
}
|
|
|
|
/// Produce the `build_info` module exposing `version` (derived from `git
|
|
/// describe`) and `build_timestamp` (committer timestamp of HEAD).
|
|
/// Consumed as `@import("build_info")` from `src/version.zig`.
|
|
///
|
|
/// Both values are reproducible per-commit: `git describe` is stable
|
|
/// until a new commit/tag/dirty-flag flip, and the committer timestamp
|
|
/// comes from `git log -1 --format=%ct`. Neither varies with wall-clock
|
|
/// time, so the Options module doesn't rebuild every invocation, which
|
|
/// used to cascade into a full exe relink on every `zig build`.
|
|
///
|
|
/// Falls back to `fallbackVersion()` / `0` when git is unavailable
|
|
/// (source tarball, pre-commit environment).
|
|
fn buildInfoOptions(b: *std.Build) *std.Build.Module {
|
|
const opts = b.addOptions();
|
|
|
|
const version = gitDescribe(b) orelse fallbackVersion();
|
|
opts.addOption([]const u8, "version", version);
|
|
|
|
// Use HEAD's committer timestamp (reproducible) instead of
|
|
// `std.time.timestamp()` (changes every build). See comment above.
|
|
const timestamp = gitHeadTimestamp(b) orelse 0;
|
|
opts.addOption(i64, "build_timestamp", timestamp);
|
|
|
|
return opts.createModule();
|
|
}
|
|
|
|
/// Run `git describe --tags --always --dirty` in the repo root and return
|
|
/// the trimmed output. Returns null on any error (git missing, not a repo,
|
|
/// non-zero exit).
|
|
fn gitDescribe(b: *std.Build) ?[]const u8 {
|
|
return gitCapture(b, &.{ "git", "describe", "--tags", "--always", "--dirty" });
|
|
}
|
|
|
|
/// Run `git log -1 --format=%ct HEAD` and parse the stdout as an i64
|
|
/// (Unix seconds). Returns null on any error. Stable per-commit, which
|
|
/// keeps the Options module cache-friendly.
|
|
fn gitHeadTimestamp(b: *std.Build) ?i64 {
|
|
const text = gitCapture(b, &.{ "git", "log", "-1", "--format=%ct", "HEAD" }) orelse return null;
|
|
return std.fmt.parseInt(i64, text, 10) catch null;
|
|
}
|
|
|
|
/// Spawn a git subcommand in the repo root, capture stdout, trim and
|
|
/// dupe it through `b.allocator`. Returns null on any error (git
|
|
/// missing, non-zero exit, empty output).
|
|
fn gitCapture(b: *std.Build, argv: []const []const u8) ?[]const u8 {
|
|
const io = b.graph.io;
|
|
const result = std.process.run(b.allocator, io, .{
|
|
.argv = argv,
|
|
.cwd = .{ .path = b.build_root.path orelse "." },
|
|
}) catch return null;
|
|
defer b.allocator.free(result.stdout);
|
|
defer b.allocator.free(result.stderr);
|
|
|
|
switch (result.term) {
|
|
.exited => |code| if (code != 0) return null,
|
|
else => return null,
|
|
}
|
|
|
|
const trimmed = std.mem.trim(u8, result.stdout, " \t\r\n");
|
|
if (trimmed.len == 0) return null;
|
|
return b.dupe(trimmed);
|
|
}
|
|
|
|
/// Read `.version` from `build.zig.zon` as a fallback when git describe
|
|
/// fails (e.g. when building from a source tarball with no .git directory).
|
|
/// Returns a static string if even that fails.
|
|
fn fallbackVersion() []const u8 {
|
|
// `build.zig.zon` is embedded at compile time so the fallback never
|
|
// requires runtime filesystem access in the built binary — we only do
|
|
// this lookup at build time, on the build host.
|
|
const zon_contents = @embedFile("build.zig.zon");
|
|
if (std.mem.indexOf(u8, zon_contents, ".version = \"")) |start| {
|
|
const after = zon_contents[start + ".version = \"".len ..];
|
|
if (std.mem.indexOfScalar(u8, after, '"')) |end| {
|
|
return after[0..end];
|
|
}
|
|
}
|
|
return "unknown";
|
|
}
|