SDL.zig icon indicating copy to clipboard operation
SDL.zig copied to clipboard

how to use with Zig's package manager?

Open hasanpasha opened this issue 2 years ago • 8 comments

build.zig.zon file:

...
        .sdl = .{
            .url = "https://github.com/MasterQ32/SDL.zig/archive/dd46ee3.tar.gz",
            .hash = "12202a0847d24f9c79302fff04e73d65a3342baecf0dfea46ac304eb68109ab6b472",
        }
...

build.zig file:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});

    const optimize = b.standardOptimizeOption(.{});

    const exe = b.addExecutable(.{
        .name = "example",
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const zgl = b.dependency("zgl", .{});
    exe.addModule("zgl", zgl.module("zgl"));

    const sdl = b.dependency("sdl", .{});
    exe.addModule("sdl", sdl.module("sdl"));

    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);

    const unit_tests = b.addTest(.{
        .root_source_file = .{ .path = "src/main.zig" },
        .target = target,
        .optimize = optimize,
    });

    const run_unit_tests = b.addRunArtifact(unit_tests);

    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_unit_tests.step);
}

I get this error:

hasan@asus:example$ zig build 
thread 94179 panic: unable to find module 'sdl'
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/debug.zig:373:22: 0x3a18a9 in panicExtra__anon_47103 (build)
    std.builtin.panic(msg, trace, ret_addr);
                     ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/debug.zig:348:15: 0x3787a9 in panic__anon_28777 (build)
    panicExtra(null, null, format, args);
              ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/Build.zig:1707:18: 0x34e347 in module (build)
            panic("unable to find module '{s}'", .{name});
                 ^
/home/hasan/coding/zig/opengl/example/build.zig:19:36: 0x30b0f9 in build (build)
    exe.addModule("sdl", sdl.module("sdl"));
                                   ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/Build.zig:1843:33: 0x2f9cb3 in runBuild__anon_7150 (build)
        .Void => build_zig.build(b),
                                ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/build_runner.zig:298:29: 0x2f58ee in main (build)
        try builder.runBuild(root);
                            ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/start.zig:370:37: 0x2e1325 in posixCallMainAndExit (build)
            var i: usize = 0;
                                    ^
/home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/lib/std/start.zig:243:5: 0x2e0e11 in _start (build)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x4 in ??? (???)
Unwind information for `???:0x4` was not available, trace may be incomplete

error: the following build command crashed:
/home/hasan/coding/zig/opengl/example/zig-cache/o/a5d18aabd80fe14db47c9868cb3b387f/build /home/hasan/opt/zig/zig-linux-x86_64-0.12.0-dev.396+55f0d8b41/zig /home/hasan/coding/zig/opengl/example /home/hasan/coding/zig/opengl/example/zig-cache /home/hasan/.cache/zig

hasanpasha avatar Oct 17 '23 14:10 hasanpasha

There is no module sdl exported. You have to still use the regular "sdk" pattern, you can just do const sdl = @import("sdl");

ikskuh avatar Oct 17 '23 15:10 ikskuh

Thanks. it worked.

hasanpasha avatar Oct 17 '23 16:10 hasanpasha

I was able to get the wrapper working by doing the following:

  • mkdir sdl2-test
  • cd sdl2-test
  • zig init
  • zig fetch --save=sdl https://github.com/MasterQ32/SDL.zig/archive/72233ed5d92dbc76bfddd3d0434bb7d04faad5cc.tar.gz.

And applying the following change to build.zig

diff --git a/build.zig b/build.zig
index d731c9a..5c21365 100644
--- a/build.zig
+++ b/build.zig
@@ -1,4 +1,5 @@
 const std = @import("std");
+const sdl = @import("sdl");
 
 // Although this function looks imperative, note that its job is to
 // declaratively construct a build graph that will be executed by an external
@@ -36,6 +37,10 @@ pub fn build(b: *std.Build) void {
         .optimize = optimize,
     });
 
+    const sdl_sdk = sdl.init(b, null);
+    sdl_sdk.link(exe, .dynamic);
+    exe.root_module.addImport("sdl", sdl_sdk.getWrapperModule());
+
     // This declares intent for the executable to be installed into the
     // standard location when the user invokes the "install" step (the default
     // step when running `zig build`).

And then adding SDL wrapper code (see examples/wrapper.zig) to main.zig

diff --git a/src/main.zig b/src/main.zig
index c8a3f67..31242e1 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,19 +1,40 @@
 const std = @import("std");
+const SDL = @import("sdl");
 
 pub fn main() !void {
+    try SDL.init(.{
+        .video = true,
+        .events = true,
+        .audio = true,
+    });
+    defer SDL.quit();
+ // ... see examples/wrapper.zig
 }

Note the name used to install the package is the name used to import it in build.zig. And the name used in addImport is the name that should be used to import it in your main.zig (or other zig file).

Then using zig build run runs the application.

jmmk avatar Jan 28 '24 01:01 jmmk

zig fetch --save=sdl https://github.com/MasterQ32/SDL.zig/archive/72233ed5d92dbc76bfddd3d0434bb7d04faad5cc.tar.gz

where are you getting these urls from?

ikouchiha47 avatar Apr 17 '24 00:04 ikouchiha47

@ikouchiha47 See https://docs.github.com/en/repositories/working-with-files/using-files/downloading-source-code-archives#source-code-archive-urls for details on that URL format.

But you should be able to take that one and replace 72233ed5d92dbc76bfddd3d0434bb7d04faad5cc with any other commit hash. The latest commit for example is https://github.com/MasterQ32/SDL.zig/commit/55caabfff7c03e42a4c6563e0f6d16cc8fa26dd6.ZZ so https://github.com/MasterQ32/SDL.zig/archive/55caabfff7c03e42a4c6563e0f6d16cc8fa26dd6.tar.gz would be the URL for the source code archive of that commit.

You can find the list of all commits here: https://github.com/MasterQ32/SDL.zig/commits/master/

jmmk avatar Apr 18 '24 00:04 jmmk

Noob question: do I have to put SDL.zig in a local subfolder in my project and use a build.zig file like in the README.md, or can I use the ZON package manager like the OP tried in https://github.com/ikskuh/SDL.zig/issues/146#issue-1947580094 ? I did the former and it worked after some light tweaking. I'd like to do the latter

aleloi avatar Jul 22 '24 07:07 aleloi

@aleloi I don't think you don't need to put it anywhere, just fetch the repo. And then follow the docs, adding the changes to build.zig. const Sdk = @import("sdl");

and then link it with const sdk = Sdk.init(b, null); sdk.link(exe, .dynamic)

ikouchiha47 avatar Aug 03 '24 02:08 ikouchiha47