#How to make a build.zig be dependent on another build.zig

1 messages · Page 1 of 1 (latest)

quick vapor
#

I want to use SDL on my project. Andrew has this repo where a build.zig is setup to compile SDL from source into an static library: https://github.com/andrewrk/SDL/blob/main/build.zig
I would like to turn this build.zig into a module so that I can have another build.zig I have dependency on it, e.g. call something like exe.addModule("sdl", sdl.module("sdl")). I followed this post: https://github.com/beachglasslabs/duckdb.zig/blob/main/build.zig
The thing is, in https://github.com/ziglang/zig/blob/master/build.zig I only see that the module is created, but nothing uses the module in the build.zig. Maybe someone could give me some advice or show me some example of how I might do this. Thanks!

GitHub

SDL with the build system replaced by Zig. Contribute to andrewrk/SDL development by creating an account on GitHub.

GitHub

A thin layer for duckdb in zig. Contribute to beachglasslabs/duckdb.zig development by creating an account on GitHub.

GitHub

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software. - ziglang/zig

vapid spear
#

Andrew's SDL build.zig doesn't expose a module because it contains no zig files / bindings. It's just a way to build a static SDL library you can link to from other projects

#

it exposes only an artifact, in this case a static lib

quick vapor
#

Ok, but if you wanted to use this artifact in another build.zig how would you go about it?
Im particularly interested in using it inside https://github.com/michal-z/zig-gamedev/blob/main/libs/zsdl/build.zig
In this build.zig michal is actually using a precompiled library. I would like to change this to use Andrew's artifact instead, e.g. build the static library and use it

GitHub

Building game development ecosystem for @ziglang! Contribute to michal-z/zig-gamedev development by creating an account on GitHub.

#

I might have to merge both build.zig but I wonder if there was a cleaner way

vapid spear
#

if you have Andrew's SDL as a dependency in build.zig.zon you could add ```
const sdl_dep = b.dependency("name given in build.zig.zon", .{.target = target, .optimize = optimize});
const sdl = sdl_dep.artifact("SDL2");

#

and then exe.linkLibrary(sdl)

#

I think

#

But I'm not sure if Andrew's SDL is up yo date with build system's changes

quick vapor
#

Oh I see. It will take the name from the zon file

#

I think it's up to dsate, I ran it with 0.12

#

Thanks, I will try your suggestion!

quick vapor
# vapid spear if you have Andrew's SDL as a dependency in build.zig.zon you could add ``` cons...

Another question, I see that normally dependencies have a url+hash

    .dependencies = .{
        .dep_name = .{
            .url = "https://link.to/dependency.tar.gz",
            .hash = "12200f41f9804eb9abff259c5d0d84f27caa0a25e0f72451a0243a806c8f94fdc433",
        },
    },

What If I dont want to use an url but instead have downloaded the files into the same project?
I.e. I have
myproj/

  • src/
  • build.zig (this my build.zig)
  • libs/
    • zdl/
      • libs/
        • SDL/ ( This is Andrews repo)
          • build.zig (this will know hwo to build the static library)
          • build.zig.zon
      • src/
      • build.zig (this build.zig should depend on the inner SDL build.zig)
vapid spear
#

Download it yourself and use just path instead of url+hash

quick vapor
#

will try!

honest raft
quick vapor
#

I had hoped linux worked since I think Andrew uses Linux.
I'm on Mac so if it works here thats fine. Mostly trying to use it for learning purposes.

honest raft
#

it should probably work under MacOS

quick vapor
quick vapor
#

Hi, maybe I can get some feedback here: https://github.com/daneelsan/DozD/tree/main
So I learned a few things:

  1. I had to put the dependency to Andrews SDL on the build.zig.zon thats at top level. Otherwise zig build complains that it can't find the dependency. Question: can I make it so that this should be specified inside libs/zsdl/build.zig.zon?
  2. Not sure if people here are familiar with zig-gamedev approach to creating packages/linking but here is my attempt to explain it: in libs/zsdl/build.zig there are two functions: 1) package and 2) link. The first one returns a custom Package thats the one in charge of configuring the zsdl package. The second one is in charge of e.g. linking to the executable. Question: in order to call linkLibrary I have to pass the SDL artifact. I couldn't figure out a way to extract this artifact from the custom zsdl package defined in this file, so I had to add an extra field to the Struct to get it out. Does this make sense?
    Im just trying to learn best practices
GitHub

A 2D game engine. Contribute to daneelsan/DozD development by creating an account on GitHub.

quick vapor
# honest raft it should probably work under MacOS

Hi, I eventually found your https://github.com/Beyley/SDL 😄
Did you, by any chance, happen to make SDL_Image build with zig?
I saw this repo that tried to do so: https://github.com/jayschwa/SDL_image/blob/zig/build.zig
However, to make it work, the author had to make some weird acrobatics, e.g. renaming things like "SDL2/SDL_endian.h" to "SDL_endian.h".
I tried to make a similar build.zig but couldn't make it work. The SDL_image.h include refers to the SDL3 directory whereas the artifact was installed in the SDL2. Any tips?
This is the build.zig I used:

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

    const lib = b.addStaticLibrary(.{
        .name = "SDL2_image",
        .target = target,
        .optimize = optimize,
    });

    lib.addIncludePath(.{ .path = "include" });
    lib.addCSourceFiles(.{ .files = &generic_src_files });
    for (supported_file_types) |file_type| {
        lib.defineCMacro(file_type, null);
    }
    lib.defineCMacro("USE_STBIMAGE", null);
    lib.linkLibC();

    const sdl = b.dependency("SDL", .{
        .target = target,
        .optimize = optimize,
    });
    const sdl_artifact = sdl.artifact("SDL2");
    lib.linkLibrary(sdl_artifact);
    lib.installLibraryHeaders(sdl_artifact);

    lib.installHeader("include/SDL3_image/SDL_image.h", "SDL2/SDL_image.h");
    b.installArtifact(lib);
}
#

Reference to the author's zig-help message: #1113905377633894400 message