2022-12-31 06:40:03 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub const Backend = enum {
|
|
|
|
no_backend,
|
|
|
|
glfw_wgpu,
|
|
|
|
win32_dx12,
|
2022-12-31 06:49:27 +00:00
|
|
|
glfw_opengl3,
|
|
|
|
sdl2_opengl3,
|
2022-12-31 06:40:03 +00:00
|
|
|
};
|
|
|
|
|
2023-02-07 23:23:40 +00:00
|
|
|
pub const Options = struct {
|
2022-12-31 06:40:03 +00:00
|
|
|
backend: Backend,
|
2023-03-27 00:24:04 +00:00
|
|
|
shared: bool = false,
|
2022-12-31 06:40:03 +00:00
|
|
|
};
|
|
|
|
|
2023-02-07 23:23:40 +00:00
|
|
|
pub const Package = struct {
|
|
|
|
options: Options,
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui: *std.Build.Module,
|
|
|
|
zgui_options: *std.Build.Module,
|
|
|
|
zgui_c_cpp: *std.Build.CompileStep,
|
|
|
|
|
|
|
|
pub fn link(pkg: Package, exe: *std.Build.CompileStep) void {
|
|
|
|
exe.linkLibrary(pkg.zgui_c_cpp);
|
|
|
|
exe.addModule("zgui", pkg.zgui);
|
|
|
|
}
|
2022-12-31 06:40:03 +00:00
|
|
|
};
|
|
|
|
|
2023-02-07 23:23:40 +00:00
|
|
|
pub fn package(
|
|
|
|
b: *std.Build,
|
2023-03-27 00:24:04 +00:00
|
|
|
target: std.zig.CrossTarget,
|
|
|
|
optimize: std.builtin.Mode,
|
2023-02-07 23:23:40 +00:00
|
|
|
args: struct {
|
|
|
|
options: Options,
|
|
|
|
},
|
|
|
|
) Package {
|
|
|
|
const step = b.addOptions();
|
|
|
|
step.addOption(Backend, "backend", args.options.backend);
|
2023-03-27 00:24:04 +00:00
|
|
|
step.addOption(bool, "shared", args.options.shared);
|
2023-02-07 23:23:40 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
const zgui_options = step.createModule();
|
2023-02-07 23:23:40 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
const zgui = b.createModule(.{
|
2023-02-07 23:23:40 +00:00
|
|
|
.source_file = .{ .path = thisDir() ++ "/src/main.zig" },
|
|
|
|
.dependencies = &.{
|
2023-03-27 00:24:04 +00:00
|
|
|
.{ .name = "zgui_options", .module = zgui_options },
|
2023-02-07 23:23:40 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
const zgui_c_cpp = if (args.options.shared) blk: {
|
|
|
|
const lib = b.addSharedLibrary(.{
|
|
|
|
.name = "zgui",
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
|
|
|
|
lib.install();
|
|
|
|
if (target.isWindows()) {
|
|
|
|
lib.defineCMacro("IMGUI_API", "__declspec(dllexport)");
|
|
|
|
lib.defineCMacro("IMPLOT_API", "__declspec(dllexport)");
|
|
|
|
lib.defineCMacro("ZGUI_API", "__declspec(dllexport)");
|
|
|
|
}
|
|
|
|
|
|
|
|
break :blk lib;
|
|
|
|
} else b.addStaticLibrary(.{
|
|
|
|
.name = "zgui",
|
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addIncludePath(thisDir() ++ "/libs");
|
|
|
|
zgui_c_cpp.addIncludePath(thisDir() ++ "/libs/imgui");
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.linkLibC();
|
|
|
|
zgui_c_cpp.linkLibCpp();
|
2022-12-31 06:40:03 +00:00
|
|
|
|
|
|
|
const cflags = &.{"-fno-sanitize=undefined"};
|
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/src/zgui.cpp", cflags);
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/imgui.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/imgui_widgets.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/imgui_tables.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/imgui_draw.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/imgui_demo.cpp", cflags);
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/implot_demo.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/implot.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/implot_items.cpp", cflags);
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
switch (args.options.backend) {
|
2022-12-31 06:40:03 +00:00
|
|
|
.glfw_wgpu => {
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addIncludePath(thisDir() ++ "/../zglfw/libs/glfw/include");
|
|
|
|
zgui_c_cpp.addIncludePath(thisDir() ++ "/../zgpu/libs/dawn/include");
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_glfw.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_wgpu.cpp", cflags);
|
2022-12-31 06:40:03 +00:00
|
|
|
},
|
2022-12-31 06:49:27 +00:00
|
|
|
.glfw_opengl3 => {
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_glfw.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_opengl3.cpp", cflags);
|
|
|
|
},
|
2022-12-31 06:40:03 +00:00
|
|
|
.win32_dx12 => {
|
2023-03-27 00:24:04 +00:00
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_win32.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_dx12.cpp", cflags);
|
|
|
|
zgui_c_cpp.linkSystemLibraryName("d3dcompiler_47");
|
|
|
|
zgui_c_cpp.linkSystemLibraryName("dwmapi");
|
2022-12-31 06:40:03 +00:00
|
|
|
},
|
2022-12-31 06:49:27 +00:00
|
|
|
.sdl2_opengl3 => {
|
2023-03-27 03:54:53 +00:00
|
|
|
if (target.isWindows()) blk: {
|
|
|
|
// see: https://github.com/MasterQ32/SDL.zig/blob/37f4ba9e31bea895fa19ef8b90d1f51111e52e67/Sdk.zig#L182-L199
|
2023-03-11 04:04:35 +00:00
|
|
|
|
2023-03-27 03:54:53 +00:00
|
|
|
zgui_c_cpp.addVcpkgPaths(if (args.options.shared) .dynamic else .static) catch break :blk;
|
2023-03-11 04:04:35 +00:00
|
|
|
|
2023-03-27 03:54:53 +00:00
|
|
|
const vcpkg_triplet = zgui_c_cpp.target.vcpkgTriplet(b.allocator, if (args.options.shared) .Dynamic else .Static) catch |e| {
|
|
|
|
std.debug.panic("failed to determing vcpkg triplet: {}", .{e});
|
|
|
|
};
|
|
|
|
defer b.allocator.free(vcpkg_triplet);
|
2023-03-11 04:04:35 +00:00
|
|
|
|
2023-03-27 03:54:53 +00:00
|
|
|
const include_path = b.pathJoin(&.{ b.vcpkg_root.found, "installed", vcpkg_triplet, "include", "SDL2" });
|
|
|
|
zgui_c_cpp.include_dirs.append(.{ .raw_path = include_path }) catch @panic("out of memory");
|
2023-03-11 04:04:35 +00:00
|
|
|
|
2023-03-27 03:54:53 +00:00
|
|
|
const bin_path = zgui_c_cpp.vcpkg_bin_path orelse @panic("vcpkg paths were found, so vcpkg_bin_path should be set ");
|
|
|
|
const dll_path = std.fs.path.join(b.allocator, &.{ bin_path, "SDL2.dll" }) catch @panic("out of memory");
|
|
|
|
|
|
|
|
std.fs.cwd().access(dll_path, .{}) catch break :blk;
|
|
|
|
b.installBinFile(dll_path, "SDL2.dll");
|
|
|
|
}
|
2023-03-11 04:04:35 +00:00
|
|
|
|
|
|
|
zgui_c_cpp.linkSystemLibrary("SDL2");
|
|
|
|
|
2022-12-31 06:49:27 +00:00
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_sdl.cpp", cflags);
|
|
|
|
zgui_c_cpp.addCSourceFile(thisDir() ++ "/libs/imgui/backends/imgui_impl_opengl3.cpp", cflags);
|
|
|
|
},
|
2022-12-31 06:40:03 +00:00
|
|
|
.no_backend => {},
|
|
|
|
}
|
2023-03-27 00:24:04 +00:00
|
|
|
|
|
|
|
return .{
|
|
|
|
.options = args.options,
|
|
|
|
.zgui = zgui,
|
|
|
|
.zgui_options = zgui_options,
|
|
|
|
.zgui_c_cpp = zgui_c_cpp,
|
|
|
|
};
|
2022-12-31 06:40:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
pub fn build(_: *std.Build) void {}
|
|
|
|
|
2022-12-31 06:40:03 +00:00
|
|
|
inline fn thisDir() []const u8 {
|
|
|
|
return comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
|
|
}
|