diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..feb8e3b --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +# SPDX-FileCopyrightText: © 2025 Mark Delk +# +# SPDX-License-Identifier: Zlib + +name: ci + +on: [pull_request] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: license check + uses: fsfe/reuse-action@v4 + + - name: setup zig + uses: mlugg/setup-zig@v2 + with: + version: 0.14.1 + + - name: Install SDL dependencies + run: | + sudo apt-get update -y + sudo apt-get install -y build-essential git make pkg-config cmake ninja-build gnome-desktop-testing libasound2-dev libpulse-dev libaudio-dev libjack-dev libsndio-dev libx11-dev libxext-dev libxrandr-dev libxcursor-dev libxfixes-dev libxi-dev libxss-dev libxtst-dev libxkbcommon-dev libdrm-dev libgbm-dev libgl1-mesa-dev libgles2-mesa-dev libegl1-mesa-dev libdbus-1-dev libibus-1.0-dev libudev-dev libpipewire-0.3-dev libwayland-dev libdecor-0-dev liburing-dev + + - name: zig build + run: zig build --summary all + + - name: Build SDL test programs + run: zig build --summary all sdl-test + + - name: Build SDL example programs + run: zig build --summary all sdl-examples + + - name: Build Zig examples + run: zig build --summary all zig-examples diff --git a/.github/workflows/reuse.yaml b/.github/workflows/reuse.yaml deleted file mode 100644 index 36718ab..0000000 --- a/.github/workflows/reuse.yaml +++ /dev/null @@ -1,15 +0,0 @@ -# SPDX-FileCopyrightText: © 2024 Mark Delk -# -# SPDX-License-Identifier: Zlib - -name: REUSE Compliance Check - -on: [push, pull_request] - -jobs: - test: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4 - - name: REUSE Compliance Check - uses: fsfe/reuse-action@v4 diff --git a/README.md b/README.md index e482bfe..856bb9b 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ SPDX-License-Identifier: Zlib # SDL.zig -Build [SDL](https://github.com/libsdl-org/SDL) using [zig](https://ziglang.org) (version 0.13.0). +Build [SDL](https://github.com/libsdl-org/SDL) 3 using [zig](https://ziglang.org) (version 0.14.1). Also includes @@ -16,6 +16,10 @@ Also includes TODO +## Alternatives / anti-endorsement + +You'll probably want to use https://github.com/castholm/SDL instead, it's more complete and better maintained. + ## Tests Build all of SDL's test programs: @@ -28,7 +32,7 @@ Build and run a specific SDL test: ``` zig build sdl-test-testaudiohotplug -zig build sdl-test-testcamera -- --camera 'Razer Kiyo' +zig build sdl-test-testcamera ``` ## Examples diff --git a/build.zig b/build.zig index f6e8bc1..18db861 100644 --- a/build.zig +++ b/build.zig @@ -8,22 +8,34 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const linkage = b.option( + std.builtin.LinkMode, + "linkage", + "`.static` or `.dynamic` (default is `.dynamic`)", + ) orelse .dynamic; + const sdl_dep = b.dependency("sdl", .{}); const sdl_ttf_dep = b.dependency("sdl_ttf", .{}); - const freetype_dep = b.dependency("freetype", .{}); + const freetype_dep = b.dependency("freetype", .{ + .linkage = linkage, + .optimize = optimize, + }); const wayland_scanner_dep = b.dependency("wayland_scanner", .{ - .target = b.host, + .target = b.graph.host, .optimize = .ReleaseFast, }); const font_dep = b.dependency("fonts", .{}); // - const lib = b.addStaticLibrary(.{ + const lib = b.addLibrary(.{ .name = "SDL3", - .target = target, - .optimize = optimize, - .strip = true, + .linkage = linkage, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = optimize, + }), }); { lib.addIncludePath(sdl_dep.path("src")); @@ -41,12 +53,12 @@ pub fn build(b: *std.Build) !void { .ReleaseFast, .ReleaseSmall, .ReleaseSafe => 0, .Debug => 3, }; - lib.defineCMacro("SDL_ASSERT_LEVEL", b.fmt("{d}", .{SDL_ASSERT_LEVEL})); + lib.root_module.addCMacro("SDL_ASSERT_LEVEL", b.fmt("{d}", .{SDL_ASSERT_LEVEL})); if (optimize != .Debug) { - lib.defineCMacro("NDEBUG", "1"); - lib.defineCMacro("__FILE__", "\"__FILE__\""); - lib.defineCMacro("__LINE__", "0"); + lib.root_module.addCMacro("NDEBUG", "1"); + lib.root_module.addCMacro("__FILE__", "\"__FILE__\""); + lib.root_module.addCMacro("__LINE__", "0"); } lib.linkLibC(); @@ -65,7 +77,7 @@ pub fn build(b: *std.Build) !void { lib.linkSystemLibrary("version"); lib.linkSystemLibrary("oleaut32"); lib.linkSystemLibrary("ole32"); - lib.defineCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1"); + lib.root_module.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1"); }, .macos => { lib.addCSourceFiles(.{ @@ -78,7 +90,7 @@ pub fn build(b: *std.Build) !void { .files = &objective_c_src_files, .flags = &.{"-fobjc-arc"}, }); - lib.defineCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1"); + lib.root_module.addCMacro("SDL_USE_BUILTIN_OPENGL_DEFINITIONS", "1"); // TODO: re-check which frameworks are needed lib.linkFramework("AudioToolbox"); @@ -94,14 +106,14 @@ pub fn build(b: *std.Build) !void { lib.linkFramework("CoreVideo"); lib.linkFramework("ForceFeedback"); lib.linkFramework("Foundation"); - lib.linkFrameworkWeak("GameController"); + lib.linkFramework("GameController"); lib.linkFramework("IOKit"); - lib.linkFrameworkWeak("Metal"); - lib.linkFrameworkWeak("QuartzCore"); - lib.linkFrameworkWeak("UniformTypeIdentifiers"); + lib.linkFramework("Metal"); + lib.linkFramework("QuartzCore"); + lib.linkFramework("UniformTypeIdentifiers"); lib.linkSystemLibrary("objc"); - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); lib.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); lib.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); @@ -345,8 +357,6 @@ pub fn build(b: *std.Build) !void { .SDL_CAMERA_DRIVER_DUMMY = 1, .SDL_CAMERA_DRIVER_V4L2 = 1, - // #define DYNAPI_NEEDS_DLOPEN 1 - .SDL_USE_IME = 1, .SDL_LIBDECOR_VERSION_MAJOR = 0, @@ -361,12 +371,33 @@ pub fn build(b: *std.Build) !void { .SDL_PROCESS_POSIX = 1, .SDL_PROCESS_DUMMY = 1, }; - lib.installConfigHeader(b.addConfigHeader(.{ - .style = .{ .cmake = sdl_dep.path( - "include/build_config/SDL_build_config.h.cmake", - ) }, + + const fix_header_exe = b.addExecutable(.{ + .name = "fix-header", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/fix_header.zig"), + .target = b.graph.host, + .optimize = optimize, + }), + }); + const fix_header = b.addRunArtifact(fix_header_exe); + { + fix_header.addFileArg(sdl_dep.path("include/build_config/SDL_build_config.h.cmake")); + } + const fixed_header = fix_header.captureStdOut(); + + const installFixedHeader = b.addInstallFile(fixed_header, "fixed_header.h"); + + const config_header = b.addConfigHeader(.{ + .style = .{ .cmake = fixed_header }, .include_path = "SDL_build_config.h", - }, values)); + }, values); + + // NOTE: needed to allow `b.addConfigHeader` to use a generated + // file with `.cmake`. (zig build system issue?) + config_header.step.dependOn(&installFixedHeader.step); + + lib.installConfigHeader(config_header); lib.addCSourceFiles(.{ .root = sdl_dep.path(""), @@ -376,18 +407,22 @@ pub fn build(b: *std.Build) !void { inline for (std.meta.fields(@TypeOf(values))) |f| { const value = b.fmt("{any}", .{@field(values, f.name)}); - lib.defineCMacro(f.name, value); + lib.root_module.addCMacro(f.name, value); + } + + if (linkage == .dynamic) { + lib.setVersionScript(sdl_dep.path("src/dynapi/SDL_dynapi.sym")); } // SDL3_DYNAMIC_API=/my/actual/libSDL3.so.0 // // If we want to _avoid_ dyn api, we can do so: - // lib.defineCMacro("SDL_dynapi_h_", "1"); - // lib.defineCMacro("SDL_DYNAMIC_API", "0"); + // lib.root_module.addCMacro("SDL_dynapi_h_", "1"); + // lib.root_module.addCMacro("SDL_DYNAMIC_API", "0"); // Avoid // SDL/include/build_config/SDL_build_config_minimal.h - lib.defineCMacro("SDL_build_config_minimal_h_", "1"); + // lib.root_module.addCMacro("SDL_build_config_minimal_h_", "1"); //-- @@ -485,25 +520,14 @@ pub fn build(b: *std.Build) !void { b.installArtifact(lib); } - // needed to allow SDL_ttf to #include "SDL.h" - const sdl_for_libs = b.addStaticLibrary(.{ - .name = "SDL3-for-libs", - .target = target, - .optimize = optimize, - .root_source_file = b.addWriteFiles().add("stub.c", ""), - .strip = true, - }); - { - sdl_for_libs.addIncludePath(sdl_dep.path("include")); - sdl_for_libs.installHeadersDirectory(sdl_dep.path("include"), "", .{}); - b.installArtifact(sdl_for_libs); - } - - const SDL_ttf = b.addStaticLibrary(.{ + const SDL_ttf = b.addLibrary(.{ .name = "SDL3_ttf", - .target = target, - .optimize = optimize, - .strip = true, + .linkage = linkage, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = optimize, + }), }); { SDL_ttf.addCSourceFiles(.{ @@ -521,16 +545,17 @@ pub fn build(b: *std.Build) !void { SDL_ttf.installHeadersDirectory(sdl_ttf_dep.path("include/SDL3_ttf"), "SDL3_ttf", .{}); SDL_ttf.linkLibrary(freetype_dep.artifact("freetype")); + SDL_ttf.linkLibrary(lib); if (target.result.os.tag == .macos) { - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); SDL_ttf.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); SDL_ttf.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); SDL_ttf.addLibraryPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/lib" }) }); } - SDL_ttf.linkLibrary(sdl_for_libs); + SDL_ttf.addIncludePath(sdl_dep.path("include")); b.installArtifact(SDL_ttf); } @@ -545,7 +570,6 @@ pub fn build(b: *std.Build) !void { \\}); \\pub const fonts = @import("fonts"); ), - .link_libc = true, }); { module.linkLibrary(lib); @@ -577,12 +601,11 @@ pub fn build(b: *std.Build) !void { .target = target, .root_source_file = b.path(b.fmt("src/{s}.zig", .{name})), .optimize = optimize, - .strip = true, }); exe.root_module.addImport("sdl", module); if (target.result.os.tag == .macos) { - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); exe.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); exe.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); @@ -855,7 +878,7 @@ pub fn build(b: *std.Build) !void { exe.linkLibrary(test_utils); if (target.result.os.tag == .macos) { - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); exe.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); exe.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); @@ -928,7 +951,7 @@ pub fn build(b: *std.Build) !void { exe.linkLibrary(lib); if (target.result.os.tag == .macos) { - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); exe.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); exe.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); @@ -1025,14 +1048,14 @@ const generic_src_files = [_][]const u8{ "src/gpu/vulkan/SDL_gpu_vulkan.c", "src/haptic/SDL_haptic.c", - "src/haptic/dummy/SDL_syshaptic.c", + // "src/haptic/dummy/SDL_syshaptic.c", "src/hidapi/SDL_hidapi.c", "src/joystick/SDL_gamepad.c", "src/joystick/SDL_joystick.c", "src/joystick/SDL_steam_virtual_gamepad.c", - // "src/joystick/dummy/SDL_sysjoystick.c", + "src/joystick/dummy/SDL_sysjoystick.c", "src/joystick/controller_type.c", "src/joystick/virtual/SDL_virtualjoystick.c", @@ -1078,7 +1101,7 @@ const generic_src_files = [_][]const u8{ "src/power/SDL_power.c", "src/process/SDL_process.c", - "src/process/dummy/SDL_dummyprocess.c", + // "src/process/dummy/SDL_dummyprocess.c", "src/render/SDL_d3dmath.c", "src/render/SDL_render.c", @@ -1154,7 +1177,7 @@ const generic_src_files = [_][]const u8{ "src/timer/SDL_timer.c", "src/tray/SDL_tray_utils.c", - "src/tray/dummy/SDL_tray.c", + // "src/tray/dummy/SDL_tray.c", "src/video/SDL_RLEaccel.c", "src/video/SDL_blit.c", diff --git a/build.zig.zon b/build.zig.zon index b9851ca..a772784 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -3,17 +3,18 @@ // SPDX-License-Identifier: Zlib .{ - .name = "sdl", - .version = "0.0.0", + .name = .sdl, + .version = "0.0.1", + .fingerprint = 0xec638ccb9c209e38, .paths = .{""}, .dependencies = .{ .sdl = .{ .url = "git+https://github.com/libsdl-org/SDL?ref=preview-3.1.8#22422f7748d5128135995ed34c8f8012861c7332", - .hash = "122099222def404a93508de2e17e8347f4ae5c043e82f2edef0fa370586af1915c6d", + .hash = "N-V-__8AAJlxgwOZIi3vQEqTUI3i4X6DR_SuXAQ-gvLt7w-j", }, .sdl_ttf = .{ .url = "git+https://github.com/libsdl-org/SDL_ttf?ref=main#767a91cc47d964e9512feae7feae0a6f3dc762f6", - .hash = "1220d88904c7398472e77670ff3a736d0050b6c501dc315f88708e05f8232060000f", + .hash = "N-V-__8AAPCAGQDYiQTHOYRy53Zw_zpzbQBQtsUB3DFfiHCO", }, .freetype = .{ .path = "build/freetype", diff --git a/build/fonts/build.zig.zon b/build/fonts/build.zig.zon index a40d593..9b962cf 100644 --- a/build/fonts/build.zig.zon +++ b/build/fonts/build.zig.zon @@ -3,13 +3,14 @@ // SPDX-License-Identifier: Zlib .{ - .name = "fonts", + .name = .fonts, .version = "0.0.0", + .fingerprint = 0x7303e8fb6c57db3a, .paths = .{""}, .dependencies = .{ .intel_one_mono = .{ - .url = "git+https://github.com/intel/intel-one-mono#V1.3.0", - .hash = "1220f1af56308368aac7a289e4d7ab23fc7688368aa3decc0d1a28e3f9f477dcb3b4", + .url = "git+https://github.com/intel/intel-one-mono?ref=V1.3.0#08d42d257a3211cf2427b35bc2eeab4622d042c8", + .hash = "N-V-__8AACfYKwHxr1Ywg2iqx6KJ5NerI_x2iDaKo97MDRoo", }, }, } diff --git a/build/freetype/build.zig b/build/freetype/build.zig index d3f77f1..da9db6a 100644 --- a/build/freetype/build.zig +++ b/build/freetype/build.zig @@ -8,21 +8,30 @@ pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const linkage = b.option( + std.builtin.LinkMode, + "linkage", + "`.static` or `.dynamic` (default is `.dynamic`)", + ) orelse .dynamic; + const freetype_dep = b.dependency("freetype", .{}); // - const lib = b.addStaticLibrary(.{ + const lib = b.addLibrary(.{ .name = "freetype", - .target = target, - .optimize = optimize, - .strip = true, + .linkage = linkage, + .root_module = b.createModule(.{ + .root_source_file = null, + .target = target, + .optimize = optimize, + }), }); { if (optimize != .Debug) { - lib.defineCMacro("NDEBUG", "1"); - lib.defineCMacro("__FILE__", "\"__FILE__\""); - lib.defineCMacro("__LINE__", "0"); + lib.root_module.addCMacro("NDEBUG", "1"); + lib.root_module.addCMacro("__FILE__", "\"__FILE__\""); + lib.root_module.addCMacro("__LINE__", "0"); } lib.addCSourceFiles(.{ @@ -72,9 +81,9 @@ pub fn build(b: *std.Build) !void { .flags = &.{}, }); - lib.defineCMacro("FT2_BUILD_LIBRARY", "1"); - lib.defineCMacro("HAVE_UNISTD_H", ""); - lib.defineCMacro("HAVE_FCNTL_H", ""); + lib.root_module.addCMacro("FT2_BUILD_LIBRARY", "1"); + lib.root_module.addCMacro("HAVE_UNISTD_H", ""); + lib.root_module.addCMacro("HAVE_FCNTL_H", ""); switch (target.result.os.tag) { .windows => { @@ -109,7 +118,7 @@ pub fn build(b: *std.Build) !void { lib.installHeadersDirectory(freetype_dep.path("include/freetype"), "freetype", .{}); if (target.result.os.tag == .macos) { - const sdk = std.zig.system.darwin.getSdk(b.allocator, b.host.result) orelse + const sdk = std.zig.system.darwin.getSdk(b.allocator, b.graph.host.result) orelse @panic("macOS SDK is missing"); lib.addSystemIncludePath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/usr/include" }) }); lib.addSystemFrameworkPath(.{ .cwd_relative = b.pathJoin(&.{ sdk, "/System/Library/Frameworks" }) }); diff --git a/build/freetype/build.zig.zon b/build/freetype/build.zig.zon index 2b3c07c..07c3b14 100644 --- a/build/freetype/build.zig.zon +++ b/build/freetype/build.zig.zon @@ -3,14 +3,15 @@ // SPDX-License-Identifier: Zlib .{ - .name = "freetype", + .name = .freetype, .version = "0.0.0", + .fingerprint = 0xac2059b689838d78, .paths = .{""}, .dependencies = .{ // NOTE: matches SDL_ttf's external/freetype submodule .freetype = .{ .url = "git+https://github.com/libsdl-org/freetype?ref=VER-2-13-2#920c5502cc3ddda88f6c7d85ee834ac611bb11cc", - .hash = "1220b81f6ecfb3fd222f76cf9106fecfa6554ab07ec7fdc4124b9bb063ae2adf969d", + .hash = "N-V-__8AAKLKpwC4H27Ps_0iL3bPkQb-z6ZVSrB-x_3EEkub", }, }, } diff --git a/build/wayland_scanner/build.zig b/build/wayland_scanner/build.zig index 77cec0f..17ee201 100644 --- a/build/wayland_scanner/build.zig +++ b/build/wayland_scanner/build.zig @@ -15,7 +15,7 @@ pub fn build(b: *std.Build) !void { const expat = b.addStaticLibrary(.{ .name = "expat", - .target = b.host, + .target = b.graph.host, .optimize = optimize, }); { @@ -34,6 +34,13 @@ pub fn build(b: *std.Build) !void { .style = .{ .cmake = expat_dep.path("expat/expat_config.h.cmake") }, .include_path = "expat_config.h", }, .{ + .BYTEORDER = @as(i64, switch (b.graph.host.result.cpu.arch.endian()) { + .little => 1234, + .big => 4321, + }), + .off_t = null, + .size_t = "unsigned", + .HAVE_STDLIB_H = 1, .HAVE_STRINGS_H = 1, .HAVE_STRING_H = 1, @@ -68,7 +75,7 @@ pub fn build(b: *std.Build) !void { const wayland_scanner = b.addExecutable(.{ .name = "wayland-scanner", - .target = b.host, + .target = b.graph.host, .optimize = optimize, }); { diff --git a/build/wayland_scanner/build.zig.zon b/build/wayland_scanner/build.zig.zon index 85986a5..f100bef 100644 --- a/build/wayland_scanner/build.zig.zon +++ b/build/wayland_scanner/build.zig.zon @@ -3,17 +3,18 @@ // SPDX-License-Identifier: Zlib .{ - .name = "wayland_scanner", + .name = .wayland_scanner, .version = "0.0.0", + .fingerprint = 0x501ba345c1864a85, .paths = .{""}, .dependencies = .{ .wayland = .{ - .url = "git+https://gitlab.freedesktop.org/wayland/wayland.git#1.22.0", - .hash = "12204035d74e1978e3e4067d81d1a74aff5eaf6a80cc70aade59ee46247489e3a131", + .url = "git+https://gitlab.freedesktop.org/wayland/wayland.git?ref=1.22.0#b2649cb3ee6bd70828a17e50beb16591e6066288", + .hash = "N-V-__8AABF0FwBANddOGXjj5AZ9gdGnSv9er2qAzHCq3lnu", }, .expat = .{ - .url = "git+https://github.com/libexpat/libexpat#R_2_6_1", - .hash = "1220ac6fead249f922d08168f43295090ba7d6e7567e0314dc06cf2fb9ce11ed2bea", + .url = "git+https://github.com/libexpat/libexpat?ref=R_2_6_1#a590b2d5846865412182805b853dd91d18f38c8d", + .hash = "N-V-__8AADuJGgisb-rSSfki0IFo9DKVCQun1udWfgMU3AbP", }, }, } diff --git a/src/fix_header.zig b/src/fix_header.zig new file mode 100644 index 0000000..70ab2e7 --- /dev/null +++ b/src/fix_header.zig @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: © Zlib Mark Delk +// +// SPDX-License-Identifier: Zlib + +// Needed to fix the following issue, until https://github.com/ziglang/zig/issues/21265 is addressed. +// +// Remove lines containing `@` +// +// ``` +// #cmakedefine SDL_VIDEO_DRIVER_X11_DYNAMIC @SDL_VIDEO_DRIVER_X11_DYNAMIC@ +// ``` +// +const std = @import("std"); + +pub const std_options: std.Options = .{ + .log_level = .debug, +}; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer if (gpa.deinit() == .leak) @panic("found memory leaks"); + const allocator = gpa.allocator(); + + const args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + if (args.len != 2) { + std.log.err( + \\Wrong number of arguments. + \\ Usage: {s} {{header}} > {{output.h}} + \\ + , .{args[0]}); + std.process.exit(1); + } + const input_file = try std.fs.cwd().openFile(args[1], .{}); + errdefer input_file.close(); + + const reader = input_file.reader(); + const writer = std.io.getStdOut().writer(); + + var line_buf = std.ArrayList(u8).init(allocator); + defer line_buf.deinit(); + + while (reader.streamUntilDelimiter(line_buf.writer(), '\n', null)) { + if (line_buf.getLastOrNull() == '\r') _ = line_buf.pop(); + defer line_buf.clearRetainingCapacity(); + + const line = line_buf.items; + + if (std.mem.count(u8, line, "@") == 0) { + _ = try writer.write(line); + } + + _ = try writer.write("\n"); + } else |err| switch (err) { + error.EndOfStream => {}, + else => |e| return e, + } +}