#Running an executable in build.zig

1 messages · Page 1 of 1 (latest)

ancient edge
#

Hi, I'm having an issue where I want to run an executable in build.zig, but I get an out of memory error if I try to use std.process.run with the Debug allocator. Any help, also I'm wondering if this is really the best way to do this in Zig?

pub fn build(b: *std.Build) !void {
    var debug_allocator: std.heap.DebugAllocator(.{}) = .init;

    var allocator: std.mem.Allocator = undefined;

    var threaded: std.Io.Threaded = .init_single_threaded;
    const io = threaded.io();

    allocator = debug_allocator.allocator();

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

    const exe = b.addExecutable(.{
        .name = "hello",
        .root_module = b.createModule(.{
            .root_source_file = b.path("hello.zig"),
            .target = b.graph.host,
        }),
    });

    const vulkan_headers = b.dependency("vulkan_headers", .{});
    const vulkan = b.dependency("vulkan", .{
        .registry = vulkan_headers.path("registry/vk.xml"),
    }).module("vulkan-zig");
    exe.root_module.addImport("vulkan", vulkan);

    const zglfw = b.dependency("zglfw", .{
        .target = target,
        .optimize = optimize,
        .import_vulkan = true,
    });

    const zglfw_mod = zglfw.module("root");
    zglfw_mod.addImport("vulkan", vulkan);
    exe.root_module.addImport("glfw", zglfw_mod);

    if (target.result.os.tag != .emscripten) {
        exe.root_module.linkLibrary(zglfw.artifact("glfw"));
    }

    b.installArtifact(exe);

    const buildshaders = b.step("buildshaders", "Build some shaders or summ");

    _ = buildshaders;

    const argv = [_][]const u8{"buildshaders.sh"};

    _ = try std.process.run(allocator, io, .{ .argv = &argv });

    // For ZLS diagnostics:
    const exe_check = b.addExecutable(.{
        .name = "hello",
        .root_module = exe.root_module,
    });

    const check = b.step("check", "check if hello compiles");
    check.dependOn(&exe_check.step);
}
mint cove
#

can you not just run a system command?

    const run_cmd_dependency = b.addRunArtifact(exe);

    const run_qemu_step = b.step("qemu", "runs qemu");
    const qemu_cmd = b.addSystemCommand(&.{
        "qemu-system-riscv64",
...
    });
    run_qemu_step.dependOn(&qemu_cmd.step);
    qemu_cmd.step.dependOn(b.getInstallStep());

ancient edge
#

I see... I was unaware of this existing.

I changed it like so:

    const buildshaders = b.step("buildshaders", "Build some shaders or summ");

    const argv = [_][]const u8{"./buildshaders.sh"};

    const shaderbuild_cmd = b.addSystemCommand(&argv);

    buildshaders.dependOn(&shaderbuild_cmd.step);
    shaderbuild_cmd.step.dependOn(b.getInstallStep());

    b.installArtifact(exe);

But this step doesn't seem to be executed

novel moon
#

Steps have to be dependencies of other steps to run

#

Steps have a .dependOn function. There is also b.default_step

#

You can do b.default_step.dependOn(your_step);

#

Currently your step is depedant on the install step but nothing is dependant on your step so its just not done