const std = @import("std"); fn resolveHeaderRoot(io: std.Io, allocator: std.mem.Allocator, source_dir: []const u8) ![]const u8 { const include_sdl3 = try std.fs.path.join(allocator, &.{ source_dir, "include", "SDL3" }); defer allocator.free(include_sdl3); if (std.Io.Dir.cwd().access(io, include_sdl3, .{})) { return try allocator.dupe(u8, "include/SDL3"); } else |_| {} const sdl3_dir = try std.fs.path.join(allocator, &.{ source_dir, "SDL3" }); defer allocator.free(sdl3_dir); if (std.Io.Dir.cwd().access(io, sdl3_dir, .{})) { return try allocator.dupe(u8, "SDL3"); } else |_| {} return try allocator.dupe(u8, "."); } fn addFetchSdlStep(b: *std.Build, helper_exe: *std.Build.Step.Compile) *std.Build.Step { const default_sdl_url = "git@github.com:castholm/SDL.git"; const official_sdl_url = "git@github.com:libsdl-org/SDL.git"; const sdl_url_option = b.option([]const u8, "sdl-url", "URL to fetch SDL3 from (use 'official' for libsdl-org/SDL)") orelse default_sdl_url; const sdl_url = if (std.mem.eql(u8, sdl_url_option, "official") or b.option(bool, "official", "use the official sdl path of git@github.com:libsdl-org/SDL.git (same as setting -Dsdl-url=official)") != null) official_sdl_url else sdl_url_option; const clean_sdl = b.option(bool, "clean", "Delete sdl3/ directory before fetching") orelse false; const sdl_checkout = b.option([]const u8, "ref", "the git ref to check out after fetching sdl, this can be tag, commit, branch..."); const fetch_step = b.step("fetch-sdl", "Fetch SDL3 source from git"); // The build graph depends on whether sdl3/ exists right now, which the // configuration cache cannot track. b.graph.poisonCache(); const sdl_exists = if (std.Io.Dir.cwd().access(b.graph.io, "sdl3", .{})) true else |_| false; var end_step: *std.Build.Step = undefined; if (clean_sdl) { const remove_dir = addHelperStep(b, helper_exe, "rmtree sdl3", &.{ "rmtree", "sdl3" }); const fetch_sdl = b.addSystemCommand(&.{ "git", "clone", sdl_url, "sdl3" }); fetch_sdl.step.dependOn(remove_dir); end_step = &fetch_sdl.step; // fetch_step.dependOn(&fetch_sdl.step); } else if (sdl_exists) { const echo_msg = b.addSystemCommand(&.{ "echo", "sdl already fetched" }); end_step = &echo_msg.step; // fetch_step.dependOn(&echo_msg.step); } else { const fetch_sdl = b.addSystemCommand(&.{ "git", "clone", sdl_url, "sdl3" }); end_step = &fetch_sdl.step; } const checkout_step = if (sdl_checkout) |ref| &b.addSystemCommand(&.{ "git", "-C", "sdl3", "checkout", ref }).step else end_step; if (sdl_checkout != null) { checkout_step.dependOn(end_step); } fetch_step.dependOn(checkout_step); return fetch_step; } /// Runs the build helper (src/build_helper.zig) with the given arguments. /// Used for filesystem work that has to happen at make time. fn addHelperStep(b: *std.Build, helper_exe: *std.Build.Step.Compile, name: []const u8, args: []const []const u8) *std.Build.Step { const run = b.addRunArtifact(helper_exe); run.addArgs(args); run.has_side_effects = true; run.setName(name); return &run.step; } pub fn generateApi(b: *std.Build, parser_exe: *std.Build.Step.Compile, helper_exe: *std.Build.Step.Compile, fetch_sdl_step: *std.Build.Step) void { const source_dir = b.option([]const u8, "sourceDir", "Parse SDL headers from an existing local directory instead of fetching git"); const output_dir = b.option([]const u8, "outputDir", "Directory where generated api/ and json/ folders should be written"); const basedir = b.option([]const u8, "basedir", "Working directory for the parser to execute in"); const c_import_path = b.option([]const u8, "cImportPath", "Path used by generated files when importing c.zig"); const effective_output_dir = output_dir orelse source_dir; const header_root_suffix = if (source_dir) |dir| resolveHeaderRoot(b.graph.io, b.allocator, dir) catch @panic("OOM") else null; // Archive existing json/ and api/ directories before regenerating const archive_step = addHelperStep( b, helper_exe, "archive generate outputs", if (effective_output_dir) |dir| &.{ "archive", dir } else &.{"archive"}, ); // Generation needs the previous outputs archived and, unless a local // source directory is used, SDL fetched. `fetch-sdl` on its own must not // touch the generated outputs. const generation_root = if (source_dir != null) archive_step else fetch_sdl_step; // Write a build marker file after the source selection step to enable caching. // The timestamp is taken at configure time, so the configuration must not be cached. b.graph.poisonCache(); const timestamp = std.Io.Clock.real.now(b.graph.io).toSeconds(); const wf = b.addWriteFiles(); const marker_file = wf.add(".buildmarker", b.fmt("{d}", .{timestamp})); _ = marker_file; wf.step.dependOn(generation_root); wf.step.dependOn(archive_step); var path_prep_step: *std.Build.Step = &wf.step; if (effective_output_dir) |dir| { const output_dir_step = addHelperStep(b, helper_exe, b.fmt("mkdir {s}", .{dir}), &.{ "mkdir", dir }); output_dir_step.dependOn(path_prep_step); path_prep_step = output_dir_step; } if (basedir) |dir| { const basedir_step = addHelperStep(b, helper_exe, b.fmt("mkdir {s}", .{dir}), &.{ "mkdir", dir }); basedir_step.dependOn(path_prep_step); path_prep_step = basedir_step; } const output_root = if (effective_output_dir) |dir| b.fmt("{s}/api", .{dir}) else "api"; const json_root = if (effective_output_dir) |dir| b.fmt("{s}/json", .{dir}) else "json"; const effective_c_import_path = c_import_path orelse "c.zig"; const c_bridge_target = if (c_import_path) |path| path else "../c.zig"; const c_bridge_path = b.fmt("{s}/c.zig", .{output_root}); const c_bridge_step = addHelperStep(b, helper_exe, b.fmt("write {s}", .{c_bridge_path}), &.{ "write", c_bridge_path, b.fmt("pub const c = @import(\"{s}\").c;\n", .{c_bridge_target}), }); c_bridge_step.dependOn(path_prep_step); path_prep_step = c_bridge_step; const stdinc_bridge_path = b.fmt("{s}/stdinc.zig", .{output_root}); const stdinc_bridge_step = addHelperStep(b, helper_exe, b.fmt("write {s}", .{stdinc_bridge_path}), &.{ "write", stdinc_bridge_path, \\pub const c = @import("c.zig").c; \\pub const Time = i64; \\pub const FunctionPointer = c.SDL_FunctionPointer; \\ }); stdinc_bridge_step.dependOn(path_prep_step); path_prep_step = stdinc_bridge_step; // All public SDL3 API headers const headers_to_generate = [_]struct { header: []const u8, output: []const u8 }{ // .{ .header = "SDL_asyncio.h", .output = "asyncio" }, // .{ .header = "SDL_atomic.h", .output = "atomic" }, .{ .header = "SDL_audio.h", .output = "audio" }, .{ .header = "SDL_blendmode.h", .output = "blendmode" }, .{ .header = "SDL_camera.h", .output = "camera" }, .{ .header = "SDL_clipboard.h", .output = "clipboard" }, // .{ .header = "SDL_cpuinfo.h", .output = "cpuinfo" }, .{ .header = "SDL_dialog.h", .output = "dialog" }, .{ .header = "SDL_endian.h", .output = "endian" }, .{ .header = "SDL_error.h", .output = "error" }, .{ .header = "SDL_events.h", .output = "events" }, .{ .header = "SDL_filesystem.h", .output = "filesystem" }, .{ .header = "SDL_gamepad.h", .output = "gamepad" }, .{ .header = "SDL_gpu.h", .output = "gpu" }, .{ .header = "SDL_guid.h", .output = "guid" }, .{ .header = "SDL_haptic.h", .output = "haptic" }, // .{ .header = "SDL_hidapi.h", .output = "hidapi" }, // Skipped: not core API .{ .header = "SDL_hints.h", .output = "hints" }, .{ .header = "SDL_init.h", .output = "init" }, .{ .header = "SDL_iostream.h", .output = "iostream" }, .{ .header = "SDL_joystick.h", .output = "joystick" }, .{ .header = "SDL_keyboard.h", .output = "keyboard" }, .{ .header = "SDL_keycode.h", .output = "keycode" }, .{ .header = "SDL_loadso.h", .output = "loadso" }, // .{ .header = "SDL_locale.h", .output = "locale" }, // .{ .header = "SDL_log.h", .output = "log" }, .{ .header = "SDL_messagebox.h", .output = "messagebox" }, // .{ .header = "SDL_metal.h", .output = "metal" }, .{ .header = "SDL_misc.h", .output = "misc" }, .{ .header = "SDL_mouse.h", .output = "mouse" }, // .{ .header = "SDL_mutex.h", .output = "mutex" }, // Skipped: not core API .{ .header = "SDL_opengl.h", .output = "opengl" }, .{ .header = "SDL_pen.h", .output = "pen" }, .{ .header = "SDL_pixels.h", .output = "pixels" }, .{ .header = "SDL_power.h", .output = "power" }, .{ .header = "SDL_process.h", .output = "process" }, .{ .header = "SDL_properties.h", .output = "properties" }, .{ .header = "SDL_rect.h", .output = "rect" }, .{ .header = "SDL_render.h", .output = "render" }, .{ .header = "SDL_scancode.h", .output = "scancode" }, .{ .header = "SDL_sensor.h", .output = "sensor" }, .{ .header = "SDL_storage.h", .output = "storage" }, .{ .header = "SDL_surface.h", .output = "surface" }, .{ .header = "SDL_system.h", .output = "system" }, // .{ .header = "SDL_thread.h", .output = "thread" }, // Skipped: not core API .{ .header = "SDL_time.h", .output = "time" }, .{ .header = "SDL_timer.h", .output = "timer" }, .{ .header = "SDL_touch.h", .output = "touch" }, .{ .header = "SDL_tray.h", .output = "tray" }, // Skipped: not core API .{ .header = "SDL_version.h", .output = "version" }, .{ .header = "SDL_video.h", .output = "video" }, // .{ .header = "SDL_vulkan.h", .output = "vulkan" }, // Skipped: Vulkan interop }; const regenerate_step = b.step("generate", "Regenerate bindings from SDL headers"); const header_root = if (source_dir) |dir| b.fmt("{s}/{s}", .{ dir, header_root_suffix.? }) else "sdl3/include/SDL3"; const timestamp_arg = b.fmt("--timestamp={d}", .{timestamp}); for (headers_to_generate) |header_info| { const regenerate = b.addRunArtifact(parser_exe); regenerate.addArg(b.fmt("{s}/{s}", .{ header_root, header_info.header })); regenerate.addArg(b.fmt("--output={s}/{s}.zig", .{ output_root, header_info.output })); regenerate.addArg(timestamp_arg); if (basedir) |dir| { regenerate.addArg(b.fmt("--basedir={s}", .{dir})); } regenerate.addArg(b.fmt("--c-import={s}", .{effective_c_import_path})); regenerate.addArg("--skip-missing"); // regenerate.addArg(b.fmt("--output=api/{s}.zig --mocks=mocks/{s}.c", .{ header_info.output, header_info.output })); regenerate.step.dependOn(path_prep_step); regenerate_step.dependOn(®enerate.step); const regenerateJson = b.addRunArtifact(parser_exe); regenerateJson.addArg(b.fmt("{s}/{s}", .{ header_root, header_info.header })); regenerateJson.addArg(b.fmt("--generate-json={s}/{s}.json", .{ json_root, header_info.output })); regenerateJson.addArg(timestamp_arg); regenerateJson.addArg("--skip-missing"); if (basedir) |dir| { regenerateJson.addArg(b.fmt("--basedir={s}", .{dir})); } regenerateJson.step.dependOn(path_prep_step); regenerate_step.dependOn(®enerateJson.step); } } pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // Helper executable for make-time filesystem work (always built for the host). const helper_exe = b.addExecutable(.{ .name = "build-helper", .root_module = b.createModule(.{ .root_source_file = b.path("src/build_helper.zig"), .target = b.graph.host, .optimize = .Debug, }), }); const fetch_sdl_step = addFetchSdlStep(b, helper_exe); // Parser executable const parser_exe = b.addExecutable(.{ .name = "sdl-parser", .root_module = b.createModule(.{ .root_source_file = b.path("src/parser.zig"), .target = target, .optimize = optimize, }), }); b.installArtifact(parser_exe); generateApi(b, parser_exe, helper_exe, fetch_sdl_step); // Run command const run_cmd = b.addRunArtifact(parser_exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the SDL3 header parser"); run_step.dependOn(&run_cmd.step); // Test mocks generation target const test_mocks_cmd = b.addRunArtifact(parser_exe); test_mocks_cmd.step.dependOn(b.getInstallStep()); test_mocks_cmd.addArg("test_small.h"); test_mocks_cmd.addArg("--output=zig-out/test_small.zig"); test_mocks_cmd.addArg("--mocks=zig-out/test_small_mock.c"); const test_mocks_step = b.step("test-mocks", "Test mock generation with test_small.h"); test_mocks_step.dependOn(&test_mocks_cmd.step); // Tests const parser_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/parser.zig"), .target = target, .optimize = optimize, }), }); const run_tests = b.addRunArtifact(parser_tests); const test_step = b.step("test", "Run parser tests"); test_step.dependOn(&run_tests.step); }