Skip to content

Commit

Permalink
build: add @import("mach").addExecutable helper
Browse files Browse the repository at this point in the history
This adds a helper that can be used people's `build.zig` code, called `@import("mach").addExecutable`,
a direct replacement for `b.addExecutable`.

The benefits of using this method are:

1. Your `build.zig` code does not need to be aware of platform-specifics that may be required to build an executable,
   for example setting a Windows manifest to ensure your app is DPI-aware.
2. You do not need to write `main.zig` entrypoint code, which although simple today is expected to become more complex
   over time as we add support for more platforms. For example, WASM and other platforms require different entrypoints
   and this can account for that without your `build.zig` containing that logic.

Steps to use:

1. Delete your `main.zig` file.
2. Define your `Modules` as a public const in your `App.zig` file, e.g.:

```zig
// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
    mach.Core,
    App,
});
```

3. Update your `build.zig` code to use `@import("mach").addExecutable` like so:

```zig
const std = @import("std");

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

    const app_mod = b.createModule(.{
        .root_source_file = b.path("src/App.zig"),
        .optimize = optimize,
        .target = target,
    });

    // Add Mach to our library and executable
    const mach_dep = b.dependency("mach", .{
        .target = target,
        .optimize = optimize,
    });
    app_mod.addImport("mach", mach_dep.module("mach"));

    // Use the Mach entrypoint to write main for us
    const exe = @import("mach").addExecutable(mach_dep.builder, .{
        .name = "hello-world",
        .app = app_mod,
        .target = target,
        .optimize = optimize,
    });
    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 app_unit_tests = b.addTest(.{
        .root_module = app_mod,
    });
    const run_app_unit_tests = b.addRunArtifact(app_unit_tests);
    const test_step = b.step("test", "Run unit tests");
    test_step.dependOn(&run_app_unit_tests.step);
}
```

Signed-off-by: Emi <[email protected]>
  • Loading branch information
emidoots committed Feb 18, 2025
1 parent 2410814 commit b14f8e6
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 201 deletions.
44 changes: 37 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,34 @@ const Example = struct {
run_step: *std.Build.Step = undefined,
};

pub fn addExecutable(
mach_builder: *std.Build,
options: struct {
name: []const u8,
app: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
},
) *std.Build.Step.Compile {
const entrypoint_mod = mach_builder.addModule(
mach_builder.fmt("{s}-entrypoint", .{options.name}),
.{
.root_source_file = mach_builder.path("src/entrypoint/main.zig"),
.optimize = options.optimize,
.target = options.target,
},
);
entrypoint_mod.addImport("app", options.app);

return mach_builder.addExecutable(.{
.name = options.name,
.root_module = entrypoint_mod,

// Win32 manifest file for DPI-awareness configuration
.win32_manifest = mach_builder.path("src/core/windows/win32.manifest"),
});
}

fn buildExamples(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
Expand All @@ -397,34 +425,36 @@ fn buildExamples(
examples: []Example,
) void {
for (examples) |*example| {
const exe = b.addExecutable(.{
const app_mod = b.addModule(example.name, .{
.root_source_file = b.path(b.fmt("examples/{s}/App.zig", .{example.name})),
});
app_mod.addImport("mach", mach_mod);
const exe = addExecutable(b, .{
.name = example.name,
.root_source_file = b.path(b.fmt("examples/{s}/main.zig", .{example.name})),
.app = app_mod,
.target = target,
.optimize = optimize,
.win32_manifest = b.path("src/core/windows/win32.manifest"),
});
exe.root_module.addImport("mach", mach_mod);

for (example.deps) |d| {
switch (d) {
.assets => {
if (b.lazyDependency("mach_example_assets", .{
.target = target,
.optimize = optimize,
})) |dep| exe.root_module.addImport("assets", dep.module("mach-example-assets"));
})) |dep| app_mod.addImport("assets", dep.module("mach-example-assets"));
},
.freetype => {
if (b.lazyDependency("mach_freetype", .{
.target = target,
.optimize = optimize,
})) |dep| exe.root_module.addImport("freetype", dep.module("mach-freetype"));
})) |dep| app_mod.addImport("freetype", dep.module("mach-freetype"));
},
.zigimg => {
if (b.lazyDependency("zigimg", .{
.target = target,
.optimize = optimize,
})) |dep| exe.root_module.addImport("zigimg", dep.module("zigimg"));
})) |dep| app_mod.addImport("zigimg", dep.module("zigimg"));
},
}
}
Expand Down
6 changes: 6 additions & 0 deletions examples/core-transparent-window/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const gpu = mach.gpu;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
@This(),
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit };
Expand Down
6 changes: 6 additions & 0 deletions examples/core-triangle/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const gpu = mach.gpu;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit };
Expand Down
22 changes: 0 additions & 22 deletions examples/core-triangle/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/custom-renderer/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const Vec3 = math.Vec3;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
App,
@import("Renderer.zig"),
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .deinit, .tick };
Expand Down
23 changes: 0 additions & 23 deletions examples/custom-renderer/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/glyphs/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const Mat4x4 = math.Mat4x4;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.gfx.Sprite,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .deinit, .tick };
Expand Down
23 changes: 0 additions & 23 deletions examples/glyphs/main.zig

This file was deleted.

9 changes: 9 additions & 0 deletions examples/hardware-check/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ const Mat4x4 = math.Mat4x4;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.gfx.Sprite,
mach.gfx.Text,
mach.Audio,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit, .deinit2, .audioStateChange };
Expand Down
25 changes: 0 additions & 25 deletions examples/hardware-check/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/piano/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ const sysaudio = mach.sysaudio;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.Audio,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit, .audioStateChange };
Expand Down
23 changes: 0 additions & 23 deletions examples/piano/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/play-opus/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const sysaudio = mach.sysaudio;

pub const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.Audio,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit, .audioStateChange };
Expand Down
23 changes: 0 additions & 23 deletions examples/play-opus/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/sprite/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const Mat4x4 = math.Mat4x4;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.gfx.Sprite,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit };
Expand Down
23 changes: 0 additions & 23 deletions examples/sprite/main.zig

This file was deleted.

7 changes: 7 additions & 0 deletions examples/text/App.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ const Mat4x4 = math.Mat4x4;

const App = @This();

// The set of Mach modules our application may use.
pub const Modules = mach.Modules(.{
mach.Core,
mach.gfx.Text,
App,
});

pub const mach_module = .app;

pub const mach_systems = .{ .main, .init, .tick, .deinit };
Expand Down
23 changes: 0 additions & 23 deletions examples/text/main.zig

This file was deleted.

Loading

0 comments on commit b14f8e6

Please sign in to comment.