Something went wrong. Try again.
A Zig library for driving Elgato Stream Deck devices over USB HID.
Something went wrong. Try again.
zig-streamdeck build.zig
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { checkEveryFileIsTested(b);
const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const hidapi_dep = b.dependency("hidapi", .{ .target = target, .optimize = optimize, });
const z2d_dep = b.dependency("z2d", .{ .target = target, .optimize = optimize, });
const z2dimg_dep = b.dependency("z2dimg", .{ .target = target, .optimize = optimize, });
const streamdeck_mod = b.addModule("streamdeck", .{ .root_source_file = b.path("src/root.zig"), .link_libc = true, .target = target, .optimize = optimize, .imports = &.{ .{ .name = "hidapi", .module = hidapi_dep.module("hidapi"), }, .{ .name = "z2d", .module = z2d_dep.module("z2d"), }, .{ .name = "z2dimg", .module = z2dimg_dep.module("z2dimg"), }, }, });
// `src/udev.zig` generates the udev rules from the device table and // has a test that reads `nix/module.nix` to check the module has not // drifted from them. `@embedFile` cannot reach outside the module's // own directory, so the file is handed in here under a name. streamdeck_mod.addAnonymousImport("nixos-module.nix", .{ .root_source_file = b.path("nix/module.nix"), });
const exe = b.addExecutable(.{ .name = "zig-streamdeck", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "streamdeck", .module = streamdeck_mod, }, }, }), });
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| { run_cmd.addArgs(args); }
const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step);
// A tool for asking attached hardware what it says about itself, // so that the device tables can be checked against a device rather // than against another implementation. const probe = b.addExecutable(.{ .name = "probe", .root_module = b.createModule(.{ .root_source_file = b.path("tools/probe.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "streamdeck", .module = streamdeck_mod }, .{ .name = "z2d", .module = z2d_dep.module("z2d") }, }, }), });
const run_probe = b.addRunArtifact(probe); if (b.args) |args| run_probe.addArgs(args); run_probe.stdio = .inherit; const probe_step = b.step("probe", "Ask every attached device what it says about itself"); probe_step.dependOn(&run_probe.step);
// The udev rules for every model, printed from the same table the // library drives the devices with. `nix/module.nix` carries what // this prints; a test in `src/udev.zig` checks that it still does. const udev = b.addExecutable(.{ .name = "udev", .root_module = b.createModule(.{ .root_source_file = b.path("tools/udev.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{ .{ .name = "streamdeck", .module = streamdeck_mod }, }, }), });
const run_udev = b.addRunArtifact(udev); if (b.args) |args| run_udev.addArgs(args); run_udev.stdio = .inherit; const udev_step = b.step("udev", "Print the udev rules for every model this library knows"); udev_step.dependOn(&run_udev.step);
const test_step = b.step("test", "Run unit tests");
// The library's own tests. `root.zig` names every file in the // module so that their tests end up in this binary; without that a // file nothing imports contributes none of them. const mod_unit_tests = b.addTest(.{ .root_module = streamdeck_mod, });
test_step.dependOn(&b.addRunArtifact(mod_unit_tests).step);
const exe_unit_tests = b.addTest(.{ .root_module = exe.root_module, });
test_step.dependOn(&b.addRunArtifact(exe_unit_tests).step);
// The fuzz targets. They are a module of their own rather than part // of the library, because they are reached from two places: the // test runner, which runs each of them over the corpus so that they // keep compiling and a known input that once failed keeps being // tried, and `tools/fuzz.zig`, which generates inputs. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "streamdeck", .module = streamdeck_mod }, }, });
const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
const fuzz_driver = b.addExecutable(.{ .name = "fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "fuzz", .module = fuzz_mod }, }, }), });
const run_fuzz = b.addRunArtifact(fuzz_driver); run_fuzz.stdio = .inherit; if (b.args) |args| run_fuzz.addArgs(args);
const fuzz_step = b.step("fuzz", "Generate inputs and run them through the fuzz targets"); fuzz_step.dependOn(&run_fuzz.step);
// Nothing else builds the driver, so without this it could stop // compiling and no test would notice. The same goes for the probe // and the udev rule printer. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = fuzz_driver.root_module }), ).step); test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = probe.root_module }), ).step); test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = udev.root_module }), ).step);}
/// Fail the build if a file under `src/` is not named in `root.zig`'s/// test block.////// Zig does not analyze a function nothing calls, and it does not run/// the tests of a file nothing imports. Between them, a file can sit in/// the tree for months quietly not compiling -- which is exactly what/// had happened to `Touchscreen.zig`. `root.zig` lists every file for/// that reason, and a list written by hand is a list that gets/// forgotten, so this checks it.fn checkEveryFileIsTested(b: *std.Build) void { const io = b.graph.io;
const root = b.build_root.handle.readFileAlloc( io, "src/root.zig", b.allocator, .limited(1 << 20), ) catch |err| std.debug.panic("unable to read src/root.zig: {t}", .{err});
var dir = b.build_root.handle.openDir(io, "src", .{ .iterate = true }) catch |err| std.debug.panic("unable to open src/: {t}", .{err}); defer dir.close(io);
var walker = dir.walk(b.allocator) catch @panic("OOM"); defer walker.deinit();
var missing: std.ArrayList([]const u8) = .empty;
while (walker.next(io) catch |err| std.debug.panic("unable to walk src/: {t}", .{err})) |entry| { if (entry.kind != .file) continue; if (!std.mem.endsWith(u8, entry.basename, ".zig")) continue;
// `root.zig` is the file holding the list, and `main.zig` is // the example's root rather than part of the library. if (std.mem.eql(u8, entry.path, "root.zig")) continue; if (std.mem.eql(u8, entry.path, "main.zig")) continue;
// The device modules are reached through `devices.zig`, whose // own tests check that every one of them is listed there. if (std.mem.startsWith(u8, entry.path, "devices/")) continue;
const needle = b.fmt("@import(\"{s}\")", .{entry.basename}); if (std.mem.indexOf(u8, root, needle) == null) { missing.append(b.allocator, b.dupe(entry.path)) catch @panic("OOM"); } }
if (missing.items.len == 0) return;
std.debug.print( \\ \\src/root.zig does not name these files in its test block, so their \\tests will not run and nothing will check the parts of them that no \\other file calls. Add them to it: \\ \\ , .{}); for (missing.items) |path| { std.debug.print(" _ = @import(\"{s}\");\n", .{path}); } std.debug.print("\n", .{}); std.process.exit(1);}