// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); const version = @import("build.zig.zon").version; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // The rules of the game, with no platform in them. const rules = b.addModule("rules", .{ .root_source_file = b.path("src/rules/root.zig"), .target = target, .optimize = optimize, }); // The game on a screen, shared by every platform's shell. const view = b.addModule("view", .{ .root_source_file = b.path("src/view/root.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "rules", .module = rules }}, }); const rules_tests = b.addTest(.{ .name = "rules-test", .root_module = rules, // The self-hosted backend emits no coverage instrumentation, which // leaves both the fuzzer and kcov with nothing to go on. .use_llvm = true, }); const view_tests = b.addTest(.{ .name = "view-test", .root_module = view, .use_llvm = true, }); const test_step = b.step("test", "Run the tests"); test_step.dependOn(&b.addRunArtifact(rules_tests).step); test_step.dependOn(&b.addRunArtifact(view_tests).step); const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&rules_tests.step); check_step.dependOn(&view_tests.step); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("shaders/embed.zig"), .target = target, .optimize = optimize, }) })).step); // The shaders, compiled from Zig to SPIR-V. const shader = compileShaders(b); // Checked against what Vulkan 1.2 accepts, when spirv-val is on the // path: Zig's SPIR-V backend is young enough that what it emits is // worth checking. if (b.findProgram(.{ .names = &.{"spirv-val"} })) |spirv_val| { const validate = b.addSystemCommand(&.{ spirv_val, "--target-env", "vulkan1.2" }); validate.addFileArg(shader); validate.expectExitCode(0); test_step.dependOn(&validate.step); } // Tools that run on the machine doing the build. const apk_tool = b.addExecutable(.{ .name = "apk", .root_module = b.createModule(.{ .root_source_file = b.path("tools/apk.zig"), .target = b.graph.host, .optimize = .Debug, }), }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = apk_tool.root_module })).step); check_step.dependOn(&apk_tool.step); const apk_step = b.step("apk", "Build the Android package, zig-out/pfeilstorch.apk"); const install_apk_step = b.step("install-apk", "Install the Android package with adb"); const run_android_step = b.step("run-android", "Install the Android package, start it, and follow its log"); const sdk_option = b.option([]const u8, "android-sdk", "The Android SDK (default: $ANDROID_HOME)"); if (sdk_option orelse b.graph.environ_map.get("ANDROID_HOME")) |sdk| { const apk = addAndroid(b, optimize, sdk, shader, apk_tool, check_step); apk_step.dependOn(&b.addInstallFile(apk, "pfeilstorch.apk").step); const adb = b.pathJoin(&.{ sdk, "platform-tools", "adb" }); const install = b.addSystemCommand(&.{ adb, "install", "-r" }); install.addFileArg(apk); install_apk_step.dependOn(&install.step); // The log is cleared first, so that what is followed is this run. const clear = b.addSystemCommand(&.{ adb, "logcat", "-c" }); clear.step.dependOn(&install.step); const start = b.addSystemCommand(&.{ adb, "shell", "am", "start", "-W", "-n", android.package ++ "/android.app.NativeActivity" }); start.step.dependOn(&clear.step); const follow = b.addSystemCommand(&.{ adb, "logcat", "-v", "color", "-s", "pfeilstorch:V", "DEBUG:V", "AndroidRuntime:E" }); follow.step.dependOn(&start.step); follow.stdio = .inherit; run_android_step.dependOn(&follow.step); } else { const fail = &b.addFail("no Android SDK: run in `nix develop`, set ANDROID_HOME, or pass -Dandroid-sdk").step; apk_step.dependOn(fail); install_apk_step.dependOn(fail); run_android_step.dependOn(fail); } if (target.result.os.tag == .linux and !target.result.abi.isAndroid()) { if (addLinux(b, target, optimize, rules, view, shader)) |exe| { b.installArtifact(exe); check_step.dependOn(&exe.step); // What a desktop needs to list it and match its window to it, // which is named by the same app ID. b.installFile("dist/dev.jcollie.pfeilstorch.desktop", "share/applications/dev.jcollie.pfeilstorch.desktop"); b.installFile("dist/dev.jcollie.pfeilstorch.svg", "share/icons/hicolor/scalable/apps/dev.jcollie.pfeilstorch.svg"); const run = b.addRunArtifact(exe); run.addPassthruArgs(); b.step("run", "Play pfeilstorch").dependOn(&run.step); const linux_tests = b.addTest(.{ .name = "linux-test", .root_module = exe.root_module, .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(linux_tests).step); check_step.dependOn(&linux_tests.step); } } } /// The game on Linux: a Wayland window, through zig-wayland-native and /// zig-xkb, drawn with Vulkan. Null until the lazy dependencies are fetched. fn addLinux( b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, rules: *std.Build.Module, view: *std.Build.Module, shader: std.Build.LazyPath, ) ?*std.Build.Step.Compile { const wayland_build = b.lazyImport(@This(), "wayland") orelse return null; const wayland = b.lazyDependency("wayland", .{ .target = target, .optimize = optimize }) orelse return null; const xkb = b.lazyDependency("xkb", .{ .target = target, .optimize = optimize }) orelse return null; const struthio = b.lazyDependency("struthio", .{ .target = target, .optimize = optimize }) orelse return null; // D-Bus, for the desktop portal's accent color. const dbus_build = b.lazyImport(@This(), "dbus_service") orelse return null; const dbus_service = b.lazyDependency("dbus_service", .{ .target = target, .optimize = optimize }) orelse return null; const mdbus = b.lazyDependency("mdbus", .{ .target = target, .optimize = optimize }) orelse return null; const dbus_interfaces = dbus_build.addInterfaces(b, dbus_service, &.{ b.path("dbus/org.freedesktop.portal.Settings.xml"), }); const xml = wayland.namedLazyPath("wayland-protocols"); const wayland_protocols = wayland_build.addProtocols(b, wayland, .{ .target = target, .optimize = optimize, .extra = &.{ xml.path(b, "unstable/xdg-decoration/xdg-decoration-unstable-v1.xml"), xml.path(b, "stable/viewporter/viewporter.xml"), xml.path(b, "staging/fractional-scale/fractional-scale-v1.xml"), xml.path(b, "staging/cursor-shape/cursor-shape-v1.xml"), // cursor-shape names the tablet tool. xml.path(b, "stable/tablet/tablet-v2.xml"), xml.path(b, "stable/linux-dmabuf/linux-dmabuf-v1.xml"), xml.path(b, "staging/linux-drm-syncobj/linux-drm-syncobj-v1.xml"), }, }); // Vulkan, from the registry: generated at build time by vulkan-zig. const vulkan_headers = b.dependency("vulkan_headers", .{}); const vulkan = b.dependency("vulkan", .{ .registry = vulkan_headers.path("registry/vk.xml") }); const exe_mod = b.createModule(.{ .root_source_file = b.path("src/linux.zig"), .target = target, .optimize = optimize, // The Vulkan loader is opened with dlopen, and it and the driver it // loads need the C library in the process; see ADR 0001. .link_libc = true, .imports = &.{ .{ .name = "rules", .module = rules }, .{ .name = "view", .module = view }, .{ .name = "vk", .module = vulkan.module("vulkan-zig") }, .{ .name = "shaders", .module = shadersModule(b, shader) }, .{ .name = "wayland-client", .module = wayland.module("client") }, .{ .name = "wayland-protocols", .module = wayland_protocols }, .{ .name = "wayland-present", .module = wayland_build.addPresent(b, wayland, wayland_protocols) }, .{ .name = "xkb", .module = xkb.module("xkb") }, .{ .name = "struthio", .module = struthio.module("struthio") }, .{ .name = "dbus_service", .module = dbus_service.module("dbus_service") }, .{ .name = "dbus_interfaces", .module = dbus_interfaces }, .{ .name = "mdbus", .module = mdbus.module("mdbus") }, }, }); return b.addExecutable(.{ .name = "pfeilstorch", .root_module = exe_mod }); } /// What the Android build is made against; the SDK itself is pinned in /// nix/android-sdk.nix, and these name what is in it. const android = struct { const package = "dev.jcollie.pfeilstorch"; /// Android 10, the first to require Vulkan of a 64-bit device. const min_sdk = 29; const target_sdk = 37; const platform = "android-37.0"; const build_tools = "37.0.0"; const ndk = "29.0.14206865"; const abis = [_]struct { arch: std.Target.Cpu.Arch, name: []const u8, triple: []const u8 }{ .{ .arch = .aarch64, .name = "arm64-v8a", .triple = "aarch64-linux-android" }, .{ .arch = .x86_64, .name = "x86_64", .triple = "x86_64-linux-android" }, }; }; /// The game on Android: `libpfeilstorch.so` for each ABI, put in a package /// with the manifest and resources by `aapt2` and `tools/apk.zig`, and /// signed by `apksigner` (see ADR 0003). Returns the signed package. fn addAndroid( b: *std.Build, optimize: std.lang.Optimize, sdk: []const u8, shader: std.Build.LazyPath, apk_tool: *std.Build.Step.Compile, check_step: *std.Build.Step, ) std.Build.LazyPath { const build_tools = b.pathJoin(&.{ sdk, "build-tools", android.build_tools }); const sysroot = b.pathJoin(&.{ sdk, "ndk", android.ndk, "toolchains", "llvm", "prebuilt", "linux-x86_64", "sysroot" }); const vulkan_headers = b.dependency("vulkan_headers", .{}); const vulkan = b.dependency("vulkan", .{ .registry = vulkan_headers.path("registry/vk.xml") }); const shaders = shadersModule(b, shader); // The resources compiled, and linked with the manifest into a package // holding nothing else. const compile = b.addSystemCommand(&.{ b.pathJoin(&.{ build_tools, "aapt2" }), "compile", "-o" }); const resources = compile.addOutputFileArg("resources.zip"); compile.addArg("--dir"); compile.addDirectoryArg(b.path("android/res")); const link = b.addSystemCommand(&.{ b.pathJoin(&.{ build_tools, "aapt2" }), "link", "-o" }); const base = link.addOutputFileArg("base.apk"); link.addArgs(&.{ "-I", b.pathJoin(&.{ sdk, "platforms", android.platform, "android.jar" }) }); link.addArg("--manifest"); link.addFileArg(b.path("android/AndroidManifest.xml")); link.addArgs(&.{ "--min-sdk-version", b.fmt("{d}", .{android.min_sdk}), "--target-sdk-version", b.fmt("{d}", .{android.target_sdk}), "--version-code", b.fmt("{d}", .{versionCode()}), "--version-name", version, }); link.addFileArg(resources); // The libraries added to it. const pack = b.addRunArtifact(apk_tool); const unsigned = pack.addOutputFileArg("unsigned.apk"); pack.addFileArg(base); for (android.abis) |abi| { const target = b.resolveTargetQuery(.{ .cpu_arch = abi.arch, .os_tag = .linux, .abi = .android, .android_api_level = android.min_sdk, }); const lib_dir = b.pathJoin(&.{ sysroot, "usr", "lib", abi.triple, b.fmt("{d}", .{android.min_sdk}) }); // Where Zig finds Bionic, Android's C library, in the NDK. const libc = b.addWriteFiles().add(b.fmt("libc-{s}.txt", .{abi.name}), b.fmt( \\include_dir={s}/usr/include \\sys_include_dir={s}/usr/include/{s} \\crt_dir={s} \\cc_dir={s} \\msvc_lib_dir= \\kernel32_lib_dir= \\darwin_sdk_dir= \\ , .{ sysroot, sysroot, abi.triple, lib_dir, lib_dir })); const rules = b.createModule(.{ .root_source_file = b.path("src/rules/root.zig"), .target = target, .optimize = optimize, }); const view = b.createModule(.{ .root_source_file = b.path("src/view/root.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "rules", .module = rules }}, }); const mod = b.createModule(.{ .root_source_file = b.path("src/android.zig"), .target = target, .optimize = optimize, // Bionic, and the NDK's system libraries; see ADR 0001. .link_libc = true, // Debug information is most of the size of a release build, and // no use in a package. .strip = optimize != .debug, .imports = &.{ .{ .name = "rules", .module = rules }, .{ .name = "view", .module = view }, .{ .name = "vk", .module = vulkan.module("vulkan-zig") }, .{ .name = "shaders", .module = shaders }, }, }); mod.addLibraryPath(.{ .cwd_relative = lib_dir }); mod.linkSystemLibrary("android", .{}); mod.linkSystemLibrary("log", .{}); mod.linkSystemLibrary("vulkan", .{}); const lib = b.addLibrary(.{ .name = "pfeilstorch", .linkage = .dynamic, .root_module = mod }); lib.setLibCFile(libc); check_step.dependOn(&lib.step); pack.addPrefixedFileArg(b.fmt("lib/{s}/libpfeilstorch.so=", .{abi.name}), lib.getEmittedBin()); } // And signed. const sign = b.addSystemCommand(&.{ b.pathJoin(&.{ build_tools, "apksigner" }), "sign", "--ks-pass", "pass:android", "--key-pass", "pass:android", "--ks-key-alias", "androiddebugkey", // The v4 scheme's signature is a file beside the package, for // streaming installs; adb does not need it. "--v4-signing-enabled", "false", "--ks", }); sign.addFileArg(keystore(b)); sign.addArg("--out"); const signed = sign.addOutputFileArg("pfeilstorch.apk"); sign.addFileArg(unsigned); sign.expectExitCode(0); return signed; } /// The key the package is signed with: `-Dkeystore`, or the debug keystore /// Android's own tools make in `~/.android`, or failing both, a key made /// for this build alone. A package signed with that last cannot be /// installed over one signed with another, so it is good for a build that /// is only checked, as CI's is, and not for one installed again and again. fn keystore(b: *std.Build) std.Build.LazyPath { if (b.option([]const u8, "keystore", "The keystore to sign the Android package with (default: ~/.android/debug.keystore)")) |path| return .{ .cwd_relative = path }; if (b.graph.environ_map.get("HOME")) |home| { const path = b.pathJoin(&.{ home, ".android", "debug.keystore" }); if (std.Io.Dir.cwd().access(b.graph.io, path, .{})) |_| return .{ .cwd_relative = path } else |_| {} } const make = b.addSystemCommand(&.{ "keytool", "-genkeypair", "-keystore" }); const made = make.addOutputFileArg("throwaway.keystore"); make.addArgs(&.{ "-storepass", "android", "-keypass", "android", "-alias", "androiddebugkey", "-keyalg", "RSA", "-keysize", "2048", "-validity", "10000", "-dname", "CN=Android Debug,O=Android,C=US", }); // It says what it is doing on standard error, which is not a failure. make.expectExitCode(0); return made; } /// The Android version code, which has to go up from one release to the /// next: the version's numbers in three digits each, and at least 1. fn versionCode() u32 { const v = std.SemanticVersion.parse(version) catch @panic("build.zig.zon: .version is not a semantic version"); return @max(1, @as(u32, @intCast(v.major * 1_000_000 + v.minor * 1_000 + v.patch))); } /// The shaders, written in Zig and compiled by Zig's own SPIR-V backend: /// one module holding both entry points. spirv32 rather than spirv64, so /// that the module uses the Logical memory model and needs no 64-bit /// integers or buffer device addresses. fn compileShaders(b: *std.Build) std.Build.LazyPath { const shader = b.addObject(.{ .name = "shapes", .root_module = b.createModule(.{ .root_source_file = b.path("shaders/shapes.zig"), .target = b.resolveTargetQuery(.{ .cpu_arch = .spirv32, .os_tag = .vulkan, .cpu_model = .{ .explicit = &std.Target.spirv.cpu.vulkan_v1_2 }, }), .optimize = .ReleaseFast, }), .use_llvm = false, }); return shader.getEmittedBin(); } /// The compiled shaders, embedded. fn shadersModule(b: *std.Build, shader: std.Build.LazyPath) *std.Build.Module { const files = b.addWriteFiles(); _ = files.addCopyFile(shader, "shapes.spv"); _ = files.addCopyFile(b.path("shaders/embed.zig"), "embed.zig"); const source = files.add("shaders.zig", \\pub const shapes align(4) = @import("embed.zig").unsourced(@embedFile("shapes.spv")); \\ ); return b.createModule(.{ .root_source_file = source }); }