Something went wrong. Try again.
A TFTP server and client library for Zig 0.16: RFC 1350 with blksize, timeout and tsize options, netascii, fuzz-tested.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203// SPDX-FileCopyrightText: © 2022 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! Builds the `tftp` module, the demonstration server, the tests, the fuzz//! targets and the API documentation.
const std = @import("std");
/// Declares the `tftp` module and the `zig-tftp` executable, and the `run`,/// `test`, `fuzz`, `fuzz-run`, `check`, `docs` and `docs-serve` steps.pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const tftp = b.addModule("tftp", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, });
const exe = b.addExecutable(.{ .name = "zig-tftp", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "tftp", .module = tftp, }, }, }), });
// The demonstration server answers every read with the text of RFC 1350, // which lives outside `src/` and so has to be handed to it as an import. exe.root_module.addAnonymousImport("rfc1350.txt", .{ .root_source_file = b.path("doc/rfc1350.txt"), });
b.installArtifact(exe);
var test_step: *std.Build.Step = undefined; var check_step: *std.Build.Step = undefined;
{ const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe); run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addPassthruArgs(); }
{ const mod_tests = b.addTest(.{ .root_module = tftp, });
const run_mod_tests = b.addRunArtifact(mod_tests);
const exe_tests = b.addTest(.{ .root_module = exe.root_module, });
const run_exe_tests = b.addRunArtifact(exe_tests);
test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); test_step.dependOn(&run_exe_tests.step);
// The fuzz targets: what the packet decoder, the option decoder, the // netascii codecs and a whole transfer must do with input nobody // wrote. They are ordinary tests as well as fuzz tests, so `zig build // test` exercises the same properties on the seeds beside them. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "tftp", .module = tftp }}, }); // Zig's fuzzer takes one test at a time, so naming a target is what // you want when a finding is being chased: // `zig build fuzz --fuzz -Dfuzz-filter=netascii`. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, // The self-hosted backend, which Debug uses by default, emits no // coverage instrumentation, and without it the fuzzer ends with // "pcs_len was zero". LLVM does, at a few seconds of compile. .use_llvm = true, .filters = if (fuzz_filter) |f| &.{f} else &.{}, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// A step of its own for `zig build fuzz --fuzz`, which needs a run // step holding nothing else: the fuzzer takes over the terminal and // runs until it is stopped, so it must not be reached by `zig build // test`. const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// The loop that drives those same targets without Zig's fuzzer: // `tools/fuzz.zig` says what it is for. Optimized, because a fuzzer's whole job is how many inputs it gets // through, and ReleaseSafe keeps every check that makes a failure a // failure. Its stack traces are inlined past reading, though, so a // finding is best looked at again with `-Dfuzz-optimize=debug`. const fuzz_optimize = b.option( std.lang.Optimize, "fuzz-optimize", "How to build `zig build fuzz-run` (default safe)", ) orelse .safe; const fuzz_run = b.addExecutable(.{ .name = "zig-tftp-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = fuzz_optimize, .imports = &.{.{ .name = "fuzz_targets", .module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = b.graph.host, .optimize = fuzz_optimize, .imports = &.{.{ .name = "tftp", .module = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = b.graph.host, .optimize = fuzz_optimize, }) }}, }) }}, }), }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; run_fuzz.addPassthruArgs(); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step);
// Nothing else builds it, so without this it could stop compiling and // `zig build test` would not notice. check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&exe.step); check_step.dependOn(&fuzz_run.step); }
{ const docs_step = b.step("docs", "Build the API documentation into zig-out/docs");
const tftp_lib = b.addLibrary(.{ .name = "tftp", .root_module = tftp, });
const install_docs = b.addInstallDirectory(.{ .source_dir = tftp_lib.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", });
docs_step.dependOn(&install_docs.step);
// The documentation is a WebAssembly viewer that fetches its sources // at runtime, which a browser refuses to do from a `file://` page, so // reading it takes a server. 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 the machine running the build, never whatever // -Dtarget the library is being built for. .target = b.graph.host, .optimize = .debug, }), });
const run_docs_server = b.addRunArtifact(docs_server); run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(tftp_lib.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It 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);
// It has a test of its own; without this it would never run. And // nothing else builds it, so it belongs in `check` too, or it could // stop compiling and no test would notice. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); }}