doggo/build.zig

111 lines
4.3 KiB
Zig
Raw Normal View History

2022-01-21 15:12:40 +01:00
const std = @import("std");
2024-07-04 15:09:23 +02:00
const Build = std.Build;
const OptimizeMode = std.builtin.OptimizeMode;
const ResolvedTarget = Build.ResolvedTarget;
const Dependency = Build.Dependency;
const sokol = @import("sokol");
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
pub fn build(b: *Build) !void {
2022-01-21 15:12:40 +01:00
const target = b.standardTargetOptions(.{});
2024-07-04 15:09:23 +02:00
const optimize = b.standardOptimizeOption(.{});
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
// note that the sokol dependency is built with `.with_imgui_sokol = true`
const dep_sokol = b.dependency("sokol", .{
2024-05-04 19:03:50 +02:00
.target = target,
2024-07-04 15:09:23 +02:00
.optimize = optimize,
.with_sokol_imgui = true,
});
const dep_cimgui = b.dependency("cimgui", .{
.target = target,
.optimize = optimize,
2024-05-04 19:03:50 +02:00
});
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
// inject the cimgui header search path into the sokol C library compile step
const cimgui_root = dep_cimgui.namedWriteFiles("cimgui").getDirectory();
dep_sokol.artifact("sokol_clib").addIncludePath(cimgui_root);
2022-03-24 13:25:40 +01:00
2024-07-04 15:09:23 +02:00
// from here on different handling for native vs wasm builds
if (target.result.isWasm()) {
try buildWasm(b, target, optimize, dep_sokol, dep_cimgui);
} else {
try buildNative(b, target, optimize, dep_sokol, dep_cimgui);
}
2024-07-17 11:14:41 +02:00
try buildTui(b, target, optimize);
2024-07-04 15:09:23 +02:00
}
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
fn buildNative(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, dep_sokol: *Dependency, dep_cimgui: *Dependency) !void {
const demo = b.addExecutable(.{
.name = "demo",
2024-07-17 11:14:41 +02:00
.root_source_file = b.path("src/gui/main.zig"),
2024-07-04 15:09:23 +02:00
.target = target,
.optimize = optimize,
});
demo.root_module.addImport("sokol", dep_sokol.module("sokol"));
demo.root_module.addImport("cimgui", dep_cimgui.module("cimgui"));
b.installArtifact(demo);
2024-07-17 11:14:41 +02:00
b.step("run-gui", "Run demo").dependOn(&b.addRunArtifact(demo).step);
2024-07-04 15:09:23 +02:00
}
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
fn buildWasm(b: *Build, target: ResolvedTarget, optimize: OptimizeMode, dep_sokol: *Dependency, dep_cimgui: *Dependency) !void {
// build the main file into a library, this is because the WASM 'exe'
// needs to be linked in a separate build step with the Emscripten linker
const demo = b.addStaticLibrary(.{
.name = "demo",
2024-07-17 11:14:41 +02:00
.root_source_file = b.path("src/gui/main.zig"),
2024-05-04 19:03:50 +02:00
.target = target,
2024-07-04 15:09:23 +02:00
.optimize = optimize,
2024-05-04 19:03:50 +02:00
});
2024-07-04 15:09:23 +02:00
demo.root_module.addImport("sokol", dep_sokol.module("sokol"));
demo.root_module.addImport("cimgui", dep_cimgui.module("cimgui"));
2022-01-21 15:12:40 +01:00
2024-07-04 15:09:23 +02:00
// get the Emscripten SDK dependency from the sokol dependency
const dep_emsdk = dep_sokol.builder.dependency("emsdk", .{});
// need to inject the Emscripten system header include path into
// the cimgui C library otherwise the C/C++ code won't find
// C stdlib headers
const emsdk_incl_path = dep_emsdk.path("upstream/emscripten/cache/sysroot/include");
dep_cimgui.artifact("cimgui_clib").addSystemIncludePath(emsdk_incl_path);
// all C libraries need to depend on the sokol library, when building for
// WASM this makes sure that the Emscripten SDK has been setup before
// C compilation is attempted (since the sokol C library depends on the
// Emscripten SDK setup step)
dep_cimgui.artifact("cimgui_clib").step.dependOn(&dep_sokol.artifact("sokol_clib").step);
// create a build step which invokes the Emscripten linker
const link_step = try sokol.emLinkStep(b, .{
.lib_main = demo,
.target = target,
.optimize = optimize,
.emsdk = dep_emsdk,
.use_webgl2 = true,
.use_emmalloc = true,
.use_filesystem = false,
.shell_file_path = dep_sokol.path("src/sokol/web/shell.html").getPath(b),
});
// ...and a special run step to start the web build output via 'emrun'
const run = sokol.emRunStep(b, .{ .name = "demo", .emsdk = dep_emsdk });
run.step.dependOn(&link_step.step);
2024-07-17 11:14:41 +02:00
b.step("run-gui", "Run demo").dependOn(&run.step);
}
fn buildTui(b: *Build, target: ResolvedTarget, optimize: OptimizeMode) !void {
const tui = b.addExecutable(.{
.name = "demo",
.root_source_file = b.path("src/tui/main.zig"),
.target = target,
.optimize = optimize,
});
2024-10-04 14:32:48 +02:00
const vaxis_dep = b.dependency("vaxis", .{
.target = target,
.optimize = optimize,
});
tui.root_module.addImport("vaxis", vaxis_dep.module("vaxis"));
2024-07-17 11:14:41 +02:00
b.installArtifact(tui);
b.step("run-tui", "Run TUI").dependOn(&b.addRunArtifact(tui).step);
2022-01-21 15:12:40 +01:00
}