Skip to content

Commit b14f8e6

Browse files
committed
build: add @import("mach").addExecutable helper
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]>
1 parent 2410814 commit b14f8e6

File tree

19 files changed

+101
-201
lines changed

19 files changed

+101
-201
lines changed

build.zig

+37-7
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,34 @@ const Example = struct {
389389
run_step: *std.Build.Step = undefined,
390390
};
391391

392+
pub fn addExecutable(
393+
mach_builder: *std.Build,
394+
options: struct {
395+
name: []const u8,
396+
app: *std.Build.Module,
397+
target: std.Build.ResolvedTarget,
398+
optimize: std.builtin.OptimizeMode,
399+
},
400+
) *std.Build.Step.Compile {
401+
const entrypoint_mod = mach_builder.addModule(
402+
mach_builder.fmt("{s}-entrypoint", .{options.name}),
403+
.{
404+
.root_source_file = mach_builder.path("src/entrypoint/main.zig"),
405+
.optimize = options.optimize,
406+
.target = options.target,
407+
},
408+
);
409+
entrypoint_mod.addImport("app", options.app);
410+
411+
return mach_builder.addExecutable(.{
412+
.name = options.name,
413+
.root_module = entrypoint_mod,
414+
415+
// Win32 manifest file for DPI-awareness configuration
416+
.win32_manifest = mach_builder.path("src/core/windows/win32.manifest"),
417+
});
418+
}
419+
392420
fn buildExamples(
393421
b: *std.Build,
394422
optimize: std.builtin.OptimizeMode,
@@ -397,34 +425,36 @@ fn buildExamples(
397425
examples: []Example,
398426
) void {
399427
for (examples) |*example| {
400-
const exe = b.addExecutable(.{
428+
const app_mod = b.addModule(example.name, .{
429+
.root_source_file = b.path(b.fmt("examples/{s}/App.zig", .{example.name})),
430+
});
431+
app_mod.addImport("mach", mach_mod);
432+
const exe = addExecutable(b, .{
401433
.name = example.name,
402-
.root_source_file = b.path(b.fmt("examples/{s}/main.zig", .{example.name})),
434+
.app = app_mod,
403435
.target = target,
404436
.optimize = optimize,
405-
.win32_manifest = b.path("src/core/windows/win32.manifest"),
406437
});
407-
exe.root_module.addImport("mach", mach_mod);
408438

409439
for (example.deps) |d| {
410440
switch (d) {
411441
.assets => {
412442
if (b.lazyDependency("mach_example_assets", .{
413443
.target = target,
414444
.optimize = optimize,
415-
})) |dep| exe.root_module.addImport("assets", dep.module("mach-example-assets"));
445+
})) |dep| app_mod.addImport("assets", dep.module("mach-example-assets"));
416446
},
417447
.freetype => {
418448
if (b.lazyDependency("mach_freetype", .{
419449
.target = target,
420450
.optimize = optimize,
421-
})) |dep| exe.root_module.addImport("freetype", dep.module("mach-freetype"));
451+
})) |dep| app_mod.addImport("freetype", dep.module("mach-freetype"));
422452
},
423453
.zigimg => {
424454
if (b.lazyDependency("zigimg", .{
425455
.target = target,
426456
.optimize = optimize,
427-
})) |dep| exe.root_module.addImport("zigimg", dep.module("zigimg"));
457+
})) |dep| app_mod.addImport("zigimg", dep.module("zigimg"));
428458
},
429459
}
430460
}

examples/core-transparent-window/App.zig

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const gpu = mach.gpu;
44

55
const App = @This();
66

7+
// The set of Mach modules our application may use.
8+
pub const Modules = mach.Modules(.{
9+
mach.Core,
10+
@This(),
11+
});
12+
713
pub const mach_module = .app;
814

915
pub const mach_systems = .{ .main, .init, .tick, .deinit };

examples/core-triangle/App.zig

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ const gpu = mach.gpu;
44

55
const App = @This();
66

7+
// The set of Mach modules our application may use.
8+
pub const Modules = mach.Modules(.{
9+
mach.Core,
10+
App,
11+
});
12+
713
pub const mach_module = .app;
814

915
pub const mach_systems = .{ .main, .init, .tick, .deinit };

examples/core-triangle/main.zig

-22
This file was deleted.

examples/custom-renderer/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ const Vec3 = math.Vec3;
99

1010
const App = @This();
1111

12+
// The set of Mach modules our application may use.
13+
pub const Modules = mach.Modules(.{
14+
mach.Core,
15+
App,
16+
@import("Renderer.zig"),
17+
});
18+
1219
pub const mach_module = .app;
1320

1421
pub const mach_systems = .{ .main, .init, .deinit, .tick };

examples/custom-renderer/main.zig

-23
This file was deleted.

examples/glyphs/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const Mat4x4 = math.Mat4x4;
1616

1717
const App = @This();
1818

19+
// The set of Mach modules our application may use.
20+
pub const Modules = mach.Modules(.{
21+
mach.Core,
22+
mach.gfx.Sprite,
23+
App,
24+
});
25+
1926
pub const mach_module = .app;
2027

2128
pub const mach_systems = .{ .main, .init, .deinit, .tick };

examples/glyphs/main.zig

-23
This file was deleted.

examples/hardware-check/App.zig

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ const Mat4x4 = math.Mat4x4;
1414

1515
const App = @This();
1616

17+
// The set of Mach modules our application may use.
18+
pub const Modules = mach.Modules(.{
19+
mach.Core,
20+
mach.gfx.Sprite,
21+
mach.gfx.Text,
22+
mach.Audio,
23+
App,
24+
});
25+
1726
pub const mach_module = .app;
1827

1928
pub const mach_systems = .{ .main, .init, .tick, .deinit, .deinit2, .audioStateChange };

examples/hardware-check/main.zig

-25
This file was deleted.

examples/piano/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ const sysaudio = mach.sysaudio;
1818

1919
const App = @This();
2020

21+
// The set of Mach modules our application may use.
22+
pub const Modules = mach.Modules(.{
23+
mach.Core,
24+
mach.Audio,
25+
App,
26+
});
27+
2128
pub const mach_module = .app;
2229

2330
pub const mach_systems = .{ .main, .init, .tick, .deinit, .audioStateChange };

examples/piano/main.zig

-23
This file was deleted.

examples/play-opus/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const sysaudio = mach.sysaudio;
1313

1414
pub const App = @This();
1515

16+
// The set of Mach modules our application may use.
17+
pub const Modules = mach.Modules(.{
18+
mach.Core,
19+
mach.Audio,
20+
App,
21+
});
22+
1623
pub const mach_module = .app;
1724

1825
pub const mach_systems = .{ .main, .init, .tick, .deinit, .audioStateChange };

examples/play-opus/main.zig

-23
This file was deleted.

examples/sprite/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ const Mat4x4 = math.Mat4x4;
1515

1616
const App = @This();
1717

18+
// The set of Mach modules our application may use.
19+
pub const Modules = mach.Modules(.{
20+
mach.Core,
21+
mach.gfx.Sprite,
22+
App,
23+
});
24+
1825
pub const mach_module = .app;
1926

2027
pub const mach_systems = .{ .main, .init, .tick, .deinit };

examples/sprite/main.zig

-23
This file was deleted.

examples/text/App.zig

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ const Mat4x4 = math.Mat4x4;
1616

1717
const App = @This();
1818

19+
// The set of Mach modules our application may use.
20+
pub const Modules = mach.Modules(.{
21+
mach.Core,
22+
mach.gfx.Text,
23+
App,
24+
});
25+
1926
pub const mach_module = .app;
2027

2128
pub const mach_systems = .{ .main, .init, .tick, .deinit };

examples/text/main.zig

-23
This file was deleted.

0 commit comments

Comments
 (0)