Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to use with Zig's package manager? #146

Open
hasanpasha opened this issue Oct 17, 2023 · 8 comments
Open

how to use with Zig's package manager? #146

hasanpasha opened this issue Oct 17, 2023 · 8 comments

Comments

@hasanpasha
Copy link

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 hasanpasha changed the title how to use with Zig's package manager. how to use with Zig's package manager? Oct 17, 2023
@ikskuh
Copy link
Owner

ikskuh commented Oct 17, 2023

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

@hasanpasha
Copy link
Author

Thanks. it worked.

@jmmk
Copy link

jmmk commented Jan 28, 2024

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.

@ikouchiha47
Copy link

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

where are you getting these urls from?

@jmmk
Copy link

jmmk commented Apr 18, 2024

@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/

@aleloi
Copy link
Contributor

aleloi commented Jul 22, 2024

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 #146 (comment) ? I did the former and it worked after some light tweaking. I'd like to do the latter

@ikouchiha47
Copy link

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

@mdsimmo
Copy link

mdsimmo commented Oct 25, 2024

Also zig noob here: when I do @import("sdl") in the build.zig file, it throws a compile error of error: no module named 'sdl' available within module root.@build.

I've added the module in the build.zig.zod file, but it doesn't seem to get recognized. What am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants