lore-forge/desktop/build.zig

135 lines
5.9 KiB
Zig

// Builds the desktop client: forge-desktop (C++, Dear ImGui on vendored UHDK) with Zig, and
// forge-service (Rust) with Cargo, installing both into zig-out/bin/ so the GUI can start the
// service beside it.
//
// zig build build and install both, and liblore (fetched by
// scripts/fetch-lore.sh) beside the service
// zig build run run the GUI (it starts the service when none is running)
// zig build test the GUI's --test smoke run (private service), then cargo test
// zig build check compile the GUI only (any -Dtarget)
//
// Cargo builds only for the host. For another -Dtarget, build the GUI here and the service
// with Cargo for that target, then put forge-service beside forge-desktop.
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const native = target.query.isNative();
const with_service = b.option(bool, "service", "Build forge-service with Cargo (default: when building for the host)") orelse native;
const uhdk = b.dependency("uhdk", .{ .target = target, .optimize = optimize });
const gui = b.addExecutable(.{
.name = "forge-desktop",
.root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
.link_libcpp = true,
}),
});
if (target.result.os.tag == .windows) gui.subsystem = .windows;
gui.root_module.addIncludePath(uhdk.path("include"));
gui.root_module.addIncludePath(b.path("vendor/imgui"));
gui.root_module.addIncludePath(b.path("gui/uhdk_imgui"));
gui.root_module.addCSourceFiles(.{
.files = &.{
"gui/src/main.cpp",
"gui/src/explorer.cpp",
"gui/src/model.cpp",
"gui/src/json.cpp",
"gui/src/platform.cpp",
"gui/src/png.cpp",
"gui/src/service_client.cpp",
"gui/src/theme.cpp",
},
.flags = &.{ "-std=c++17", "-Wall", "-Wextra", "-Werror" },
});
// The font and licence texts, through #embed: a C23 feature Clang allows in C++ as an
// extension, hence no -Werror here.
gui.root_module.addCSourceFile(.{
.file = b.path("gui/src/embedded.cpp"),
.flags = &.{ "-std=c++17", "-Wno-c23-extensions" },
});
gui.root_module.addCSourceFiles(.{
.files = &.{
"gui/uhdk_imgui/imgui_impl_uhdk.cpp",
"vendor/imgui/imgui.cpp",
"vendor/imgui/imgui_draw.cpp",
"vendor/imgui/imgui_tables.cpp",
"vendor/imgui/imgui_widgets.cpp",
"vendor/imgui/backends/imgui_impl_opengl3.cpp",
},
.flags = &.{"-std=c++17"},
});
gui.root_module.linkLibrary(uhdk.artifact("uhdk"));
const check = b.step("check", "Compile forge-desktop without installing or running it");
check.dependOn(&gui.step);
const install_gui = b.addInstallArtifact(gui, .{});
b.getInstallStep().dependOn(&install_gui.step);
// Cargo keeps its own incremental state in target/, so it runs every time.
const service_exe = if (target.result.os.tag == .windows) "forge-service.exe" else "forge-service";
var service_built: ?std.Build.LazyPath = null;
var cargo_step: ?*std.Build.Step = null;
var liblore: ?std.Build.LazyPath = null;
if (with_service) {
const release = optimize != .debug;
const cargo = b.addSystemCommand(&.{ "cargo", "build", "--quiet", "--package", "forge-service", "--manifest-path" });
cargo.addFileArg(b.path("Cargo.toml"));
if (release) cargo.addArg("--release");
cargo.has_side_effects = true;
const built = b.path(b.fmt("target/{s}/{s}", .{ if (release) "release" else "debug", service_exe }));
const install = b.addInstallBinFile(built, service_exe);
install.step.dependOn(&cargo.step);
b.getInstallStep().dependOn(&install.step);
service_built = built;
cargo_step = &cargo.step;
// The service loads liblore from beside itself. The pinned release is fetched once into
// ../.lore/ and copied from there; Windows builds of the service are done by hand.
if (target.result.os.tag != .windows) {
const library = if (target.result.os.tag == .macos) "liblore.dylib" else "liblore.so";
const fetch = b.addSystemCommand(&.{"../scripts/fetch-lore.sh"});
fetch.setCwd(b.path("."));
fetch.addArg("--liblore-to");
const fetched = fetch.addOutputFileArg(library);
fetch.addFileInput(b.path("../lore.lock"));
const install_lib = b.addInstallBinFile(fetched, library);
b.getInstallStep().dependOn(&install_lib.step);
liblore = fetched;
}
}
// `run` and `test` run the GUI from Zig's cache, so they name the service explicitly.
const run = b.addRunArtifact(gui);
if (service_built) |built| {
run.addArg("--service");
run.addFileArg(built);
run.step.dependOn(cargo_step.?);
if (liblore) |library| run.addPrefixedFileArg("--liblore=", library);
}
run.addPassthruArgs();
b.step("run", "Run forge-desktop").dependOn(&run.step);
const test_step = b.step("test", "Run forge-desktop --test against a private service, then the Cargo tests");
if (service_built) |built| {
const smoke = b.addRunArtifact(gui);
smoke.addArg("--service");
smoke.addFileArg(built);
if (liblore) |library| smoke.addPrefixedFileArg("--liblore=", library);
smoke.addArg("--test");
smoke.has_side_effects = true;
smoke.step.dependOn(cargo_step.?);
test_step.dependOn(&smoke.step);
const cargo_test = b.addSystemCommand(&.{ "cargo", "test", "--quiet", "--manifest-path" });
cargo_test.addFileArg(b.path("Cargo.toml"));
cargo_test.has_side_effects = true;
test_step.dependOn(&cargo_test.step);
}
}