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

examples/build.zig needs update #22

Open
GLJeff opened this issue Mar 28, 2023 · 2 comments
Open

examples/build.zig needs update #22

GLJeff opened this issue Mar 28, 2023 · 2 comments

Comments

@GLJeff
Copy link

GLJeff commented Mar 28, 2023

I am a zig noob so not entirely sure, but I believe this file needs to be updated for the new zig version in order to be able to build the examples properly

@GLJeff
Copy link
Author

GLJeff commented Mar 29, 2023

const builtin = @import("builtin");
const std = @import("std");
const Builder = std.build.Builder;
const CrossTarget = std.zig.CrossTarget;
const Mode = std.builtin.Mode;

// const pkgs = struct {
//     const network = std.build.Pkg{
//         .name = "win32",
//         .path = "../zigwin32/win32.zig",
//     };
// };

pub fn build(b: *Builder) !void {
    const target = b.standardTargetOptions(.{});
    if (builtin.os.tag != .windows) {
        if (target.os_tag == null or target.os_tag.? != .windows) {
            std.log.err("target is not windows", .{});
            std.log.info("try building with one of -Dtarget=native-windows, -Dtarget=x86-windows or -Dtarget=x86_64-windows\n", .{});
            std.os.exit(1);
        }
    }

    const mode = b.standardOptimizeOption(.{});
    try makeExe(b, target, mode, "helloworld", .Console);
    try makeExe(b, target, mode, "helloworld-window", .Windows);
    try makeExe(b, target, mode, "d2dcircle", .Windows);
    try makeExe(b, target, mode, "opendialog", .Windows);
    try makeExe(b, target, mode, "wasapi", .Console);
    try makeExe(b, target, mode, "net", .Console);
}

fn makeExe(
    b: *Builder,
    target: CrossTarget,
    mode: Mode,
    root: []const u8,
    subsystem: std.Target.SubSystem,
) !void {
    const src = try std.mem.concat(b.allocator, u8, &[_][]const u8{ root, ".zig" });

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

    exe.single_threaded = true;
    //exe.setTarget(target);
    //exe.setBuildMode(mode);
    exe.subsystem = subsystem;
    exe.install();

	exe.addAnonymousModule("win32", .{ .source_file = .{ .path = "../zigwin32/win32.zig" } });

    //const run_cmd = exe.run();
    //run_cmd.step.dependOn(b.getInstallStep());

    //const run_step = b.step("run", "Run the app");
    //run_step.dependOn(&run_cmd.step);
}

this appears to work for anyone interested.

@Banacial
Copy link

Formatted mine to this ( zig version: 0.12.0-dev.414+461036069), just to test the examples after building everything from respective source first.

const builtin = @import("builtin");
const std = @import("std");
const Builder = std.build.Builder;
const Step = std.build.Step;
const CrossTarget = std.zig.CrossTarget;

pub fn build(b: *Builder) !void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    const win32 = b.createModule(.{
        .source_file = .{ .path = "zigwin32/win32.zig" },
    });

    try addExample(b, target, optimize, win32, "helloworld", .Console);
    try addExample(b, target, optimize, win32, "wasapi", .Console);
    try addExample(b, target, optimize, win32, "net", .Console);
    try addExample(b, target, optimize, win32, "helloworld-window", .Windows);
    try addExample(b, target, optimize, win32, "d2dcircle", .Windows);
    try addExample(b, target, optimize, win32, "opendialog", .Windows);
}

fn concat(b: *Builder, slices: []const []const u8) []u8 {
    return std.mem.concat(b.allocator, u8, slices) catch unreachable;
}

fn addExample(
    b: *Builder,
    target: CrossTarget,
    optimize: std.builtin.Mode,
    win32: *std.Build.Module,
    root: []const u8,
    subsystem: std.Target.SubSystem,
) !void {
    const basename = concat(b, &.{ root, ".zig" });

    const exe = b.addExecutable(.{
        .name = root,
        .root_source_file = .{ .path = b.pathJoin(&.{ "examples", basename }) },
        .target = target,
        .optimize = optimize,
    });
    exe.subsystem = subsystem;
    if (subsystem == .Console) {
        exe.single_threaded = true;
    }

    exe.addModule("win32", win32);

    b.installArtifact(exe);
    const run_cmd = b.addRunArtifact(exe);
    run_cmd.step.dependOn(b.getInstallStep());
    if (b.args) |args| {
        run_cmd.addArgs(args);
    }
}

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

2 participants