// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); /// The net-snmp tools this installs copies of itself as. One binary dispatches /// on the name it was invoked under, because these differ only in which PDU /// they send -- and because a differential test against net-snmp then means /// running two programs with an identical argument vector. const tool_names = [_][]const u8{ "snmpget", "snmpwalk", "snmpbulkwalk", "snmpset", "snmptrapd", }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // The Basic Encoding Rules, and the OBJECT IDENTIFIER type. Extracted // from zig-ldap, which speaks the same encoding for a different protocol. const asn1 = b.dependency("asn1", .{ .target = target, .optimize = optimize }); // The ciphers and modes std.crypto leaves out, which SNMPv3 privacy needs // three of: DES (std has none), AES-192 (std ships Aes128 and Aes256 and // nothing between), and CFB (std.crypto.modes has only counter mode). const crypto_ext = b.dependency("std_crypto_ext", .{ .target = target, .optimize = optimize }); // IP addresses, for the `IpAddress` application type and for reading a // peer off the command line. const z46 = b.dependency("z46", .{ .target = target, .optimize = optimize }); // MAC addresses, for rendering the `PhysAddress` and `MacAddress` // textual conventions -- `ifPhysAddress` being the one everybody asks // for. const macaddress = b.dependency("macaddress", .{ .target = target, .optimize = optimize }); // MIB modules, so that the tools can take `sysDescr.0` rather than // 1.3.6.1.2.1.1.1.0 and render a value through the display hint its // textual convention gives it. A compiler for a different language than // the protocol, which is why it is a repository of its own. const smi = b.dependency("smi", .{ .target = target, .optimize = optimize }); // One module. The BER layer is a dependency, the protocol types and the // client live here: splitting them would cost a consumer a dependency // edge to save nothing, since Zig only analyses what is referenced. const mod = b.addModule("snmp", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = &.{ .{ .name = "asn1", .module = asn1.module("asn1") }, .{ .name = "std_crypto_ext", .module = crypto_ext.module("std_crypto_ext") }, .{ .name = "z46", .module = z46.module("z46") }, .{ .name = "macaddress", .module = macaddress.module("macaddress") }, .{ .name = "smi", .module = smi.module("smi") }, }, }); const exe = b.addExecutable(.{ .name = "zig-snmp", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "snmp", .module = mod }}, }), }); b.installArtifact(exe); // And again under each of net-snmp's names. `main.zig` dispatches on // argv[0], so these are the same bytes installed several times rather // than several programs. for (tool_names) |name| { const install = b.addInstallBinFile(exe.getEmittedBin(), name); b.getInstallStep().dependOn(&install.step); } const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.stdio = .inherit; run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the command line client"); run_step.dependOn(&run_cmd.step); // A test executable covers one module, so each needs its own. Missing one // out would not fail: its tests would simply never run. // // Every one is compiled with LLVM rather than the self-hosted backend, // which emits no coverage instrumentation and no debug information kcov // can use: under it the fuzzer finds an empty table of program counters // and stops with "pcs_len was zero". `build.zig` cannot see whether // `--fuzz` was given, so this is on for plain test runs too. const test_step = b.step("test", "Run tests"); for ([_]*std.Build.Module{ mod, exe.root_module }) |m| { test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m, .use_llvm = true, })).step); } // Where net-snmp's agent lives, for the tests that drive a real one. // // Resolved here rather than in the test, because the child is spawned // with a deliberately minimal environment that has no PATH in it. The // devshell puts `snmpd` on PATH; if it is not there the option is null // and the oracle tests skip rather than fail, so `zig build test` still // works for somebody who has not got net-snmp. const options = b.addOptions(); options.addOption(?[]const u8, "snmpd", b.option( []const u8, "snmpd", "Path to net-snmp's snmpd, for the oracle tests (default: found on PATH)", ) orelse b.findProgram(.{ .names = &.{"snmpd"} })); // And net-snmp's notification sender, for the receiver's oracle tests. // Same reasoning as `snmpd`, and null means those tests skip. Only `snmptrap` is // needed -- `snmpinform` is a symlink to it that dispatches on argv[0], // and `-Ci` is the flag that does the same thing without depending on // the name surviving a spawn. options.addOption(?[]const u8, "snmptrap", b.findProgram(.{ .names = &.{"snmptrap"} })); // The oracle tests: our client against net-snmp's agent, spawned on a // loopback high port. `snmpd` needs no privilege to do that, which is // what lets this be an ordinary test rather than a virtual machine. const oracle_mod = b.createModule(.{ .root_source_file = b.path("tests/oracle.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "snmp", .module = mod }}, }); oracle_mod.addOptions("build_options", options); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = oracle_mod, .use_llvm = true, })).step); // The end-to-end tests drive a real client against an agent of their own // over a loopback socket, which is the only way to prove that what the // encoder writes is what the decoder reads with a kernel in between. const e2e_mod = b.createModule(.{ .root_source_file = b.path("tests/e2e.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "snmp", .module = mod }}, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = e2e_mod, .use_llvm = true, })).step); // The real device, which is opt-in and never runs in CI: it is one switch // on one private network, so the tests skip unless the environment names // it. See tests/device.zig. const device_mod = b.createModule(.{ .root_source_file = b.path("tests/device.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "snmp", .module = mod }}, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = device_mod, .use_llvm = true, })).step); // The fuzz targets: what the decoders must do with input nobody wrote. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "snmp", .module = mod }}, }); 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, .filters = if (fuzz_filter) |f| &.{f} else &.{}, .use_llvm = true, }); 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); const fuzz_run = b.addExecutable(.{ .name = "zig-snmp-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_mod }}, }), }); 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. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&fuzz_run.step); check_step.dependOn(&exe.step); // -- documentation ------------------------------------------------------- const library = b.addLibrary(.{ .name = "snmp", .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); 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"), .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(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); 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); test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); }