2022-12-31 06:40:03 +00:00
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
pub const Backend = enum {
|
|
|
|
no_backend,
|
|
|
|
glfw_wgpu,
|
2024-01-16 23:53:07 +00:00
|
|
|
glfw_opengl3,
|
2024-03-22 14:49:07 +00:00
|
|
|
glfw_dx12,
|
2022-12-31 06:40:03 +00:00
|
|
|
win32_dx12,
|
2024-07-04 00:55:17 +00:00
|
|
|
glfw,
|
2024-01-23 18:49:56 +00:00
|
|
|
mach_glfw_opengl3,
|
2022-12-31 06:40:03 +00:00
|
|
|
};
|
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
pub fn build(b: *std.Build) void {
|
|
|
|
const optimize = b.standardOptimizeOption(.{});
|
|
|
|
const target = b.standardTargetOptions(.{});
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
const options = .{
|
|
|
|
.backend = b.option(Backend, "backend", "Backend to build (default: no_backend)") orelse .no_backend,
|
|
|
|
.shared = b.option(
|
|
|
|
bool,
|
|
|
|
"shared",
|
|
|
|
"Bulid as a shared library",
|
|
|
|
) orelse false,
|
|
|
|
.with_implot = b.option(
|
|
|
|
bool,
|
|
|
|
"with_implot",
|
|
|
|
"Build with bundled implot source",
|
|
|
|
) orelse true,
|
2024-07-04 00:55:17 +00:00
|
|
|
.with_te = b.option(
|
|
|
|
bool,
|
|
|
|
"with_te",
|
|
|
|
"Build with bundled test engine support",
|
|
|
|
) orelse false,
|
|
|
|
.use_wchar32 = b.option(
|
|
|
|
bool,
|
|
|
|
"use_wchar32",
|
|
|
|
"Extended unicode support",
|
|
|
|
) orelse false,
|
2024-03-22 14:49:07 +00:00
|
|
|
};
|
2023-03-27 00:24:04 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
const options_step = b.addOptions();
|
|
|
|
inline for (std.meta.fields(@TypeOf(options))) |field| {
|
|
|
|
options_step.addOption(field.type, field.name, @field(options, field.name));
|
2023-03-27 00:24:04 +00:00
|
|
|
}
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
const options_module = options_step.createModule();
|
|
|
|
|
|
|
|
_ = b.addModule("root", .{
|
2024-07-04 00:55:17 +00:00
|
|
|
.root_source_file = b.path("src/gui.zig"),
|
2024-01-23 18:21:58 +00:00
|
|
|
.imports = &.{
|
2024-03-22 14:49:07 +00:00
|
|
|
.{ .name = "zgui_options", .module = options_module },
|
2023-02-07 23:23:40 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
const cflags = &.{"-fno-sanitize=undefined"};
|
|
|
|
|
|
|
|
const imgui = if (options.shared) blk: {
|
2023-03-27 00:24:04 +00:00
|
|
|
const lib = b.addSharedLibrary(.{
|
2024-03-22 14:49:07 +00:00
|
|
|
.name = "imgui",
|
2023-03-27 00:24:04 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
|
|
|
|
2024-01-23 18:21:58 +00:00
|
|
|
if (target.result.os.tag == .windows) {
|
2023-03-27 00:24:04 +00:00
|
|
|
lib.defineCMacro("IMGUI_API", "__declspec(dllexport)");
|
|
|
|
lib.defineCMacro("IMPLOT_API", "__declspec(dllexport)");
|
|
|
|
lib.defineCMacro("ZGUI_API", "__declspec(dllexport)");
|
|
|
|
}
|
|
|
|
|
2024-01-23 18:21:58 +00:00
|
|
|
if (target.result.os.tag == .macos) {
|
2024-01-16 23:53:07 +00:00
|
|
|
lib.linker_allow_shlib_undefined = true;
|
|
|
|
}
|
|
|
|
|
2023-03-27 00:24:04 +00:00
|
|
|
break :blk lib;
|
|
|
|
} else b.addStaticLibrary(.{
|
2024-03-22 14:49:07 +00:00
|
|
|
.name = "imgui",
|
2023-03-27 00:24:04 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
b.installArtifact(imgui);
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-07-04 00:55:17 +00:00
|
|
|
imgui.addIncludePath(b.path("libs"));
|
|
|
|
imgui.addIncludePath(b.path("libs/imgui"));
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.linkLibC();
|
|
|
|
if (target.result.abi != .msvc)
|
|
|
|
imgui.linkLibCpp();
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFile(.{
|
2024-07-04 00:55:17 +00:00
|
|
|
.file = b.path("src/zgui.cpp"),
|
2023-08-06 20:47:17 +00:00
|
|
|
.flags = cflags,
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFiles(.{
|
|
|
|
.files = &.{
|
|
|
|
"libs/imgui/imgui.cpp",
|
|
|
|
"libs/imgui/imgui_widgets.cpp",
|
|
|
|
"libs/imgui/imgui_tables.cpp",
|
|
|
|
"libs/imgui/imgui_draw.cpp",
|
|
|
|
"libs/imgui/imgui_demo.cpp",
|
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
if (options.with_implot) {
|
|
|
|
imgui.defineCMacro("ZGUI_IMPLOT", "1");
|
|
|
|
imgui.addCSourceFiles(.{
|
2024-01-16 23:53:07 +00:00
|
|
|
.files = &.{
|
2024-03-22 14:49:07 +00:00
|
|
|
"libs/imgui/implot_demo.cpp",
|
|
|
|
"libs/imgui/implot.cpp",
|
|
|
|
"libs/imgui/implot_items.cpp",
|
2024-01-16 23:53:07 +00:00
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
|
|
|
} else {
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.defineCMacro("ZGUI_IMPLOT", "0");
|
2023-08-06 20:47:17 +00:00
|
|
|
}
|
2022-12-31 06:40:03 +00:00
|
|
|
|
2024-07-04 00:55:17 +00:00
|
|
|
if (options.use_wchar32) {
|
|
|
|
imgui.defineCMacro("IMGUI_USE_WCHAR32", "1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.with_te) {
|
|
|
|
imgui.defineCMacro("ZGUI_TE", "1");
|
|
|
|
|
|
|
|
imgui.defineCMacro("IMGUI_ENABLE_TEST_ENGINE", null);
|
|
|
|
imgui.defineCMacro("IMGUI_TEST_ENGINE_ENABLE_COROUTINE_STDTHREAD_IMPL", "1");
|
|
|
|
|
|
|
|
imgui.addIncludePath(b.path("libs/imgui_test_engine/"));
|
|
|
|
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_capture_tool.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_context.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_coroutine.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_engine.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_exporters.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_perftool.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_ui.cpp"), .flags = cflags });
|
|
|
|
imgui.addCSourceFile(.{ .file = b.path("libs/imgui_test_engine/imgui_te_utils.cpp"), .flags = cflags });
|
|
|
|
|
|
|
|
// TODO: Workaround because zig on win64 doesn have phtreads
|
|
|
|
// TODO: Implement corutine in zig can solve this
|
|
|
|
if (target.result.os.tag == .windows) {
|
|
|
|
const src: []const []const u8 = &.{
|
|
|
|
"libs/winpthreads/src/nanosleep.c",
|
|
|
|
"libs/winpthreads/src/cond.c",
|
|
|
|
"libs/winpthreads/src/barrier.c",
|
|
|
|
"libs/winpthreads/src/misc.c",
|
|
|
|
"libs/winpthreads/src/clock.c",
|
|
|
|
"libs/winpthreads/src/libgcc/dll_math.c",
|
|
|
|
"libs/winpthreads/src/spinlock.c",
|
|
|
|
"libs/winpthreads/src/thread.c",
|
|
|
|
"libs/winpthreads/src/mutex.c",
|
|
|
|
"libs/winpthreads/src/sem.c",
|
|
|
|
"libs/winpthreads/src/sched.c",
|
|
|
|
"libs/winpthreads/src/ref.c",
|
|
|
|
"libs/winpthreads/src/rwlock.c",
|
|
|
|
};
|
|
|
|
|
|
|
|
const winpthreads = b.addStaticLibrary(.{
|
|
|
|
.name = "winpthreads",
|
|
|
|
.optimize = optimize,
|
|
|
|
.target = target,
|
|
|
|
});
|
|
|
|
winpthreads.want_lto = false;
|
|
|
|
winpthreads.root_module.sanitize_c = false;
|
|
|
|
if (optimize == .Debug or optimize == .ReleaseSafe)
|
|
|
|
winpthreads.bundle_compiler_rt = true
|
|
|
|
else
|
|
|
|
winpthreads.root_module.strip = true;
|
|
|
|
winpthreads.addCSourceFiles(.{ .files = src, .flags = &.{
|
|
|
|
"-Wall",
|
|
|
|
"-Wextra",
|
|
|
|
} });
|
|
|
|
winpthreads.defineCMacro("__USE_MINGW_ANSI_STDIO", "1");
|
|
|
|
winpthreads.addIncludePath(b.path("libs/winpthreads/include"));
|
|
|
|
winpthreads.addIncludePath(b.path("libs/winpthreads/src"));
|
|
|
|
winpthreads.linkLibC();
|
|
|
|
b.installArtifact(winpthreads);
|
|
|
|
imgui.linkLibrary(winpthreads);
|
|
|
|
imgui.addSystemIncludePath(b.path("libs/winpthreads/include"));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
imgui.defineCMacro("ZGUI_TE", "0");
|
|
|
|
}
|
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
switch (options.backend) {
|
2022-12-31 06:40:03 +00:00
|
|
|
.glfw_wgpu => {
|
2024-01-23 18:21:58 +00:00
|
|
|
const zglfw = b.dependency("zglfw", .{});
|
|
|
|
const zgpu = b.dependency("zgpu", .{});
|
2024-07-04 00:55:17 +00:00
|
|
|
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
|
|
|
|
imgui.addIncludePath(zgpu.path("libs/dawn/include"));
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFiles(.{
|
2024-01-16 23:53:07 +00:00
|
|
|
.files = &.{
|
2024-03-22 14:49:07 +00:00
|
|
|
"libs/imgui/backends/imgui_impl_glfw.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_wgpu.cpp",
|
2024-01-16 23:53:07 +00:00
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
.glfw_opengl3 => {
|
2024-01-23 18:21:58 +00:00
|
|
|
const zglfw = b.dependency("zglfw", .{});
|
2024-07-04 00:55:17 +00:00
|
|
|
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFiles(.{
|
2024-01-16 23:53:07 +00:00
|
|
|
.files = &.{
|
2024-03-22 14:49:07 +00:00
|
|
|
"libs/imgui/backends/imgui_impl_glfw.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_opengl3.cpp",
|
2024-01-16 23:53:07 +00:00
|
|
|
},
|
|
|
|
.flags = &(cflags.* ++ .{"-DIMGUI_IMPL_OPENGL_LOADER_CUSTOM"}),
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
},
|
2024-03-22 14:49:07 +00:00
|
|
|
.glfw_dx12 => {
|
|
|
|
const zglfw = b.dependency("zglfw", .{});
|
2024-07-04 00:55:17 +00:00
|
|
|
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFiles(.{
|
|
|
|
.files = &.{
|
|
|
|
"libs/imgui/backends/imgui_impl_glfw.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_dx12.cpp",
|
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
|
|
|
imgui.linkSystemLibrary("d3dcompiler_47");
|
|
|
|
},
|
2024-01-23 18:49:56 +00:00
|
|
|
.mach_glfw_opengl3 => {
|
|
|
|
imgui.addCSourceFiles(.{
|
|
|
|
.files = &.{
|
|
|
|
"libs/imgui/backends/imgui_impl_glfw.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_opengl3.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_opengl3_loader.h",
|
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
|
|
|
},
|
2022-12-31 06:40:03 +00:00
|
|
|
.win32_dx12 => {
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.addCSourceFiles(.{
|
2024-01-16 23:53:07 +00:00
|
|
|
.files = &.{
|
2024-03-22 14:49:07 +00:00
|
|
|
"libs/imgui/backends/imgui_impl_win32.cpp",
|
|
|
|
"libs/imgui/backends/imgui_impl_dx12.cpp",
|
2024-01-16 23:53:07 +00:00
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
2024-03-22 14:49:07 +00:00
|
|
|
imgui.linkSystemLibrary("d3dcompiler_47");
|
|
|
|
imgui.linkSystemLibrary("dwmapi");
|
2024-07-04 00:55:17 +00:00
|
|
|
switch (target.result.abi) {
|
|
|
|
.msvc => imgui.linkSystemLibrary("Gdi32"),
|
|
|
|
.gnu => imgui.linkSystemLibrary("gdi32"),
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
.glfw => {
|
|
|
|
const zglfw = b.dependency("zglfw", .{});
|
|
|
|
imgui.addIncludePath(zglfw.path("libs/glfw/include"));
|
|
|
|
imgui.addCSourceFiles(.{
|
|
|
|
.files = &.{
|
|
|
|
"libs/imgui/backends/imgui_impl_glfw.cpp",
|
|
|
|
},
|
|
|
|
.flags = cflags,
|
|
|
|
});
|
2022-12-31 06:40:03 +00:00
|
|
|
},
|
|
|
|
.no_backend => {},
|
|
|
|
}
|
2023-03-27 00:24:04 +00:00
|
|
|
|
2024-01-16 23:53:07 +00:00
|
|
|
const test_step = b.step("test", "Run zgui tests");
|
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
const tests = b.addTest(.{
|
|
|
|
.name = "zgui-tests",
|
2024-07-04 00:55:17 +00:00
|
|
|
.root_source_file = b.path("src/gui.zig"),
|
2024-01-16 23:53:07 +00:00
|
|
|
.target = target,
|
|
|
|
.optimize = optimize,
|
|
|
|
});
|
2024-03-22 14:49:07 +00:00
|
|
|
b.installArtifact(tests);
|
|
|
|
|
|
|
|
tests.root_module.addImport("zgui_options", options_module);
|
|
|
|
tests.linkLibrary(imgui);
|
2023-03-27 00:24:04 +00:00
|
|
|
|
2024-03-22 14:49:07 +00:00
|
|
|
test_step.dependOn(&b.addRunArtifact(tests).step);
|
2022-12-31 06:40:03 +00:00
|
|
|
}
|