From 27157135edde78bbc7d88ddd54cea8980fed2b6a Mon Sep 17 00:00:00 2001 From: peterino2 Date: Sat, 7 Mar 2026 10:54:39 -0800 Subject: [PATCH] updated generated files --- README.md | 16 ++++++++++++---- build.zig | 48 ++++++++++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 6bebc21..595896e 100644 --- a/README.md +++ b/README.md @@ -37,13 +37,21 @@ You can specify which repo to use with `-Dsdl-url=` the git refs will `-Dofficial` will use libsdl org's github repo. -If you already have SDL headers locally, use `-DtargetDir=` instead of fetching from git: +If you already have SDL headers locally, use `-DsourceDir=` instead of fetching from git: -``` -zig build generate -DtargetDir=D:/path/to/SDL +``` +zig build generate -DsourceDir=D:/path/to/SDL ``` -`targetDir` may point at an SDL checkout root (`include/SDL3`) or directly at a directory containing the SDL headers. Generated outputs go to `/api` and `/json`, and parser scratch directories (`tmp`, `debug`, `archive`) are rooted there as well. +`sourceDir` may point at an SDL checkout root (`include/SDL3`) or directly at a directory containing the SDL headers. Generated outputs go to `/api` and `/json` unless `-DoutputDir` is provided. + +If you want to read headers from one location and write outputs somewhere else, add `-DoutputDir=`: + +``` +zig build generate -DsourceDir=D:/path/to/SDL -DoutputDir=D:/path/to/output +``` + +That writes `api/` and `json/` under `outputDir` while still reading headers from `sourceDir`. `--basedir` remains the parser working directory for `tmp/`, `debug/`, and `archive/debug`. ## Parser Usage diff --git a/build.zig b/build.zig index 0e93880..c1eecde 100644 --- a/build.zig +++ b/build.zig @@ -1,13 +1,13 @@ const std = @import("std"); -fn resolveHeaderRoot(allocator: std.mem.Allocator, target_dir: []const u8) ![]const u8 { - const include_sdl3 = try std.fs.path.join(allocator, &.{ target_dir, "include", "SDL3" }); +fn resolveHeaderRoot(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.fs.cwd().access(include_sdl3, .{})) { return try allocator.dupe(u8, "include/SDL3"); } else |_| {} - const sdl3_dir = try std.fs.path.join(allocator, &.{ target_dir, "SDL3" }); + const sdl3_dir = try std.fs.path.join(allocator, &.{ source_dir, "SDL3" }); defer allocator.free(sdl3_dir); if (std.fs.cwd().access(sdl3_dir, .{})) { return try allocator.dupe(u8, "SDL3"); @@ -156,16 +156,17 @@ const MakeDirStep = struct { }; pub fn generateApi(b: *std.Build, parser_exe: *std.Build.Step.Compile, fetch_sdl_step: *std.Build.Step) void { - const target_dir = b.option([]const u8, "targetDir", "Parse SDL headers from an existing local directory and generate api/ there instead of fetching git"); + 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 effective_basedir = target_dir orelse basedir; - const header_root_suffix = if (target_dir) |dir| resolveHeaderRoot(b.allocator, dir) catch @panic("OOM") else null; + const effective_output_dir = output_dir orelse source_dir; + const header_root_suffix = if (source_dir) |dir| resolveHeaderRoot(b.allocator, dir) catch @panic("OOM") else null; // Archive existing json/ and api/ directories before regenerating - const archive_step = ArchiveStep.create(b, effective_basedir); + const archive_step = ArchiveStep.create(b, effective_output_dir); - const generation_root = if (target_dir != null) archive_step else fetch_sdl_step; - if (target_dir == null) { + const generation_root = if (source_dir != null) archive_step else fetch_sdl_step; + if (source_dir == null) { fetch_sdl_step.dependOn(archive_step); } @@ -176,10 +177,17 @@ pub fn generateApi(b: *std.Build, parser_exe: *std.Build.Step.Compile, fetch_sdl _ = marker_file; wf.step.dependOn(generation_root); - // Create basedir if specified - const basedir_step = if (effective_basedir) |dir| MakeDirStep.create(b, dir) else &wf.step; - if (effective_basedir != null) - basedir_step.dependOn(&wf.step); + var path_prep_step: *std.Build.Step = &wf.step; + if (effective_output_dir) |dir| { + const output_dir_step = MakeDirStep.create(b, dir); + output_dir_step.dependOn(path_prep_step); + path_prep_step = output_dir_step; + } + if (basedir) |dir| { + const basedir_step = MakeDirStep.create(b, dir); + basedir_step.dependOn(path_prep_step); + path_prep_step = basedir_step; + } // All public SDL3 API headers (53 total) // Skipped: assert, thread, hidapi, mutex, tray (not core APIs or problematic) @@ -239,12 +247,12 @@ pub fn generateApi(b: *std.Build, parser_exe: *std.Build.Step.Compile, fetch_sdl }; const regenerate_step = b.step("generate", "Regenerate bindings from SDL headers"); - const header_root = if (target_dir) |dir| + const header_root = if (source_dir) |dir| b.fmt("{s}/{s}", .{ dir, header_root_suffix.? }) else "sdl3/include/SDL3"; - const output_root = if (target_dir) |dir| b.fmt("{s}/api", .{dir}) else "api"; - const json_root = if (target_dir) |dir| b.fmt("{s}/json", .{dir}) else "json"; + 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 timestamp_arg = b.fmt("--timestamp={d}", .{timestamp}); @@ -253,21 +261,21 @@ pub fn generateApi(b: *std.Build, parser_exe: *std.Build.Step.Compile, fetch_sdl 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 (effective_basedir) |dir| { + if (basedir) |dir| { regenerate.addArg(b.fmt("--basedir={s}", .{dir})); } // regenerate.addArg(b.fmt("--output=api/{s}.zig --mocks=mocks/{s}.c", .{ header_info.output, header_info.output })); - regenerate.step.dependOn(basedir_step); + 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); - if (effective_basedir) |dir| { + if (basedir) |dir| { regenerateJson.addArg(b.fmt("--basedir={s}", .{dir})); } - regenerateJson.step.dependOn(basedir_step); + regenerateJson.step.dependOn(path_prep_step); regenerate_step.dependOn(®enerateJson.step); } }