Something went wrong. Try again.
A Zig library for astronomical calculations: the Sun and Moon from anywhere on Earth, the Moon's phases and libration, and time scales.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const mod = b.addModule("astro", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
const test_step = b.step("test", "Run tests"); const mod_tests = b.addTest(.{ .name = "astro-test", .root_module = mod, // The self-hosted backend, which Debug otherwise uses, emits no // coverage instrumentation and debug info kcov can read, so both the // fuzzer and `kcov` would report nothing at all without LLVM. .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(mod_tests).step);
const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&mod_tests.step);
// The tests of the public API: worked examples, reference tables and // properties. A module of their own, importing `astro` as a consumer // does, so that they cannot reach past the API into the internals. const api_tests = b.addTest(.{ .name = "astro-api-test", .root_module = b.createModule(.{ .root_source_file = b.path("tests/tests.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "astro", .module = mod }}, }), .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(api_tests).step); check_step.dependOn(&api_tests.step);
// The fuzz targets. They are ordinary tests as well, so `zig build test` // checks the same properties on the seeds beside them; `zig build fuzz // --fuzz` hands them to Zig's fuzzer, which takes over the terminal, so // that has a step of its own that `test` does not reach. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_tests = b.addTest(.{ .name = "astro-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "astro", .module = mod }}, }), // The fuzzer needs the coverage instrumentation only LLVM emits. .use_llvm = true, .filters = if (fuzz_filter) |f| &.{f} else &.{}, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); check_step.dependOn(&fuzz_tests.step);
const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// -- documentation ------------------------------------------------------- // // Zig emits the API documentation as a side effect of compiling, so the // module is built as a library purely to get at it. What comes out is not // a page but a program: a WebAssembly viewer, its javascript, and a tar of // the sources it reads from. const library = b.addLibrary(.{ .name = "astro", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step);
// That viewer fetches `sources.tar` and `main.wasm` at runtime, which a // browser refuses to do from a `file://` page, so reading the docs // locally means serving them. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000;
const docs_server = b.addExecutable(.{ .name = "docs-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/docs_server.zig"), // Always built for the machine running the build, never for // whatever -Dtarget the library is being built for. .target = b.graph.host, .optimize = .Debug, }), });
const run_docs_server = b.addRunArtifact(docs_server); // Served straight from the emitted directory, because the build does not // know the install prefix while it is being configured. Depending on the // install step keeps `docs-serve` leaving the same `zig-out/docs` behind // that `docs` does. run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // The server runs until interrupted, so its output has to reach the // terminal rather than being captured by the build runner. run_docs_server.stdio = .inherit;
const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step);
// The server has tests of its own; without this they would never run. // Nothing else builds it, so it belongs in `check` too. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step);}